Modularize

This commit is contained in:
Jeremy Soller 2019-09-27 17:23:32 -06:00
parent e97175932f
commit ffd9e6eca2
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
22 changed files with 284 additions and 251 deletions

View File

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

30
src/delay.c Normal file
View File

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

7
src/gctrl.c Normal file
View File

@ -0,0 +1,7 @@
#include "include/gctrl.h"
void gctrl_init(void) {
SPCTRL1 = 0x03;
BADRSEL = 0;
RSTS = 0x84;
}

100
src/gpio.c Normal file
View File

@ -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;
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delay.h Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

25
src/include/pin.h Normal file
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

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

View File

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

6
src/include/reset.h Normal file
View File

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

6
src/kbc.c Normal file
View File

@ -0,0 +1,6 @@
#include "include/kbc.h"
void kbc_init(void) {
KBIRQR = 0;
KBHICR = 0x48;
}

15
src/kbscan.c Normal file
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

@ -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 <stdbool.h>
#include <stdio.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) {
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;
}

17
src/pin.c Normal file
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);
}
}

6
src/pmc.c Normal file
View File

@ -0,0 +1,6 @@
#include "include/pmc.h"
void pmc_init(void) {
PM1CTL = 0x41;
PM2CTL = 0x41;
}

10
src/ps2.c Normal file
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;
}

5
src/reset.c Normal file
View File

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

View File

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

27
src/stdio.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#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;
}