Send AMS Errors

This commit is contained in:
jazzpi
2022-06-24 04:38:00 +02:00
parent c8d6894e4b
commit 405b255936
6 changed files with 74 additions and 13 deletions

View File

@ -16,6 +16,8 @@
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_can.h"
#include <stdint.h>
static CAN_HandleTypeDef* handle_ams;
static CAN_HandleTypeDef* handle_car;
@ -127,7 +129,8 @@ void ams_can_send_heartbeat() {
}
}
void ams_can_send_error(AMS_ErrorCode error_code) {
void ams_can_send_error(AMS_ErrorCode error_code,
uint32_t transmission_timeout) {
static CAN_TxHeaderTypeDef header;
header.IDE = CAN_ID_STD;
header.DLC = 8;
@ -143,13 +146,15 @@ void ams_can_send_error(AMS_ErrorCode error_code) {
CAN_TX_MAILBOX0 | CAN_TX_MAILBOX1 | CAN_TX_MAILBOX2);
uint32_t mailbox;
HAL_CAN_AddTxMessage(handle_ams, &header, data, &mailbox);
ams_can_wait_for_free_mailboxes(handle_ams, 3, transmission_timeout);
}
HAL_StatusTypeDef ams_can_wait_for_free_mailbox(CAN_HandleTypeDef* handle,
uint32_t timeout) {
HAL_StatusTypeDef ams_can_wait_for_free_mailboxes(CAN_HandleTypeDef* handle,
int num_mailboxes,
uint32_t timeout) {
uint32_t end = HAL_GetTick() + timeout;
while (HAL_GetTick() < end) {
if (HAL_CAN_GetTxMailboxesFreeLevel(handle) > 0) {
if (HAL_CAN_GetTxMailboxesFreeLevel(handle) >= num_mailboxes) {
return HAL_OK;
}
}

View File

@ -16,6 +16,7 @@
#include <string.h>
uint16_t cell_voltages[N_CELLS];
uint16_t max_voltage, min_voltage;
BQ_Status bq_status;
BQ_Error_Description bq_error;
@ -82,11 +83,20 @@ void afe_measure() {
lastmeasurementtime = HAL_GetTick();
if (retval == BQ_COMM_OK) {
uint16_t max = 0;
uint16_t min = 0xFFFF;
for (int n = 0; n < N_CELLS; n++) {
cell_voltages[N_CELLS - 1 - n] =
(uint16_t)(cellvoltagebuffer[2 * n] << 8) +
(uint16_t)cellvoltagebuffer[2 * n + 1];
uint16_t v = cellvoltagebuffer[2 * n] << 8 | cellvoltagebuffer[2 * n + 1];
cell_voltages[N_CELLS - 1 - n] = v;
if (v > max) {
max = v;
}
if (v < min) {
min = v;
}
}
max_voltage = max;
min_voltage = min;
} else if (retval == BQ_COMM_ERR_HAL) {
bq_set_error_with_loc(BQ_COMM_ERR_HAL, BQ_ERROR_LOC_MEASURE);
} else if (retval == BQ_COMM_ERR_CRC) {

View File

@ -21,10 +21,12 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "AMS_CAN.h"
#include "BQ_Abstraction_Layer.h"
#include "EEPROM.h"
#include "FanControl.h"
#include "TMP144.h"
#include "common_defs.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_gpio.h"
@ -94,6 +96,29 @@ void update_status_leds() {
}
}
void check_error_conditions() {
if (bq_status != BQ_RDY) {
ams_can_send_error(AMS_ERROR_BQ, AMS_ERROR_TX_TIMEOUT);
}
tmp144_check_timeouts();
if (tmp144_bus_busbar.state == TMP144_ERROR ||
tmp144_bus_other.state == TMP144_ERROR) {
ams_can_send_error(AMS_ERROR_TMP144, AMS_ERROR_TX_TIMEOUT);
}
if (min_voltage < THRESH_UV) {
ams_can_send_error(AMS_ERROR_UV, AMS_ERROR_TX_TIMEOUT);
}
if (max_voltage > THRESH_OV) {
ams_can_send_error(AMS_ERROR_OV, AMS_ERROR_TX_TIMEOUT);
}
if (min_temperature < THRESH_UT) {
ams_can_send_error(AMS_ERROR_UT, AMS_ERROR_TX_TIMEOUT);
}
if (max_temperature > THRESH_OT) {
ams_can_send_error(AMS_ERROR_OT, AMS_ERROR_TX_TIMEOUT);
}
}
void delay_period() {
static uint32_t last_it = 0;
uint32_t now = HAL_GetTick();
@ -161,8 +186,9 @@ int main(void) {
/* USER CODE BEGIN 3 */
update_status_leds();
afe_measure();
tmp144_read_temps();
check_error_conditions();
delay_period();
fan_ctrl_set_power((HAL_GetTick() / 100) % 100);
}
/* USER CODE END 3 */
}