Air001基于Keil环境点灯和调试输出工程配置
- 📌官方环境搭建教程介绍:
https://wiki.luatos.com/chips/air001/Air001-MDK.html
- 🔖本人使用的是基于HAL库环境搭建的。
- 📍SDK开发资源链接:
https://gitee.com/openLuat/luatos-soc-air001
✨这里就不介绍具体的环境搭建详细教程了,可以参考官方给出的教程自行搭建,这里主要讲一些外设模块的使能配置,文件架构以及引脚功能复用这一块的内容。
- ✨本人是十分不喜欢直接使用SDK中的例程资源作为模板来使用的,虽然官方在所提供资源中提供了相关模版,但是并适合个人开发者使用习惯。里面的例程资源,大多都是根据他们自己出的开发板而展开的。例如下面的这些源文件代码,在很多例程中都被引用,而作为个人开发者使用,这些都是一些没必要的包含。
- 🍁Keil工程必要的文件架构图参考:
🛠相关外设使能配置
- 🌿启用对应的外设宏。
找到头文件
air001xx_hal_conf.h
,启用对应的外设宏定义。如果下面没有添加条件编译的头文件,那么可以直接跳过。
air001xx_hal_conf.h
条件编译的宏:
#ifdef HAL_MODULE_ENABLED
#include "air001xx_hal.h"
#endif /* HAL_MODULE_ENABLED */
#ifdef HAL_RCC_MODULE_ENABLED
#include "air001xx_hal_rcc.h"
#endif /* HAL_RCC_MODULE_ENABLED */
#ifdef HAL_EXTI_MODULE_ENABLED
#include "air001xx_hal_exti.h"
#endif /* HAL_EXTI_MODULE_ENABLED */
#ifdef HAL_GPIO_MODULE_ENABLED
#include "air001xx_hal_gpio.h"
#endif /* HAL_GPIO_MODULE_ENABLED */
#ifdef HAL_CORTEX_MODULE_ENABLED
#include "air001xx_hal_cortex.h"
#endif /* HAL_CORTEX_MODULE_ENABLED */
#ifdef HAL_DMA_MODULE_ENABLED
#include "air001xx_hal_dma.h"
#endif /* HAL_DMA_MODULE_ENABLED */
#ifdef HAL_ADC_MODULE_ENABLED
#include "air001xx_hal_adc.h"
#endif /* HAL_ADC_MODULE_ENABLED */
#ifdef HAL_CRC_MODULE_ENABLED
#include "air001xx_hal_crc.h"
#endif /* HAL_CRC_MODULE_ENABLED */
#ifdef HAL_COMP_MODULE_ENABLED
#include "air001xx_hal_comp.h"
#endif /* HAL_COMP_MODULE_ENABLED */
#ifdef HAL_FLASH_MODULE_ENABLED
#include "air001xx_hal_flash.h"
#endif /* HAL_FLASH_MODULE_ENABLED */
#ifdef HAL_I2C_MODULE_ENABLED
#include "air001xx_hal_i2c.h"
#endif /* HAL_I2C_MODULE_ENABLED */
#ifdef HAL_IWDG_MODULE_ENABLED
#include "air001xx_hal_iwdg.h"
#endif /* HAL_IWDG_MODULE_ENABLED */
#ifdef HAL_PWR_MODULE_ENABLED
#include "air001xx_hal_pwr.h"
#endif /* HAL_PWR_MODULE_ENABLED */
#ifdef HAL_RTC_MODULE_ENABLED
#include "air001xx_hal_rtc.h"
#endif /* HAL_RTC_MODULE_ENABLED */
#ifdef HAL_SPI_MODULE_ENABLED
#include "air001xx_hal_spi.h"
#endif /* HAL_SPI_MODULE_ENABLED */
#ifdef HAL_TIM_MODULE_ENABLED
#include "air001xx_hal_tim.h"
#endif /* HAL_TIM_MODULE_ENABLED */
#ifdef HAL_LPTIM_MODULE_ENABLED
#include "air001xx_hal_lptim.h"
#endif /* HAL_LPTIM_MODULE_ENABLED */
#ifdef HAL_UART_MODULE_ENABLED
#include "air001xx_hal_uart.h"
#endif /* HAL_UART_MODULE_ENABLED */
#ifdef HAL_WWDG_MODULE_ENABLED
#include "air001xx_hal_wwdg.h"
#endif /* HAL_WWDG_MODULE_ENABLED */
#ifdef HAL_LED_MODULE_ENABLED
#include "air001xx_hal_led.h"
#endif /* HAL_LED_MODULE_ENABLED */
#ifdef HAL_USART_MODULE_ENABLED
#include "air001xx_hal_usart.h"
#endif /* HAL_USART_MODULE_ENABLED */
- 🌿添加对应的外设源文件。
📓引脚复用
- 🌿端口复用功能列表:
- 🌿引脚复用对应的代码定义的头文件:
air001xx_hal_gpio_ex.h
/*------------------------- AIR001xx ------------------------*/
#if (defined(AIR00103PRE)|| defined(AIR00130PRE) || defined(AIR001_DEV))
/**
* @brief AF 0 selection
*/
#define GPIO_AF0_SWJ ((uint8_t)0x00) /*!< SWJ (SWD) Alternate Function mapping */
#define GPIO_AF0_SPI1 ((uint8_t)0x00) /*!< SPI1 Alternate Function mapping */
#define GPIO_AF0_SPI2 ((uint8_t)0x00) /*!< SPI2 Alternate Function mapping */
#define GPIO_AF0_TIM14 ((uint8_t)0x00) /*!< TIM14 Alternate Function mapping */
#define GPIO_AF0_USART1 ((uint8_t)0x00) /*!< USART1 Alternate Function mapping */
/**
* @brief AF 1 selection
*/
#define GPIO_AF1_IR ((uint8_t)0x01) /*!< IR Alternate Function mapping */
#define GPIO_AF1_SPI2 ((uint8_t)0x01) /*!< SPI2 Alternate Function mapping */
#define GPIO_AF1_TIM1 ((uint8_t)0x01) /*!< TIM1 Alternate Function mapping */
#define GPIO_AF1_TIM3 ((uint8_t)0x01) /*!< TIM3 Alternate Function mapping */
#define GPIO_AF1_USART1 ((uint8_t)0x01) /*!< USART1 Alternate Function mapping */
/**
* @brief AF 2 selection
*/
#define GPIO_AF2_SPI2 ((uint8_t)0x02) /*!< SPI2 Alternate Function mapping */
#define GPIO_AF2_TIM1 ((uint8_t)0x02) /*!< TIM1 Alternate Function mapping */
#define GPIO_AF2_TIM14 ((uint8_t)0x02) /*!< TIM14 Alternate Function mapping */
#define GPIO_AF2_TIM16 ((uint8_t)0x02) /*!< TIM16 Alternate Function mapping */
#define GPIO_AF2_TIM17 ((uint8_t)0x02) /*!< TIM17 Alternate Function mapping */
/**
* @brief AF 3 selection
*/
#define GPIO_AF3_LED ((uint8_t)0x03U) /*!< AF3: LED Alternate Function mapping */
#define GPIO_AF3_USART1 ((uint8_t)0x03U) /*!< AF3: USART1 Alternate Function mapping */
#define GPIO_AF3_USART2 ((uint8_t)0x03U) /*!< AF3: USART2 Alternate Function mapping */
#define GPIO_AF3_SPI2 ((uint8_t)0x03U) /*!< AF3: SPI2 Alternate Function mapping */
/**
* @brief AF 4 selection
*/
#define GPIO_AF4_TIM14 ((uint8_t)0x04) /*!< TIM14 Alternate Function mapping */
#define GPIO_AF4_USART2 ((uint8_t)0x04) /*!< USART2 Alternate Function mapping */
/**
* @brief AF 5 selection
*/
#define GPIO_AF5_LPTIM ((uint8_t)0x05) /*!< LPTIM1 Alternate Function mapping */
#define GPIO_AF5_USART2 ((uint8_t)0x05) /*!< USART2 Alternate Function mapping */
#define GPIO_AF5_EVENTOUT ((uint8_t)0x05) /*!< EVENTOUT Alternate Function mapping */
#define GPIO_AF5_TIM16 ((uint8_t)0x05) /*!< TIM16 Alternate Function mapping */
#define GPIO_AF5_TIM17 ((uint8_t)0x05) /*!< TIM17 Alternate Function mapping */
#define GPIO_AF5_MCO ((uint8_t)0x05) /*!< MCO Alternate Function mapping */
/**
* @brief AF 6 selection
*/
#define GPIO_AF6_I2C ((uint8_t)0x06) /*!< I2C1 Alternate Function mapping */
#define GPIO_AF6_LED ((uint8_t)0x06U) /*!< AF6: LED Alternate Function mapping */
#define GPIO_AF6_MCO ((uint8_t)0x06) /*!< MCO Alternate Function mapping */
#define GPIO_AF6_EVENTOUT ((uint8_t)0x06U) /*!< AF6: EVENTOUT Alternate Function mapping */
/**
* @brief AF 7 selection
*/
#define GPIO_AF7_EVENTOUT ((uint8_t)0x07) /*!< EVENTOUT Alternate Function mapping */
#define GPIO_AF7_COMP1 ((uint8_t)0x07U) /*!< AF7: COMP1 Alternate Function mapping */
#define GPIO_AF7_COMP2 ((uint8_t)0x07U) /*!< AF7: COMP2 Alternate Function mapping */
/**
* @brief AF 8 selection
*/
#define GPIO_AF8_USART1 ((uint8_t)0x08U) /*!< AF8: USART1 Alternate Function mapping */
/**
* @brief AF 9 selection
*/
#define GPIO_AF9_USART2 ((uint8_t)0x09) /*!< USART2 Alternate Function mapping */
/**
* @brief AF 10 selection
*/
#define GPIO_AF10_SPI1 ((uint8_t)0x0A) /*!< SPI2 Alternate Function mapping */
/**
* @brief AF 11 selection
*/
#define GPIO_AF11_SPI2 ((uint8_t)0x0B) /*!< SPI2 Alternate Function mapping */
/**
* @brief AF 12 selection
*/
#define GPIO_AF12_I2C ((uint8_t)0x0C) /*!< I2C1 Alternate Function mapping */
/**
* @brief AF 13 selection
*/
#define GPIO_AF13_TIM1 ((uint8_t)0x0D) /*!< TIM1 Alternate Function mapping */
#define GPIO_AF13_TIM3 ((uint8_t)0x0D) /*!< TIM3 Alternate Function mapping */
#define GPIO_AF13_TIM14 ((uint8_t)0x0D) /*!< TIM14 Alternate Function mapping */
#define GPIO_AF13_TIM17 ((uint8_t)0x0D) /*!< TIM17 Alternate Function mapping */
/**
* @brief AF 14 selection
*/
#define GPIO_AF14_TIM1 ((uint8_t)0x0E) /*!< TIM1 Alternate Function mapping */
/**
* @brief AF 15 selection
*/
#define GPIO_AF15_RTCOUT ((uint8_t)0x0fU)
#define GPIO_AF15_MCO ((uint8_t)0x0FU) /*!< MCO Alternate Function mapping */
#define GPIO_AF15_IR ((uint8_t)0x0FU) /*!< IR Alternate Function mapping */
#define IS_GPIO_AF(AF) ((AF) <= (uint8_t)0x0f)
#elif defined(AIR00102APRE)
/**
* @brief AF 0 selection
*/
#define GPIO_AF0_SWJ ((uint8_t)0x00) /*!< SWJ (SWD) Alternate Function mapping */
#define GPIO_AF0_SPI1 ((uint8_t)0x00) /*!< SPI1 Alternate Function mapping */
#define GPIO_AF0_USART1 ((uint8_t)0x00) /*!< USART1 Alternate Function mapping */
/**
🔨串口配置
- 🌿Air001有2个串口,
- 🔖如果想将PA2和PA3配置为串口1:
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
- 🔖如果想将PA2和PA3配置为串口2:
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
- 🌿usart.h文件
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.h
* @brief This file contains all the function prototypes for
* the usart.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USART_H__
#define __USART_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#define USART1_ENABLED USART1 //使能串口1,
//#define USART2_ENABLED USART2 //使能串口2
#define USARTx_BAUDRATE 115200
#define USARTx_RX_GPIO_PORT GPIOA
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define USARTx_RX_PIN GPIO_PIN_3
#define USARTx_TX_GPIO_PORT GPIOA
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define USARTx_TX_PIN GPIO_PIN_2
#ifdef USART1_ENABLED
#define USARTx_TX_AF GPIO_AF1_USART1 //USART2:GPIO_AF4_USART2 USART1:GPIO_AF1_USART1
#define USARTx_RX_AF GPIO_AF1_USART1 //USART2:GPIO_AF4_USART2 USART1:GPIO_AF1_USART1
#define USARTx_IRQHandler USART2_IRQHandler
#define USARTx_IRQ USART2_IRQn
#endif
#ifdef USART2_ENABLED
#define USARTx_TX_AF GPIO_AF4_USART2 //USART2:GPIO_AF4_USART2 USART1:GPIO_AF1_USART1
#define USARTx_RX_AF GPIO_AF4_USART2 //USART2:GPIO_AF4_USART2 USART1:GPIO_AF1_USART1
#define USARTx_IRQHandler USART2_IRQHandler
#define USARTx_IRQ USART2_IRQn
#endif
extern UART_HandleTypeDef UartHandle;
void MX_USART_UART_Init(void);
#ifdef __cplusplus
}
#endif
#endif /* __USART_H__ */
- 🌿
usart.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usart.c
* @brief This file provides code for the configuration
* of the USART instances.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usart.h"
#include "stdio.h"
UART_HandleTypeDef UartHandle;
void USART_CLK_ENABLE(void)
{
__IO uint32_t tmpreg = 0x00U;
SET_BIT(RCC->APBENR1, RCC_APBENR1_USART2EN);
/* RCC外围时钟启用后的延迟 */
tmpreg = READ_BIT(RCC->APBENR1, RCC_APBENR1_USART2EN);
UNUSED(tmpreg);
}
void MX_USART_UART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_CLK_ENABLE();
UartHandle.Instance = USART1; //使能串口:USART1/USART2
UartHandle.Init.BaudRate = USARTx_BAUDRATE;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
Error_Handler();
}
USARTx_RX_GPIO_CLK_ENABLE();//使能串口引脚对应的端口时钟
USARTx_TX_GPIO_CLK_ENABLE();
/**USART GPIO Configuration
PA2 ------> USART1/2-->TX
PA3 ------> USART1/2-->RX
*/
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
/* ENABLE NVIC */
HAL_NVIC_SetPriority(USARTx_IRQ, 0, 1);
HAL_NVIC_EnableIRQ(USARTx_IRQ);
}
void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// if(uartHandle->Instance == USART1)
// {
/* USARTx clock enable 时钟使能 */
__HAL_RCC_USART1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = USARTx_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USARTx_TX_AF;
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = USARTx_RX_PIN;
GPIO_InitStruct.Alternate = USARTx_RX_AF;
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
/* ENABLE NVIC */
HAL_NVIC_SetPriority(USARTx_IRQ, 0, 1);
HAL_NVIC_EnableIRQ(USARTx_IRQ);
// }
}
void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{
if(uartHandle->Instance == USART1)
{
/* Peripheral clock disable */
__HAL_RCC_USART1_CLK_DISABLE();
/**USART GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
}
}
/*可调用printf*/
int fputc(int ch, FILE* f)
{
/*&huart1指的是串口1,如果用别的串口就修改数字*/
HAL_UART_Transmit(&UartHandle , (uint8_t*)&ch , 1 , 1000);
return ch;
}
#if 0 //默认不启用串口接收
//重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE* f)
{
uint8_t ch = 0;
HAL_UART_Receive(&UartHandle, &ch, 1, 1000);
return ch;
}
#endif
/* USER CODE END 1 */
🔨GPIO输入输出配置
gpio.c
/* Includes ------------------------------------------------------------------*/
#include "gpio.h"
/*----------------------------------------------------------------------------*/
/* Configure GPIO */
/*----------------------------------------------------------------------------*/
/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
// __HAL_RCC_GPIOA_CLK_ENABLE(); /* 使能GPIOA时钟 */
__HAL_RCC_GPIOB_CLK_ENABLE(); /* 使能GPIOB时钟 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3; /* 使能对应引脚 */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; /* 推挽输出 */
GPIO_InitStruct.Pull = GPIO_PULLUP; /* 使能上拉 */
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; /* GPIO速度 */
/* GPIO初始化 */
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
- 🌿
gpio.h
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
void MX_GPIO_Init(void);
#ifdef __cplusplus
}
#endif
#endif /*__ GPIO_H__ */
📚个人配置的点灯+串口调试输出模版工程
- 🔖说明工程中配置了ASF转BIN文件调用的指令,方便串口烧录。📍具体参考《Air001基于Keil环境开发,使用airisp串口命令行烧录》
- 🌴工程架构“
链接:https://pan.baidu.com/s/1iTcc5wyVwj_4L3U7FAteqA
提取码:9mei