Add AVR GPIO
This commit is contained in:
		
							
								
								
									
										33
									
								
								src/arch/avr/gpio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								src/arch/avr/gpio.c
									
									
									
									
									
										Normal 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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								src/arch/avr/include/arch/gpio.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/arch/avr/include/arch/gpio.h
									
									
									
									
									
										Normal 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
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user