Andriy Makukha
/
football_project_wo_output
football_project_wo_output
Fork of football_project by
main.cpp
- Committer:
- elmbed
- Date:
- 2015-11-29
- Revision:
- 18:affef3a7db2a
- Parent:
- 17:d8b901d791fd
- Child:
- 19:afcbb425b3cf
File content as of revision 18:affef3a7db2a:
/* * 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. * * Using * rev 327 of BLE_API * rev 102 of nRF51822 w/ modification--See below * rev 97 of mbed lib * corresponding to: * rev 493 of mbed-src w/ modification to targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/serial_api.c * (Source no longer needed now that * so things compile properly. * * Changed * nRF51822/nordic/pstorage_platform.h PSTORAGE_MIN_BLOCK_SIZE changed from 0x010 to 4. */ /* 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 <cstdarg> #include <cstdio> #include "mbed.h" // #include "rtos.h" #include "BLEDevice.h" #include "DFUService.h" #include "UARTService.h" #include "DeviceInformationService.h" #include "MTSSerialFlowControl.h" #include "PhoneAppIO.h" #include "TA.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 */ void loop(); void setup(); void getRadioInput(char *ibuffer, int size); static Timer tmr; unsigned long millis() { return tmr.read_ms(); } unsigned long micros() { return tmr.read_us(); } extern "C" { #include "softdevice_handler.h" // Attempt to get pstorage enqueued cmd callbacks. #include "app_timer.h" #include "pstorage.h" // void My_UART0_IRQHandler(); void pin_mode( PinName, PinMode ); } // 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); // Not using "LED" or "LED1" because target NRF51822 for FOTA uses different pins. static DigitalOut led0( P0_19, 1 ); // BLE Nano Low =On static DigitalOut led1( P0_3, 0 ); // TA Baseboard High=On (OK on Nano: Alt RxD) // DigitalOut led1( (trSwp ? P0_4 : P0_3), 0 ); // TA Baseboard High=On (And don't use P0_4 on Nano) // App timer, app scheduler, and pstorage are already setup by bootloader. static BLEDevice ble; //int trSwp = 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.] - No need now. // * Now using $Sub$$UART0_IRQHandler to override UART0_IRQHandler without needing to use lib source. // NVIC_SetVector( UART0_IRQn, (uint32_t)My_UART0_IRQHandler ); // Might have worked--No need. // // 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( 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; // Buffer for holding data from the phone // to the device static char phoneToDev[200] = {0}; // Current position in the buffer static int phoneToDevPos = 0; UARTService *uartServicePtr; PhoneAppIO *phoneP; extern TA ta; extern void radio_init(); extern void radio_loop(); // True when connected to a phone 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; } void disconnectionCallback( Gap::Handle_t handle, Gap::DisconnectionReason_t reason ) { connected = false; ble.startAdvertising(); ta.post_color(0); } 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); } /* Writes the string given to the phone. * * @param *data - the string to send. */ static void writeToPhoneImpl(char *data) { if (phoneP != NULL) { for (int i = 0; i < strlen(data); ++i) { phoneP->putchar(data[i]); // If we don't call maybeHandleWrite all hell breaks loose and // the app crashes. :( if (i != 0 && i % 10 == 0) { int counter = 0; while(phoneP->maybeHandleWrite() == 0 && ++counter < 20) { wait_us(1); } } } phoneP->maybeHandleWrite(); } } static char wtp_buff[150] = {0}; extern "C" void writeToPhone(char *format, ...) { va_list arg; va_start(arg, format ); vsnprintf(wtp_buff, sizeof(wtp_buff), format, arg); writeToPhoneImpl(wtp_buff); va_end(arg); } void onDataWritten( const GattCharacteristicWriteCBParams *params ) { if( phoneP != NULL ) { uint16_t bytesRead = phoneP->maybeHandleRead( params ); // Also writes to txPayload if( 0 != bytesRead ) { memcpy(phoneToDev+phoneToDevPos, txPayload, bytesRead); phoneToDevPos += bytesRead; return; } } } void onDataSent( unsigned count ) { } void toPhoneChk( void ) { } static uint32_t boot_cnt_data = 0; static uint16_t data_block = 9*20 +75; // Last block -- Use for app-start count. static uint16_t data_size = 4; static uint16_t data_offset = 0; // 12 for 16-byte blocks. static bool data_loaded = false; static bool data_cleared = false; static bool data_stored = false; static pstorage_handle_t pstorage_id; static uint32_t pstorage_read_test() { uint32_t err_code; pstorage_handle_t p_block_id; do { err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); if( NRF_SUCCESS != err_code ) break; err_code = pstorage_load( (uint8_t *)&boot_cnt_data, &p_block_id, data_size, data_offset ); if( NRF_SUCCESS != err_code ) break; } while( 0 ); return err_code; } static uint32_t pstorage_clear_test() { uint32_t err_code; pstorage_handle_t p_block_id; do { err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); if( NRF_SUCCESS != err_code ) break; err_code = pstorage_clear( &p_block_id, PSTORAGE_MIN_BLOCK_SIZE ); if( NRF_SUCCESS != err_code ) break; } while( 0 ); return err_code; } static uint32_t pstorage_write_test() { uint32_t err_code; pstorage_handle_t p_block_id; do { err_code = pstorage_block_identifier_get( &pstorage_id, data_block, &p_block_id ); if( NRF_SUCCESS != err_code ) break; err_code = pstorage_store( &p_block_id, (uint8_t *)&boot_cnt_data, data_size, data_offset ); } while( 0 ); return err_code; } static void pstorage_cb_handler( pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len ) { switch( op_code ) { case PSTORAGE_LOAD_OP_CODE: if( NRF_SUCCESS == result ) { // Load operation successful. data_loaded = true; // Flag to signal load is done. } else { // Load operation failed. //pcfc.printf( "\r\nWarn: pstorage load operation error: %x\r\n", result ); } break; case PSTORAGE_UPDATE_OP_CODE: case PSTORAGE_STORE_OP_CODE: if( NRF_SUCCESS == result ) { // Store operation successful. data_stored = true; // Flag to signal store is done. } else { // Store operation failed. //pcfc.printf( "\r\nWarn: pstorage store operation error: %x\r\n", result ); } // Source memory can now be reused or freed. break; case PSTORAGE_CLEAR_OP_CODE: if( NRF_SUCCESS == result ) { // Clear operation successful. data_cleared = true; // Flag to store to the same data area. } else { // Clear operation failed. //pcfc.printf( "\r\nWarn: pstorage clear operation error: %x\r\n", result ); } break; } } static uint32_t pstorage_setup() { pstorage_module_param_t pstorage_param; // Setup pstorage with 9*20 +76 blocks of 4 bytes each. (or 9*20/4 + 19 for 16 byte blocks) pstorage_param.block_size = 4; // Recommended to be >= 4 bytes. pstorage_param.block_count = 9*20 +76; // 9 Sequences x 20 Stations + 76 blocks to fill out to 1k. pstorage_param.cb = pstorage_cb_handler; return pstorage_register( &pstorage_param, &pstorage_id ); } void periodicCallback( void ) { led0 = !led0; led1 = !led1; // rts = !rts; } /* void led_thread( void const *args ) { while( true ) { led0 = !led0; led1 = !led1; Thread::wait( 1000 ); } } */ int main( void ) { Ticker ticker; ticker.attach( periodicCallback, 0.1 ); 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] ); uint32_t p_count; uint32_t pstorageErr = pstorage_init(); // This needs to be called, even though apparently called in bootloader--Check other stuff. pstorage_access_status_get( &p_count ); 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) ); 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 = new PhoneAppIO(ble, uartServicePtr->getRXCharacteristicHandle(), uartServicePtr->getTXCharacteristicHandle() ); phone->loopbackMode = LOOPBACK_MODE; phoneP = phone; DigitalOut *buzzPin = new DigitalOut(p20); *buzzPin = 1; setup(); //radio_init(); tmr.start(); // Main Loop while( true) { ble.waitForEvent(); if (phoneToDevPos > 0) { getRadioInput(phoneToDev, phoneToDevPos ); phoneToDevPos = 0; } loop(); //radio_loop(); if (connected) { phoneP->maybeHandleWrite(); } while( 0 <= phone->getchar() ); // Eat input. } } /**@brief Function for error handling, which is called when an error has occurred. * * @warning This handler is an example only and does not fit a final product. You need to analyze * how your product is supposed to react in case of error. * * @param[in] error_code Error code supplied to the handler. * @param[in] line_num Line number where the handler is called. * @param[in] p_file_name Pointer to the file name. */ void $Sub$$app_error_handler( uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name ) { // nrf_gpio_pin_set( ASSERT_LED_PIN_NO ); //led1 = 1; // This call can be used for debug purposes during application development. // @note CAUTION: Activating this code will write the stack to flash on an error. // This function should NOT be used in a final product. // It is intended STRICTLY for development/debugging purposes. // The flash write will happen EVEN if the radio is active, thus interrupting // any communication. // Use with care. Uncomment the line below to use. // ble_debug_assert_handler(error_code, line_num, p_file_name); // On assert, the system can only recover with a reset. NVIC_SystemReset(); } /* EOF */