文章目录
- 一、简介
- 1.工具的目的
- 2. 更新点
- 下个更新
- 三、配置文件
一、简介
1.工具的目的
① 可视化选择IO口功能。
② 自由配置IO支持的功能。
③ 适用各类MCU,方便移植和开发。
④ 功能配置和裁剪(选项-syscfg-待完成–需要适配keil语法有些麻烦)
ls_syscfg.h 目的更快的配置外设参数和功能,跟专注业务代码开发。
2. 更新点
① 新增宏名称
② 注释
③ 回调函数(中断回调、按键回调)
④ 变更排序方式,按GPIOA、B、C排序
⑤ 回调函数会在ls_gpio.h生成弱函数,可由外部重新定义。
__WEAK void key0 (key_event_t event) {
switch (event) {
case KEY_EVENT_SINGLE_CLICK:
break;
}
}
下个更新
- syscfg功能
- 相应例程更新
- 基本完成功能
更多基于工具的项目开源。
三、配置文件
/********************************************************************************
* @file ls_gpio.h
* @author jianqiang.xue
* @version V1.1.0
* @date 2022-12-11
* @brief 业务类--GPIO定义
********************************************************************************/
#ifndef __LS_GPIO_H
#define __LS_GPIO_H
#define LS_IO_VERSION "CX32L003_通用模板"
#include "cx32l003_hal.h"
#include "cx32l003_hal_def.h"
#include "ls_syscfg.h"
#include "bsp_gpio.h"
#include "bsp_exti.h"
/* GPIO_EXTI_INT_EDGE; // 中断类型 边沿触发
GPIO_EXTI_INT_LOWFALL; // 下降沿触发中断
GPIO_EXTI_INT_HIGHRISE; // 上升沿触发中断
GPIO_EXTI_INT_FALLRISE; // 下降沿上升沿都触发中断
GPIO_EXTI_INT_LEVEL; // 中断类型 电平触发
GPIO_EXTI_INT_LOWFALL; // 低电平触发中断
GPIO_EXTI_INT_HIGHRISE; // 高电平触发中断 */
#define GPIO_CLK(GPIO) (RCC_HCLKEN_##GPIO##CKEN)
#define GPIO_APBx (0)
/************************************GPIO***********************************/
#define LS_IO_NUM (16)
/* Public Struct -------------------------------------------------------------*/
union io_support_t {
uint64_t val; // 对外值
struct {
uint64_t swo : 1;
uint64_t adc0_ch0 : 1;
uint64_t adc0_ch1 : 1;
uint64_t adc0_ch2 : 1;
uint64_t adc0_ch3 : 1;
uint64_t adc0_ch4 : 1;
uint64_t adc0_ch5 : 1;
uint64_t adc0_ch6 : 1;
uint64_t adc1_ch0 : 1;
uint64_t adc1_ch1 : 1;
uint64_t adc1_ch2 : 1;
uint64_t adc1_ch3 : 1;
uint64_t adc1_ch4 : 1;
uint64_t adc1_ch5 : 1;
uint64_t adc1_ch6 : 1;
uint64_t tim1_ch1 : 1; // PWM0
uint64_t tim1_ch2 : 1; // PWM1
uint64_t tim1_ch3 : 1; // PWM2
uint64_t tim1_ch4 : 1; // PWM3
uint64_t tim2_ch1 : 1; // PWM4
uint64_t tim2_ch2 : 1; // PWM5
uint64_t tim2_ch3 : 1; // PWM6
uint64_t tim2_ch4 : 1; // PWM7
uint64_t uart0_tx : 1;
uint64_t uart0_rx : 1;
uint64_t uart1_tx : 1;
uint64_t uart1_rx : 1;
uint64_t uart2_tx : 1;
uint64_t uart2_rx : 1;
uint64_t i2c0_scl : 1; // 硬件IIC
uint64_t i2c0_sda : 1;
uint64_t i2c1_scl : 1;
uint64_t i2c1_sda : 1;
uint64_t i2c_scl_soft : 1; // 软件IIC
uint64_t i2c_sda_soft : 1;
uint64_t spi0_clk : 1;
uint64_t spi0_mosi : 1;
uint64_t spi0_miso : 1;
uint64_t spi0_nss : 1;
uint64_t spi1_clk : 1;
uint64_t spi1_mosi : 1;
uint64_t spi1_miso : 1;
uint64_t spi1_nss : 1;
#if !LS_IO_EXTEND_SUPPORT
uint64_t reserve : 7;
#endif
} bit;
};
typedef enum {
IO_TYPE_IN_NULL = 0, // 无上下拉
IO_TYPE_IN_PULLUP, // 输入,上拉
IO_TYPE_IN_PULLDOWN, // 输入,下拉
IO_TYPE_OUT_PP, // 默认输出 推免输出
IO_TYPE_OUT_OD, // 开漏输出
// 中断输入
IO_TYPE_INIRQ_LEVEL_LOW, // 电平触发-低电平
IO_TYPE_INIRQ_LEVEL_HIGH, // 电平触发-高电平
IO_TYPE_INIRQ_EDGE_LOWFALL, // 边沿触发-下降沿
IO_TYPE_INIRQ_EDGE_HIGHRISE, // 边沿触发-上降沿
IO_TYPE_INIRQ_EDGE_FALLRISE, // 边沿触发-任意电平变化
IO_TYPE_SWO, // 默认烧录脚
IO_TYPE_KEY, // 按键
IO_TYPE_MATRIX_KEY_X, // 矩阵键盘X轴,会注册到软定时器中扫描
IO_TYPE_MATRIX_KEY_Y, // 矩阵键盘Y轴,会注册到软定时器中扫描
IO_TYPE_W2812X,
IO_TYPE_ADC0_CH0, // ADC0_0
IO_TYPE_ADC0_CH1, // ADC0_1
IO_TYPE_ADC0_CH2, // ADC0_2
IO_TYPE_ADC0_CH3, // ADC0_3
IO_TYPE_ADC0_CH4, // ADC0_4
IO_TYPE_ADC0_CH5, // ADC0_5
IO_TYPE_ADC0_CH6, // ADC0_6
IO_TYPE_ADC1_CH0, // ADC1_0
IO_TYPE_ADC1_CH1, // ADC1_1
IO_TYPE_ADC1_CH2, // ADC1_2
IO_TYPE_ADC1_CH3, // ADC1_3
IO_TYPE_ADC1_CH4, // ADC1_4
IO_TYPE_ADC1_CH5, // ADC1_5
IO_TYPE_ADC1_CH6, // ADC1_5
IO_TYPE_TIM1_CH1, // PWM0
IO_TYPE_TIM1_CH2, // PWM1
IO_TYPE_TIM1_CH3, // PWM2
IO_TYPE_TIM1_CH4, // PWM3
IO_TYPE_TIM2_CH1, // PWM4
IO_TYPE_TIM2_CH2, // PWM5
IO_TYPE_TIM2_CH3, // PWM6
IO_TYPE_TIM2_CH4, // PWM7
IO_TYPE_UART0_TX, // 串口0,TX
IO_TYPE_UART0_RX, // 串口0,RX
IO_TYPE_UART1_TX, // 串口1,TX
IO_TYPE_UART1_RX, // 串口1,RX
IO_TYPE_UART2_TX, // 串口2,TX
IO_TYPE_UART2_RX, // 串口2,RX
IO_TYPE_I2C0_SCL, // 硬件IIC0
IO_TYPE_I2C0_SDA, // 硬件IIC0
IO_TYPE_I2C1_SCL, // 硬件IIC1
IO_TYPE_I2C1_SDA, // 硬件IIC1
IO_TYPE_I2C_SCL_SOFT, // 软件IIC
IO_TYPE_I2C_SDA_SOFT, // 软件IIC
IO_TYPE_SPI0_CLK,
IO_TYPE_SPI0_MOSI,
IO_TYPE_SPI0_MISO,
IO_TYPE_SPI0_NSS,
IO_TYPE_SPI1_CLK,
IO_TYPE_SPI1_MOSI,
IO_TYPE_SPI1_MISO,
IO_TYPE_SPI1_NSS,
IO_TYPE_MAX,
} io_type_t;
extern const union io_support_t g_io_support[LS_IO_NUM];
extern const io_type_t g_io_default_type[LS_IO_NUM];
extern const bsp_gpio_t g_io_cfg[LS_IO_NUM];
#ifdef LS_IO
const bsp_gpio_t g_io_cfg[LS_IO_NUM] = {
{(GPIOA), (GPIO_PIN_1), GPIO_CLK(GPIOA), (0), (GPIOA_IRQn)},
{(GPIOA), (GPIO_PIN_2), GPIO_CLK(GPIOA), (0), (GPIOA_IRQn)},
{(GPIOA), (GPIO_PIN_3), GPIO_CLK(GPIOA), (0), (GPIOA_IRQn)},
{(GPIOB), (GPIO_PIN_4), GPIO_CLK(GPIOB), (0), (GPIOB_IRQn)},
{(GPIOB), (GPIO_PIN_5), GPIO_CLK(GPIOB), (0), (GPIOB_IRQn)},
{(GPIOC), (GPIO_PIN_3), GPIO_CLK(GPIOC), (0), (GPIOC_IRQn)},
{(GPIOC), (GPIO_PIN_4), GPIO_CLK(GPIOC), (0), (GPIOC_IRQn)},
{(GPIOC), (GPIO_PIN_5), GPIO_CLK(GPIOC), (0), (GPIOC_IRQn)},
{(GPIOC), (GPIO_PIN_6), GPIO_CLK(GPIOC), (0), (GPIOC_IRQn)},
{(GPIOC), (GPIO_PIN_7), GPIO_CLK(GPIOC), (0), (GPIOC_IRQn)},
{(GPIOD), (GPIO_PIN_1), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
{(GPIOD), (GPIO_PIN_2), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
{(GPIOD), (GPIO_PIN_3), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
{(GPIOD), (GPIO_PIN_4), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
{(GPIOD), (GPIO_PIN_5), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
{(GPIOD), (GPIO_PIN_6), GPIO_CLK(GPIOD), (0), (GPIOD_IRQn)},
};
const union io_support_t g_io_support[LS_IO_NUM] = {
{0x45000000}, {0x4022920000}, {0x4010200000}, {0x4020800000}, {0x841080000},
{0x42220004}, {0x24440008}, {0x808000000}, {0x1010408002}, {0x4100001},
{0x2000001}, {0x808010010}, {0x1100020}, {0x888000}, {0x2022C00040},
{0x1045110080},
};
const io_type_t g_io_default_type[LS_IO_NUM] = {
IO_TYPE_UART0_RX, IO_TYPE_UART0_TX, IO_TYPE_KEY, IO_TYPE_KEY, IO_TYPE_INIRQ_EDGE_FALLRISE,
IO_TYPE_INIRQ_EDGE_HIGHRISE, IO_TYPE_TIM1_CH4, IO_TYPE_TIM2_CH1, IO_TYPE_ADC0_CH0, IO_TYPE_SWO,
IO_TYPE_SWO, IO_TYPE_TIM1_CH2, IO_TYPE_TIM2_CH2, IO_TYPE_TIM1_CH1, IO_TYPE_TIM2_CH4,
IO_TYPE_ADC0_CH6,
};
__weak void app_io_exti_cb(void *gpiox, uint16_t gpio_pin) {
// [中断回调START]
if (gpiox == GPIOB && gpio_pin == 5) {
222()
}
if (gpiox == GPIOC && gpio_pin == 3) {
666()
}
// [中断回调END]
}
#endif
// [按键扫描START]
__WEAK const ls_key_cb_t ls_key_cb_list[] = {
{0, 555},
{1, 666},
{0xFF, NULL} // 必须有
};
__WEAK void 555 (key_event_t event) {
switch (event) {
case KEY_EVENT_SINGLE_CLICK:
break;
}
}
__WEAK void 666 (key_event_t event) {
switch (event) {
case KEY_EVENT_SINGLE_CLICK:
break;
}
}
// [按键扫描END]
// [引脚宏名称START]
#define 33 (2) // 222
#define 1 (3) // 2
#define 4 (4) // 5
#define 6 (5) // 7
// [引脚宏名称END]
#endif // __LS_GPIO_H
gitee
1.0版本
- ADC
AT+ADC0_CFG?
AT+ADC0_TIMER_MS=300
AT+ADC0_START
AT+ADC0_STOP
AT+GET_ADC0_VAL?
AT+ADC0_CFG=?
AT+ADC0_CFG=1,10
#include "bsp_adc.h"
extern bsp_adc_cfg_t g_adc0_cfg;
extern os_timer_id OS_TIMER_ID(adc0_collection);
extern uint32_t g_adc0_timer_ms;
static int atcmd_adc0_cfg_help(atcmd_pack_t *pack) {
char buff[60] = {0};
snprintf(buff, 60, "AT+ADC0_CFG=<clk_sel(0-7)>,<sample_num(0-255)>\r\n");
strcat(buff, AT_OK);
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_get_adc0_cfg(atcmd_pack_t *pack) {
char buff[30] = {0};
snprintf(buff, 30, "%d,%d\r\n", g_adc0_cfg.clk_sel, g_adc0_cfg.sample_num);
strcat(buff, AT_OK);
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_set_adc0_cfg(atcmd_pack_t *pack) {
uint32_t argc[2] = {0};
char buff[20] = {0};
pack->argc = sscanf((char*)(pack->data), "%d,%d", &argc[0], &argc[1]);
if (pack->argc != 2) {
strcat(buff, AT_ARGCERR);
pack->reply((uint8_t*)buff, strlen(buff));
return -2;
}
bsp_adc0_deinit();
g_adc0_cfg.clk_sel = (uint8_t)argc[0];
g_adc0_cfg.sample_num = argc[1];
if (bsp_adc0_init(&g_adc0_cfg)) {
strcat(buff, AT_ERROR);
} else {
strcat(buff, AT_OK);
}
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_set_adc0_timer_ms(atcmd_pack_t *pack) {
uint32_t argc[1] = {0};
uint8_t buff[40] = {0};
pack->argc = sscanf((char*)(pack->data), "%d", &argc[0]);
if (pack->argc != 1) return -1;
// 先停止周期任务,并将任务全部取消。
OS_TIMER_STOP(adc0_collection);
if (argc[0]) {
if (os_timer_restart(OS_TIMER_ID(adc0_collection), argc[0]) != OS_OK)
strcat((char*)buff, "adc0 timer restart fail");
g_adc0_timer_ms = argc[0];
} else {
if (os_timer_stop(OS_TIMER_ID(adc0_collection)) != OS_OK)
strcat((char*)buff, "adc0 timer stop fail");
}
strcat((char*)buff, AT_OK);
pack->reply(buff, strlen((char*)buff));
return 0;
}
static int atcmd_adc0_start(atcmd_pack_t *pack) {
uint8_t buff[40] = {0};
if (os_timer_restart(OS_TIMER_ID(adc0_collection), g_adc0_timer_ms) != OS_OK)
strcat((char*)buff, "adc0 timer restart fail");
strcat((char*)buff, AT_OK);
pack->reply(buff, strlen((char*)buff));
return 0;
}
static int atcmd_adc0_stop(atcmd_pack_t *pack) {
uint8_t buff[40] = {0};
if (os_timer_stop(OS_TIMER_ID(adc0_collection)) != OS_OK)
strcat((char*)buff, "adc0 timer stop fail");
strcat((char*)buff, AT_OK);
pack->reply(buff, strlen((char*)buff));
return 0;
}
static int atcmd_get_adc0_val(atcmd_pack_t *pack) {
uint8_t buff[60] = {0};
snprintf((char *)buff, 60, "%d,%d,%d,%d,%d,%d,%d,%d\r\n",
bsp_adc0_get_ch_val(0), bsp_adc0_get_ch_val(1), bsp_adc0_get_ch_val(2), bsp_adc0_get_ch_val(3),
bsp_adc0_get_ch_val(4), bsp_adc0_get_ch_val(5), bsp_adc0_get_ch_val(6), bsp_adc0_get_ch_val(7));
strcat((char*)buff, AT_OK);
pack->reply(buff, strlen((char*)buff));
return 0;
}
static int atcmd_get_adc0_designated_val(atcmd_pack_t *pack) {
uint32_t argc[1];
uint8_t buff[30] = {0};
pack->argc = sscanf((char*)(pack->data), "%d", &argc[0]);
if (pack->argc != 1) return -1;
snprintf((char *)buff, 30, "%d", bsp_adc0_get_ch_val(argc[0]));
strcat((char*)buff, AT_OK);
pack->reply(buff, strlen((char*)buff));
return 0;
}
ATCMD_INIT("AT+ADC0_TIMER_MS=", atcmd_set_adc0_timer_ms);
ATCMD_INIT("AT+ADC0_START", atcmd_adc0_start);
ATCMD_INIT("AT+ADC0_STOP", atcmd_adc0_stop);
ATCMD_INIT("AT+GET_ADC0_VAL=", atcmd_get_adc0_designated_val);
ATCMD_INIT("AT+GET_ADC0_VAL?", atcmd_get_adc0_val);
ATCMD_INIT("AT+ADC0_CFG=?", atcmd_adc0_cfg_help);
ATCMD_INIT("AT+ADC0_CFG=", atcmd_set_adc0_cfg);
ATCMD_INIT("AT+ADC0_CFG?", atcmd_get_adc0_cfg);
- PWM
AT+PWM=0,10
AT+TIM1_CFG=7,99,0
AT+TIM1_CFG=?
AT+PWM_TASK=
#include "bsp_pwm.h"
#include "bsp_tim.h"
#if LS_TIM1_EN
extern bsp_tim_cfg_t g_tim1_cfg;
static int atcmd_set_tim1_cfg_help(atcmd_pack_t *pack) {
char buff[85] = {0};
snprintf(buff, 60, "AT+TIM1_CFG=<prescaler(0-65534)>,<period(0-65534)>,<level_logic(0-1)>\r\n");
strcat(buff, AT_OK);
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_set_tim1_cfg(atcmd_pack_t *pack) {
uint32_t argc[3] = {0};
char buff[10] = {0};
pack->argc = sscanf((char*)(pack->data), "%d,%d,%d", &argc[0], &argc[1], &argc[2]);
if (pack->argc != 3) {
strcat(buff, AT_ARGCERR);
pack->reply((uint8_t*)buff, strlen(buff));
return -2;
}
bsp_tim1_pwm_deinit();
g_tim1_cfg.prescaler = (uint16_t)argc[0];
g_tim1_cfg.period = (uint16_t)argc[1];
g_tim1_cfg.level_logic = (uint8_t)argc[2];
if (bsp_tim1_pwm_init(&g_tim1_cfg)) {
strcat(buff, AT_ERROR);
} else {
strcat(buff, AT_OK);
}
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
ATCMD_INIT("AT+TIM1_CFG=?", atcmd_set_tim1_cfg_help);
ATCMD_INIT("AT+TIM1_CFG=", atcmd_set_tim1_cfg);
#endif
#if LS_TIM2_EN
extern bsp_tim_cfg_t g_tim2_cfg;
static int atcmd_set_tim2_cfg_help(atcmd_pack_t *pack) {
char buff[85] = {0};
snprintf(buff, 60, "AT+TIM2_CFG=<prescaler(0-65534)>,<period(0-65534)>,<level_logic(0-1)>\r\n");
strcat(buff, AT_OK);
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_set_tim2_cfg(atcmd_pack_t *pack) {
uint32_t argc[3] = {0};
char buff[10] = {0};
pack->argc = sscanf((char*)(pack->data), "%d,%d,%d", &argc[0], &argc[1], &argc[2]);
if (pack->argc != 3) {
strcat(buff, AT_ARGCERR);
pack->reply((uint8_t*)buff, strlen(buff));
return -2;
}
bsp_tim2_pwm_deinit();
g_tim2_cfg.prescaler = (uint16_t)argc[0];
g_tim2_cfg.period = (uint16_t)argc[1];
g_tim2_cfg.level_logic = (uint8_t)argc[2];
if (bsp_tim2_pwm_init(&g_tim2_cfg)) {
strcat(buff, AT_ERROR);
} else {
strcat(buff, AT_OK);
}
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
ATCMD_INIT("AT+TIM2_CFG=?", atcmd_set_tim2_cfg_help);
ATCMD_INIT("AT+TIM2_CFG=", atcmd_set_tim2_cfg);
#endif
static int atcmd_set_pwm_val_help(atcmd_pack_t *pack) {
char buff[60] = {0};
snprintf(buff, 60, "AT+PWM=<ch(0-7)>,<val(0-65535)>\r\n");
strcat(buff, AT_OK);
pack->reply((uint8_t*)buff, strlen(buff));
return 0;
}
static int atcmd_set_pwm_val(atcmd_pack_t *pack) {
uint32_t argc[2] = {0};
uint8_t buff[10] = {0};
pack->argc = sscanf((char*)(pack->data), "%d,%d", &argc[0], &argc[1]);
if (pack->argc != 2) {
strcat((char*)buff, AT_ARGCERR);
pack->reply(buff, strlen((char*)buff));
return -2;
}
if (bsp_pwm_set_pulse(argc[0], argc[1])) {
strcat((char*)buff, AT_ERROR);
} else {
strcat((char*)buff, AT_OK);
}
pack->reply(buff, strlen((char*)buff));
return 0;
}
ATCMD_INIT("AT+PWM=?", atcmd_set_pwm_val_help);
ATCMD_INIT("AT+PWM=", atcmd_set_pwm_val);