SAADC battery meter.
Dependencies: aconno_bsp adc52832_common
main.cpp
- Committer:
- jurica238814
- Date:
- 2017-08-22
- Revision:
- 0:b5d49f70dcb3
- Child:
- 2:f25e171b8bc1
File content as of revision 0:b5d49f70dcb3:
/* * aconno.de * Made by Jurica Resetar * All right reserved * */ #include "stdio.h" #include "mbed.h" #include "ble/BLE.h" #include "acd52832_bsp.h" #include "GapAdvertisingData.h" #include "nrf52_uart.h" #define SLEEP_TIME (2.0) /* Sleep time in seconds */ #define WAKE_UP_TIME (2000) /* Awake time in ms */ #define MSD_SIZE (1) /* Manufacturer Specific Data lenght (in B) */ #define ADV_INTERVAL (1000) /* Advertising interval in ms */ #define TX_POWER (4) /* TX power (in dB) */ #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */ #define TX (p25) #define RX (p26) bool SLEEP = true; BLE &ble = BLE::Instance(); GapAdvertisingData adv_data = GapAdvertisingData(); uint8_t MSD[MSD_SIZE] = {0x00}; /** * Restart Advertising on disconnection */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){ BLE::Instance().gap().startAdvertising(); } /** * Function for waking the core up */ void wakeMeUp(void){ SLEEP = false; } /** * This function is called when the ble initialization process has failed */ void onBleInitError(BLE &ble, ble_error_t error){ /* Avoid compiler warnings */ (void) ble; (void) error; /* Initialization error handling should go here */ } /** * Callback triggered when the ble initialization process has finished */ void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){ BLE& ble = params->ble; ble_error_t error = params->error; if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ onBleInitError(ble, error); return; } /* Ensure that it is the default instance of BLE */ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } ble.gap().onDisconnection(disconnectionCallback); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(ADV_INTERVAL); ble.gap().startAdvertising(); } void updateData(){ static uint8_t simple_counter = 0; MSD[0] = simple_counter++; adv_data = ble.getAdvertisingData(); adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, MSD, MSD_SIZE); ble.setAdvertisingData(adv_data); } int main(void){ NRF52_UART serial = NRF52_UART(TX, RX, Baud9600); char buffer[255] = {0}; uint8_t buffer_s; int printBufferLen; int16_t SAADCDataPointer; Ticker ticker; ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up ble.init(bleInitComplete); ble.gap().setTxPower(TX_POWER); // Set TX power to TX_POWER /* SpinWait for initialization to complete. This is necessary because the * BLE object is used in the main loop below. */ while (ble.hasInitialized() == false) { /* spin loop */ } while(1){ buffer_s = 0x0C; serial.send(&buffer_s, sizeof(buffer_s)); printBufferLen = sprintf(buffer, "ADC initialisation...\r\n"); serial.send(buffer, printBufferLen); NRF_SAADC->ENABLE = 1; NRF_SAADC->RESULT.MAXCNT = 1; printBufferLen = sprintf(buffer, "SAADC enabled.\r\n"); serial.send(buffer, printBufferLen); NRF_SAADC->RESULT.PTR =(uint32_t) &SAADCDataPointer; NRF_SAADC->CH[0].PSELP = 9; // Input positive pin is VDD printBufferLen = sprintf(buffer, "ADC initialisation done!\r\n"); serial.send(buffer, printBufferLen); NRF_SAADC->TASKS_START = 1; while(!NRF_SAADC->EVENTS_STARTED); printBufferLen = sprintf(buffer, "SAADC started.\r\n"); serial.send(buffer, printBufferLen); NRF_SAADC->TASKS_SAMPLE = 1; printBufferLen = sprintf(buffer, "SAADC sample started.\r\n"); serial.send(buffer, printBufferLen); while(!NRF_SAADC->EVENTS_END); printBufferLen = sprintf(buffer, "SAADC has filled up the Result buffer.\r\n"); serial.send(buffer, printBufferLen); while(!NRF_SAADC->EVENTS_DONE); printBufferLen = sprintf(buffer, "SAADC conversation task has been ended.\r\n"); serial.send(buffer, printBufferLen); while(!NRF_SAADC->EVENTS_RESULTDONE); printBufferLen = sprintf(buffer, "SAADC data is ready to get transfereed to RAM.\r\n"); serial.send(buffer, printBufferLen); printBufferLen = sprintf(buffer, "SAADC Data transfereed: %d.\r\n", (int32_t) NRF_SAADC->RESULT.AMOUNT); serial.send(buffer, printBufferLen); NRF_SAADC->TASKS_STOP = 1; printBufferLen = sprintf(buffer, "SAADC stopping...\r\n"); serial.send(buffer, printBufferLen); while(!NRF_SAADC->EVENTS_STOPPED); printBufferLen = sprintf(buffer, "SAADC stopped.\r\n"); serial.send(buffer, printBufferLen); printBufferLen = sprintf(buffer, "SAADC row: %d\r\n", SAADCDataPointer); serial.send(buffer, printBufferLen); printBufferLen = sprintf(buffer, "SAADC voltage: %f\r\n", SAADCDataPointer*(1.0/1024)*3.6); serial.send(buffer, printBufferLen); wait_ms(3000); } while (true){ if (SLEEP && GO_TO_SLEEP){ ble.gap().stopAdvertising(); sleep(); ble.waitForEvent(); } else{ // I'm awake updateData(); ble.gap().startAdvertising(); wait_ms(WAKE_UP_TIME); SLEEP = true; } } }