Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
main.cpp
- Committer:
- AntonLS
- Date:
- 2015-06-16
- Revision:
- 12:6d313d575f84
- Parent:
- 11:d3aa5fca2330
- Child:
- 13:28332f65d14b
File content as of revision 12:6d313d575f84:
/* * TA test * * TODO maybe have a mode where the serial port I/O can be swapped, * such that what the nRF generates is sent out the serial port, * and what comes in the serial port goes into the nRF. * Maybe could use the now-unused CTS pin for that. */ /* mbed Microcontroller Library * Copyright (c) 2006-2013 ARM Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "mbed.h" // #include "rtos.h" #include "BLEDevice.h" #include "DFUService.h" #include "UARTService.h" #include "DeviceInformationService.h" #include "MTSSerialFlowControl.h" #include "PhoneAppIO.h" #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console; * it will have an impact on code-size and power consumption. */ #define LOOPBACK_MODE 0 // Loopback mode #if NEED_CONSOLE_OUTPUT #define DEBUG(...) { printf(__VA_ARGS__); } #else #define DEBUG(...) /* nothing */ #endif /* #if NEED_CONSOLE_OUTPUT */ BLEDevice ble; extern "C" { void My_UART0_IRQHandler(); void pin_mode( PinName, PinMode ); } // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins. DigitalOut led0( P0_19, 1 ); // BLE Nano Low =On DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On // DigitalOut rts( RTS_PIN_NUMBER ); DigitalIn cts( CTS_PIN_NUMBER, PullDown ); // We'll use as a mode switch for serial data source. TODO // Check if we should swap serial Rx/Tx for early rev of "Little Brain" DigitalIn trSwp( P0_30, PullUp ); // Wait to settle. int foo = (wait( 0.1 ), 0); // Note: From the datasheet: // PSELRXD, PSELRTS, PSELTRTS and PSELTXD must only be configured when the UART is disabled. // But a version of serial_init() erroneously enabled the uart before the setting of those, // which messed up flow control. Apparently the setting is ONCE per ON mode. ARGH! // So we made our own versions of Serial and SerialBase (MySerial and MySerialBase) // to not use serial_init() in serial_api.c, so flow control is setup correctly * // MTSSerial now uses our MySerial instead of Serial, and now uses hw flow control by default * // * We can't change the uart interrupt vector, so we comment-out the handler in // serial_api.c, and rebuild the mbed lib for low-level hw flow control to work. // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might work. TODO // MTSSerialFlowControl uses "manual" (non-hardware-low-level) flow control based on its // internal buffer--Rx servicing usually is fast enough not to need hw flow control, so it's okay. // // mts::MTSSerialFlowControl pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), RTS_PIN_NUMBER, CTS_PIN_NUMBER, 384, 2688 ); mts::MTSSerial pcfc( (trSwp ? USBRX : USBTX), (trSwp ? USBTX : USBRX), 256, 1280, RTS_PIN_NUMBER, NC ); // 256, 2560 uint8_t txPayload[TXRX_BUF_LEN] = { 0 }; char deviceName[6]; // "TAF00"; Gap::address_t macAddr; Gap::addr_type_t *pAdType; // const uint8_t DevInfoServiceUUID_rev[] = // { // (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE & 0xFF), (uint8_t)(GattService::UUID_DEVICE_INFORMATION_SERVICE >> 8) // }; UARTService *uartServicePtr; PhoneAppIO *phoneP; bool connected = false; void connectionCallback( Gap::Handle_t, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *connParams ) { connected = true; DEBUG( "Connected!\n\r" ); } void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ) { connected = false; DEBUG( "Disconnected!\n\r" ); DEBUG( "Restarting the advertising process\n\r" ); ble.startAdvertising(); } bool updateCharacteristic( GattAttribute::Handle_t handle, const uint8_t *data, uint16_t bytesRead ) { ble_error_t err = ble.updateCharacteristicValue( handle, data, bytesRead ); if( (err == BLE_ERROR_BUFFER_OVERFLOW) || (err == BLE_ERROR_PARAM_OUT_OF_RANGE ) ) { pcfc.printf( "\r\nBLE %d! ", err ); } else if ( err == BLE_STACK_BUSY ) { // Common error when pumping data. } return (err != BLE_ERROR_NONE); } void onDataWritten( const GattCharacteristicWriteCBParams *params ) { if( phoneP != NULL ) { uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload if( 0 != bytesRead ) { DEBUG( "received %u bytes\n\r", bytesRead ); // Also write to serial port... // pcfc.printf( "From app: " ); pcfc.write( (char *)txPayload, bytesRead ); // pcfc.printf( "\r\n" ); return; } } // Other Characteristics here. } void onDataSent( unsigned count ) { } void periodicCallback( void ) { led0 = !led0; led1 = !led1; // rts = !rts; } void toPhoneChk( void ) { // if( 0 != rts.read() ) pcfc.puts( "\r\n!RTS disengaged.\r\n" ); // When not using HWFC. if( phoneP != NULL ) { char ch; // Get any data from serial port buffer--Full lines if avail--Last line after >= 20 chars. for( int cnt=1; 0 != pcfc.atomicRead( ch ); cnt++ ) { if( 0 > phoneP->putchar( ch ) ) { pcfc.printf( " * " ); break; } if( (cnt >= 20) && ('\n' == ch) ) break; } // Write to outgoing characteristic if anything is pending. if( 0 != phoneP->maybeHandleWrite() ) { // pcfc.printf( "ToPhoneHandler \r\n" ); } } } /* void led_thread( void const *args ) { while( true ) { led0 = !led0; led1 = !led1; Thread::wait( 1000 ); } } */ int main( void ) { // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // TODO maybe try before instantiating pcfc. Ticker ticker; ticker.attach( periodicCallback, 1 ); // Thread thread( led_thread ); pcfc.baud( 57600 ); DEBUG( "Initialising the nRF51822\n\r" ); ble.init(); ble.onConnection( connectionCallback ); ble.onDisconnection( disconnectionCallback ); ble.onDataWritten( onDataWritten ); ble.onDataSent( onDataSent ); /* setup advertising */ ble.accumulateAdvertisingPayload( GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); ble.setAdvertisingType( GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED ); // Get MAC addr so we can create a device name using it. ble.getAddress( pAdType, macAddr ); sprintf( deviceName, "T%02X%02X", macAddr[1], macAddr[0] ); pcfc.printf( "\r\nNano nano! I am \"%s\"\r\n", deviceName ); #if LOOPBACK_MODE pcfc.printf( "\r\nIn BLE Loopback mode.\r\n" ); #endif ble.accumulateAdvertisingPayload( GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName) ); ble.accumulateAdvertisingPayload( GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS, (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed) ); // Necessary? // ble.accumulateScanResponse( GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, // (const uint8_t *)DevInfoServiceUUID_rev, sizeof(DevInfoServiceUUID_rev) ); ble.setAdvertisingInterval( Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS( 200 ) ); ble.startAdvertising(); DeviceInformationService deviceInfo( ble, "TRX", "TrueAgility", "SN0001", "hw-rev1", "fw-rev1" ); /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a * control characteristic which can be used to trigger the application to * handover control to a resident bootloader. */ DFUService dfu( ble ); UARTService uartService( ble ); uartServicePtr = &uartService; PhoneAppIO phone( ble, uartService.getRXCharacteristicHandle(), uartService.getTXCharacteristicHandle() ); phone.loopbackMode = LOOPBACK_MODE; phoneP = ☎ Timer tmr; tmr.start(); // Main Loop for( uint32_t loop=1; ;loop++ ) { ble.waitForEvent(); toPhoneChk(); // Write any pending data to phone. while( 0 <= phone.getchar() ); // Eat input. // if( !(loop % 50) ) phone.printf( "Post: %d\r\n", tmr.read_ms() ); } } /* EOF */