football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Apr 13 14:02:48 2015 +0000
Revision:
2:fe1566cdb6e7
Parent:
1:0ba687d4196f
Child:
3:388e441be8df
Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AntonLS 0:28ca4562fe1a 1 /*
AntonLS 0:28ca4562fe1a 2 * TA test
AntonLS 0:28ca4562fe1a 3 *
AntonLS 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 0:28ca4562fe1a 30
AntonLS 0:28ca4562fe1a 31 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
AntonLS 0:28ca4562fe1a 32 * it will have an impact on code-size and power consumption. */
AntonLS 0:28ca4562fe1a 33
AntonLS 0:28ca4562fe1a 34 #if NEED_CONSOLE_OUTPUT
AntonLS 0:28ca4562fe1a 35 #define DEBUG(...) { printf(__VA_ARGS__); }
AntonLS 0:28ca4562fe1a 36 #else
AntonLS 0:28ca4562fe1a 37 #define DEBUG(...) /* nothing */
AntonLS 0:28ca4562fe1a 38 #endif /* #if NEED_CONSOLE_OUTPUT */
AntonLS 0:28ca4562fe1a 39
AntonLS 0:28ca4562fe1a 40 BLEDevice ble;
AntonLS 0:28ca4562fe1a 41
AntonLS 0:28ca4562fe1a 42 #define TXRX_BUF_LEN 20
AntonLS 0:28ca4562fe1a 43
AntonLS 0:28ca4562fe1a 44 extern "C"
AntonLS 0:28ca4562fe1a 45 {
AntonLS 2:fe1566cdb6e7 46 // serial_t _my_serial;
AntonLS 0:28ca4562fe1a 47 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 48 }
AntonLS 0:28ca4562fe1a 49
AntonLS 1:0ba687d4196f 50 // Note: From the datasheet:
AntonLS 1:0ba687d4196f 51 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 1:0ba687d4196f 52 // But serial_init() erroneously enables the uart before the setting of those,
AntonLS 1:0ba687d4196f 53 // which messes up flow control. Apparently the setting is ONCE per ON mode. ARGH!
AntonLS 1:0ba687d4196f 54 // So we will make our own versions of Serial and SerialBase to (MySerial and MySerialBase)
AntonLS 1:0ba687d4196f 55 // to not use serial_init() in serial_api.c, so flow control is setup correctly.
AntonLS 1:0ba687d4196f 56 // MTSSerial now uses our MySerial instead of Serial.
AntonLS 1:0ba687d4196f 57 // Since MTSSerial now uses hardware flow control by default, MTSSerialFlowControl isn't needed
AntonLS 1:0ba687d4196f 58 // unless the hardware doesn't have hardware flow control capability (but it does.)
AntonLS 1:0ba687d4196f 59 //
AntonLS 0:28ca4562fe1a 60 // mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1280, 1280 );
AntonLS 0:28ca4562fe1a 61 mts::MTSSerial pcfc( USBTX, USBRX, 1280, 1280 );
AntonLS 0:28ca4562fe1a 62
AntonLS 0:28ca4562fe1a 63 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 64
AntonLS 0:28ca4562fe1a 65 static uint8_t rx_buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 66 static uint8_t rx_len = 0;
AntonLS 0:28ca4562fe1a 67
AntonLS 0:28ca4562fe1a 68
AntonLS 1:0ba687d4196f 69 DigitalOut led1( LED1 );
AntonLS 1:0ba687d4196f 70 DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 1:0ba687d4196f 71
AntonLS 1:0ba687d4196f 72
AntonLS 0:28ca4562fe1a 73 const char *deviceName = "TAF00";
AntonLS 0:28ca4562fe1a 74
AntonLS 0:28ca4562fe1a 75 const uint8_t DevInfoServiceUUID_rev[] =
AntonLS 0:28ca4562fe1a 76 {
AntonLS 0:28ca4562fe1a 77 (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
AntonLS 0:28ca4562fe1a 78 };
AntonLS 0:28ca4562fe1a 79
AntonLS 0:28ca4562fe1a 80 UARTService *uartServicePtr;
AntonLS 0:28ca4562fe1a 81
AntonLS 0:28ca4562fe1a 82 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 83 {
AntonLS 0:28ca4562fe1a 84 DEBUG( "Disconnected!\n\r" );
AntonLS 0:28ca4562fe1a 85 DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 86 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 87 }
AntonLS 0:28ca4562fe1a 88
AntonLS 0:28ca4562fe1a 89 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 90 {
AntonLS 0:28ca4562fe1a 91 if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
AntonLS 0:28ca4562fe1a 92 {
AntonLS 0:28ca4562fe1a 93 uint8_t buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 94 uint16_t bytesRead = params->len;
AntonLS 0:28ca4562fe1a 95
AntonLS 0:28ca4562fe1a 96 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 0:28ca4562fe1a 97
AntonLS 0:28ca4562fe1a 98 // Loopback data from Central.
AntonLS 0:28ca4562fe1a 99 // ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead ); // Notifies.
AntonLS 0:28ca4562fe1a 100
AntonLS 0:28ca4562fe1a 101 // Also write to serial port...
AntonLS 0:28ca4562fe1a 102 ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
AntonLS 0:28ca4562fe1a 103 memset( txPayload, 0, TXRX_BUF_LEN );
AntonLS 0:28ca4562fe1a 104 memcpy( txPayload, buf, TXRX_BUF_LEN );
AntonLS 1:0ba687d4196f 105 // pcfc.printf( "From app: " );
AntonLS 0:28ca4562fe1a 106 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 1:0ba687d4196f 107 // pcfc.printf( "\r\n" );
AntonLS 0:28ca4562fe1a 108 }
AntonLS 0:28ca4562fe1a 109 }
AntonLS 0:28ca4562fe1a 110
AntonLS 0:28ca4562fe1a 111 // bool notReady;
AntonLS 0:28ca4562fe1a 112
AntonLS 0:28ca4562fe1a 113 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 114 {
AntonLS 0:28ca4562fe1a 115 // notReady = false;
AntonLS 0:28ca4562fe1a 116 }
AntonLS 0:28ca4562fe1a 117
AntonLS 0:28ca4562fe1a 118 void periodicCallback( void )
AntonLS 0:28ca4562fe1a 119 {
AntonLS 0:28ca4562fe1a 120 led1 = !led1;
AntonLS 0:28ca4562fe1a 121 rts = !rts;
AntonLS 0:28ca4562fe1a 122 }
AntonLS 0:28ca4562fe1a 123
AntonLS 0:28ca4562fe1a 124 void uartCB( void )
AntonLS 0:28ca4562fe1a 125 {
AntonLS 0:28ca4562fe1a 126 // if( notReady ) return;
AntonLS 0:28ca4562fe1a 127
AntonLS 0:28ca4562fe1a 128 // Set line from serial port to RX characteristic (From cone)
AntonLS 0:28ca4562fe1a 129 while( pcfc.readable() )
AntonLS 0:28ca4562fe1a 130 {
AntonLS 0:28ca4562fe1a 131 char ch;
AntonLS 0:28ca4562fe1a 132 pcfc.read( ch );
AntonLS 0:28ca4562fe1a 133 rx_buf[rx_len++] = ch;
AntonLS 0:28ca4562fe1a 134 if( rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
AntonLS 0:28ca4562fe1a 135 {
AntonLS 0:28ca4562fe1a 136 // notReady = true;
AntonLS 0:28ca4562fe1a 137
AntonLS 0:28ca4562fe1a 138 ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len ); // Notifies.
AntonLS 1:0ba687d4196f 139 // pcfc.printf( "RecHandler \r\n" );
AntonLS 0:28ca4562fe1a 140 rx_len = 0;
AntonLS 0:28ca4562fe1a 141 break;
AntonLS 0:28ca4562fe1a 142 }
AntonLS 0:28ca4562fe1a 143 }
AntonLS 0:28ca4562fe1a 144 }
AntonLS 0:28ca4562fe1a 145
AntonLS 0:28ca4562fe1a 146 int main( void )
AntonLS 0:28ca4562fe1a 147 {
AntonLS 0:28ca4562fe1a 148 led1 = 1;
AntonLS 0:28ca4562fe1a 149 Ticker ticker;
AntonLS 0:28ca4562fe1a 150 ticker.attach( periodicCallback, 1 );
AntonLS 0:28ca4562fe1a 151
AntonLS 0:28ca4562fe1a 152
AntonLS 0:28ca4562fe1a 153 pcfc.baud( 57600 );
AntonLS 0:28ca4562fe1a 154 pcfc.rxClear();
AntonLS 0:28ca4562fe1a 155 // pc.attach( uartCB, pc.RxIrq );
AntonLS 1:0ba687d4196f 156 pcfc.printf( "\r\nNano nano!\r\n" );
AntonLS 0:28ca4562fe1a 157
AntonLS 1:0ba687d4196f 158 pcfc.printf( "RTS pin: %d\r\n", rts.read() );
AntonLS 0:28ca4562fe1a 159
AntonLS 0:28ca4562fe1a 160 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 1:0ba687d4196f 161
AntonLS 2:fe1566cdb6e7 162
AntonLS 0:28ca4562fe1a 163 ble.init();
AntonLS 0:28ca4562fe1a 164 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 165 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 166 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 167
AntonLS 0:28ca4562fe1a 168 /* setup advertising */
AntonLS 0:28ca4562fe1a 169 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED );
AntonLS 0:28ca4562fe1a 170 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 0:28ca4562fe1a 171 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 0:28ca4562fe1a 172 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 0:28ca4562fe1a 173 ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 174 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 0:28ca4562fe1a 175
AntonLS 0:28ca4562fe1a 176 ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 177 (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );
AntonLS 0:28ca4562fe1a 178
AntonLS 0:28ca4562fe1a 179 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 1000 ) );
AntonLS 0:28ca4562fe1a 180 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 181
AntonLS 0:28ca4562fe1a 182 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 0:28ca4562fe1a 183
AntonLS 0:28ca4562fe1a 184 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
AntonLS 0:28ca4562fe1a 185 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 186 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 187 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 188
AntonLS 0:28ca4562fe1a 189 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 190 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 191
AntonLS 0:28ca4562fe1a 192 while( true )
AntonLS 0:28ca4562fe1a 193 {
AntonLS 0:28ca4562fe1a 194 ble.waitForEvent();
AntonLS 0:28ca4562fe1a 195
AntonLS 0:28ca4562fe1a 196 uartCB();
AntonLS 0:28ca4562fe1a 197 }
AntonLS 0:28ca4562fe1a 198 }
AntonLS 0:28ca4562fe1a 199
AntonLS 0:28ca4562fe1a 200 /* EOF */