Skip to content

Commit

Permalink
Add uptime counter to the health packet (commaai#391)
Browse files Browse the repository at this point in the history
* Added uptime counter to the health packet

* fix tests by setting power save on on EON build
  • Loading branch information
robbederks authored and rbiasini committed Nov 22, 2019
1 parent 1662481 commit f458d67
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.6.5
v1.6.6
2 changes: 1 addition & 1 deletion board/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ typedef void (*board_set_led)(uint8_t color, bool enabled);
typedef void (*board_set_usb_power_mode)(uint8_t mode);
typedef void (*board_set_esp_gps_mode)(uint8_t mode);
typedef void (*board_set_can_mode)(uint8_t mode);
typedef void (*board_usb_power_mode_tick)(uint64_t tcnt);
typedef void (*board_usb_power_mode_tick)(uint32_t uptime);
typedef bool (*board_check_ignition)(void);
typedef uint32_t (*board_read_current)(void);
typedef void (*board_set_ir_power)(uint8_t percentage);
Expand Down
4 changes: 2 additions & 2 deletions board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ void black_set_can_mode(uint8_t mode){
}
}

void black_usb_power_mode_tick(uint64_t tcnt){
UNUSED(tcnt);
void black_usb_power_mode_tick(uint32_t uptime){
UNUSED(uptime);
// Not applicable
}

Expand Down
4 changes: 2 additions & 2 deletions board/boards/pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ void pedal_set_can_mode(uint8_t mode){
}
}

void pedal_usb_power_mode_tick(uint64_t tcnt){
UNUSED(tcnt);
void pedal_usb_power_mode_tick(uint32_t uptime){
UNUSED(uptime);
// Not applicable
}

Expand Down
4 changes: 2 additions & 2 deletions board/boards/uno.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ void uno_set_bootkick(bool enabled){
set_gpio_output(GPIOB, 14, !enabled);
}

void uno_usb_power_mode_tick(uint64_t tcnt){
if(tcnt == 3U){
void uno_usb_power_mode_tick(uint32_t uptime){
if(uptime == 3U){
uno_set_bootkick(false);
}
}
Expand Down
24 changes: 12 additions & 12 deletions board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ uint32_t white_read_current(void){
return adc_get(ADCCHAN_CURRENT);
}

uint64_t marker = 0;
void white_usb_power_mode_tick(uint64_t tcnt){
uint32_t marker = 0;
void white_usb_power_mode_tick(uint32_t uptime){

// on EON or BOOTSTUB, no state machine
#if !defined(BOOTSTUB) && !defined(EON)
Expand All @@ -173,55 +173,55 @@ void white_usb_power_mode_tick(uint64_t tcnt){

switch (usb_power_mode) {
case USB_POWER_CLIENT:
if ((tcnt - marker) >= CLICKS) {
if ((uptime - marker) >= CLICKS) {
if (!is_enumerated) {
puts("USBP: didn't enumerate, switching to CDP mode\n");
// switch to CDP
white_set_usb_power_mode(USB_POWER_CDP);
marker = tcnt;
marker = uptime;
}
}
// keep resetting the timer if it's enumerated
if (is_enumerated) {
marker = tcnt;
marker = uptime;
}
break;
case USB_POWER_CDP:
// been CLICKS clicks since we switched to CDP
if ((tcnt-marker) >= CLICKS) {
if ((uptime - marker) >= CLICKS) {
// measure current draw, if positive and no enumeration, switch to DCP
if (!is_enumerated && (current < CURRENT_THRESHOLD)) {
puts("USBP: no enumeration with current draw, switching to DCP mode\n");
white_set_usb_power_mode(USB_POWER_DCP);
marker = tcnt;
marker = uptime;
}
}
// keep resetting the timer if there's no current draw in CDP
if (current >= CURRENT_THRESHOLD) {
marker = tcnt;
marker = uptime;
}
break;
case USB_POWER_DCP:
// been at least CLICKS clicks since we switched to DCP
if ((tcnt-marker) >= CLICKS) {
if ((uptime - marker) >= CLICKS) {
// if no current draw, switch back to CDP
if (current >= CURRENT_THRESHOLD) {
puts("USBP: no current draw, switching back to CDP mode\n");
white_set_usb_power_mode(USB_POWER_CDP);
marker = tcnt;
marker = uptime;
}
}
// keep resetting the timer if there's current draw in DCP
if (current < CURRENT_THRESHOLD) {
marker = tcnt;
marker = uptime;
}
break;
default:
puts("USB power mode invalid\n"); // set_usb_power_mode prevents assigning invalid values
break;
}
#else
UNUSED(tcnt);
UNUSED(uptime);
#endif
}

Expand Down
12 changes: 6 additions & 6 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ void set_safety_mode(uint16_t mode, int16_t param) {

int get_health_pkt(void *dat) {
struct __attribute__((packed)) {
uint32_t uptime_pkt;
uint32_t voltage_pkt;
uint32_t current_pkt;
uint32_t can_send_errs_pkt;
Expand All @@ -144,6 +145,7 @@ int get_health_pkt(void *dat) {
uint8_t power_save_enabled_pkt;
} *health = dat;

health->uptime_pkt = uptime_cnt;
health->voltage_pkt = adc_get_voltage();
health->current_pkt = current_board->read_current();

Expand Down Expand Up @@ -630,8 +632,6 @@ void __attribute__ ((noinline)) enable_fpu(void) {
SCB->CPACR |= ((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
}

uint64_t tcnt = 0;

// go into SILENT when the EON does not send a heartbeat for this amount of seconds.
#define EON_HEARTBEAT_IGNITION_CNT_ON 5U
#define EON_HEARTBEAT_IGNITION_CNT_OFF 2U
Expand All @@ -642,12 +642,12 @@ void TIM1_BRK_TIM9_IRQHandler(void) {
if (TIM9->SR != 0) {
can_live = pending_can_live;

current_board->usb_power_mode_tick(tcnt);
current_board->usb_power_mode_tick(uptime_cnt);

//puth(usart1_dma); puts(" "); puth(DMA2_Stream5->M0AR); puts(" "); puth(DMA2_Stream5->NDTR); puts("\n");

// reset this every 16th pass
if ((tcnt & 0xFU) == 0U) {
if ((uptime_cnt & 0xFU) == 0U) {
pending_can_live = 0;
}
#ifdef DEBUG
Expand All @@ -666,7 +666,7 @@ void TIM1_BRK_TIM9_IRQHandler(void) {

// turn off the blue LED, turned on by CAN
// unless we are in power saving mode
current_board->set_led(LED_BLUE, (tcnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED));
current_board->set_led(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED));

// increase heartbeat counter and cap it at the uint32 limit
if (heartbeat_counter < __UINT32_MAX__) {
Expand Down Expand Up @@ -695,7 +695,7 @@ void TIM1_BRK_TIM9_IRQHandler(void) {
#endif

// on to the next one
tcnt += 1U;
uptime_cnt += 1U;
}
TIM9->SR = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion board/main_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ void can_set_obd(uint8_t harness_orientation, bool obd);
uint8_t hw_type = 0;
const board *current_board;
bool is_enumerated = 0;
uint32_t heartbeat_counter = 0;
uint32_t heartbeat_counter = 0;
uint32_t uptime_cnt = 0;
34 changes: 19 additions & 15 deletions python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,23 @@ def call_control_api(self, msg):

def health(self):
dat = self._handle.controlRead(Panda.REQUEST_IN, 0xd2, 0, 0, 28)
a = struct.unpack("IIIIIBBBBBBBBB", dat)
a = struct.unpack("IIIIIIBBBBBBBBB", dat)
return {
"voltage": a[0],
"current": a[1],
"can_send_errs": a[2],
"can_fwd_errs": a[3],
"gmlan_send_errs": a[4],
"ignition_line": a[5],
"ignition_can": a[6],
"controls_allowed": a[7],
"gas_interceptor_detected": a[8],
"car_harness_status": a[9],
"usb_power_mode": a[10],
"safety_mode": a[11],
"fault_status": a[12],
"power_save_enabled": a[13]
"uptime": a[0],
"voltage": a[1],
"current": a[2],
"can_send_errs": a[3],
"can_fwd_errs": a[4],
"gmlan_send_errs": a[5],
"ignition_line": a[6],
"ignition_can": a[7],
"controls_allowed": a[8],
"gas_interceptor_detected": a[9],
"car_harness_status": a[10],
"usb_power_mode": a[11],
"safety_mode": a[12],
"fault_status": a[13],
"power_save_enabled": a[14]
}

# ******************* control *******************
Expand Down Expand Up @@ -409,6 +410,9 @@ def get_secret(self):
def set_usb_power(self, on):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe6, int(on), 0, b'')

def set_power_save(self, power_save_enabled=0):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xe7, int(power_save_enabled), 0, b'')

def set_esp_power(self, on):
self._handle.controlWrite(Panda.REQUEST_OUT, 0xd9, int(on), 0, b'')

Expand Down
1 change: 1 addition & 0 deletions tests/automated/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def wrapper(panda_serials=None, **kwargs):
panda.set_can_speed_kbps(bus, speed)
clear_can_buffers(panda)
_thread.start_new_thread(heartbeat_thread, (panda,))
panda.set_power_save(False)

# Run test function
ret = fn(*pandas, **kwargs)
Expand Down

0 comments on commit f458d67

Please sign in to comment.