From 51b884c44c858aa56aae6675a2c3a8b2d882768e Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 1 Mar 2018 13:03:40 -0500 Subject: keypad support, making stuff good --- src/clock.c | 9 ++------ src/display_draw.c | 4 ++-- src/gpio.c | 5 +---- src/keypad.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 17 +++++++++------ src/script.c | 16 ++++++++++++++ src/stm32l4xx_it.c | 12 +++++------ src/task.c | 9 ++------ 8 files changed, 103 insertions(+), 32 deletions(-) create mode 100644 src/keypad.c (limited to 'src') diff --git a/src/clock.c b/src/clock.c index 3d31f91..0c8ca7c 100644 --- a/src/clock.c +++ b/src/clock.c @@ -7,7 +7,7 @@ #define STK_CALIB *((uint32_t *)0xE000E01C) // ticks since init -static uint32_t ticks = 0; +volatile uint32_t ticks = 0; void clock_init(void) { @@ -31,6 +31,7 @@ void clock_init(void) // set system clock to PLL RCC->CFGR &= ~(RCC_CFGR_SW); + RCC->CFGR &= ~(RCC_CFGR_HPRE_Msk); RCC->CFGR |= RCC_CFGR_SW_PLL; while ((RCC->CFGR & RCC_CFGR_SWS_PLL) != RCC_CFGR_SWS_PLL); @@ -45,18 +46,12 @@ void delay(uint32_t count) while (ticks < target); } -__attribute__ ((naked)) void SysTick_Handler(void) { - uint32_t lr; - asm("mov %0, lr" : "=r" (lr)); - // just keep counting ticks++; if (!(ticks & 3)) SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk; - - asm("mov lr, %0; bx lr" :: "r" (lr)); } diff --git a/src/display_draw.c b/src/display_draw.c index 27ed796..a315bf7 100644 --- a/src/display_draw.c +++ b/src/display_draw.c @@ -92,12 +92,12 @@ void dsp_rect(int x, int y, int w, int h, uint16_t color) { dsp_set_addr(x, y, x + w - 1, y + h - 1); int countdown = w * h; + LOCK; do { - LOCK; dsp_write_data(color >> 8); dsp_write_data(color & 0xFF); - UNLOCK; } while (countdown--); + UNLOCK; } void dsp_line(int x, int y, int i, int j, uint16_t color) diff --git a/src/gpio.c b/src/gpio.c index c2617ee..900a3f1 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -32,10 +32,7 @@ void gpio_mode(GPIO_TypeDef *port, uint8_t pin, uint8_t mode) void gpio_dout(GPIO_TypeDef *port, uint8_t pin, uint8_t val) { - if (val) - port->BSRR |= (1 << pin); - else - port->BRR |= (1 << pin); + port->BSRR |= (1 << (val ? pin : pin + 16)); } uint8_t gpio_din(GPIO_TypeDef *port, uint8_t pin) diff --git a/src/keypad.c b/src/keypad.c new file mode 100644 index 0000000..41fc924 --- /dev/null +++ b/src/keypad.c @@ -0,0 +1,63 @@ +#include +#include + +#define PIN_0 GPIO_PORT(A, 11) +#define PIN_1 GPIO_PORT(B, 13) +#define PIN_2 GPIO_PORT(B, 2) +#define PIN_3 GPIO_PORT(A, 12) +#define PIN_4 GPIO_PORT(B, 14) +#define PIN_5 GPIO_PORT(B, 11) +#define PIN_6 GPIO_PORT(C, 5) +#define PIN_7 GPIO_PORT(B, 15) +#define PIN_8 GPIO_PORT(B, 12) +#define PIN_9 GPIO_PORT(C, 6) +#define PIN_S GPIO_PORT(B, 1) +#define PIN_P GPIO_PORT(C, 8) + +typedef struct { + GPIO_TypeDef *port; + uint16_t pin; + uint16_t keycode; +} key_t; + +static const key_t keypad_map[12] = { + { PIN_0, K0 }, + { PIN_1, K1 }, + { PIN_2, K2 }, + { PIN_3, K3 }, + { PIN_4, K4 }, + { PIN_5, K5 }, + { PIN_6, K6 }, + { PIN_7, K7 }, + { PIN_8, K8 }, + { PIN_9, K9 }, + { PIN_S, KS }, + { PIN_P, KP } +}; + +void keypad_init(void) +{ + for (uint8_t i = 0; i < 12; i++) { + GPIO_TypeDef *p = keypad_map[i].port; + uint16_t pin = keypad_map[i].pin; + gpio_mode(p, pin, OUTPUT); + gpio_dout(p, pin, 0); + gpio_mode(p, pin, INPUT); + gpio_pupd(p, pin, PULLDOWN); + } +} + +uint16_t keypad_get(void) +{ + uint16_t state = 0; + for (uint8_t i = 0; i < 12; i++) { + if (gpio_din(keypad_map[i].port, keypad_map[i].pin)) + state |= keypad_map[i].keycode; + } + return state; +} + +uint8_t keypad_isdown(uint16_t keycode) +{ + return (keypad_get() & keycode); +} diff --git a/src/main.c b/src/main.c index 54c2403..abcb8e6 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,7 @@ #include #include #include +#include extern uint8_t _ebss; extern char *itoa(int, char *, int); @@ -22,24 +23,27 @@ void task_interpreter(void); int main(void) { asm("cpsid i"); - // disable write buffer - *((uint32_t *)0xE000E008) |= 2; // prepare flash latency for 80MHz operation FLASH->ACR &= ~(FLASH_ACR_LATENCY); FLASH->ACR |= FLASH_ACR_LATENCY_4WS; - //MPU->CTRL |= MPU_CTRL_ENABLE_Msk | MPU_CTRL_PRIVDEFENA_Msk; clock_init(); heap_init(&_ebss); gpio_init(); + keypad_init(); serial_init(); random_init(); + //extern void keypad_init(void); + //keypad_init(); + gpio_mode(GPIOA, 5, OUTPUT); // enable FPU SCB->CPACR |= (0xF << 20); + // enable MPU + //MPU->CTRL |= MPU_CTRL_PRIVDEFENA_Msk | MPU_CTRL_ENABLE_Msk; task_init(kmain); while (1); @@ -47,17 +51,18 @@ int main(void) void kmain(void) { - asm("cpsie i"); dsp_init(); dsp_rect(0, 0, LCD_WIDTH, LCD_HEIGHT, dsp_color(0, 0, 0)); dsp_cursoron(); task_start(task_interpreter, 4096); while (1) { - gpio_dout(GPIOA, 5, 1); + gpio_dout(GPIOA, 5, + (keypad_isdown(K0))); + /*gpio_dout(GPIOA, 5, 1); delay(250); gpio_dout(GPIOA, 5, 0); - delay(250); + delay(250);*/ } } diff --git a/src/script.c b/src/script.c index adf402b..78165b0 100644 --- a/src/script.c +++ b/src/script.c @@ -8,6 +8,7 @@ #include #include #include +#include int script_puts(interpreter *it); int script_gets(interpreter *it); @@ -17,6 +18,7 @@ int script_ppos(interpreter *it); int script_line(interpreter *it); int script_color(interpreter *it); int script_rand(interpreter *it); +int script_getkey(interpreter *it); void script_loadlib(interpreter *it) { @@ -28,6 +30,7 @@ void script_loadlib(interpreter *it) inew_cfunc(it, "line", script_line); inew_cfunc(it, "color", script_color); inew_cfunc(it, "rand", script_rand); + inew_cfunc(it, "getkey", script_getkey); } int script_puts(interpreter *it) @@ -118,3 +121,16 @@ int script_rand(interpreter *it) free(v); return 0; } + +int script_getkey(interpreter *it) +{ + variable *v = (variable *)malloc(sizeof(variable)); + v->valtype = INTEGER; + INT(v) = keypad_get(); + v->svalue = 0; + isetstr(v); + iret(it, v); + free(v->svalue); + free(v); + return 0; +} diff --git a/src/stm32l4xx_it.c b/src/stm32l4xx_it.c index f643db3..e54a192 100644 --- a/src/stm32l4xx_it.c +++ b/src/stm32l4xx_it.c @@ -12,12 +12,12 @@ void serial_puts(const char *s) void perror(const char *s) { - extern task_t *current; + //extern task_t *current; serial_puts(s); - char buf[200]; - snprintf(buf, 200, "xPSR: %x\r\nPC: %x\r\nLR: %x\r\n", current->sp[0], - current->sp[1], current->sp[2]); - serial_puts(buf); + //char buf[200]; + //snprintf(buf, 200, "xPSR: %x\r\nPC: %x\r\nLR: %x\r\n", current->sp[0], + // current->sp[1], current->sp[2]); + //serial_puts(buf); } __attribute__ ((naked)) @@ -27,7 +27,7 @@ __attribute__ ((naked)) void HardFault_Handler(void) { GPIOA->BSRR |= (1 << 5); - //perror("Hard Fault!"); + perror("Hard Fault!"); while (1); } diff --git a/src/task.c b/src/task.c index 6a755de..537785d 100644 --- a/src/task.c +++ b/src/task.c @@ -34,12 +34,6 @@ task_t *task_create(void (*code)(void), uint32_t stackSize) t->sp[14] = 0xFFFFFFFD; t->sp[15] = (uint32_t)code; t->sp[16] = 0x01000000; - - //void *sp = (uint8_t *)t->stack + stackSize - 64; // 16 words - //t->sp = (uint32_t *)sp; - //t->sp[13] = (uint32_t)task_exit; - //t->sp[14] = (uint32_t)code; - //t->sp[15] = 0x01000000; return t; } @@ -53,7 +47,8 @@ void task_init(void (*init)(void)) asm("\ msr psp, %0; \ mrs r0, control; \ - orr r0, r0, #2; \ + orr r0, r0, #3; \ + cpsie i; \ msr control, r0; \ isb; \ bx %1; \ -- cgit v1.2.3