football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sun Jun 21 08:44:24 2015 +0000
Revision:
13:28332f65d14b
Parent:
12:6d313d575f84
Child:
14:76fb883a3cb8
Flash-write pstorage module test feature added.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AntonLS 0:28ca4562fe1a 1 /*
AntonLS 0:28ca4562fe1a 2 * TA test
AntonLS 0:28ca4562fe1a 3 *
AntonLS 11:d3aa5fca2330 4 * TODO maybe have a mode where the serial port I/O can be swapped,
AntonLS 11:d3aa5fca2330 5 * such that what the nRF generates is sent out the serial port,
AntonLS 11:d3aa5fca2330 6 * and what comes in the serial port goes into the nRF.
AntonLS 11:d3aa5fca2330 7 * Maybe could use the now-unused CTS pin for that.
AntonLS 13:28332f65d14b 8 *
AntonLS 13:28332f65d14b 9 * Using
AntonLS 13:28332f65d14b 10 * rev 327 of BLE_API
AntonLS 13:28332f65d14b 11 * rev 102 of nRF51822 w/ modification--See below
AntonLS 13:28332f65d14b 12 * rev 493 of mbed-src w/ modification to targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c
AntonLS 13:28332f65d14b 13 * so things compile properly.
AntonLS 13:28332f65d14b 14 *
AntonLS 13:28332f65d14b 15 * Changed
AntonLS 13:28332f65d14b 16 * nRF51822/nordic/pstorage_platform.h PSTORAGE_MIN_BLOCK_SIZE changed from 0x010 to 4.
AntonLS 0:28ca4562fe1a 17 */
AntonLS 0:28ca4562fe1a 18
AntonLS 0:28ca4562fe1a 19 /* mbed Microcontroller Library
AntonLS 0:28ca4562fe1a 20 * Copyright (c) 2006-2013 ARM Limited
AntonLS 0:28ca4562fe1a 21 *
AntonLS 0:28ca4562fe1a 22 * Licensed under the Apache License, Version 2.0 (the "License");
AntonLS 0:28ca4562fe1a 23 * you may not use this file except in compliance with the License.
AntonLS 0:28ca4562fe1a 24 * You may obtain a copy of the License at
AntonLS 0:28ca4562fe1a 25 *
AntonLS 0:28ca4562fe1a 26 * http://www.apache.org/licenses/LICENSE-2.0
AntonLS 0:28ca4562fe1a 27 *
AntonLS 0:28ca4562fe1a 28 * Unless required by applicable law or agreed to in writing, software
AntonLS 0:28ca4562fe1a 29 * distributed under the License is distributed on an "AS IS" BASIS,
AntonLS 0:28ca4562fe1a 30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AntonLS 0:28ca4562fe1a 31 * See the License for the specific language governing permissions and
AntonLS 0:28ca4562fe1a 32 * limitations under the License.
AntonLS 0:28ca4562fe1a 33 */
AntonLS 0:28ca4562fe1a 34
AntonLS 0:28ca4562fe1a 35 #include "mbed.h"
AntonLS 12:6d313d575f84 36 // #include "rtos.h"
AntonLS 0:28ca4562fe1a 37 #include "BLEDevice.h"
AntonLS 0:28ca4562fe1a 38
AntonLS 0:28ca4562fe1a 39 #include "DFUService.h"
AntonLS 0:28ca4562fe1a 40 #include "UARTService.h"
AntonLS 0:28ca4562fe1a 41 #include "DeviceInformationService.h"
AntonLS 0:28ca4562fe1a 42
AntonLS 0:28ca4562fe1a 43 #include "MTSSerialFlowControl.h"
AntonLS 5:1b9734e68327 44 #include "PhoneAppIO.h"
AntonLS 0:28ca4562fe1a 45
AntonLS 0:28ca4562fe1a 46 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
AntonLS 0:28ca4562fe1a 47 * it will have an impact on code-size and power consumption. */
AntonLS 0:28ca4562fe1a 48
AntonLS 11:d3aa5fca2330 49 #define LOOPBACK_MODE 0 // Loopback mode
AntonLS 4:17b8edf264c3 50
AntonLS 0:28ca4562fe1a 51 #if NEED_CONSOLE_OUTPUT
AntonLS 0:28ca4562fe1a 52 #define DEBUG(...) { printf(__VA_ARGS__); }
AntonLS 0:28ca4562fe1a 53 #else
AntonLS 0:28ca4562fe1a 54 #define DEBUG(...) /* nothing */
AntonLS 0:28ca4562fe1a 55 #endif /* #if NEED_CONSOLE_OUTPUT */
AntonLS 0:28ca4562fe1a 56
AntonLS 0:28ca4562fe1a 57 extern "C"
AntonLS 0:28ca4562fe1a 58 {
AntonLS 13:28332f65d14b 59 #include "softdevice_handler.h" // Attempt to get pstorage enqueued cmd callbacks.
AntonLS 13:28332f65d14b 60 #include "app_timer.h"
AntonLS 13:28332f65d14b 61 #include "pstorage.h"
AntonLS 13:28332f65d14b 62
AntonLS 12:6d313d575f84 63 void My_UART0_IRQHandler();
AntonLS 12:6d313d575f84 64
AntonLS 0:28ca4562fe1a 65 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 66 }
AntonLS 0:28ca4562fe1a 67
AntonLS 12:6d313d575f84 68 // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins.
AntonLS 12:6d313d575f84 69 DigitalOut led0( P0_19, 1 ); // BLE Nano Low =On
AntonLS 12:6d313d575f84 70 DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On
AntonLS 12:6d313d575f84 71 // DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 12:6d313d575f84 72 DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO
AntonLS 12:6d313d575f84 73
AntonLS 12:6d313d575f84 74 // Check if we should swap serial Rx/Tx for early rev of "Little Brain"
AntonLS 12:6d313d575f84 75 DigitalIn trSwp( P0_30, PullUp );
AntonLS 12:6d313d575f84 76
AntonLS 12:6d313d575f84 77 // Wait to settle.
AntonLS 12:6d313d575f84 78 int foo = (wait( 0.1 ), 0);
AntonLS 12:6d313d575f84 79
AntonLS 13:28332f65d14b 80 // App timer, app scheduler, and pstorage are already setup by bootloader.
AntonLS 13:28332f65d14b 81
AntonLS 13:28332f65d14b 82 BLEDevice ble;
AntonLS 13:28332f65d14b 83
AntonLS 12:6d313d575f84 84
AntonLS 1:0ba687d4196f 85 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 86 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 11:d3aa5fca2330 87 // But a version of serial_init() erroneously enabled the uart before the setting of those,
AntonLS 11:d3aa5fca2330 88 // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 11:d3aa5fca2330 89 // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase)
AntonLS 7:205ef63d311a 90 // to not use serial_init() in serial_api.c, so flow control is setup correctly *
AntonLS 7:205ef63d311a 91 // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default *
AntonLS 11:d3aa5fca2330 92 // * We can't change the uart interrupt vector, so we comment-out the handler in
AntonLS 11:d3aa5fca2330 93 // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work.
AntonLS 12:6d313d575f84 94 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might work. TODO
AntonLS 6:ef758ac3c928 95 // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its
AntonLS 11:d3aa5fca2330 96 // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay.
AntonLS 1:0ba687d4196f 97 //
AntonLS 12:6d313d575f84 98 // mts::MTSSerialFlowControl pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
AntonLS 12:6d313d575f84 99 mts::MTSSerial pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), 256, 1280, RTS_PIN_NUMBER, NC ); // 256, 2560
AntonLS 0:28ca4562fe1a 100
AntonLS 0:28ca4562fe1a 101 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 102
AntonLS 0:28ca4562fe1a 103
AntonLS 5:1b9734e68327 104 char deviceName[6]; // "TAF00";
AntonLS 5:1b9734e68327 105 Gap::address_t macAddr;
AntonLS 5:1b9734e68327 106 Gap::addr_type_t *pAdType;
AntonLS 5:1b9734e68327 107
AntonLS 0:28ca4562fe1a 108
AntonLS 12:6d313d575f84 109 // const uint8_t DevInfoServiceUUID_rev[] =
AntonLS 12:6d313d575f84 110 // {
AntonLS 12:6d313d575f84 111 // (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
AntonLS 12:6d313d575f84 112 // };
AntonLS 0:28ca4562fe1a 113
AntonLS 0:28ca4562fe1a 114 UARTService *uartServicePtr;
AntonLS 11:d3aa5fca2330 115 PhoneAppIO *phoneP;
AntonLS 0:28ca4562fe1a 116
AntonLS 11:d3aa5fca2330 117 bool connected = false;
AntonLS 5:1b9734e68327 118
AntonLS 11:d3aa5fca2330 119 void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType,
AntonLS 11:d3aa5fca2330 120 const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams )
AntonLS 11:d3aa5fca2330 121 {
AntonLS 11:d3aa5fca2330 122 connected = true;
AntonLS 11:d3aa5fca2330 123
AntonLS 11:d3aa5fca2330 124 DEBUG( "Connected!\n\r" );
AntonLS 11:d3aa5fca2330 125 }
AntonLS 5:1b9734e68327 126
AntonLS 0:28ca4562fe1a 127 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 128 {
AntonLS 11:d3aa5fca2330 129 connected = false;
AntonLS 11:d3aa5fca2330 130
AntonLS 0:28ca4562fe1a 131 DEBUG( "Disconnected!\n\r" );
AntonLS 0:28ca4562fe1a 132 DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 133 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 134 }
AntonLS 0:28ca4562fe1a 135
AntonLS 5:1b9734e68327 136 bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
AntonLS 5:1b9734e68327 137 {
AntonLS 11:d3aa5fca2330 138 ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead );
AntonLS 5:1b9734e68327 139
AntonLS 5:1b9734e68327 140 if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
AntonLS 5:1b9734e68327 141 (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
AntonLS 7:205ef63d311a 142 {
AntonLS 10:72ceef287b0f 143 pcfc.printf( "\r\nBLE %d! ", err );
AntonLS 7:205ef63d311a 144
AntonLS 7:205ef63d311a 145 } else if ( err == BLE_STACK_BUSY )
AntonLS 7:205ef63d311a 146 {
AntonLS 11:d3aa5fca2330 147 // Common error when pumping data.
AntonLS 7:205ef63d311a 148 }
AntonLS 5:1b9734e68327 149
AntonLS 5:1b9734e68327 150 return (err != BLE_ERROR_NONE);
AntonLS 5:1b9734e68327 151 }
AntonLS 5:1b9734e68327 152
AntonLS 0:28ca4562fe1a 153 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 154 {
AntonLS 11:d3aa5fca2330 155 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 156 {
AntonLS 11:d3aa5fca2330 157 uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload
AntonLS 11:d3aa5fca2330 158 if( 0 != bytesRead )
AntonLS 11:d3aa5fca2330 159 {
AntonLS 11:d3aa5fca2330 160 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 0:28ca4562fe1a 161
AntonLS 11:d3aa5fca2330 162 // Also write to serial port...
AntonLS 11:d3aa5fca2330 163 // pcfc.printf( "From app: " );
AntonLS 11:d3aa5fca2330 164 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 11:d3aa5fca2330 165 // pcfc.printf( "\r\n" );
AntonLS 5:1b9734e68327 166
AntonLS 11:d3aa5fca2330 167 return;
AntonLS 11:d3aa5fca2330 168 }
AntonLS 0:28ca4562fe1a 169 }
AntonLS 11:d3aa5fca2330 170
AntonLS 11:d3aa5fca2330 171 // Other Characteristics here.
AntonLS 0:28ca4562fe1a 172 }
AntonLS 0:28ca4562fe1a 173
AntonLS 0:28ca4562fe1a 174 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 175 {
AntonLS 0:28ca4562fe1a 176 }
AntonLS 0:28ca4562fe1a 177
AntonLS 11:d3aa5fca2330 178 void toPhoneChk( void )
AntonLS 0:28ca4562fe1a 179 {
AntonLS 9:95dc84e9fb7f 180 // if( 0 != rts.read() ) pcfc.puts( "\r\n!RTS disengaged.\r\n" ); // When not using HWFC.
AntonLS 3:388e441be8df 181
AntonLS 11:d3aa5fca2330 182 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 183 {
AntonLS 11:d3aa5fca2330 184 char ch;
AntonLS 11:d3aa5fca2330 185 // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars.
AntonLS 11:d3aa5fca2330 186 for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ )
AntonLS 0:28ca4562fe1a 187 {
AntonLS 11:d3aa5fca2330 188 if( 0 > phoneP->putchar( ch ) )
AntonLS 11:d3aa5fca2330 189 {
AntonLS 11:d3aa5fca2330 190 pcfc.printf( " * " );
AntonLS 11:d3aa5fca2330 191 break;
AntonLS 11:d3aa5fca2330 192 }
AntonLS 11:d3aa5fca2330 193 if( (cnt >= 20) && ('\n' == ch) ) break;
AntonLS 5:1b9734e68327 194 }
AntonLS 11:d3aa5fca2330 195 // Write to outgoing characteristic if anything is pending.
AntonLS 11:d3aa5fca2330 196 if( 0 != phoneP->maybeHandleWrite() )
AntonLS 5:1b9734e68327 197 {
AntonLS 11:d3aa5fca2330 198 // pcfc.printf( "ToPhoneHandler \r\n" );
AntonLS 0:28ca4562fe1a 199 }
AntonLS 0:28ca4562fe1a 200 }
AntonLS 0:28ca4562fe1a 201 }
AntonLS 0:28ca4562fe1a 202
AntonLS 13:28332f65d14b 203
AntonLS 13:28332f65d14b 204 static uint32_t boot_cnt_data = 0;
AntonLS 13:28332f65d14b 205 static uint16_t data_block = 9*20 +75; // Last block -- Use for app-start count.
AntonLS 13:28332f65d14b 206 static uint16_t data_size = 4;
AntonLS 13:28332f65d14b 207 static uint16_t data_offset = 0; // 12 for 16-byte blocks.
AntonLS 13:28332f65d14b 208 static bool data_loaded = false;
AntonLS 13:28332f65d14b 209 static bool data_cleared = false;
AntonLS 13:28332f65d14b 210 static bool data_stored = false;
AntonLS 13:28332f65d14b 211 static pstorage_handle_t pstorage_id;
AntonLS 13:28332f65d14b 212 static uint32_t pstorage_read_test()
AntonLS 13:28332f65d14b 213 {
AntonLS 13:28332f65d14b 214 uint32_t err_code;
AntonLS 13:28332f65d14b 215
AntonLS 13:28332f65d14b 216 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 217
AntonLS 13:28332f65d14b 218 do
AntonLS 13:28332f65d14b 219 {
AntonLS 13:28332f65d14b 220 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 221 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 222
AntonLS 13:28332f65d14b 223 err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset );
AntonLS 13:28332f65d14b 224 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 225
AntonLS 13:28332f65d14b 226 } while( 0 );
AntonLS 13:28332f65d14b 227
AntonLS 13:28332f65d14b 228 return err_code;
AntonLS 13:28332f65d14b 229 }
AntonLS 13:28332f65d14b 230 static uint32_t pstorage_clear_test()
AntonLS 13:28332f65d14b 231 {
AntonLS 13:28332f65d14b 232 uint32_t err_code;
AntonLS 13:28332f65d14b 233
AntonLS 13:28332f65d14b 234 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 235
AntonLS 13:28332f65d14b 236 do
AntonLS 13:28332f65d14b 237 {
AntonLS 13:28332f65d14b 238 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 239 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 240
AntonLS 13:28332f65d14b 241 err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE );
AntonLS 13:28332f65d14b 242 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 243
AntonLS 13:28332f65d14b 244 } while( 0 );
AntonLS 13:28332f65d14b 245
AntonLS 13:28332f65d14b 246 return err_code;
AntonLS 13:28332f65d14b 247 }
AntonLS 13:28332f65d14b 248 static uint32_t pstorage_write_test()
AntonLS 13:28332f65d14b 249 {
AntonLS 13:28332f65d14b 250 uint32_t err_code;
AntonLS 13:28332f65d14b 251
AntonLS 13:28332f65d14b 252 pstorage_handle_t p_block_id;
AntonLS 13:28332f65d14b 253
AntonLS 13:28332f65d14b 254 do
AntonLS 13:28332f65d14b 255 {
AntonLS 13:28332f65d14b 256 err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id );
AntonLS 13:28332f65d14b 257 if( NRF_SUCCESS != err_code ) break;
AntonLS 13:28332f65d14b 258
AntonLS 13:28332f65d14b 259 err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset );
AntonLS 13:28332f65d14b 260
AntonLS 13:28332f65d14b 261 } while( 0 );
AntonLS 13:28332f65d14b 262
AntonLS 13:28332f65d14b 263 return err_code;
AntonLS 13:28332f65d14b 264 }
AntonLS 13:28332f65d14b 265 static void pstorage_cb_handler( pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len )
AntonLS 13:28332f65d14b 266 {
AntonLS 13:28332f65d14b 267 switch( op_code )
AntonLS 13:28332f65d14b 268 {
AntonLS 13:28332f65d14b 269 case PSTORAGE_LOAD_OP_CODE:
AntonLS 13:28332f65d14b 270 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 271 {
AntonLS 13:28332f65d14b 272 // Load operation successful.
AntonLS 13:28332f65d14b 273 data_loaded = true; // Flag to signal load is done.
AntonLS 13:28332f65d14b 274
AntonLS 13:28332f65d14b 275 } else
AntonLS 13:28332f65d14b 276 {
AntonLS 13:28332f65d14b 277 // Load operation failed.
AntonLS 13:28332f65d14b 278 pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 279 }
AntonLS 13:28332f65d14b 280 break;
AntonLS 13:28332f65d14b 281 case PSTORAGE_UPDATE_OP_CODE:
AntonLS 13:28332f65d14b 282 case PSTORAGE_STORE_OP_CODE:
AntonLS 13:28332f65d14b 283 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 284 {
AntonLS 13:28332f65d14b 285 // Store operation successful.
AntonLS 13:28332f65d14b 286 data_stored = true; // Flag to signal store is done.
AntonLS 13:28332f65d14b 287
AntonLS 13:28332f65d14b 288 } else
AntonLS 13:28332f65d14b 289 {
AntonLS 13:28332f65d14b 290 // Store operation failed.
AntonLS 13:28332f65d14b 291 pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 292 }
AntonLS 13:28332f65d14b 293 // Source memory can now be reused or freed.
AntonLS 13:28332f65d14b 294 break;
AntonLS 13:28332f65d14b 295 case PSTORAGE_CLEAR_OP_CODE:
AntonLS 13:28332f65d14b 296 if( NRF_SUCCESS == result )
AntonLS 13:28332f65d14b 297 {
AntonLS 13:28332f65d14b 298 // Clear operation successful.
AntonLS 13:28332f65d14b 299 data_cleared = true; // Flag to store to the same data area.
AntonLS 13:28332f65d14b 300
AntonLS 13:28332f65d14b 301 } else
AntonLS 13:28332f65d14b 302 {
AntonLS 13:28332f65d14b 303 // Clear operation failed.
AntonLS 13:28332f65d14b 304 pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result );
AntonLS 13:28332f65d14b 305 }
AntonLS 13:28332f65d14b 306 break;
AntonLS 13:28332f65d14b 307 default:
AntonLS 13:28332f65d14b 308 pcfc.printf( "\r\nWarn: pstorage unknown op: %x error: %x\r\n", op_code, result );
AntonLS 13:28332f65d14b 309 }
AntonLS 13:28332f65d14b 310 }
AntonLS 13:28332f65d14b 311 static uint32_t pstorage_setup()
AntonLS 13:28332f65d14b 312 {
AntonLS 13:28332f65d14b 313 pstorage_module_param_t pstorage_param;
AntonLS 13:28332f65d14b 314
AntonLS 13:28332f65d14b 315 // Setup pstorage with 9*20 +76 blocks of 4 bytes each. (or 9*20/4 + 19 for 16 byte blocks)
AntonLS 13:28332f65d14b 316 pstorage_param.block_size = 4; // Recommended to be >= 4 bytes.
AntonLS 13:28332f65d14b 317 pstorage_param.block_count = 9*20 +76; // 9 Sequences x 20 Stations + 76 blocks to fill out to 1k.
AntonLS 13:28332f65d14b 318 pstorage_param.cb = pstorage_cb_handler;
AntonLS 13:28332f65d14b 319
AntonLS 13:28332f65d14b 320 return pstorage_register( &pstorage_param, &pstorage_id );
AntonLS 13:28332f65d14b 321 }
AntonLS 13:28332f65d14b 322
AntonLS 13:28332f65d14b 323 void periodicCallback( void )
AntonLS 13:28332f65d14b 324 {
AntonLS 13:28332f65d14b 325 led0 = !led0;
AntonLS 13:28332f65d14b 326 led1 = !led1;
AntonLS 13:28332f65d14b 327 // rts = !rts;
AntonLS 13:28332f65d14b 328 }
AntonLS 13:28332f65d14b 329
AntonLS 12:6d313d575f84 330 /*
AntonLS 12:6d313d575f84 331 void led_thread( void const *args )
AntonLS 12:6d313d575f84 332 {
AntonLS 12:6d313d575f84 333 while( true )
AntonLS 12:6d313d575f84 334 {
AntonLS 12:6d313d575f84 335 led0 = !led0;
AntonLS 12:6d313d575f84 336 led1 = !led1;
AntonLS 12:6d313d575f84 337 Thread::wait( 1000 );
AntonLS 12:6d313d575f84 338 }
AntonLS 12:6d313d575f84 339 }
AntonLS 12:6d313d575f84 340 */
AntonLS 12:6d313d575f84 341
AntonLS 13:28332f65d14b 342
AntonLS 0:28ca4562fe1a 343 int main( void )
AntonLS 0:28ca4562fe1a 344 {
AntonLS 12:6d313d575f84 345 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // TODO maybe try before instantiating pcfc.
AntonLS 12:6d313d575f84 346
AntonLS 0:28ca4562fe1a 347 Ticker ticker;
AntonLS 0:28ca4562fe1a 348 ticker.attach( periodicCallback, 1 );
AntonLS 12:6d313d575f84 349 // Thread thread( led_thread );
AntonLS 0:28ca4562fe1a 350
AntonLS 0:28ca4562fe1a 351 pcfc.baud( 57600 );
AntonLS 0:28ca4562fe1a 352
AntonLS 0:28ca4562fe1a 353 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 1:0ba687d4196f 354
AntonLS 2:fe1566cdb6e7 355
AntonLS 0:28ca4562fe1a 356 ble.init();
AntonLS 11:d3aa5fca2330 357 ble.onConnection( connectionCallback );
AntonLS 0:28ca4562fe1a 358 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 359 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 360 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 361
AntonLS 0:28ca4562fe1a 362 /* setup advertising */
AntonLS 12:6d313d575f84 363 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED |
AntonLS 12:6d313d575f84 364 GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
AntonLS 0:28ca4562fe1a 365 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 5:1b9734e68327 366
AntonLS 13:28332f65d14b 367
AntonLS 5:1b9734e68327 368 // Get MAC addr so we can create a device name using it.
AntonLS 5:1b9734e68327 369 ble.getAddress( pAdType, macAddr );
AntonLS 5:1b9734e68327 370 sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
AntonLS 5:1b9734e68327 371
AntonLS 5:1b9734e68327 372 pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName );
AntonLS 11:d3aa5fca2330 373 #if LOOPBACK_MODE
AntonLS 5:1b9734e68327 374 pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
AntonLS 5:1b9734e68327 375 #endif
AntonLS 5:1b9734e68327 376
AntonLS 13:28332f65d14b 377 uint32_t p_count;
AntonLS 13:28332f65d14b 378 uint32_t pstorageErr = pstorage_init(); // This needs to be called, even though apparently called in bootloader--Check other stuff.
AntonLS 13:28332f65d14b 379 pstorage_access_status_get( &p_count );
AntonLS 13:28332f65d14b 380 // pcfc.printf( "\r\nInitial pstorage count: %d\r\n", p_count );
AntonLS 13:28332f65d14b 381
AntonLS 13:28332f65d14b 382 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage init error: %x\r\n", pstorageErr );
AntonLS 13:28332f65d14b 383 else
AntonLS 13:28332f65d14b 384 {
AntonLS 13:28332f65d14b 385 pstorageErr = pstorage_setup();
AntonLS 13:28332f65d14b 386 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage setup error: %x\r\n", pstorageErr );
AntonLS 13:28332f65d14b 387 else
AntonLS 13:28332f65d14b 388 {
AntonLS 13:28332f65d14b 389 pstorageErr = pstorage_read_test();
AntonLS 13:28332f65d14b 390 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage read attempt error: %x\r\n", pstorageErr );
AntonLS 13:28332f65d14b 391 else
AntonLS 13:28332f65d14b 392 {
AntonLS 13:28332f65d14b 393 // In this test setup, we use the load callback to signal start to clear,
AntonLS 13:28332f65d14b 394 // then we use the clear callback to signal start to write.
AntonLS 13:28332f65d14b 395 }
AntonLS 13:28332f65d14b 396 }
AntonLS 13:28332f65d14b 397 }
AntonLS 13:28332f65d14b 398
AntonLS 0:28ca4562fe1a 399 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 0:28ca4562fe1a 400 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 0:28ca4562fe1a 401 ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 402 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 0:28ca4562fe1a 403
AntonLS 12:6d313d575f84 404 // Necessary?
AntonLS 12:6d313d575f84 405 // ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 12:6d313d575f84 406 // (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );
AntonLS 0:28ca4562fe1a 407
AntonLS 11:d3aa5fca2330 408 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 200 ) );
AntonLS 0:28ca4562fe1a 409 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 410
AntonLS 0:28ca4562fe1a 411 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 0:28ca4562fe1a 412
AntonLS 0:28ca4562fe1a 413 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
AntonLS 0:28ca4562fe1a 414 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 415 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 416 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 417
AntonLS 0:28ca4562fe1a 418 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 419 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 420
AntonLS 11:d3aa5fca2330 421 PhoneAppIO phone( ble, uartService.getRXCharacteristicHandle(),
AntonLS 11:d3aa5fca2330 422 uartService.getTXCharacteristicHandle() );
AntonLS 11:d3aa5fca2330 423 phone.loopbackMode = LOOPBACK_MODE;
AntonLS 11:d3aa5fca2330 424 phoneP = ☎
AntonLS 11:d3aa5fca2330 425
AntonLS 11:d3aa5fca2330 426 Timer tmr;
AntonLS 11:d3aa5fca2330 427 tmr.start();
AntonLS 11:d3aa5fca2330 428
AntonLS 11:d3aa5fca2330 429 // Main Loop
AntonLS 11:d3aa5fca2330 430 for( uint32_t loop=1; ;loop++ )
AntonLS 0:28ca4562fe1a 431 {
AntonLS 0:28ca4562fe1a 432 ble.waitForEvent();
AntonLS 0:28ca4562fe1a 433
AntonLS 11:d3aa5fca2330 434 toPhoneChk(); // Write any pending data to phone.
AntonLS 11:d3aa5fca2330 435
AntonLS 11:d3aa5fca2330 436 while( 0 <= phone.getchar() ); // Eat input.
AntonLS 11:d3aa5fca2330 437
AntonLS 11:d3aa5fca2330 438 // if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() );
AntonLS 13:28332f65d14b 439
AntonLS 13:28332f65d14b 440 app_sched_execute(); // Attempt to get pstorage enqueued cmd callbacks.
AntonLS 13:28332f65d14b 441
AntonLS 13:28332f65d14b 442 // Update persistent storage if ready...
AntonLS 13:28332f65d14b 443 if( data_loaded )
AntonLS 13:28332f65d14b 444 {
AntonLS 13:28332f65d14b 445 data_loaded = false;
AntonLS 13:28332f65d14b 446
AntonLS 13:28332f65d14b 447 boot_cnt_data++;
AntonLS 13:28332f65d14b 448 pcfc.printf( "\r\nApp boot #%d\r\n", boot_cnt_data );
AntonLS 13:28332f65d14b 449
AntonLS 13:28332f65d14b 450 pstorageErr = pstorage_clear_test();
AntonLS 13:28332f65d14b 451 // pcfc.printf( "pstorage clear command sent.\r\n" );
AntonLS 13:28332f65d14b 452 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage clear attempt error: %x\r\n", pstorageErr );
AntonLS 13:28332f65d14b 453
AntonLS 13:28332f65d14b 454 } else if( data_cleared )
AntonLS 13:28332f65d14b 455 {
AntonLS 13:28332f65d14b 456 data_cleared = false;
AntonLS 13:28332f65d14b 457
AntonLS 13:28332f65d14b 458 // pcfc.printf( "\r\npstorage clear operation successful.\r\n" );
AntonLS 13:28332f65d14b 459
AntonLS 13:28332f65d14b 460 pstorageErr = pstorage_write_test();
AntonLS 13:28332f65d14b 461 // pcfc.printf( "pstorage write command sent.\r\n" );
AntonLS 13:28332f65d14b 462 if( NRF_SUCCESS != pstorageErr ) pcfc.printf( "\r\nWarn: pstorage write attempt error: %x\r\n", pstorageErr );
AntonLS 13:28332f65d14b 463
AntonLS 13:28332f65d14b 464 } else if( data_stored )
AntonLS 13:28332f65d14b 465 {
AntonLS 13:28332f65d14b 466 data_stored = false;
AntonLS 13:28332f65d14b 467
AntonLS 13:28332f65d14b 468 // pcfc.printf( "\r\npstorage store operation successful.\r\n" );
AntonLS 13:28332f65d14b 469 }
AntonLS 13:28332f65d14b 470
AntonLS 0:28ca4562fe1a 471 }
AntonLS 0:28ca4562fe1a 472 }
AntonLS 0:28ca4562fe1a 473
AntonLS 0:28ca4562fe1a 474 /* EOF */