![](/media/cache/profiles/profileJosh.jpg.50x50_q85.jpg)
A speedtest
Dependencies: aconno_SEGGER_RTT CustomService
main.cpp@9:7e87f594f788, 2019-02-15 (annotated)
- Committer:
- gaggenwaschke
- Date:
- Fri Feb 15 11:30:09 2019 +0000
- Revision:
- 9:7e87f594f788
- Parent:
- 8:3d3b77caecbd
Just Speedtest;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gaggenwaschke | 6:58023588fb4b | 1 | /* Copyright (c) 2016 Aconno-> All Rights Reserved-> |
jurica238814 | 0:796985b808bc | 2 | * |
gaggenwaschke | 6:58023588fb4b | 3 | * Licensees are granted free, non-transferabl use of the information-> NO |
gaggenwaschke | 6:58023588fb4b | 4 | * WARRANTY of ANY KIND is provided-> This heading must NOT be removed from |
gaggenwaschke | 6:58023588fb4b | 5 | * the file-> |
jurica238814 | 0:796985b808bc | 6 | * |
jurica238814 | 0:796985b808bc | 7 | * aconno simple example program |
jurica238814 | 0:796985b808bc | 8 | * blinky LED LD1 |
jurica238814 | 0:796985b808bc | 9 | */ |
gaggenwaschke | 6:58023588fb4b | 10 | |
gaggenwaschke | 6:58023588fb4b | 11 | #define WAKE_UP_TIME (100) |
jurica238814 | 0:796985b808bc | 12 | |
gaggenwaschke | 6:58023588fb4b | 13 | #include "wavr.h" |
gaggenwaschke | 6:58023588fb4b | 14 | #include "CountdownService.h" |
gaggenwaschke | 8:3d3b77caecbd | 15 | #include "SEGGER_RTT.h" |
gaggenwaschke | 6:58023588fb4b | 16 | |
gaggenwaschke | 8:3d3b77caecbd | 17 | #define printf(...) SEGGER_RTT_printf(0,__VA_ARGS__) |
gaggenwaschke | 6:58023588fb4b | 18 | |
gaggenwaschke | 8:3d3b77caecbd | 19 | EventQueue *queue; |
gaggenwaschke | 6:58023588fb4b | 20 | DigitalOut LED_RE(PIN_LED_RED), LED_GR(PIN_LED_GREEN), LED_BL(PIN_LED_BLUE); |
gaggenwaschke | 8:3d3b77caecbd | 21 | AnalogIn MEAS_BAT(PIN_BAT_VOL); |
gaggenwaschke | 6:58023588fb4b | 22 | BLE *bl(&BLE::Instance()); |
gaggenwaschke | 6:58023588fb4b | 23 | uint8_t *mac_address = new uint8_t[6]; |
gaggenwaschke | 8:3d3b77caecbd | 24 | CountdownService *ctd_service; |
gaggenwaschke | 8:3d3b77caecbd | 25 | uint32_t received = 0; |
gaggenwaschke | 8:3d3b77caecbd | 26 | |
gaggenwaschke | 6:58023588fb4b | 27 | int countEventId; |
gaggenwaschke | 6:58023588fb4b | 28 | |
gaggenwaschke | 8:3d3b77caecbd | 29 | struct __attribute__((packed, aligned(1))) bleHeader |
gaggenwaschke | 8:3d3b77caecbd | 30 | { |
gaggenwaschke | 8:3d3b77caecbd | 31 | uint16_t nordicID; // BLE SIG is 0x0059 |
gaggenwaschke | 8:3d3b77caecbd | 32 | uint8_t aconnoID; // aconno id is 69 |
gaggenwaschke | 8:3d3b77caecbd | 33 | uint8_t productID; |
gaggenwaschke | 8:3d3b77caecbd | 34 | uint8_t versionNumber; |
gaggenwaschke | 8:3d3b77caecbd | 35 | }; |
gaggenwaschke | 6:58023588fb4b | 36 | |
gaggenwaschke | 8:3d3b77caecbd | 37 | static bleHeader GetHeader() { |
gaggenwaschke | 8:3d3b77caecbd | 38 | bleHeader header; |
gaggenwaschke | 8:3d3b77caecbd | 39 | header.nordicID = 0x0059; |
gaggenwaschke | 8:3d3b77caecbd | 40 | header.aconnoID = 0x69; |
gaggenwaschke | 8:3d3b77caecbd | 41 | header.productID = PRODUCT_ID; |
gaggenwaschke | 8:3d3b77caecbd | 42 | header.versionNumber = VERSION_NUMBER; |
gaggenwaschke | 8:3d3b77caecbd | 43 | return header; |
gaggenwaschke | 6:58023588fb4b | 44 | } |
gaggenwaschke | 6:58023588fb4b | 45 | |
gaggenwaschke | 6:58023588fb4b | 46 | /** Is called when a connection is established */ |
gaggenwaschke | 6:58023588fb4b | 47 | void OnConnection(const Gap::ConnectionCallbackParams_t *params){ |
gaggenwaschke | 6:58023588fb4b | 48 | bl->gap().stopAdvertising(); |
gaggenwaschke | 6:58023588fb4b | 49 | // Do blinky on connect |
gaggenwaschke | 6:58023588fb4b | 50 | LED_BL = 0; |
gaggenwaschke | 6:58023588fb4b | 51 | wait_ms(400); |
gaggenwaschke | 6:58023588fb4b | 52 | LED_BL = 1; |
gaggenwaschke | 6:58023588fb4b | 53 | } |
gaggenwaschke | 6:58023588fb4b | 54 | |
gaggenwaschke | 6:58023588fb4b | 55 | /** Is called on disconnect of BT */ |
gaggenwaschke | 6:58023588fb4b | 56 | void OnDisconnection(const Gap::DisconnectionCallbackParams_t *params){ |
gaggenwaschke | 6:58023588fb4b | 57 | LED_BL = 1; // switch off blue LED |
gaggenwaschke | 6:58023588fb4b | 58 | bl->gap().startAdvertising(); |
gaggenwaschke | 6:58023588fb4b | 59 | } |
gaggenwaschke | 6:58023588fb4b | 60 | |
gaggenwaschke | 6:58023588fb4b | 61 | /** handler for init error */ |
gaggenwaschke | 6:58023588fb4b | 62 | void BlinkRed() |
gaggenwaschke | 6:58023588fb4b | 63 | { |
gaggenwaschke | 6:58023588fb4b | 64 | LED_RE = !LED_RE; |
gaggenwaschke | 6:58023588fb4b | 65 | } |
gaggenwaschke | 6:58023588fb4b | 66 | |
gaggenwaschke | 8:3d3b77caecbd | 67 | void OnWSent(uint8_t *data, uint16_t length) { |
gaggenwaschke | 8:3d3b77caecbd | 68 | // do some blinky |
gaggenwaschke | 8:3d3b77caecbd | 69 | LED_BL != LED_BL; |
gaggenwaschke | 8:3d3b77caecbd | 70 | received += length; |
gaggenwaschke | 8:3d3b77caecbd | 71 | if ((int)(received - length) / 1024 < (uint32_t)(received / 1024)) |
gaggenwaschke | 8:3d3b77caecbd | 72 | printf("received %u bytes\n", received); |
gaggenwaschke | 8:3d3b77caecbd | 73 | } |
gaggenwaschke | 8:3d3b77caecbd | 74 | |
gaggenwaschke | 6:58023588fb4b | 75 | /** Is called when the bl initialization process has finished. */ |
gaggenwaschke | 6:58023588fb4b | 76 | void OnBleInitComplete(BLE::InitializationCompleteCallbackContext *params){ |
gaggenwaschke | 6:58023588fb4b | 77 | BLE& bl = params->ble; |
gaggenwaschke | 6:58023588fb4b | 78 | ble_error_t error = params->error; |
jurica238814 | 0:796985b808bc | 79 | |
gaggenwaschke | 6:58023588fb4b | 80 | if (error != BLE_ERROR_NONE) { |
gaggenwaschke | 6:58023588fb4b | 81 | /* In case of error, forward the error handling to onBleInitError */ |
gaggenwaschke | 6:58023588fb4b | 82 | queue->call_every(500, BlinkRed); |
gaggenwaschke | 6:58023588fb4b | 83 | return; |
gaggenwaschke | 6:58023588fb4b | 84 | } |
gaggenwaschke | 6:58023588fb4b | 85 | |
gaggenwaschke | 6:58023588fb4b | 86 | // Get my MAC address |
gaggenwaschke | 6:58023588fb4b | 87 | Gap::AddressType_t addr_type; |
gaggenwaschke | 6:58023588fb4b | 88 | Gap::Address_t address; |
gaggenwaschke | 6:58023588fb4b | 89 | bl.gap().getAddress(&addr_type, address); |
gaggenwaschke | 6:58023588fb4b | 90 | |
gaggenwaschke | 6:58023588fb4b | 91 | // attach callbacks |
gaggenwaschke | 6:58023588fb4b | 92 | bl.gap().onDisconnection(OnDisconnection); |
gaggenwaschke | 6:58023588fb4b | 93 | bl.gap().onConnection(OnConnection); |
gaggenwaschke | 6:58023588fb4b | 94 | |
gaggenwaschke | 8:3d3b77caecbd | 95 | ctd_service = new CountdownService(bl.gattServer(), OnWSent); |
gaggenwaschke | 8:3d3b77caecbd | 96 | bleHeader header = GetHeader(); |
gaggenwaschke | 6:58023588fb4b | 97 | |
gaggenwaschke | 6:58023588fb4b | 98 | // setup advertising |
gaggenwaschke | 6:58023588fb4b | 99 | //bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
gaggenwaschke | 6:58023588fb4b | 100 | bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME)); |
gaggenwaschke | 8:3d3b77caecbd | 101 | bl.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&header, sizeof(header)); |
gaggenwaschke | 6:58023588fb4b | 102 | |
gaggenwaschke | 6:58023588fb4b | 103 | bl.gap().setTxPower(TX_POWER); |
gaggenwaschke | 6:58023588fb4b | 104 | bl.gap().setAdvertisingInterval(ADVERTISING_INTERVAL_MS); // --> Has to be at least 100ms! |
gaggenwaschke | 6:58023588fb4b | 105 | |
gaggenwaschke | 6:58023588fb4b | 106 | bl.gap().startAdvertising(); |
gaggenwaschke | 6:58023588fb4b | 107 | } |
jurica238814 | 0:796985b808bc | 108 | |
gaggenwaschke | 6:58023588fb4b | 109 | /** Initializes the flags for propper GPIO operation */ |
gaggenwaschke | 6:58023588fb4b | 110 | void InitGPIO() { |
gaggenwaschke | 6:58023588fb4b | 111 | // Below block of code has to be uncommented if reset block or NFC block is |
gaggenwaschke | 6:58023588fb4b | 112 | // also uncommented. |
gaggenwaschke | 6:58023588fb4b | 113 | NRF_NVMC->CONFIG = 0x00000002; // Erase enabl UICR |
gaggenwaschke | 6:58023588fb4b | 114 | NRF_NVMC->ERASEUICR = 0x00000001; // Erase all |
gaggenwaschke | 6:58023588fb4b | 115 | NRF_NVMC->CONFIG = 0x00000001; // Write enabl UICR |
gaggenwaschke | 6:58023588fb4b | 116 | |
gaggenwaschke | 6:58023588fb4b | 117 | // The following block of code is used to disabl reset input on |
gaggenwaschke | 6:58023588fb4b | 118 | // microcontroler and turn it int gpio. This is used for example on Aconno |
gaggenwaschke | 6:58023588fb4b | 119 | // DK because it has one of the I2C lines connected to reset. Uncoment it |
gaggenwaschke | 6:58023588fb4b | 120 | // if it is needed. |
gaggenwaschke | 6:58023588fb4b | 121 | //NRF_UICR->PSELRESET[0] = 0x8000000F; // Disabl RESET pin -> Enabl GPIO |
gaggenwaschke | 6:58023588fb4b | 122 | //NRF_UICR->PSELRESET[1] = 0x8000000F; // Disabl RESET pin -> Enabl GPIO |
gaggenwaschke | 6:58023588fb4b | 123 | NRF_UICR->PSELRESET[0] = 0x80000015; |
gaggenwaschke | 6:58023588fb4b | 124 | NRF_UICR->PSELRESET[1] = 0x80000015; |
gaggenwaschke | 6:58023588fb4b | 125 | |
gaggenwaschke | 6:58023588fb4b | 126 | // The following block of code is used to enabl NFC pins as gpio. |
gaggenwaschke | 6:58023588fb4b | 127 | NRF_UICR->NFCPINS = 0xFFFFFFFE; // Change NFC to GPIO function |
gaggenwaschke | 6:58023588fb4b | 128 | } |
gaggenwaschke | 6:58023588fb4b | 129 | |
gaggenwaschke | 6:58023588fb4b | 130 | /** Initializes everything needed for bluetooth. */ |
gaggenwaschke | 6:58023588fb4b | 131 | void InitBT() { |
gaggenwaschke | 6:58023588fb4b | 132 | bl->init(OnBleInitComplete); |
gaggenwaschke | 6:58023588fb4b | 133 | bl->gap().setTxPower(TX_POWER); // Set TX power to TX_POWER |
gaggenwaschke | 6:58023588fb4b | 134 | |
gaggenwaschke | 6:58023588fb4b | 135 | // potential delay for setup, sync or async |
gaggenwaschke | 6:58023588fb4b | 136 | while (bl->hasInitialized() == false) { /* spin loop */ } |
gaggenwaschke | 6:58023588fb4b | 137 | } |
gaggenwaschke | 6:58023588fb4b | 138 | |
gaggenwaschke | 6:58023588fb4b | 139 | /** Initializes all settings for GPIO, timers and BLE. */ |
gaggenwaschke | 6:58023588fb4b | 140 | void Init () { |
gaggenwaschke | 6:58023588fb4b | 141 | InitGPIO(); |
gaggenwaschke | 6:58023588fb4b | 142 | LED_RE = 1; |
gaggenwaschke | 6:58023588fb4b | 143 | LED_GR = 1; |
gaggenwaschke | 6:58023588fb4b | 144 | LED_BL = 1; |
gaggenwaschke | 6:58023588fb4b | 145 | InitBT(); |
gaggenwaschke | 6:58023588fb4b | 146 | } |
gaggenwaschke | 6:58023588fb4b | 147 | |
gaggenwaschke | 6:58023588fb4b | 148 | /** Schedules the Processing of BLE events for eventQueue */ |
gaggenwaschke | 6:58023588fb4b | 149 | void ScheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
gaggenwaschke | 6:58023588fb4b | 150 | BLE &bl = context->ble; |
gaggenwaschke | 6:58023588fb4b | 151 | queue->call(Callback<void()> (&bl, &BLE::processEvents)); |
gaggenwaschke | 6:58023588fb4b | 152 | } |
jurica238814 | 0:796985b808bc | 153 | |
jurica238814 | 0:796985b808bc | 154 | int main(){ |
gaggenwaschke | 8:3d3b77caecbd | 155 | printf("\n\n\n\n--------------------------------\nController bootet\n\n"); |
gaggenwaschke | 8:3d3b77caecbd | 156 | |
gaggenwaschke | 6:58023588fb4b | 157 | queue = new EventQueue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE); |
gaggenwaschke | 6:58023588fb4b | 158 | Init(); |
gaggenwaschke | 6:58023588fb4b | 159 | |
gaggenwaschke | 6:58023588fb4b | 160 | bl->onEventsToProcess(ScheduleBleEventsProcessing); |
gaggenwaschke | 8:3d3b77caecbd | 161 | // from now on all execution is controlled by the eventQueue. |
gaggenwaschke | 8:3d3b77caecbd | 162 | // enables the automatic use of low power and other features. |
gaggenwaschke | 6:58023588fb4b | 163 | queue->dispatch_forever(); |
jurica238814 | 0:796985b808bc | 164 | } |