Skip to content

Commit

Permalink
some battery management
Browse files Browse the repository at this point in the history
  • Loading branch information
ClockSelect committed Mar 17, 2021
1 parent 63f595a commit a631da4
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 9 deletions.
26 changes: 23 additions & 3 deletions inc/aegis.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
// delivered by the ADC with accurate voltmeter measures to find out the
// exact values needed for his specific box.
//------------------------------------------------------------------------------
#define BVO_CELL1 (+40)
#define BVO_CELL2 (-65)
#define BVO_USB (+365)
#define BVO_CELL1 (+70) // (+40)
#define BVO_CELL2 (+45) // (-65)
#define BVO_USB (+200)
#define BVO_PF4 (+400)


//------------------------------------------------------------------------------
Expand All @@ -45,6 +46,7 @@ typedef enum eEvent
EVENT_NULL = 0, // Null event
EVENT_KEY, // Key input event
EVENT_DISPLAY, // Display event
EVENT_HARDWARE, // Hardware event (battery, ato, usb...)
EVENT_MAX // Total number of event types
}
Event_e;
Expand Down Expand Up @@ -73,6 +75,13 @@ typedef enum eEV_D
}
EV_D_e;

typedef enum eEV_H
{
EV_H_NULL = 0,
EV_H_BATT_STATUS // Battery status change
}
EV_H_e;


//------------------------------------------------------------------------------
// Event structure
Expand All @@ -91,6 +100,7 @@ typedef struct sEvent
union {
EV_K_e k;
EV_D_e d;
EV_H_e h;
uint8_t id;
};
uint8_t p1;
Expand Down Expand Up @@ -199,10 +209,20 @@ extern int SMInputEvent( Event_t *ev );
// Battery Management
//------------------------------------------------------------------------------

typedef enum eBattStatus
{
BATT_OK = 0,
BATT_LOW,
BATT_UNK
}
BATT_STATUS;

extern void BMStartup( void );
extern void BMUpdateBattery( void );
extern void BMReadBattery( void );
extern void BMGetCells( uint32_t *v1, uint32_t *v2 );
extern void BMUpdateStatus( void );
extern BATT_STATUS BMGetStatus( void );


//------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/aegis.s
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ WaitUs PROC

ENDP


ALIGN

END
44 changes: 42 additions & 2 deletions src/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ typedef struct battery_s
uint32_t vcell1;
uint32_t vcell2;
uint32_t vtotal;
BATT_STATUS status;
uint8_t cell1_low :1;
uint8_t cell2_low :1;
}
battery_t;

Expand All @@ -23,6 +26,7 @@ static battery_t battery;
//------------------------------------------------------------------------------
void BMStartup( void )
{
battery.status = BATT_UNK;
}


Expand All @@ -34,6 +38,7 @@ void BMUpdateBattery( void )
if ( t - last > TI_UPDATE_BATTERY )
{
BMReadBattery();
BMUpdateStatus();
}
}

Expand All @@ -43,12 +48,14 @@ void BMReadBattery( void )
uint32_t sample;
uint32_t vbat1, vbat2;

sample = ADCSample( ADC_CH_BATT_CELL1, 16 );
sample = ADCSample( ADC_CH_BATT_CELL1, 1 );
vbat1 = 3 * ADC_VREF * sample >> 13;

sample = ADCSample( ADC_CH_BATT_TOTAL, 16 );
sample = ADCSample( ADC_CH_BATT_TOTAL, 1 );
vbat2 = 3 * ADC_VREF * sample >> 12;

if ( battery.status == BATT_LOW ) vbat2 += BVO_PF4;

if ( vbat2 > vbat1 ) vbat2 -= vbat1;
if ( vbat1 > 0 ) vbat1 += BVO_CELL1;
if ( vbat2 > 0 ) vbat2 += BVO_CELL2;
Expand All @@ -64,3 +71,36 @@ void BMGetCells( uint32_t *v1, uint32_t *v2 )
*v1 = battery.vcell1;
*v2 = battery.vcell2;
}


void BMUpdateStatus( void )
{
BATT_STATUS old = battery.status;

if ( battery.vcell1 < 3000 ) battery.cell1_low = 1;
else if ( battery.vcell1 >= 3100 ) battery.cell1_low = 0;
if ( battery.vcell2 < 3000 ) battery.cell2_low = 1;
else if ( battery.vcell2 >= 3100 ) battery.cell2_low = 0;

if ( battery.cell1_low || battery.cell2_low )
{
battery.status = BATT_LOW;
PF4 = 0;
}
else
{
battery.status = BATT_OK;
PF4 = 1;
}

if ( battery.status != old )
{
EMSendEvent1P( EVENT_HARDWARE, EV_H_BATT_STATUS, battery.status );
}
}


BATT_STATUS BMGetStatus( void )
{
return battery.status;
}
3 changes: 2 additions & 1 deletion src/devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ void ConfigureGPIO( void )
PF1 = 0;
PF2 = 0;
PF3 = 0;
PF4 = 1;
PF7 = 0;

PF4 = 0;
}


Expand Down
20 changes: 20 additions & 0 deletions src/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ int EMGetNextEvent( Event_t *ev )

static void EMKeyEvent( Event_t *ev );
static void EMDisplayEvent( Event_t *ev );
static void EMHardwareEvent( Event_t *ev );


void EMHandleEvents()
Expand All @@ -118,6 +119,10 @@ void EMHandleEvents()
EMDisplayEvent( &ev );
break;

case EVENT_HARDWARE:
EMHardwareEvent( &ev );
break;

case EVENT_NULL:
default:
break;
Expand Down Expand Up @@ -186,3 +191,18 @@ static void EMDisplayEvent( Event_t *ev )
break;
}
}


static void EMHardwareEvent( Event_t *ev )
{
switch ( ev->h )
{
case EV_H_BATT_STATUS:
break;

case EV_H_NULL:
default:
break;
}
}

16 changes: 15 additions & 1 deletion src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,20 @@ static const uint8_t simple_font_x48[] =
0b00000000
};

static const uint8_t simple_font_x4B[] =
{
0b00000000,
0b10000100,
0b10001000,
0b10010000,
0b10110000,
0b11010000,
0b10001000,
0b10001000,
0b10000100,
0b00000000
};

static const uint8_t simple_font_x4C[] =
{
0b00000000,
Expand Down Expand Up @@ -414,7 +428,7 @@ static const font_chardesc_t simple_font_chars_3[] =
{ 7, simple_font_x48 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 6, simple_font_x4B },
{ 6, simple_font_x4C },
{ 0, 0 },
{ 0, 0 },
Expand Down
10 changes: 8 additions & 2 deletions src/screens/screen_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ extern int ScrMainRefresh( void )
BMGetCells( &vbat1, &vbat2 );

FormatNumber( buf, vbat1, 4, 3, 3 );
r.left = 9;
display->Print( buf, &r );

FormatNumber( buf, vbat2, 4, 3, 3 );
r.left = 44;
r.left = 33;
display->Print( buf, &r );

BATT_STATUS bs = BMGetStatus();
if ( bs != BATT_UNK )
{
r.left = 66;
display->Print( bs == BATT_OK ? "OK" : "LO" , &r );
}
}

return 1;
Expand Down

0 comments on commit a631da4

Please sign in to comment.