Skip to content

Commit

Permalink
FW: Flash saving not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dlktdr committed May 12, 2024
1 parent 210c61d commit 246068c
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 41 deletions.
2 changes: 1 addition & 1 deletion firmware/src/src/ble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void bt_Thread()
while (1) {
k_poll(btRunEvents, 1, K_FOREVER);

if (pauseForFlash || k_sem_count_get(&btPauseSem) == 1) {
if (k_sem_count_get(&flashWriteSemaphore) == 1 || k_sem_count_get(&btPauseSem) == 1) {
k_msleep(10);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/src/btparahead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void BTHeadStart()

// Discover BT Address
bt_id_get(addrarry, &addrcnt);
if (addrcnt > 0) bt_addr_le_to_str(&addrarry[0], _address, sizeof(_address));
if (addrcnt == 1) bt_addr_le_to_str(&addrarry[0], _address, sizeof(_address));

crc = 0;
bufferIndex = 0;
Expand Down
8 changes: 4 additions & 4 deletions firmware/src/src/htmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ void start(void)
LOG_INF("Starting IO");
io_init();

// Load settings from flash - trackersettings.cpp
LOG_INF("Loading Settings");
trkset.loadFromEEPROM();

// Serial Setup, we have a CDC Device, enable USB
#if defined(DT_N_INST_0_zephyr_cdc_acm_uart)
LOG_INF("Starting USB\n");
Expand Down Expand Up @@ -100,10 +104,6 @@ void start(void)
PWM_Init(PWM_FREQUENCY);
#endif

// Load settings from flash - trackersettings.cpp
LOG_INF("Loading Settings");
trkset.loadFromEEPROM();

// Check if center button is held down, force BT Configuration mode
LOG_INF("Checking Center Button");
if (readCenterButton()) {
Expand Down
6 changes: 3 additions & 3 deletions firmware/src/src/include/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#define CALCULATE_PERIOD 7000 // (us) Channel Calculations
#define UART_PERIOD 4000 // (us) Update rate of UART
#define PWM_FREQUENCY 50 // (ms) PWM Period
#define PAUSE_BEFORE_FLASH 60 // (ms) Time to pause all threads before Flash writing
#define PAUSE_BEFORE_FLASH 30 // (ms) Time to pause all threads before Flash writing

// Thread Stack Sizes
#if defined(CONFIG_SOC_SERIES_NRF52X)
Expand Down Expand Up @@ -110,8 +110,8 @@
#define IO_THREAD_PRIO PRIORITY_LOW
#define SERIAL_THREAD_PRIO PRIORITY_LOW
#define DATA_THREAD_PRIO PRIORITY_LOW
#define BT_THREAD_PRIO PRIORITY_HIGH
#define SENSOR_THREAD_PRIO PRIORITY_MED
#define BT_THREAD_PRIO -PRIORITY_HIGH
#define SENSOR_THREAD_PRIO PRIORITY_HIGH
#define CALCULATE_THREAD_PRIO PRIORITY_HIGH
#define UARTRX_THREAD_PRIO PRIORITY_LOW - 2
#define UARTTX_THREAD_PRIO PRIORITY_HIGH
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/src/include/soc_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stdint.h>

extern volatile bool pauseForFlash;
extern struct k_sem flashWriteSemaphore;

int socReadFlash(char *dataout, int len);
int socWriteFlash(const uint8_t *data, int len);
Expand Down
12 changes: 11 additions & 1 deletion firmware/src/src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void io_Thread()
k_msleep(IO_PERIOD);
k_poll(ioRunEvents, 1, K_FOREVER);

if (pauseForFlash) continue;
if (k_sem_count_get(&flashWriteSemaphore) == 1) continue;

#if defined(HAS_NOTIFYLED)
// LEDS
Expand Down Expand Up @@ -255,6 +255,16 @@ void io_Thread()
led_sequence[1].time = 20;
led_sequence[2].time = 0; // End Sequence

// Gyro not calibrated, bluetooth is searching. Red Pulsing, Pulse of Blue
} else if (_ledmode & LED_GYROCAL && _ledmode & LED_BTSCANNING) {
led_sequence[0].RGB = RGB_BLUE;
led_sequence[0].time = 50;
led_sequence[1].RGB = RGB_RED;
led_sequence[1].time = 400;
led_sequence[2].RGB = RGB_OFF;
led_sequence[2].time = 350;
led_sequence[3].time = 0; // End Sequence

// Gyro Calibration Mode - Red slow, equal flashing
} else if (_ledmode & LED_GYROCAL) { // Priority 2
led_sequence[0].RGB = RGB_RED;
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/src/sense.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void calculate_Thread()
// Do not execute below until after initialization has happened
k_poll(calculateRunEvents, 1, K_FOREVER);

if (pauseForFlash) {
if (k_sem_count_get(&flashWriteSemaphore) == 1) {
k_msleep(10);
continue;
}
Expand Down Expand Up @@ -771,7 +771,7 @@ void sensor_Thread()
// Do not execute below until after initialization has happened
k_poll(senseRunEvents, 1, K_FOREVER);

if (pauseForFlash) {
if (k_sem_count_get(&flashWriteSemaphore) == 1) {
k_msleep(10);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion firmware/src/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void serial_Thread()
k_poll(serialRunEvents, 1, K_FOREVER);
k_msleep(SERIAL_PERIOD);

if (pauseForFlash) {
if (k_sem_count_get(&flashWriteSemaphore) == 1) {
continue;
}

Expand Down
52 changes: 30 additions & 22 deletions firmware/src/src/soc_flash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ spi_flash_mmap_handle_t handle;

#include "defines.h"


LOG_MODULE_REGISTER(flash);

#define FLASH_ADDRESS FIXED_PARTITION_OFFSET(storage_partition)
#define FLASH_SIZE FIXED_PARTITION_SIZE(storage_partition)

volatile bool pauseForFlash = false;
#define FALSH_PARTITION storage_partition
#define FLASH_OFFSET FIXED_PARTITION_OFFSET(FALSH_PARTITION)
#define FLASH_SIZE FIXED_PARTITION_SIZE(FALSH_PARTITION)
#define FLASH_DEVICE FIXED_PARTITION_DEVICE(FALSH_PARTITION)

// Pause for flash write to complete
K_SEM_DEFINE(flashWriteSemaphore, 0, 1);

// Use a pointer to flash on these architectures
#if defined(CONFIG_SOC_ESP32) ||\
Expand Down Expand Up @@ -71,51 +72,55 @@ const uint8_t *socGetMMFlashPtr()
}

/* map selected region */
spi_flash_mmap(FLASH_ADDRESS, FLASH_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&mem_ptr, &handle);
spi_flash_mmap(FLASH_OFFSET, FLASH_SIZE, SPI_FLASH_MMAP_DATA, (const void **)&mem_ptr, &handle);
isFlashMemMapped = true;
}
#elif defined(CONFIG_SOC_SERIES_NRF52X)
mem_ptr = (const uint8_t *)FLASH_ADDRESS;
mem_ptr = (const uint8_t *)FLASH_OFFSET;
#else
#warning "No memory mapping for this SOC"
socReadFlash(mem_ptr);
#endif
// Is flash memory blank. Return a indicator there is nothing to be read
if(*mem_ptr == 0xFF) {
return (uint8_t *)"{\"UUID\":837727}";
return (uint8_t *)"{\"UUID\":837727,\"TEST\":343}";
}
return mem_ptr;
}

int socReadFlash(uint8_t dataout[FLASH_SIZE])
{
const struct device *flash_device = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
const struct device *flash_device = FLASH_DEVICE;
if (!device_is_ready(flash_device)) {
LOG_ERR("%s: device not ready.\n", flash_device->name);
return -1;
}
memset(dataout, 0, FLASH_SIZE);
flash_read(flash_device, FLASH_ADDRESS, dataout, FLASH_SIZE);
flash_read(flash_device, FLASH_OFFSET, dataout, FLASH_SIZE);
return 0;
}

void socClearFlash()
{
const struct device *flash_device = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller));
const struct device *flash_device = FLASH_DEVICE;
if (!flash_device) {
LOG_ERR("Flash Device Not Found!");
return;
}

pauseForFlash = true;
LOG_INF("Erasing Flash at Offset: %d", FLASH_OFFSET);
LOG_INF("Flash Size: %d", FLASH_SIZE);

k_sem_give(&flashWriteSemaphore);

k_msleep(PAUSE_BEFORE_FLASH);

if (flash_erase(flash_device, FLASH_ADDRESS, FLASH_SIZE) != 0) {
if (flash_erase(flash_device, FLASH_OFFSET, FLASH_SIZE) != 0) {
LOG_ERR("Flash erase Failure");
pauseForFlash = false;

return;
}
pauseForFlash = false;
k_sem_take(&flashWriteSemaphore, K_NO_WAIT);
LOG_INF("Flash erase succeeded");
}

Expand All @@ -130,26 +135,29 @@ int socWriteFlash(const uint8_t *datain, int len)
return -1;
}

pauseForFlash = true;
k_sem_give(&flashWriteSemaphore);
k_msleep(PAUSE_BEFORE_FLASH);

if (flash_erase(flash_dev, FLASH_ADDRESS, FLASH_SIZE) != 0) {
LOG_INF("Erasing Flash at Offset: %d", FLASH_OFFSET);
LOG_INF("Flash Size: %d", FLASH_SIZE);

if (flash_erase(flash_dev, FLASH_OFFSET, FLASH_SIZE) != 0) {
LOG_ERR("Flash erase Failure");
pauseForFlash = false;
k_sem_take(&flashWriteSemaphore, K_NO_WAIT);
return -1;
}

LOG_INF("Flash erase succeeded");
k_msleep(PAUSE_BEFORE_FLASH);

if (flash_write(flash_dev, FLASH_ADDRESS, (const void *)datain, len) != 0) {
if (flash_write(flash_dev, FLASH_OFFSET, (const void *)datain, len) != 0) {
LOG_ERR("Flash write failed!");
pauseForFlash = false;
k_sem_take(&flashWriteSemaphore, K_NO_WAIT);
return -1;
}

LOG_INF("Flash write succeeded");

pauseForFlash = false;
k_sem_take(&flashWriteSemaphore, K_NO_WAIT);

return 0;
}
6 changes: 3 additions & 3 deletions firmware/src/src/uart_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void uartRx_Thread()
k_poll(uartRxRunEvents, 1, K_FOREVER);
k_usleep(UART_PERIOD);

if (pauseForFlash) {
if (k_sem_count_get(&flashWriteSemaphore) == 1) {
continue;
}

Expand Down Expand Up @@ -115,8 +115,8 @@ void uartTx_Thread()
{
while (1) {
k_poll(uartTxRunEvents, 1, K_FOREVER);
if (pauseForFlash) {
k_msleep(1000);
if (k_sem_count_get(&flashWriteSemaphore) == 1) {
k_msleep(10);
continue;
}

Expand Down
4 changes: 3 additions & 1 deletion firmware/src/zephyr/nrf_prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ CONFIG_SENSOR=y
# Flash
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_SOC_FLASH_NRF=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_SOC_FLASH_NRF_EMULATE_ONE_BYTE_WRITE_ACCESS=y

#Bluetooth
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1024
Expand All @@ -112,7 +114,7 @@ CONFIG_BT_GATT_DYNAMIC_DB=y
CONFIG_BT_GAP_AUTO_UPDATE_CONN_PARAMS=n
CONFIG_BT_L2CAP_TX_MTU=65
CONFIG_BT_CTLR_RX_BUFFERS=2
CONFIG_BT_CTLR_LLL_PRIO=0
#CONFIG_BT_CTLR_LLL_PRIO=0
CONFIG_BT_USER_PHY_UPDATE=y
CONFIG_BT_CTLR_PHY_CODED=y
CONFIG_BT_CTLR_PHY_2M=y
Expand Down
2 changes: 1 addition & 1 deletion gui/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,8 @@ void MainWindow::paramReceiveStart()
void MainWindow::paramReceiveComplete()
{
statusMessage(tr("Parameters Request Complete"),5000);
updateToUI();
waitingOnParameters = false;
updateToUI();

// Request the Features
jsonht->requestFeatures();
Expand Down

0 comments on commit 246068c

Please sign in to comment.