Hi, i am tryint to use the battery characteristic on P8 do I need to have a dedicated GPIO for the GPIO_SETTINGS_BATTERY ?
If so does this GPIO has to be connected to a physical PIN ?
I am usine 2 AA battery or mini USB using 5 V to 3.3 V LDO to power the board with automatic detection of mini USB vs battery. Right now everytime I read the characteristic using BLE Explorer even after 40 hours of usage It is still reporting 100 %
Other than calling blebat_Init(); do I need to plug specific code ?
// Following structure defines GPIO configuration used by the application
const BLE_PROFILE_GPIO_CFG meatsitter_gpio_cfg =
{
/*.gpio_pin =*/
{
-1,//GPIO_PIN_WP, // This need to be used to enable/disable NVRAM write protect
-1,//GPIO_PIN_BUTTON, // Button GPIO is configured to trigger either direction of interrupt
-1,//GPIO_PIN_LED, // LED GPIO, optional to provide visual effects
8,//GPIO_PIN_BATTERY, // Battery monitoring GPIO. When it is lower than particular level, it will give notification to the application
-1,//GPIO_PIN_BUZZER, // Buzzer GPIO, optional to provide audio effects
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 // other GPIOs are not used
},
/*.gpio_flag =*/
{
0,//GPIO_SETTINGS_WP,
0,//GPIO_SETTINGS_BUTTON,
0,//GPIO_SETTINGS_LED,
0,//GPIO_SETTINGS_BATTERY,
0,//GPIO_SETTINGS_BUZZER,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
}
};
// Initialize GATT database
void my_database_init(void)
{
//Initialized ROM code which will monitor the battery
blebat_Init();
}
Solved! Go to Solution.
The battery monitoring system (using P15) is coded into the rom of our solutions. One cannot changed it to say P8 in this case.
I believe we exclusively use P15 for this. You can look up Wiced-Smart\bleapp\app\blebat.c for more details.
You can also look up wiced_sense sample app on how to implement battery monitoring.
I was able to use P8 as adc input and read the pin since I don't use the BLE GAT battery service. Instead I am sending the percentage inside a custom characteristic.
I merged some code from the wiced sense sample to do something like that :
I am using 2 AAA battery unlike wiced sense which is using a coin cell battery so what would it relate to for WICED_SENSE_BATTERY_LEVEL_FULL ?
Is the only way to know consist in reading ADC value at full charge ?
For the correct value for adc_SetInputRange for 2 AAA battery I used adc_SetInputRange(ADC_RANGE_0_3P6V);
#define WICED_SENSE_BATTERY_LEVEL_FULL (3000) <-- not sure where this number is coming from and how it correlate ti AAA battery instead of coin cell
#define WICED_SENSE_BATTERY_LEVEL_EMPTY (1800)
/// input voltage range selection
typedef enum ADC_INPUT_RANGE_SEL
{
/// Range 0 to 1p2V. USe when no input to any
/// ADC channel exceeds 1.2V.
ADC_RANGE_0_1P2V=0,
/// Range 0 to 2p4V. USe when no input to any
/// ADC channel exceeds 2.4V.
ADC_RANGE_0_2P4V,
/// Range 0 to 3p6V. USe when no input to any
/// ADC channel exceeds 3.6V.
ADC_RANGE_0_3P6V,
ADC_MAX_RANGES=3,
}ADC_INPUT_RANGE_SEL;
#define BLEPTUM_ADC_CH1 ADC_INPUT_P14
#define BLEPTUM_ADC_CH2 ADC_INPUT_P18 -> change to ADC_INPUT_P8
#define BLEPTUM_ADC_CH3 ADC_INPUT_P0
#define WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE 8
INT16 wiced_sense_battery_measurements[WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE];
UINT8 wiced_sense_battery_measurement_count = 0;
INT32 wiced_sense_battery_measurements_sum = 0;
UINT8 wiced_sense_battery_measurements_oldest_index = 0;
INT16 wiced_sense_battery_measurements_average = 0;
UINT8 wiced_sense_battery_level_in_percentage = 0;
static void wiced_sense_scale_battery_level_to_percentage_and_update(void);
static void wiced_sense_measure_battery_level_and_average(void);
bleapp_set_cfg((UINT8 *)myapp_gatt_database,
sizeof(myapp_gatt_database),
(void *)&myapp_cfg,
(void *)&myapp_puart_cfg,
(void *)NULL, // leave GPIO init blank
myapp_create);
void init()
{
//init ADC
int i=0;
adc_config();
adc_SetInputRange(ADC_RANGE_0_3P6V);
// Fill up the average measurements
for(i = 0; i < WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE; i++)
{
// Get the first few battery level readings.
wiced_sense_measure_battery_level_and_average();
}
}
// Measures the average ADC reading of the battery level
void wiced_sense_measure_battery_level_and_average(void)
{
// P8 is connected to the battery.
INT16 new_measurement = adc_readVoltage(BLEPTUM_ADC_CH2);
// Subtract the oldest
wiced_sense_battery_measurements_sum -= wiced_sense_battery_measurements[wiced_sense_battery_measurements_oldest_index];
// Add the newest
wiced_sense_battery_measurements[wiced_sense_battery_measurements_oldest_index] = new_measurement;
wiced_sense_battery_measurements_sum += new_measurement;
// Adjust the oldest index and wraparound
if(++wiced_sense_battery_measurements_oldest_index >= WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE)
wiced_sense_battery_measurements_oldest_index = 0;
wiced_sense_battery_measurements_average = wiced_sense_battery_measurements_sum / WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE;
if((wiced_sense_battery_measurements_sum % WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE) >= (WICED_SENSE_NUM_BAT_MEASUREMENTS_TO_AVERAGE / 2))
wiced_sense_battery_measurements_average++;
}
//Scale battery level into percentage
void wiced_sense_scale_battery_level_to_percentage_and_update(void)
{
UINT32 temp;
wiced_sense_measure_battery_level_and_average();
ble_trace1("Battery: wiced_sense_battery_measurements_average = %d", wiced_sense_battery_measurements_average);
if(wiced_sense_battery_measurements_average > WICED_SENSE_BATTERY_LEVEL_FULL)
wiced_sense_battery_level_in_percentage = 100;
else if (wiced_sense_battery_measurements_average <= WICED_SENSE_BATTERY_LEVEL_EMPTY)
wiced_sense_battery_level_in_percentage = 0;
else
{
temp = wiced_sense_battery_measurements_average - WICED_SENSE_BATTERY_LEVEL_EMPTY;
temp *= 100; // for percentage.
temp /= (WICED_SENSE_BATTERY_LEVEL_FULL - WICED_SENSE_BATTERY_LEVEL_EMPTY);
wiced_sense_battery_level_in_percentage = temp; // this is the value we can send back to the device connected to this wiced device
}
ble_trace1("Battery: Level = %d", wiced_sense_battery_level_in_percentage);
}
// handles the fine timeout application processing, every tick (12.5ms)
static void bleptum_FineTimeout(UINT32 count)
{
wiced_sense_scale_battery_level_to_percentage_and_update();
}
The battery monitoring system (using P15) is coded into the rom of our solutions. One cannot changed it to say P8 in this case.
Those lines came from blebat.c (see line 93 and 94) as wiced sense used the Battery Service for battery level monitoring via P15.