首先感谢21ic、感谢极海,有幸参与APM32F107VC MINIBOARD开发板测评活动;
极海半导体作为国内MCU的佼佼者,产品涵盖了Cortex M0+\M3\M4、RISC-V、蓝牙低功耗等,应用领域广泛,更是致力于开发工业级/车规级微控制器,并在不断的开拓创新;通过参与这次测评活动来熟悉极海MCU的开发过程,在基于APM32F107VCT6 MINI开发板的基础上搭建开发环境、完成基于MINI开发板的示例工程、结合项目中常用的外设ADC和DAC进行测试、对保证程序可靠运行的IWDT和WWDT进行熟悉和测试、对MCU内部自带的FLASH读、写、保护等功能进行熟悉和测试、最后完成MCU在实际项目中的IAP功能进行实现和测试。
我们先准备一些资料:
1、APM32F107 MINI开发板使用说明书:
APM32F107 MINI开发板使用说明书V1.1.pdf
(2.21 MB)
2、KEIL的PACK安装包:
Geehy.APM32F1xx_DFP.1.1.0.pack.zip
(4.28 MB)
3、SDK底层驱动库程序及示例程序:APM32F10x_SDK_V1.8(官网下载)
4、硬件设计原理图:
APM32F107VC MINIBOARD V1.0.SchDoc.pdf
(33.61 KB)
上面这些资料也可以登录到极海官网进行下载……
接下来我们使用KEIL的MDK集成开发环境结合官方提供的SDK库来创建一个示例工程:
步骤1、创建工程
步骤2、添加程序到工程
步骤3、配置工程选项
接下来我们需要参考原理图对LED、KEY和USART进行初始化配置,通过USART和重载fputc函数来实现printf打印的功能;另外再移植两个开源的组件:一个是MultiButton,它是用来实现按键功能处理的;另外一个是MultiTimer,在我们不使用RTOS的情况下,就通过这个组件结合SysTick滴答定时器实现时间片的任务管理和调试……代码如下所示:
LED实现代码:
MultiTimer LED_MultiTimer;
/*!
* [url=home.php?mod=space&uid=247401]@brief[/url] LED MultiTimer callback
*
* @param None
*
* @retval None
*
*/
void LED_MultiTimerCallback(MultiTimer *timer, void *userData)
{
if (BIT_SET != GPIO_ReadOutputBit(GPIOE, GPIO_PIN_5))
{
GPIO_WriteBitValue(GPIOE, GPIO_PIN_5, BIT_SET);
}
else
{
GPIO_WriteBitValue(GPIOE, GPIO_PIN_5, BIT_RESET);
}
if (BIT_SET != GPIO_ReadOutputBit(GPIOE, GPIO_PIN_6))
{
GPIO_WriteBitValue(GPIOE, GPIO_PIN_6, BIT_SET);
}
else
{
GPIO_WriteBitValue(GPIOE, GPIO_PIN_6, BIT_RESET);
}
MultiTimerStart(&LED_MultiTimer, 250, LED_MultiTimerCallback, "LED");
}
/*!
* @brief LED Init
*
* @param None
*
* @retval None
*
*/
void LED_Init(void)
{
GPIO_Config_T GPIO_ConfigStruct;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
GPIO_ConfigStructInit(&GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_5 | GPIO_PIN_6;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_ConfigStruct.mode = GPIO_MODE_OUT_PP;
GPIO_Config(GPIOE, &GPIO_ConfigStruct);
MultiTimerStart(&LED_MultiTimer, 250, LED_MultiTimerCallback, "LED");
}
KEY实现代码:
MultiTimer KEY_MultiTimer;
struct Button btnKEY1, btnKEY2;
/*!
* @brief Read key input level
*
* @param btn
*
* @retval level
*
*/
uint8_t read_pin_level(uint8_t pin_id)
{
switch (pin_id)
{
case 0:
return (GPIO_ReadInputBit(GPIOA, GPIO_PIN_0));
case 1:
return (GPIO_ReadInputBit(GPIOA, GPIO_PIN_1));
default:
return (BIT_SET);
}
}
/*!
* @brief Key handler
*
* @param btn
*
* @retval None
*
*/
void KEY_Handler(void *btn)
{
Button *key = btn;
uint8_t button_id = key->button_id + 1;
switch (key->event)
{
case PRESS_DOWN:
printf("\r\nKEY%d : PRESS_DOWN", button_id);
break;
case PRESS_UP:
printf("\r\nKEY%d : PRESS_UP", button_id);
break;
case SINGLE_CLICK:
printf("\r\nKEY%d : SINGLE_CLICK", button_id);
break;
case DOUBLE_CLICK:
printf("\r\nKEY%d : DOUBLE_CLICK", button_id);
break;
case LONG_PRESS_START:
printf("\r\nKEY%d : LONG_PRESS_START", button_id);
break;
case LONG_PRESS_HOLD:
printf("\r\nKEY%d : LONG_PRESS_HOLD", button_id);
break;
default:
break;
}
}
/*!
* @brief KEY MultiTimer callback
*
* @param None
*
* @retval None
*
*/
void KEY_MultiTimerCallback(MultiTimer *timer, void *userData)
{
button_ticks();
MultiTimerStart(&KEY_MultiTimer, 5, KEY_MultiTimerCallback, "KEY");
}
/*!
* @brief KEY Init
*
* @param None
*
* @retval None
*
*/
void KEY_Init(void)
{
GPIO_Config_T GPIO_ConfigStruct;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
GPIO_ConfigStructInit(&GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
button_init(&btnKEY1, read_pin_level, BIT_RESET, 0);
button_attach(&btnKEY1, SINGLE_CLICK, KEY_Handler);
button_start(&btnKEY1);
button_init(&btnKEY2, read_pin_level, BIT_RESET, 1);
button_attach(&btnKEY2, SINGLE_CLICK, KEY_Handler);
button_start(&btnKEY2);
MultiTimerStart(&KEY_MultiTimer, 5, KEY_MultiTimerCallback, "KEY");
}
printf实现代码:
/*!
* @brief USART1 Init with baudrate
*
* @param None
*
* @retval None
*
*/
void USART_Init(uint32_t Baudrate)
{
GPIO_Config_T GPIO_ConfigStruct;
USART_Config_T USART_ConfigStruct;
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_USART1);
USART_ConfigStructInit(&USART_ConfigStruct);
USART_ConfigStruct.baudRate = 115200;
USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
USART_ConfigStruct.parity = USART_PARITY_NONE;
USART_ConfigStruct.mode = USART_MODE_TX;
USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
USART_Config(USART1, &USART_ConfigStruct);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
GPIO_ConfigStructInit(&GPIO_ConfigStruct);
GPIO_ConfigStruct.pin = GPIO_PIN_9;
GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
GPIO_Config(GPIOA, &GPIO_ConfigStruct);
USART_Enable(USART1);
}
/*!
* @brief redefine fputc function
*
* @param ch, *f
*
* @retval ch
*
*/
int fputc(int ch, FILE *f)
{
USART_TxData(USART1, (uint8_t)ch);
while (RESET == USART_ReadStatusFlag(USART1, USART_FLAG_TXBE))
{
}
return (ch);
}
主程序及中断程序:
volatile uint32_t SysTick_Tick = 0;
/*!
* @brief Get SysTick tick
*
* @param None
*
* @retval SysTick_Tick
*
*/
uint64_t SysTick_GetTick(void)
{
return (SysTick_Tick);
}
/*!
* @brief Main program
*
* @param None
*
* @retval None
*
*/
int main(void)
{
MultiTimerInstall(SysTick_GetTick);
SysTick_Config(SystemCoreClock / 1000);
USART_Init(115200);
printf("\r\nAPM32F107VC MINIBOARD V1.0 %s %s\r\n", __DATE__, __TIME__);
KEY_Init();
LED_Init();
while (1)
{
MultiTimerYield();
}
}
/*!
* @brief This function handles SysTick Handler
*
* @param None
*
* @retval None
*
*/
void SysTick_Handler(void)
{
SysTick_Tick++;
}
最后我们将编译无误的程序下载到开发板运行,LED灯间隔在闪烁,当我们按下相应的按键后会有消息打印出来……
软件工程源代码:
Project_20230217.zip
(478.65 KB)
|