functional keypad, va_arg bug discovery

master
Clyne 7 years ago
parent b398f319c7
commit efd1e11475

@ -83,7 +83,7 @@ void gpio_init(void);
* @param pupd pullup/pulldown enable
* @see GPIO_PUPD
*/
void gpio_pupd(GPIO_TypeDef *port, uint8_t pin, uint8_t pupd);
void gpio_pupd(GPIO_TypeDef *port, uint32_t pin, uint32_t pupd);
/**
* Sets whether to use push-pull or open drain for the given pin.
@ -92,7 +92,7 @@ void gpio_pupd(GPIO_TypeDef *port, uint8_t pin, uint8_t pupd);
* @param type what to use
* @see GPIO_TYPE
*/
void gpio_type(GPIO_TypeDef *port, uint8_t pin, uint8_t type);
void gpio_type(GPIO_TypeDef *port, uint32_t pin, uint32_t type);
/**
* Sets the pin's speed.
@ -101,7 +101,7 @@ void gpio_type(GPIO_TypeDef *port, uint8_t pin, uint8_t type);
* @param speed the speed to use
* @see GPIO_SPEED
*/
void gpio_speed(GPIO_TypeDef *port, uint8_t pin, uint8_t speed);
void gpio_speed(GPIO_TypeDef *port, uint32_t pin, uint32_t speed);
/**
* Sets the pin's i/o mode.
@ -110,7 +110,7 @@ void gpio_speed(GPIO_TypeDef *port, uint8_t pin, uint8_t speed);
* @param mode the mode to use
* @see GPIO_MODE
*/
void gpio_mode(GPIO_TypeDef *port, uint8_t pin, uint8_t mode);
void gpio_mode(GPIO_TypeDef *port, uint32_t pin, uint32_t mode);
/**
* Sets the state of a digital output pin.
@ -118,7 +118,7 @@ void gpio_mode(GPIO_TypeDef *port, uint8_t pin, uint8_t mode);
* @param pin the pin
* @param val non-zero for high, zero for low
*/
void gpio_dout(GPIO_TypeDef *port, uint8_t pin, uint8_t val);
void gpio_dout(GPIO_TypeDef *port, uint32_t pin, uint32_t val);
/**
* Reads a digital input pin.
@ -126,6 +126,6 @@ void gpio_dout(GPIO_TypeDef *port, uint8_t pin, uint8_t val);
* @param pin the pin
* @return non-zero for high, zero for low
*/
uint8_t gpio_din(GPIO_TypeDef *port, uint8_t pin);
uint32_t gpio_din(GPIO_TypeDef *port, uint32_t pin);
#endif // GPIO_H_

@ -1,8 +1,8 @@
input = 0
while (1) {
while (input == 0) {
input = getkey
}
print(input)
input = 0
print("> ")
input = gets()
answer = solve(input)
print("\n")
print(answer)
print("\n")
}

Binary file not shown.

@ -41,8 +41,8 @@ SECTIONS {
*(.text)
*(.text*)
*(.init)
*(.fini)
KEEP(*(.init))
KEEP(*(.fini))
. = ALIGN(8);
} > FLASH
@ -64,14 +64,14 @@ SECTIONS {
/* init_array/fini_array (TODO understand this) */
.init_array : {
__init_array_start = .;
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array.*))
KEEP(*(.init_array*))
__init_array_end = .;
} > FLASH
.fini_array : {
__fini_array_start = .;
KEEP(*(SORT(.fini_array.*)))
KEEP(*(.fini_array.*))
KEEP(*(.fini_array*))
__fini_array_end = .;
} > FLASH
@ -88,10 +88,10 @@ SECTIONS {
/* uninitialized data */
.bss : {
. = ALIGN(4);
. = ALIGN(8);
__bss_start__ = .;
*(.bss)
. = ALIGN(4);
. = ALIGN(8);
__bss_end__ = .;
} > RAM
}

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

@ -23,15 +23,15 @@
#include <gpio.h>
#include <task.h>
#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);

@ -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 <https://www.gnu.org/licenses/>.
*/
#include <stm32l476xx.h>
#include <gpio.h>
#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;
}
}

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

Loading…
Cancel
Save