Organize into arch, board, and ec modules

This commit is contained in:
Jeremy Soller
2019-09-29 20:13:03 -06:00
parent 9d056547e6
commit ded5181926
47 changed files with 518 additions and 486 deletions

18
src/arch/8051/delay.c Normal file
View File

@@ -0,0 +1,18 @@
#include <8051.h>
#include <arch/delay.h>
#include <arch/timer.h>
// One millisecond in ticks is determined as follows:
// 9.2 MHz is the clock rate
// The timer divider is 12
// The timer rate is 12 / 9.2 MHz = 1.304 us
// The ticks are 1000 ms / (1.304 us) = 766.667
// 65536 - 766.667 = 64769.33
void delay_ms(int ms) {
for (int i = 0; i < ms; i++) {
timer_mode_1(64769);
timer_wait();
timer_stop();
}
}

View File

@@ -0,0 +1,6 @@
#ifndef _ARCH_DELAY_H
#define _ARCH_DELAY_H
void delay_ms(int ms);
#endif // _ARCH_DELAY_H

View File

@@ -0,0 +1,6 @@
#ifndef _ARCH_RESET_H
#define _ARCH_RESET_H
void reset(void);
#endif // _ARCH_RESET_H

View File

@@ -0,0 +1,8 @@
#ifndef _ARCH_TIMER_H
#define _ARCH_TIMER_H
void timer_mode_1(int value);
void timer_wait(void);
void timer_stop(void);
#endif // _ARCH_TIMER_H

5
src/arch/8051/reset.c Normal file
View File

@@ -0,0 +1,5 @@
#include <arch/reset.h>
void reset(void) {
__asm__("ljmp 0");
}

20
src/arch/8051/timer.c Normal file
View File

@@ -0,0 +1,20 @@
#include <8051.h>
#include <arch/timer.h>
void timer_mode_1(int value) {
timer_stop();
TMOD = 0x01;
TH0 = (unsigned char)(value >> 8);
TL0 = (unsigned char)value;
TR0 = 1;
}
void timer_wait(void) {
while (TF0 == 0) {}
}
void timer_stop(void) {
TR0 = 0;
TF0 = 0;
}

View File

@@ -0,0 +1,34 @@
#ifndef _ARCH_UART_H
#define _ARCH_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);
extern struct Uart * uart_stdio;
void uart_stdio_init(int num, unsigned long baud);
#endif // _ARCH_UART_H

99
src/arch/avr/uart.c Normal file
View File

@@ -0,0 +1,99 @@
#include <stdio.h>
#include <avr/io.h>
#include <arch/uart.h>
#include <board/cpu.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;
}
struct Uart * uart_stdio = NULL;
int uart_stdio_get(FILE * stream) {
return (int)uart_read(uart_stdio);
}
int uart_stdio_put(char data, FILE * stream) {
if (data == '\n') {
uart_write(uart_stdio, '\r');
}
uart_write(uart_stdio, (unsigned char)data);
return 0;
}
FILE uart_stdio_file = FDEV_SETUP_STREAM(uart_stdio_put, uart_stdio_get, _FDEV_SETUP_RW);
void uart_stdio_init(int num, unsigned long baud) {
struct Uart * uart = uart_new(num);
if(uart != NULL) {
uart_init(uart, baud);
uart_stdio = uart;
stdin = stdout = stderr = &uart_stdio_file;
}
}