#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/irq.h"
// Declare the main assembly code entry point.
void main_asm();
// Initialise a GPIO pin – see SDK for detail on gpio_init()
void asm_gpio_init(uint pin) {
gpio_init(pin);
}
// Set direction of a GPIO pin – see SDK for detail on gpio_set_dir()
void asm_gpio_set_dir(uint pin, bool out) {
gpio_set_dir(pin, out);
}
// Get the value of a GPIO pin – see SDK for detail on gpio_get()
bool asm_gpio_get(uint pin) {
return gpio_get(pin);
}
// Set the value of a GPIO pin – see SDK for detail on gpio_put()
void asm_gpio_put(uint pin, bool value) {
gpio_put(pin, value);
}
// Enable falling-edge interrupt – see SDK for detail on gpio_set_irq_enabled()
void asm_gpio_set_irq(uint pin) {
gpio_set_irq_enabled(pin, GPIO_IRQ_EDGE_FALL, true);
}
// GPIO Interrupt Handler for all buttons
void gpio_callback(uint gpio, uint32_t events) {
printf("Interrupt on GPIO %d!\n", gpio);
}
// Setup GPIO and interrupts
void setup_gpio() {
// LED Setup
asm_gpio_init(25); // Built-in LED on GP25
asm_gpio_set_dir(25, true); // Set GP25 as output
// Button Setup
asm_gpio_init(20); // Button for halving rate
asm_gpio_set_dir(20, false); // Set GP20 as input
asm_gpio_set_irq(20); // Enable interrupt for GP20
asm_gpio_init(21); // Button for toggling LED state
asm_gpio_set_dir(21, false); // Set GP21 as input
asm_gpio_set_irq(21); // Enable interrupt for GP21
asm_gpio_init(22); // Button for doubling rate
asm_gpio_set_dir(22, false); // Set GP22 as input
asm_gpio_set_irq(22); // Enable interrupt for GP22
// Attach interrupt handler
gpio_set_irq_enabled_with_callback(20, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
gpio_set_irq_enabled_with_callback(21, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
gpio_set_irq_enabled_with_callback(22, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
}
// Main entry point of the application
int main() {
stdio_init_all(); // Initialise all basic IO
printf("Assignment #1...\n"); // Basic print to console
setup_gpio(); // Setup GPIO pins and interrupts
main_asm(); // Jump into the ASM code
return 0; // Application return code
}