football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Committer:
AntonLS
Date:
Mon Apr 13 07:23:58 2015 +0000
Revision:
0:28ca4562fe1a
Child:
1:0ba687d4196f
ARGH

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 0:28ca4562fe1a 46 // serial_t _serial;
AntonLS 0:28ca4562fe1a 47 void pin_mode( PinName, PinMode );
AntonLS 0:28ca4562fe1a 48 }
AntonLS 0:28ca4562fe1a 49
AntonLS 0:28ca4562fe1a 50 // mts::MTSSerialFlowControl pcfc( USBTX, USBRX, RTS_PIN_NUMBER, CTS_PIN_NUMBER, 1280, 1280 );
AntonLS 0:28ca4562fe1a 51 mts::MTSSerial pcfc( USBTX, USBRX, 1280, 1280 );
AntonLS 0:28ca4562fe1a 52
AntonLS 0:28ca4562fe1a 53 uint8_t txPayload[TXRX_BUF_LEN] = { 0 };
AntonLS 0:28ca4562fe1a 54
AntonLS 0:28ca4562fe1a 55 static uint8_t rx_buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 56 static uint8_t rx_len = 0;
AntonLS 0:28ca4562fe1a 57
AntonLS 0:28ca4562fe1a 58
AntonLS 0:28ca4562fe1a 59 const char *deviceName = "TAF00";
AntonLS 0:28ca4562fe1a 60
AntonLS 0:28ca4562fe1a 61 const uint8_t DevInfoServiceUUID_rev[] =
AntonLS 0:28ca4562fe1a 62 {
AntonLS 0:28ca4562fe1a 63 (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8)
AntonLS 0:28ca4562fe1a 64 };
AntonLS 0:28ca4562fe1a 65
AntonLS 0:28ca4562fe1a 66 DigitalOut led1( LED1 );
AntonLS 0:28ca4562fe1a 67 DigitalOut rts( RTS_PIN_NUMBER );
AntonLS 0:28ca4562fe1a 68
AntonLS 0:28ca4562fe1a 69 UARTService *uartServicePtr;
AntonLS 0:28ca4562fe1a 70
AntonLS 0:28ca4562fe1a 71 void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason )
AntonLS 0:28ca4562fe1a 72 {
AntonLS 0:28ca4562fe1a 73 DEBUG( "Disconnected!\n\r" );
AntonLS 0:28ca4562fe1a 74 DEBUG( "Restarting the advertising process\n\r" );
AntonLS 0:28ca4562fe1a 75 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 76 }
AntonLS 0:28ca4562fe1a 77
AntonLS 0:28ca4562fe1a 78 void onDataWritten( const GattCharacteristicWriteCBParams *params )
AntonLS 0:28ca4562fe1a 79 {
AntonLS 0:28ca4562fe1a 80 if( (uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle()) )
AntonLS 0:28ca4562fe1a 81 {
AntonLS 0:28ca4562fe1a 82 uint8_t buf[TXRX_BUF_LEN];
AntonLS 0:28ca4562fe1a 83 uint16_t bytesRead = params->len;
AntonLS 0:28ca4562fe1a 84
AntonLS 0:28ca4562fe1a 85 DEBUG( "received %u bytes\n\r", bytesRead );
AntonLS 0:28ca4562fe1a 86
AntonLS 0:28ca4562fe1a 87 // Loopback data from Central.
AntonLS 0:28ca4562fe1a 88 // ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead ); // Notifies.
AntonLS 0:28ca4562fe1a 89
AntonLS 0:28ca4562fe1a 90 // Also write to serial port...
AntonLS 0:28ca4562fe1a 91 ble.readCharacteristicValue( uartServicePtr->getTXCharacteristicHandle(), buf, &bytesRead );
AntonLS 0:28ca4562fe1a 92 memset( txPayload, 0, TXRX_BUF_LEN );
AntonLS 0:28ca4562fe1a 93 memcpy( txPayload, buf, TXRX_BUF_LEN );
AntonLS 0:28ca4562fe1a 94 // pc.printf( "From app: " );
AntonLS 0:28ca4562fe1a 95 /*
AntonLS 0:28ca4562fe1a 96 for( int index=0; index < bytesRead; index++ )
AntonLS 0:28ca4562fe1a 97 {
AntonLS 0:28ca4562fe1a 98 pc.putc( txPayload[index] );
AntonLS 0:28ca4562fe1a 99 }
AntonLS 0:28ca4562fe1a 100 */
AntonLS 0:28ca4562fe1a 101 pcfc.write( (char *)txPayload, bytesRead );
AntonLS 0:28ca4562fe1a 102 // pc.printf( "\r\n" );
AntonLS 0:28ca4562fe1a 103 }
AntonLS 0:28ca4562fe1a 104 }
AntonLS 0:28ca4562fe1a 105
AntonLS 0:28ca4562fe1a 106 // bool notReady;
AntonLS 0:28ca4562fe1a 107
AntonLS 0:28ca4562fe1a 108 void onDataSent( unsigned count )
AntonLS 0:28ca4562fe1a 109 {
AntonLS 0:28ca4562fe1a 110 // notReady = false;
AntonLS 0:28ca4562fe1a 111 }
AntonLS 0:28ca4562fe1a 112
AntonLS 0:28ca4562fe1a 113 void periodicCallback( void )
AntonLS 0:28ca4562fe1a 114 {
AntonLS 0:28ca4562fe1a 115 led1 = !led1;
AntonLS 0:28ca4562fe1a 116 rts = !rts;
AntonLS 0:28ca4562fe1a 117 }
AntonLS 0:28ca4562fe1a 118
AntonLS 0:28ca4562fe1a 119 void uartCB( void )
AntonLS 0:28ca4562fe1a 120 {
AntonLS 0:28ca4562fe1a 121 // if( notReady ) return;
AntonLS 0:28ca4562fe1a 122
AntonLS 0:28ca4562fe1a 123 // Set line from serial port to RX characteristic (From cone)
AntonLS 0:28ca4562fe1a 124 while( pcfc.readable() )
AntonLS 0:28ca4562fe1a 125 {
AntonLS 0:28ca4562fe1a 126 // rx_buf[rx_len++] = pc.getc();
AntonLS 0:28ca4562fe1a 127 char ch;
AntonLS 0:28ca4562fe1a 128 pcfc.read( ch );
AntonLS 0:28ca4562fe1a 129 rx_buf[rx_len++] = ch;
AntonLS 0:28ca4562fe1a 130 if( rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\r' )
AntonLS 0:28ca4562fe1a 131 {
AntonLS 0:28ca4562fe1a 132 // notReady = true;
AntonLS 0:28ca4562fe1a 133
AntonLS 0:28ca4562fe1a 134 ble.updateCharacteristicValue( uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len ); // Notifies.
AntonLS 0:28ca4562fe1a 135 // pc.printf( "RecHandler \r\n" );
AntonLS 0:28ca4562fe1a 136 rx_len = 0;
AntonLS 0:28ca4562fe1a 137 break;
AntonLS 0:28ca4562fe1a 138 }
AntonLS 0:28ca4562fe1a 139 }
AntonLS 0:28ca4562fe1a 140 }
AntonLS 0:28ca4562fe1a 141
AntonLS 0:28ca4562fe1a 142 int main( void )
AntonLS 0:28ca4562fe1a 143 {
AntonLS 0:28ca4562fe1a 144 led1 = 1;
AntonLS 0:28ca4562fe1a 145 Ticker ticker;
AntonLS 0:28ca4562fe1a 146 ticker.attach( periodicCallback, 1 );
AntonLS 0:28ca4562fe1a 147
AntonLS 0:28ca4562fe1a 148
AntonLS 0:28ca4562fe1a 149 // Note: From the datasheet:
AntonLS 0:28ca4562fe1a 150 // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled.
AntonLS 0:28ca4562fe1a 151 // But serial_init() erroneously enables the uart before the setting of those,
AntonLS 0:28ca4562fe1a 152 // which messes up flow control. Apparently the setting is ONCE per ON mode. ARGH! What
AntonLS 0:28ca4562fe1a 153 // it appears to do is to allow ->HWFC but NOT ->NoFlowControl. So we will make our own
AntonLS 0:28ca4562fe1a 154 // versions of Serial ans SerialBase to override serial_init() in serial_api.c to initially
AntonLS 0:28ca4562fe1a 155 // set to what we want.
AntonLS 0:28ca4562fe1a 156 //
AntonLS 0:28ca4562fe1a 157 /*
AntonLS 0:28ca4562fe1a 158 _serial.uart->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos);
AntonLS 0:28ca4562fe1a 159
AntonLS 0:28ca4562fe1a 160 _serial.uart->PSELRTS = 0xFFFFFFFF; // Disable RTS
AntonLS 0:28ca4562fe1a 161 _serial.uart->PSELCTS = 0xFFFFFFFF; // Disable CTS
AntonLS 0:28ca4562fe1a 162
AntonLS 0:28ca4562fe1a 163 _serial.uart->CONFIG &= ~0x01; // Disable HWFC; // Needs to be done when enabled maybe?
AntonLS 0:28ca4562fe1a 164
AntonLS 0:28ca4562fe1a 165 NRF_GPIO->DIR |= (1<<RTS_PIN_NUMBER);
AntonLS 0:28ca4562fe1a 166 NRF_GPIO->DIR &= ~(1<<CTS_PIN_NUMBER);
AntonLS 0:28ca4562fe1a 167
AntonLS 0:28ca4562fe1a 168 NRF_GPIO->OUTSET = (1<<RTS_PIN_NUMBER);
AntonLS 0:28ca4562fe1a 169
AntonLS 0:28ca4562fe1a 170 // These change the PSELs when enabled which is a no-no, but they also change the HWFC which may require being enabled...
AntonLS 0:28ca4562fe1a 171 // pcfc.set_flow_control( Serial::RTSCTS, RTS_PIN_NUMBER, CTS_PIN_NUMBER ); // Doesn't work because DEVICE_SERIAL_FC is not defined for target.
AntonLS 0:28ca4562fe1a 172 // serial_set_flow_control( &_serial, (FlowControl)Serial::RTSCTS, RTS_PIN_NUMBER, CTS_PIN_NUMBER ); // Workaround DEVICE_SERIAL_FC not being defined (when using Serial class.)
AntonLS 0:28ca4562fe1a 173
AntonLS 0:28ca4562fe1a 174 // serial_set_flow_control( &_serial, (FlowControl)Serial::Disabled, RTS_PIN_NUMBER, CTS_PIN_NUMBER ); // Make sure Flow control mode is off so RTS/CTS can be done w/ GPIO.
AntonLS 0:28ca4562fe1a 175
AntonLS 0:28ca4562fe1a 176 // _serial.uart->PSELRTS = RTS_PIN_NUMBER;
AntonLS 0:28ca4562fe1a 177 // _serial.uart->PSELCTS = CTS_PIN_NUMBER;
AntonLS 0:28ca4562fe1a 178
AntonLS 0:28ca4562fe1a 179 _serial.uart->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
AntonLS 0:28ca4562fe1a 180
AntonLS 0:28ca4562fe1a 181 _serial.uart->TASKS_STARTTX = 1;
AntonLS 0:28ca4562fe1a 182 _serial.uart->TASKS_STARTRX = 1;
AntonLS 0:28ca4562fe1a 183 _serial.uart->EVENTS_RXDRDY = 0;
AntonLS 0:28ca4562fe1a 184 */
AntonLS 0:28ca4562fe1a 185 pin_mode( CTS_PIN_NUMBER, PullDown ); // Workaround for lower layer pulling high (and other end seems to be floating.)
AntonLS 0:28ca4562fe1a 186
AntonLS 0:28ca4562fe1a 187
AntonLS 0:28ca4562fe1a 188 pcfc.baud( 57600 );
AntonLS 0:28ca4562fe1a 189 pcfc.rxClear();
AntonLS 0:28ca4562fe1a 190 // pc.attach( uartCB, pc.RxIrq );
AntonLS 0:28ca4562fe1a 191 char *hello = "\r\nNano nano!\r\n";
AntonLS 0:28ca4562fe1a 192 // pc.printf( hello );
AntonLS 0:28ca4562fe1a 193 pcfc.write( hello, strlen( hello ) );
AntonLS 0:28ca4562fe1a 194
AntonLS 0:28ca4562fe1a 195 char foo[3];
AntonLS 0:28ca4562fe1a 196 sprintf( foo, "%d", LED1 );
AntonLS 0:28ca4562fe1a 197 pcfc.write( foo, 3 );
AntonLS 0:28ca4562fe1a 198 // if( rts.read() == 0 ) pcfc.write( '0' ); else pcfc.write( '1' );
AntonLS 0:28ca4562fe1a 199
AntonLS 0:28ca4562fe1a 200 DEBUG( "Initialising the nRF51822\n\r" );
AntonLS 0:28ca4562fe1a 201 ble.init();
AntonLS 0:28ca4562fe1a 202 ble.onDisconnection( disconnectionCallback );
AntonLS 0:28ca4562fe1a 203 ble.onDataWritten( onDataWritten );
AntonLS 0:28ca4562fe1a 204 ble.onDataSent( onDataSent );
AntonLS 0:28ca4562fe1a 205
AntonLS 0:28ca4562fe1a 206 /* setup advertising */
AntonLS 0:28ca4562fe1a 207 ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED );
AntonLS 0:28ca4562fe1a 208 ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED );
AntonLS 0:28ca4562fe1a 209 ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME,
AntonLS 0:28ca4562fe1a 210 (const uint8_t *)deviceName, strlen(deviceName) );
AntonLS 0:28ca4562fe1a 211 ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 212 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) );
AntonLS 0:28ca4562fe1a 213
AntonLS 0:28ca4562fe1a 214 ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
AntonLS 0:28ca4562fe1a 215 (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) );
AntonLS 0:28ca4562fe1a 216
AntonLS 0:28ca4562fe1a 217 ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 1000 ) );
AntonLS 0:28ca4562fe1a 218 ble.startAdvertising();
AntonLS 0:28ca4562fe1a 219
AntonLS 0:28ca4562fe1a 220 DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" );
AntonLS 0:28ca4562fe1a 221
AntonLS 0:28ca4562fe1a 222 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
AntonLS 0:28ca4562fe1a 223 * control characteristic which can be used to trigger the application to
AntonLS 0:28ca4562fe1a 224 * handover control to a resident bootloader. */
AntonLS 0:28ca4562fe1a 225 DFUService dfu( ble );
AntonLS 0:28ca4562fe1a 226
AntonLS 0:28ca4562fe1a 227 UARTService uartService( ble );
AntonLS 0:28ca4562fe1a 228 uartServicePtr = &uartService;
AntonLS 0:28ca4562fe1a 229
AntonLS 0:28ca4562fe1a 230 while( true )
AntonLS 0:28ca4562fe1a 231 {
AntonLS 0:28ca4562fe1a 232 ble.waitForEvent();
AntonLS 0:28ca4562fe1a 233
AntonLS 0:28ca4562fe1a 234 uartCB();
AntonLS 0:28ca4562fe1a 235 }
AntonLS 0:28ca4562fe1a 236 }
AntonLS 0:28ca4562fe1a 237
AntonLS 0:28ca4562fe1a 238 /* EOF */