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