我们知道CH32V307是基于32位RISC-V内核设计的大容量通用微控制器,搭载V4F内核,支持单精度浮点指令集,具有更高的运算性能。支持内置PHY收发器的USB2.0高速接口(480Mbps),千兆以太网MAC等,板上的资源非常丰富。
本次实验基于官方SDK提供的参考例程进行,IDE采用MounRiver Studio v1.91,HarmonyOS源码开源在gitee上,可通过链接:LiteOS_m的源码仓库获取,文件列表结构如下图所示:
我们可以将源文件拷贝到基本工程中,并在IDE中包含其头文件。
代码中实现两个线程任务,分别以不同频率点亮LED1、LED2,这里需要注意的是,官方的原理图中KEY与PC0位置调换了,开发板上的LED、KEY需要杜邦线跳接。这里笔者通过杜邦线将LED1与PC3连接,LED2与PC1连接,如下图所示:
部分参考代码如下:
#include "debug.h"
#include "los_tick.h"
#include "los_task.h"
#include "los_config.h"
#include "los_interrupt.h"
#include "los_debug.h"
#include "los_compiler.h"
#include "ch32v30x.h"
#include "ch32v30x_gpio.h"
VOID taskSampleEntry2(VOID)
{
u8 i = 0;
while(1) {
LOS_TaskDelay(1000);
GPIO_WriteBit(GPIOC, GPIO_Pin_3, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET));
printf("taskSampleEntry2 running,task2 SP:%08x\n",__get_SP());
}
}
VOID taskSampleEntry1(VOID)
{
u8 i = 0;
while(1) {
LOS_TaskDelay(300);
GPIO_WriteBit(GPIOC, GPIO_Pin_1, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET));
printf("taskSampleEntry1 running,task1 SP:%08x\n",__get_SP());
}
}
void LED_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
UINT32 taskSample(VOID)
{
UINT32 uwRet;
UINT32 taskID1,taskID2;
TSK_INIT_PARAM_S stTask={0};
stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry1;
stTask.uwStackSize = 0X500;
stTask.pcName = "taskSampleEntry1";
stTask.usTaskPrio = 6;
uwRet = LOS_TaskCreate(&taskID1, &stTask);
if (uwRet != LOS_OK) {
printf("create task1 failed\n");
}
stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry2;
stTask.uwStackSize = 0X500;
stTask.pcName = "taskSampleEntry2";
stTask.usTaskPrio = 7;
uwRet = LOS_TaskCreate(&taskID2, &stTask);
if (uwRet != LOS_OK) {
printf("create task2 failed\n");
}
EXTI0_INT_INIT();
return LOS_OK;
}
LITE_OS_SEC_TEXT_INIT int main(void)
{
unsigned int ret;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
SystemCoreClockUpdate();
Delay_Init();
LED_GPIO_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n",SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
ret = LOS_KernelInit();
taskSample();
if (ret == LOS_OK)
{
LOS_Start();
}
while (1) {
__asm volatile("nop");
}
}
编译完成后,在IDE中可直接点下载图标或按F8即可。
串口打印信息输出如下:
闪灯效果
|