RT-thread版本:4.0.1,几乎是最新版
为了能让像我一样喜欢白嫖的小伙伴能不求人借会员就可以下载到资源,我把代码的百度云链接放在文章末尾,小伙伴们可以用百度云下载。
代码使用的是串口6作为二号串口,可能有些代码是显示串口2或者3的,那是我懒得改了,忽略即可。整个工程使用设备驱动的方式调用串口6,将串口6接收到的数据从串口1输出。串口6配置函数我写在USART.c中,如果后续想要使用其他串口的话在USART.c中参照USART6_Config();的样子写一个函数就好了。
PS:由于F767的官方例程并没有串口6的相关配置,所以我是自己添加的,串口中断优先级是占用了串口5的优先级,使用串口5的话需要注意一下
keil工程文件位于RT-thread_v4.0.1\rt-thread\bsp\stm32\stm32f767-atk-apollo中,不想重复打开的话可以创建一个快捷方式
/*main.c*/
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-06 BalanceTWK first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <rtdbg.h>
#include <serial.h>
#include <USART.h>
#include <bsp_led.h>//开发板LED配置文件,PB1,0分别为LED0,1
//#include "STM32f767xx.h"
rt_device_t UART6_find;//创建任务查找句柄
struct rt_semaphore sem;//创建信号量
struct serial_configure UART6_config= RT_SERIAL_CONFIG_DEFAULT;//定义串口6参数配置结构体,这里选用默认配置
rt_thread_t UART6_th;
rt_err_t rx_callback(rt_device_t dev,rt_size_t size)//设置中断回调函数
{
rt_sem_release(&sem);//唤醒信号量
LED1(ON);
return RT_EOK;
}
void UART6_rec_thread_entry(void *parameter)//串口6接收线程入口
{
char UART6_rx_buffer;
while(1)
{
while(rt_device_read(UART6_find,0,&UART6_rx_buffer,1)!=1)//每接收到一个字符就触发一次中断
{
LED0(ON);
rt_sem_take(&sem,RT_WAITING_FOREVER);
}
rt_kprintf("%c",UART6_rx_buffer);
}
}
int main(void)
{
USART6_Config();
rt_err_t receive = 0;
UART6_find=rt_device_find("uart6");
if(UART6_find==RT_NULL)
{
rt_kprintf("UART6查找失败");
return -ENOMEM;
}
receive=rt_device_open(UART6_find,RT_DEVICE_FLAG_INT_RX|RT_DEVICE_FLAG_INT_RX);//以读写+中断接收方式打开串口2设备,接收到的值传给receive
if(receive<0)
{
rt_kprintf("UART6打开失败");
return -receive;
}
rt_device_control(UART6_find,RT_DEVICE_CTRL_CONFIG,(void *)&UART6_config);
rt_device_set_rx_indicate(UART6_find,rx_callback);
rt_sem_init(&sem,"rx_sem",0,RT_IPC_FLAG_FIFO);//设置接收信号量,初始数据为0,接收到数据调用上面的rx_callback后信号量才会+1
//创建串口6数据接收线程
UART6_th = rt_thread_create("UART6_receive",UART6_rec_thread_entry,NULL,1024,10,5);
if(UART6_th==RT_NULL)
{
rt_kprintf("UART6数据接收线程创建失败");
return -ENOMEM;
}
rt_thread_startup(UART6_th);
rt_device_write(UART6_find,0,"Uart6 tset...",rt_strlen("Uart6 tset..."));//发送字符
return RT_EOK;
}
/*board.c*/
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-11-06 SummerGift first version
*/
#include "board.h"
#include "drv_common.h"
#include "drv_usart.h"
#include "USART.h"
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/**Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1;
PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
}
void rt_hw_board_init()
{
#ifdef SCB_EnableICache
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
#endif
#ifdef SCB_EnableDCache
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
#endif
/* HAL_Init() function is called at the beginning of the program */
HAL_Init();
/* System clock initialization */
SystemClock_Config();
rt_hw_systick_init();
rt_kprintf("开始串口3初始化");
USART6_Config();
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
//printf("finish usart3 init");
/* Heap initialization */
#if defined(RT_USING_HEAP)
rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
#endif
/* Pin driver initialization is open by default */
#ifdef RT_USING_PIN
rt_hw_pin_init();
#endif
/* USART driver initialization is open by default */
#ifdef RT_USING_SERIAL
rt_hw_usart_init();
#endif
/* Set the shell console output device */
#ifdef RT_USING_CONSOLE
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
/* Board underlying hardware initialization */
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
}
例程文件:
链接:https://pan.baidu.com/s/11J8h4v3S94OtzzYpyJfzMw? 提取码:mp5b? --来自百度网盘超级会员V5的分享
|