bsp_spi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /********************** 头文件 *************************/
  2. #include "bsp_spi.h"
  3. #define SPI_INSTANCE 0 /**< SPI instance index. */
  4. static volatile bool spi_xfer_done; //SPI数据传输完成标志
  5. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
  6. static uint8_t spi_tx_buf[256]; /**< TX buffer. */
  7. static uint8_t spi_rx_buf[256]; /**< RX buffer. */
  8. /**
  9. * @brief SPI user event handler.
  10. * @param event
  11. */
  12. void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
  13. void * p_context)
  14. {
  15. spi_xfer_done = true;
  16. }
  17. void SPI_Init(void)
  18. {
  19. // nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG(SPI_INSTANCE);
  20. // spi_config.ss_pin = SPI_CS_PIN;
  21. // APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
  22. nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  23. spi_config.ss_pin = SPI_SS_PIN;
  24. spi_config.miso_pin = SPI_MISO_PIN;
  25. spi_config.mosi_pin = SPI_MOSI_PIN;
  26. spi_config.sck_pin = SPI_SCK_PIN;
  27. APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
  28. }
  29. /*****************************************************************************
  30. ** 描 述:读出一个字节
  31. ** 入 参:无
  32. ** 返回值:读出的数据
  33. ******************************************************************************/
  34. uint8_t SpiFlash_ReadOneByte(void)
  35. {
  36. uint8_t len = 1;
  37. spi_tx_buf[0] = 0xFF;
  38. spi_xfer_done = false;
  39. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, len, spi_rx_buf, len));
  40. while(!spi_xfer_done)
  41. ;
  42. return (spi_rx_buf[0]);
  43. }
  44. /*****************************************************************************
  45. ** 描 述:写入一个字节
  46. ** 入 参:Dat:待写入的数据
  47. ** 返回值:无
  48. ******************************************************************************/
  49. void SpiFlash_WriteOneByte(uint8_t Dat)
  50. {
  51. uint8_t len = 1;
  52. spi_tx_buf[0] = Dat;
  53. spi_xfer_done = false;
  54. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, len, spi_rx_buf, len));
  55. while(!spi_xfer_done)
  56. ;
  57. }
  58. /*****************************************************************************
  59. ** 描 述:写入命令
  60. ** 入 参:*CMD:指向待写入的命令
  61. ** 返回值:RET_SUCCESS
  62. ******************************************************************************/
  63. uint8_t SpiFlash_Write_CMD(uint8_t *CMD)
  64. {
  65. uint8_t len = 3;
  66. spi_tx_buf[0] = *CMD;
  67. spi_tx_buf[1] = *(CMD+1);
  68. spi_tx_buf[2] = *(CMD+2);
  69. spi_xfer_done = false;
  70. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, len, spi_rx_buf, len));
  71. while(!spi_xfer_done)
  72. ;
  73. return RET_SUCCESS;
  74. }
  75. /*****************************************************************************
  76. ** 描 述:写使能
  77. ** 入 参:无
  78. ** 返回值:无
  79. ******************************************************************************/
  80. void SpiFlash_Write_Enable(void)
  81. {
  82. spi_xfer_done = false;
  83. SpiFlash_WriteOneByte(SPIFlash_WriteEnable_CMD);
  84. while(!spi_xfer_done)
  85. ;
  86. }
  87. /*****************************************************************************
  88. ** 描 述:擦除扇区,W25Q128FVSIG最小的擦除单位是扇区
  89. ** 入 参:Block_Num:块号
  90. ** Sector_Number:扇区号
  91. ** 返回值:
  92. ******************************************************************************/
  93. void SPIFlash_Erase_Sector(uint8_t Block_Num,uint8_t Sector_Number)
  94. {
  95. SpiFlash_Write_Enable();
  96. spi_tx_buf[0] = SPIFlash_SecErase_CMD;
  97. spi_tx_buf[1] = Block_Num;
  98. spi_tx_buf[2] = Sector_Number<<4;
  99. spi_tx_buf[3] = 0x00;
  100. spi_xfer_done = false;
  101. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, 4, spi_rx_buf, 4));
  102. while(!spi_xfer_done)
  103. ;
  104. nrf_delay_ms(10); //每次擦除数据都要延时等待写入结束
  105. return ;
  106. }
  107. /*****************************************************************************
  108. ** 描 述:向指定的地址写入数据
  109. ** *pBuffer:指向待写入的数据
  110. ** WriteAddr:写入的起始地址
  111. ** WriteBytesNum:读出的字节数
  112. ** 返回值:RET_SUCCESS
  113. ******************************************************************************/
  114. uint8_t SpiFlash_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t WriteBytesNum)
  115. {
  116. uint8_t len;
  117. SpiFlash_Write_Enable();
  118. spi_tx_buf[0] = SPIFlash_PageProgram_CMD;
  119. spi_tx_buf[1] = (uint8_t)((WriteAddr&0x00ff0000)>>16);
  120. spi_tx_buf[2] = (uint8_t)((WriteAddr&0x0000ff00)>>8);
  121. spi_tx_buf[3] = (uint8_t)WriteAddr;
  122. memcpy(&spi_tx_buf[4],pBuffer,WriteBytesNum);
  123. len = WriteBytesNum + 4;
  124. spi_xfer_done = false;
  125. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, len, spi_rx_buf, 0));
  126. while(!spi_xfer_done)
  127. ;
  128. return RET_SUCCESS;
  129. }
  130. /*****************************************************************************
  131. ** 描 述:从指定的地址读出指定长度的数据
  132. ** 入 参:pBuffer:指向存放读出数据的首地址
  133. ** ReadAddr:待读出数据的起始地址
  134. ** ReadBytesNum:读出的字节数
  135. ** 返回值:
  136. ******************************************************************************/
  137. uint8_t SpiFlash_Read(uint8_t *pBuffer,uint32_t ReadAddr,uint32_t ReadBytesNum)
  138. {
  139. uint8_t len;
  140. SpiFlash_Write_Enable();
  141. spi_tx_buf[0] = SPIFlash_ReadData_CMD;
  142. spi_tx_buf[1] = (uint8_t)((ReadAddr&0x00ff0000)>>16);
  143. spi_tx_buf[2] = (uint8_t)((ReadAddr&0x0000ff00)>>8);
  144. spi_tx_buf[3] = (uint8_t)ReadAddr;
  145. len = ReadBytesNum + 4;
  146. spi_xfer_done = false;
  147. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, spi_tx_buf, len, spi_rx_buf, len));
  148. while(!spi_xfer_done)
  149. ;
  150. memcpy(pBuffer,&spi_rx_buf[4],ReadBytesNum);
  151. return RET_SUCCESS;
  152. }