Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/sphinx/drv.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Libraries
:caption: Contents

_api/drv_as5048b
_api/drv_battery
_api/drv_hdlc
_api/drv_imu
_api/drv_ism330
Expand Down
1 change: 1 addition & 0 deletions doc/sphinx/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Libraries examples
:maxdepth: 1

_examples/01drv_as5048b
_examples/01drv_battery
_examples/01drv_imu
_examples/01drv_ism330
_examples/01drv_lis2mdl
Expand Down
40 changes: 40 additions & 0 deletions drv/battery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#ifndef __BATTERY_H
#define __BATTERY_H

/**
* @defgroup drv_battery Battery level measurement functions
* @ingroup drv
* @brief Functions for measuring battery level
*
* @{
* @file
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
* @copyright Inria, 2025
* @}
*/

#include <stdint.h>

// For reading the battery level
#if defined(BOARD_DOTBOT_V3)
#define DB_BATTERY_LEVEL_PIN (DB_SAADC_INPUT_AIN1)
#else
#define DB_BATTERY_LEVEL_PIN (DB_SAADC_INPUT_VDD)
#endif

/// Maximum battery level in millivolts
#define DB_BATTERY_LEVEL_MAX_MV (3000)

/**
* @brief Initialize the battery level measurement module
*/
void db_battery_level_init(void);

/**
* @brief Read the battery level in millivolts
*
* @return Battery level in millivolts
*/
uint16_t db_battery_level_read(void);

#endif // __BATTERY_H
19 changes: 19 additions & 0 deletions drv/battery/battery.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>

#include "battery.h"

#include "saadc.h"

void db_battery_level_init(void) {
db_saadc_init(DB_SAADC_RESOLUTION_12BIT);
}

uint16_t db_battery_level_read(void) {
uint16_t value_12b = 0;
db_saadc_read(DB_BATTERY_LEVEL_PIN, &value_12b);
uint16_t voltage_mv = (uint16_t)(((float)value_12b / 4095) * 3600);
if (voltage_mv > DB_BATTERY_LEVEL_MAX_MV) {
voltage_mv = DB_BATTERY_LEVEL_MAX_MV;
}
return voltage_mv;
}
9 changes: 9 additions & 0 deletions drv/drv.emProject
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
<file file_name="as5048b.c" />
<file file_name="../as5048b.h" />
</project>
<project Name="00drv_dotbot_battery">
<configuration
Name="Common"
project_dependencies="00bsp_saadc(bsp)"
project_directory="battery"
project_type="Library" />
<file file_name="battery.c" />
<file file_name="../battery.h" />
</project>
<project Name="00drv_dotbot_hdlc">
<configuration
Name="Common"
Expand Down
1 change: 1 addition & 0 deletions projects/01drv_battery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Reads the battery level
24 changes: 24 additions & 0 deletions projects/01drv_battery/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file
* @ingroup samples_drv
* @author Alexandre Abadie <alexandre.abadie@inria.fr>
* @brief Simple application used to read and print the battery level
*
* @copyright Inria, 2025
*
*/
#include <nrf.h>
#include <stdio.h>
#include "timer.h"
#include "battery.h"

int main(void) {
db_timer_init(0);
db_battery_level_init();

while (1) {
uint16_t battery_level = db_battery_level_read();
printf("Battery level: %d mV\n", battery_level);
db_timer_delay_ms(0, 1000);
}
}
24 changes: 24 additions & 0 deletions projects/projects-bsp-drv.emProject
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,30 @@
<file file_name="$(ProjectDir)/../../nRF/System/cpu.c" />
</folder>
</project>
<project Name="01drv_battery">
<configuration
Name="Common"
project_dependencies="00drv_dotbot_battery(drv);00bsp_dotbot_board(bsp);00bsp_timer(bsp)"
project_directory="01drv_battery"
project_type="Executable" />
<folder Name="Setup">
<file file_name="$(ProjectDir)/../../nRF/Setup/$(Target)_flash_placement.xml" />
<file file_name="$(ProjectDir)/../../nRF/Setup/$(Target)_MemoryMap.xml">
<configuration Name="Common" file_type="Memory Map" />
</file>
<file file_name="../../nRF/Scripts/nRF_Target.js">
<configuration Name="Common" file_type="Reset Script" />
</file>
</folder>
<folder Name="Source">
<configuration Name="Common" filter="c;cpp;cxx;cc;h;s;asm;inc" />
<file file_name="main.c" />
</folder>
<folder Name="System">
<file file_name="$(ProjectDir)/../../nRF/System/$(Target)_system_init.c" />
<file file_name="$(ProjectDir)/../../nRF/System/cpu.c" />
</folder>
</project>
<project Name="01drv_imu">
<configuration
Name="Common"
Expand Down