Reorganize to allow compiling and running AVR firmware

This commit is contained in:
Jeremy Soller
2019-09-29 17:13:31 -06:00
parent 608326af30
commit b04352cb63
38 changed files with 198 additions and 3 deletions

View File

@ -0,0 +1 @@
EC=atmega2560

View File

@ -0,0 +1,6 @@
#ifndef CPU_H
#define CPU_H
#define F_CPU 16000000ULL
#endif // CPU_H

View File

@ -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

View File

@ -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

View File

@ -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 (;;) {}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -0,0 +1,15 @@
#include <stdio.h>
#include "include/acpi.h"
uint8_t acpi_read(uint8_t addr) {
uint8_t data = 0;
//TODO
printf("acpi_read %02X = %02X\n", addr, data);
return data;
}
void acpi_write(uint8_t addr, uint8_t data) {
//TODO
printf("acpi_write %02X = %02X\n", addr, data);
}

View File

@ -0,0 +1,18 @@
#include <8051.h>
#include "include/delay.h"
#include "include/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,7 @@
#include "include/gctrl.h"
void gctrl_init(void) {
SPCTRL1 = 0x03;
BADRSEL = 0;
RSTS = 0x84;
}

View File

@ -0,0 +1,121 @@
#include <stdio.h>
#include "include/gpio.h"
void gpio_init() {
// Enable LPC reset on GPD2
GCR = 0x04;
// Set GPIO data
GPDRA = 0;
GPDRB = (1 << 0);
GPDRC = 0;
GPDRD = (1 << 5) | (1 << 4) | (1 << 3);
GPDRE = 0;
GPDRF = (1 << 7) | (1 << 6);
GPDRG = 0;
GPDRH = 0;
GPDRI = 0;
GPDRJ = 0;
// Set GPIO control
GPCRA0 = 0x80;
GPCRA1 = 0x00;
GPCRA2 = 0x00;
GPCRA3 = 0x80;
GPCRA4 = 0x40;
GPCRA5 = 0x44;
GPCRA6 = 0x44;
GPCRA7 = 0x44;
GPCRB0 = 0x44;
GPCRB1 = 0x44;
GPCRB2 = 0x84;
GPCRB3 = 0x00;
GPCRB4 = 0x00;
GPCRB5 = 0x44;
GPCRB6 = 0x84;
GPCRB7 = 0x80;
GPCRC0 = 0x80;
GPCRC1 = 0x84;
GPCRC2 = 0x84;
GPCRC3 = 0x84;
GPCRC4 = 0x44;
GPCRC5 = 0x44;
GPCRC6 = 0x40;
GPCRC7 = 0x44;
GPCRD0 = 0x84;
GPCRD1 = 0x84;
GPCRD2 = 0x00;
GPCRD3 = 0x80;
GPCRD4 = 0x80;
GPCRD5 = 0x44;
GPCRD6 = 0x80;
GPCRD7 = 0x80;
GPCRE0 = 0x44;
GPCRE1 = 0x44;
GPCRE2 = 0x80;
GPCRE3 = 0x40;
GPCRE4 = 0x42;
GPCRE5 = 0x40;
GPCRE6 = 0x44;
GPCRE7 = 0x44;
GPCRF0 = 0x80;
GPCRF1 = 0x44;
GPCRF2 = 0x84;
GPCRF3 = 0x44;
GPCRF4 = 0x80;
GPCRF5 = 0x80;
GPCRF6 = 0x00;
GPCRF7 = 0x80;
GPCRG0 = 0x44;
GPCRG1 = 0x44;
GPCRG2 = 0x40;
GPCRG3 = 0x00;
GPCRG4 = 0x00;
GPCRG5 = 0x00;
GPCRG6 = 0x44;
GPCRG7 = 0x00;
GPCRH0 = 0x00;
GPCRH1 = 0x80;
GPCRH2 = 0x44;
GPCRH3 = 0x44;
GPCRH4 = 0x80;
GPCRH5 = 0x80;
GPCRH6 = 0x80;
GPCRH7 = 0x80;
GPCRI0 = 0x00;
GPCRI1 = 0x00;
GPCRI2 = 0x80;
GPCRI3 = 0x00;
GPCRI4 = 0x00;
GPCRI5 = 0x80;
GPCRI6 = 0x80;
GPCRI7 = 0x80;
GPCRJ0 = 0x82;
GPCRJ1 = 0x80;
GPCRJ2 = 0x40;
GPCRJ3 = 0x80;
GPCRJ4 = 0x44;
GPCRJ5 = 0x40;
GPCRJ6 = 0x44;
GPCRJ7 = 0x80;
}
void gpio_debug_bank(char bank, unsigned char data) {
for(char i = 0; i < 8; i++) {
printf("%c%d = %d\n", bank, i, (data >> i) & 1);
}
}
void gpio_debug(void) {
gpio_debug_bank('A', GPDRA);
gpio_debug_bank('B', GPDRB);
gpio_debug_bank('C', GPDRC);
gpio_debug_bank('D', GPDRD);
gpio_debug_bank('E', GPDRE);
gpio_debug_bank('F', GPDRF);
gpio_debug_bank('G', GPDRG);
gpio_debug_bank('H', GPDRH);
gpio_debug_bank('I', GPDRI);
gpio_debug_bank('J', GPDRJ);
}

View File

@ -0,0 +1,9 @@
#ifndef _ACPI_H_
#define _ACPI_H_
#include <stdint.h>
uint8_t acpi_read(uint8_t addr);
void acpi_write(uint8_t addr, uint8_t data);
#endif // _ACPI_H_

View File

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

View File

@ -0,0 +1,10 @@
#ifndef _GCTRL_H_
#define _GCTRL_H_
void gctrl_init(void);
__xdata volatile unsigned char __at(0x2006) RSTS;
__xdata volatile unsigned char __at(0x200A) BADRSEL;
__xdata volatile unsigned char __at(0x200D) SPCTRL1;
#endif // _GCTRL_H_

View File

@ -0,0 +1,143 @@
#ifndef _GPIO_H_
#define _GPIO_H_
void gpio_init(void);
void gpio_debug(void);
__xdata volatile unsigned char __at(0x1600) GCR;
__xdata volatile unsigned char __at(0x1601) GPDRA;
__xdata volatile unsigned char __at(0x1602) GPDRB;
__xdata volatile unsigned char __at(0x1603) GPDRC;
__xdata volatile unsigned char __at(0x1604) GPDRD;
__xdata volatile unsigned char __at(0x1605) GPDRE;
__xdata volatile unsigned char __at(0x1606) GPDRF;
__xdata volatile unsigned char __at(0x1607) GPDRG;
__xdata volatile unsigned char __at(0x1608) GPDRH;
__xdata volatile unsigned char __at(0x1609) GPDRI;
__xdata volatile unsigned char __at(0x160A) GPDRJ;
__xdata volatile unsigned char __at(0x160D) GPDRM;
__xdata volatile unsigned char __at(0x1661) GPDMRA;
__xdata volatile unsigned char __at(0x1662) GPDMRB;
__xdata volatile unsigned char __at(0x1663) GPDMRC;
__xdata volatile unsigned char __at(0x1664) GPDMRD;
__xdata volatile unsigned char __at(0x1665) GPDMRE;
__xdata volatile unsigned char __at(0x1666) GPDMRF;
__xdata volatile unsigned char __at(0x1667) GPDMRG;
__xdata volatile unsigned char __at(0x1668) GPDMRH;
__xdata volatile unsigned char __at(0x1669) GPDMRI;
__xdata volatile unsigned char __at(0x166A) GPDMRJ;
__xdata volatile unsigned char __at(0x166D) GPDMRM;
__xdata volatile unsigned char __at(0x1671) GPOTA;
__xdata volatile unsigned char __at(0x1672) GPOTB;
__xdata volatile unsigned char __at(0x1673) GPOTC;
__xdata volatile unsigned char __at(0x1674) GPOTD;
__xdata volatile unsigned char __at(0x1675) GPOTE;
__xdata volatile unsigned char __at(0x1676) GPOTF;
__xdata volatile unsigned char __at(0x1677) GPOTG;
__xdata volatile unsigned char __at(0x1678) GPOTH;
__xdata volatile unsigned char __at(0x1679) GPOTI;
__xdata volatile unsigned char __at(0x167A) GPOTJ;
// GPOTM does not exist
__xdata volatile unsigned char __at(0x1610) GPCRA0;
__xdata volatile unsigned char __at(0x1611) GPCRA1;
__xdata volatile unsigned char __at(0x1612) GPCRA2;
__xdata volatile unsigned char __at(0x1613) GPCRA3;
__xdata volatile unsigned char __at(0x1614) GPCRA4;
__xdata volatile unsigned char __at(0x1615) GPCRA5;
__xdata volatile unsigned char __at(0x1616) GPCRA6;
__xdata volatile unsigned char __at(0x1617) GPCRA7;
__xdata volatile unsigned char __at(0x1618) GPCRB0;
__xdata volatile unsigned char __at(0x1619) GPCRB1;
__xdata volatile unsigned char __at(0x161A) GPCRB2;
__xdata volatile unsigned char __at(0x161B) GPCRB3;
__xdata volatile unsigned char __at(0x161C) GPCRB4;
__xdata volatile unsigned char __at(0x161D) GPCRB5;
__xdata volatile unsigned char __at(0x161E) GPCRB6;
__xdata volatile unsigned char __at(0x161F) GPCRB7;
__xdata volatile unsigned char __at(0x1620) GPCRC0;
__xdata volatile unsigned char __at(0x1621) GPCRC1;
__xdata volatile unsigned char __at(0x1622) GPCRC2;
__xdata volatile unsigned char __at(0x1623) GPCRC3;
__xdata volatile unsigned char __at(0x1624) GPCRC4;
__xdata volatile unsigned char __at(0x1625) GPCRC5;
__xdata volatile unsigned char __at(0x1626) GPCRC6;
__xdata volatile unsigned char __at(0x1627) GPCRC7;
__xdata volatile unsigned char __at(0x1628) GPCRD0;
__xdata volatile unsigned char __at(0x1629) GPCRD1;
__xdata volatile unsigned char __at(0x162A) GPCRD2;
__xdata volatile unsigned char __at(0x162B) GPCRD3;
__xdata volatile unsigned char __at(0x162C) GPCRD4;
__xdata volatile unsigned char __at(0x162D) GPCRD5;
__xdata volatile unsigned char __at(0x162E) GPCRD6;
__xdata volatile unsigned char __at(0x162F) GPCRD7;
__xdata volatile unsigned char __at(0x1630) GPCRE0;
__xdata volatile unsigned char __at(0x1631) GPCRE1;
__xdata volatile unsigned char __at(0x1632) GPCRE2;
__xdata volatile unsigned char __at(0x1633) GPCRE3;
__xdata volatile unsigned char __at(0x1634) GPCRE4;
__xdata volatile unsigned char __at(0x1635) GPCRE5;
__xdata volatile unsigned char __at(0x1636) GPCRE6;
__xdata volatile unsigned char __at(0x1637) GPCRE7;
__xdata volatile unsigned char __at(0x1638) GPCRF0;
__xdata volatile unsigned char __at(0x1639) GPCRF1;
__xdata volatile unsigned char __at(0x163A) GPCRF2;
__xdata volatile unsigned char __at(0x163B) GPCRF3;
__xdata volatile unsigned char __at(0x163C) GPCRF4;
__xdata volatile unsigned char __at(0x163D) GPCRF5;
__xdata volatile unsigned char __at(0x163E) GPCRF6;
__xdata volatile unsigned char __at(0x163F) GPCRF7;
__xdata volatile unsigned char __at(0x1640) GPCRG0;
__xdata volatile unsigned char __at(0x1641) GPCRG1;
__xdata volatile unsigned char __at(0x1642) GPCRG2;
__xdata volatile unsigned char __at(0x1643) GPCRG3;
__xdata volatile unsigned char __at(0x1644) GPCRG4;
__xdata volatile unsigned char __at(0x1645) GPCRG5;
__xdata volatile unsigned char __at(0x1646) GPCRG6;
__xdata volatile unsigned char __at(0x1647) GPCRG7;
__xdata volatile unsigned char __at(0x1648) GPCRH0;
__xdata volatile unsigned char __at(0x1649) GPCRH1;
__xdata volatile unsigned char __at(0x164A) GPCRH2;
__xdata volatile unsigned char __at(0x164B) GPCRH3;
__xdata volatile unsigned char __at(0x164C) GPCRH4;
__xdata volatile unsigned char __at(0x164D) GPCRH5;
__xdata volatile unsigned char __at(0x164E) GPCRH6;
__xdata volatile unsigned char __at(0x164F) GPCRH7;
__xdata volatile unsigned char __at(0x1650) GPCRI0;
__xdata volatile unsigned char __at(0x1651) GPCRI1;
__xdata volatile unsigned char __at(0x1652) GPCRI2;
__xdata volatile unsigned char __at(0x1653) GPCRI3;
__xdata volatile unsigned char __at(0x1654) GPCRI4;
__xdata volatile unsigned char __at(0x1655) GPCRI5;
__xdata volatile unsigned char __at(0x1656) GPCRI6;
__xdata volatile unsigned char __at(0x1657) GPCRI7;
__xdata volatile unsigned char __at(0x1658) GPCRJ0;
__xdata volatile unsigned char __at(0x1659) GPCRJ1;
__xdata volatile unsigned char __at(0x165A) GPCRJ2;
__xdata volatile unsigned char __at(0x165B) GPCRJ3;
__xdata volatile unsigned char __at(0x165C) GPCRJ4;
__xdata volatile unsigned char __at(0x165D) GPCRJ5;
__xdata volatile unsigned char __at(0x165E) GPCRJ6;
__xdata volatile unsigned char __at(0x165F) GPCRJ7;
__xdata volatile unsigned char __at(0x16A0) GPCRM0;
__xdata volatile unsigned char __at(0x16A1) GPCRM1;
__xdata volatile unsigned char __at(0x16A2) GPCRM2;
__xdata volatile unsigned char __at(0x16A3) GPCRM3;
__xdata volatile unsigned char __at(0x16A4) GPCRM4;
__xdata volatile unsigned char __at(0x16A5) GPCRM5;
__xdata volatile unsigned char __at(0x16A6) GPCRM6;
#endif // _GPIO_H_

View File

@ -0,0 +1,43 @@
#ifndef _KBC_H_
#define _KBC_H_
#include <stdint.h>
void kbc_init(void);
struct Kbc {
// Control register
volatile uint8_t * control;
// Interrupt control register
volatile uint8_t * irq;
// Status register
volatile uint8_t * status;
// Keyboard out register
volatile uint8_t * keyboard_out;
// Mouse out register
volatile uint8_t * mouse_out;
// Data in register
volatile uint8_t * data_in;
};
extern __code struct Kbc KBC;
#define KBC_STS_OBF (1 << 0)
#define KBC_STS_IBF (1 << 1)
#define KBC_STS_CMD (1 << 3)
uint8_t kbc_status(struct Kbc * kbc);
uint8_t kbc_read(struct Kbc * kbc);
void kbc_keyboard(struct Kbc * kbc, uint8_t data);
void kbc_mouse(struct Kbc * kbc, uint8_t data);
void kbc_event(struct Kbc * kbc);
__xdata volatile uint8_t __at(0x1300) KBHICR;
__xdata volatile uint8_t __at(0x1302) KBIRQR;
__xdata volatile uint8_t __at(0x1304) KBHISR;
__xdata volatile uint8_t __at(0x1306) KBHIKDOR;
__xdata volatile uint8_t __at(0x1308) KBHIMDOR;
__xdata volatile uint8_t __at(0x130A) KBHIDIR;
#endif // _KBC_H_

View File

@ -0,0 +1,47 @@
#ifndef _KBSCAN_H_
#define _KBSCAN_H_
void kbscan_init(void);
__xdata volatile unsigned char __at(0x1D00) KSOL;
__xdata volatile unsigned char __at(0x1D01) KSOH1;
__xdata volatile unsigned char __at(0x1D02) KSOCTRL;
__xdata volatile unsigned char __at(0x1D03) KSOH2;
__xdata volatile unsigned char __at(0x1D04) KSI;
__xdata volatile unsigned char __at(0x1D05) KSICTRLR;
__xdata volatile unsigned char __at(0x1D06) KSIGCTRL;
__xdata volatile unsigned char __at(0x1D07) KSIGOEN;
__xdata volatile unsigned char __at(0x1D08) KSIGDAT;
__xdata volatile unsigned char __at(0x1D09) KSIGDMRR;
__xdata volatile unsigned char __at(0x1D0A) KSOHGCTRL;
__xdata volatile unsigned char __at(0x1D0B) KSOHGOEN;
__xdata volatile unsigned char __at(0x1D0C) KSOHGDMRR;
__xdata volatile unsigned char __at(0x1D0D) KSOLGCTRL;
__xdata volatile unsigned char __at(0x1D0E) KSOLGOEN;
__xdata volatile unsigned char __at(0x1D0F) KSOLGDMRR;
__xdata volatile unsigned char __at(0x1D10) KSO0LSDR;
__xdata volatile unsigned char __at(0x1D11) KSO1LSDR;
__xdata volatile unsigned char __at(0x1D12) KSO2LSDR;
__xdata volatile unsigned char __at(0x1D13) KSO3LSDR;
__xdata volatile unsigned char __at(0x1D14) KSO4LSDR;
__xdata volatile unsigned char __at(0x1D15) KSO5LSDR;
__xdata volatile unsigned char __at(0x1D16) KSO6LSDR;
__xdata volatile unsigned char __at(0x1D17) KSO7LSDR;
__xdata volatile unsigned char __at(0x1D18) KSO8LSDR;
__xdata volatile unsigned char __at(0x1D19) KSO9LSDR;
__xdata volatile unsigned char __at(0x1D1A) KSO10LSDR;
__xdata volatile unsigned char __at(0x1D1B) KSO11LSDR;
__xdata volatile unsigned char __at(0x1D1C) KSO12LSDR;
__xdata volatile unsigned char __at(0x1D1D) KSO13LSDR;
__xdata volatile unsigned char __at(0x1D1E) KSO14LSDR;
__xdata volatile unsigned char __at(0x1D1F) KSO15LSDR;
__xdata volatile unsigned char __at(0x1D20) KSO16LSDR;
__xdata volatile unsigned char __at(0x1D21) KSO17LSDR;
__xdata volatile unsigned char __at(0x1D22) SDC1R;
__xdata volatile unsigned char __at(0x1D23) SDC2R;
__xdata volatile unsigned char __at(0x1D24) SDC3R;
__xdata volatile unsigned char __at(0x1D25) SDSR;
#endif // _KBSCAN_H_

View File

@ -0,0 +1,25 @@
#ifndef _PIN_H_
#define _PIN_H_
#include <stdbool.h>
#include "gpio.h"
struct Pin {
__xdata volatile unsigned char * data;
__xdata volatile unsigned char * mirror;
__xdata volatile unsigned char * control;
unsigned char value;
};
#define PIN(BLOCK, NUMBER) { \
.data = &GPDR ## BLOCK, \
.mirror = &GPDMR ## BLOCK, \
.control = &GPCR ## BLOCK ## NUMBER, \
.value = (1 << NUMBER), \
}
bool pin_get(struct Pin * pin);
void pin_set(struct Pin * pin, bool value);
#endif // _PIN_H_

View File

@ -0,0 +1,42 @@
#ifndef _PMC_H_
#define _PMC_H_
#include <stdint.h>
void pmc_init(void);
struct Pmc {
// Status register
volatile uint8_t * status;
// Data out register
volatile uint8_t * data_out;
// Data in register
volatile uint8_t * data_in;
// Control register
volatile uint8_t * control;
};
extern __code struct Pmc PMC_1;
extern __code struct Pmc PMC_2;
#define PMC_STS_OBF (1 << 0)
#define PMC_STS_IBF (1 << 1)
#define PMC_STS_CMD (1 << 3)
uint8_t pmc_status(struct Pmc * pmc);
uint8_t pmc_read(struct Pmc * pmc);
void pmc_write(struct Pmc * pmc, uint8_t data);
void pmc_event(struct Pmc * pmc);
__xdata volatile uint8_t __at(0x1500) PM1STS;
__xdata volatile uint8_t __at(0x1501) PM1DO;
__xdata volatile uint8_t __at(0x1504) PM1DI;
__xdata volatile uint8_t __at(0x1506) PM1CTL;
__xdata volatile uint8_t __at(0x1510) PM2STS;
__xdata volatile uint8_t __at(0x1511) PM2DO;
__xdata volatile uint8_t __at(0x1514) PM2DI;
__xdata volatile uint8_t __at(0x1516) PM2CTL;
#endif // _PMC_H_

View File

@ -0,0 +1,22 @@
#ifndef _PS2_H_
#define _PS2_H_
void ps2_init(void);
__xdata volatile unsigned char __at(0x1700) PSCTL1;
__xdata volatile unsigned char __at(0x1701) PSCTL2;
__xdata volatile unsigned char __at(0x1702) PSCTL3;
__xdata volatile unsigned char __at(0x1704) PSINT1;
__xdata volatile unsigned char __at(0x1705) PSINT2;
__xdata volatile unsigned char __at(0x1706) PSINT3;
__xdata volatile unsigned char __at(0x1708) PSSTS1;
__xdata volatile unsigned char __at(0x1709) PSSTS2;
__xdata volatile unsigned char __at(0x170A) PSSTS3;
__xdata volatile unsigned char __at(0x170C) PSDAT1;
__xdata volatile unsigned char __at(0x170D) PSDAT2;
__xdata volatile unsigned char __at(0x170E) PSDAT3;
#endif // _PS2_H_

View File

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

View File

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

View File

@ -0,0 +1,45 @@
#include <stdio.h>
#include "include/kbc.h"
__code struct Kbc KBC = {
.control = &KBHICR,
.irq = &KBIRQR,
.status = &KBHISR,
.keyboard_out = &KBHIKDOR,
.mouse_out = &KBHIMDOR,
.data_in = &KBHIDIR,
};
void kbc_init(void) {
*(KBC.irq) = 0;
*(KBC.control) = 0x48;
}
uint8_t kbc_status(struct Kbc * kbc) {
return *(kbc->status);
}
uint8_t kbc_read(struct Kbc * kbc) {
return *(kbc->data_in);
}
void kbc_keyboard(struct Kbc * kbc, uint8_t data) {
*(kbc->keyboard_out) = data;
}
void kbc_mouse(struct Kbc * kbc, uint8_t data) {
*(kbc->mouse_out) = data;
}
void kbc_event(struct Kbc * kbc) {
uint8_t sts = kbc_status(kbc);
if (sts & KBC_STS_IBF) {
uint8_t data = kbc_read(kbc);
if (sts & KBC_STS_CMD) {
printf("kbc cmd: %02X\n", data);
} else {
printf("kbc data: %02X\n", data);
}
}
}

View File

@ -0,0 +1,15 @@
#include "include/kbscan.h"
void kbscan_init(void) {
KSOCTRL = 0x05;
KSICTRLR = 0x04;
// Set all outputs to GPIO mode and high
KSOH2 = 0xFF;
KSOH1 = 0xFF;
KSOL = 0xFF;
KSOHGCTRL = 0xFF;
KSOHGOEN = 0xFF;
KSOLGCTRL = 0xFF;
KSOLGOEN = 0xFF;
}

View File

@ -0,0 +1,85 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "include/delay.h"
#include "include/gpio.h"
#include "include/gctrl.h"
#include "include/kbc.h"
#include "include/pin.h"
#include "include/pmc.h"
#include "include/ps2.h"
#include "include/kbscan.h"
void external_0(void) __interrupt(0) {
printf("external_0\n");
}
void timer_0(void) __interrupt(1) {
printf("timer_0\n");
}
void external_1(void) __interrupt(2) {
printf("external_1\n");
}
void timer_1(void) __interrupt(3) {
printf("timer_1\n");
}
void serial(void) __interrupt(4) {
printf("serial\n");
}
void timer_2(void) __interrupt(5) {
printf("timer_2\n");
}
void init(void) {
gpio_init();
gctrl_init();
kbc_init();
pmc_init();
kbscan_init();
//TODO: INTC, PECI, PWM, SMBUS
// PECI information can be found here: https://www.intel.com/content/dam/www/public/us/en/documents/design-guides/core-i7-lga-2011-guide.pdf
}
void power_button(struct Pin * button) {
static bool last = false;
// Check if the power switch goes low
bool new = pin_get(button);
if (!new && last) {
printf("Power Switch\n");
}
last = new;
}
struct Pin PWR_SW = PIN(D, 0);
struct Pin LED_BAT_CHG = PIN(A, 5);
struct Pin LED_BAT_FULL = PIN(A, 6);
struct Pin LED_PWR = PIN(A, 7);
struct Pin LED_ACIN = PIN(C, 7);
struct Pin LED_AIRPLANE_N = PIN(G, 6);
__code const char * MODEL = "galp3-c";
void main(void) {
init();
// Set the battery full LED (to know our firmware is loaded)
pin_set(&LED_BAT_FULL, true);
printf("Hello from System76 EC for %s!\n", MODEL);
gpio_debug();
for(;;) {
power_button(&PWR_SW);
kbc_event(&KBC);
pmc_event(&PMC_1);
}
}

View File

@ -0,0 +1,17 @@
#include "include/pin.h"
bool pin_get(struct Pin * pin) {
if (*(pin->data) & pin->value) {
return true;
} else {
return false;
}
}
void pin_set(struct Pin * pin, bool value) {
if (value) {
*(pin->data) |= pin->value;
} else {
*(pin->data) &= ~(pin->value);
}
}

View File

@ -0,0 +1,84 @@
#include <stdio.h>
#include "include/acpi.h"
#include "include/pmc.h"
#define PMC(NUM) { \
.status = &PM ## NUM ## STS, \
.data_out = &PM ## NUM ## DO, \
.data_in = &PM ## NUM ## DI, \
.control = &PM ## NUM ## CTL, \
}
__code struct Pmc PMC_1 = PMC(1);
__code struct Pmc PMC_2 = PMC(2);
void pmc_init(void) {
*(PMC_1.control) = 0x41;
*(PMC_2.control) = 0x41;
}
uint8_t pmc_status(struct Pmc * pmc) {
return *(pmc->status);
}
uint8_t pmc_read(struct Pmc * pmc) {
return *(pmc->data_in);
}
void pmc_write(struct Pmc * pmc, uint8_t data) {
*(pmc->data_out) = data;
}
enum PmcState {
PMC_STATE_DEFAULT,
PMC_STATE_ACPI_READ,
PMC_STATE_ACPI_WRITE,
PMC_STATE_ACPI_WRITE_ADDR,
};
void pmc_event(struct Pmc * pmc) {
static enum PmcState state = PMC_STATE_DEFAULT;
static uint8_t state_data[2] = {0, 0};
uint8_t sts = pmc_status(pmc);
if (sts & PMC_STS_IBF) {
uint8_t data = pmc_read(pmc);
if (sts & PMC_STS_CMD) {
printf("pmc cmd: %02X\n", data);
switch (data) {
case 0x80:
state = PMC_STATE_ACPI_READ;
break;
case 0x81:
state = PMC_STATE_ACPI_WRITE;
break;
default:
state = PMC_STATE_DEFAULT;
break;
}
} else {
printf("pmc data: %02X\n", data);
switch (state) {
case PMC_STATE_ACPI_READ:
state = PMC_STATE_DEFAULT;
uint8_t value = acpi_read(data);
pmc_write(pmc, value);
break;
case PMC_STATE_ACPI_WRITE:
state = PMC_STATE_ACPI_WRITE_ADDR;
state_data[0] = data;
break;
case PMC_STATE_ACPI_WRITE_ADDR:
state = PMC_STATE_DEFAULT;
acpi_write(state_data[0], data);
break;
default:
state = PMC_STATE_DEFAULT;
break;
}
}
}
}

View File

@ -0,0 +1,10 @@
#include "include/ps2.h"
void ps2_init(void) {
PSCTL1 = 0x11;
PSCTL2 = 0x41;
PSCTL3 = 0x41;
PSINT1 = 0x04;
PSINT2 = 0x04;
PSINT3 = 0x04;
}

View File

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

View File

@ -0,0 +1,7 @@
static __code char __at(0x40) SIGNATURE[32] = {
0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0x94,
0x85, 0x12, 0x5A, 0x5A, 0xAA, 0x00, 0x55, 0x55,
0x49, 0x54, 0x45, 0x20, 0x54, 0x65, 0x63, 0x68,
0x2E, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x20, 0x20
};

View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include "include/kbscan.h"
#include "include/timer.h"
// Wait 25 us
// 65536 - (25 / 1.304) = 65517
void parallel_delay(void) {
timer_mode_1(65517);
timer_wait();
timer_stop();
}
// This takes a time of 25 us * 3 = 75 us
// That produces a frequency of 13.333 KHz
// Which produces a bitrate of 106.667 KHz
void parallel_write(unsigned char value) {
// Make sure clock is high
KSOH1 = 0xFF;
parallel_delay();
// Set value
KSOL = value;
parallel_delay();
// Set clock low
KSOH1 = 0;
parallel_delay();
// Set clock high again
KSOH1 = 0xFF;
}
int putchar(int c) {
unsigned char byte = (unsigned char)c;
parallel_write(byte);
return (int)byte;
}

View File

@ -0,0 +1,20 @@
#include <8051.h>
#include "include/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;
}