基于 STM32CubeMX 添加 RT-Thread 操作系统组件(十三)- 邮箱

概述 

       本篇只要介绍这么使用STM32CubeMx工具添加RT-Thread操作系统组件,码代码的IDE是keil。介绍单线程SRAM静态内存使用。如果还不知道,这么使用STM32CubeMx工具添加RT-Thread操作系统组件,请移步到《基于 STM32CubeMX 添加 RT-Thread 操作系统组件(一)- 详细介绍操作步骤》文章阅读。好了,喝杯茶先^_^,继续前行。上一篇介绍关于《软件定时器》

 

一、STM32CubeMx配置

 

二、KEIL IDE

  1. 在Application/User文件夹,新建app_rt_thread.c文件,并添加如下代码:
    #include "rtthread.h"
    #include "main.h"
    #include "stdio.h"
    #include "usart.h"
    
    
    
    
    /* 定义线程控制块 */
    static rt_thread_t receive_thread = RT_NULL;
    static rt_thread_t send_thread = RT_NULL;
    /* 定义邮箱控制块 */
    static rt_mailbox_t test_mail = RT_NULL;
    
    /* 变量声明  */
    char test_str1[] = "this is a mail test 1";/* 邮箱消息 test1 */
    char test_str2[] = "this is a mail test 2";/* 邮箱消息 test2 */
    
    /* 函数声明 */
    static void receive_thread_entry(void* parameter);
    static void send_thread_entry(void* parameter);
    
    
    
    int MX_RT_Thread_Init(void)
    {
    	rt_kprintf("This is an RTT email message experiment!\n");
    	rt_kprintf("Press K1 | K2 for mailbox experiment test!\n");
    	/* 创建一个邮箱 */
    	test_mail = rt_mb_create("test_mail", /* 邮箱名字 */
    						10, /* 邮箱大小 */
    						RT_IPC_FLAG_FIFO);/* 信号量模式 FIFO(0x00)*/
    						if (test_mail != RT_NULL)
    						rt_kprintf("Email created successfully!\n\n");
    	/* 线程控制块指针 */
    	receive_thread = rt_thread_create( "receive", /* 线程名字 */
    						receive_thread_entry, /* 线程入口函数 */
    						RT_NULL, /* 线程入口函数参数 */
    						512, /* 线程栈大小 */
    						3, /* 线程的优先级 */
    						20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (receive_thread != RT_NULL)
    		rt_thread_startup(receive_thread);
    	else
    		return -1;
    	/* 线程控制块指针 */
    	send_thread = rt_thread_create( "send", /* 线程名字 */
    						send_thread_entry, /* 线程入口函数 */
    						RT_NULL, /* 线程入口函数参数 */
    						512, /* 线程栈大小 */
    						2, /* 线程的优先级 */
    						20); /* 线程时间片 */
    	/* 启动线程,开启调度 */
    	if (send_thread != RT_NULL)
    		rt_thread_startup(send_thread);
    	else
    		return -1;
    }
    
    /*
    ************************************************************
    * 线程定义
    *********************************************************
    */
    static void receive_thread_entry(void* parameter)
    {
    	rt_err_t uwRet = RT_EOK;
    	char *r_str;
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		/* 等待接邮箱消息 */
    		uwRet = rt_mb_recv(test_mail, /* 邮箱对象句柄 */
    					(rt_uint32_t*)&r_str, /* 接收邮箱消息 */
    					RT_WAITING_FOREVER);/* 指定超时事件,一直等 */
    		if (RT_EOK == uwRet) { /* 如果接收完成并且正确 */
    			rt_kprintf ( "The contents of the mailbox are:%s\n\n",r_str);
    			HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);//LED1 反转
    		} else
    		rt_kprintf ( "Error receiving email! Error code is 0x%x\n",uwRet);
    	}
    }
    
    static void send_thread_entry(void* parameter)
    {
    	rt_err_t uwRet = RT_EOK;
    	/* 线程都是一个无限循环,不能返回 */
    	while (1) {
    		//如果 KEY1 被单击
    		if ( HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin) == GPIO_PIN_SET ) {
    		rt_kprintf ( "KEY1 is clicked\n" );
    		/* 发送一个邮箱消息 1 */
    		uwRet = rt_mb_send(test_mail,(rt_uint32_t)&test_str1);
    		if (RT_EOK == uwRet)
    			rt_kprintf ( "The email message has been sent successfully\n" );
    		else
    			rt_kprintf ( "The mailbox message failed to send\n" );
    		}
    		//如果 KEY2 被单击
    		
    		if ( HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin) == GPIO_PIN_SET ) {
    			rt_kprintf ( "KEY2 is clicked\n" );
    		/* 发送一个邮箱 2 */
    		uwRet = rt_mb_send(test_mail,(rt_uint32_t)&test_str2);
    		if (RT_EOK == uwRet)
    			rt_kprintf ( "The email message has been sent successfully\n" );
    		else
    			rt_kprintf ( "The mailbox message failed to send\n" );
    		}
    		rt_thread_delay(20); //每 20ms 扫描一次
    	}
    }
    
    
    
  2. 在main.c文件添加如下代码:
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "usart.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    extern int MX_RT_Thread_Init(void);
    /* 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 PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    int main(void)
    {
      /* USER CODE BEGIN 1 */
    
      /* USER CODE END 1 */
    
      /* MCU Configuration--------------------------------------------------------*/
    
      /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
      HAL_Init();
    
      /* USER CODE BEGIN Init */
    
      /* USER CODE END Init */
    
      /* Configure the system clock */
      SystemClock_Config();
    
      /* USER CODE BEGIN SysInit */
    
      /* USER CODE END SysInit */
    
      /* Initialize all configured peripherals */
      MX_GPIO_Init();
      MX_USART1_UART_Init();
      /* USER CODE BEGIN 2 */
      MX_RT_Thread_Init();
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
      }
      /* USER CODE END 3 */
    }

     

  3. 自定义rt_hw_console_output()函数,在kservice.c文件添加中(重映射串口控制台到 rt_kprintf 函数)代码:
    #include "usart.h"
    .
    .
    .
    
    RT_WEAK void rt_hw_console_output(const char *str)
    {
        /* empty console output */
    	/* 进入临界段 */
    	rt_enter_critical();
    	/* 直到字符串结束 */
    	while (*str!='\0') {
    	/* 换行 */
    	if (*str=='\n') {
    		HAL_UART_Transmit(&huart1,(uint8_t *)'\r',1,1000);
    	}
    		HAL_UART_Transmit(&huart1,(uint8_t *)(str++),1,1000);
    	}
    	/* 退出临界段 */
    	rt_exit_critical();
    }

  4. 运行结果
        

    源码:git​​​​​​​

猜你喜欢

转载自blog.csdn.net/qq_36075612/article/details/107629055