SAADC battery meter.

Dependencies:   aconno_bsp adc52832_common

Committer:
jurica238814
Date:
Tue Aug 22 12:18:42 2017 +0000
Revision:
0:b5d49f70dcb3
Child:
2:f25e171b8bc1
SAADC battery works. Library todo.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 0:b5d49f70dcb3 1 /*
jurica238814 0:b5d49f70dcb3 2 * aconno.de
jurica238814 0:b5d49f70dcb3 3 * Made by Jurica Resetar
jurica238814 0:b5d49f70dcb3 4 * All right reserved
jurica238814 0:b5d49f70dcb3 5 *
jurica238814 0:b5d49f70dcb3 6 */
jurica238814 0:b5d49f70dcb3 7
jurica238814 0:b5d49f70dcb3 8 #include "stdio.h"
jurica238814 0:b5d49f70dcb3 9 #include "mbed.h"
jurica238814 0:b5d49f70dcb3 10 #include "ble/BLE.h"
jurica238814 0:b5d49f70dcb3 11 #include "acd52832_bsp.h"
jurica238814 0:b5d49f70dcb3 12 #include "GapAdvertisingData.h"
jurica238814 0:b5d49f70dcb3 13 #include "nrf52_uart.h"
jurica238814 0:b5d49f70dcb3 14
jurica238814 0:b5d49f70dcb3 15 #define SLEEP_TIME (2.0) /* Sleep time in seconds */
jurica238814 0:b5d49f70dcb3 16 #define WAKE_UP_TIME (2000) /* Awake time in ms */
jurica238814 0:b5d49f70dcb3 17 #define MSD_SIZE (1) /* Manufacturer Specific Data lenght (in B) */
jurica238814 0:b5d49f70dcb3 18 #define ADV_INTERVAL (1000) /* Advertising interval in ms */
jurica238814 0:b5d49f70dcb3 19 #define TX_POWER (4) /* TX power (in dB) */
jurica238814 0:b5d49f70dcb3 20 #define GO_TO_SLEEP (0) /* Sleep flag: 0 -> Device will not go to sleep, 1 -> Will go to sleep mode */
jurica238814 0:b5d49f70dcb3 21
jurica238814 0:b5d49f70dcb3 22 #define TX (p25)
jurica238814 0:b5d49f70dcb3 23 #define RX (p26)
jurica238814 0:b5d49f70dcb3 24
jurica238814 0:b5d49f70dcb3 25 bool SLEEP = true;
jurica238814 0:b5d49f70dcb3 26
jurica238814 0:b5d49f70dcb3 27 BLE &ble = BLE::Instance();
jurica238814 0:b5d49f70dcb3 28 GapAdvertisingData adv_data = GapAdvertisingData();
jurica238814 0:b5d49f70dcb3 29 uint8_t MSD[MSD_SIZE] = {0x00};
jurica238814 0:b5d49f70dcb3 30
jurica238814 0:b5d49f70dcb3 31
jurica238814 0:b5d49f70dcb3 32 /**
jurica238814 0:b5d49f70dcb3 33 * Restart Advertising on disconnection
jurica238814 0:b5d49f70dcb3 34 */
jurica238814 0:b5d49f70dcb3 35 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 0:b5d49f70dcb3 36 BLE::Instance().gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 37 }
jurica238814 0:b5d49f70dcb3 38
jurica238814 0:b5d49f70dcb3 39 /**
jurica238814 0:b5d49f70dcb3 40 * Function for waking the core up
jurica238814 0:b5d49f70dcb3 41 */
jurica238814 0:b5d49f70dcb3 42 void wakeMeUp(void){
jurica238814 0:b5d49f70dcb3 43 SLEEP = false;
jurica238814 0:b5d49f70dcb3 44 }
jurica238814 0:b5d49f70dcb3 45
jurica238814 0:b5d49f70dcb3 46 /**
jurica238814 0:b5d49f70dcb3 47 * This function is called when the ble initialization process has failed
jurica238814 0:b5d49f70dcb3 48 */
jurica238814 0:b5d49f70dcb3 49 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 0:b5d49f70dcb3 50 /* Avoid compiler warnings */
jurica238814 0:b5d49f70dcb3 51 (void) ble;
jurica238814 0:b5d49f70dcb3 52 (void) error;
jurica238814 0:b5d49f70dcb3 53 /* Initialization error handling should go here */
jurica238814 0:b5d49f70dcb3 54 }
jurica238814 0:b5d49f70dcb3 55
jurica238814 0:b5d49f70dcb3 56 /**
jurica238814 0:b5d49f70dcb3 57 * Callback triggered when the ble initialization process has finished
jurica238814 0:b5d49f70dcb3 58 */
jurica238814 0:b5d49f70dcb3 59 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 0:b5d49f70dcb3 60 BLE& ble = params->ble;
jurica238814 0:b5d49f70dcb3 61 ble_error_t error = params->error;
jurica238814 0:b5d49f70dcb3 62
jurica238814 0:b5d49f70dcb3 63 if (error != BLE_ERROR_NONE) {
jurica238814 0:b5d49f70dcb3 64 /* In case of error, forward the error handling to onBleInitError */
jurica238814 0:b5d49f70dcb3 65 onBleInitError(ble, error);
jurica238814 0:b5d49f70dcb3 66 return;
jurica238814 0:b5d49f70dcb3 67 }
jurica238814 0:b5d49f70dcb3 68
jurica238814 0:b5d49f70dcb3 69 /* Ensure that it is the default instance of BLE */
jurica238814 0:b5d49f70dcb3 70 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jurica238814 0:b5d49f70dcb3 71 return;
jurica238814 0:b5d49f70dcb3 72 }
jurica238814 0:b5d49f70dcb3 73
jurica238814 0:b5d49f70dcb3 74 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 0:b5d49f70dcb3 75
jurica238814 0:b5d49f70dcb3 76 /* setup advertising */
jurica238814 0:b5d49f70dcb3 77 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
jurica238814 0:b5d49f70dcb3 78 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 0:b5d49f70dcb3 79 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
jurica238814 0:b5d49f70dcb3 80 ble.gap().setAdvertisingInterval(ADV_INTERVAL);
jurica238814 0:b5d49f70dcb3 81 ble.gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 82 }
jurica238814 0:b5d49f70dcb3 83
jurica238814 0:b5d49f70dcb3 84 void updateData(){
jurica238814 0:b5d49f70dcb3 85 static uint8_t simple_counter = 0;
jurica238814 0:b5d49f70dcb3 86
jurica238814 0:b5d49f70dcb3 87 MSD[0] = simple_counter++;
jurica238814 0:b5d49f70dcb3 88 adv_data = ble.getAdvertisingData();
jurica238814 0:b5d49f70dcb3 89 adv_data.updateData(adv_data.MANUFACTURER_SPECIFIC_DATA, MSD, MSD_SIZE);
jurica238814 0:b5d49f70dcb3 90 ble.setAdvertisingData(adv_data);
jurica238814 0:b5d49f70dcb3 91 }
jurica238814 0:b5d49f70dcb3 92
jurica238814 0:b5d49f70dcb3 93
jurica238814 0:b5d49f70dcb3 94 int main(void){
jurica238814 0:b5d49f70dcb3 95 NRF52_UART serial = NRF52_UART(TX, RX, Baud9600);
jurica238814 0:b5d49f70dcb3 96 char buffer[255] = {0};
jurica238814 0:b5d49f70dcb3 97 uint8_t buffer_s;
jurica238814 0:b5d49f70dcb3 98 int printBufferLen;
jurica238814 0:b5d49f70dcb3 99 int16_t SAADCDataPointer;
jurica238814 0:b5d49f70dcb3 100
jurica238814 0:b5d49f70dcb3 101 Ticker ticker;
jurica238814 0:b5d49f70dcb3 102 ticker.attach(wakeMeUp, SLEEP_TIME); // Wake the device up
jurica238814 0:b5d49f70dcb3 103
jurica238814 0:b5d49f70dcb3 104 ble.init(bleInitComplete);
jurica238814 0:b5d49f70dcb3 105 ble.gap().setTxPower(TX_POWER); // Set TX power to TX_POWER
jurica238814 0:b5d49f70dcb3 106
jurica238814 0:b5d49f70dcb3 107 /* SpinWait for initialization to complete. This is necessary because the
jurica238814 0:b5d49f70dcb3 108 * BLE object is used in the main loop below. */
jurica238814 0:b5d49f70dcb3 109 while (ble.hasInitialized() == false) { /* spin loop */ }
jurica238814 0:b5d49f70dcb3 110
jurica238814 0:b5d49f70dcb3 111 while(1){
jurica238814 0:b5d49f70dcb3 112 buffer_s = 0x0C;
jurica238814 0:b5d49f70dcb3 113 serial.send(&buffer_s, sizeof(buffer_s));
jurica238814 0:b5d49f70dcb3 114
jurica238814 0:b5d49f70dcb3 115 printBufferLen = sprintf(buffer, "ADC initialisation...\r\n");
jurica238814 0:b5d49f70dcb3 116 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 117
jurica238814 0:b5d49f70dcb3 118 NRF_SAADC->ENABLE = 1;
jurica238814 0:b5d49f70dcb3 119 NRF_SAADC->RESULT.MAXCNT = 1;
jurica238814 0:b5d49f70dcb3 120 printBufferLen = sprintf(buffer, "SAADC enabled.\r\n");
jurica238814 0:b5d49f70dcb3 121 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 122
jurica238814 0:b5d49f70dcb3 123 NRF_SAADC->RESULT.PTR =(uint32_t) &SAADCDataPointer;
jurica238814 0:b5d49f70dcb3 124 NRF_SAADC->CH[0].PSELP = 9; // Input positive pin is VDD
jurica238814 0:b5d49f70dcb3 125
jurica238814 0:b5d49f70dcb3 126 printBufferLen = sprintf(buffer, "ADC initialisation done!\r\n");
jurica238814 0:b5d49f70dcb3 127 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 128
jurica238814 0:b5d49f70dcb3 129 NRF_SAADC->TASKS_START = 1;
jurica238814 0:b5d49f70dcb3 130 while(!NRF_SAADC->EVENTS_STARTED);
jurica238814 0:b5d49f70dcb3 131 printBufferLen = sprintf(buffer, "SAADC started.\r\n");
jurica238814 0:b5d49f70dcb3 132 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 133
jurica238814 0:b5d49f70dcb3 134 NRF_SAADC->TASKS_SAMPLE = 1;
jurica238814 0:b5d49f70dcb3 135 printBufferLen = sprintf(buffer, "SAADC sample started.\r\n");
jurica238814 0:b5d49f70dcb3 136 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 137
jurica238814 0:b5d49f70dcb3 138 while(!NRF_SAADC->EVENTS_END);
jurica238814 0:b5d49f70dcb3 139 printBufferLen = sprintf(buffer, "SAADC has filled up the Result buffer.\r\n");
jurica238814 0:b5d49f70dcb3 140 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 141
jurica238814 0:b5d49f70dcb3 142 while(!NRF_SAADC->EVENTS_DONE);
jurica238814 0:b5d49f70dcb3 143 printBufferLen = sprintf(buffer, "SAADC conversation task has been ended.\r\n");
jurica238814 0:b5d49f70dcb3 144 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 145
jurica238814 0:b5d49f70dcb3 146 while(!NRF_SAADC->EVENTS_RESULTDONE);
jurica238814 0:b5d49f70dcb3 147 printBufferLen = sprintf(buffer, "SAADC data is ready to get transfereed to RAM.\r\n");
jurica238814 0:b5d49f70dcb3 148 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 149
jurica238814 0:b5d49f70dcb3 150 printBufferLen = sprintf(buffer, "SAADC Data transfereed: %d.\r\n", (int32_t) NRF_SAADC->RESULT.AMOUNT);
jurica238814 0:b5d49f70dcb3 151 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 152
jurica238814 0:b5d49f70dcb3 153 NRF_SAADC->TASKS_STOP = 1;
jurica238814 0:b5d49f70dcb3 154 printBufferLen = sprintf(buffer, "SAADC stopping...\r\n");
jurica238814 0:b5d49f70dcb3 155 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 156
jurica238814 0:b5d49f70dcb3 157 while(!NRF_SAADC->EVENTS_STOPPED);
jurica238814 0:b5d49f70dcb3 158 printBufferLen = sprintf(buffer, "SAADC stopped.\r\n");
jurica238814 0:b5d49f70dcb3 159 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 160
jurica238814 0:b5d49f70dcb3 161 printBufferLen = sprintf(buffer, "SAADC row: %d\r\n", SAADCDataPointer);
jurica238814 0:b5d49f70dcb3 162 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 163
jurica238814 0:b5d49f70dcb3 164 printBufferLen = sprintf(buffer, "SAADC voltage: %f\r\n", SAADCDataPointer*(1.0/1024)*3.6);
jurica238814 0:b5d49f70dcb3 165 serial.send(buffer, printBufferLen);
jurica238814 0:b5d49f70dcb3 166 wait_ms(3000);
jurica238814 0:b5d49f70dcb3 167 }
jurica238814 0:b5d49f70dcb3 168
jurica238814 0:b5d49f70dcb3 169 while (true){
jurica238814 0:b5d49f70dcb3 170 if (SLEEP && GO_TO_SLEEP){
jurica238814 0:b5d49f70dcb3 171 ble.gap().stopAdvertising();
jurica238814 0:b5d49f70dcb3 172 sleep();
jurica238814 0:b5d49f70dcb3 173 ble.waitForEvent();
jurica238814 0:b5d49f70dcb3 174 }
jurica238814 0:b5d49f70dcb3 175 else{
jurica238814 0:b5d49f70dcb3 176 // I'm awake
jurica238814 0:b5d49f70dcb3 177 updateData();
jurica238814 0:b5d49f70dcb3 178 ble.gap().startAdvertising();
jurica238814 0:b5d49f70dcb3 179 wait_ms(WAKE_UP_TIME);
jurica238814 0:b5d49f70dcb3 180 SLEEP = true;
jurica238814 0:b5d49f70dcb3 181 }
jurica238814 0:b5d49f70dcb3 182 }
jurica238814 0:b5d49f70dcb3 183 }