【极海APM32F407 Tiny Board】玩转FreeRTOS
[复制链接]
本帖最后由 damiaa 于 2023-5-28 10:55 编辑
【极海APM32F407 Tiny Board】玩转FreeRTOS
极海APM32F407 Tiny Board的sdk里面是有一些应用的,只不过是用的MINI板。下面就Tiny板的使用做一些介绍和实验。
1,...\APM32F4xx_SDK_v1.3\Middlewares\FreeRTOS目录里面有个文件FreeRTOSv202012.00-LTS.exe运行一下,把freertos解压到一个目录。
现在就把它解压到这个目录 ,原因就是例子的配置的目录就是这里。
2,打开例子...\APM32F4xx_SDK_v1.3\Examples\RTOS\FreeRTOS\Project\MDK\APM32F4xx_FreeRTOS项目
3,修改预处理符号APM32F407_MINI为APM32F407_TINY适用Tiny板子。
4,配置仿真器(前面的帖子说明过)。
5,修改代码如下:
#include "main.h"
#include "Board.h"
#include "stdio.h"
/** FreeRTOS */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"
#define DATA_BUF_SIZE (32)
typedef enum {FALSE, TRUE} BOOL;
/** USART1 receive buffer*/
uint8_t rxDataBufUSART1[DATA_BUF_SIZE] = {0};
/** USART1 transmit buffer*/
uint8_t txDataBufUSART1[DATA_BUF_SIZE] = {0};
/** USART2 receive buffer*/
uint8_t rxDataBufUSART2[DATA_BUF_SIZE] = {0};
/** USART2 transmit buffer*/
uint8_t txDataBufUSART2[DATA_BUF_SIZE] = {0};
/** USART Write data */
void USART_Write(USART_T* usart,uint8_t *dat);
/** USART Init */
void USART_Init(void);
/** Delay */
void Delay(uint32_t count);
/** Buffer compare*/
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size);
/** Task handle */
static TaskHandle_t xHandleTaskLedToggle = NULL;
static TaskHandle_t xHandleTaskUsartTest = NULL;
/** User create task */
static void UserTaskCreate(void);
/** Led toggle task */
void vTaskLedToggle(void *pvParameters);
/** Usart1 test task */
void vTaskUsartTest(void *pvParameters);
int main(void){
APM_TINY_LEDInit(LED2);
APM_TINY_LEDInit(LED3);
/** USART Initialization */
USART_Init();
/** User create task */
UserTaskCreate();
/** Startup FreeRTOS */
vTaskStartScheduler();
while(1);
}
static void UserTaskCreate(void){
xTaskCreate(vTaskLedToggle,
"TaskLedToggle",
128,
NULL,
0,
&xHandleTaskLedToggle);
xTaskCreate(vTaskUsartTest,
"TaskUsartTest",
128,
NULL,
2,
&xHandleTaskUsartTest);
}
void vTaskLedToggle(void *pvParameters){
while(1){
APM_TINY_LEDToggle(LED2);
vTaskDelay(500);
}
}
void vTaskUsartTest(void *pvParameters){
BOOL state = TRUE;
int timeout = 0;
for(int i = 0; i < DATA_BUF_SIZE; i++){
txDataBufUSART1[i] = txDataBufUSART2[i] = i;
}
while(1)
{
/** Send Data from USART1 to USART2 */
timeout = 0;
for(int i = 0; i < DATA_BUF_SIZE; i++){
/** Wait until end of transmission */
while(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_TXBE) == RESET){
vTaskDelay(10);
timeout++;
if(timeout > 10)
break;
}
USART_TxData(TINY_COM1, txDataBufUSART1[i]);
/** Wait until end of reception */
while(USART_ReadStatusFlag(TINY_COM2, USART_FLAG_RXBNE) == RESET){
vTaskDelay(10);
timeout++;
if(timeout > 10)
break;
}
rxDataBufUSART2[i] = USART_RxData(TINY_COM2);
}
/** Verify data */
state = BufferCompare(rxDataBufUSART2, txDataBufUSART1, DATA_BUF_SIZE);
/** Data is ok then turn off LED3 */
if(state == TRUE)
APM_TINY_LEDOn(LED3);
else
APM_TINY_LEDOff(LED3);
/** Task blocking time Delay */
vTaskDelay(1000);
timeout = 0;
/** Send Data from USART2 to USART1 */
for(int i = 0; i < DATA_BUF_SIZE; i++){
/** Wait until end of transmission */
while(USART_ReadStatusFlag(TINY_COM2, USART_FLAG_TXBE) == RESET){
vTaskDelay(10);
timeout++;
if(timeout > 10)
break;
}
USART_TxData(TINY_COM2, txDataBufUSART2[i]);
/** Wait until end of reception */
while(USART_ReadStatusFlag(TINY_COM1, USART_FLAG_RXBNE) == RESET){
vTaskDelay(10);
timeout++;
if(timeout > 10)
break;
}
rxDataBufUSART1[i] = USART_RxData(TINY_COM1);
}
/** Verify data */
state = BufferCompare(rxDataBufUSART1, txDataBufUSART2, DATA_BUF_SIZE);
/** Data is ok then turn off LED3 */
if(state == TRUE)
APM_TINY_LEDOn(LED3);
else
APM_TINY_LEDOff(LED3);
/** Task blocking time Delay */
vTaskDelay(2000);
APM_TINY_LEDOff(LED3);
vTaskDelay(2000);
}
}
BOOL BufferCompare(uint8_t *buf1, uint8_t *buf2, uint8_t size){
uint8_t i;
for(i = 0; i < size; i++){
if(buf1[i] != buf2[i])
return FALSE;
}
return TRUE;
}
void USART_Init(void){
USART_Config_T usartConfigStruct;
usartConfigStruct.baudRate = 115200;
usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
APM_TINY_COMInit(COM1, &usartConfigStruct);
APM_TINY_COMInit(COM2, &usartConfigStruct);
Delay(0x7FFF);
/** Disable USART1 RXBNE interrput */
USART_DisableInterrupt(TINY_COM1,USART_INT_RXBNE);
USART_DisableInterrupt(TINY_COM2,USART_INT_RXBNE);
/** Disable USART2 RXBNE interrput */
USART_ClearStatusFlag(TINY_COM1, USART_FLAG_RXBNE);
USART_ClearStatusFlag(TINY_COM2, USART_FLAG_RXBNE);
}
void Delay(uint32_t count){
volatile uint32_t delay = count;
while(delay--);
}
6,这里说明一下,freertos创建 了2个任务,一个是让led2每0.5秒交替亮灭。
任务二是串口1和串口 2直接互发数据正确就led3亮,完成 后保持2秒亮,然后熄灭2秒然后再开始下一次比较。如果led3能几秒亮然后熄灭一阵再亮就对了。
7,串口1和2直接连接好线,编译运行,查看结果:
8,谢谢。
758d0780e6cef433bbb34808e0639e78
|