diff --git a/Makefile b/Makefile index 4f0fb69..82d5ead 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ # Parameter for current board -BOARD?=galp3-c +#BOARD?=system76/galp3-c +BOARD?=arduino/atmega2560 # Set build directory BUILD=build/$(BOARD) diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..4e8b774 --- /dev/null +++ b/doc/README.md @@ -0,0 +1,5 @@ +# System76 EC documetation + +## TODO + +- Define what functions are required in each board, ec, and arch diff --git a/src/arch/8051/arch.mk b/src/arch/8051/arch.mk index 1f81633..37b9505 100644 --- a/src/arch/8051/arch.mk +++ b/src/arch/8051/arch.mk @@ -9,12 +9,12 @@ sim: $(BUILD)/ec.rom --no-default-features \ -- $< -# Convert from Intel Hex format to binary +# Convert from Intel Hex file to binary file $(BUILD)/ec.rom: $(BUILD)/ec.ihx @mkdir -p $(@D) makebin -p < $< > $@ -# Link object files into Intel Hex format +# Link object files into Intel Hex file $(BUILD)/ec.ihx: $(OBJ) @mkdir -p $(@D) $(CC) -o $@ $^ diff --git a/src/arch/avr/arch.mk b/src/arch/avr/arch.mk new file mode 100644 index 0000000..b754757 --- /dev/null +++ b/src/arch/avr/arch.mk @@ -0,0 +1,26 @@ +CC=avr-gcc -mmcu=$(EC) +OBJ=$(patsubst src/%.c,$(BUILD)/%.o,$(SRC)) + +# Run EC rom in simulator +sim: $(BUILD)/ec.elf + simavr $< --mcu $(EC) + +# Convert from Intel Hex file to binary file +$(BUILD)/ec.rom: $(BUILD)/ec.ihx + @mkdir -p $(@D) + makebin -p < $< > $@ + +# Convert from ELF file to Intel Hex file +$(BUILD)/ec.ihx: $(OBJ) + @mkdir -p $(@D) + avr-objcopy -j .text -j .data -O ihex $< $@ + +# Link object files into ELF file +$(BUILD)/ec.elf: $(OBJ) + @mkdir -p $(@D) + $(CC) -o $@ $^ + +# Compile C files into object files +$(BUILD)/%.o: src/%.c $(INCLUDE) + @mkdir -p $(@D) + $(CC) -o $@ -c $< diff --git a/src/board/arduino/atmega2560/board.mk b/src/board/arduino/atmega2560/board.mk new file mode 100644 index 0000000..dcdaa57 --- /dev/null +++ b/src/board/arduino/atmega2560/board.mk @@ -0,0 +1 @@ +EC=atmega2560 diff --git a/src/board/arduino/atmega2560/include/cpu.h b/src/board/arduino/atmega2560/include/cpu.h new file mode 100644 index 0000000..9cca1db --- /dev/null +++ b/src/board/arduino/atmega2560/include/cpu.h @@ -0,0 +1,6 @@ +#ifndef CPU_H +#define CPU_H + +#define F_CPU 16000000ULL + +#endif // CPU_H diff --git a/src/board/arduino/atmega2560/include/stdio.h b/src/board/arduino/atmega2560/include/stdio.h new file mode 100644 index 0000000..15a7669 --- /dev/null +++ b/src/board/arduino/atmega2560/include/stdio.h @@ -0,0 +1,7 @@ +#ifndef STDIO_H +#define STDIO_H + +extern struct Uart * stdio_uart; +void stdio_init(int num, unsigned long baud); + +#endif // STDIO_H diff --git a/src/board/arduino/atmega2560/include/uart.h b/src/board/arduino/atmega2560/include/uart.h new file mode 100644 index 0000000..1983531 --- /dev/null +++ b/src/board/arduino/atmega2560/include/uart.h @@ -0,0 +1,31 @@ +#ifndef UART_H +#define UART_H + +#include + +struct Uart { + volatile uint8_t * a; + volatile uint8_t * b; + volatile uint8_t * c; + volatile uint8_t * data; + volatile uint8_t * baud_l; + volatile uint8_t * baud_h; + uint8_t a_read; + uint8_t a_write; + uint8_t a_init; + uint8_t b_init; + uint8_t c_init; +}; + +void uart_init(struct Uart * uart, unsigned long baud); + +int uart_count(); +struct Uart * uart_new(int num); + +unsigned char uart_can_read(struct Uart * uart); +unsigned char uart_can_write(struct Uart * uart); + +unsigned char uart_read(struct Uart * uart); +void uart_write(struct Uart * uart, unsigned char data); + +#endif // UART_H diff --git a/src/board/arduino/atmega2560/main.c b/src/board/arduino/atmega2560/main.c new file mode 100644 index 0000000..efe1110 --- /dev/null +++ b/src/board/arduino/atmega2560/main.c @@ -0,0 +1,14 @@ +#include + +#include "include/stdio.h" + +void init(void) { + stdio_init(0, 9600); +} + +void main(void) { + init(); + + printf("Hello from System76 EC for the Arduino Mega 2560!\n"); + for (;;) {} +} diff --git a/src/board/arduino/atmega2560/stdio.c b/src/board/arduino/atmega2560/stdio.c new file mode 100644 index 0000000..0a0cbf3 --- /dev/null +++ b/src/board/arduino/atmega2560/stdio.c @@ -0,0 +1,29 @@ +#include + +#include "include/stdio.h" +#include "include/uart.h" + +struct Uart * stdio_uart = NULL; + +int stdio_get(FILE * stream) { + return (int)uart_read(stdio_uart); +} + +int stdio_put(char data, FILE * stream) { + if (data == '\n') { + uart_write(stdio_uart, '\r'); + } + uart_write(stdio_uart, (unsigned char)data); + return 0; +} + +FILE stdio_file = FDEV_SETUP_STREAM(stdio_put, stdio_get, _FDEV_SETUP_RW); + +void stdio_init(int num, unsigned long baud) { + struct Uart * uart = uart_new(num); + if(uart != NULL) { + uart_init(uart, baud); + stdio_uart = uart; + stdin = stdout = stderr = &stdio_file; + } +} diff --git a/src/board/arduino/atmega2560/uart.c b/src/board/arduino/atmega2560/uart.c new file mode 100644 index 0000000..8cb4edb --- /dev/null +++ b/src/board/arduino/atmega2560/uart.c @@ -0,0 +1,74 @@ +#include +#include + +#include "include/cpu.h" +#include "include/uart.h" + +#define UART(N) \ + { \ + &UCSR ## N ## A, \ + &UCSR ## N ## B, \ + &UCSR ## N ## C, \ + &UDR ## N, \ + &UBRR ## N ## L, \ + &UBRR ## N ## H, \ + _BV(RXC ## N), \ + _BV(UDRE ## N), \ + 0, \ + _BV(RXEN ## N) | _BV(TXEN ## N), \ + _BV(UCSZ ## N ## 1) | _BV(UCSZ ## N ## 0) \ + } + +#if defined(__AVR_ATmega32U4__) + static struct Uart UARTS[] = { + UART(1) + }; +#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + static struct Uart UARTS[] = { + UART(0), + UART(1), + UART(2), + UART(3) + }; +#else + #error "Could not find UART definitions" +#endif + +int uart_count() { + return sizeof(UARTS)/sizeof(struct Uart); +} + +struct Uart * uart_new(int num) { + if (num < uart_count()) { + return &UARTS[num]; + } else { + return NULL; + } +} + +void uart_init(struct Uart * uart, unsigned long baud) { + unsigned long baud_prescale = (F_CPU / (baud * 16UL)) - 1; + *(uart->baud_h) = (uint8_t)(baud_prescale>>8); + *(uart->baud_l) = (uint8_t)(baud_prescale); + *(uart->a) = uart->a_init; + *(uart->b) = uart->b_init; + *(uart->c) = uart->c_init; +} + +unsigned char uart_can_read(struct Uart * uart) { + return (*(uart->a)) & uart->a_read; +} + +unsigned char uart_read(struct Uart * uart) { + while (!uart_can_read(uart)) ; + return *(uart->data); +} + +unsigned char uart_can_write(struct Uart * uart) { + return (*(uart->a)) & uart->a_write; +} + +void uart_write(struct Uart * uart, unsigned char data) { + while (!uart_can_write(uart)) ; + *(uart->data) = data; +} diff --git a/src/acpi.c b/src/board/system76/galp3-c/acpi.c similarity index 100% rename from src/acpi.c rename to src/board/system76/galp3-c/acpi.c diff --git a/src/board/galp3-c/board.mk b/src/board/system76/galp3-c/board.mk similarity index 100% rename from src/board/galp3-c/board.mk rename to src/board/system76/galp3-c/board.mk diff --git a/src/delay.c b/src/board/system76/galp3-c/delay.c similarity index 100% rename from src/delay.c rename to src/board/system76/galp3-c/delay.c diff --git a/src/gctrl.c b/src/board/system76/galp3-c/gctrl.c similarity index 100% rename from src/gctrl.c rename to src/board/system76/galp3-c/gctrl.c diff --git a/src/gpio.c b/src/board/system76/galp3-c/gpio.c similarity index 100% rename from src/gpio.c rename to src/board/system76/galp3-c/gpio.c diff --git a/src/include/acpi.h b/src/board/system76/galp3-c/include/acpi.h similarity index 100% rename from src/include/acpi.h rename to src/board/system76/galp3-c/include/acpi.h diff --git a/src/include/delay.h b/src/board/system76/galp3-c/include/delay.h similarity index 100% rename from src/include/delay.h rename to src/board/system76/galp3-c/include/delay.h diff --git a/src/include/gctrl.h b/src/board/system76/galp3-c/include/gctrl.h similarity index 100% rename from src/include/gctrl.h rename to src/board/system76/galp3-c/include/gctrl.h diff --git a/src/include/gpio.h b/src/board/system76/galp3-c/include/gpio.h similarity index 100% rename from src/include/gpio.h rename to src/board/system76/galp3-c/include/gpio.h diff --git a/src/include/kbc.h b/src/board/system76/galp3-c/include/kbc.h similarity index 100% rename from src/include/kbc.h rename to src/board/system76/galp3-c/include/kbc.h diff --git a/src/include/kbscan.h b/src/board/system76/galp3-c/include/kbscan.h similarity index 100% rename from src/include/kbscan.h rename to src/board/system76/galp3-c/include/kbscan.h diff --git a/src/include/pin.h b/src/board/system76/galp3-c/include/pin.h similarity index 100% rename from src/include/pin.h rename to src/board/system76/galp3-c/include/pin.h diff --git a/src/include/pmc.h b/src/board/system76/galp3-c/include/pmc.h similarity index 100% rename from src/include/pmc.h rename to src/board/system76/galp3-c/include/pmc.h diff --git a/src/include/ps2.h b/src/board/system76/galp3-c/include/ps2.h similarity index 100% rename from src/include/ps2.h rename to src/board/system76/galp3-c/include/ps2.h diff --git a/src/include/reset.h b/src/board/system76/galp3-c/include/reset.h similarity index 100% rename from src/include/reset.h rename to src/board/system76/galp3-c/include/reset.h diff --git a/src/include/timer.h b/src/board/system76/galp3-c/include/timer.h similarity index 100% rename from src/include/timer.h rename to src/board/system76/galp3-c/include/timer.h diff --git a/src/kbc.c b/src/board/system76/galp3-c/kbc.c similarity index 100% rename from src/kbc.c rename to src/board/system76/galp3-c/kbc.c diff --git a/src/kbscan.c b/src/board/system76/galp3-c/kbscan.c similarity index 100% rename from src/kbscan.c rename to src/board/system76/galp3-c/kbscan.c diff --git a/src/main.c b/src/board/system76/galp3-c/main.c similarity index 100% rename from src/main.c rename to src/board/system76/galp3-c/main.c diff --git a/src/pin.c b/src/board/system76/galp3-c/pin.c similarity index 100% rename from src/pin.c rename to src/board/system76/galp3-c/pin.c diff --git a/src/pmc.c b/src/board/system76/galp3-c/pmc.c similarity index 100% rename from src/pmc.c rename to src/board/system76/galp3-c/pmc.c diff --git a/src/ps2.c b/src/board/system76/galp3-c/ps2.c similarity index 100% rename from src/ps2.c rename to src/board/system76/galp3-c/ps2.c diff --git a/src/reset.c b/src/board/system76/galp3-c/reset.c similarity index 100% rename from src/reset.c rename to src/board/system76/galp3-c/reset.c diff --git a/src/signature.c b/src/board/system76/galp3-c/signature.c similarity index 100% rename from src/signature.c rename to src/board/system76/galp3-c/signature.c diff --git a/src/stdio.c b/src/board/system76/galp3-c/stdio.c similarity index 100% rename from src/stdio.c rename to src/board/system76/galp3-c/stdio.c diff --git a/src/timer.c b/src/board/system76/galp3-c/timer.c similarity index 100% rename from src/timer.c rename to src/board/system76/galp3-c/timer.c diff --git a/src/ec/atmega2560/ec.mk b/src/ec/atmega2560/ec.mk new file mode 100644 index 0000000..40fb5ae --- /dev/null +++ b/src/ec/atmega2560/ec.mk @@ -0,0 +1 @@ +ARCH=avr