Add AVR GPIO

This commit is contained in:
Jeremy Soller 2019-09-29 21:47:37 -06:00
parent 5c3fb062fd
commit 01bae12644
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
3 changed files with 65 additions and 0 deletions

33
src/arch/avr/gpio.c Normal file
View File

@ -0,0 +1,33 @@
#include <arch/gpio.h>
bool gpio_get_dir(struct Gpio * gpio) {
if (*gpio->ddr & gpio->value) {
return true;
} else {
return false;
}
}
void gpio_set_dir(struct Gpio * gpio, bool value) {
if (value) {
*gpio->ddr |= gpio->value;
} else {
*gpio->ddr &= ~gpio->value;
}
}
bool gpio_get(struct Gpio * gpio) {
if (*gpio->pin & gpio->value) {
return true;
} else {
return false;
}
}
void gpio_set(struct Gpio * gpio, bool value) {
if (value) {
*gpio->port |= gpio->value;
} else {
*gpio->port &= ~gpio->value;
}
}

View File

@ -0,0 +1,27 @@
#ifndef _ARCH_GPIO_H
#define _ARCH_GPIO_H
#include <avr/io.h>
#include <stdbool.h>
#include <stdint.h>
struct Gpio {
volatile uint8_t * pin;
volatile uint8_t * ddr;
volatile uint8_t * port;
uint8_t value;
};
#define GPIO(BLOCK, NUMBER) { \
.pin = &PIN ## BLOCK, \
.ddr = &DDR ## BLOCK, \
.port = &PORT ## BLOCK, \
.value = (1 << NUMBER), \
}
bool gpio_get(struct Gpio * gpio);
void gpio_set(struct Gpio * gpio, bool value);
bool gpio_get_dir(struct Gpio * gpio);
void gpio_set_dir(struct Gpio * gpio, bool value);
#endif // _ARCH_GPIO_H

View File

@ -1,14 +1,19 @@
#include <stdio.h>
#include <arch/gpio.h>
#include <arch/uart.h>
void init(void) {
uart_stdio_init(0, 9600);
}
struct Gpio LED = GPIO(B, 5);
int main(void) {
init();
gpio_set_dir(&LED, true);
gpio_set(&LED, false);
printf("Hello from System76 EC for the Arduino Uno!\n");
for (;;) {
int c = getchar();