STM32F103操作RN8209G

RN8209和STM32都是常用芯片,可能第一次用的人会需要花一点时间来上手,但总体上来说并不太难

实际使用的STM32芯片是STM32F103RC,针对不同的目标芯片,可能需要调整一下代码中的细节部分


(1)首先是SPI初始化

void SPI_RN8209_Init(void)
{
SPI_InitTypeDef  SPI_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable SPI2 and GPIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE);

  /*!< SPI Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
   
  /*!< Configure RN8209 pins: SCK */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /*!< Configure RN8209 pins: MISO */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /*!< Configure RN8209 pins: MOSI */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /*!< Configure RN8209 CS pin */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Deselect RN8209: Chip Select high */
 SPI_VI_CS0_HIGH();

  /* SPI2 configuration */
  // W25X16: data input on the DIO pin is sampled on the rising edge of the CLK. 
  // Data on the DO and DIO pins are clocked out on the falling edge of CLK.
  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_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* Enable SPI2  */
  SPI_Cmd(SPI2, ENABLE);
}

RN8209对SPI的信号有一定要求,具体请参考数据手册,按要求设置STM32的SPI接口


(2)向RN8209发送单个字节,并读回返回的单字节数据

通过SPI发送单个字节是RN8209的基础操作,后面的读写寄存器均基于这个函数

u8 SPI_RN8209_SendByte(u8 byte)
{
  /* Loop while DR register in not emplty */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  SPI_I2S_SendData(SPI2, byte);
  /* Wait to receive a byte */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
  /* Return the byte read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI2);
}


(3)写RN8209寄存器。因为有多种操作,所以发送的字节长度,用户可选

u32 SPI_RN8209_WriteRegisters(uint8_t* pDataBuf, uint8_t nNum, uint8_t nAddr)
{
uint8_t i,temp;

SPI_VI_CS0_LOW();
  
        /* Send "RDID " instruction */
temp = nAddr|0x80;
        SPI_RN8209_SendByte(temp);

for(i=0;i<nNum;i++)
{
SPI_RN8209_SendByte(pDataBuf[i]);
}

SPI_VI_CS0_HIGH();
        return 0;

}


(4)读RN8209寄存器。读寄存器有1-4字节,四种长度,所以做了一个分类

u32 SPI_RN8209_ReadRegisters(uint8_t nNum, uint8_t nAddr)
{
u32 Temp = 0;
uint8_t Temp0 = 0, Temp1 = 0, Temp2 = 0, Temp3 = 0;

SPI_VI_CS0_LOW();
  
      /* Send "RDID " instruction */
      SPI_RN8209_SendByte(nAddr);

switch(nNum)
{
case 1:
/* Read a byte from the FLASH */
Temp0 = SPI_RN8209_SendByte(Dummy_Byte);
Temp = Temp0;
break;
case 2:
Temp0 = SPI_RN8209_SendByte(Dummy_Byte);
Temp1 = SPI_RN8209_SendByte(Dummy_Byte);
Temp = (Temp0 << 8) | Temp1;
break;
case 3:
Temp0 = SPI_RN8209_SendByte(Dummy_Byte);
Temp1 = SPI_RN8209_SendByte(Dummy_Byte);
Temp2 = SPI_RN8209_SendByte(Dummy_Byte);
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
break;
case 4:
Temp0 = SPI_RN8209_SendByte(Dummy_Byte);
Temp1 = SPI_RN8209_SendByte(Dummy_Byte);
Temp2 = SPI_RN8209_SendByte(Dummy_Byte);
Temp3 = SPI_RN8209_SendByte(Dummy_Byte);
Temp = (Temp0 << 24) | (Temp1 << 16) | (Temp2 << 8) | Temp3;
break;
default:
break;
}
SPI_VI_CS0_HIGH();
        return Temp;
}


有了以上这四个函数做基础操作函数,然后就可以根据手册,对RN8209芯片进行各种操作了。

祝使用愉快,^_^


另外:默认的电流测量通道增益是16,可以通过写SYSCON(00H)寄存器来修改

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页