init
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
app_data.cpp@51:9078e6928412, 2019-05-23 (annotated)
- Committer:
- pathfindr
- Date:
- Thu May 23 11:39:28 2019 +0000
- Revision:
- 51:9078e6928412
- Parent:
- 34:4493c9f6d707
init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pathfindr | 34:4493c9f6d707 | 1 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 2 | //This module: |
pathfindr | 34:4493c9f6d707 | 3 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 4 | #include "app_data.h" |
pathfindr | 34:4493c9f6d707 | 5 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 6 | //LOCAL Macros |
pathfindr | 34:4493c9f6d707 | 7 | FlashIAP flash; |
pathfindr | 34:4493c9f6d707 | 8 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 9 | //debugging state |
pathfindr | 34:4493c9f6d707 | 10 | #define DEBUG_THIS DEBUG_APP_DATA |
pathfindr | 34:4493c9f6d707 | 11 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 12 | //IO pin setup |
pathfindr | 34:4493c9f6d707 | 13 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 14 | //NONE |
pathfindr | 34:4493c9f6d707 | 15 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 16 | //GLOBAL Variables |
pathfindr | 34:4493c9f6d707 | 17 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 18 | app_data_t app_data; |
pathfindr | 34:4493c9f6d707 | 19 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 20 | //LOCAL Data structures |
pathfindr | 34:4493c9f6d707 | 21 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 22 | //NONE |
pathfindr | 34:4493c9f6d707 | 23 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 24 | //LOCAL Constants |
pathfindr | 34:4493c9f6d707 | 25 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 26 | //NONE |
pathfindr | 34:4493c9f6d707 | 27 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 28 | //LOCAL Variables |
pathfindr | 34:4493c9f6d707 | 29 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 30 | //NONE |
pathfindr | 34:4493c9f6d707 | 31 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 32 | //LOCAL Function prototypes |
pathfindr | 34:4493c9f6d707 | 33 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 34 | //NONE |
pathfindr | 34:4493c9f6d707 | 35 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 36 | //LOCAL Function definitions |
pathfindr | 34:4493c9f6d707 | 37 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 38 | //NONE |
pathfindr | 34:4493c9f6d707 | 39 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 40 | //GLOBAL Function definitions |
pathfindr | 34:4493c9f6d707 | 41 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 42 | extern bool set_flag(app_data_t *ptr, uint32_t flag) |
pathfindr | 34:4493c9f6d707 | 43 | { |
pathfindr | 34:4493c9f6d707 | 44 | ptr->status_flags |= flag; |
pathfindr | 34:4493c9f6d707 | 45 | return 0; |
pathfindr | 34:4493c9f6d707 | 46 | } |
pathfindr | 34:4493c9f6d707 | 47 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 48 | extern bool get_flag(app_data_t *ptr, uint32_t flag) |
pathfindr | 34:4493c9f6d707 | 49 | { |
pathfindr | 34:4493c9f6d707 | 50 | bool result; |
pathfindr | 34:4493c9f6d707 | 51 | result = (ptr->status_flags & flag); |
pathfindr | 34:4493c9f6d707 | 52 | return result; |
pathfindr | 34:4493c9f6d707 | 53 | } |
pathfindr | 34:4493c9f6d707 | 54 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 55 | extern bool clr_flag(app_data_t *ptr, uint32_t flag) |
pathfindr | 34:4493c9f6d707 | 56 | { |
pathfindr | 34:4493c9f6d707 | 57 | ptr->status_flags &= ~flag; |
pathfindr | 34:4493c9f6d707 | 58 | return 0; |
pathfindr | 34:4493c9f6d707 | 59 | } |
pathfindr | 34:4493c9f6d707 | 60 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 61 | extern bool tgl_flag(app_data_t *ptr, uint32_t flag) |
pathfindr | 34:4493c9f6d707 | 62 | { |
pathfindr | 34:4493c9f6d707 | 63 | ptr->status_flags ^= flag; |
pathfindr | 34:4493c9f6d707 | 64 | return 0; |
pathfindr | 34:4493c9f6d707 | 65 | } |
pathfindr | 34:4493c9f6d707 | 66 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 67 | extern bool rst_flag_buffer(app_data_t *ptr) |
pathfindr | 34:4493c9f6d707 | 68 | { |
pathfindr | 34:4493c9f6d707 | 69 | ptr->status_flags = 0; |
pathfindr | 34:4493c9f6d707 | 70 | return 0; |
pathfindr | 34:4493c9f6d707 | 71 | } |
pathfindr | 34:4493c9f6d707 | 72 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 73 | extern bool write_app_data_to_flash(app_data_t *ptr) |
pathfindr | 34:4493c9f6d707 | 74 | //write the app data to the memory location defined by APPDATA_START |
pathfindr | 34:4493c9f6d707 | 75 | { |
pathfindr | 34:4493c9f6d707 | 76 | bool pass = false; |
pathfindr | 34:4493c9f6d707 | 77 | int8_t error = 0; //used to catch errors in the flash operations |
pathfindr | 51:9078e6928412 | 78 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Init 1");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 79 | error = flash.init(); |
pathfindr | 34:4493c9f6d707 | 80 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 81 | { |
pathfindr | 34:4493c9f6d707 | 82 | //DEBUG(DEBUG_THIS, "ERROR: flash init %d \r\n", error); |
pathfindr | 51:9078e6928412 | 83 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERROR: flash init %d",error);debug_exe(); |
pathfindr | 34:4493c9f6d707 | 84 | return false; |
pathfindr | 34:4493c9f6d707 | 85 | } |
pathfindr | 34:4493c9f6d707 | 86 | const uint32_t page_size = flash.get_page_size(); |
pathfindr | 34:4493c9f6d707 | 87 | char page_buffer[page_size]; |
pathfindr | 34:4493c9f6d707 | 88 | uint32_t addr = APPDATA_START; |
pathfindr | 34:4493c9f6d707 | 89 | uint32_t value[1] = {0}; |
pathfindr | 34:4493c9f6d707 | 90 | uint32_t val = 0; |
pathfindr | 51:9078e6928412 | 91 | uint8_t setting = 1; |
pathfindr | 51:9078e6928412 | 92 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Init 2");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 93 | flash.init(); |
pathfindr | 34:4493c9f6d707 | 94 | // DEBUGGING APP DATA MEMORY CRASH |
pathfindr | 51:9078e6928412 | 95 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Erase");debug_exe(); |
pathfindr | 51:9078e6928412 | 96 | error = flash.erase(addr, flash.get_sector_size(addr)); |
pathfindr | 34:4493c9f6d707 | 97 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 98 | { |
pathfindr | 34:4493c9f6d707 | 99 | //DEBUG(DEBUG_THIS, "ERROR: flash erase %d \r\n", error); |
pathfindr | 51:9078e6928412 | 100 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERROR: flash erase %d",error);debug_exe(); |
pathfindr | 34:4493c9f6d707 | 101 | return false; |
pathfindr | 34:4493c9f6d707 | 102 | } |
pathfindr | 34:4493c9f6d707 | 103 | |
pathfindr | 34:4493c9f6d707 | 104 | //DEBUG(DEBUG_THIS, "WRITE VALUES FROM RAM TO FLASH... \r\n\n"); |
pathfindr | 51:9078e6928412 | 105 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Write 1");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 106 | for(setting=1; setting<=N_SETTINGS; setting++) |
pathfindr | 34:4493c9f6d707 | 107 | { |
pathfindr | 34:4493c9f6d707 | 108 | switch(setting) |
pathfindr | 34:4493c9f6d707 | 109 | { |
pathfindr | 34:4493c9f6d707 | 110 | case 1: val = ptr->current_firmware_version; break; |
pathfindr | 34:4493c9f6d707 | 111 | case 2: val = ptr->target_firmware_version; break; |
pathfindr | 34:4493c9f6d707 | 112 | case 3: val = ptr->app_execution_fail_counter; break; |
pathfindr | 34:4493c9f6d707 | 113 | case 4: val = ptr->blinky_state; break; |
pathfindr | 34:4493c9f6d707 | 114 | case 5: val = ptr->sleep_counter; break; |
pathfindr | 34:4493c9f6d707 | 115 | case 6: val = ptr->sleep_duration; break; |
pathfindr | 34:4493c9f6d707 | 116 | case 7: val = ptr->update_duration; break; |
pathfindr | 34:4493c9f6d707 | 117 | case 8: val = ptr->reset_counter; break; |
pathfindr | 34:4493c9f6d707 | 118 | case 9: val = ptr->wd_counter; break; |
pathfindr | 34:4493c9f6d707 | 119 | case 10: val = ptr->status_flags; break; |
pathfindr | 34:4493c9f6d707 | 120 | default: |
pathfindr | 34:4493c9f6d707 | 121 | //DEBUG(DEBUG_THIS, "Undefined state in function: %s() \r\n", __FUNCTION__); |
pathfindr | 34:4493c9f6d707 | 122 | break; |
pathfindr | 34:4493c9f6d707 | 123 | } |
pathfindr | 34:4493c9f6d707 | 124 | |
pathfindr | 34:4493c9f6d707 | 125 | //DEBUG(DEBUG_THIS, "COPY : %u to BUFFER \r\n", val); |
pathfindr | 34:4493c9f6d707 | 126 | //transfer the 32bit uint to the char page buffer |
pathfindr | 34:4493c9f6d707 | 127 | page_buffer[0] = (char)(val & 0x000000FF); |
pathfindr | 34:4493c9f6d707 | 128 | page_buffer[1] = (char)((val & 0x0000FF00) >> 8); |
pathfindr | 34:4493c9f6d707 | 129 | page_buffer[2] = (char)((val & 0x00FF0000) >> 16); |
pathfindr | 34:4493c9f6d707 | 130 | page_buffer[3] = (char)((val & 0xFF000000) >> 24); |
pathfindr | 34:4493c9f6d707 | 131 | |
pathfindr | 34:4493c9f6d707 | 132 | //transfer the page buffer to the flash memory |
pathfindr | 51:9078e6928412 | 133 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Write 2");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 134 | error = flash.program(page_buffer, addr, page_size); |
pathfindr | 34:4493c9f6d707 | 135 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 136 | { |
pathfindr | 34:4493c9f6d707 | 137 | //DEBUG(DEBUG_THIS, "ERROR: flash program %d \r\n", error); |
pathfindr | 51:9078e6928412 | 138 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERROR: flash program %d",error);debug_exe(); |
pathfindr | 34:4493c9f6d707 | 139 | return false; |
pathfindr | 34:4493c9f6d707 | 140 | } |
pathfindr | 34:4493c9f6d707 | 141 | //for debug check read back the value... |
pathfindr | 34:4493c9f6d707 | 142 | //doesn't need error checking, not essential. |
pathfindr | 34:4493c9f6d707 | 143 | memset(value, 0x00, 4); |
pathfindr | 34:4493c9f6d707 | 144 | flash.read(value, addr, 4); |
pathfindr | 34:4493c9f6d707 | 145 | //DEBUG(DEBUG_THIS, "READ FROM ADDR: 0x%08x \t VALUE: %u \r\n", addr, value[0]); |
pathfindr | 34:4493c9f6d707 | 146 | addr += page_size; |
pathfindr | 34:4493c9f6d707 | 147 | } |
pathfindr | 34:4493c9f6d707 | 148 | |
pathfindr | 51:9078e6928412 | 149 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Deinit");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 150 | error = flash.deinit(); |
pathfindr | 34:4493c9f6d707 | 151 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 152 | { |
pathfindr | 34:4493c9f6d707 | 153 | //DEBUG(DEBUG_THIS, "ERROR: flash deinit %d \r\n", error); |
pathfindr | 51:9078e6928412 | 154 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERROR: flash deinit %d",error);debug_exe(); |
pathfindr | 34:4493c9f6d707 | 155 | return false; |
pathfindr | 34:4493c9f6d707 | 156 | } |
pathfindr | 34:4493c9f6d707 | 157 | //DEBUG(DEBUG_THIS, "DONE \r\n"); |
pathfindr | 51:9078e6928412 | 158 | debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Flash - Done");debug_exe(); |
pathfindr | 34:4493c9f6d707 | 159 | return pass; |
pathfindr | 34:4493c9f6d707 | 160 | } |
pathfindr | 34:4493c9f6d707 | 161 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 162 | extern bool read_app_data_from_flash(app_data_t *ptr) |
pathfindr | 34:4493c9f6d707 | 163 | //read the app data from the memory location defined by APPDATA_START |
pathfindr | 34:4493c9f6d707 | 164 | //and restore to APP_DATA |
pathfindr | 34:4493c9f6d707 | 165 | { |
pathfindr | 34:4493c9f6d707 | 166 | bool pass = false; |
pathfindr | 34:4493c9f6d707 | 167 | int8_t error = 0; |
pathfindr | 34:4493c9f6d707 | 168 | error = flash.init(); |
pathfindr | 34:4493c9f6d707 | 169 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 170 | { |
pathfindr | 34:4493c9f6d707 | 171 | //DEBUG(DEBUG_THIS, "ERROR: flash init %d \r\n", error); |
pathfindr | 34:4493c9f6d707 | 172 | return false; |
pathfindr | 34:4493c9f6d707 | 173 | } |
pathfindr | 34:4493c9f6d707 | 174 | const uint32_t page_size = flash.get_page_size(); |
pathfindr | 34:4493c9f6d707 | 175 | uint32_t addr = APPDATA_START; |
pathfindr | 34:4493c9f6d707 | 176 | uint32_t value[1] = {0}; |
pathfindr | 34:4493c9f6d707 | 177 | uint8_t setting = 1; |
pathfindr | 34:4493c9f6d707 | 178 | |
pathfindr | 34:4493c9f6d707 | 179 | //DEBUG(DEBUG_THIS, "\r\nLOAD VALUES TO RAM FROM FLASH... \r\n"); |
pathfindr | 34:4493c9f6d707 | 180 | |
pathfindr | 34:4493c9f6d707 | 181 | for(setting=1; setting<=N_SETTINGS; setting++) |
pathfindr | 34:4493c9f6d707 | 182 | { |
pathfindr | 34:4493c9f6d707 | 183 | error = flash.read(value, addr, 4); |
pathfindr | 34:4493c9f6d707 | 184 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 185 | { |
pathfindr | 34:4493c9f6d707 | 186 | //DEBUG(DEBUG_THIS, "ERROR: flash read %d \r\n", error); |
pathfindr | 34:4493c9f6d707 | 187 | return false; |
pathfindr | 34:4493c9f6d707 | 188 | } |
pathfindr | 34:4493c9f6d707 | 189 | switch(setting) |
pathfindr | 34:4493c9f6d707 | 190 | { |
pathfindr | 34:4493c9f6d707 | 191 | case 1: ptr->current_firmware_version = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 192 | case 2: ptr->target_firmware_version = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 193 | case 3: ptr->app_execution_fail_counter = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 194 | case 4: ptr->blinky_state = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 195 | case 5: ptr->sleep_counter = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 196 | case 6: ptr->sleep_duration = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 197 | case 7: ptr->update_duration = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 198 | case 8: ptr->reset_counter = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 199 | case 9: ptr->wd_counter = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 200 | case 10: ptr->status_flags = value[0]; break; |
pathfindr | 34:4493c9f6d707 | 201 | default: |
pathfindr | 34:4493c9f6d707 | 202 | //DEBUG(DEBUG_THIS, "Undefined state in function: %s() \r\n", __FUNCTION__); |
pathfindr | 34:4493c9f6d707 | 203 | break; |
pathfindr | 34:4493c9f6d707 | 204 | } |
pathfindr | 34:4493c9f6d707 | 205 | //DEBUG(DEBUG_THIS, "READ FROM ADDR: 0x%08x \t VALUE: %u \r\n", addr, value[0]); |
pathfindr | 34:4493c9f6d707 | 206 | addr += page_size; |
pathfindr | 34:4493c9f6d707 | 207 | } |
pathfindr | 34:4493c9f6d707 | 208 | |
pathfindr | 34:4493c9f6d707 | 209 | error = flash.deinit(); |
pathfindr | 34:4493c9f6d707 | 210 | if(error != 0) |
pathfindr | 34:4493c9f6d707 | 211 | { |
pathfindr | 34:4493c9f6d707 | 212 | //DEBUG(DEBUG_THIS, "ERROR: flash deinit %d \r\n", error); |
pathfindr | 34:4493c9f6d707 | 213 | return false; |
pathfindr | 34:4493c9f6d707 | 214 | } |
pathfindr | 34:4493c9f6d707 | 215 | //DEBUG(DEBUG_THIS, "DONE \r\n"); |
pathfindr | 34:4493c9f6d707 | 216 | return pass; |
pathfindr | 34:4493c9f6d707 | 217 | } |
pathfindr | 34:4493c9f6d707 | 218 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 219 | extern void clear_app_data(app_data_t *ptr) |
pathfindr | 34:4493c9f6d707 | 220 | //reset the app data values |
pathfindr | 34:4493c9f6d707 | 221 | { |
pathfindr | 34:4493c9f6d707 | 222 | ptr->current_firmware_version = 0; |
pathfindr | 34:4493c9f6d707 | 223 | ptr->target_firmware_version = 0; |
pathfindr | 34:4493c9f6d707 | 224 | ptr->app_execution_fail_counter = 0; |
pathfindr | 34:4493c9f6d707 | 225 | ptr->blinky_state = 0; |
pathfindr | 34:4493c9f6d707 | 226 | ptr->sleep_counter = 0; |
pathfindr | 34:4493c9f6d707 | 227 | ptr->sleep_duration = 0; |
pathfindr | 34:4493c9f6d707 | 228 | ptr->update_duration = 0; |
pathfindr | 34:4493c9f6d707 | 229 | ptr->reset_counter = 0; |
pathfindr | 34:4493c9f6d707 | 230 | ptr->wd_counter = 0; |
pathfindr | 34:4493c9f6d707 | 231 | ptr->status_flags = 0; |
pathfindr | 34:4493c9f6d707 | 232 | return; |
pathfindr | 34:4493c9f6d707 | 233 | } |
pathfindr | 34:4493c9f6d707 | 234 | //------------------------------------------------------------------------------ |
pathfindr | 34:4493c9f6d707 | 235 | extern void set_app_data(app_data_t *ptr) |
pathfindr | 34:4493c9f6d707 | 236 | //test function - load some initial values for testing |
pathfindr | 34:4493c9f6d707 | 237 | { |
pathfindr | 34:4493c9f6d707 | 238 | ptr->current_firmware_version = 0xFFFF0000; |
pathfindr | 34:4493c9f6d707 | 239 | ptr->target_firmware_version = 0x0000FFFF; |
pathfindr | 34:4493c9f6d707 | 240 | ptr->app_execution_fail_counter = 0x00FFFF00; |
pathfindr | 34:4493c9f6d707 | 241 | //set flags in one go... |
pathfindr | 34:4493c9f6d707 | 242 | ptr->status_flags = 0xAAAAAAAA; //flag - load from bank 2 to bank 1. |
pathfindr | 34:4493c9f6d707 | 243 | //...or one at a time |
pathfindr | 34:4493c9f6d707 | 244 | set_flag(ptr, rollback_flag); |
pathfindr | 34:4493c9f6d707 | 245 | clr_flag(ptr, app_execution_flag); |
pathfindr | 34:4493c9f6d707 | 246 | tgl_flag(ptr, registered_flag); |
pathfindr | 34:4493c9f6d707 | 247 | set_flag(ptr, first_run_flag); |
pathfindr | 34:4493c9f6d707 | 248 | return; |
pathfindr | 34:4493c9f6d707 | 249 | } |
pathfindr | 34:4493c9f6d707 | 250 | //------------------------------------------------------------------------------ |