Fixed block size, button debug.

This commit is contained in:
Daniele Lacamera 2019-11-02 14:44:45 +01:00
parent dee9335db4
commit 1c02765920
4 changed files with 48 additions and 30 deletions

View file

@ -4,7 +4,7 @@ LD:=$(CROSS_COMPILE)gcc
USECFS:=$(HOME)/src/usecfs USECFS:=$(HOME)/src/usecfs
WOLFSSL:=$(HOME)/src/wolfssl WOLFSSL:=$(HOME)/src/wolfssl
OBJS:=startup.o main.o system.o $(USECFS)/src/usecfs_dev_spi.o $(USECFS)/src/usecfs.o mem.o led.o \ OBJS:=startup.o main.o system.o usecfs_dev_spi.o $(USECFS)/src/usecfs.o mem.o led.o \
i2c.o display.o font_twisted.o spi.o spi_flash.o button.o systick.o newlib.o uart.o ui.o \ i2c.o display.o font_twisted.o spi.o spi_flash.o button.o systick.o newlib.o uart.o ui.o \
sdcard.o random.o sdcard.o random.o
OBJS+= \ OBJS+= \
@ -30,7 +30,7 @@ LSCRIPT:=target.ld
OBJCOPY:=$(CROSS_COMPILE)objcopy OBJCOPY:=$(CROSS_COMPILE)objcopy
CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=200 -ffreestanding -Wno-unused -DBLOCK_SIZE=512 -I$(USECFS)/src -I. -I$(WOLFSSL) -Ilib/unicore-mx/include CFLAGS:=-mcpu=cortex-m3 -mthumb -Wall -Wno-main -Wstack-usage=200 -ffreestanding -Wno-unused -DBLOCK_SIZE=4096 -I$(USECFS)/src -I. -I$(WOLFSSL) -Ilib/unicore-mx/include
CFLAGS+=-specs=nano.specs -lc -lg CFLAGS+=-specs=nano.specs -lc -lg
CFLAGS+=$(UMXFLAGS) CFLAGS+=$(UMXFLAGS)
#CFLAGS+=-O2 #CFLAGS+=-O2

View file

@ -19,6 +19,16 @@
#include "unicore-mx/stm32/f4/adc.h" #include "unicore-mx/stm32/f4/adc.h"
#include "unicore-mx/stm32/f4/nvic.h" #include "unicore-mx/stm32/f4/nvic.h"
// Uncomment to enable debug
//#define BUTTON_DEBUG
#ifdef BUTTON_DEBUG
# define DBG printf
#else
# define DBG(...) do {} while (0)
#endif
#define BUTTON_DEBOUNCE_TIME 50 #define BUTTON_DEBOUNCE_TIME 50
#define BUTTON_HOLD_TIME 1500 #define BUTTON_HOLD_TIME 1500
@ -142,7 +152,7 @@ int button_poll(void (*callback)(uint8_t press, int hold))
if((Buttons[b].state == IDLE) || (Buttons[b].state == RELEASING)) { if((Buttons[b].state == IDLE) || (Buttons[b].state == RELEASING)) {
Buttons[b].state = PRESSING; Buttons[b].state = PRESSING;
Buttons[b].transition_start_timestamp = jiffies; Buttons[b].transition_start_timestamp = jiffies;
printf("%06u: ST: PRESSING\r\n", jiffies); DBG("%06u: ST: PRESSING\r\n", jiffies);
button_press_pending = 0; button_press_pending = 0;
return 0; return 0;
} else { } else {
@ -153,13 +163,13 @@ int button_poll(void (*callback)(uint8_t press, int hold))
Buttons[b].state = HOLD; Buttons[b].state = HOLD;
Buttons[b].transition_start_timestamp = jiffies; Buttons[b].transition_start_timestamp = jiffies;
button_press_pending = 0; button_press_pending = 0;
printf("%06u: ST: HOLD\r\n", jiffies); DBG("%06u: ST: HOLD\r\n", jiffies);
} }
} else if (p_interval > BUTTON_DEBOUNCE_TIME) { } else if (p_interval > BUTTON_DEBOUNCE_TIME) {
if (Buttons[b].state == PRESSING) { if (Buttons[b].state == PRESSING) {
callback(b, 0); callback(b, 0);
Buttons[b].state = PRESSED; Buttons[b].state = PRESSED;
printf("%06u: ST: PRESSED\r\n", jiffies); DBG("%06u: ST: PRESSED\r\n", jiffies);
button_press_pending = 0; button_press_pending = 0;
} }
if (Buttons[b].state == HOLD) { if (Buttons[b].state == HOLD) {
@ -178,12 +188,12 @@ int button_poll(void (*callback)(uint8_t press, int hold))
if ((jiffies - Buttons[i].transition_start_timestamp) > (BUTTON_DEBOUNCE_TIME)) { if ((jiffies - Buttons[i].transition_start_timestamp) > (BUTTON_DEBOUNCE_TIME)) {
Buttons[i].state = IDLE; Buttons[i].state = IDLE;
Buttons[i].transition_start_timestamp = 0; Buttons[i].transition_start_timestamp = 0;
printf("%06u: ST: IDLE\r\n", jiffies); DBG("%06u: ST: IDLE\r\n", jiffies);
} }
} else if (Buttons[i].state != IDLE) { } else if (Buttons[i].state != IDLE) {
Buttons[i].state = RELEASING; Buttons[i].state = RELEASING;
Buttons[i].transition_start_timestamp = jiffies; Buttons[i].transition_start_timestamp = jiffies;
printf("%06u: ST:0\r\n", jiffies); DBG("%06u: ST:0\r\n", jiffies);
} }
} }
} }

View file

@ -31,9 +31,7 @@
#include "system.h" #include "system.h"
#include "sdcard.h" #include "sdcard.h"
#if (!defined(BLOCK_SIZE)) || (BLOCK_SIZE != 512) #define SDCARD_BLOCK_SIZE 512
# error BLOCK_SIZE undefined or invalid
#endif
#ifndef NULL #ifndef NULL
#define NULL (void *)(0x00000000) #define NULL (void *)(0x00000000)
@ -391,7 +389,7 @@ sdio_scr(struct dev_sd *sd, SDIO_CARD c) {
* Read a Block from our Card * Read a Block from our Card
* *
* NB: There is a possibly useless test in this code, during the read * NB: There is a possibly useless test in this code, during the read
* phase it allows that the SDIO card might try to send more than BLOCK_SIZE * phase it allows that the SDIO card might try to send more than SDCARD_BLOCK_SIZE
* bytes (128 32 bit longs) and allows it to do so, constantly over * bytes (128 32 bit longs) and allows it to do so, constantly over
* writing the last long in the just-in-case-over-long-by-1 data buffer. * writing the last long in the just-in-case-over-long-by-1 data buffer.
* To compromise the system you would need a borked or custom crafted * To compromise the system you would need a borked or custom crafted
@ -410,14 +408,14 @@ int sdcard_read(void *dev, void *_buf, uint32_t lba)
uint32_t buf_len = 0; uint32_t buf_len = 0;
if (! SDIO_CARD_CCS(sd->card)) { if (! SDIO_CARD_CCS(sd->card)) {
addr = lba * BLOCK_SIZE; // non HC cards use byte address addr = lba * SDCARD_BLOCK_SIZE; // non HC cards use byte address
} }
err = sdio_select(sd, sd->card->rca); err = sdio_select(sd, sd->card->rca);
if (! err) { if (! err) {
err = stm32_sdio_command(sd, 16, BLOCK_SIZE); err = stm32_sdio_command(sd, 16, SDCARD_BLOCK_SIZE);
if (!err) { if (!err) {
SDIO_DTIMER = 0xffffffff; SDIO_DTIMER = 0xffffffff;
SDIO_DLEN = BLOCK_SIZE; SDIO_DLEN = SDCARD_BLOCK_SIZE;
SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 | SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 |
SDIO_DCTRL_DTDIR | SDIO_DCTRL_DTDIR |
SDIO_DCTRL_DTEN; SDIO_DCTRL_DTEN;
@ -442,7 +440,7 @@ int sdcard_read(void *dev, void *_buf, uint32_t lba)
err = SDIO_EUNKNOWN; // Unknown Error! err = SDIO_EUNKNOWN; // Unknown Error!
} }
} else { } else {
err = BLOCK_SIZE; err = SDCARD_BLOCK_SIZE;
} }
} }
} }
@ -471,16 +469,16 @@ int sdcard_write(void *dev, void *_buf, uint32_t lba)
if (! SDIO_CARD_CCS(c)) { if (! SDIO_CARD_CCS(c)) {
addr = lba * BLOCK_SIZE; // non HC cards use byte address addr = lba * SDCARD_BLOCK_SIZE; // non HC cards use byte address
} }
err = sdio_select(sd, c->rca); err = sdio_select(sd, c->rca);
if (! err) { if (! err) {
/* Set Block Size to BLOCK_SIZE */ /* Set Block Size to SDCARD_BLOCK_SIZE */
err = stm32_sdio_command(sd, 16, BLOCK_SIZE); err = stm32_sdio_command(sd, 16, SDCARD_BLOCK_SIZE);
if (!err) { if (!err) {
SDIO_DTIMER = 0xffffffff; SDIO_DTIMER = 0xffffffff;
SDIO_DLEN = BLOCK_SIZE; SDIO_DLEN = SDCARD_BLOCK_SIZE;
SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 | SDIO_DCTRL = SDIO_DCTRL_DBLOCKSIZE_9 |
SDIO_DCTRL_DTEN; SDIO_DCTRL_DTEN;
err = stm32_sdio_command(sd, 24, addr); err = stm32_sdio_command(sd, 24, addr);
@ -509,7 +507,7 @@ int sdcard_write(void *dev, void *_buf, uint32_t lba)
// deselect the card // deselect the card
(void) sdio_select(sd, 0); (void) sdio_select(sd, 0);
if (err == 0) if (err == 0)
return BLOCK_SIZE; return SDCARD_BLOCK_SIZE;
return err; return err;
} }
@ -689,10 +687,10 @@ stm32_sdio_open(struct dev_sd *sd) {
* In the V1 Case : * In the V1 Case :
* Size = 1<<BLOCK_LEN * 1<<(MULT+2) * (C_SIZE+1) bytes. * Size = 1<<BLOCK_LEN * 1<<(MULT+2) * (C_SIZE+1) bytes.
* In the V2 Case : * In the V2 Case :
* Size = (C_SIZE + 1) * BLOCK_SIZEK bytes. * Size = (C_SIZE + 1) * SDCARD_BLOCK_SIZEK bytes.
* But for our structure we want the size in BLOCK_SIZE byte "blocks" * But for our structure we want the size in SDCARD_BLOCK_SIZE byte "blocks"
* since that is the addressing unit we're going to export so * since that is the addressing unit we're going to export so
* we compute the size / BLOCK_SIZE as the "size" for the structure. * we compute the size / SDCARD_BLOCK_SIZE as the "size" for the structure.
*/ */
if (! err) { if (! err) {

24
ui.c
View file

@ -14,10 +14,15 @@
#include <string.h> #include <string.h>
#include "usecfs.h" #include "usecfs.h"
/* Uncomment for device initialization */
//#define DEVICE_INITIALIZATION
static const char welcome_message0[]= " There is no "; static const char welcome_message0[]= " There is no ";
static const char welcome_message1[]= "knowledge that "; static const char welcome_message1[]= "knowledge that ";
static const char welcome_message2[]= " is not power. "; static const char welcome_message2[]= " is not power. ";
static const uint8_t uuid[UUID_LEN] = { static const uint8_t uuid[UUID_LEN] = {
0xb4, 0x6b, 0x82, 0x05, 0xb6, 0xcd, 0x28, 0xaa, 0xc7, 0x81, 0x2e, 0x2d, 0xb4, 0x6b, 0x82, 0x05, 0xb6, 0xcd, 0x28, 0xaa, 0xc7, 0x81, 0x2e, 0x2d,
0xfd, 0xdd, 0x5e, 0x70, 0x3f, 0xbf, 0x09, 0x03, 0x1b, 0x5f, 0xbe, 0xc6, 0xfd, 0xdd, 0x5e, 0x70, 0x3f, 0xbf, 0x09, 0x03, 0x1b, 0x5f, 0xbe, 0xc6,
@ -55,15 +60,22 @@ void ui_msg(const char *txt)
void lock(void) void lock(void)
{ {
//led_on(RED_LED); //led_on(RED_LED);
ui_msg("Locked."); ui_msg("Device is Locked");
fsm_state = ST_LOCKED; fsm_state = ST_LOCKED;
} }
static void pwrst(void)
{
memset(password, 0, MAX_PASSWORD);
pin_idx = 0;
}
int unlock(void) int unlock(void)
{ {
uint8_t stored_uuid[UUID_LEN]; uint8_t stored_uuid[UUID_LEN];
if (pin_idx < 6) { if (pin_idx < 6) {
ui_msg("pin code too short"); display_text(6, "Failed.");
pwrst();
return -1; return -1;
} }
display_scroll(NULL, 0); display_scroll(NULL, 0);
@ -72,7 +84,7 @@ int unlock(void)
display_text(5, "Unlocking secret"); display_text(5, "Unlocking secret");
if ((usecfs_init(password, 0, stored_uuid) == 0) && if ((usecfs_init(password, 0, stored_uuid) == 0) &&
(memcmp(stored_uuid, uuid, UUID_LEN) == 0)) { (memcmp(stored_uuid, uuid, UUID_LEN) == 0)) {
memset(password, 0, MAX_PASSWORD); pwrst();
fsm_state = ST_UNLOCKED; fsm_state = ST_UNLOCKED;
ui_msg("Device UNLOCKED"); ui_msg("Device UNLOCKED");
return 0; return 0;
@ -85,8 +97,7 @@ int unlock(void)
} }
#endif #endif
display_text(6, "Failed."); display_text(6, "Failed.");
memset(password, 0, MAX_PASSWORD); pwrst();
pin_idx = 0;
fsm_state = ST_LOCKED; fsm_state = ST_LOCKED;
return -1; return -1;
} }
@ -134,9 +145,8 @@ void ui_button_press(uint16_t button)
int i; int i;
char c; char c;
if (fsm_state == ST_LOCKED) { if (fsm_state == ST_LOCKED) {
pwrst();
ui_msg("Insert pin:"); ui_msg("Insert pin:");
pin_idx = 0;
memset(password, 0, MAX_PASSWORD);
fsm_state = ST_PIN_ENTRY; fsm_state = ST_PIN_ENTRY;
return; return;
} }