本人用RTOS创建了五个任务, 如下:
- /* RTOS 多任务创建调度机制 */
- osThreadDef(TCPSOCKETSERVER, TCP_Socket_Server, osPriorityRealtime, 0, 512); 1
- TCPSOCKETSERVERHandle = osThreadCreate(osThread(TCPSOCKETSERVER), NULL);
- osThreadDef(STM32F4ADC, STM32F429_ADC1_Init, osPriorityNormal, 0, 512); 2
- STM32F4ADCRHandle = osThreadCreate(osThread(STM32F4ADC), NULL);
- osThreadDef(RTCCLOCKTIME, RTC_CLOCK_TIME, osPriorityNormal, 0, 512); 3
- RTCCLOCKTIMEHandle = osThreadCreate(osThread(RTCCLOCKTIME), NULL);
- osThreadDef(RTOSLEDTEST, RTOS_LED_TEST,osPriorityNormal, 0, 512); 4
- RTOSLEDTESTHandle = osThreadCreate(osThread(RTOSLEDTEST), NULL);
- osThreadDef(RTOSKEYEXIT, RTOS_KEY_EXIT, osPriorityNormal, 0, 512); 5
- RTOSKEYEXITHandle = osThreadCreate(osThread(RTOSKEYEXIT), NULL);
同时运行的时候, 2,3,4,5 工作正常,1 可以创建任务,只是无法Ping通,暂停2,3,4,5 单独执行1 ,就没问题,不知道这个问题出在哪里,很愚蒙
- /**
- ******************************************************************************
- * File Name : main.c
- * Description : Main program body
- ******************************************************************************
- * This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * Copyright (c) 2017-2018 淮安奥创科技有限公司
- * All rights reserved.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "stm32f4xx_hal.h"
- #include "tcp_socket_server.h"
- #include "stm32f429_device_Init.h"
- #include "stm32f4xx_oled_conf.h"
- #include "stm32f4xx_oled_image.h"
- #include "cmsis_os.h"
- #include "lwip.h"
- #define V25 0.76 // V25 = 25 °C 时的电压值 0.76 (mV)
- #define KeyTaskNumber 5 // 当前需要KEY切换的任务数值
- /* Private variables ---------------------------------------------------------*/
- osThreadId TCPSOCKETSERVERHandle;
- osThreadId STM32F4ADCRHandle;
- osThreadId RTCCLOCKTIMEHandle;
- osThreadId RTOSLEDTESTHandle;
- osThreadId RTOSKEYEXITHandle;
- /* Private function prototypes -----------------------------------------------*/
- static void TCP_Socket_Server(void const * argument);
- static void STM32F429_ADC1_Init(void const * argument);
- static void RTC_CLOCK_TIME(void const * argument);
- static void RTOS_LED_TEST(void const * argument);
- static void RTOS_KEY_EXIT(void const * argument);
- static void STM32_System_PowerON(void);
- static void RTOS_TASK_Init(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- int main(int argc, char **argv) {
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- OLED_GPIO_Init();
- STM32_System_PowerON();
- MX_USART3_UART_Init();
- RTOS_TASK_Init();
- /* RTOS 开始调度内核 */
- osKernelStart();
- /* USER CODE BEGIN WHILE */
- while (1) {
- }
- }
- static void RTOS_TASK_Init() {
- /* RTOS 多任务创建调度机制 */
- osThreadDef(TCPSOCKETSERVER, TCP_Socket_Server, osPriorityRealtime, 0, 512);
- TCPSOCKETSERVERHandle = osThreadCreate(osThread(TCPSOCKETSERVER), NULL);
- osThreadDef(STM32F4ADC, STM32F429_ADC1_Init, osPriorityNormal, 0, 512);
- STM32F4ADCRHandle = osThreadCreate(osThread(STM32F4ADC), NULL);
- osThreadDef(RTCCLOCKTIME, RTC_CLOCK_TIME, osPriorityNormal, 0, 512);
- RTCCLOCKTIMEHandle = osThreadCreate(osThread(RTCCLOCKTIME), NULL);
- osThreadDef(RTOSLEDTEST, RTOS_LED_TEST,osPriorityNormal, 0, 512);
- RTOSLEDTESTHandle = osThreadCreate(osThread(RTOSLEDTEST), NULL);
- osThreadDef(RTOSKEYEXIT, RTOS_KEY_EXIT, osPriorityNormal, 0, 512);
- RTOSKEYEXITHandle = osThreadCreate(osThread(RTOSKEYEXIT), NULL);
- }
- static void TCP_Socket_Server(void const * argument) {
- /* LwIP 初始化栈 */
- MX_LWIP_Init();
- for(;;) {
- Tcp_Socket_Serve_Init();
- osDelay(100);
- }
- }
- static void STM32F429_ADC1_Init(void const * argument) {
- double Voltage1 = 0,Voltage2 = 0,Voltage3 = 0;
- uint32_t i = 0;
- uint32_t ADC_ConvertedValue[3];
- __IO uint16_t ADC_Temperature;
- MX_DMA_Init();
- MX_ADC1_Init();
- HAL_ADC_Start_DMA( hadc1, (uint32_t*) ADC_ConvertedValue, 3);
- for(;;) {
- /*
- * ADC_ConvertedValue*3.3/4096为将数据转化为电压,单位为V
- * V25 = 0.76V Avg_Slope = 2.5(mv) = 0.0025V
- * 使用以下公式计算温度:
- * 温度(单位为 °C) = {(VSENSE — V25) / Avg_Slope} + 25
- * 其中:
- * — V25 = 25 °C 时的 VSENSE 值
- * — Avg_Slope = 温度与 VSENSE 曲线的平均斜率(以 mV/°C 或 μV/°C 表示)
- */
- for(i = 0; i < 3; i++) {
- printf("ADC1-ARM_CPU_TEMP电压采集(DMA): ");
- printf("[%d] -> %d nr",i, ADC_ConvertedValue);
- }
- printf("-------------------------------------------- nr");
- Voltage1 = (float)ADC_ConvertedValue[0] *3.3/4096;
- Voltage2 = (float)ADC_ConvertedValue[1] *3.3/4096;
- Voltage3 = (float)ADC_ConvertedValue[2] *3.3/4096;
- printf(" Voltage1 = %f nr Voltage2 = %f nr Voltage3 = %f nr",Voltage1, Voltage2, Voltage3);
- ADC_Temperature = (Voltage1 - V25) / 0.0025 + 25;
- printf(" CPU_TEMP = %d °C nr",ADC_Temperature);
- printf("-------------------------------------------- nr");
- osDelay(100);
- }
- }
- static void RTC_CLOCK_TIME(void const * argument) {
- /* RTC 时钟初始化函数 */
- MX_RTC_Init();
- for(;;) {
- HAL_RTC_GetTime( hrtc, TimeInit, RTC_FORMAT_BIN); // 获取RTC当前时间(必须先获取时间)
- HAL_RTC_GetDate( hrtc, DateInit, RTC_FORMAT_BIN); // 获取RTC当前日期
- printf("-------------------------RTC 实时时钟显示-----------------------nr");
- printf("公历:%2d年%d月%d日 nr",2000+DateInit.Year, DateInit.Month, DateInit.Date);
- printf("时间:%-2d时%d分%d秒 nr",TimeInit.Hours, TimeInit.Minutes, TimeInit.Seconds);
- printf("----------------------------------------------------------------nr");
- osDelay(100);
- }
- }
- static void RTOS_LED_TEST(void const * argument) {
- for(;;) {
- LED3_RED_SET();
- osDelay(520);
- LED3_RED_RESET();
- osDelay(520);
- }
- }
- static void RTOS_KEY_EXIT(void const * argument) {
- uint32_t count = 0;
- for(;;) {
- if(KEY_LED_SWITCH() == Key_Down) {
- count++;
- }
- if(count > KeyTaskNumber) {
- count = 0;
- }
- printf("按键计数: %d nr",count);
- osDelay(100);
- }
- }
- void STM32_System_PowerON() {
- OLED_IMAGES_SHOW(2,128,LiYaOSLogo);
- HAL_Delay(2500);
- }
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
0
|
1个回答
|
|
|