简体   繁体   English

如何通过 MISO 引脚使用 stm32f103(主)获取 nRF24l01(从)寄存器数据

[英]How do I take nRF24l01(slave) Register data using stm32f103 (master) through MISO pin

I'm trying to make a custom library for nRF24l01 for a stm32f103 target device, and I am writing code for a primary TX device.我正在尝试为 stm32f103 目标设备为 nRF24l01 制作自定义库,并且正在为主要 TX 设备编写代码。 And here I'm trying to read the register contents of nRF by sending the R_REGISTER command along with the address I am looking for, but I'm not able to figure out how to read the data after the R_REGISTER command is transmitted.在这里,我试图通过发送 R_REGISTER 命令以及我正在寻找的地址来读取 nRF 的寄存器内容,但我无法弄清楚在 R_REGISTER 命令发送后如何读取数据。

And i'm using the standard stm32f10x.h header file which comes along with startup files on Kiel uVision5.我正在使用标准 stm32f10x.h header 文件,该文件与 Kiel uVision5 上的启动文件一起提供。

Here are the configurations,以下是配置,

clock setup时钟设置

RCC->CR |= RCC_CR_HSION;    //HSI on
while( !(RCC_CR_HSIRDY & (RCC->CR)) );  //wait till its ready
//clocks for peripherals 
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; //enable clock forport A
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; //enable clock for alternate functions
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //enable clock for SPI1

GPIO setup these are my custom-defined functions, they just work fine GPIO 设置这些是我自定义的功能,它们工作正常

//GPIO pin setup as alternate function
pin_mode(IOPA, GPIOA, 7, op_50MHz, op_afpp);    //MOSI pin as GPIO alternate_pin can run upto 50MHz
pin_mode(IOPA, GPIOA, 6, ip, ip_pupd);  //MISO pin as GPIO alternate_pin can run upto 50MHz
pin_mode(IOPA, GPIOA, 5, op_50MHz, op_afpp);    //SCK pin as GPIO alternate_pin can run upto 50MHz
pin_mode(IOPA, GPIOA, 4, op_50MHz, op_gppp);    //CS pin as GPIO general_puspose_pin can run upto 50MHz

SPI setup SPI 设置

SPI1->CR1 |= SPI_CR1_MSTR;  //master mode
SPI1->CR1 |= SPI_CR1_BR_0 | SPI_CR1_BR_1 | SPI_CR1_BR_2;    //at 571Kbps, max 31Mbps
SPI1->CR1 |= SPI_CR1_SSI;   //Software slave management enabled
SPI1->CR2 |= SPI_CR2_SSOE;  //SS o/p enable
SPI1->CR1 |= SPI_CR1_SPE;   //turn on the SPI

Im stuck here我被困在这里

uint8_t SPI_read_uint8_t(uint8_t addr){
    uint8_t reg_val;
    //sending the read command first along with address where we are reading from
    delay_us(50);
    digital_writepin(GPIOA, 4, LOW);
    SPI1->DR = (R_REGISTER | addr);  //sending the R_REGISTER command along with address
    while( (SPI1->SR) & (SPI_SR_BSY) );

    //please help here, how do I read the Register data from MISO pin  

SPI 读取操作应如下所示

uint8_t spi_read_write(uint8_t data)
{
    while(SPI1 -> SR & SPI_SR_RXNE) (void)*(volatile uint8_t *)&SPI1 -> DR;   //clean the FIFO
    *(volatile uint8_t *)&SPI1 -> DR = data;
    while(!(SPI1 -> SR & SPI_SR_RXNE));
    return *(volatile uint8_t *)&SPI1 -> DR;
}

uint8_t youroperation(uint8_t command, uint16_t *data)
{
    uint8_t status;
    setCSLine();
    status = spi_read_write(command);
    *data = spi_read_write(0);
    *data |= ((uint16_t)spi_read_write(0)) << 8;
    clearCSLine();   
    return status
}

youroperation will return Sn status register command parameter means Cn command bits data will contain the Dx data bits youroperation将返回Sn状态寄存器command参数表示Cn命令位data将包含 Dx 数据位

To read data from SPI slave you need to send dummy bytes because master provides the clock for the slave.要从 SPI 从机读取数据,您需要发送虚拟字节,因为主机为从机提供时钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM