From efd1e11475088284803f5db0f554f6ef2d0268f5 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Tue, 27 Mar 2018 12:00:57 -0400 Subject: functional keypad, va_arg bug discovery --- src/gpio.c | 12 +++++----- src/keypad.c | 66 +++++++++++++++++++++++++-------------------------- src/keypad.c.bak | 72 -------------------------------------------------------- src/script.c | 26 ++++++++++++++++---- 4 files changed, 61 insertions(+), 115 deletions(-) delete mode 100644 src/keypad.c.bak (limited to 'src') diff --git a/src/gpio.c b/src/gpio.c index 2e50e88..db67465 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -26,36 +26,36 @@ void gpio_init(void) RCC->AHB2ENR |= 0xFF; } -void gpio_pupd(GPIO_TypeDef *port, uint8_t pin, uint8_t pupd) +void gpio_pupd(GPIO_TypeDef *port, uint32_t pin, uint32_t pupd) { port->PUPDR &= ~(0x03 << (2 * pin)); port->PUPDR |= pupd << (2 * pin); } -void gpio_speed(GPIO_TypeDef *port, uint8_t pin, uint8_t speed) +void gpio_speed(GPIO_TypeDef *port, uint32_t pin, uint32_t speed) { port->OSPEEDR &= ~(0x03 << (2 * pin)); port->OSPEEDR |= speed << (2 * pin); } -void gpio_type(GPIO_TypeDef *port, uint8_t pin, uint8_t type) +void gpio_type(GPIO_TypeDef *port, uint32_t pin, uint32_t type) { port->OTYPER &= ~(1 << pin); port->OTYPER |= type << pin; } -void gpio_mode(GPIO_TypeDef *port, uint8_t pin, uint8_t mode) +void gpio_mode(GPIO_TypeDef *port, uint32_t pin, uint32_t mode) { port->MODER &= ~(0x03 << (2 * pin)); port->MODER |= mode << (2 * pin); } -void gpio_dout(GPIO_TypeDef *port, uint8_t pin, uint8_t val) +void gpio_dout(GPIO_TypeDef *port, uint32_t pin, uint32_t val) { port->BSRR |= (1 << (val ? pin : pin + 16)); } -uint8_t gpio_din(GPIO_TypeDef *port, uint8_t pin) +uint32_t gpio_din(GPIO_TypeDef *port, uint32_t pin) { return port->IDR & (1 << pin); } diff --git a/src/keypad.c b/src/keypad.c index 20a67c2..f76271c 100644 --- a/src/keypad.c +++ b/src/keypad.c @@ -23,15 +23,15 @@ #include #include -#define ROW_0 GPIO_PORT(B, 15) -#define ROW_1 GPIO_PORT(B, 14) -#define ROW_2 GPIO_PORT(B, 13) +#define ROW_0 GPIO_PORT(A, 12) +#define ROW_1 GPIO_PORT(B, 12) +#define ROW_2 GPIO_PORT(B, 11) #define ROW_3 GPIO_PORT(C, 4) -#define COL_0 GPIO_PORT(B, 1) -#define COL_1 GPIO_PORT(B, 2) -#define COL_2 GPIO_PORT(B, 11) -#define COL_3 GPIO_PORT(B, 12) -#define COL_4 GPIO_PORT(A, 11) +#define COL_0 GPIO_PORT(B, 13) +#define COL_1 GPIO_PORT(B, 14) +#define COL_2 GPIO_PORT(B, 15) +#define COL_3 GPIO_PORT(B, 1) +#define COL_4 GPIO_PORT(B, 2) #define ROWS 4 #define COLS 5 @@ -49,43 +49,43 @@ static const port_t keypad_cols[COLS] = { { COL_0 }, { COL_1 }, { COL_2 }, { COL_3 }, { COL_4 } }; -//static const int keypad_map[ROWS][COLS] = { -// { '7', '8', '9', 'x', '/' }, -// { '4', '5', '6', 'y', '*' }, -// { '3', '2', '1', 'z', '-' }, -// { '.', '0', '\b', '\n', '+' } -//}; +static const int keypad_map[ROWS][COLS] = { + { '7', '8', '9', 'x', '/' }, + { '4', '5', '6', 'y', '*' }, + { '1', '2', '3', '=', '-' }, + { '.', '0', '\b', '\n', '+' } +}; #define BUFFER_SIZE 8 -static char keypad_buffer = 'A';//[BUFFER_SIZE]; +static char keypad_buffer = 0;//[BUFFER_SIZE]; //static int keypad_buffer_pos = -1; void keypad_task(void) { - //unsigned int col = 0; + unsigned int col = 0; while (1) { - // gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1); - // for (unsigned int row = 0; row < ROWS; row++) { - // if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) { - // //if (keypad_buffer_pos < BUFFER_SIZE) - // keypad_buffer/*[++keypad_buffer_pos]*/ = keypad_map[row][col]; - // while (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) - // delay(1); - // break; - // } - // } - // gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 0); - // col++; - // if (col == COLS) - // col = 0; - + gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 1); delay(10); + for (unsigned int row = 0; row < ROWS; row++) { + if (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) { + //if (keypad_buffer_pos < BUFFER_SIZE) + keypad_buffer/*[++keypad_buffer_pos]*/ = keypad_map[row][col]; + while (gpio_din(keypad_rows[row].port, keypad_rows[row].pin)) + delay(1); + break; + } + } + + gpio_dout(keypad_cols[col].port, keypad_cols[col].pin, 0); + col++; + if (col == COLS) + col = 0; } } void keypad_init(void) { - for (uint8_t i = 0; i < ROWS; i++) { + for (int i = 0; i < ROWS; i++) { GPIO_TypeDef *p = keypad_rows[i].port; uint16_t pin = keypad_rows[i].pin; gpio_mode(p, pin, OUTPUT); @@ -95,7 +95,7 @@ void keypad_init(void) gpio_pupd(p, pin, PULLDOWN); } - for (uint8_t i = 0; i < COLS; i++) { + for (int i = 0; i < COLS; i++) { GPIO_TypeDef *p = keypad_cols[i].port; uint16_t pin = keypad_cols[i].pin; gpio_mode(p, pin, OUTPUT); diff --git a/src/keypad.c.bak b/src/keypad.c.bak deleted file mode 100644 index 2fbefff..0000000 --- a/src/keypad.c.bak +++ /dev/null @@ -1,72 +0,0 @@ -/** - * @file keypad.c - * Manages the GPIO keypad using IO expanders - * - * Copyright (C) 2018 Clyne Sullivan - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include - -#define ADDR 0x20 - -#define CONTROL 0x41 -#define ADDRESS 0x12 - -void keypad_init(void) -{ - // clock init - RCC->CCIPR &= ~(RCC_CCIPR_I2C1SEL_Msk); - RCC->CCIPR |= 2 << RCC_CCIPR_I2C1SEL_Pos; - RCC->APB1ENR1 |= RCC_APB1ENR1_I2C1EN; - - // set times - // PRESC, SCLDEL, SDADEL, SCLH, SCLL - I2C1->TIMINGR = (0 << 28) | (2 << 20) | (0 << 16) | (2 << 8) | 4; - - // gpio init - gpio_mode(GPIOB, 8, ALTERNATE); - gpio_mode(GPIOB, 9, ALTERNATE); - GPIOB->AFR[1] &= ~(0xFF); - GPIOB->AFR[1] |= 0x44; - - // go go go - I2C1->CR1 |= I2C_CR1_PE; - - I2C1->CR2 |= ADDR << 1; - //I2C1->CR2 |= I2C_CR2_RD_WRN; - I2C1->CR2 &= ~(I2C_CR2_NBYTES); - I2C1->CR2 |= 1 << I2C_CR2_NBYTES_Pos; - I2C1->CR2 |= I2C_CR2_RELOAD; - I2C1->CR2 |= I2C_CR2_START; - - while (!(I2C1->ISR & I2C_ISR_TXE)); - - I2C1->TXDR = ADDRESS; - - while (I2C1->ISR & I2C_ISR_BUSY); - - I2C1->ICR |= 0x30; - I2C1->CR2 |= I2C_CR2_RD_WRN; - I2C1->CR2 |= I2C_CR2_RELOAD; - I2C1->CR2 |= I2C_CR2_START; - - while (1) { - while (!(I2C1->ISR & I2C_ISR_RXNE)); - uint32_t v = I2C1->RXDR; - (void)v; - } -} diff --git a/src/script.c b/src/script.c index be0fa37..0ce5509 100644 --- a/src/script.c +++ b/src/script.c @@ -35,6 +35,7 @@ #define igetarg_integer(it, n) ((int)igetarg(it, n)->value.f) int script_puts(instance *it); +int script_putchar(instance *it); int script_gets(instance *it); int script_delay(instance *it); int script_rect(instance *it); @@ -49,6 +50,7 @@ int script_menu(instance *it); void script_loadlib(instance *it) { inew_cfunc(it, "print", script_puts); + inew_cfunc(it, "putchar", script_putchar); inew_cfunc(it, "gets", script_gets); inew_cfunc(it, "getkey", script_getkey); inew_cfunc(it, "ppos", script_ppos); @@ -86,7 +88,7 @@ int script_puts(instance *it) variable *v = igetarg(it, 0); if (v->type == NUMBER) { char buf[33]; - snprintf(buf, 33, "%f", v->value.f); + snprintf(buf, 33, "%d", (int)v->value.f); // TODO dsp_puts(buf); } else if (v->type == STRING) { dsp_puts((const char *)v->value.p); @@ -94,6 +96,18 @@ int script_puts(instance *it) return 0; } +int script_putchar(instance *it) +{ + variable *v = igetarg(it, 0); + char buf[2]; + + buf[0] = (int)v->value.f; + buf[1] = '\0'; + dsp_puts(buf); + + return 0; +} + int script_gets(instance *it) { char *s = malloc(64); @@ -101,7 +115,11 @@ int script_gets(instance *it) int index = 0; do { - c[0] = serial_get(); + do { + c[0] = keypad_get(); + delay(1); + } while (c[0] == 0); + //c[0] = serial_get(); s[index] = c[0]; if (c[0] == '\b' || c[0] == 127) { index--; @@ -109,10 +127,10 @@ int script_gets(instance *it) dsp_puts("\b"); index--; } - } else if (c[0] != '\r') { + } else if (c[0] != '\n'/*'\r'*/) { dsp_puts(c); } - } while (s[index] != '\r' && index++ < 63); + } while (s[index] != '\n'/*'\r'*/ && index++ < 63); s[index] = '\0'; variable *r = make_vars(0, s); -- cgit v1.2.3