pstd2/Core/Src/freertos.c

313 lines
9.0 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 "communication.h"
#include "ProximitySwitches.h"
#include "dac.h"
#include "ctr_logic.h"
#include "bldc.h"
#include "hc595display.h"
#include "ds18b20.h"
#include "flash.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 */
osThreadId defaultTaskHandle;
osThreadId task_comHandle;
osThreadId task_get_switchHandle;
osThreadId task_ctr_motorHandle;
osThreadId task_display_reHandle;
osMutexId beep_mutexHandle;
osSemaphoreId binary_sem_nssHandle;
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
static void fun_get_switch_pwr_vol(void);
/* USER CODE END FunctionPrototypes */
void StartDefaultTask(void const * argument);
void start_task_com(void const * argument);
void start_task_get_switch_vol(void const * argument);
void start_task_ctr_motor(void const * argument);
void start_task_disp_result(void const * argument);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize)
{
*ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
*ppxIdleTaskStackBuffer = &xIdleStack[0];
*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
/* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */
/**
* @brief FreeRTOS initialization
* @param None
* @retval None
*/
void MX_FREERTOS_Init(void) {
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Create the mutex(es) */
/* definition and creation of beep_mutex */
osMutexDef(beep_mutex);
beep_mutexHandle = osMutexCreate(osMutex(beep_mutex));
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* Create the semaphores(s) */
/* definition and creation of binary_sem_nss */
osSemaphoreDef(binary_sem_nss);
binary_sem_nssHandle = osSemaphoreCreate(osSemaphore(binary_sem_nss), 1);
/* 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) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of task_com */
osThreadDef(task_com, start_task_com, osPriorityIdle, 0, 2048);
task_comHandle = osThreadCreate(osThread(task_com), NULL);
/* definition and creation of task_get_switch */
osThreadDef(task_get_switch, start_task_get_switch_vol, osPriorityIdle, 0, 128);
task_get_switchHandle = osThreadCreate(osThread(task_get_switch), NULL);
/* definition and creation of task_ctr_motor */
osThreadDef(task_ctr_motor, start_task_ctr_motor, osPriorityIdle, 0, 512);
task_ctr_motorHandle = osThreadCreate(osThread(task_ctr_motor), NULL);
/* definition and creation of task_display_re */
osThreadDef(task_display_re, start_task_disp_result, osPriorityIdle, 0, 128);
task_display_reHandle = osThreadCreate(osThread(task_display_re), NULL);
/* 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 const * argument)
{
/* USER CODE BEGIN StartDefaultTask */
/* Infinite loop */
for (;;)
{
HAL_GPIO_TogglePin(PF2_LED_GPIO_Port, PF2_LED_Pin);
osDelay(500);
}
/* USER CODE END StartDefaultTask */
}
/* USER CODE BEGIN Header_start_task_com */
/**
* @brief Function implementing the task_com thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_task_com */
void start_task_com(void const * argument)
{
/* USER CODE BEGIN start_task_com */
fun_ini_modbus(&modbus_rtu_A);
fun_ini_modbus(&modbus_rtu_B);
fun_ini_modbus(&modbus_rtu_hmi);
/* Infinite loop */
for (;;)
{
if (motor_X == MOTOR_A)
fun_proc_modbus(&modbus_rtu_A);
else if (motor_X == MOTOR_B)
fun_proc_modbus(&modbus_rtu_B);
fun_proc_modbus(&modbus_rtu_hmi);
osDelay(1);
}
/* USER CODE END start_task_com */
}
/* USER CODE BEGIN Header_start_task_get_switch_vol */
/**
* @brief Function implementing the task_get_switch thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_task_get_switch_vol */
void start_task_get_switch_vol(void const * argument)
{
/* USER CODE BEGIN start_task_get_switch_vol */
fun_ini_adc();
fun_ini_switch_pwr(&prox_swith_pwr_A);
fun_ini_switch_pwr(&prox_swith_pwr_B);
/* Infinite loop */
for (;;)
{
fun_get_switch_vol();
fun_get_switch_pwr_vol();
fun_ctr_switch_pwr(&prox_swith_pwr_A);
fun_ctr_switch_pwr(&prox_swith_pwr_B);
osDelay(1);
//osDelay(500);
}
/* USER CODE END start_task_get_switch_vol */
}
/* USER CODE BEGIN Header_start_task_ctr_motor */
/**
* @brief Function implementing the task_ctr_motor thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_task_ctr_motor */
void start_task_ctr_motor(void const * argument)
{
/* USER CODE BEGIN start_task_ctr_motor */
fun_ini_freq(&freq_opto);
fun_ini_freq(&freq_namur);
fun_ini_encoder(&htim1, &encoder_B);
fun_ini_encoder(&htim3, &encoder_A);
TMC2240A_A_EN_H();
TMC2240A_B_EN_H();
TMC2240A_RST_H();
fun_ini_tmc2240a(&motor_A);
vTaskDelay(10);
fun_ini_tmc2240a(&motor_B);
//motor_X = FlashRead(0x08040000);
/* Infinite loop */
for (;;)
{
if (motor_X == MOTOR_A)
{
motor_B.en = FALSE;
fun_set_motor(&motor_B);
fun_get_encoder(&htim3, &encoder_A);
fun_ctr_motor(&motor_A);
}
else if (motor_X == MOTOR_B)
{
motor_A.en = FALSE;
fun_set_motor(&motor_A);
fun_get_encoder(&htim1, &encoder_B);
fun_ctr_motor(&motor_B);
}
fun_ctr_bldc(&bldc_A);
fun_ctr_beep(500);
osDelay(10);
}
/* USER CODE END start_task_ctr_motor */
}
/* USER CODE BEGIN Header_start_task_disp_result */
/**
* @brief Function implementing the task_display_re thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_start_task_disp_result */
void start_task_disp_result(void const * argument)
{
/* USER CODE BEGIN start_task_disp_result */
fun_ini_bus_port(&bus_port_A);
fun_ini_bus_port(&bus_port_B);
fun_ini_ds18b20(&ds18b20_A);
fun_ini_ds18b20(&ds18b20_B);
fun_ini_595_display(&hc595display_A);
fun_ini_595_display(&hc595display_B);
/* Infinite loop */
for (;;)
{
ds18b20_read_temperature_cb(&bus_port_A, &ds18b20_A);
ds18b20_read_temperature_cb(&bus_port_B, &ds18b20_B);
fun_ctr_595_display(&hc595display_A);
fun_ctr_595_display(&hc595display_B);
osDelay(1000);
}
/* USER CODE END start_task_disp_result */
}
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
static void fun_get_switch_pwr_vol()
{
prox_swith_pwr_A.voltage_pv = prox_switch_A_vol[0];
prox_swith_pwr_B.voltage_pv = prox_switch_B_vol[0];
}
/* USER CODE END Application */