帮我写一段基于stm32发了03c8t6的SPI2的nrf24l01的底层驱动代码
时间: 2023-03-25 18:03:08 浏览: 156
好的,我可以回答这个问题。以下是基于stm32发了03c8t6的SPI2的nrf24l01的底层驱动代码:
#include "stm32f10x.h"
#define NRF24L01_CE_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_8)
#define NRF24L01_CE_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_8)
#define NRF24L01_CSN_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_9)
#define NRF24L01_CSN_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_9)
void nrf24l01_spi_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t nrf24l01_spi_rw(uint8_t data)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
void nrf24l01_write_reg(uint8_t reg, uint8_t value)
{
NRF24L01_CSN_LOW();
nrf24l01_spi_rw(reg | x20);
nrf24l01_spi_rw(value);
NRF24L01_CSN_HIGH();
}
uint8_t nrf24l01_read_reg(uint8_t reg)
{
uint8_t value;
NRF24L01_CSN_LOW();
nrf24l01_spi_rw(reg);
value = nrf24l01_spi_rw(xff);
NRF24L01_CSN_HIGH();
return value;
}
void nrf24l01_write_buf(uint8_t reg, uint8_t *buf, uint8_t len)
{
NRF24L01_CSN_LOW();
nrf24l01_spi_rw(reg | x20);
for (uint8_t i = ; i < len; i++) {
nrf24l01_spi_rw(buf[i]);
}
NRF24L01_CSN_HIGH();
}
void nrf24l01_read_buf(uint8_t reg, uint8_t *buf, uint8_t len)
{
NRF24L01_CSN_LOW();
nrf24l01_spi_rw(reg);
for (uint8_t i = ; i < len; i++) {
buf[i] = nrf24l01_spi_rw(xff);
}
NRF24L01_CSN_HIGH();
}
void nrf24l01_init(void)
{
nrf24l01_spi_init();
NRF24L01_CE_LOW();
NRF24L01_CSN_HIGH();
nrf24l01_write_reg(x00, xa);
nrf24l01_write_reg(x01, x30);
nrf24l01_write_reg(x02, x03);
nrf24l01_write_reg(x03, x03);
nrf24l01_write_reg(x04, x03);
nrf24l01_write_reg(x05, x02);
nrf24l01_write_reg(x06, xf);
nrf24l01_write_reg(x07, xc);
nrf24l01_write_reg(x1c, x00);
nrf24l01_write_reg(x1d, x00);
nrf24l01_write_reg(x1e, x00);
nrf24l01_write_reg(x1f, x00);
}
void nrf24l01_tx_packet(uint8_t *buf, uint8_t len)
{
NRF24L01_CE_LOW();
nrf24l01_write_buf(xa, buf, len);
NRF24L01_CE_HIGH();
}
void nrf24l01_rx_packet(uint8_t *buf, uint8_t len)
{
NRF24L01_CE_HIGH();
nrf24l01_read_buf(x61, buf, len);
nrf24l01_write_reg(x07, x70);
}