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

@@ -16,6 +16,8 @@
#define STAY_AWAKE_TIME 60 // seconds to stay awake without K15 #define STAY_AWAKE_TIME 60 // seconds to stay awake without K15
bool CanDataTask_CarCanActive();
// Start the sleep counter check // Start the sleep counter check
void BSP_POWER_Init() { void BSP_POWER_Init() {
@@ -52,7 +54,11 @@ void BSP_POWER_WakeUp() {
*/ */
void BSP_POWER_EnterStandby() { void BSP_POWER_EnterStandby() {
BSP_SDLogger_Flush(); // if there is still communication on the CAN bus, we should not go to standby
// intstead we should reset the system, because the CAN bus would wake us up again
if (CanDataTask_CarCanActive()) {
NVIC_SystemReset();
}
// stop the sytem interrupts // stop the sytem interrupts
__disable_irq(); __disable_irq();

View File

@@ -4,7 +4,7 @@
#include "stdio.h" #include "stdio.h"
#include "string.h" #include "string.h"
uint8_t block_buffer[2048] = {0}; uint8_t block_buffer[512] = {0};
size_t block_buffer_index = 0; size_t block_buffer_index = 0;
char file_name[20]; char file_name[20];
@@ -15,6 +15,11 @@ void BSP_SDLogger_ULOG(ulog_level_t level, char *msg) {
BSP_SDLogger_Write(ulog_buffer, send_length); BSP_SDLogger_Write(ulog_buffer, send_length);
} }
#include "cmsis_os2.h"
// Define the mutex
osMutexId_t logger_mutex;
// init the logger buffer and the file // init the logger buffer and the file
// filename is generated based on the log number // filename is generated based on the log number
void BSP_SDLogger_Init(int log_number) { void BSP_SDLogger_Init(int log_number) {
@@ -34,11 +39,12 @@ void BSP_SDLogger_Init(int log_number) {
// close the file // close the file
f_close(&file); f_close(&file);
// Create the mutex
logger_mutex = osMutexNew(NULL);
} }
// this fuctions assumes you have the mutex
void BSP_SDLogger_Flush() { void BSP_SDLogger_Flush_private() {
// open the file // open the file
FIL file; FIL file;
FRESULT res = f_open(&file, file_name, FA_OPEN_APPEND | FA_WRITE ); FRESULT res = f_open(&file, file_name, FA_OPEN_APPEND | FA_WRITE );
@@ -59,14 +65,26 @@ void BSP_SDLogger_Flush() {
// reset the buffer index // reset the buffer index
block_buffer_index = 0; block_buffer_index = 0;
}
void BSP_SDLogger_Flush() {
// Acquire the mutex
osMutexAcquire(logger_mutex, osWaitForever);
// flush the buffer
BSP_SDLogger_Flush_private();
// Release the mutex
osMutexRelease(logger_mutex);
} }
// write data to the logger // write data to the logger
// data is only written to the buffer! // data is only written to the buffer!
// once the buffer is full, the data is written to the file // once the buffer is full, the data is written to the file
void BSP_SDLogger_Write(char *data, size_t length) { void BSP_SDLogger_Write(char *data, size_t length) {
// Acquire the mutex
osMutexAcquire(logger_mutex, osWaitForever);
size_t buffer_size = sizeof(block_buffer); size_t buffer_size = sizeof(block_buffer);
size_t remaining_size = buffer_size - block_buffer_index; size_t remaining_size = buffer_size - block_buffer_index;
@@ -77,7 +95,7 @@ void BSP_SDLogger_Write(char *data, size_t length) {
memcpy(&block_buffer[block_buffer_index], data, remaining_size); memcpy(&block_buffer[block_buffer_index], data, remaining_size);
block_buffer_index += remaining_size; block_buffer_index += remaining_size;
// write the buffer to the file // write the buffer to the file
BSP_SDLogger_Flush(); BSP_SDLogger_Flush_private();
// write the remaining data to the buffer // write the remaining data to the buffer
memcpy(&block_buffer[block_buffer_index], &data[remaining_size], length - remaining_size); memcpy(&block_buffer[block_buffer_index], &data[remaining_size], length - remaining_size);
block_buffer_index = length - remaining_size; block_buffer_index = length - remaining_size;
@@ -87,5 +105,6 @@ void BSP_SDLogger_Write(char *data, size_t length) {
block_buffer_index += length; block_buffer_index += length;
} }
// Release the mutex
osMutexRelease(logger_mutex);
} }

View File

@@ -10,6 +10,7 @@
#include "version_info.h" #include "version_info.h"
#include "ulog.h" #include "ulog.h"
#include "BSP_POWER.h" #include "BSP_POWER.h"
#include "BSP_GPIO.h"
// Define thread flags // Define thread flags
#define FLAG_FDCAN_RX_FIFO0 (1<<0) #define FLAG_FDCAN_RX_FIFO0 (1<<0)
#define FLAG_FDCAN_RX_FIFO1 (1<<1) #define FLAG_FDCAN_RX_FIFO1 (1<<1)
@@ -32,7 +33,7 @@ const osThreadAttr_t CanDataTask_attr = {
// Memory for the task // Memory for the task
StaticTask_t CarCanTask_cb; StaticTask_t CarCanTask_cb;
uint32_t CarCanTask_stk[256]; uint32_t CarCanTask_stk[512];
// Attributes for the task // Attributes for the task
osThreadId_t CarCanTask_id; osThreadId_t CarCanTask_id;
const osThreadAttr_t CarCanTask_attr = { 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 // convert byte to 2 hex characters
void byteToHex(uint8_t byte, char * hex) { 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) { if (HAL_FDCAN_GetRxMessage(&hfdcan2, FDCAN_RX_FIFO1, &RxHeader, RxData) != HAL_OK) {
Error_Handler(); Error_Handler();
} else { } else {
char msg[17] = {0};
// do something with the can data // 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(RxHeader.Identifier == 0x391) {
if (RxData[1] == 4) if (RxData[1] == 4)
{ {
// car was unlocked // 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 // 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 // send the unlock message to the car
if ((RxData[0] & 0x0F) == 0x01) { if ((RxData[0] & 0x0F) == 0x01) {
// car was unlocked // car was unlocked
last_car_message_time = osKernelGetTickCount(); byteToHex(RxData[0], &msg[0]);
last_unlock_message_time = osKernelGetTickCount();
} }
if ((RxData[0] & 0x0F) == 0x02) { if ((RxData[0] & 0x0F) == 0x02) {
// car was locked // 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 * @return false if no car can message has been received
*/ */
bool CanDataTask_gotCarCanMessage() { 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) { 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) { if(hfdcan == &hfdcan2) {
last_car_message_time = osKernelGetTickCount();
osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO0); osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO0);
} }
@@ -319,6 +351,7 @@ void HAL_FDCAN_RxFifo1Callback(FDCAN_HandleTypeDef *hfdcan, uint32_t RxFifo1ITs)
} }
if(hfdcan == &hfdcan2) { if(hfdcan == &hfdcan2) {
last_car_message_time = osKernelGetTickCount();
osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO1); osThreadFlagsSet(CarCanTask_id, FLAG_FDCAN_RX_FIFO1);
} }
} }

View File

@@ -6,3 +6,5 @@ void CanDataTask_start();
bool CanDataTask_gotCarCanMessage(); bool CanDataTask_gotCarCanMessage();
bool CanDataTask_CarCanActive();

View File

@@ -10,7 +10,7 @@
osThreadAttr_t task_attr = { osThreadAttr_t task_attr = {
.name = "LightStateTask", .name = "LightStateTask",
.priority = osPriorityNormal, .priority = osPriorityNormal,
.stack_size = 1024 .stack_size = 2048
}; };
// Function prototype for the task function // Function prototype for the task function

View File

@@ -72,7 +72,7 @@ const osThreadAttr_t defaultTask_attributes = {
osThreadId_t waitForStartConfirmHandle; osThreadId_t waitForStartConfirmHandle;
const osThreadAttr_t waitForStartConfirm_attributes = { const osThreadAttr_t waitForStartConfirm_attributes = {
.name = "waitForStartConfirm", .name = "waitForStartConfirm",
.stack_size = 256 * 4, .stack_size = 512 * 4,
.priority = (osPriority_t) osPriorityNormal, .priority = (osPriority_t) osPriorityNormal,
}; };

View File

@@ -99,6 +99,21 @@ void HardFault_Handler(void)
while (1) while (1)
{ {
/* USER CODE BEGIN W1_HardFault_IRQn 0 */ /* USER CODE BEGIN W1_HardFault_IRQn 0 */
HAL_GPIO_TogglePin(DIO12_L2_GPIO_Port, DIO12_L2_Pin);
for (size_t i = 0; i < 10000000; i++)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
/* USER CODE END W1_HardFault_IRQn 0 */ /* USER CODE END W1_HardFault_IRQn 0 */
} }
} }
@@ -114,6 +129,18 @@ void MemManage_Handler(void)
while (1) while (1)
{ {
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
HAL_GPIO_TogglePin(DIO12_L2_GPIO_Port, DIO12_L2_Pin);
for (size_t i = 0; i < 30000000; i++)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
/* USER CODE END W1_MemoryManagement_IRQn 0 */ /* USER CODE END W1_MemoryManagement_IRQn 0 */
} }
} }