-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize to allow compiling and running AVR firmware
- Loading branch information
Showing
38 changed files
with
198 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# System76 EC documetation | ||
|
||
## TODO | ||
|
||
- Define what functions are required in each board, ec, and arch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
EC=atmega2560 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#ifndef CPU_H | ||
#define CPU_H | ||
|
||
#define F_CPU 16000000ULL | ||
|
||
#endif // CPU_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef UART_H | ||
#define UART_H | ||
|
||
#include <stdint.h> | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <stdio.h> | ||
|
||
#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 (;;) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <stdio.h> | ||
|
||
#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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <stdio.h> | ||
#include <avr/io.h> | ||
|
||
#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; | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ARCH=avr |