From 63870906a02df3c812fcb740bd369550ae59b150 Mon Sep 17 00:00:00 2001 From: Steven James Date: Fri, 26 Apr 2019 04:23:22 -0400 Subject: [PATCH 1/2] Update adc_vbat.ino to also work with nrf52840 --- .../examples/Hardware/adc_vbat/adc_vbat.ino | 72 ++++++++----------- 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/libraries/Bluefruit52Lib/examples/Hardware/adc_vbat/adc_vbat.ino b/libraries/Bluefruit52Lib/examples/Hardware/adc_vbat/adc_vbat.ino index ceaa0a14..72eefa51 100644 --- a/libraries/Bluefruit52Lib/examples/Hardware/adc_vbat/adc_vbat.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/adc_vbat/adc_vbat.ino @@ -1,10 +1,21 @@ -#define VBAT_PIN (A7) + #define VBAT_MV_PER_LSB (0.73242188F) // 3.0V ADC range and 12-bit ADC resolution = 3000mV/4096 + +#ifdef NRF52840_XXAA // if this is for nrf52840 +#define VBAT_PIN (A6) +#define VBAT_DIVIDER (0.5F) // 150K + 150K voltage divider on VBAT +#define VBAT_DIVIDER_COMP (2.0F) // Compensation factor for the VBAT divider +#else +#define VBAT_PIN (A7) #define VBAT_DIVIDER (0.71275837F) // 2M + 0.806M voltage divider on VBAT = (2M / (0.806M + 2M)) #define VBAT_DIVIDER_COMP (1.403F) // Compensation factor for the VBAT divider +#endif -int readVBAT(void) { - int raw; +#define REAL_VBAT_MV_PER_LSB (VBAT_DIVIDER_COMP * VBAT_MV_PER_LSB) + + +float readVBAT(void) { + float raw; // Set the analog reference to 3.0V (default = 3.6V) analogReference(AR_INTERNAL_3_0); @@ -22,38 +33,23 @@ int readVBAT(void) { analogReference(AR_DEFAULT); analogReadResolution(10); - return raw; + // Convert the raw value to compensated mv, taking the resistor- + // divider into account (providing the actual LIPO voltage) + // ADC range is 0..3000mV and resolution is 12-bit (0..4095) + return raw * REAL_VBAT_MV_PER_LSB; } uint8_t mvToPercent(float mvolts) { - uint8_t battery_level; + if(mvolts<3300) + return 0; - if (mvolts >= 3000) - { - battery_level = 100; - } - else if (mvolts > 2900) - { - battery_level = 100 - ((3000 - mvolts) * 58) / 100; - } - else if (mvolts > 2740) - { - battery_level = 42 - ((2900 - mvolts) * 24) / 160; - } - else if (mvolts > 2440) - { - battery_level = 18 - ((2740 - mvolts) * 12) / 300; - } - else if (mvolts > 2100) - { - battery_level = 6 - ((2440 - mvolts) * 6) / 340; - } - else - { - battery_level = 0; - } + if(mvolts <3600) { + mvolts -= 3300; + return mvolts/30; + } - return battery_level; + mvolts -= 3600; + return 10 + (mvolts * 0.15F ); // thats mvolts /6.66666666 } void setup() { @@ -66,23 +62,13 @@ void setup() { void loop() { // Get a raw ADC reading - int vbat_raw = readVBAT(); + float vbat_mv = readVBAT(); // Convert from raw mv to percentage (based on LIPO chemistry) - uint8_t vbat_per = mvToPercent(vbat_raw * VBAT_MV_PER_LSB); - - // Convert the raw value to compensated mv, taking the resistor- - // divider into account (providing the actual LIPO voltage) - // ADC range is 0..3000mV and resolution is 12-bit (0..4095), - // VBAT voltage divider is 2M + 0.806M, which needs to be added back - float vbat_mv = (float)vbat_raw * VBAT_MV_PER_LSB * VBAT_DIVIDER_COMP; + uint8_t vbat_per = mvToPercent(vbat_mv); // Display the results - Serial.print("ADC = "); - Serial.print(vbat_raw * VBAT_MV_PER_LSB); - Serial.print(" mV ("); - Serial.print(vbat_raw); - Serial.print(") "); + Serial.print("LIPO = "); Serial.print(vbat_mv); Serial.print(" mV ("); From 7f03236718fb1fc86080a04a9afc571ecbf1a0b2 Mon Sep 17 00:00:00 2001 From: Pol Van Aubel Date: Fri, 3 May 2019 22:47:12 +0200 Subject: [PATCH 2/2] Fix interrupts for device with multiple I/O Ports Fixes adafruit/Adafruit_nRF52_Arduino#259 --- cores/nRF5/WInterrupts.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cores/nRF5/WInterrupts.c b/cores/nRF5/WInterrupts.c index 39e56e07..3ca2f8b4 100644 --- a/cores/nRF5/WInterrupts.c +++ b/cores/nRF5/WInterrupts.c @@ -30,6 +30,12 @@ #define NUMBER_OF_GPIO_TE 4 #endif +#ifdef GPIOTE_CONFIG_PORT_Msk +#define GPIOTE_CONFIG_PORT_PIN_Msk (GPIOTE_CONFIG_PORT_Msk | GPIOTE_CONFIG_PSEL_Msk) +#else +#define GPIOTE_CONFIG_PORT_PIN_Msk GPIOTE_CONFIG_PSEL_Msk +#endif + static voidFuncPtr callbacksInt[NUMBER_OF_GPIO_TE]; static bool callbackDeferred[NUMBER_OF_GPIO_TE]; static int8_t channelMap[NUMBER_OF_GPIO_TE]; @@ -95,8 +101,8 @@ int attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) callbacksInt[ch] = callback; callbackDeferred[ch] = deferred; - NRF_GPIOTE->CONFIG[ch] &= ~(GPIOTE_CONFIG_PSEL_Msk | GPIOTE_CONFIG_POLARITY_Msk); - NRF_GPIOTE->CONFIG[ch] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) | + NRF_GPIOTE->CONFIG[ch] &= ~(GPIOTE_CONFIG_PORT_PIN_Msk | GPIOTE_CONFIG_POLARITY_Msk); + NRF_GPIOTE->CONFIG[ch] |= ((pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PORT_PIN_Msk) | ((polarity << GPIOTE_CONFIG_POLARITY_Pos) & GPIOTE_CONFIG_POLARITY_Msk); NRF_GPIOTE->CONFIG[ch] |= GPIOTE_CONFIG_MODE_Event;