summaryrefslogtreecommitdiffstats
path: root/tmk_core/common
diff options
context:
space:
mode:
authorAlex Ong <the.onga@gmail.com>2019-01-26 12:13:19 +1100
committerAlex Ong <the.onga@gmail.com>2019-01-26 12:13:19 +1100
commitc9ba618654417ec115809a031d315f8327c79ad4 (patch)
treecd5b907af5bebde7062897ff847e473232ed1214 /tmk_core/common
parent2bb2977c133646c4e056960e72029270d77cc1eb (diff)
parentd977daa8dc9136746425f9e1414e1f93cb161877 (diff)
DO NOT USE Merge branch 'master' into debounce_refactor
Merged, however now there are two debounce.h and debounce.c to mess around with and coalesce. # Conflicts: # quantum/matrix.c
Diffstat (limited to 'tmk_core/common')
-rw-r--r--tmk_core/common/action.c10
-rw-r--r--tmk_core/common/action.h1
-rw-r--r--tmk_core/common/action_layer.c303
-rw-r--r--tmk_core/common/action_util.c4
-rw-r--r--tmk_core/common/arm_atsam/timer.c20
-rwxr-xr-xtmk_core/common/chibios/eeprom_stm32.c710
-rwxr-xr-xtmk_core/common/chibios/eeprom_stm32.h68
-rwxr-xr-xtmk_core/common/chibios/flash_stm32.c15
-rwxr-xr-xtmk_core/common/chibios/flash_stm32.h1
-rw-r--r--tmk_core/common/eeconfig.c4
-rw-r--r--tmk_core/common/eeconfig.h21
-rw-r--r--tmk_core/common/eeprom.h1
-rw-r--r--tmk_core/common/host.h11
-rw-r--r--tmk_core/common/keyboard.h2
-rw-r--r--tmk_core/common/keycode.h20
-rw-r--r--tmk_core/common/progmem.h6
-rw-r--r--tmk_core/common/report.h8
-rw-r--r--tmk_core/common/wait.h4
18 files changed, 371 insertions, 838 deletions
diff --git a/tmk_core/common/action.c b/tmk_core/common/action.c
index b99c2acaa7..ec8d6ed7b9 100644
--- a/tmk_core/common/action.c
+++ b/tmk_core/common/action.c
@@ -653,7 +653,7 @@ void process_action(keyrecord_t *record, action_t action)
#ifndef NO_ACTION_TAPPING
#ifdef RETRO_TAPPING
- if (!is_tap_key(record->event.key)) {
+ if (!is_tap_action(action)) {
retro_tapping_counter = 0;
} else {
if (event.pressed) {
@@ -929,7 +929,15 @@ void clear_keyboard_but_mods_and_keys()
bool is_tap_key(keypos_t key)
{
action_t action = layer_switch_get_action(key);
+ return is_tap_action(action);
+}
+/** \brief Utilities for actions. (FIXME: Needs better description)
+ *
+ * FIXME: Needs documentation.
+ */
+bool is_tap_action(action_t action)
+{
switch (action.kind.id) {
case ACT_LMODS_TAP:
case ACT_RMODS_TAP:
diff --git a/tmk_core/common/action.h b/tmk_core/common/action.h
index 8e47e5339e..799e3bb0ef 100644
--- a/tmk_core/common/action.h
+++ b/tmk_core/common/action.h
@@ -97,6 +97,7 @@ void clear_keyboard_but_mods(void);
void clear_keyboard_but_mods_and_keys(void);
void layer_switch(uint8_t new_layer);
bool is_tap_key(keypos_t key);
+bool is_tap_action(action_t action);
#ifndef NO_ACTION_TAPPING
void process_record_tap_hint(keyrecord_t *record);
diff --git a/tmk_core/common/action_layer.c b/tmk_core/common/action_layer.c
index 120ce3f51b..6ff8c5549b 100644
--- a/tmk_core/common/action_layer.c
+++ b/tmk_core/common/action_layer.c
@@ -17,82 +17,76 @@ uint32_t default_layer_state = 0;
/** \brief Default Layer State Set At user Level
*
- * FIXME: Needs docs
+ * Run user code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_user(uint32_t state) {
- return state;
+ return state;
}
/** \brief Default Layer State Set At Keyboard Level
*
- * FIXME: Needs docs
+ * Run keyboard code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_kb(uint32_t state) {
- return default_layer_state_set_user(state);
+ return default_layer_state_set_user(state);
}
/** \brief Default Layer State Set
*
- * FIXME: Needs docs
+ * Static function to set the default layer state, prints debug info and clears keys
*/
-static void default_layer_state_set(uint32_t state)
-{
- state = default_layer_state_set_kb(state);
- debug("default_layer_state: ");
- default_layer_debug(); debug(" to ");
- default_layer_state = state;
- default_layer_debug(); debug("\n");
+static void default_layer_state_set(uint32_t state) {
+ state = default_layer_state_set_kb(state);
+ debug("default_layer_state: ");
+ default_layer_debug(); debug(" to ");
+ default_layer_state = state;
+ default_layer_debug(); debug("\n");
#ifdef STRICT_LAYER_RELEASE
- clear_keyboard_but_mods(); // To avoid stuck keys
+ clear_keyboard_but_mods(); // To avoid stuck keys
#else
- clear_keyboard_but_mods_and_keys(); // Don't reset held keys
+ clear_keyboard_but_mods_and_keys(); // Don't reset held keys
#endif
}
/** \brief Default Layer Print
*
- * FIXME: Needs docs
+ * Print out the hex value of the 32-bit default layer state, as well as the value of the highest bit.
*/
-void default_layer_debug(void)
-{
- dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
+void default_layer_debug(void) {
+ dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
}
/** \brief Default Layer Set
*
- * FIXME: Needs docs
+ * Sets the default layer state.
*/
-void default_layer_set(uint32_t state)
-{
- default_layer_state_set(state);
+void default_layer_set(uint32_t state) {
+ default_layer_state_set(state);
}
#ifndef NO_ACTION_LAYER
/** \brief Default Layer Or
*
- * FIXME: Needs docs
+ * Turns on the default layer based on matching bits between specifed layer and existing layer state
*/
-void default_layer_or(uint32_t state)
-{
- default_layer_state_set(default_layer_state | state);
+void default_layer_or(uint32_t state) {
+ default_layer_state_set(default_layer_state | state);
}
/** \brief Default Layer And
*
- * FIXME: Needs docs
+ * Turns on default layer based on matching enabled bits between specifed layer and existing layer state
*/
-void default_layer_and(uint32_t state)
-{
- default_layer_state_set(default_layer_state & state);
+void default_layer_and(uint32_t state) {
+ default_layer_state_set(default_layer_state & state);
}
/** \brief Default Layer Xor
*
- * FIXME: Needs docs
+ * Turns on default layer based on non-matching bits between specifed layer and existing layer state
*/
-void default_layer_xor(uint32_t state)
-{
- default_layer_state_set(default_layer_state ^ state);
+void default_layer_xor(uint32_t state) {
+ default_layer_state_set(default_layer_state ^ state);
}
#endif
@@ -104,170 +98,168 @@ uint32_t layer_state = 0;
/** \brief Layer state set user
*
- * FIXME: Needs docs
+ * Runs user code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_user(uint32_t state) {
- return state;
+ return state;
}
/** \brief Layer state set keyboard
*
- * FIXME: Needs docs
+ * Runs keyboard code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_kb(uint32_t state) {
- return layer_state_set_user(state);
+ return layer_state_set_user(state);
}
/** \brief Layer state set
*
- * FIXME: Needs docs
+ * Sets the layer to match the specifed state (a bitmask)
*/
-void layer_state_set(uint32_t state)
-{
- state = layer_state_set_kb(state);
- dprint("layer_state: ");
- layer_debug(); dprint(" to ");
- layer_state = state;
- layer_debug(); dprintln();
+void layer_state_set(uint32_t state) {
+ state = layer_state_set_kb(state);
+ dprint("layer_state: ");
+ layer_debug(); dprint(" to ");
+ layer_state = state;
+ layer_debug(); dprintln();
#ifdef STRICT_LAYER_RELEASE
- clear_keyboard_but_mods(); // To avoid stuck keys
+ clear_keyboard_but_mods(); // To avoid stuck keys
#else
- clear_keyboard_but_mods_and_keys(); // Don't reset held keys
+ clear_keyboard_but_mods_and_keys(); // Don't reset held keys
#endif
}
/** \brief Layer clear
*
- * FIXME: Needs docs
+ * Turn off all layers
*/
-void layer_clear(void)
-{
- layer_state_set(0);
+void layer_clear(void) {
+ layer_state_set(0);
}
/** \brief Layer state is
*
- * FIXME: Needs docs
+ * Return whether the given state is on (it might still be shadowed by a higher state, though)
*/
-bool layer_state_is(uint8_t layer)
-{
- return layer_state_cmp(layer_state, layer);
+bool layer_state_is(uint8_t layer) {
+ return layer_state_cmp(layer_state, layer);
}
/** \brief Layer state compare
*
- * FIXME: Needs docs
+ * Used for comparing layers {mostly used for unit testing}
*/
bool layer_state_cmp(uint32_t cmp_layer_state, uint8_t layer) {
- if (!cmp_layer_state) { return layer == 0; }
- return (cmp_layer_state & (1UL<<layer)) != 0;
+ if (!cmp_layer_state) { return layer == 0; }
+ return (cmp_layer_state & (1UL<<layer)) != 0;
}
/** \brief Layer move
*
- * FIXME: Needs docs
+ * Turns on the given layer and turn off all other layers
*/
-void layer_move(uint8_t layer)
-{
- layer_state_set(1UL<<layer);
+void layer_move(uint8_t layer) {
+ layer_state_set(1UL<<layer);
}
/** \brief Layer on
*
- * FIXME: Needs docs
+ * Turns on given layer
*/
-void layer_on(uint8_t layer)
-{
- layer_state_set(layer_state | (1UL<<layer));
+void layer_on(uint8_t layer) {
+ layer_state_set(layer_state | (1UL<<layer));
}
/** \brief Layer off
*
- * FIXME: Needs docs
+ * Turns off given layer
*/
-void layer_off(uint8_t layer)
-{
- layer_state_set(layer_state & ~(1UL<<layer));
+void layer_off(uint8_t layer) {
+ layer_state_set(layer_state & ~(1UL<<layer));
}
/** \brief Layer invert
*
- * FIXME: Needs docs
+ * Toggle the given layer (set it if it's unset, or unset it if it's set)
*/
-void layer_invert(uint8_t layer)
-{
- layer_state_set(layer_state ^ (1UL<<layer));
+void layer_invert(uint8_t layer) {
+ layer_state_set(layer_state ^ (1UL<<layer));
}
/** \brief Layer or
*
- * FIXME: Needs docs
+ * Turns on layers based on matching bits between specifed layer and existing layer state
*/
-void layer_or(uint32_t state)
-{
- layer_state_set(layer_state | state);
+void layer_or(uint32_t state) {
+ layer_state_set(layer_state | state);
}
/** \brief Layer and
*
- * FIXME: Needs docs
+ * Turns on layers based on matching enabled bits between specifed layer and existing layer state
*/
-void layer_and(uint32_t state)
-{
- layer_state_set(layer_state & state);
+void layer_and(uint32_t state) {
+ layer_state_set(layer_state & state);
}
/** \brief Layer xor
*
- * FIXME: Needs docs
+ * Turns on layers based on non-matching bits between specifed layer and existing layer state
*/
-void layer_xor(uint32_t state)
-{
- layer_state_set(layer_state ^ state);
+void layer_xor(uint32_t state) {
+ layer_state_set(layer_state ^ state);
}
/** \brief Layer debug printing
*
- * FIXME: Needs docs
+ * Print out the hex value of the 32-bit layer state, as well as the value of the highest bit.
*/
-void layer_debug(void)
-{
- dprintf("%08lX(%u)", layer_state, biton32(layer_state));
+void layer_debug(void) {
+ dprintf("%08lX(%u)", layer_state, biton32(layer_state));
}
#endif
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
+/** \brief source layer cache
+ */
+
uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}};
-void update_source_layers_cache(keypos_t key, uint8_t layer)
-{
- const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
- const uint8_t storage_row = key_number / 8;
- const uint8_t storage_bit = key_number % 8;
-
- for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
- source_layers_cache[storage_row][bit_number] ^=
- (-((layer & (1U << bit_number)) != 0)
- ^ source_layers_cache[storage_row][bit_number])
- & (1U << storage_bit);
- }
+/** \brief update source layers cache
+ *
+ * Updates the cached keys when changing layers
+ */
+void update_source_layers_cache(keypos_t key, uint8_t layer) {
+ const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
+ const uint8_t storage_row = key_number / 8;
+ const uint8_t storage_bit = key_number % 8;
+
+ for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
+ source_layers_cache[storage_row][bit_number] ^=
+ (-((layer & (1U << bit_number)) != 0)
+ ^ source_layers_cache[storage_row][bit_number])
+ & (1U << storage_bit);
+ }
}
-uint8_t read_source_layers_cache(keypos_t key)
-{
- const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
- const uint8_t storage_row = key_number / 8;
- const uint8_t storage_bit = key_number % 8;
- uint8_t layer = 0;
-
- for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
- layer |=
- ((source_layers_cache[storage_row][bit_number]
- & (1U << storage_bit)) != 0)
- << bit_number;
- }
-
- return layer;
+/** \brief read source layers cache
+ *
+ * reads the cached keys stored when the layer was changed
+ */
+uint8_t read_source_layers_cache(keypos_t key) {
+ const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
+ const uint8_t storage_row = key_number / 8;
+ const uint8_t storage_bit = key_number % 8;
+ uint8_t layer = 0;
+
+ for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
+ layer |=
+ ((source_layers_cache[storage_row][bit_number]
+ & (1U << storage_bit)) != 0)
+ << bit_number;
+ }
+
+ return layer;
}
#endif
@@ -278,61 +270,58 @@ uint8_t read_source_layers_cache(keypos_t key)
* when the layer is switched after the down event but before the up
* event as they may get stuck otherwise.
*/
-action_t store_or_get_action(bool pressed, keypos_t key)
-{
+action_t store_or_get_action(bool pressed, keypos_t key) {
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
- if (disable_action_cache) {
- return layer_switch_get_action(key);
- }
-
- uint8_t layer;
-
- if (pressed) {
- layer = layer_switch_get_layer(key);
- update_source_layers_cache(key, layer);
- }
- else {
- layer = read_source_layers_cache(key);
- }
- return action_for_key(layer, key);
-#else
+ if (disable_action_cache) {
return layer_switch_get_action(key);
+ }
+
+ uint8_t layer;
+
+ if (pressed) {
+ layer = layer_switch_get_layer(key);
+ update_source_layers_cache(key, layer);
+ }
+ else {
+ layer = read_source_layers_cache(key);
+ }
+ return action_for_key(layer, key);
+#else
+ return layer_switch_get_action(key);
#endif
}
/** \brief Layer switch get layer
*
- * FIXME: Needs docs
+ * Gets the layer based on key info
*/
-int8_t layer_switch_get_layer(keypos_t key)
-{
+int8_t layer_switch_get_layer(keypos_t key) {
#ifndef NO_ACTION_LAYER
- action_t action;
- action.code = ACTION_TRANSPARENT;
-
- uint32_t layers = layer_state | default_layer_state;
- /* check top layer first */
- for (int8_t i = 31; i >= 0; i--) {
- if (layers & (1UL<<i)) {
- action = action_for_key(i, key);
- if (action.code != ACTION_TRANSPARENT) {
- return i;
- }
- }
+ action_t action;
+ action.code = ACTION_TRANSPARENT;
+
+ uint32_t layers = layer_state | default_layer_state;
+ /* check top layer first */
+ for (int8_t i = 31; i >= 0; i--) {
+ if (layers & (1UL<<i)) {
+ action = action_for_key(i, key);
+ if (action.code != ACTION_TRANSPARENT) {
+ return i;
+ }
}
- /* fall back to layer 0 */
- return 0;
+ }
+ /* fall back to layer 0 */
+ return 0;
#else
- return biton32(default_layer_state);
+ return biton32(default_layer_state);
#endif
}
/** \brief Layer switch get layer
*
- * FIXME: Needs docs
+ * Gets action code based on key position
*/
-action_t layer_switch_get_action(keypos_t key)
-{
- return action_for_key(layer_switch_get_layer(key), key);
+action_t layer_switch_get_action(keypos_t key) {
+ return action_for_key(layer_switch_get_layer(key), key);
}
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index afd4ae8b25..58401ace55 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -54,7 +54,7 @@ int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
-static int16_t oneshot_time = 0;
+static uint16_t oneshot_time = 0;
bool has_oneshot_mods_timed_out(void) {
return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
}
@@ -79,7 +79,7 @@ inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
-static int16_t oneshot_layer_time = 0;
+static uint16_t oneshot_layer_time = 0;
inline bool has_oneshot_layer_timed_out() {
return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
!(get_oneshot_layer_state() & ONESHOT_TOGGLED);
diff --git a/tmk_core/common/arm_atsam/timer.c b/tmk_core/common/arm_atsam/timer.c
index bcfe5002c3..6c3905e308 100644
--- a/tmk_core/common/arm_atsam/timer.c
+++ b/tmk_core/common/arm_atsam/timer.c
@@ -9,7 +9,7 @@ void set_time(uint64_t tset)
void timer_init(void)
{
- ms_clk = 0;
+ timer_clear();
}
uint16_t timer_read(void)
@@ -37,23 +37,7 @@ uint32_t timer_elapsed32(uint32_t tlast)
return TIMER_DIFF_32(timer_read32(), tlast);
}
-uint32_t timer_elapsed64(uint32_t tlast)
-{
- uint64_t tnow = timer_read64();
- return (tnow >= tlast ? tnow - tlast : UINT64_MAX - tlast + tnow);
-}
-
void timer_clear(void)
{
- ms_clk = 0;
-}
-
-void wait_ms(uint64_t msec)
-{
- CLK_delay_ms(msec);
-}
-
-void wait_us(uint16_t usec)
-{
- CLK_delay_us(usec);
+ set_time(0);
}
diff --git a/tmk_core/common/chibios/eeprom_stm32.c b/tmk_core/common/chibios/eeprom_stm32.c
index a869985501..a15430d676 100755
--- a/tmk_core/common/chibios/eeprom_stm32.c
+++ b/tmk_core/common/chibios/eeprom_stm32.c
@@ -10,664 +10,206 @@
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
- * This files are free to use from https://github.com/rogerclarkmelbourne/Arduino_STM32 and
- * https://github.com/leaflabs/libmaple
+ * This files are free to use from http://engsta.com/stm32-flash-memory-eeprom-emulator/ by
+ * Artur F.
*
* Modifications for QMK and STM32F303 by Yiancar
*/
+#include <stdio.h>
+#include <string.h>
#include "eeprom_stm32.h"
+/*****************************************************************************
+ * Allows to use the internal flash to store non volatile data. To initialize
+ * the functionality use the EEPROM_Init() function. Be sure that by reprogramming
+ * of the controller just affected pages will be deleted. In other case the non
+ * volatile data will be lost.
+******************************************************************************/
+
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+/* Functions -----------------------------------------------------------------*/
+
+uint8_t DataBuf[FEE_PAGE_SIZE];
+/*****************************************************************************
+* Delete Flash Space used for user Data, deletes the whole space between
+* RW_PAGE_BASE_ADDRESS and the last uC Flash Page
+******************************************************************************/
+uint16_t EEPROM_Init(void) {
+ // unlock flash
+ FLASH_Unlock();
- FLASH_Status EE_ErasePage(uint32_t);
-
- uint16_t EE_CheckPage(uint32_t, uint16_t);
- uint16_t EE_CheckErasePage(uint32_t, uint16_t);
- uint16_t EE_Format(void);
- uint32_t EE_FindValidPage(void);
- uint16_t EE_GetVariablesCount(uint32_t, uint16_t);
- uint16_t EE_PageTransfer(uint32_t, uint32_t, uint16_t);
- uint16_t EE_VerifyPageFullWriteVariable(uint16_t, uint16_t);
-
- uint32_t PageBase0 = EEPROM_PAGE0_BASE;
- uint32_t PageBase1 = EEPROM_PAGE1_BASE;
- uint32_t PageSize = EEPROM_PAGE_SIZE;
- uint16_t Status = EEPROM_NOT_INIT;
-
-// See http://www.st.com/web/en/resource/technical/document/application_note/CD00165693.pdf
-
-/**
- * @brief Check page for blank
- * @param page base address
- * @retval Success or error
- * EEPROM_BAD_FLASH: page not empty after erase
- * EEPROM_OK: page blank
- */
-uint16_t EE_CheckPage(uint32_t pageBase, uint16_t status)
-{
- uint32_t pageEnd = pageBase + (uint32_t)PageSize;
-
- // Page Status not EEPROM_ERASED and not a "state"
- if ((*(__IO uint16_t*)pageBase) != EEPROM_ERASED && (*(__IO uint16_t*)pageBase) != status)
- return EEPROM_BAD_FLASH;
- for(pageBase += 4; pageBase < pageEnd; pageBase += 4)
- if ((*(__IO uint32_t*)pageBase) != 0xFFFFFFFF) // Verify if slot is empty
- return EEPROM_BAD_FLASH;
- return EEPROM_OK;
-}
-
-/**
- * @brief Erase page with increment erase counter (page + 2)
- * @param page base address
- * @retval Success or error
- * FLASH_COMPLETE: success erase
- * - Flash error code: on write Flash error
- */
-FLASH_Status EE_ErasePage(uint32_t pageBase)
-{
- FLASH_Status FlashStatus;
- uint16_t data = (*(__IO uint16_t*)(pageBase));
- if ((data == EEPROM_ERASED) || (data == EEPROM_VALID_PAGE) || (data == EEPROM_RECEIVE_DATA))
- data = (*(__IO uint16_t*)(pageBase + 2)) + 1;
- else
- data = 0;
-
- FlashStatus = FLASH_ErasePage(pageBase);
- if (FlashStatus == FLASH_COMPLETE)
- FlashStatus = FLASH_ProgramHalfWord(pageBase + 2, data);
-
- return FlashStatus;
-}
+ // Clear Flags
+ //FLASH_ClearFlag(FLASH_SR_EOP|FLASH_SR_PGERR|FLASH_SR_WRPERR);
-/**
- * @brief Check page for blank and erase it
- * @param page base address
- * @retval Success or error
- * - Flash error code: on write Flash error
- * - EEPROM_BAD_FLASH: page not empty after erase
- * - EEPROM_OK: page blank
- */
-uint16_t EE_CheckErasePage(uint32_t pageBase, uint16_t status)
-{
- uint16_t FlashStatus;
- if (EE_CheckPage(pageBase, status) != EEPROM_OK)
- {
- FlashStatus = EE_ErasePage(pageBase);
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
- return EE_CheckPage(pageBase, status);
- }
- return EEPROM_OK;
+ return FEE_DENSITY_BYTES;
}
+/*****************************************************************************
+* Erase the whole reserved Flash Space used for user Data
+******************************************************************************/
+void EEPROM_Erase (void) {
-/**
- * @brief Find valid Page for write or read operation
- * @param Page0: Page0 base address
- * Page1: Page1 base address
- * @retval Valid page address (PAGE0 or PAGE1) or NULL in case of no valid page was found
- */
-uint32_t EE_FindValidPage(void)
-{
- uint16_t status0 = (*(__IO uint16_t*)PageBase0); // Get Page0 actual status
- uint16_t status1 = (*(__IO uint16_t*)PageBase1); // Get Page1 actual status
-
- if (status0 == EEPROM_VALID_PAGE && status1 == EEPROM_ERASED)
- return PageBase0;
- if (status1 == EEPROM_VALID_PAGE && status0 == EEPROM_ERASED)
- return PageBase1;
-
- return 0;
-}
+ int page_num = 0;
-/**
- * @brief Calculate unique variables in EEPROM
- * @param start: address of first slot to check (page + 4)
- * @param end: page end address
- * @param address: 16 bit virtual address of the variable to excluse (or 0XFFFF)
- * @retval count of variables
- */
-uint16_t EE_GetVariablesCount(uint32_t pageBase, uint16_t skipAddress)
-{
- uint16_t varAddress, nextAddress;
- uint32_t idx;
- uint32_t pageEnd = pageBase + (uint32_t)PageSize;
- uint16_t count = 0;
-
- for (pageBase += 6; pageBase < pageEnd; pageBase += 4)
- {
- varAddress = (*(__IO uint16_t*)pageBase);
- if (varAddress == 0xFFFF || varAddress == skipAddress)
- continue;
-
- count++;
- for(idx = pageBase + 4; idx < pageEnd; idx += 4)
- {
- nextAddress = (*(__IO uint16_t*)idx);
- if (nextAddress == varAddress)
- {
- count--;
- break;
- }
- }
- }
- return count;
+ // delete all pages from specified start page to the last page
+ do {
+ FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE));
+ page_num++;
+ } while (page_num < FEE_DENSITY_PAGES);
}
+/*****************************************************************************
+* Writes once data byte to flash on specified address. If a byte is already
+* written, the whole page must be copied to a buffer, the byte changed and
+* the manipulated buffer written after PageErase.
+*******************************************************************************/
+uint16_t EEPROM_WriteDataByte (uint16_t Address, uint8_t DataByte) {
-/**
- * @brief Transfers last updated variables data from the full Page to an empty one.
- * @param newPage: new page base address
- * @param oldPage: old page base address
- * @param SkipAddress: 16 bit virtual address of the variable (or 0xFFFF)
- * @retval Success or error status:
- * - FLASH_COMPLETE: on success
- * - EEPROM_OUT_SIZE: if valid new page is full
- * - Flash error code: on write Flash error
- */
-uint16_t EE_PageTransfer(uint32_t newPage, uint32_t oldPage, uint16_t SkipAddress)
-{
- uint32_t oldEnd, newEnd;
- uint32_t oldIdx, newIdx, idx;
- uint16_t address, data, found;
- FLASH_Status FlashStatus;
-
- // Transfer process: transfer variables from old to the new active page
- newEnd = newPage + ((uint32_t)PageSize);
-
- // Find first free element in new page
- for (newIdx = newPage + 4; newIdx < newEnd; newIdx += 4)
- if ((*(__IO uint32_t*)newIdx) == 0xFFFFFFFF) // Verify if element
- break; // contents are 0xFFFFFFFF
- if (newIdx >= newEnd)
- return EEPROM_OUT_SIZE;
-
- oldEnd = oldPage + 4;
- oldIdx = oldPage + (uint32_t)(PageSize - 2);
-
- for (; oldIdx > oldEnd; oldIdx -= 4)
- {
- address = *(__IO uint16_t*)oldIdx;
- if (address == 0xFFFF || address == SkipAddress)
- continue; // it's means that power off after write data
-
- found = 0;
- for (idx = newPage + 6; idx < newIdx; idx += 4)
- if ((*(__IO uint16_t*)(idx)) == address)
- {
- found = 1;
- break;
- }
-
- if (found)
- continue;
-
- if (newIdx < newEnd)
- {
- data = (*(__IO uint16_t*)(oldIdx - 2));
-
- FlashStatus = FLASH_ProgramHalfWord(newIdx, data);
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
+ FLASH_Status FlashStatus = FLASH_COMPLETE;
- FlashStatus = FLASH_ProgramHalfWord(newIdx + 2, address);
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
+ uint32_t page;
+ int i;
- newIdx += 4;
- }
- else
- return EEPROM_OUT_SIZE;
+ // exit if desired address is above the limit (e.G. under 2048 Bytes for 4 pages)
+ if (Address > FEE_DENSITY_BYTES) {
+ return 0;
}
- // Erase the old Page: Set old Page status to EEPROM_EEPROM_ERASED status
- data = EE_CheckErasePage(oldPage, EEPROM_ERASED);
- if (data != EEPROM_OK)
- return data;
+ // calculate which page is affected (Pagenum1/Pagenum2...PagenumN)
+ page = (FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address)) & 0x00000FFF;
- // Set new Page status
- FlashStatus = FLASH_ProgramHalfWord(newPage, EEPROM_VALID_PAGE);
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
+ if (page % FEE_PAGE_SIZE) page = page + FEE_PAGE_SIZE;
+ page = (page / FEE_PAGE_SIZE) - 1;
- return EEPROM_OK;
-}
+ // if current data is 0xFF, the byte is empty, just overwrite with the new one
+ if ((*(__IO uint16_t*)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) {
-/**
- * @brief Verify if active page is full and Writes variable in EEPROM.
- * @param Address: 16 bit virtual address of the variable
- * @param Data: 16 bit data to be written as variable value
- * @retval Success or error status:
- * - FLASH_COMPLETE: on success
- * - EEPROM_PAGE_FULL: if valid page is full (need page transfer)
- * - EEPROM_NO_VALID_PAGE: if no valid page was found
- * - EEPROM_OUT_SIZE: if EEPROM size exceeded
- * - Flash error code: on write Flash error
- */
-uint16_t EE_VerifyPageFullWriteVariable(uint16_t Address, uint16_t Data)
-{
- FLASH_Status FlashStatus;
- uint32_t idx, pageBase, pageEnd, newPage;
- uint16_t count;
-
- // Get valid Page for write operation
- pageBase = EE_FindValidPage();
- if (pageBase == 0)
- return EEPROM_NO_VALID_PAGE;
-
- // Get the valid Page end Address
- pageEnd = pageBase + PageSize; // Set end of page
-
- for (idx = pageEnd - 2; idx > pageBase; idx -= 4)
- {
- if ((*(__IO uint16_t*)idx) == Address) // Find last value for address
- {
- count = (*(__IO uint16_t*)(idx - 2)); // Read last data
- if (count == Data)
- return EEPROM_OK;
- if (count == 0xFFFF)
- {
- FlashStatus = FLASH_ProgramHalfWord(idx - 2, Data); // Set variable data
- if (FlashStatus == FLASH_COMPLETE)
- return EEPROM_OK;
- }
- break;
- }
+ FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte));
}
+ else {
- // Check each active page address starting from begining
- for (idx = pageBase + 4; idx < pageEnd; idx += 4)
- if ((*(__IO uint32_t*)idx) == 0xFFFFFFFF) // Verify if element
- { // contents are 0xFFFFFFFF
- FlashStatus = FLASH_ProgramHalfWord(idx, Data); // Set variable data
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
- FlashStatus = FLASH_ProgramHalfWord(idx + 2, Address); // Set variable virtual address
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
- return EEPROM_OK;
- }
-
- // Empty slot not found, need page transfer
- // Calculate unique variables in page
- count = EE_GetVariablesCount(pageBase, Address) + 1;
- if (count >= (PageSize / 4 - 1))
- return EEPROM_OUT_SIZE;
-
- if (pageBase == PageBase1)
- newPage = PageBase0; // New page address where variable will be moved to
- else
- newPage = PageBase1;
-
- // Set the new Page status to RECEIVE_DATA status
- FlashStatus = FLASH_ProgramHalfWord(newPage, EEPROM_RECEIVE_DATA);
- if (FlashStatus != FLASH_COMPLETE)
- return FlashStatus;
-
- // Write the variable passed as parameter in the new active page
- FlashStatus = FLASH_ProgramHalfWord(newPage + 4, Data);
- if (FlashStatus != FLASH_COMPLETE)