GD32F303驱动LLCC68的代码
时间: 2023-08-05 19:04:11 浏览: 230
51单片机驱动SX1262(LLCC68)LoRa驱动
以下是使用 GD32F303 板载的 SPI 接口驱动 LLCC68 模块的代码示例:
```c
#include "gd32f30x.h"
#include "systick.h"
#include "spi.h"
#define LLCC68_SPI SPI0
#define LLCC68_SPI_CLK RCU_SPI0
#define LLCC68_SPI_CS_PIN GPIO_PIN_5
#define LLCC68_SPI_CS_GPIO_PORT GPIOA
#define LLCC68_SPI_CS_GPIO_CLK RCU_GPIOA
#define LLCC68_RESET_PIN GPIO_PIN_6
#define LLCC68_RESET_GPIO_PORT GPIOA
#define LLCC68_RESET_GPIO_CLK RCU_GPIOA
#define LLCC68_SPI_CS_LOW() gpio_bit_reset(LLCC68_SPI_CS_GPIO_PORT, LLCC68_SPI_CS_PIN)
#define LLCC68_SPI_CS_HIGH() gpio_bit_set(LLCC68_SPI_CS_GPIO_PORT, LLCC68_SPI_CS_PIN)
void llcc68_hw_init(void)
{
/* Enable LLCC68 reset GPIO clock */
rcu_periph_clock_enable(LLCC68_RESET_GPIO_CLK);
/* Configure LLCC68 reset pin */
gpio_init(LLCC68_RESET_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LLCC68_RESET_PIN);
gpio_bit_reset(LLCC68_RESET_GPIO_PORT, LLCC68_RESET_PIN);
/* Enable LLCC68 SPI GPIO and SPI clock */
rcu_periph_clock_enable(LLCC68_SPI_CS_GPIO_CLK);
rcu_periph_clock_enable(LLCC68_SPI_CLK);
/* Configure LLCC68 SPI CS pin */
gpio_init(LLCC68_SPI_CS_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LLCC68_SPI_CS_PIN);
gpio_bit_set(LLCC68_SPI_CS_GPIO_PORT, LLCC68_SPI_CS_PIN);
/* Configure SPI parameters */
spi_parameter_struct spi_init_struct;
spi_struct_para_init(&spi_init_struct);
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_2;
spi_init(LLCC68_SPI, &spi_init_struct);
/* Enable SPI */
spi_enable(LLCC68_SPI);
}
void llcc68_hw_reset(void)
{
gpio_bit_set(LLCC68_RESET_GPIO_PORT, LLCC68_RESET_PIN);
delay_1ms(10);
gpio_bit_reset(LLCC68_RESET_GPIO_PORT, LLCC68_RESET_PIN);
delay_1ms(10);
gpio_bit_set(LLCC68_RESET_GPIO_PORT, LLCC68_RESET_PIN);
delay_1ms(10);
}
void llcc68_hw_spi_write(uint8_t *buffer, uint16_t size)
{
LLCC68_SPI_CS_LOW();
for (uint16_t i = 0; i < size; i++) {
spi_i2s_data_transmit(LLCC68_SPI, buffer[i]);
while (RESET == spi_i2s_flag_get(LLCC68_SPI, SPI_FLAG_TBE));
while (RESET == spi_i2s_flag_get(LLCC68_SPI, SPI_FLAG_RBNE));
spi_i2s_data_receive(LLCC68_SPI);
}
LLCC68_SPI_CS_HIGH();
}
void llcc68_hw_spi_read(uint8_t *buffer, uint16_t size)
{
LLCC68_SPI_CS_LOW();
for (uint16_t i = 0; i < size; i++) {
spi_i2s_data_transmit(LLCC68_SPI, 0x00);
while (RESET == spi_i2s_flag_get(LLCC68_SPI, SPI_FLAG_TBE));
while (RESET == spi_i2s_flag_get(LLCC68_SPI, SPI_FLAG_RBNE));
buffer[i] = spi_i2s_data_receive(LLCC68_SPI);
}
LLCC68_SPI_CS_HIGH();
}
```
这里使用了 GD32F303 板载的 SPI 接口来驱动 LLCC68 模块,同时也提供了 LLCC68 的复位功能。在 `llcc68_hw_init()` 中,我们对 SPI 接口进行了初始化,并且对 GPIO 进行了配置。`llcc68_hw_reset()` 用于执行 LLCC68 模块的复位操作。最后,`llcc68_hw_spi_write()` 和 `llcc68_hw_spi_read()` 分别用于向 LLCC68 模块写入数据和读取数据。注意,在这个示例中,SPI 接口与 LLCC68 模块之间使用了软件 NSS 信号。如果你的模块需要使用硬件 NSS 信号,请根据需要进行修改。
阅读全文