Sample application that shows the use of persistent storage (pstorage) in Nordic nRF51822 tigether with a BLE service

Dependencies:   BLE_API mbed

Simple example that shows the use of pstorage (persistent storage) in nRF51822

The code is based on an example proposed at Nordic Developer Zone:

https://devzone.nordicsemi.com/question/15271/how-can-i-write-10kb-of-data-to-internal-flash/?answer=17300#post-id-17300

Committer:
tumaku
Date:
Thu Mar 05 14:55:52 2015 +0000
Revision:
1:b8cefa34f89d
Parent:
0:49d5cce77458
Example of pstaorage in Nordic nRF51822 together with BLE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tumaku 0:49d5cce77458 1 // Sample app that shows the use of persistent staprage in nRF51822 (pstorage) together with BLE
tumaku 0:49d5cce77458 2 // Based on the sample code proposed at Nordic developers forum:
tumaku 0:49d5cce77458 3 // https://devzone.nordicsemi.com/question/15271/how-can-i-write-10kb-of-data-to-internal-flash/?answer=17300#post-id-17300
tumaku 0:49d5cce77458 4 // https://devzone.nordicsemi.com/attachment/9b7ebaa65cd8f4f0eeb9d8fd99937c54
tumaku 0:49d5cce77458 5
tumaku 0:49d5cce77458 6 #include "mbed.h"
tumaku 0:49d5cce77458 7
tumaku 0:49d5cce77458 8 #include "BLEDevice.h"
tumaku 0:49d5cce77458 9
tumaku 0:49d5cce77458 10 #include "UARTService.h"
tumaku 0:49d5cce77458 11
tumaku 0:49d5cce77458 12 #include "pstorage.h"
tumaku 0:49d5cce77458 13
tumaku 0:49d5cce77458 14 #include "nrf_error.h"
tumaku 0:49d5cce77458 15
tumaku 0:49d5cce77458 16
tumaku 0:49d5cce77458 17 DigitalOut magnet(p15);
tumaku 0:49d5cce77458 18
tumaku 0:49d5cce77458 19 #define MAGNET_ON 1
tumaku 0:49d5cce77458 20 #define MAGNET_OFF 0
tumaku 0:49d5cce77458 21 Ticker ticker;
tumaku 0:49d5cce77458 22 int tickerState=MAGNET_OFF;
tumaku 0:49d5cce77458 23
tumaku 0:49d5cce77458 24 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
tumaku 0:49d5cce77458 25 * it will have an impact on code-size and power consumption. */
tumaku 0:49d5cce77458 26
tumaku 0:49d5cce77458 27 #if NEED_CONSOLE_OUTPUT
tumaku 0:49d5cce77458 28 #define DEBUG(...) { printf(__VA_ARGS__); } //Defaults to stdio without having to wirte pcUart explicitly
tumaku 0:49d5cce77458 29 #else
tumaku 0:49d5cce77458 30 #define DEBUG(...) /* nothing */
tumaku 0:49d5cce77458 31 #endif /* #if NEED_CONSOLE_OUTPUT */
tumaku 0:49d5cce77458 32
tumaku 0:49d5cce77458 33 Serial pcUart(USBTX, USBRX); // tx, rx Still required to read data coming from the PC
tumaku 0:49d5cce77458 34
tumaku 0:49d5cce77458 35 BLEDevice ble; // Create Bluetooth object
tumaku 0:49d5cce77458 36
tumaku 0:49d5cce77458 37 UARTService *uartServicePtr;
tumaku 0:49d5cce77458 38
tumaku 0:49d5cce77458 39
tumaku 0:49d5cce77458 40 #define PS_NOT_INIT 0
tumaku 0:49d5cce77458 41 #define PS_WAITING_FOR_NEXT_COMMAND 1
tumaku 0:49d5cce77458 42 //#define PS_STATE_INIT_AND_REGISTER 2
tumaku 0:49d5cce77458 43 #define PS_CLEAR_3_BLOCKS 3
tumaku 0:49d5cce77458 44 #define PS_STORE_ALL 4
tumaku 0:49d5cce77458 45 #define PS_CLEAR_2_BLOCKS 5
tumaku 0:49d5cce77458 46 #define PS_UPDATE_1_BLCK 6
tumaku 0:49d5cce77458 47
tumaku 0:49d5cce77458 48 uint8_t psState= PS_NOT_INIT;
tumaku 0:49d5cce77458 49 uint8_t psNextState= PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 50
tumaku 0:49d5cce77458 51 pstorage_handle_t handle;
tumaku 0:49d5cce77458 52 pstorage_handle_t block_0_handle;
tumaku 0:49d5cce77458 53 pstorage_handle_t block_1_handle;
tumaku 0:49d5cce77458 54 pstorage_handle_t block_2_handle;
tumaku 0:49d5cce77458 55 pstorage_module_param_t param;
tumaku 0:49d5cce77458 56 uint8_t source_data_0[16] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F};
tumaku 0:49d5cce77458 57 uint8_t source_data_1[16] = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F};
tumaku 0:49d5cce77458 58 uint8_t source_data_2[16] = {0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F};
tumaku 0:49d5cce77458 59 uint8_t source_data_9[16] = {0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34};
tumaku 0:49d5cce77458 60 uint8_t dest_data_0[16];
tumaku 0:49d5cce77458 61 uint8_t dest_data_1[16];
tumaku 0:49d5cce77458 62 uint8_t dest_data_2[16];
tumaku 0:49d5cce77458 63
tumaku 0:49d5cce77458 64
tumaku 0:49d5cce77458 65 static void psLoadAllBlocks(void);
tumaku 0:49d5cce77458 66
tumaku 0:49d5cce77458 67 #define PS_NOT_INIT 0
tumaku 0:49d5cce77458 68 #define PS_WAITING_FOR_NEXT_COMMAND 1
tumaku 0:49d5cce77458 69 //#define PS_STATE_INIT_AND_REGISTER 2
tumaku 0:49d5cce77458 70 #define PS_CLEAR_3_BLOCKS 3
tumaku 0:49d5cce77458 71 #define PS_STORE_ALL 4
tumaku 0:49d5cce77458 72 #define PS_CLEAR_2_BLOCKS 5
tumaku 0:49d5cce77458 73 #define PS_UPDATE_1_BLCK 6
tumaku 0:49d5cce77458 74
tumaku 0:49d5cce77458 75
tumaku 0:49d5cce77458 76
tumaku 0:49d5cce77458 77
tumaku 0:49d5cce77458 78
tumaku 0:49d5cce77458 79 void magnetControl(void)
tumaku 0:49d5cce77458 80 {
tumaku 0:49d5cce77458 81
tumaku 0:49d5cce77458 82 DEBUG("Timer\r\n");
tumaku 0:49d5cce77458 83 DEBUG("Timer State %d \r\n",tickerState);
tumaku 0:49d5cce77458 84
tumaku 0:49d5cce77458 85 if (tickerState==MAGNET_ON) {
tumaku 0:49d5cce77458 86 magnet=1;
tumaku 0:49d5cce77458 87 tickerState=MAGNET_OFF;
tumaku 0:49d5cce77458 88 } else { // (tickerState==MAGNET_OFF)
tumaku 0:49d5cce77458 89 magnet=0;
tumaku 0:49d5cce77458 90 tickerState=MAGNET_OFF;
tumaku 0:49d5cce77458 91 ticker.detach();
tumaku 0:49d5cce77458 92 }
tumaku 0:49d5cce77458 93 }
tumaku 0:49d5cce77458 94
tumaku 0:49d5cce77458 95 /* BLE disconnected callback */
tumaku 0:49d5cce77458 96 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
tumaku 0:49d5cce77458 97 {
tumaku 0:49d5cce77458 98 DEBUG("Disconnected!\n\r");
tumaku 0:49d5cce77458 99 DEBUG("Restarting the advertising process\n\r");
tumaku 0:49d5cce77458 100 ble.startAdvertising();
tumaku 0:49d5cce77458 101 }
tumaku 0:49d5cce77458 102
tumaku 0:49d5cce77458 103 /* BLE UART data received callback */
tumaku 0:49d5cce77458 104 void onDataWritten(const GattCharacteristicWriteCBParams *params)
tumaku 0:49d5cce77458 105 {
tumaku 0:49d5cce77458 106 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { //If characters received over BLE
tumaku 0:49d5cce77458 107 uint16_t bytesRead = params->len;
tumaku 0:49d5cce77458 108 DEBUG("received %u bytes\n\r", bytesRead);
tumaku 0:49d5cce77458 109 DEBUG("Received string: '");
tumaku 0:49d5cce77458 110 //Note the size of data expands to the largest string received. Need to use bytesRead to resize.
tumaku 0:49d5cce77458 111 for (int i=0;i<bytesRead; i++) {
tumaku 0:49d5cce77458 112 DEBUG("%c",params->data[i]);
tumaku 0:49d5cce77458 113 }
tumaku 0:49d5cce77458 114 DEBUG("'\n\r");
tumaku 0:49d5cce77458 115 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data,bytesRead); // Echo received characters back over BLE
tumaku 0:49d5cce77458 116 }
tumaku 0:49d5cce77458 117 ticker.detach();
tumaku 0:49d5cce77458 118 tickerState=MAGNET_ON;
tumaku 0:49d5cce77458 119 ticker.attach(magnetControl,0.7);
tumaku 0:49d5cce77458 120 }
tumaku 0:49d5cce77458 121
tumaku 0:49d5cce77458 122 /* pcUART read callback */
tumaku 0:49d5cce77458 123 void rxInterrupt(void)
tumaku 0:49d5cce77458 124 {
tumaku 0:49d5cce77458 125 while (pcUart.readable()) {
tumaku 0:49d5cce77458 126 if (pcUart.readable()) {
tumaku 0:49d5cce77458 127 char s[2];
tumaku 0:49d5cce77458 128 s[1]=0;
tumaku 0:49d5cce77458 129 s[0]=pcUart.getc();
tumaku 0:49d5cce77458 130 if (s[0]=='0') psLoadAllBlocks();
tumaku 0:49d5cce77458 131 if (psState==PS_WAITING_FOR_NEXT_COMMAND){
tumaku 0:49d5cce77458 132 switch(s[0]) {
tumaku 0:49d5cce77458 133 case '1':
tumaku 0:49d5cce77458 134 psNextState=PS_CLEAR_3_BLOCKS;
tumaku 0:49d5cce77458 135 break;
tumaku 0:49d5cce77458 136 case '2':
tumaku 0:49d5cce77458 137 psNextState=PS_STORE_ALL;
tumaku 0:49d5cce77458 138 break;
tumaku 0:49d5cce77458 139 case '3':
tumaku 0:49d5cce77458 140 psNextState=PS_CLEAR_2_BLOCKS;
tumaku 0:49d5cce77458 141 break;
tumaku 0:49d5cce77458 142 case '4':
tumaku 0:49d5cce77458 143 psNextState=PS_UPDATE_1_BLCK;
tumaku 0:49d5cce77458 144 break;
tumaku 0:49d5cce77458 145 default:
tumaku 0:49d5cce77458 146 break;
tumaku 0:49d5cce77458 147 }
tumaku 0:49d5cce77458 148 }
tumaku 0:49d5cce77458 149 // DEBUG("-%c_",s[0]);
tumaku 0:49d5cce77458 150 uartServicePtr->write(s,1);
tumaku 0:49d5cce77458 151 }
tumaku 0:49d5cce77458 152 }
tumaku 0:49d5cce77458 153 }
tumaku 0:49d5cce77458 154
tumaku 0:49d5cce77458 155 static void checkPsState(pstorage_block_t blockId) {
tumaku 0:49d5cce77458 156 if (psState==PS_CLEAR_3_BLOCKS ) {
tumaku 0:49d5cce77458 157 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 158 return;
tumaku 0:49d5cce77458 159 }
tumaku 0:49d5cce77458 160 if ((psState==PS_STORE_ALL )&&(blockId==block_2_handle.block_id)) {
tumaku 0:49d5cce77458 161 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 162 return;
tumaku 0:49d5cce77458 163 }
tumaku 0:49d5cce77458 164 if (psState==PS_CLEAR_2_BLOCKS ) {
tumaku 0:49d5cce77458 165 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 166 return;
tumaku 0:49d5cce77458 167 }
tumaku 0:49d5cce77458 168 if (psState==PS_UPDATE_1_BLCK ) {
tumaku 0:49d5cce77458 169 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 170 return;
tumaku 0:49d5cce77458 171 }
tumaku 0:49d5cce77458 172 }
tumaku 0:49d5cce77458 173
tumaku 0:49d5cce77458 174 static void example_cb_handler(pstorage_handle_t * handle,
tumaku 0:49d5cce77458 175 uint8_t op_code,
tumaku 0:49d5cce77458 176 uint32_t result,
tumaku 0:49d5cce77458 177 uint8_t * p_data,
tumaku 0:49d5cce77458 178 uint32_t data_len)
tumaku 0:49d5cce77458 179 {
tumaku 0:49d5cce77458 180 checkPsState(handle->block_id);
tumaku 0:49d5cce77458 181 DEBUG("ps handler\r\n");
tumaku 0:49d5cce77458 182 switch(op_code)
tumaku 0:49d5cce77458 183 {
tumaku 0:49d5cce77458 184 case PSTORAGE_LOAD_OP_CODE:
tumaku 0:49d5cce77458 185 if (result == NRF_SUCCESS)
tumaku 0:49d5cce77458 186 {
tumaku 0:49d5cce77458 187 DEBUG("ps LOAD ok %d \r\n",handle->block_id);
tumaku 0:49d5cce77458 188 }
tumaku 0:49d5cce77458 189 else
tumaku 0:49d5cce77458 190 {
tumaku 0:49d5cce77458 191 DEBUG("ps LOAD Failed %d %d \r\n",handle->block_id, result);
tumaku 0:49d5cce77458 192 }
tumaku 0:49d5cce77458 193 break;
tumaku 0:49d5cce77458 194 case PSTORAGE_STORE_OP_CODE:
tumaku 0:49d5cce77458 195 if (result == NRF_SUCCESS)
tumaku 0:49d5cce77458 196 {
tumaku 0:49d5cce77458 197 DEBUG("ps STORE ok %d \r\n",handle->block_id);
tumaku 0:49d5cce77458 198 }
tumaku 0:49d5cce77458 199 else
tumaku 0:49d5cce77458 200 {
tumaku 0:49d5cce77458 201 DEBUG("ps STORE Failed %d %d \r\n",handle->block_id, result);
tumaku 0:49d5cce77458 202 }
tumaku 0:49d5cce77458 203 break;
tumaku 0:49d5cce77458 204 case PSTORAGE_UPDATE_OP_CODE:
tumaku 0:49d5cce77458 205 if (result == NRF_SUCCESS)
tumaku 0:49d5cce77458 206 {
tumaku 0:49d5cce77458 207 DEBUG("ps UPDATE ok %d \r\n",handle->block_id);
tumaku 0:49d5cce77458 208 }
tumaku 0:49d5cce77458 209 else
tumaku 0:49d5cce77458 210 {
tumaku 0:49d5cce77458 211 DEBUG("ps UPDATE Failed %d %d \r\n",handle->block_id, result);
tumaku 0:49d5cce77458 212 }
tumaku 0:49d5cce77458 213 break;
tumaku 0:49d5cce77458 214 case PSTORAGE_CLEAR_OP_CODE:
tumaku 0:49d5cce77458 215 if (result == NRF_SUCCESS)
tumaku 0:49d5cce77458 216 {
tumaku 0:49d5cce77458 217 DEBUG("ps CLEAR ok %d \r\n",handle->block_id);
tumaku 0:49d5cce77458 218 }
tumaku 0:49d5cce77458 219 else
tumaku 0:49d5cce77458 220 {
tumaku 0:49d5cce77458 221 DEBUG("ps CLEAR Failed %d %d \r\n",handle->block_id, result);
tumaku 0:49d5cce77458 222 }
tumaku 0:49d5cce77458 223 break;
tumaku 0:49d5cce77458 224 case PSTORAGE_ERROR_OP_CODE:
tumaku 0:49d5cce77458 225 DEBUG("ps ERROR %d \r\n",handle->block_id);
tumaku 0:49d5cce77458 226 break;
tumaku 0:49d5cce77458 227 }
tumaku 0:49d5cce77458 228 }
tumaku 0:49d5cce77458 229
tumaku 0:49d5cce77458 230
tumaku 0:49d5cce77458 231 static void psInit(void)
tumaku 0:49d5cce77458 232 {
tumaku 0:49d5cce77458 233 uint32_t retval;
tumaku 0:49d5cce77458 234 retval = pstorage_init();
tumaku 0:49d5cce77458 235 if(retval != NRF_SUCCESS)
tumaku 0:49d5cce77458 236 {
tumaku 0:49d5cce77458 237 DEBUG("ps init error\r\n");
tumaku 0:49d5cce77458 238 return;
tumaku 0:49d5cce77458 239 } else {
tumaku 0:49d5cce77458 240 DEBUG("ps init ok\r\n");
tumaku 0:49d5cce77458 241 }
tumaku 0:49d5cce77458 242
tumaku 0:49d5cce77458 243 param.block_size = 16; //Select block size of 16 bytes
tumaku 0:49d5cce77458 244 param.block_count = 10; //Select 10 blocks, total of 160 bytes
tumaku 0:49d5cce77458 245 param.cb = example_cb_handler; //Set the pstorage callback handler
tumaku 0:49d5cce77458 246
tumaku 0:49d5cce77458 247 retval = pstorage_register(&param, &handle);
tumaku 0:49d5cce77458 248 if (retval != NRF_SUCCESS)
tumaku 0:49d5cce77458 249 {
tumaku 0:49d5cce77458 250 DEBUG("ps register error\r\n");
tumaku 0:49d5cce77458 251 return;
tumaku 0:49d5cce77458 252 } else {
tumaku 0:49d5cce77458 253 DEBUG("ps register ok\r\n");
tumaku 0:49d5cce77458 254 }
tumaku 0:49d5cce77458 255 //Get block identifiers
tumaku 0:49d5cce77458 256 pstorage_block_identifier_get(&handle, 0, &block_0_handle);
tumaku 0:49d5cce77458 257 pstorage_block_identifier_get(&handle, 1, &block_1_handle);
tumaku 0:49d5cce77458 258 pstorage_block_identifier_get(&handle, 2, &block_2_handle);
tumaku 0:49d5cce77458 259
tumaku 0:49d5cce77458 260 DEBUG("ps identifier get OK\r\n");
tumaku 0:49d5cce77458 261 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 262 psNextState= PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 263 }
tumaku 0:49d5cce77458 264
tumaku 0:49d5cce77458 265
tumaku 0:49d5cce77458 266 static void psClear3Blocks(void) {
tumaku 0:49d5cce77458 267 uint32_t retval;
tumaku 0:49d5cce77458 268 psState= PS_CLEAR_3_BLOCKS;
tumaku 0:49d5cce77458 269 retval= pstorage_clear(&block_0_handle, 48); //Clear 48 bytes
tumaku 0:49d5cce77458 270 if (retval != NRF_SUCCESS)
tumaku 0:49d5cce77458 271 {
tumaku 0:49d5cce77458 272 DEBUG("ps clear error\r\n");
tumaku 0:49d5cce77458 273 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 274 return;
tumaku 0:49d5cce77458 275 } else {
tumaku 0:49d5cce77458 276 DEBUG("ps clear ok\r\n");
tumaku 0:49d5cce77458 277 }
tumaku 0:49d5cce77458 278 }
tumaku 0:49d5cce77458 279
tumaku 0:49d5cce77458 280 static void psStoreAll(void) {
tumaku 0:49d5cce77458 281 uint32_t retval;
tumaku 0:49d5cce77458 282 psState= PS_STORE_ALL;
tumaku 0:49d5cce77458 283 retval= pstorage_store(&block_0_handle, source_data_0, 16, 0);
tumaku 0:49d5cce77458 284 if (retval != NRF_SUCCESS) {
tumaku 0:49d5cce77458 285 DEBUG("ps store block 0 error\r\n");
tumaku 0:49d5cce77458 286 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 287 return;
tumaku 0:49d5cce77458 288 } else {
tumaku 0:49d5cce77458 289 DEBUG("ps store block 0 ok\r\n");
tumaku 0:49d5cce77458 290 }
tumaku 0:49d5cce77458 291
tumaku 0:49d5cce77458 292 retval= pstorage_store(&block_1_handle, source_data_1, 16, 0);
tumaku 0:49d5cce77458 293 if (retval != NRF_SUCCESS) {
tumaku 0:49d5cce77458 294 DEBUG("ps store block 1 error\r\n");
tumaku 0:49d5cce77458 295 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 296 return;
tumaku 0:49d5cce77458 297 } else {
tumaku 0:49d5cce77458 298 DEBUG("ps store block 1 ok\r\n");
tumaku 0:49d5cce77458 299 }
tumaku 0:49d5cce77458 300
tumaku 0:49d5cce77458 301 retval= pstorage_store(&block_2_handle, source_data_2, 16, 0);
tumaku 0:49d5cce77458 302 if (retval != NRF_SUCCESS) {
tumaku 0:49d5cce77458 303 DEBUG("ps store block 2 error\r\n");
tumaku 0:49d5cce77458 304 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 305 return;
tumaku 0:49d5cce77458 306 } else {
tumaku 0:49d5cce77458 307 DEBUG("ps store block 2 ok\r\n");
tumaku 0:49d5cce77458 308 }
tumaku 0:49d5cce77458 309
tumaku 0:49d5cce77458 310 uint32_t count;
tumaku 0:49d5cce77458 311 retval = pstorage_access_status_get(&count);
tumaku 0:49d5cce77458 312 DEBUG("ps pending %d returncode %d\r\n", count,retval);
tumaku 0:49d5cce77458 313 }
tumaku 0:49d5cce77458 314
tumaku 0:49d5cce77458 315 static void psClear2Blocks(void) {
tumaku 0:49d5cce77458 316 uint32_t retval;
tumaku 0:49d5cce77458 317 psState= PS_CLEAR_2_BLOCKS;
tumaku 0:49d5cce77458 318 retval= pstorage_clear(&block_0_handle, 32); //Clear 32 bytes
tumaku 0:49d5cce77458 319 if (retval != NRF_SUCCESS) {
tumaku 0:49d5cce77458 320 DEBUG("ps clear error\r\n");
tumaku 0:49d5cce77458 321 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 322 return;
tumaku 0:49d5cce77458 323 } else {
tumaku 0:49d5cce77458 324 DEBUG("ps clear ok\r\n");
tumaku 0:49d5cce77458 325 }
tumaku 0:49d5cce77458 326 }
tumaku 0:49d5cce77458 327
tumaku 0:49d5cce77458 328
tumaku 0:49d5cce77458 329 static void psUpdate1Block(void) {
tumaku 0:49d5cce77458 330 uint32_t retval;
tumaku 0:49d5cce77458 331 psState= PS_UPDATE_1_BLCK;
tumaku 0:49d5cce77458 332 retval=pstorage_update(&block_0_handle, source_data_9, 16, 0);
tumaku 0:49d5cce77458 333 if (retval != NRF_SUCCESS) {
tumaku 0:49d5cce77458 334 DEBUG("ps clear error\r\n");
tumaku 0:49d5cce77458 335 psState=PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 336 return;
tumaku 0:49d5cce77458 337 } else {
tumaku 0:49d5cce77458 338 DEBUG("ps clear ok\r\n");
tumaku 0:49d5cce77458 339 }
tumaku 0:49d5cce77458 340 }
tumaku 0:49d5cce77458 341
tumaku 0:49d5cce77458 342 static void psLoadAllBlocks(void) {
tumaku 0:49d5cce77458 343 pstorage_load(dest_data_0, &block_0_handle, 16, 0);
tumaku 0:49d5cce77458 344 pstorage_load(dest_data_1, &block_1_handle, 16, 0);
tumaku 0:49d5cce77458 345 pstorage_load(dest_data_2, &block_2_handle, 16, 0);
tumaku 0:49d5cce77458 346 dest_data_0[15]=0;
tumaku 0:49d5cce77458 347 dest_data_1[15]=0;
tumaku 0:49d5cce77458 348 dest_data_2[15]=0;
tumaku 0:49d5cce77458 349 DEBUG ("Zero: %s\r\n", dest_data_0);
tumaku 0:49d5cce77458 350 DEBUG ("One: %s\r\n", dest_data_1);
tumaku 0:49d5cce77458 351 DEBUG ("Two: %s\r\n", dest_data_2);
tumaku 0:49d5cce77458 352 }
tumaku 0:49d5cce77458 353
tumaku 0:49d5cce77458 354
tumaku 0:49d5cce77458 355 int main(void)
tumaku 0:49d5cce77458 356 {
tumaku 0:49d5cce77458 357
tumaku 0:49d5cce77458 358 magnet=0;
tumaku 0:49d5cce77458 359 tickerState=MAGNET_OFF;
tumaku 0:49d5cce77458 360
tumaku 0:49d5cce77458 361 // Not used so not enabled
tumaku 0:49d5cce77458 362 pcUart.attach(&rxInterrupt,Serial::RxIrq); //interrupt for incoming data available from PC connection
tumaku 0:49d5cce77458 363
tumaku 0:49d5cce77458 364 //ticker.attach(periodicCallback, 0.1);
tumaku 0:49d5cce77458 365
tumaku 0:49d5cce77458 366 DEBUG("Initialising the nRF51822\n\r");
tumaku 0:49d5cce77458 367 ble.init();
tumaku 0:49d5cce77458 368 psInit();
tumaku 0:49d5cce77458 369 ble.onDisconnection(disconnectionCallback); // Define callback function for BLE disconnection event
tumaku 0:49d5cce77458 370 ble.onDataWritten(onDataWritten); // Define callback function for BLE Data received event
tumaku 0:49d5cce77458 371
tumaku 0:49d5cce77458 372 /* setup advertising */
tumaku 0:49d5cce77458 373 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); // Indicate that Legacy Bluetooth in not supported
tumaku 0:49d5cce77458 374 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
tumaku 0:49d5cce77458 375 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
tumaku 0:49d5cce77458 376 (const uint8_t *)"B_MAGNET", sizeof("B_MAGNET") - 1);
tumaku 0:49d5cce77458 377 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
tumaku 0:49d5cce77458 378 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
tumaku 0:49d5cce77458 379
tumaku 0:49d5cce77458 380 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500)); // Set advertising interval to 1 second
tumaku 0:49d5cce77458 381 ble.startAdvertising(); // Start advertising
tumaku 0:49d5cce77458 382
tumaku 0:49d5cce77458 383 UARTService uartService(ble); // Create BLE UART service
tumaku 0:49d5cce77458 384 uartServicePtr = &uartService; // Initalise pointer to point to UART Service
tumaku 0:49d5cce77458 385
tumaku 0:49d5cce77458 386 while (true) {
tumaku 0:49d5cce77458 387 if ((psState==PS_WAITING_FOR_NEXT_COMMAND)&&(psNextState!=PS_WAITING_FOR_NEXT_COMMAND)){
tumaku 0:49d5cce77458 388 switch(psNextState) {
tumaku 0:49d5cce77458 389 case PS_CLEAR_3_BLOCKS:
tumaku 0:49d5cce77458 390 psClear3Blocks();
tumaku 0:49d5cce77458 391 break;
tumaku 0:49d5cce77458 392 case PS_STORE_ALL:
tumaku 0:49d5cce77458 393 psStoreAll();
tumaku 0:49d5cce77458 394 break;
tumaku 0:49d5cce77458 395 case PS_CLEAR_2_BLOCKS:
tumaku 0:49d5cce77458 396 psClear2Blocks();
tumaku 0:49d5cce77458 397 break;
tumaku 0:49d5cce77458 398 case PS_UPDATE_1_BLCK:
tumaku 0:49d5cce77458 399 psUpdate1Block();
tumaku 0:49d5cce77458 400 break;
tumaku 0:49d5cce77458 401 default:
tumaku 0:49d5cce77458 402 DEBUG ("Unexpected next pstorage state");
tumaku 0:49d5cce77458 403 }
tumaku 0:49d5cce77458 404 psNextState= PS_WAITING_FOR_NEXT_COMMAND;
tumaku 0:49d5cce77458 405 }
tumaku 0:49d5cce77458 406 ble.waitForEvent(); // Wait for BLE events
tumaku 0:49d5cce77458 407 }
tumaku 0:49d5cce77458 408 }