football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Tue Jun 16 18:16:49 2015 +0000
Revision:
12:6d313d575f84
Parent:
11:d3aa5fca2330
Child:
13:28332f65d14b
LED, serial IO changes for new hardware, etc.

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 0:28ca4562fe1a 8 */
AntonLS 0:28ca4562fe1a 9
AntonLS 0:28ca4562fe1a 10 /* mbed Microcontroller Library
AntonLS 0:28ca4562fe1a 11 * Copyright (c) 2006-2013 ARM Limited
AntonLS 0:28ca4562fe1a 12 *
AntonLS 0:28ca4562fe1a 13 * Licensed under the Apache License, Version 2.0 (the "License");
AntonLS 0:28ca4562fe1a 14 * you may not use this file except in compliance with the License.
AntonLS 0:28ca4562fe1a 15 * You may obtain a copy of the License at
AntonLS 0:28ca4562fe1a 16 *
AntonLS 0:28ca4562fe1a 17 * http://www.apache.org/licenses/LICENSE-2.0
AntonLS 0:28ca4562fe1a 18 *
AntonLS 0:28ca4562fe1a 19 * Unless required by applicable law or agreed to in writing, software
AntonLS 0:28ca4562fe1a 20 * distributed under the License is distributed on an "AS IS" BASIS,
AntonLS 0:28ca4562fe1a 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AntonLS 0:28ca4562fe1a 22 * See the License for the specific language governing permissions and
AntonLS 0:28ca4562fe1a 23 * limitations under the License.
AntonLS 0:28ca4562fe1a 24 */
AntonLS 0:28ca4562fe1a 25
AntonLS 0:28ca4562fe1a 26 #include "mbed.h"
AntonLS 12:6d313d575f84 27 // #include "rtos.h"
AntonLS 0:28ca4562fe1a 28 #include "BLEDevice.h"
AntonLS 0:28ca4562fe1a 29
AntonLS 0:28ca4562fe1a 30 #include "DFUService.h"
AntonLS 0:28ca4562fe1a 31 #include "UARTService.h"
AntonLS 0:28ca4562fe1a 32 #include "DeviceInformationService.h"
AntonLS 0:28ca4562fe1a 33
AntonLS 0:28ca4562fe1a 34 #include "MTSSerialFlowControl.h"
AntonLS 5:1b9734e68327 35 #include "PhoneAppIO.h"
AntonLS 0:28ca4562fe1a 36
AntonLS 0:28ca4562fe1a 37 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
AntonLS 0:28ca4562fe1a 38 * it will have an impact on code-size and power consumption. */
AntonLS 0:28ca4562fe1a 39
AntonLS 11:d3aa5fca2330 40 #define LOOPBACK_MODE 0 // Loopback mode
AntonLS 4:17b8edf264c3 41
AntonLS 0:28ca4562fe1a 42 #if NEED_CONSOLE_OUTPUT
AntonLS 0:28ca4562fe1a 43 #define DEBUG(...) { printf(__VA_ARGS__); }
AntonLS 0:28ca4562fe1a 44 #else
AntonLS 0:28ca4562fe1a 45 #define DEBUG(...) /* nothing */
AntonLS 0:28ca4562fe1a 46 #endif /* #if NEED_CONSOLE_OUTPUT */
AntonLS 0:28ca4562fe1a 47
AntonLS 0:28ca4562fe1a 48 BLEDevice ble;
AntonLS 0:28ca4562fe1a 49
AntonLS 0:28ca4562fe1a 50 extern "C"
AntonLS 0:28ca4562fe1a 51 {
AntonLS 12:6d313d575f84 52 void My_UART0_IRQHandler();
AntonLS 12:6d313d575f84 53
AntonLS 0:28ca4562fe1a 54 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 55 }
AntonLS 0:28ca4562fe1a 56
AntonLS 12:6d313d575f84 57 // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins.
AntonLS 12:6d313d575f84 58 DigitalOut led0( P0_19, 1 ); // BLE Nano Low =On
AntonLS 12:6d313d575f84 59 DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On
AntonLS 12:6d313d575f84 60 // DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 12:6d313d575f84 61 DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO
AntonLS 12:6d313d575f84 62
AntonLS 12:6d313d575f84 63 // Check if we should swap serial Rx/Tx for early rev of "Little Brain"
AntonLS 12:6d313d575f84 64 DigitalIn trSwp( P0_30, PullUp );
AntonLS 12:6d313d575f84 65
AntonLS 12:6d313d575f84 66 // Wait to settle.
AntonLS 12:6d313d575f84 67 int foo = (wait( 0.1 ), 0);
AntonLS 12:6d313d575f84 68
AntonLS 12:6d313d575f84 69
AntonLS 1:0ba687d4196f 70 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 71 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 11:d3aa5fca2330 72 // But a version of serial_init() erroneously enabled the uart before the setting of those,
AntonLS 11:d3aa5fca2330 73 // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 11:d3aa5fca2330 74 // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase)
AntonLS 7:205ef63d311a 75 // to not use serial_init() in serial_api.c, so flow control is setup correctly *
AntonLS 7:205ef63d311a 76 // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default *
AntonLS 11:d3aa5fca2330 77 // * We can't change the uart interrupt vector, so we comment-out the handler in
AntonLS 11:d3aa5fca2330 78 // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work.
AntonLS 12:6d313d575f84 79 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might work. TODO
AntonLS 6:ef758ac3c928 80 // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its
AntonLS 11:d3aa5fca2330 81 // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay.
AntonLS 1:0ba687d4196f 82 //
AntonLS 12:6d313d575f84 83 // mts::MTSSerialFlowControl pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
AntonLS 12:6d313d575f84 84 mts::MTSSerial pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), 256, 1280, RTS_PIN_NUMBER, NC ); // 256, 2560
AntonLS 0:28ca4562fe1a 85
AntonLS 0:28ca4562fe1a 86 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 87
AntonLS 0:28ca4562fe1a 88
AntonLS 5:1b9734e68327 89 char deviceName[6]; // "TAF00";
AntonLS 5:1b9734e68327 90 Gap::address_t macAddr;
AntonLS 5:1b9734e68327 91 Gap::addr_type_t *pAdType;
AntonLS 5:1b9734e68327 92
AntonLS 0:28ca4562fe1a 93
AntonLS 12:6d313d575f84 94 // const uint8_t DevInfoServiceUUID_rev[] =
AntonLS 12:6d313d575f84 95 // {
AntonLS 12:6d313d575f84 96 // (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
AntonLS 12:6d313d575f84 97 // };
AntonLS 0:28ca4562fe1a 98
AntonLS 0:28ca4562fe1a 99 UARTService *uartServicePtr;
AntonLS 11:d3aa5fca2330 100 PhoneAppIO *phoneP;
AntonLS 0:28ca4562fe1a 101
AntonLS 11:d3aa5fca2330 102 bool connected = false;
AntonLS 5:1b9734e68327 103
AntonLS 11:d3aa5fca2330 104 void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType,
AntonLS 11:d3aa5fca2330 105 const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams )
AntonLS 11:d3aa5fca2330 106 {
AntonLS 11:d3aa5fca2330 107 connected = true;
AntonLS 11:d3aa5fca2330 108
AntonLS 11:d3aa5fca2330 109 DEBUG( "Connected!\n\r" );
AntonLS 11:d3aa5fca2330 110 }
AntonLS 5:1b9734e68327 111
AntonLS 0:28ca4562fe1a 112 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 113 {
AntonLS 11:d3aa5fca2330 114 connected = false;
AntonLS 11:d3aa5fca2330 115
AntonLS 0:28ca4562fe1a 116 DEBUG( "Disconnected!\n\r" );
AntonLS 0:28ca4562fe1a 117 DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 118 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 119 }
AntonLS 0:28ca4562fe1a 120
AntonLS 5:1b9734e68327 121 bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
AntonLS 5:1b9734e68327 122 {
AntonLS 11:d3aa5fca2330 123 ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead );
AntonLS 5:1b9734e68327 124
AntonLS 5:1b9734e68327 125 if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
AntonLS 5:1b9734e68327 126 (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
AntonLS 7:205ef63d311a 127 {
AntonLS 10:72ceef287b0f 128 pcfc.printf( "\r\nBLE %d! ", err );
AntonLS 7:205ef63d311a 129
AntonLS 7:205ef63d311a 130 } else if ( err == BLE_STACK_BUSY )
AntonLS 7:205ef63d311a 131 {
AntonLS 11:d3aa5fca2330 132 // Common error when pumping data.
AntonLS 7:205ef63d311a 133 }
AntonLS 5:1b9734e68327 134
AntonLS 5:1b9734e68327 135 return (err != BLE_ERROR_NONE);
AntonLS 5:1b9734e68327 136 }
AntonLS 5:1b9734e68327 137
AntonLS 0:28ca4562fe1a 138 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 139 {
AntonLS 11:d3aa5fca2330 140 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 141 {
AntonLS 11:d3aa5fca2330 142 uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload
AntonLS 11:d3aa5fca2330 143 if( 0 != bytesRead )
AntonLS 11:d3aa5fca2330 144 {
AntonLS 11:d3aa5fca2330 145 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 0:28ca4562fe1a 146
AntonLS 11:d3aa5fca2330 147 // Also write to serial port...
AntonLS 11:d3aa5fca2330 148 // pcfc.printf( "From app: " );
AntonLS 11:d3aa5fca2330 149 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 11:d3aa5fca2330 150 // pcfc.printf( "\r\n" );
AntonLS 5:1b9734e68327 151
AntonLS 11:d3aa5fca2330 152 return;
AntonLS 11:d3aa5fca2330 153 }
AntonLS 0:28ca4562fe1a 154 }
AntonLS 11:d3aa5fca2330 155
AntonLS 11:d3aa5fca2330 156 // Other Characteristics here.
AntonLS 0:28ca4562fe1a 157 }
AntonLS 0:28ca4562fe1a 158
AntonLS 0:28ca4562fe1a 159 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 160 {
AntonLS 0:28ca4562fe1a 161 }
AntonLS 0:28ca4562fe1a 162
AntonLS 0:28ca4562fe1a 163 void periodicCallback( void )
AntonLS 0:28ca4562fe1a 164 {
AntonLS 12:6d313d575f84 165 led0 = !led0;
AntonLS 11:d3aa5fca2330 166 led1 = !led1;
AntonLS 3:388e441be8df 167 // rts = !rts;
AntonLS 0:28ca4562fe1a 168 }
AntonLS 0:28ca4562fe1a 169
AntonLS 11:d3aa5fca2330 170 void toPhoneChk( void )
AntonLS 0:28ca4562fe1a 171 {
AntonLS 9:95dc84e9fb7f 172 // if( 0 != rts.read() ) pcfc.puts( "\r\n!RTS disengaged.\r\n" ); // When not using HWFC.
AntonLS 3:388e441be8df 173
AntonLS 11:d3aa5fca2330 174 if( phoneP != NULL )
AntonLS 0:28ca4562fe1a 175 {
AntonLS 11:d3aa5fca2330 176 char ch;
AntonLS 11:d3aa5fca2330 177 // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars.
AntonLS 11:d3aa5fca2330 178 for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ )
AntonLS 0:28ca4562fe1a 179 {
AntonLS 11:d3aa5fca2330 180 if( 0 > phoneP->putchar( ch ) )
AntonLS 11:d3aa5fca2330 181 {
AntonLS 11:d3aa5fca2330 182 pcfc.printf( " * " );
AntonLS 11:d3aa5fca2330 183 break;
AntonLS 11:d3aa5fca2330 184 }
AntonLS 11:d3aa5fca2330 185 if( (cnt >= 20) && ('\n' == ch) ) break;
AntonLS 5:1b9734e68327 186 }
AntonLS 11:d3aa5fca2330 187 // Write to outgoing characteristic if anything is pending.
AntonLS 11:d3aa5fca2330 188 if( 0 != phoneP->maybeHandleWrite() )
AntonLS 5:1b9734e68327 189 {
AntonLS 11:d3aa5fca2330 190 // pcfc.printf( "ToPhoneHandler \r\n" );
AntonLS 0:28ca4562fe1a 191 }
AntonLS 0:28ca4562fe1a 192 }
AntonLS 0:28ca4562fe1a 193 }
AntonLS 0:28ca4562fe1a 194
AntonLS 12:6d313d575f84 195 /*
AntonLS 12:6d313d575f84 196 void led_thread( void const *args )
AntonLS 12:6d313d575f84 197 {
AntonLS 12:6d313d575f84 198 while( true )
AntonLS 12:6d313d575f84 199 {
AntonLS 12:6d313d575f84 200 led0 = !led0;
AntonLS 12:6d313d575f84 201 led1 = !led1;
AntonLS 12:6d313d575f84 202 Thread::wait( 1000 );
AntonLS 12:6d313d575f84 203 }
AntonLS 12:6d313d575f84 204 }
AntonLS 12:6d313d575f84 205 */
AntonLS 12:6d313d575f84 206
AntonLS 0:28ca4562fe1a 207 int main( void )
AntonLS 0:28ca4562fe1a 208 {
AntonLS 12:6d313d575f84 209 // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // TODO maybe try before instantiating pcfc.
AntonLS 12:6d313d575f84 210
AntonLS 0:28ca4562fe1a 211 Ticker ticker;
AntonLS 0:28ca4562fe1a 212 ticker.attach( periodicCallback, 1 );
AntonLS 12:6d313d575f84 213 // Thread thread( led_thread );
AntonLS 0:28ca4562fe1a 214
AntonLS 0:28ca4562fe1a 215 pcfc.baud( 57600 );
AntonLS 0:28ca4562fe1a 216
AntonLS 0:28ca4562fe1a 217 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 1:0ba687d4196f 218
AntonLS 2:fe1566cdb6e7 219
AntonLS 0:28ca4562fe1a 220 ble.init();
AntonLS 11:d3aa5fca2330 221 ble.onConnection( connectionCallback );
AntonLS 0:28ca4562fe1a 222 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 223 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 224 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 225
AntonLS 0:28ca4562fe1a 226 /* setup advertising */
AntonLS 12:6d313d575f84 227 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED |
AntonLS 12:6d313d575f84 228 GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
AntonLS 0:28ca4562fe1a 229 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 5:1b9734e68327 230
AntonLS 5:1b9734e68327 231 // Get MAC addr so we can create a device name using it.
AntonLS 5:1b9734e68327 232 ble.getAddress( pAdType, macAddr );
AntonLS 5:1b9734e68327 233 sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
AntonLS 5:1b9734e68327 234
AntonLS 5:1b9734e68327 235 pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName );
AntonLS 11:d3aa5fca2330 236 #if LOOPBACK_MODE
AntonLS 5:1b9734e68327 237 pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
AntonLS 5:1b9734e68327 238 #endif
AntonLS 5:1b9734e68327 239
AntonLS 0:28ca4562fe1a 240 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 0:28ca4562fe1a 241 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 0:28ca4562fe1a 242 ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 243 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 0:28ca4562fe1a 244
AntonLS 12:6d313d575f84 245 // Necessary?
AntonLS 12:6d313d575f84 246 // ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 12:6d313d575f84 247 // (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );
AntonLS 0:28ca4562fe1a 248
AntonLS 11:d3aa5fca2330 249 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 200 ) );
AntonLS 0:28ca4562fe1a 250 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 251
AntonLS 0:28ca4562fe1a 252 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 0:28ca4562fe1a 253
AntonLS 0:28ca4562fe1a 254 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
AntonLS 0:28ca4562fe1a 255 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 256 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 257 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 258
AntonLS 0:28ca4562fe1a 259 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 260 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 261
AntonLS 11:d3aa5fca2330 262 PhoneAppIO phone( ble, uartService.getRXCharacteristicHandle(),
AntonLS 11:d3aa5fca2330 263 uartService.getTXCharacteristicHandle() );
AntonLS 11:d3aa5fca2330 264 phone.loopbackMode = LOOPBACK_MODE;
AntonLS 11:d3aa5fca2330 265 phoneP = ☎
AntonLS 11:d3aa5fca2330 266
AntonLS 11:d3aa5fca2330 267 Timer tmr;
AntonLS 11:d3aa5fca2330 268 tmr.start();
AntonLS 11:d3aa5fca2330 269
AntonLS 11:d3aa5fca2330 270 // Main Loop
AntonLS 11:d3aa5fca2330 271 for( uint32_t loop=1; ;loop++ )
AntonLS 0:28ca4562fe1a 272 {
AntonLS 0:28ca4562fe1a 273 ble.waitForEvent();
AntonLS 0:28ca4562fe1a 274
AntonLS 11:d3aa5fca2330 275 toPhoneChk(); // Write any pending data to phone.
AntonLS 11:d3aa5fca2330 276
AntonLS 11:d3aa5fca2330 277 while( 0 <= phone.getchar() ); // Eat input.
AntonLS 11:d3aa5fca2330 278
AntonLS 11:d3aa5fca2330 279 // if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() );
AntonLS 0:28ca4562fe1a 280 }
AntonLS 0:28ca4562fe1a 281 }
AntonLS 0:28ca4562fe1a 282
AntonLS 0:28ca4562fe1a 283 /* EOF */