初始化参数结构
APP驱动的初始化参数主要包括:
- 外设ID定义app_xxx_id_t,如I2C的ID0、ID1、ID2等。
- 外设角色定义app_xxx_role_t,如I2C的APP_I2C_ROLE_MASTER、APP_I2C_ROLE_SLAVE。
- 外设DMA配置定义app_xxx_dma_cfg_t,如I2C需要配置DMA读写使用的DMA实例和通道。
- 初始化外设GPIO口定义app_xxx_pin_cfg_t,如GPIO上下拉、输入输出、MUX选择。
- 外设数据传输类参数定义xxx_init_t,如SPI的一次传输数据大小、时钟极性、时钟相位、采样频率等。
以I2C为例,初始化参数结构体定义如下:
/**
* @brief I2C parameters structure definition
*/
typedef struct
{
app_i2c_id_t id; /**< specified I2C module ID. */
app_i2c_role_t role; /**< specified the role of I2C. */
app_i2c_pin_cfg_t pin_cfg; /**< the pin configuration information for
the specified I2C module. */
app_i2c_dma_cfg_t dma_cfg; /**< I2C operate mode. */
i2c_init_t init; /**< I2C communication parameters. */
i2c_env_t i2c_dev;
} app_i2c_params_t;
app_xxx_id_t成员
ID成员用于外设拥有多个实例的情况,例如GR5526的I2C外设和UART外设均有6个实例,这里的ID设置为0 ~ 5的任意数字,表明使用对应数字的外设实例。当某些外设如DSPI、OSPI、GPU、DC等只有一个实例时,参数结构体则无此ID成员变量。
app_xxx_pin_cfg_t成员
GPIO引脚设置成员用于需要对IO引脚进行操作的外设,例如UART、SPI、I2C等。该成员由以下四个子成员组成:
- 引脚类型,如普通GPIOA/GPIOB/GPIOC/NORMAL,不掉电AON_GPIO和模拟数字混合MSIOA。仅GR551x支持NORMAL类型,可以被GPIOA/GPIOB替换使用。对于GR551x系列芯片,推荐使用GPIOA/GPIOB引脚。
- 引脚映射,通过配置app_io_mux_t,对照Datasheet中的Pin Mux表,可以将IO配置为对应功能的引脚,以GR5526为例,选择GPIOA4、MUX3可以将GPIOA4配置成UART3的发送功能引脚。
- 引脚编号,除NORMAL类型,该编号可选0 ~ 15任意一个数字。NORMAL类型的引脚编号为0 ~ 31。
- 引脚模式,可以配置引脚的内部上拉、下拉和浮空。
对于不需要配置GPIO的外设,如TIMER、DUAL_TIMER等,则无需定义该成员变量。
以UART为例,app_uart_pin_cfg_t成员定义如下:
typedef struct
{
app_io_type_t type; /**< Specifies the type of UART IO. */
app_io_mux_t mux; /**< Specifies the Peripheral to be
connected to the selected pins. */
uint32_t pin; /**< Specifies the IO pins to be configured.
This parameter can be any value of
@ref GR55xx_pins. */
app_io_pull_t pull; /**< Specifies the Pull-up or Pull-Down
activation for the selected pins. */
}app_uart_pin_t;
/**
* @brief UART pins config Structures
*/
typedef struct
{
app_uart_pin_t tx; /**< Set the configuration of UART TX pin. */
app_uart_pin_t rx; /**< Set the configuration of UART RX pin. */
app_uart_pin_t cts; /**< Set the configuration of UART CTS pin. */
app_uart_pin_t rts; /**< Set the configuration of UART RTS pin. */
}app_uart_pin_cfg_t;
xxx_init_t成员
外设参数配置成员用于配置每个外设特有的参数,以PWM为例,pwm_init_t成员定义如下:
typedef struct
{
uint32_t mode; /**< Specifies the PWM output mode state.
This parameter can be a value of
@ref PWM_Mode */
uint32_t align; /**< Specifies the PWM alignment mode with
three channels
This parameter can be a value
of @ref PWM_Alignment_Mode */
uint32_t freq; /**< Specifies the PWM frequency.
This parameter must be a number between
0 ~ SystemFreq/2 (max = 32Mhz).*/
uint32_t bperiod; /**< Specifies the PWM breath period in breath mode.
Unit: ms.
This parameter must be a number between
0 ~ 0xFFFFFFFF/SystemFreq*1000. */
uint32_t hperiod; /**< Specifies the PWM hold period in breath mode.
Unit: ms.
This parameter must be a number between
0 ~ 0xFFFFFF/SystemFreq*1000. */
uint32_t bstoplvl; /**< Specifies the PWM io level when stop.
This parameter can be a value
of @ref PWM_STOP_LVL */
pwm_channel_init_t channel_a; /**< Specifies the configuration parameters
of channel A. */
pwm_channel_init_t channel_b; /**< Specifies the configuration parameters
of channel B. */
pwm_channel_init_t channel_c; /**< Specifies the configuration parameters
of channel C. */
} pwm_init_t;