一、开箱及简介:
今天收到了论坛寄来的NUCLEO-F091RC开发板,晚上便小试了一下,板载的STMF091RCT6是以Cortex-M0为内核的高性能MCU,该芯片具有256KB的Flash和32KB的SRAM,主频可以到48MHz。此外,该芯片的一大特色就是外围通信接口异常丰富,具有两路I2C接口,两路SPI接口,八路UART,以及一路CAN接口,因此非常适合外设和通信模块比较多的应用场景。板子的靓照如下:
二、RT-Thread Nano移植:
RT-Thread Nano 是RT-Thread操作系统的极简版,其内存资源占用极小,功能包括任务处理、软件定时器、信号量、邮箱和实时调度等相对完整的实时操作系统特性,是一款可裁剪的、抢占式实时多任务的 RTOS。RT-Thread Nano可以使用多种方式进行移植,这里介绍通过使用Keil-MDK进行移植的过程。
1、首先使用STM32CubeMX软件选择NUCLEO-F091RC开发板生成裸机工程,在生成代码的过程中,需要注意将异常处理函数HardFault_Handler()和悬挂处理函数 PendSV_Handler()以及系统嘀嗒定时器函数SysTick_Handler()屏蔽掉,因为这几个函数已由 RT-Thread 实现。具有设置如下图所示。
2、从官网下载RT-Thread Nano pack安装文件,RT-Thread Nano 离线安装包下载,下载结束后双击文件进行安装:
安装完毕以后,打开Keil工程文件,在 Manage Rum-Time Environment 里 "Software Component" 栏找到 RTOS,Variant 栏选择 RT-Thread,然后勾选 kernel,点击 "OK" 就添加 RT-Thread 内核到工程了。
3、系统时钟配置,需要将主函数中的HAL_Init()函数和SystemClock_Config()函数注释掉,并在RT-Thread代码中的board.c里的调用和添加外部函数声明,如下图所示。
4、经过以上的步骤,便已经移植好了RT-Thread Nano。接下来就可以在主函数中引用“rtthread.h”并编写应用程序了。本例程通过静态创建线程的方法编写了LED灯闪烁和串口输出打印两个线程,代码如下:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* [url=home.php?mod=space&uid=288409]@file[/url] : main.c
* [url=home.php?mod=space&uid=247401]@brief[/url] : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "rtthread.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 PV */
static struct rt_thread led_thread;
static char led_thread_stack[256];
static struct rt_thread serial_thread;
static char serial_thread_stack[256];
/* 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 */
static void led_thread_entry(void *parameter)
{
while(1)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
rt_thread_mdelay(500);
}
}
static void serial_thread_entry(void *parameter)
{
while(1)
{
HAL_UART_Transmit(&huart2, "rt-thread nano running...", sizeof("rt-thread nano running..."), 0xFFFF);
rt_thread_mdelay(1000);
}
}
/* 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_USART2_UART_Init();
/* USER CODE BEGIN 2 */
rt_err_t ret;
ret = rt_thread_init(&led_thread,
"ledflash",
led_thread_entry,
RT_NULL,
&led_thread_stack[0],
sizeof(led_thread_stack),
RT_THREAD_PRIORITY_MAX - 2,
20);
if(ret == RT_EOK)
{
rt_thread_startup(&led_thread);
}
ret = rt_thread_init(&serial_thread,
"serialtest",
serial_thread_entry,
RT_NULL,
&serial_thread_stack[0],
sizeof(serial_thread_stack),
RT_THREAD_PRIORITY_MAX - 2,
20);
if(ret == RT_EOK)
{
rt_thread_startup(&serial_thread);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
rt_thread_mdelay(200);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
编译程序,并通过板载的ST-Link下载程序至开发板即可看到LED每一秒亮灭一次,同时串口一秒打印输出一次。
|