Motion_EC_Stm32_archived/Core/Src/freertos.c

605 lines
18 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* 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 "iwdg.h"
#include "gpio.h"
#include "usart.h"
#include "stdio.h"
#include "navikit.h"
#include "coulomb.h"
#include "stdbool.h"
#include "adc.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",
.priority = (osPriority_t) osPriorityNormal,
.stack_size = 128 * 4
};
/* Definitions for LedBlinkTask */
osThreadId_t LedBlinkTaskHandle;
const osThreadAttr_t LedBlinkTask_attributes = {
.name = "LedBlinkTask",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for IWDGRefreshTask */
osThreadId_t IWDGRefreshTaskHandle;
const osThreadAttr_t IWDGRefreshTask_attributes = {
.name = "IWDGRefreshTask",
.priority = (osPriority_t) osPriorityHigh,
.stack_size = 128 * 4
};
/* Definitions for EventDetect */
osThreadId_t EventDetectHandle;
const osThreadAttr_t EventDetect_attributes = {
.name = "EventDetect",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for CoulombRead */
osThreadId_t CoulombReadHandle;
const osThreadAttr_t CoulombRead_attributes = {
.name = "CoulombRead",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for StateSwitchTask */
osThreadId_t StateSwitchTaskHandle;
const osThreadAttr_t StateSwitchTask_attributes = {
.name = "StateSwitchTask",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for PowerMonitTask */
osThreadId_t PowerMonitTaskHandle;
const osThreadAttr_t PowerMonitTask_attributes = {
.name = "PowerMonitTask",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for uartQueueTask */
osThreadId_t uartQueueTaskHandle;
const osThreadAttr_t uartQueueTask_attributes = {
.name = "uartQueueTask",
.priority = (osPriority_t) osPriorityLow,
.stack_size = 128 * 4
};
/* Definitions for uartQueue */
osMessageQueueId_t uartQueueHandle;
const osMessageQueueAttr_t uartQueue_attributes = {
.name = "uartQueue"
};
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
void StartLedBlinkTask(void *argument);
void StartIWDGRefreshTask(void *argument);
void StartEventDetect(void *argument);
void StartCoulombRead(void *argument);
void StartStateSwitchTask(void *argument);
void StartPowerMonitTask(void *argument);
void StartUartQueueTask(void *argument);
extern void MX_USB_DEVICE_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.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 */
/* Create the queue(s) */
/* creation of uartQueue */
uartQueueHandle = osMessageQueueNew (1024, sizeof(uint8_t), &uartQueue_attributes);
/* 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);
/* creation of LedBlinkTask */
LedBlinkTaskHandle = osThreadNew(StartLedBlinkTask, NULL, &LedBlinkTask_attributes);
/* creation of IWDGRefreshTask */
IWDGRefreshTaskHandle = osThreadNew(StartIWDGRefreshTask, NULL, &IWDGRefreshTask_attributes);
/* creation of EventDetect */
EventDetectHandle = osThreadNew(StartEventDetect, NULL, &EventDetect_attributes);
/* creation of CoulombRead */
CoulombReadHandle = osThreadNew(StartCoulombRead, NULL, &CoulombRead_attributes);
/* creation of StateSwitchTask */
StateSwitchTaskHandle = osThreadNew(StartStateSwitchTask, NULL, &StateSwitchTask_attributes);
/* creation of PowerMonitTask */
PowerMonitTaskHandle = osThreadNew(StartPowerMonitTask, NULL, &PowerMonitTask_attributes);
/* creation of uartQueueTask */
uartQueueTaskHandle = osThreadNew(StartUartQueueTask, NULL, &uartQueueTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
}
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
/* init code for USB_DEVICE */
MX_USB_DEVICE_Init();
/* USER CODE BEGIN StartDefaultTask */
HAL_GPIO_WritePin(USB2_FS_ENUM_CTL_GPIO_Port,USB2_FS_ENUM_CTL_Pin, GPIO_PIN_SET);
Beep(50);
printf("\nCore initial successfully\n");
printf("---------------------------------------------\n");
printf("Copyright (c) Powered by www.autolabor.com.cn\n");
osDelay(100);
printf("BIOS SW Version: V0.9, build: %s, %s\n",__DATE__ ,__TIME__ );
printf("HAL Version: %lu \n", HAL_GetHalVersion());
printf("Revision ID: %lu \n", HAL_GetREVID());
printf("Device ID: %lu \n", HAL_GetDEVID());
osDelay(100);
printf("Chip UID: %lu%lu%lu \n", HAL_GetUIDw0(),HAL_GetUIDw1(),HAL_GetUIDw2);
printf("----------------------------------------------\n");
/* Infinite loop */
for(;;)
{
osDelay(100);
// printf("Time:[%f s]; OUT_24:[%f V]; OUT_5:[%f V]; OUT_12:[%f V]; BKP_BAT:[%f V]; MAIN_PWR:[%f V]\n",(float)(osKernelGetTickCount()/1000.0),NaviKit.pmb.rails.out_24v,NaviKit.pmb.rails.out_5v,NaviKit.pmb.rails.out_12v,NaviKit.pmb.rails.bkp_bat,NaviKit.pmb.rails.main_pwr);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_StartLedBlinkTask */
/**
* @brief Function implementing the LedBlinkTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartLedBlinkTask */
void StartLedBlinkTask(void *argument)
{
/* USER CODE BEGIN StartLedBlinkTask */
/* Infinite loop */
for(;;)
{
switch(NaviKit.sys.sta){
case running:
{
if(HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin))
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);//turn on power led
if(HAL_GPIO_ReadPin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin) == GPIO_PIN_SET)//sys run led is off
{
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on sys run led
osDelay(50);
}
else
{
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led
osDelay(300);
}
}break;
case shutdown:
{
if(!HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin))
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);//turn off power led
if(HAL_GPIO_ReadPin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin) == GPIO_PIN_SET)//sys run led is off
{
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on sys run led
osDelay(20);
}
else
{
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led
osDelay(2000);
}
}break;
case sleep:
{//sleep mode
for(uint16_t i=0;i<25;i++)
{//20 light level
for(uint16_t j=0;j<3;j++)
{//the time length of every light level
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);
osDelay(i);
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);
osDelay(25-i);
}
}
for(uint16_t i=0;i<25;i++)
{
for(uint16_t j=0;j<3;j++)
{
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);
osDelay(25-i);
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);
osDelay(i);
}
}
}break;
case dfu:
{
if(HAL_GPIO_ReadPin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin) == GPIO_PIN_SET)//power led is off
{
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_RESET);//turn on power led
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_RESET);//turn on run led
osDelay(300);
}
else
{
HAL_GPIO_WritePin(SYS_POWER_LED_CTL_GPIO_Port,SYS_POWER_LED_CTL_Pin,GPIO_PIN_SET);//turn off sys run led
HAL_GPIO_WritePin(SYS_RUN_LED_CTL_GPIO_Port,SYS_RUN_LED_CTL_Pin,GPIO_PIN_SET);//turn off run led
osDelay(300);
}
}
}
}
/* USER CODE END StartLedBlinkTask */
}
/* USER CODE BEGIN Header_StartIWDGRefreshTask */
/**
* @brief Function implementing the IWDGRefreshTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartIWDGRefreshTask */
void StartIWDGRefreshTask(void *argument)
{
/* USER CODE BEGIN StartIWDGRefreshTask */
/* Infinite loop */
for(;;)
{
HAL_IWDG_Refresh(&hiwdg);
// printf("2\n");
osDelay(1000);
}
/* USER CODE END StartIWDGRefreshTask */
}
/* USER CODE BEGIN Header_StartEventDetect */
/**
* @brief Function implementing the EventDetect thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartEventDetect */
void StartEventDetect(void *argument)
{
/* USER CODE BEGIN StartEventDetect */
osDelay(100);
if(HAL_GPIO_ReadPin(SYS_CUSTOM_BTN_GPIO_Port,SYS_CUSTOM_BTN_Pin) == GPIO_PIN_RESET)
{
printf("Custom button has been pushed. \n");
NaviKit.sys.next_sta = isp;
}
/* Infinite loop */
for(;;)
{
if(NaviKit.sys.power_btn == true)
{//power btn has been pushed
uint8_t count =0;
while(NaviKit.sys.power_btn && count<=10){
osDelay(100);
count ++;
}
if(count >10)
{//power btn has been pushed more than 1000 ms
// while(NaviKit.sys.power_btn == true);//wait to release button
switch(NaviKit.sys.sta){
case running:{//system is running now, user request to shutdown
NaviKit.sys.next_sta = shutdown;
}break;
case shutdown:{//system is shutdown now , user request to power on
NaviKit.sys.next_sta = running;
}break;
case dfu:{
NaviKit.sys.next_sta = shutdown;
}break;
case sleep:{
NaviKit.sys.next_sta = running;
}break;
}
}
else
{
//sleep mode is unused
// if(NaviKit.sys.sta == runing)
// NaviKit.sys.sta = sleep;
// else if(NaviKit.sys.sta == sleep)
// NaviKit.sys.sta = runing;
}
}
if(NaviKit.sys.custom_btn == true )
{//custom button has been pushed
uint8_t count =0;
while(NaviKit.sys.custom_btn && count<=20){
osDelay(100);
count ++;
}
if(count > 20 )
{//custom button has been pushed over 1000 ms
if((NaviKit.sys.power_btn == true) && (NaviKit.sys.sta == shutdown))//into dfu state
NaviKit.sys.next_sta = dfu;
else//TODO: force restart
NaviKit.sys.next_sta = shutdown;
// if(NaviKit.sys.sta == runing)
// NaviKit.sys.next_sta = shutdown;
// else if(NaviKit.sys.sta == shutdown)
// NaviKit.sys.next_sta = runing;
}
}
osDelay(10);
}
/* USER CODE END StartEventDetect */
}
/* USER CODE BEGIN Header_StartCoulombRead */
/**
* @brief Function implementing the CoulombRead thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartCoulombRead */
void StartCoulombRead(void *argument)
{
/* USER CODE BEGIN StartCoulombRead */
//写寄存器方法
coulomb_write_config_load();
coulomb_write_config_actual_to_raw();
coulomb_write_config();
//读寄存器方法
coulomb_read_status_and_config();
coulomb_read_status_raw_to_actual();
coulomb_read_config_raw_to_actual();
/* Infinite loop */
for(;;)
{
coulomb_read_status_and_config();
coulomb_read_status_raw_to_actual();
if(NaviKit.sys.sta == running)
osDelay(500);
else
osDelay(5000);
}
/* USER CODE END StartCoulombRead */
}
/* USER CODE BEGIN Header_StartStateSwitchTask */
/**
* @brief Function implementing the StateSwitchTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartStateSwitchTask */
void StartStateSwitchTask(void *argument)
{
/* USER CODE BEGIN StartStateSwitchTask */
/* Infinite loop */
for(;;)
{
osDelay(10);
if(NaviKit.sys.next_sta != NaviKit.sys.sta)
{
switch(NaviKit.sys.next_sta)
{
case shutdown:
{//only from running state
if((NaviKit.sys.sta == running))// && (NaviKit.som.shutdown_req == true)
enter_shutdown_state();
}break;
case running:
{//from sleep and shutdown state
enter_runing_state();
}break;
case sleep:
{//only form running state
if(NaviKit.sys.sta == running)
enter_sleep_state();
}break;
case dfu:
{
enter_dfu_state();
}break;
case isp:
{
enter_isp_state();
}break;
}
NaviKit.sys.sta = NaviKit.sys.next_sta;
}
}
/* USER CODE END StartStateSwitchTask */
}
/* USER CODE BEGIN Header_StartPowerMonitTask */
/**
* @brief Function: Monit som power status and PMB status
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartPowerMonitTask */
void StartPowerMonitTask(void *argument)
{
/* USER CODE BEGIN StartPowerMonitTask */
HAL_ADCEx_Calibration_Start(&hadc1);
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&(NaviKit.pmb.rails.adc), ADC_CH_COUNT);
/* Infinite loop */
for(;;)
{
osDelay(1000);
// printf("out_24v: %f \n",NaviKit.pmb.out_24v_div16);
// printf("out_5v: %f \n",NaviKit.pmb.out_5v_div8);
// printf("out_12v: %f \n",NaviKit.pmb.out_12v_div8);
// printf("bkp_bat: %f \n",NaviKit.pmb.bkp_bat_div8);
// printf("main_pwr: %f \n",NaviKit.pmb.main_pwr_div16);
}
/* USER CODE END StartPowerMonitTask */
}
/* USER CODE BEGIN Header_StartUartQueueTask */
/**
* @brief Function implementing the uartQueueTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartUartQueueTask */
void StartUartQueueTask(void *argument)
{
/* USER CODE BEGIN StartUartQueueTask */
uint8_t count,temp[256];
// osMessageQueuePut(uartQueueHandle,"t",0,100);
// osMessageQueuePut(uartQueueHandle,"e",0,100);
// osMessageQueuePut(uartQueueHandle,"s",0,100);
// osMessageQueuePut(uartQueueHandle,"t",0,100);
/* Infinite loop */
for(;;)
{
osDelay(1);
count = osMessageQueueGetCount(uartQueueHandle);
if(count){
for(uint32_t i=0;i<count;i++){
osMessageQueueGet(uartQueueHandle,&temp[i],0,10);
uint32_t space = osMessageQueueGetSpace(uartQueueHandle);
}
// taskENTER_CRITICAL();
while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX){}
HAL_UART_Transmit(&huart1,(uint8_t *)&temp,count,0xffff);
// taskEXIT_CRITICAL();
}
}
/* USER CODE END StartUartQueueTask */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
//printf redefine
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
// while(HAL_UART_GetState(&huart1) == HAL_UART_STATE_BUSY_TX){}
// HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xffff);
//queue space check before enqueue
uint32_t space = osMessageQueueGetSpace(uartQueueHandle);
if(space){
osMessageQueuePut(uartQueueHandle,(uint8_t*)&ch,0,10);
space = osMessageQueueGetSpace(uartQueueHandle);
}
else{
// uint8_t front[1];//trash
// osMessageQueueGet(uartQueueHandle,front,0,10);
// osMessageQueuePut(uartQueueHandle,(uint8_t *)&ch,0,100);
}
return ch;
}
/* USER CODE END Application */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/