trying to improve stabiltiy

This commit is contained in:
2024-05-22 02:39:18 +02:00
parent e892a21f97
commit 2b12ea9602
7 changed files with 126 additions and 39 deletions

View File

@@ -10,6 +10,7 @@
#include "version_info.h"
#include "ulog.h"
#include "BSP_POWER.h"
#include "BSP_GPIO.h"
// Define thread flags
#define FLAG_FDCAN_RX_FIFO0 (1<<0)
#define FLAG_FDCAN_RX_FIFO1 (1<<1)
@@ -32,7 +33,7 @@ const osThreadAttr_t CanDataTask_attr = {
// Memory for the task
StaticTask_t CarCanTask_cb;
uint32_t CarCanTask_stk[256];
uint32_t CarCanTask_stk[512];
// Attributes for the task
osThreadId_t CarCanTask_id;
const osThreadAttr_t CarCanTask_attr = {
@@ -129,7 +130,8 @@ void CanDataTask_func(void *argument) {
}
}
static uint64_t last_car_message_time = UINT64_MAX;
static uint64_t last_unlock_message_time = UINT64_MAX;
static uint64_t last_car_message_time = 0;
// convert byte to 2 hex characters
void byteToHex(uint8_t byte, char * hex) {
@@ -249,23 +251,33 @@ void CarCanTask_func(void *argument) {
if (HAL_FDCAN_GetRxMessage(&hfdcan2, FDCAN_RX_FIFO1, &RxHeader, RxData) != HAL_OK) {
Error_Handler();
} else {
char msg[17] = {0};
// do something with the can data
ULOG_DEBUG("Car LOCK MSG: %x, %d", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength));
if(RxHeader.Identifier == 0x391) {
if (RxData[1] == 4)
{
// car was unlocked
last_car_message_time = osKernelGetTickCount();
last_unlock_message_time = osKernelGetTickCount();
byteToHex(RxData[0], &msg[0]);
byteToHex(RxData[1], &msg[2]);
byteToHex(RxData[2], &msg[4]);
}
if (RxData[1] == 80)
if (RxData[1] ==0x80)
{
// car was locked
BSP_POWER_EnterStandby();
if (!BSP_GPIO_K15isSet()) {
NVIC_SystemReset();
}
byteToHex(RxData[0], &msg[0]);
byteToHex(RxData[1], &msg[2]);
byteToHex(RxData[2], &msg[4]);
}
}
@@ -274,17 +286,26 @@ void CarCanTask_func(void *argument) {
// send the unlock message to the car
if ((RxData[0] & 0x0F) == 0x01) {
// car was unlocked
last_car_message_time = osKernelGetTickCount();
byteToHex(RxData[0], &msg[0]);
last_unlock_message_time = osKernelGetTickCount();
}
if ((RxData[0] & 0x0F) == 0x02) {
// car was locked
BSP_POWER_EnterStandby();
if (!BSP_GPIO_K15isSet()) {
NVIC_SystemReset();
}
byteToHex(RxData[0], &msg[0]);
}
}
ULOG_DEBUG("Car LOCK MSG: %x, %d %s", RxHeader.Identifier, CLS_BSP_DLC_ToBytes(RxHeader.DataLength), msg );
}
}
}
@@ -297,7 +318,17 @@ void CarCanTask_func(void *argument) {
* @return false if no car can message has been received
*/
bool CanDataTask_gotCarCanMessage() {
return last_car_message_time != UINT64_MAX;
return last_unlock_message_time != UINT64_MAX;
}
bool CanDataTask_CarCanActive() {
if (last_car_message_time == 0) {
return false;
}
return osKernelGetTickCount() - last_car_message_time < 1000;
}
void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs) {
@@ -307,6 +338,7 @@ void HAL_FDCAN_RxFifo0Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo0ITs)
}
if(hfdcan == &hfdcan2) {
last_car_message_time = osKernelGetTickCount();
osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO0);
}
@@ -319,6 +351,7 @@ void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
}
if(hfdcan == &hfdcan2) {
last_car_message_time = osKernelGetTickCount();
osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO1);
}
}