football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Fri Apr 17 22:12:43 2015 +0000
Revision:
7:205ef63d311a
Parent:
6:ef758ac3c928
Child:
8:d5d055be2bb8
Works again.

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 0:28ca4562fe1a 4 */
AntonLS 0:28ca4562fe1a 5
AntonLS 0:28ca4562fe1a 6 /* mbed Microcontroller Library
AntonLS 0:28ca4562fe1a 7 * Copyright (c) 2006-2013 ARM Limited
AntonLS 0:28ca4562fe1a 8 *
AntonLS 0:28ca4562fe1a 9 * Licensed under the Apache License, Version 2.0 (the "License");
AntonLS 0:28ca4562fe1a 10 * you may not use this file except in compliance with the License.
AntonLS 0:28ca4562fe1a 11 * You may obtain a copy of the License at
AntonLS 0:28ca4562fe1a 12 *
AntonLS 0:28ca4562fe1a 13 * http://www.apache.org/licenses/LICENSE-2.0
AntonLS 0:28ca4562fe1a 14 *
AntonLS 0:28ca4562fe1a 15 * Unless required by applicable law or agreed to in writing, software
AntonLS 0:28ca4562fe1a 16 * distributed under the License is distributed on an "AS IS" BASIS,
AntonLS 0:28ca4562fe1a 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AntonLS 0:28ca4562fe1a 18 * See the License for the specific language governing permissions and
AntonLS 0:28ca4562fe1a 19 * limitations under the License.
AntonLS 0:28ca4562fe1a 20 */
AntonLS 0:28ca4562fe1a 21
AntonLS 0:28ca4562fe1a 22 #include "mbed.h"
AntonLS 0:28ca4562fe1a 23 #include "BLEDevice.h"
AntonLS 0:28ca4562fe1a 24
AntonLS 0:28ca4562fe1a 25 #include "DFUService.h"
AntonLS 0:28ca4562fe1a 26 #include "UARTService.h"
AntonLS 0:28ca4562fe1a 27 #include "DeviceInformationService.h"
AntonLS 0:28ca4562fe1a 28
AntonLS 0:28ca4562fe1a 29 #include "MTSSerialFlowControl.h"
AntonLS 5:1b9734e68327 30 #include "PhoneAppIO.h"
AntonLS 0:28ca4562fe1a 31
AntonLS 0:28ca4562fe1a 32 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
AntonLS 0:28ca4562fe1a 33 * it will have an impact on code-size and power consumption. */
AntonLS 0:28ca4562fe1a 34
AntonLS 5:1b9734e68327 35 // #define LOOPBACK_MODE // Loopback mode
AntonLS 4:17b8edf264c3 36
AntonLS 0:28ca4562fe1a 37 #if NEED_CONSOLE_OUTPUT
AntonLS 0:28ca4562fe1a 38 #define DEBUG(...) { printf(__VA_ARGS__); }
AntonLS 0:28ca4562fe1a 39 #else
AntonLS 0:28ca4562fe1a 40 #define DEBUG(...) /* nothing */
AntonLS 0:28ca4562fe1a 41 #endif /* #if NEED_CONSOLE_OUTPUT */
AntonLS 0:28ca4562fe1a 42
AntonLS 0:28ca4562fe1a 43 BLEDevice ble;
AntonLS 0:28ca4562fe1a 44
AntonLS 5:1b9734e68327 45
AntonLS 0:28ca4562fe1a 46 #define TXRX_BUF_LEN 20
AntonLS 0:28ca4562fe1a 47
AntonLS 0:28ca4562fe1a 48 extern "C"
AntonLS 0:28ca4562fe1a 49 {
AntonLS 2:fe1566cdb6e7 50 // serial_t _my_serial;
AntonLS 0:28ca4562fe1a 51 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 52 }
AntonLS 0:28ca4562fe1a 53
AntonLS 1:0ba687d4196f 54 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 55 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 1:0ba687d4196f 56 // But serial_init() erroneously enables the uart before the setting of those,
AntonLS 1:0ba687d4196f 57 // which messes up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 7:205ef63d311a 58 // So we made our own versions of Serial and SerialBase to (MySerial and MySerialBase)
AntonLS 7:205ef63d311a 59 // to not use serial_init() in serial_api.c, so flow control is setup correctly *
AntonLS 7:205ef63d311a 60 // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default *
AntonLS 7:205ef63d311a 61 // * But unfortunately we can't change the uart interrupt vector in the interrupt table,
AntonLS 7:205ef63d311a 62 // and the current low-level support in serial_api.c won't pass through non-tx/rx interrupts,
AntonLS 7:205ef63d311a 63 // so we'd need to rebuild the mbed lib for low-level hw flow control to work.
AntonLS 6:ef758ac3c928 64 // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its
AntonLS 6:ef758ac3c928 65 // internal buffer--Rx servicing seems fast enough not to need hw flow control, so it's okay.
AntonLS 1:0ba687d4196f 66 //
AntonLS 7:205ef63d311a 67 // mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 );
AntonLS 7:205ef63d311a 68 mts::MTSSerial pcfc( USBTX, USBRX, 384, 2688 );
AntonLS 0:28ca4562fe1a 69
AntonLS 0:28ca4562fe1a 70 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 71
AntonLS 0:28ca4562fe1a 72 static uint8_t rx_buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 73 static uint8_t rx_len = 0;
AntonLS 0:28ca4562fe1a 74
AntonLS 0:28ca4562fe1a 75
AntonLS 1:0ba687d4196f 76 DigitalOut led1( LED1 );
AntonLS 1:0ba687d4196f 77 DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 1:0ba687d4196f 78
AntonLS 1:0ba687d4196f 79
AntonLS 5:1b9734e68327 80 char deviceName[6]; // "TAF00";
AntonLS 5:1b9734e68327 81 Gap::address_t macAddr;
AntonLS 5:1b9734e68327 82 Gap::addr_type_t *pAdType;
AntonLS 5:1b9734e68327 83
AntonLS 0:28ca4562fe1a 84
AntonLS 0:28ca4562fe1a 85 const uint8_t DevInfoServiceUUID_rev[] =
AntonLS 0:28ca4562fe1a 86 {
AntonLS 0:28ca4562fe1a 87 (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
AntonLS 0:28ca4562fe1a 88 };
AntonLS 0:28ca4562fe1a 89
AntonLS 0:28ca4562fe1a 90 UARTService *uartServicePtr;
AntonLS 0:28ca4562fe1a 91
AntonLS 5:1b9734e68327 92 // PhoneAppIO toPhone( &ble );
AntonLS 5:1b9734e68327 93
AntonLS 5:1b9734e68327 94
AntonLS 0:28ca4562fe1a 95 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 96 {
AntonLS 0:28ca4562fe1a 97 DEBUG( "Disconnected!\n\r" );
AntonLS 0:28ca4562fe1a 98 DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 99 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 100 }
AntonLS 0:28ca4562fe1a 101
AntonLS 5:1b9734e68327 102 bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead )
AntonLS 5:1b9734e68327 103 {
AntonLS 5:1b9734e68327 104 ble_error_t err;
AntonLS 5:1b9734e68327 105
AntonLS 5:1b9734e68327 106 // toPhone.toPhoneBuf( (uint8_t *)data, bytesRead );
AntonLS 5:1b9734e68327 107
AntonLS 5:1b9734e68327 108 err = ble.updateCharacteristicValue( handle, data, bytesRead );
AntonLS 5:1b9734e68327 109
AntonLS 5:1b9734e68327 110 if( (err == BLE_ERROR_BUFFER_OVERFLOW) ||
AntonLS 5:1b9734e68327 111 (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) )
AntonLS 7:205ef63d311a 112 {
AntonLS 7:205ef63d311a 113 pcfc.printf( "\r\nBLE %d! ", err );
AntonLS 7:205ef63d311a 114
AntonLS 7:205ef63d311a 115 } else if ( err == BLE_STACK_BUSY )
AntonLS 7:205ef63d311a 116 {
AntonLS 7:205ef63d311a 117 }
AntonLS 5:1b9734e68327 118
AntonLS 5:1b9734e68327 119 return (err != BLE_ERROR_NONE);
AntonLS 5:1b9734e68327 120 }
AntonLS 5:1b9734e68327 121
AntonLS 0:28ca4562fe1a 122 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 123 {
AntonLS 0:28ca4562fe1a 124 if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
AntonLS 0:28ca4562fe1a 125 {
AntonLS 0:28ca4562fe1a 126 uint8_t buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 127 uint16_t bytesRead = params->len;
AntonLS 0:28ca4562fe1a 128
AntonLS 0:28ca4562fe1a 129 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 0:28ca4562fe1a 130
AntonLS 4:17b8edf264c3 131 #ifdef LOOPBACK_MODE
AntonLS 0:28ca4562fe1a 132 // Loopback data from Central.
AntonLS 5:1b9734e68327 133 updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead ); // Notifies.
AntonLS 4:17b8edf264c3 134 #endif
AntonLS 0:28ca4562fe1a 135
AntonLS 5:1b9734e68327 136 ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
AntonLS 5:1b9734e68327 137
AntonLS 0:28ca4562fe1a 138 // Also write to serial port...
AntonLS 0:28ca4562fe1a 139 memset( txPayload, 0, TXRX_BUF_LEN );
AntonLS 0:28ca4562fe1a 140 memcpy( txPayload, buf, TXRX_BUF_LEN );
AntonLS 1:0ba687d4196f 141 // pcfc.printf( "From app: " );
AntonLS 0:28ca4562fe1a 142 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 1:0ba687d4196f 143 // pcfc.printf( "\r\n" );
AntonLS 0:28ca4562fe1a 144 }
AntonLS 0:28ca4562fe1a 145 }
AntonLS 0:28ca4562fe1a 146
AntonLS 5:1b9734e68327 147 bool bleWasntReady;
AntonLS 0:28ca4562fe1a 148
AntonLS 0:28ca4562fe1a 149 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 150 {
AntonLS 0:28ca4562fe1a 151 }
AntonLS 0:28ca4562fe1a 152
AntonLS 0:28ca4562fe1a 153 void periodicCallback( void )
AntonLS 0:28ca4562fe1a 154 {
AntonLS 0:28ca4562fe1a 155 led1 = !led1;
AntonLS 3:388e441be8df 156 // rts = !rts;
AntonLS 0:28ca4562fe1a 157 }
AntonLS 0:28ca4562fe1a 158
AntonLS 0:28ca4562fe1a 159 void uartCB( void )
AntonLS 0:28ca4562fe1a 160 {
AntonLS 3:388e441be8df 161 if( 0 != rts.read() ) pcfc.puts( "\r\n!RTS disengaged.\r\n" ); // Can we read rts when in HW fc mode?
AntonLS 3:388e441be8df 162
AntonLS 0:28ca4562fe1a 163 // Set line from serial port to RX characteristic (From cone)
AntonLS 0:28ca4562fe1a 164 while( pcfc.readable() )
AntonLS 0:28ca4562fe1a 165 {
AntonLS 5:1b9734e68327 166 if( !bleWasntReady )
AntonLS 0:28ca4562fe1a 167 {
AntonLS 5:1b9734e68327 168 char ch;
AntonLS 5:1b9734e68327 169 pcfc.read( ch );
AntonLS 5:1b9734e68327 170 rx_buf[rx_len++] = ch;
AntonLS 5:1b9734e68327 171 }
AntonLS 5:1b9734e68327 172 if( bleWasntReady || rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
AntonLS 5:1b9734e68327 173 {
AntonLS 5:1b9734e68327 174 bleWasntReady = updateCharacteristic( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len ); // Notifies.
AntonLS 0:28ca4562fe1a 175
AntonLS 1:0ba687d4196f 176 // pcfc.printf( "RecHandler \r\n" );
AntonLS 5:1b9734e68327 177
AntonLS 5:1b9734e68327 178 if( !bleWasntReady ) rx_len = 0;
AntonLS 0:28ca4562fe1a 179 break;
AntonLS 0:28ca4562fe1a 180 }
AntonLS 0:28ca4562fe1a 181 }
AntonLS 0:28ca4562fe1a 182 }
AntonLS 0:28ca4562fe1a 183
AntonLS 0:28ca4562fe1a 184 int main( void )
AntonLS 0:28ca4562fe1a 185 {
AntonLS 0:28ca4562fe1a 186 led1 = 1;
AntonLS 0:28ca4562fe1a 187 Ticker ticker;
AntonLS 0:28ca4562fe1a 188 ticker.attach( periodicCallback, 1 );
AntonLS 0:28ca4562fe1a 189
AntonLS 0:28ca4562fe1a 190
AntonLS 0:28ca4562fe1a 191 pcfc.baud( 57600 );
AntonLS 0:28ca4562fe1a 192 pcfc.rxClear();
AntonLS 0:28ca4562fe1a 193 // pc.attach( uartCB, pc.RxIrq );
AntonLS 0:28ca4562fe1a 194
AntonLS 0:28ca4562fe1a 195 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 1:0ba687d4196f 196
AntonLS 2:fe1566cdb6e7 197
AntonLS 0:28ca4562fe1a 198 ble.init();
AntonLS 0:28ca4562fe1a 199 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 200 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 201 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 202
AntonLS 0:28ca4562fe1a 203 /* setup advertising */
AntonLS 0:28ca4562fe1a 204 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED );
AntonLS 0:28ca4562fe1a 205 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 5:1b9734e68327 206
AntonLS 5:1b9734e68327 207 // Get MAC addr so we can create a device name using it.
AntonLS 5:1b9734e68327 208 ble.getAddress( pAdType, macAddr );
AntonLS 5:1b9734e68327 209 sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] );
AntonLS 5:1b9734e68327 210
AntonLS 5:1b9734e68327 211 pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName );
AntonLS 5:1b9734e68327 212 #ifdef LOOPBACK_MODE
AntonLS 5:1b9734e68327 213 pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" );
AntonLS 5:1b9734e68327 214 #endif
AntonLS 5:1b9734e68327 215
AntonLS 0:28ca4562fe1a 216 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 0:28ca4562fe1a 217 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 0:28ca4562fe1a 218 ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 219 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 0:28ca4562fe1a 220
AntonLS 0:28ca4562fe1a 221 ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 222 (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );
AntonLS 0:28ca4562fe1a 223
AntonLS 0:28ca4562fe1a 224 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 1000 ) );
AntonLS 0:28ca4562fe1a 225 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 226
AntonLS 0:28ca4562fe1a 227 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 0:28ca4562fe1a 228
AntonLS 0:28ca4562fe1a 229 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
AntonLS 0:28ca4562fe1a 230 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 231 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 232 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 233
AntonLS 0:28ca4562fe1a 234 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 235 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 236
AntonLS 0:28ca4562fe1a 237 while( true )
AntonLS 0:28ca4562fe1a 238 {
AntonLS 0:28ca4562fe1a 239 ble.waitForEvent();
AntonLS 0:28ca4562fe1a 240
AntonLS 0:28ca4562fe1a 241 uartCB();
AntonLS 0:28ca4562fe1a 242 }
AntonLS 0:28ca4562fe1a 243 }
AntonLS 0:28ca4562fe1a 244
AntonLS 0:28ca4562fe1a 245 /* EOF */