Commit 8859a933 authored by schneider's avatar schneider

Merge branch 'power' into 'master'

Add a power module to access the PMIC values

See merge request card10/firmware!177
parents 010f7ac4 35011d66
.. py:module:: Power
``power`` - PMIC power module handling
======================================
The :py:mod:`power` module allows you to read the card10's power status
in your scripts.
**Example**:
.. code-block:: python
import power
print(power.read_battery_voltage())
.. py:function:: read_battery_voltage()
Read the battery voltage in V. Please keep in mind that battery
voltage behaves exponentially when interpreting this value.
.. warning::
Card10 will hard-shutdown once the voltage drops below 3.4 V
.. py:function:: read_battery_current()
Read the battery-side current flow in A.
.. py:function:: read_chargin_voltage()
Read the charge voltage in V.
.. py:function:: read_chargin_current()
Read the charge current in A.
.. py:function:: read_system_voltage()
Read the system-side voltate in V.
.. py:function:: read_thermistor_voltage()
Read the thermistor voltage in V.
There is a resistor network from GND over a thermistor
(10K at room temperature) over 10K to the Thermistor Bias voltage.
This reads the voltage between thermistor and resistor.
......@@ -54,6 +54,13 @@ typedef _Bool bool;
#define API_DISP_PIXEL 0x28
#define API_DISP_FRAMEBUFFER 0x29
/* API_BATTERY_VOLTAGE 0x30 */
#define API_BATTERY_CURRENT 0x31
#define API_CHARGEIN_VOLTAGE 0x32
#define API_CHARGEIN_CURRENT 0x33
#define API_SYSTEM_VOLTAGE 0x34
#define API_THERMISTOR_VOLTAGE 0x35
#define API_FILE_OPEN 0x40
#define API_FILE_CLOSE 0x41
#define API_FILE_READ 0x42
......@@ -227,15 +234,42 @@ API(API_SYSTEM_EXEC, int __epic_exec(char *name));
API(API_SYSTEM_RESET, void epic_system_reset(void));
/**
* Battery Voltage
* PMIC API
* ===============
*/
/**
* Read the current battery voltage.
*/
API(API_BATTERY_VOLTAGE, int epic_read_battery_voltage(float *result));
/**
* Read the current battery current.
*/
API(API_BATTERY_CURRENT, int epic_read_battery_current(float *result));
/**
* Read the current charge voltage.
*/
API(API_CHARGEIN_VOLTAGE, int epic_read_chargein_voltage(float *result));
/**
* Read the current charge current.
*/
API(API_CHARGEIN_CURRENT, int epic_read_chargein_current(float *result));
/**
* Read the current system voltage.
*/
API(API_SYSTEM_VOLTAGE, int epic_read_system_voltage(float *result));
/**
* Read the current thermistor voltage.
*/
API(API_THERMISTOR_VOLTAGE, int epic_read_thermistor_voltage(float *result));
/**
* UART/Serial Interface
* =====================
......
......@@ -231,6 +231,50 @@ int epic_read_battery_voltage(float *result)
return pmic_read_amux(PMIC_AMUX_BATT_U, result);
}
/*
* API-call for battery current
*/
int epic_read_battery_current(float *result)
{
return pmic_read_amux(PMIC_AMUX_BATT_CHG_I, result);
}
/*
* API-call for charge voltage
*/
int epic_read_chargein_voltage(float *result)
{
return pmic_read_amux(PMIC_AMUX_CHGIN_U, result);
}
/*
* API-call for charge voltage
*/
int epic_read_chargein_current(float *result)
{
return pmic_read_amux(PMIC_AMUX_BATT_CHG_I, result);
}
/*
* API-call for system voltage
*/
int epic_read_system_voltage(float *result)
{
return pmic_read_amux(PMIC_AMUX_SYS_U, result);
}
/*
* API-call for thermistor voltage
*
* Thermistor is as 10k at room temperature,
* voltage divided with another 10k.
* (50% V_bias at room temperature)
*/
int epic_read_thermistor_voltage(float *result)
{
return pmic_read_amux(PMIC_AMUX_THM_U, result);
}
static StaticTimer_t pmic_timer_data;
static void vPmicTimerCb(TimerHandle_t xTimer)
{
......
......@@ -11,6 +11,7 @@ modsrc = files(
'modules/light_sensor.c',
'modules/os.c',
'modules/personal_state.c',
'modules/power.c',
'modules/sys_display.c',
'modules/utime.c',
'modules/vibra.c',
......
......@@ -141,7 +141,7 @@ static mp_obj_t mp_os_urandom(mp_obj_t size_in)
vstr_t vstr;
vstr_init_len(&vstr, size);
epic_trng_read((uint8_t*)vstr.buf, size);
epic_trng_read((uint8_t *)vstr.buf, size);
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
}
......
#include "epicardium.h"
#include "py/builtin.h"
#include "py/obj.h"
#include "py/runtime.h"
static mp_obj_t mp_power_read_battery_voltage()
{
float result;
int status = epic_read_battery_voltage(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_battery_voltage_obj, mp_power_read_battery_voltage
);
static mp_obj_t mp_power_read_battery_current()
{
float result;
int status = epic_read_battery_current(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_battery_current_obj, mp_power_read_battery_current
);
static mp_obj_t mp_power_read_chargein_voltage()
{
float result;
int status = epic_read_chargein_voltage(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_chargein_voltage_obj, mp_power_read_chargein_voltage
);
static mp_obj_t mp_power_read_chargein_current()
{
float result;
int status = epic_read_chargein_current(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_chargein_current_obj, mp_power_read_chargein_current
);
static mp_obj_t mp_power_read_system_voltage()
{
float result;
int status = epic_read_system_voltage(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_system_voltage_obj, mp_power_read_system_voltage
);
static mp_obj_t mp_power_read_thermistor_voltage()
{
float result;
int status = epic_read_thermistor_voltage(&result);
if (status < 0) {
mp_raise_OSError(-status);
return mp_const_none;
}
return mp_obj_new_float(result);
}
static MP_DEFINE_CONST_FUN_OBJ_0(
power_read_thermistor_voltage_obj, mp_power_read_thermistor_voltage
);
static const mp_rom_map_elem_t power_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_power) },
{ MP_ROM_QSTR(MP_QSTR_read_battery_voltage),
MP_ROM_PTR(&power_read_battery_voltage_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_battery_current),
MP_ROM_PTR(&power_read_battery_current_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_chargein_voltage),
MP_ROM_PTR(&power_read_chargein_voltage_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_chargein_current),
MP_ROM_PTR(&power_read_chargein_current_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_system_voltage),
MP_ROM_PTR(&power_read_system_voltage_obj) },
{ MP_ROM_QSTR(MP_QSTR_read_thermistor_voltage),
MP_ROM_PTR(&power_read_thermistor_voltage_obj) },
};
static MP_DEFINE_CONST_DICT(power_module_globals, power_module_globals_table);
const mp_obj_module_t power_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&power_module_globals,
};
/* clang-format off */
MP_REGISTER_MODULE(MP_QSTR_power, power_module, MODULE_POWER_ENABLED);
......@@ -71,6 +71,15 @@ Q(x)
Q(y)
Q(z)
/* power */
Q(power)
Q(read_battery_voltage)
Q(read_battery_current)
Q(read_chargein_voltage)
Q(read_chargein_current)
Q(read_system_voltage)
Q(read_thermistor_voltage)
/* display */
Q(sys_display)
Q(display)
......
......@@ -55,6 +55,7 @@ int mp_hal_trng_read_int(void);
#define MODULE_LIGHT_SENSOR_ENABLED (1)
#define MODULE_OS_ENABLED (1)
#define MODULE_PERSONAL_STATE_ENABLED (1)
#define MODULE_POWER_ENABLED (1)
#define MODULE_UTIME_ENABLED (1)
#define MODULE_VIBRA_ENABLED (1)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment