【上海航芯 ACM32F070开发板+触控功能评估板】01.基础工程&KEY&LED&BUZZER&SHELL
[复制链接]
之前接触到航芯的芯片还是加密芯片,现在都已经出MCU产品了,在M0产品上看到了很多M4才有功能,比如AES算法、TRNG随机数、QSPI、Fast-Plus I2C等,还有丰富的模拟外设;收到开发板也很开心,从包装到里面的垫都感觉到厂家追求……这是我收到包装最好的开发板了
准备资料
固件库资料合集:
固件库.rar
(11.94 MB, 下载次数: 1)
准备环境
安装KEIL PACK芯片支持包
创建工程
添加工程文件
配置工程
实现功能
参考官方例程程序,在创建的空工程上添加LED、KEY、BUUZER这3个设置驱动模块,移植MultiButton结合KEY实现按键检测扫描和处理,移植Letter-Shell结合USART实现Shell功能,移植MultiTime实现整个程序的任务注册、调度和处理,通过TIM的PWM输出功能实现BUZZER的控制。
实现代码
/*******************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] BUZZER.c
* [url=home.php?mod=space&uid=1315547]@author[/url] King
* [url=home.php?mod=space&uid=252314]@version[/url] V1.00
* [url=home.php?mod=space&uid=311857]@date[/url] 21-Oct-2022
* [url=home.php?mod=space&uid=159083]@brief[/url] ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __BUZZER_C__
/* Includes ------------------------------------------------------------------*/
#include "BUZZER.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define TIM_CLOCK_FREQ (8000000U)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
MultiTimer BUZZER_MultiTimer;
/* Private variables ---------------------------------------------------------*/
BUZZER_TypeDef BUZZER_TAB[] =
{
{50, 100, 1},
{50, 100, 1},
{50, 100, 0},
};
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* [url=home.php?mod=space&uid=1020061]@attention[/url] *******************************************************************************/
void BUZZER_MultiTimerCallback(MultiTimer *timer, void *userData)
{
static uint8_t State = 0, Index = 0;
if(State == 0)
{
State = 1; HAL_TIM_PWM_Output_Start(TIM15, TIM_CHANNEL_1);
MultiTimerStart(&BUZZER_MultiTimer, BUZZER_TAB[Index].PlayMS, BUZZER_MultiTimerCallback, "BUZZER");
}
else
{
State = 0; HAL_TIM_PWM_Output_Stop( TIM15, TIM_CHANNEL_1);
if(BUZZER_TAB[Index].KeepOn)
{
MultiTimerStart(&BUZZER_MultiTimer, BUZZER_TAB[Index++].WaitMS, BUZZER_MultiTimerCallback, "BUZZER");
}
else
{
Index = 0;
}
}
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void BUZZER_Init(void)
{
TIM_HandleTypeDef TIM_Handler;
TIM_OC_InitTypeDef Tim_OC_Init_Para;
uint32_t timer_clock = System_Get_APBClock();
if(System_Get_SystemClock() != System_Get_APBClock())
{
timer_clock = System_Get_APBClock() << 1;
}
TIM_Handler.Instance = TIM15;
TIM_Handler.Init.ARRPreLoadEn = TIM_ARR_PRELOAD_ENABLE;
TIM_Handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TIM_Handler.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM_Handler.Init.RepetitionCounter = 0;
TIM_Handler.Init.Prescaler = (timer_clock / TIM_CLOCK_FREQ) - 1;
if(timer_clock % TIM_CLOCK_FREQ > TIM_CLOCK_FREQ / 2)
{
TIM_Handler.Init.Prescaler = TIM_Handler.Init.Prescaler + 1;
}
TIM_Handler.Init.Period = (TIM_CLOCK_FREQ / 1000000) * 500 - 1; //---Period = 500us
HAL_TIMER_MSP_Init(&TIM_Handler);
HAL_TIMER_Base_Init(&TIM_Handler);
Tim_OC_Init_Para.OCMode = OUTPUT_MODE_PWM1;
Tim_OC_Init_Para.OCIdleState = OUTPUT_IDLE_STATE_0;
Tim_OC_Init_Para.OCNIdleState = OUTPUT_IDLE_STATE_0;
Tim_OC_Init_Para.OCPolarity = OUTPUT_POL_ACTIVE_HIGH;
Tim_OC_Init_Para.OCNPolarity = OUTPUT_POL_ACTIVE_HIGH;
Tim_OC_Init_Para.OCFastMode = OUTPUT_FAST_MODE_DISABLE;
Tim_OC_Init_Para.Pulse = (TIM_Handler.Init.Period + 1) / 2; //---Duty = 50%
HAL_TIMER_Output_Config(TIM_Handler.Instance, &Tim_OC_Init_Para, TIM_CHANNEL_1);
GPIO_InitTypeDef GPIOD_Handle;
GPIOD_Handle.Pin = GPIO_PIN_1; //---TIM15_CH1N
GPIOD_Handle.Mode = GPIO_MODE_AF_PP;
GPIOD_Handle.Pull = GPIO_NOPULL;
GPIOD_Handle.Alternate = GPIO_FUNCTION_1;
HAL_GPIO_Init(GPIOD, &GPIOD_Handle);
MultiTimerStart(&BUZZER_MultiTimer, 100, BUZZER_MultiTimerCallback, "BUZZER");
}
/*********************************END OF FILE**********************************/
/*******************************************************************************
* @file KEY.c
* @author King
* @version V1.00
* @date 21-Oct-2022
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __KEY_C__
/* Includes ------------------------------------------------------------------*/
#include "KEY.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
MultiTimer KEY_MultiTimer;
/* Private variables ---------------------------------------------------------*/
struct Button btnKEY;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
uint8_t read_pin_level(uint8_t pin_id)
{
return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_Handler(void *btn)
{
Button *key = btn; char *KEY_NAME[1] = {"USER_PB"};
switch(key->event)
{
case PRESS_DOWN:
printf("\r\n%s : PRESS_DOWN", KEY_NAME[key->button_id]);
break;
case PRESS_UP:
printf("\r\n%s : PRESS_UP", KEY_NAME[key->button_id]);
break;
case SINGLE_CLICK:
printf("\r\n%s : SINGLE_CLICK", KEY_NAME[key->button_id]);
break;
case DOUBLE_CLICK:
printf("\r\n%s : DOUBLE_CLICK", KEY_NAME[key->button_id]);
break;
case LONG_PRESS_START:
printf("\r\n%s : LONG_PRESS_START", KEY_NAME[key->button_id]);
break;
case LONG_PRESS_HOLD:
printf("\r\n%s : LONG_PRESS_HOLD", KEY_NAME[key->button_id]);
break;
default:
break;
}
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_RepeatHandler(void *btn)
{
Button *key = btn; char *KEY_NAME[1] = {"USER_PB"};
printf("\r\n%s : PRESS_REPEAT", KEY_NAME[key->button_id]);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_MultiTimerCallback(MultiTimer *timer, void *userData)
{
button_ticks();
MultiTimerStart(&KEY_MultiTimer, 5, KEY_MultiTimerCallback, "KEY");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void KEY_Init(void)
{
GPIO_InitTypeDef GPIOC_Handle;
GPIOC_Handle.Pin = GPIO_PIN_13;
GPIOC_Handle.Mode = GPIO_MODE_INPUT;
GPIOC_Handle.Pull = GPIO_PULLUP;
GPIOC_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOC, &GPIOC_Handle);
button_init( &btnKEY, read_pin_level, GPIO_PIN_CLEAR, 0);
button_attach(&btnKEY, SINGLE_CLICK, KEY_Handler);
button_start( &btnKEY);
MultiTimerStart(&KEY_MultiTimer, 5, KEY_MultiTimerCallback, "KEY");
}
/*********************************END OF FILE**********************************/
/*******************************************************************************
* @file LED.c
* @author King
* @version V1.00
* @date 21-Oct-2022
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __LED_C__
/* Includes ------------------------------------------------------------------*/
#include "LED.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
MultiTimer LED_MultiTimer;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void LED_MultiTimerCallback(MultiTimer *timer, void *userData)
{
if(!HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_3))
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
}
else
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_CLEAR);
}
MultiTimerStart(&LED_MultiTimer, 250, LED_MultiTimerCallback, "LED");
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void LED_Init(void)
{
GPIO_InitTypeDef GPIOD_Handle;
GPIOD_Handle.Pin = GPIO_PIN_3;
GPIOD_Handle.Mode = GPIO_MODE_OUTPUT_PP;
GPIOD_Handle.Pull = GPIO_PULLUP;
GPIOD_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOD, &GPIOD_Handle);
MultiTimerStart(&LED_MultiTimer, 250, LED_MultiTimerCallback, "LED");
}
/*********************************END OF FILE**********************************/
/*******************************************************************************
* @file shell_port.c
* @author King
* @version V1.00
* @date 21-Oct-2022
* @brief ......
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#define __SHELL_PORT_C__
/* Includes ------------------------------------------------------------------*/
#include "shell_port.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
SHELL_TypeDef shell;
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef UART2_Handle;
/* Private variables ---------------------------------------------------------*/
uint8_t UART2_RxData = 0;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported function prototypes ----------------------------------------------*/
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void shellPortWrite(const char ch)
{
UART2->DR = (uint8_t)ch;
while((UART2->FR & UART_FR_BUSY));
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void shellPortInit(void)
{
UART2_Handle.Instance = UART2;
UART2_Handle.Init.BaudRate = 115200;
UART2_Handle.Init.WordLength = UART_WORDLENGTH_8B;
UART2_Handle.Init.StopBits = UART_STOPBITS_1;
UART2_Handle.Init.Parity = UART_PARITY_NONE;
UART2_Handle.Init.Mode = UART_MODE_TX_RX_DEBUG;
UART2_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
HAL_UART_Init(&UART2_Handle);
HAL_UART_Enable_Disable_FIFO(&UART2_Handle, FUNC_ENABLE);
shell.write = shellPortWrite;
shellInit(&shell);
HAL_UART_Receive_IT(&UART2_Handle, &UART2_RxData, 1, UART_RX_FIFO_1_2);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void UART2_IRQHandler(void)
{
HAL_UART_IRQHandler(&UART2_Handle);
}
/*******************************************************************************
* @brief
* @param
* @retval
* @attention
*******************************************************************************/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == UART2)
{
shellHandler(&shell, UART2_RxData);
HAL_UART_Receive_IT(&UART2_Handle, &UART2_RxData, 1, UART_RX_FIFO_1_2);
}
}
/******************* (C) COPYRIGHT 2022 *************************END OF FILE***/
运行结果
程序下载后,按下RESET复位运行,此时LED灯间隔闪烁;需要注意的是由于底板的KEY与扩展板SHT30传感器共用了,所以在测试KEY功能时,不要连接扩展板;在连接扩展板时,复位运行会听到BUZZER蜂鸣器响3声;除此之外通过调试下载工具枚举的COM口,将MCU的信息打印到PC机的终端软件上,如下图所示:
相关总结和疑问
1、在KEIL配置下载后复位运行的选项后,当程序下载完成后,芯片并没有复位运行,而是需要手动去按一下RESET按键才能复位运行;发现在板载的调试下载器部分与MCU有一个NRST排针跳帽没有短接,但短接之后,下载程序后依旧没有自动复位运行,这是怎么回事呢?
软件工程源代码
|