football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
elmbed
Date:
Sun Nov 29 13:52:53 2015 +0000
Revision:
18:affef3a7db2a
Parent:
17:d8b901d791fd
Child:
19:afcbb425b3cf
Device to phone comms working

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