Refactor timers, add GPIO debugging, and increase print speed
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@@ -8,7 +8,11 @@ clean:
 | 
			
		||||
	rm -rf build
 | 
			
		||||
 | 
			
		||||
sim: build/ec.rom
 | 
			
		||||
	cargo run --release --manifest-path ecsim/Cargo.toml -- $<
 | 
			
		||||
	cargo run \
 | 
			
		||||
		--release \
 | 
			
		||||
		--manifest-path ecsim/Cargo.toml \
 | 
			
		||||
		--no-default-features \
 | 
			
		||||
		-- $<
 | 
			
		||||
 | 
			
		||||
build/ec.rom: build/ec.ihx
 | 
			
		||||
	mkdir -p build
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								ecsim
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								ecsim
									
									
									
									
									
								
							 Submodule ecsim updated: 7e685cd815...f735f202a6
									
								
							
							
								
								
									
										30
									
								
								src/delay.c
									
									
									
									
									
								
							
							
						
						
									
										30
									
								
								src/delay.c
									
									
									
									
									
								
							@@ -1,30 +1,18 @@
 | 
			
		||||
#include <8051.h>
 | 
			
		||||
 | 
			
		||||
#include "include/delay.h"
 | 
			
		||||
#include "include/timer.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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 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++) {
 | 
			
		||||
        // 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();
 | 
			
		||||
        timer_wait();
 | 
			
		||||
        timer_stop();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										21
									
								
								src/gpio.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/gpio.c
									
									
									
									
									
								
							@@ -1,3 +1,5 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#include "include/gpio.h"
 | 
			
		||||
 | 
			
		||||
void gpio_init() {
 | 
			
		||||
@@ -98,3 +100,22 @@ void gpio_init() {
 | 
			
		||||
    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);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@
 | 
			
		||||
#define _GPIO_H_
 | 
			
		||||
 | 
			
		||||
void gpio_init(void);
 | 
			
		||||
void gpio_debug(void);
 | 
			
		||||
 | 
			
		||||
__xdata volatile unsigned char __at(0x1600) GCR;
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								src/include/timer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/include/timer.h
									
									
									
									
									
										Normal 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_
 | 
			
		||||
@@ -19,6 +19,8 @@ struct Pin LED_AIRPLANE_N = PIN(G, 6);
 | 
			
		||||
 | 
			
		||||
struct Pin PWR_SW = PIN(D, 0);
 | 
			
		||||
 | 
			
		||||
static char * MODEL = "galp3-c";
 | 
			
		||||
 | 
			
		||||
void main() {
 | 
			
		||||
    gpio_init();
 | 
			
		||||
    gctrl_init();
 | 
			
		||||
@@ -29,10 +31,10 @@ void main() {
 | 
			
		||||
    //TODO: INTC, PECI, PWM, SMBUS
 | 
			
		||||
 | 
			
		||||
    // Set the battery full LED (to know our firmware is loaded)
 | 
			
		||||
    pin_set(&LED_BAT_CHG, true);
 | 
			
		||||
    delay_ms(1000);
 | 
			
		||||
    pin_set(&LED_BAT_FULL, true);
 | 
			
		||||
    printf("Hello from System76 EC!\n");
 | 
			
		||||
    printf("Hello from System76 EC for %s!\n", MODEL);
 | 
			
		||||
 | 
			
		||||
    gpio_debug();
 | 
			
		||||
 | 
			
		||||
    bool last = false;
 | 
			
		||||
    for(;;) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								src/stdio.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								src/stdio.c
									
									
									
									
									
								
							@@ -1,20 +1,31 @@
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
 | 
			
		||||
#include "include/delay.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;
 | 
			
		||||
    delay_ms(1);
 | 
			
		||||
    parallel_delay();
 | 
			
		||||
 | 
			
		||||
    // Set value
 | 
			
		||||
    KSOL = value;
 | 
			
		||||
    delay_ms(1);
 | 
			
		||||
    parallel_delay();
 | 
			
		||||
 | 
			
		||||
    // Set clock low
 | 
			
		||||
    KSOH1 = 0;
 | 
			
		||||
    delay_ms(1);
 | 
			
		||||
    parallel_delay();
 | 
			
		||||
 | 
			
		||||
    // Set clock high again
 | 
			
		||||
    KSOH1 = 0xFF;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										20
									
								
								src/timer.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/timer.c
									
									
									
									
									
										Normal 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;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user