football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Sat Jul 11 01:57:40 2015 +0000
Revision:
16:3c873f2c8a27
Parent:
15:b86c4b798aa1
Child:
17:d8b901d791fd
Converted BLE_API lib to folder so the version stays frozen.

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