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