Add gpio.{h, c}

This commit is contained in:
Alexander Heldt
2024-07-28 11:42:38 +02:00
parent 9b1e1b6f21
commit a8a5e21b77
14 changed files with 8228 additions and 721 deletions

16
src/gpio.c Normal file
View File

@@ -0,0 +1,16 @@
#include <inttypes.h>
#include <stdbool.h>
#include "gpio.h"
void gpio_set_mode(uint16_t pin, GPIO_MODE mode) {
struct gpio *gpio = GPIO(PINPORT(pin)); // GPIO port address
int pn = PINNUM(pin); // Pin number
gpio->MODER &= ~(0x0011 << (pn * 2)); // Clear existing setting. Each pin uses 2 bits
gpio->MODER |= (mode & 0b011) << (pn * 2); // Set new mode. Each pin uses 2 bits
}
void gpio_write(uint16_t pin, bool val) {
struct gpio *gpio = GPIO(PINPORT(pin));
gpio->BSRR = (0b0011 << PINNUM(pin)) << (val ? 0 : 16);
}