From ffd9e6eca2b160676c9d6e48585e64c6391fc450 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 27 Sep 2019 17:23:32 -0600 Subject: [PATCH] Modularize --- Makefile | 2 +- src/delay.c | 30 +++ src/gctrl.c | 7 + src/gpio.c | 100 +++++++++ src/include/delay.h | 6 + src/include/gctrl.h | 2 + src/include/gpio.h | 2 + src/include/kbc.h | 2 + src/include/kbscan.h | 2 + src/include/pin.h | 25 +++ src/include/pmc.h | 2 + src/include/ps2.h | 2 + src/include/reset.h | 6 + src/kbc.c | 6 + src/kbscan.c | 15 ++ src/main.c | 256 +---------------------- src/pin.c | 17 ++ src/pmc.c | 6 + src/ps2.c | 10 + src/reset.c | 5 + src/{include/signature.h => signature.c} | 5 - src/stdio.c | 27 +++ 22 files changed, 284 insertions(+), 251 deletions(-) create mode 100644 src/delay.c create mode 100644 src/gctrl.c create mode 100644 src/gpio.c create mode 100644 src/include/delay.h create mode 100644 src/include/pin.h create mode 100644 src/include/reset.h create mode 100644 src/kbc.c create mode 100644 src/kbscan.c create mode 100644 src/pin.c create mode 100644 src/pmc.c create mode 100644 src/ps2.c create mode 100644 src/reset.c rename src/{include/signature.h => signature.c} (78%) create mode 100644 src/stdio.c diff --git a/Makefile b/Makefile index 026e363..bef42f7 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ build/ec.rom: build/ec.ihx build/ec.ihx: $(OBJ) mkdir -p build - sdcc -mmcs51 -o $@ $< + sdcc -mmcs51 -o $@ $^ build/%.rel: src/%.c $(INCLUDE) mkdir -p build diff --git a/src/delay.c b/src/delay.c new file mode 100644 index 0000000..b39d3ea --- /dev/null +++ b/src/delay.c @@ -0,0 +1,30 @@ +#include <8051.h> + +#include "include/delay.h" + +void timer_clear(void) { + TR0 = 0; + TF0 = 0; +} + +void timer_mode_1(int value) { + timer_clear(); + TMOD = 0x01; + TH0 = (unsigned char)(value >> 8); + TL0 = (unsigned char)value; + TR0 = 1; +} + +void delay_ms(int ms) { + for (int i = 0; i < ms; i++) { + // 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 + timer_mode_1(64769); + while (TF0 == 0) {} + timer_clear(); + } +} diff --git a/src/gctrl.c b/src/gctrl.c new file mode 100644 index 0000000..ad84aed --- /dev/null +++ b/src/gctrl.c @@ -0,0 +1,7 @@ +#include "include/gctrl.h" + +void gctrl_init(void) { + SPCTRL1 = 0x03; + BADRSEL = 0; + RSTS = 0x84; +} diff --git a/src/gpio.c b/src/gpio.c new file mode 100644 index 0000000..a288152 --- /dev/null +++ b/src/gpio.c @@ -0,0 +1,100 @@ +#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; +} diff --git a/src/include/delay.h b/src/include/delay.h new file mode 100644 index 0000000..cc9eef9 --- /dev/null +++ b/src/include/delay.h @@ -0,0 +1,6 @@ +#ifndef _DELAY_H_ +#define _DELAY_H_ + +void delay_ms(int ms); + +#endif // _DELAY_H_ diff --git a/src/include/gctrl.h b/src/include/gctrl.h index 944c219..b19086c 100644 --- a/src/include/gctrl.h +++ b/src/include/gctrl.h @@ -1,6 +1,8 @@ #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; diff --git a/src/include/gpio.h b/src/include/gpio.h index a88d621..ade4ea7 100644 --- a/src/include/gpio.h +++ b/src/include/gpio.h @@ -1,6 +1,8 @@ #ifndef _GPIO_H_ #define _GPIO_H_ +void gpio_init(void); + __xdata volatile unsigned char __at(0x1600) GCR; __xdata volatile unsigned char __at(0x1601) GPDRA; diff --git a/src/include/kbc.h b/src/include/kbc.h index f88f9c3..1254527 100644 --- a/src/include/kbc.h +++ b/src/include/kbc.h @@ -1,6 +1,8 @@ #ifndef _KBC_H_ #define _KBC_H_ +void kbc_init(void); + __xdata volatile unsigned char __at(0x1300) KBHICR; __xdata volatile unsigned char __at(0x1302) KBIRQR; __xdata volatile unsigned char __at(0x1304) KBHISR; diff --git a/src/include/kbscan.h b/src/include/kbscan.h index 82a8964..82bdc24 100644 --- a/src/include/kbscan.h +++ b/src/include/kbscan.h @@ -1,6 +1,8 @@ #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; diff --git a/src/include/pin.h b/src/include/pin.h new file mode 100644 index 0000000..67e31df --- /dev/null +++ b/src/include/pin.h @@ -0,0 +1,25 @@ +#ifndef _PIN_H_ +#define _PIN_H_ + +#include + +#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_ diff --git a/src/include/pmc.h b/src/include/pmc.h index ce9cbe5..6dff028 100644 --- a/src/include/pmc.h +++ b/src/include/pmc.h @@ -1,6 +1,8 @@ #ifndef _PMC_H_ #define _PMC_H_ +void pmc_init(void); + __xdata volatile unsigned char __at(0x1506) PM1CTL; __xdata volatile unsigned char __at(0x1516) PM2CTL; diff --git a/src/include/ps2.h b/src/include/ps2.h index 0ffc9e5..3975898 100644 --- a/src/include/ps2.h +++ b/src/include/ps2.h @@ -1,6 +1,8 @@ #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; diff --git a/src/include/reset.h b/src/include/reset.h new file mode 100644 index 0000000..0201dcc --- /dev/null +++ b/src/include/reset.h @@ -0,0 +1,6 @@ +#ifndef _RESET_H_ +#define _RESET_H_ + +void reset(void); + +#endif // _RESET_H_ diff --git a/src/kbc.c b/src/kbc.c new file mode 100644 index 0000000..dfedfd8 --- /dev/null +++ b/src/kbc.c @@ -0,0 +1,6 @@ +#include "include/kbc.h" + +void kbc_init(void) { + KBIRQR = 0; + KBHICR = 0x48; +} diff --git a/src/kbscan.c b/src/kbscan.c new file mode 100644 index 0000000..7444211 --- /dev/null +++ b/src/kbscan.c @@ -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; +} diff --git a/src/main.c b/src/main.c index f1181a1..1801058 100644 --- a/src/main.c +++ b/src/main.c @@ -1,219 +1,15 @@ -#include "include/signature.h" - -void reset() { - __asm__("ljmp 0"); -} - -#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; -} - -#include "include/gctrl.h" - -void gctrl_init() { - SPCTRL1 = 0x03; - BADRSEL = 0; - RSTS = 0x84; -} - -#include "include/kbc.h" - -void kbc_init() { - KBIRQR = 0; - KBHICR = 0x48; -} - -#include "include/pmc.h" - -void pmc_init() { - PM1CTL = 0x41; - PM2CTL = 0x41; -} - -#include "include/ps2.h" - -void ps2_init() { - PSCTL1 = 0x11; - PSCTL2 = 0x41; - PSCTL3 = 0x41; - PSINT1 = 0x04; - PSINT2 = 0x04; - PSINT3 = 0x04; -} - -#include "include/kbscan.h" - -void kbscan_init() { - 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; -} - #include <8051.h> - -void timer_clear(void) { - TR0 = 0; - TF0 = 0; -} - -void timer_mode_1(int value) { - timer_clear(); - TMOD = 0x01; - TH0 = (unsigned char)(value >> 8); - TL0 = (unsigned char)value; - TR0 = 1; -} - -void delay_ms(int ms) { - for (int i = 0; i < ms; i++) { - // 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 - timer_mode_1(64769); - while (TF0 == 0) {} - timer_clear(); - } -} - #include +#include -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) { - 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); - } -} +#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" struct Pin LED_BAT_CHG = PIN(A, 5); struct Pin LED_BAT_FULL = PIN(A, 6); @@ -223,41 +19,11 @@ struct Pin LED_AIRPLANE_N = PIN(G, 6); struct Pin PWR_SW = PIN(D, 0); -void parallel_write(unsigned char value) { - // Make sure clock is high - KSOH1 = 0xFF; - delay_ms(1); - - // Set value - KSOL = value; - delay_ms(1); - - // Set clock low - KSOH1 = 0; - pin_set(&LED_ACIN, true); - delay_ms(1); - - // Set clock high again - pin_set(&LED_ACIN, false); - KSOH1 = 0xFF; -} - -void puts(const char * s) { - char c; - while (c = *(s++)) { - parallel_write((unsigned char)c); - } -} - void main() { gpio_init(); - gctrl_init(); - kbc_init(); - pmc_init(); - kbscan_init(); //TODO: INTC, PECI, PWM, SMBUS @@ -266,14 +32,14 @@ void main() { pin_set(&LED_BAT_CHG, true); delay_ms(1000); pin_set(&LED_BAT_FULL, true); - puts("Hello from System76 EC!\n"); + printf("Hello from System76 EC!\n"); bool last = false; for(;;) { // Check if the power switch goes low bool new = pin_get(&PWR_SW); if (!new && last) { - puts("Power Switch\n"); + printf("Power Switch\n"); } last = new; } diff --git a/src/pin.c b/src/pin.c new file mode 100644 index 0000000..47c0666 --- /dev/null +++ b/src/pin.c @@ -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); + } +} diff --git a/src/pmc.c b/src/pmc.c new file mode 100644 index 0000000..9fee1dc --- /dev/null +++ b/src/pmc.c @@ -0,0 +1,6 @@ +#include "include/pmc.h" + +void pmc_init(void) { + PM1CTL = 0x41; + PM2CTL = 0x41; +} diff --git a/src/ps2.c b/src/ps2.c new file mode 100644 index 0000000..24609ab --- /dev/null +++ b/src/ps2.c @@ -0,0 +1,10 @@ +#include "include/ps2.h" + +void ps2_init(void) { + PSCTL1 = 0x11; + PSCTL2 = 0x41; + PSCTL3 = 0x41; + PSINT1 = 0x04; + PSINT2 = 0x04; + PSINT3 = 0x04; +} diff --git a/src/reset.c b/src/reset.c new file mode 100644 index 0000000..3acf4ca --- /dev/null +++ b/src/reset.c @@ -0,0 +1,5 @@ +#include "include/reset.h" + +void reset(void) { + __asm__("ljmp 0"); +} diff --git a/src/include/signature.h b/src/signature.c similarity index 78% rename from src/include/signature.h rename to src/signature.c index bc976e5..05ee931 100644 --- a/src/include/signature.h +++ b/src/signature.c @@ -1,6 +1,3 @@ -#ifndef _SIGNATURE_H_ -#define _SIGNATURE_H_ - static __code char __at(0x40) SIGNATURE[32] = { 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0xA5, 0x94, 0x85, 0x12, 0x5A, 0x5A, 0xAA, 0x00, 0x55, 0x55, @@ -8,5 +5,3 @@ static __code char __at(0x40) SIGNATURE[32] = { 0x49, 0x54, 0x45, 0x20, 0x54, 0x65, 0x63, 0x68, 0x2E, 0x20, 0x49, 0x6E, 0x63, 0x2E, 0x20, 0x20 }; - -#endif // _SIGNATURE_H_ diff --git a/src/stdio.c b/src/stdio.c new file mode 100644 index 0000000..c4145d5 --- /dev/null +++ b/src/stdio.c @@ -0,0 +1,27 @@ +#include + +#include "include/delay.h" +#include "include/kbscan.h" + +void parallel_write(unsigned char value) { + // Make sure clock is high + KSOH1 = 0xFF; + delay_ms(1); + + // Set value + KSOL = value; + delay_ms(1); + + // Set clock low + KSOH1 = 0; + delay_ms(1); + + // Set clock high again + KSOH1 = 0xFF; +} + +int putchar(int c) { + unsigned char byte = (unsigned char)c; + parallel_write(byte); + return (int)byte; +}