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
bool CanDataTask_CarCanActive();
// Start the sleep counter check
void BSP_POWER_Init() {
@@ -52,7 +54,11 @@ void BSP_POWER_WakeUp() {
*/
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
__disable_irq();

View File

@@ -4,7 +4,7 @@
#include "stdio.h"
#include "string.h"
uint8_t block_buffer[2048] = {0};
uint8_t block_buffer[512] = {0};
size_t block_buffer_index = 0;
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);
}
#include "cmsis_os2.h"
// Define the mutex
osMutexId_t logger_mutex;
// init the logger buffer and the file
// filename is generated based on the log number
void BSP_SDLogger_Init(int log_number) {
@@ -34,40 +39,53 @@ void BSP_SDLogger_Init(int log_number) {
// close the file
f_close(&file);
// Create the mutex
logger_mutex = osMutexNew(NULL);
}
// this fuctions assumes you have the mutex
void BSP_SDLogger_Flush_private() {
// open the file
FIL file;
FRESULT res = f_open(&file, file_name, FA_OPEN_APPEND | FA_WRITE );
if (res != FR_OK) {
ULOG_ERROR("Failed to open file %s", file_name);
}
// write the buffer to the file
UINT bytes_written;
res = f_write(&file, block_buffer, block_buffer_index, &bytes_written);
if (res != FR_OK) {
ULOG_ERROR("Failed to write to file %s", file_name);
}
// close the file
f_close(&file);
// reset the buffer index
block_buffer_index = 0;
}
void BSP_SDLogger_Flush() {
// open the file
FIL file;
FRESULT res = f_open(&file, file_name, FA_OPEN_APPEND | FA_WRITE );
if (res != FR_OK) {
ULOG_ERROR("Failed to open file %s", file_name);
}
// write the buffer to the file
UINT bytes_written;
res = f_write(&file, block_buffer, block_buffer_index, &bytes_written);
if (res != FR_OK) {
ULOG_ERROR("Failed to write to file %s", file_name);
}
// close the file
f_close(&file);
// reset the buffer index
block_buffer_index = 0;
// 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
// data is only written to the buffer!
// once the buffer is full, the data is written to the file
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 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);
block_buffer_index += remaining_size;
// write the buffer to the file
BSP_SDLogger_Flush();
BSP_SDLogger_Flush_private();
// write the remaining data to the buffer
memcpy(&block_buffer[block_buffer_index], &data[remaining_size], 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;
}
// Release the mutex
osMutexRelease(logger_mutex);
}

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);
}
}

View File

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

View File

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

View File

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

View File

@@ -99,6 +99,21 @@ void HardFault_Handler(void)
while (1)
{
/* 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 */
}
}
@@ -114,6 +129,18 @@ void MemManage_Handler(void)
while (1)
{
/* 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 */
}
}