aboutsummaryrefslogtreecommitdiff
path: root/platforms/chibios/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'platforms/chibios/drivers')
-rw-r--r--platforms/chibios/drivers/eeprom/eeprom_legacy_emulated_flash.c7
-rw-r--r--platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.c6
-rw-r--r--platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.h2
-rw-r--r--platforms/chibios/drivers/spi_master.c9
-rw-r--r--platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c33
5 files changed, 47 insertions, 10 deletions
diff --git a/platforms/chibios/drivers/eeprom/eeprom_legacy_emulated_flash.c b/platforms/chibios/drivers/eeprom/eeprom_legacy_emulated_flash.c
index a81fe3353c..9857ac046b 100644
--- a/platforms/chibios/drivers/eeprom/eeprom_legacy_emulated_flash.c
+++ b/platforms/chibios/drivers/eeprom/eeprom_legacy_emulated_flash.c
@@ -24,6 +24,7 @@
#include "debug.h"
#include "eeprom_legacy_emulated_flash.h"
#include "legacy_flash_ops.h"
+#include "eeprom_driver.h"
/*
* We emulate eeprom by writing a snapshot compacted view of eeprom contents,
@@ -564,6 +565,12 @@ void eeprom_driver_init(void) {
EEPROM_Init();
}
+void eeprom_driver_format(bool erase) {
+ /* emulated eepron requires the write log data structures to be erased before use. */
+ (void)erase;
+ eeprom_driver_erase();
+}
+
void eeprom_driver_erase(void) {
EEPROM_Erase();
}
diff --git a/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.c b/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.c
index ed26cc7145..628137a0b3 100644
--- a/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.c
+++ b/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.c
@@ -52,6 +52,12 @@ static inline void STM32_L0_L1_EEPROM_Lock(void) {
void eeprom_driver_init(void) {}
+void eeprom_driver_format(bool erase) {
+ if (erase) {
+ eeprom_driver_erase();
+ }
+}
+
void eeprom_driver_erase(void) {
STM32_L0_L1_EEPROM_Unlock();
diff --git a/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.h b/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.h
index 616d7ccbee..c1d801f225 100644
--- a/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.h
+++ b/platforms/chibios/drivers/eeprom/eeprom_stm32_L0_L1.h
@@ -20,7 +20,7 @@
The size used by the STM32 L0/L1 EEPROM driver.
*/
#ifndef STM32_ONBOARD_EEPROM_SIZE
-# ifdef VIA_ENABLE
+# ifdef DYNAMIC_KEYMAP_ENABLE
# define STM32_ONBOARD_EEPROM_SIZE 1024
# else
# include "eeconfig.h"
diff --git a/platforms/chibios/drivers/spi_master.c b/platforms/chibios/drivers/spi_master.c
index 57fc53d49f..fcdbc9ecf0 100644
--- a/platforms/chibios/drivers/spi_master.c
+++ b/platforms/chibios/drivers/spi_master.c
@@ -64,9 +64,14 @@ __attribute__((weak)) void spi_init(void) {
}
bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) {
+#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
+ spiAcquireBus(&SPI_DRIVER);
+#endif // (SPI_USE_MUTUAL_EXCLUSION == TRUE)
+
if (spiStarted) {
return false;
}
+
#if SPI_SELECT_MODE != SPI_SELECT_MODE_NONE
if (slavePin == NO_PIN) {
return false;
@@ -326,4 +331,8 @@ void spi_stop(void) {
spiStop(&SPI_DRIVER);
spiStarted = false;
}
+
+#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
+ spiReleaseBus(&SPI_DRIVER);
+#endif // (SPI_USE_MUTUAL_EXCLUSION == TRUE)
}
diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
index 3d6ed52e5c..f49c4a45b0 100644
--- a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
+++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl.c
@@ -14,11 +14,15 @@ static flash_sector_t first_sector = WEAR_LEVELING_EFL_FIRST_SECTOR;
static flash_sector_t first_sector = UINT16_MAX;
#endif // defined(WEAR_LEVELING_EFL_FIRST_SECTOR)
+#if !defined(WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT)
+# define WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT 0
+#endif // WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT
+
static flash_sector_t sector_count = UINT16_MAX;
static BaseFlash * flash;
-
-static volatile bool is_issuing_read = false;
-static volatile bool ecc_error_occurred = false;
+static bool flash_erased_is_one;
+static volatile bool is_issuing_read = false;
+static volatile bool ecc_error_occurred = false;
// "Automatic" detection of the flash size -- ideally ChibiOS would have this already, but alas, it doesn't.
static inline uint32_t detect_flash_size(void) {
@@ -51,10 +55,19 @@ bool backing_store_init(void) {
uint32_t counter = 0;
uint32_t flash_size = detect_flash_size();
+ // Check if the hardware erase is logic 1
+ flash_erased_is_one = (desc->attributes & FLASH_ATTR_ERASED_IS_ONE) ? true : false;
+
+ if (WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT >= desc->sectors_count) {
+ // Last sector defined is greater than available number of sectors. Can't do anything here. Fault.
+ chSysHalt("Last sector intended to be used with wear_leveling is beyond available flash descriptor range");
+ }
+
#if defined(WEAR_LEVELING_EFL_FIRST_SECTOR)
// Work out how many sectors we want to use, working forwards from the first sector specified
- for (flash_sector_t i = 0; i < desc->sectors_count - first_sector; ++i) {
+ flash_sector_t last_sector = desc->sectors_count - WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT;
+ for (flash_sector_t i = 0; i < last_sector - first_sector; ++i) {
counter += flashGetSectorSize(flash, first_sector + i);
if (counter >= (WEAR_LEVELING_BACKING_SIZE)) {
sector_count = i + 1;
@@ -70,9 +83,9 @@ bool backing_store_init(void) {
#else // defined(WEAR_LEVELING_EFL_FIRST_SECTOR)
// Work out how many sectors we want to use, working backwards from the end of the flash
- flash_sector_t last_sector = desc->sectors_count;
- for (flash_sector_t i = 0; i < desc->sectors_count; ++i) {
- first_sector = desc->sectors_count - i - 1;
+ flash_sector_t last_sector = desc->sectors_count - WEAR_LEVELING_EFL_OMIT_LAST_SECTOR_COUNT;
+ for (flash_sector_t i = 0; i < last_sector; ++i) {
+ first_sector = last_sector - i - 1;
if (flashGetSectorOffset(flash, first_sector) >= flash_size) {
last_sector = first_sector;
continue;
@@ -124,7 +137,9 @@ bool backing_store_write(uint32_t address, backing_store_int_t value) {
uint32_t offset = (base_offset + address);
bs_dprintf("Write ");
wl_dump(offset, &value, sizeof(value));
- value = ~value;
+ if (flash_erased_is_one) {
+ value = ~value;
+ }
return flashProgram(flash, offset, sizeof(value), (const uint8_t *)&value) == FLASH_NO_ERROR;
}
@@ -138,7 +153,7 @@ static backing_store_int_t backing_store_safe_read_from_location(backing_store_i
backing_store_int_t value;
is_issuing_read = true;
ecc_error_occurred = false;
- value = ~(*loc);
+ value = flash_erased_is_one ? ~(*loc) : (*loc);
is_issuing_read = false;
return value;
}