285 lines
7.3 KiB
C
285 lines
7.3 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* File Name : freertos.c
|
|
* Description : Code for freertos applications
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2024 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
#include "main.h"
|
|
#include "cmsis_os.h"
|
|
|
|
/* Private includes ----------------------------------------------------------*/
|
|
/* USER CODE BEGIN Includes */
|
|
#include "UsbDataHandler.h"
|
|
#include "CanDataTask.h"
|
|
#include "CLS.h"
|
|
#include "fatfs.h"
|
|
#include "ulog.h"
|
|
#include "stdio.h"
|
|
#include "tim.h"
|
|
#include "BSP_EE24.h"
|
|
#include "BSP_INA.h"
|
|
#include "BSP_POWER.h"
|
|
#include "LightTask.h"
|
|
#include "LightState.h"
|
|
#include "BSP_GPIO.h"
|
|
#include "BSP_ADC.h"
|
|
#include "BSP_SDLogger.h"
|
|
#include "Vehicle.h"
|
|
|
|
/* USER CODE END Includes */
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
/* USER CODE BEGIN PTD */
|
|
|
|
/* USER CODE END PTD */
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PD */
|
|
|
|
/* USER CODE END PD */
|
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
/* USER CODE BEGIN PM */
|
|
|
|
/* USER CODE END PM */
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
/* USER CODE BEGIN Variables */
|
|
|
|
/* USER CODE END Variables */
|
|
/* Definitions for defaultTask */
|
|
osThreadId_t defaultTaskHandle;
|
|
const osThreadAttr_t defaultTask_attributes = {
|
|
.name = "defaultTask",
|
|
.stack_size = 512 * 4,
|
|
.priority = (osPriority_t) osPriorityNormal,
|
|
};
|
|
|
|
osThreadId_t waitForStartConfirmHandle;
|
|
const osThreadAttr_t waitForStartConfirm_attributes = {
|
|
.name = "waitForStartConfirm",
|
|
.stack_size = 512 * 4,
|
|
.priority = (osPriority_t) osPriorityNormal,
|
|
};
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* USER CODE BEGIN FunctionPrototypes */
|
|
|
|
/* USER CODE END FunctionPrototypes */
|
|
|
|
void StartDefaultTask(void *argument);
|
|
void WaitForStartConfirm_Task(void *argument);
|
|
|
|
extern void MX_USB_DEVICE_Init(void);
|
|
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
|
|
|
|
/* Hook prototypes */
|
|
void configureTimerForRunTimeStats(void);
|
|
unsigned long getRunTimeCounterValue(void);
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
|
|
__weak void configureTimerForRunTimeStats(void)
|
|
{
|
|
HAL_TIM_Base_Start(&htim2);
|
|
}
|
|
|
|
__weak unsigned long getRunTimeCounterValue(void)
|
|
{
|
|
return __HAL_TIM_GetCounter(&htim2);
|
|
}
|
|
/* USER CODE END 1 */
|
|
|
|
/**
|
|
* @brief FreeRTOS initialization
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
void MX_FREERTOS_Init(void) {
|
|
/* USER CODE BEGIN Init */
|
|
/* USER CODE END Init */
|
|
|
|
/* USER CODE BEGIN RTOS_MUTEX */
|
|
/* add mutexes, ... */
|
|
/* USER CODE END RTOS_MUTEX */
|
|
|
|
/* USER CODE BEGIN RTOS_SEMAPHORES */
|
|
/* add semaphores, ... */
|
|
/* USER CODE END RTOS_SEMAPHORES */
|
|
|
|
/* USER CODE BEGIN RTOS_TIMERS */
|
|
/* start timers, add new ones, ... */
|
|
/* USER CODE END RTOS_TIMERS */
|
|
|
|
/* USER CODE BEGIN RTOS_QUEUES */
|
|
/* add queues, ... */
|
|
/* USER CODE END RTOS_QUEUES */
|
|
|
|
/* Create the thread(s) */
|
|
/* creation of defaultTask */
|
|
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
|
|
|
|
/* USER CODE BEGIN RTOS_THREADS */
|
|
|
|
waitForStartConfirmHandle = osThreadNew(WaitForStartConfirm_Task, NULL, &waitForStartConfirm_attributes);
|
|
|
|
ULOG_INFO("Setup UsbDataHandler");
|
|
UsbDataHandler_Start();
|
|
ULOG_INFO("Setup CanDataTask");
|
|
CanDataTask_start();
|
|
/* USER CODE END RTOS_THREADS */
|
|
|
|
/* USER CODE BEGIN RTOS_EVENTS */
|
|
/* add events, ... */
|
|
/* USER CODE END RTOS_EVENTS */
|
|
|
|
}
|
|
|
|
/* USER CODE BEGIN Header_StartDefaultTask */
|
|
void AddHeadlightMessages();
|
|
|
|
/**
|
|
* @brief Function implementing the defaultTask thread.
|
|
* @param argument: Not used
|
|
* @retval None
|
|
*/
|
|
|
|
#include "CLS_BSP.h"
|
|
#include "CLSAddress.h"
|
|
/* USER CODE END Header_StartDefaultTask */
|
|
void StartDefaultTask(void *argument)
|
|
{
|
|
/* init code for USB_DEVICE */
|
|
MX_USB_DEVICE_Init();
|
|
/* USER CODE BEGIN StartDefaultTask */
|
|
BSP_EE24_Init();
|
|
BSP_INA_Init();
|
|
|
|
retSD = f_mount(&SDFatFS,SDPath,0);
|
|
retSD = f_open(&SDFile, "counter.bin", FA_READ);
|
|
|
|
uint64_t counter = 0;
|
|
unsigned int size=0;
|
|
if(retSD == 0) {
|
|
retSD = f_read(&SDFile, &counter, sizeof(counter),&size);
|
|
}
|
|
retSD = f_close(&SDFile);
|
|
counter++;
|
|
retSD = f_open(&SDFile, "counter.bin", FA_CREATE_ALWAYS | FA_WRITE);
|
|
retSD = f_write(&SDFile,&counter,sizeof(counter),&size);
|
|
retSD = f_close(&SDFile);
|
|
|
|
{
|
|
char output[64];
|
|
snprintf(output,64,"Read SD Bootcount %lld" ,counter);
|
|
ULOG_INFO(output);
|
|
}
|
|
|
|
BSP_SDLogger_Init(counter);
|
|
AddHeadlightMessages();
|
|
|
|
char INA_LOG[72];
|
|
|
|
/* Infinite loop */
|
|
for(;;)
|
|
{
|
|
osDelay(1000);
|
|
|
|
uint16_t current = BSP_INA_Current()*10;
|
|
uint16_t voltage = BSP_INA_Voltage();
|
|
uint32_t power = (current * voltage)/1000; // milli watts
|
|
float power_W = (float)power/1000.0;
|
|
float voltage_V =(float) voltage /1000.0;
|
|
|
|
snprintf(INA_LOG, sizeof(INA_LOG),"Voltage[mV] %.2f Current[mA] %d P[W]: %.2f", voltage_V, current, power_W);
|
|
ULOG_INFO(INA_LOG);
|
|
|
|
|
|
|
|
// Read the battery voltage
|
|
float bus = BSP_ADC_ReadBusValue();
|
|
float dimm = BSP_ADC_ReadDimmerValue();
|
|
|
|
// Print the battery voltage and dimmer value
|
|
char output[64];
|
|
snprintf(output,64,"Bus voltage: %.2fV, Dimmer voltage: %.2fV", bus, dimm);
|
|
ULOG_INFO(output);
|
|
|
|
|
|
}
|
|
/* USER CODE END StartDefaultTask */
|
|
}
|
|
|
|
/* Private application code --------------------------------------------------*/
|
|
/* USER CODE BEGIN Application */
|
|
|
|
void WaitForStartConfirm_Task(void *argument) {
|
|
|
|
// wait for up to 1 s and check if either K15 is set or we got a Car CAN message
|
|
// once one of these is true, we can start the power systems.
|
|
// after waiting for 1s, the system should shutdown / go to standby mode
|
|
uint32_t tick = osKernelGetTickCount();
|
|
uint32_t delayTime = 50; // Set the initial delay time to 50ms
|
|
uint32_t maxDelayTime = 1000; // Set the maximum delay time to 1000ms
|
|
|
|
while(1) {
|
|
osDelayUntil(tick);
|
|
tick += delayTime;
|
|
if(BSP_GPIO_K15isSet() || Vehicle_gotUnlockMessage()) {
|
|
BSP_POWER_FullPowerMode();
|
|
ULOG_INFO("Power systems started");
|
|
osThreadExit();
|
|
|
|
while (1)
|
|
{
|
|
osDelay(1000);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
if(tick > maxDelayTime) {
|
|
while (1)
|
|
{
|
|
ULOG_INFO("System in standby mode");
|
|
BSP_POWER_EnterStandby();
|
|
NVIC_SystemReset();
|
|
}
|
|
osThreadExit();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// this is called from BSP_POWER_FullPowerMode
|
|
// this function should start tasks that depend on the power being on
|
|
void StartPowerTasks(void) {
|
|
|
|
ULOG_INFO("Setup CLS");
|
|
CLS_Init();
|
|
ULOG_INFO("Setup LightTask");
|
|
LightTask_start();
|
|
LightStateTask_start();
|
|
|
|
|
|
}
|
|
|
|
/* USER CODE END Application */
|
|
|