下载WIN版本AT32 Work Bench压缩包,解压后直接双击AT32_Work_Bench.exe打开工具 系列选择AT32F423,MCU型号选择AT32F423VCT7,点击新建
配置PD13、PD14、PD15为输出模式,初始化输出高电平
配置PF0、PF1为外部晶振引脚 配置调试接口SWD模式
配置NVIC为抢占优先级4位,子优先级0位;开启systick中断 配置时钟 代码预览,根据图形化配置后会生成的初始化代码 点击生成代码 设置项目名、项目位置、工具链、固件包位置,点击确定,生成工程 生成的工程文件夹中已经包含freertos文件夹 也可从freertos官网https://www.freertos.org/zh-cn-cmn-s/下载最新源码 将freertos文件添加到工程中,其中port.c文件在portable->rvds->ARM_CM4F文件夹中,heap_4.c文件在portable-> memmang文件夹中, 添加freertos的.h头文件 打开官网的FreeRTOS源码Demo文件夹,把M4F内核的芯片例程中的FreeRTOSConfig.h文件拷贝到工程的FreeRTOS文件夹下的include文件夹中 注释禁用at32f423_int.c中的PendSV_Handler()、SysTick_Handler()、SVC_Handler(),使用freertos port.c中的xPortPendSVHandler()、xPortSysTickHandler()、vPortSVCHandler() 修改FreeRTOSConfig.h配置,configUSE_IDLE_HOOK、configUSE_TICK_HOOK、configCHECK_FOR_STACK_OVERFLOW、configUSE_MALLOC_FAILED_HOOK改为0,configTOTAL_HEAP_SIZE改为20k 测试代码设计4个任务:start_task、LEDR_task、LEDY_task、LEDG_task,start_task用来创建LEDR_task、LEDY_task、LEDG_task 3个任务,LEDR_task控制LEDR 2秒闪烁1次,LEDY_task控制LEDY 1秒闪烁1次,LEDG_task控制LEDG 0.5秒闪烁1次,编译下载测试
部分代码 /* Includes ------------------------------------------------------------------*/
#include "at32f423_wk_config.h"
/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "stdio.h"
#include "FreeRTOS.h"
#include "task.h"
/* add user code end private includes */
/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */
/* add user code end private typedef */
/* private define ------------------------------------------------------------*/
/* add user code begin private define */
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
#define LEDR_TASK_PRIO 2
#define LEDR_TASK_STK_SIZE 128
#define LEDY_TASK_PRIO 2
#define LEDY_TASK_STK_SIZE 128
#define LEDG_TASK_PRIO 2
#define LEDG_TASK_STK_SIZE 128
/* add user code end private define */
/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */
/* add user code end private macro */
/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */
static TaskHandle_t StartTask_Handler = NULL;
static TaskHandle_t LEDRTask_Handler = NULL;
static TaskHandle_t LEDYTask_Handler = NULL;
static TaskHandle_t LEDGTask_Handler = NULL;
/* add user code end private variables */
/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */
void LEDR_task(void *pvParameters);
void LEDY_task(void *pvParameters);
void LEDG_task(void *pvParameters);
/* add user code end function prototypes */
/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */
static void start_task(void *pvParameters)
{
taskENTER_CRITICAL();
xTaskCreate((TaskFunction_t )LEDR_task,
(const char* )"LEDR_task",
(uint16_t )LEDR_TASK_STK_SIZE,
(void* )NULL,
(UBaseType_t )LEDR_TASK_PRIO,
(TaskHandle_t* )&LEDRTask_Handler);
xTaskCreate((TaskFunction_t )LEDY_task,
(const char* )"LEDY_task",
(uint16_t )LEDY_TASK_STK_SIZE,
(void* )NULL,
(UBaseType_t )LEDY_TASK_PRIO,
(TaskHandle_t* )&LEDYTask_Handler);
xTaskCreate((TaskFunction_t )LEDG_task,
(const char* )"LEDG_task",
(uint16_t )LEDG_TASK_STK_SIZE,
(void* )NULL,
(UBaseType_t )LEDG_TASK_PRIO,
(TaskHandle_t* )&LEDGTask_Handler);
vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}
void LEDR_task(void *pvParameters)
{
while (1)
{
//printf("LEDR_task run ...\r\n");
gpio_bits_toggle(GPIOD, LED_R_PIN);
vTaskDelay(2000);
}
}
void LEDY_task(void *pvParameters)
{
while (1)
{
//printf("LEDY_task run ...\r\n");
gpio_bits_toggle(GPIOD, LED_Y_PIN);
vTaskDelay(1000);
}
}
void LEDG_task(void *pvParameters)
{
while (1)
{
//printf("LEDG_task run ...\r\n");
gpio_bits_toggle(GPIOD, LED_G_PIN);
vTaskDelay(500);
}
}
/* add user code end 0 */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] take some delay for waiting power stable, delay is about 60ms with frequency 8MHz.
* @param none
* @retval none
*/
static void wk_wait_for_power_stable(void)
{
volatile uint32_t delay = 0;
for(delay = 0; delay < 50000; delay++);
}
/**
* @brief main function.
* @param none
* @retval none
*/
int main(void)
{
/* add user code begin 1 */
/* add user code end 1 */
/* add a necessary delay to ensure that Vdd is higher than the operating
voltage of battery powered domain (2.57V) when the battery powered
domain is powered on for the first time and being operated. */
wk_wait_for_power_stable();
/* system clock config. */
wk_system_clock_config();
/* config periph clock. */
wk_periph_clock_config();
/* system tick config */
SysTick_Config(system_core_clock / 1000);
/* nvic config. */
wk_nvic_config();
/* init gpio function. */
wk_gpio_config();
/* add user code begin 2 */
xTaskCreate((TaskFunction_t )start_task,
(const char* )"start_task",
(uint16_t )START_STK_SIZE,
(void* )NULL,
(UBaseType_t )START_TASK_PRIO,
(TaskHandle_t* )&StartTask_Handler);
vTaskStartScheduler();
return 0;
/* add user code end 2 */
//while(1)
//{
/* add user code begin 3 */
/* add user code end 3 */
//}
}
测试例程
|