hal_spi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "hal_spi.h"
  2. #include "hc32_ll_dma.h"
  3. #include "hc32_ll_aos.h"
  4. #include "hc32_ll_interrupts.h"
  5. void bsp_spi_init(void);
  6. void bsp_spi_deinit(void);
  7. void bsp_spi_active(void);
  8. void bsp_spi_inactive(void);
  9. int32_t bsp_spi_trans(const uint8_t *pu8TxBuf, uint32_t u32Size);
  10. int32_t bsp_spi_receive(uint8_t *pu8RxBuf, uint32_t u32Size);
  11. /*******************************************************************************
  12. * Local variable definitions ('static')
  13. ******************************************************************************/
  14. stc_w25qxx_ll_t m_stcW25qxxLL = {
  15. .Delay = DDL_DelayMS,
  16. .Init = bsp_spi_init,
  17. .DeInit = bsp_spi_deinit,
  18. .Active = bsp_spi_active,
  19. .Inactive = bsp_spi_inactive,
  20. .Trans = bsp_spi_trans,
  21. .Receive = bsp_spi_receive,
  22. };
  23. void bsp_spi_active(void)
  24. {
  25. BSP_SPI_CS_ACTIVE();
  26. }
  27. void bsp_spi_inactive(void)
  28. {
  29. BSP_SPI_CS_INACTIVE();
  30. }
  31. void bsp_spi_init(void)
  32. {
  33. stc_gpio_init_t stcGpioInit;
  34. stc_spi_init_t stcSpiInit;
  35. stc_spi_delay_t stcSpiDelayCfg;
  36. // (void)GPIO_StructInit(&stcGpioInit);
  37. // stcGpioInit.u16PinDrv = PIN_HIGH_DRV;
  38. // (void)GPIO_Init(BSP_SPI_SCK_PORT, BSP_SPI_SCK_PIN, &stcGpioInit);
  39. // (void)GPIO_Init(BSP_SPI_MOSI_PORT, BSP_SPI_MOSI_PIN, &stcGpioInit);
  40. // (void)GPIO_Init(BSP_SPI_MISO_PORT, BSP_SPI_MISO_PIN, &stcGpioInit);
  41. //
  42. // GPIO_SetDebugPort(GPIO_PIN_TDI, DISABLE);
  43. // stcGpioInit.u16PinDir = PIN_DIR_OUT;
  44. // stcGpioInit.u16PinState = PIN_STAT_SET;
  45. // (void)GPIO_Init(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN, &stcGpioInit);
  46. GPIO_SetDebugPort(GPIO_PIN_TDI, DISABLE);
  47. GPIO_SetFunc(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN, BSP_SPI_CS_PIN_FUNC);
  48. GPIO_SetFunc(BSP_SPI_SCK_PORT, BSP_SPI_SCK_PIN, BSP_SPI_SCK_PIN_FUNC);
  49. GPIO_SetFunc(BSP_SPI_MOSI_PORT, BSP_SPI_MOSI_PIN, BSP_SPI_MOSI_PIN_FUNC);
  50. GPIO_SetFunc(BSP_SPI_MISO_PORT, BSP_SPI_MISO_PIN, BSP_SPI_MISO_PIN_FUNC);
  51. /* Clear initialize structure */
  52. (void)SPI_StructInit(&stcSpiInit);
  53. (void)SPI_DelayStructInit(&stcSpiDelayCfg);
  54. /* Configure peripheral clock */
  55. FCG_Fcg1PeriphClockCmd(BSP_SPI_PERIPH_CLK, ENABLE);
  56. /* SPI De-initialize */
  57. SPI_DeInit(BSP_SPI_UNIT);
  58. /* Configuration SPI structure */
  59. stcSpiInit.u32WireMode = SPI_4_WIRE;
  60. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  61. stcSpiInit.u32MasterSlave = SPI_MASTER;
  62. stcSpiInit.u32ModeFaultDetect = SPI_MD_FAULT_DETECT_DISABLE;
  63. stcSpiInit.u32Parity = SPI_PARITY_INVD;
  64. stcSpiInit.u32SpiMode = SPI_MD_0;
  65. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV4;
  66. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  67. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  68. (void)SPI_Init(BSP_SPI_UNIT, &stcSpiInit);
  69. stcSpiDelayCfg.u32IntervalDelay = SPI_INTERVAL_TIME_8SCK;
  70. stcSpiDelayCfg.u32ReleaseDelay = SPI_RELEASE_TIME_8SCK;
  71. stcSpiDelayCfg.u32SetupDelay = SPI_SETUP_TIME_1SCK;
  72. (void)SPI_DelayTimeConfig(BSP_SPI_UNIT, &stcSpiDelayCfg);
  73. SPI_Cmd(BSP_SPI_UNIT, ENABLE);
  74. // stc_spi_init_t stcSpiInit;
  75. // /* Configure Port */
  76. // GPIO_SetFunc(BSP_SPI_CS_PORT, BSP_SPI_CS_PIN, BSP_SPI_CS_PIN_FUNC);
  77. // GPIO_SetFunc(BSP_SPI_SCK_PORT, BSP_SPI_SCK_PIN, BSP_SPI_SCK_PIN_FUNC);
  78. // GPIO_SetFunc(BSP_SPI_MOSI_PORT, BSP_SPI_MOSI_PIN, BSP_SPI_MOSI_PIN_FUNC);
  79. // GPIO_SetFunc(BSP_SPI_MISO_PORT, BSP_SPI_MISO_PIN, BSP_SPI_MISO_PIN_FUNC);
  80. // /* Configuration SPI */
  81. // FCG_Fcg1PeriphClockCmd(BSP_SPI_PERIPH_CLK, ENABLE);
  82. // SPI_StructInit(&stcSpiInit);
  83. // stcSpiInit.u32WireMode = SPI_4_WIRE;
  84. // stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  85. // stcSpiInit.u32MasterSlave = SPI_MASTER;
  86. // stcSpiInit.u32Parity = SPI_PARITY_INVD;
  87. // stcSpiInit.u32SpiMode = SPI_MD_0;
  88. // stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV96;
  89. // stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  90. // stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  91. // stcSpiInit.u32FrameLevel = SPI_1_FRAME;
  92. // (void)SPI_Init(BSP_SPI_UNIT, &stcSpiInit);
  93. // SPI_Cmd(BSP_SPI_UNIT, ENABLE);
  94. }
  95. void bsp_spi_deinit(void)
  96. {
  97. /* SPI De-initialize */
  98. SPI_DeInit(BSP_SPI_UNIT);
  99. }
  100. int32_t bsp_spi_trans(const uint8_t *pu8TxBuf, uint32_t u32Size)
  101. {
  102. return SPI_Trans(BSP_SPI_UNIT, pu8TxBuf, u32Size, BSP_SPI_TIMEOUT);
  103. }
  104. int32_t bsp_spi_receive(uint8_t *pu8RxBuf, uint32_t u32Size)
  105. {
  106. return SPI_Receive(BSP_SPI_UNIT, pu8RxBuf, u32Size, BSP_SPI_TIMEOUT);
  107. }
  108. void bsp_w25qxx_init(void)
  109. {
  110. (void)W25QXX_Init(&m_stcW25qxxLL);
  111. }
  112. void BSP_W25QXX_DeInit(void)
  113. {
  114. (void)W25QXX_DeInit(&m_stcW25qxxLL);
  115. }
  116. int32_t BSP_W25QXX_Write(uint32_t u32Addr, const uint8_t *pu8Data, uint32_t u32NumByteToWrite)
  117. {
  118. uint32_t u32TempSize;
  119. uint32_t u32AddrOfst = 0UL;
  120. int32_t i32Ret = LL_ERR_INVD_PARAM;
  121. DDL_ASSERT((u32Addr + u32NumByteToWrite) <= W25Q64_MAX_ADDR);
  122. if ((pu8Data != NULL) && (u32NumByteToWrite > 0UL)) {
  123. while (u32NumByteToWrite != 0UL) {
  124. if (u32NumByteToWrite >= W25Q64_PAGE_SIZE) {
  125. u32TempSize = W25Q64_PAGE_SIZE;
  126. } else {
  127. u32TempSize = u32NumByteToWrite;
  128. }
  129. i32Ret = W25QXX_PageProgram(&m_stcW25qxxLL, u32Addr, (const uint8_t *)&pu8Data[u32AddrOfst], u32TempSize);
  130. if (i32Ret != LL_OK) {
  131. break;
  132. }
  133. u32NumByteToWrite -= u32TempSize;
  134. u32AddrOfst += u32TempSize;
  135. u32Addr += u32TempSize;
  136. }
  137. }
  138. return i32Ret;
  139. }
  140. void W25QXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
  141. {
  142. uint16_t pageremain;
  143. pageremain=256-WriteAddr%256; //单页剩余的字节数
  144. if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//不大于256个字节
  145. while(1)
  146. {
  147. W25QXX_PageProgram(&m_stcW25qxxLL,WriteAddr,pBuffer,NumByteToWrite);
  148. if(NumByteToWrite==pageremain)break;//写入结束了
  149. else //NumByteToWrite>pageremain
  150. {
  151. pBuffer+=pageremain;
  152. WriteAddr+=pageremain;
  153. NumByteToWrite-=pageremain; //减去已经写入了的字节数
  154. if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
  155. else pageremain=NumByteToWrite; //不够256个字节了
  156. }
  157. }
  158. }
  159. uint8_t flash_data_buffer[4096];
  160. void spi_flash_write(uint32_t WriteAddr,uint8_t* pBuffer,uint16_t NumByteToWrite)
  161. {
  162. uint32_t secpos;
  163. uint16_t secoff;
  164. uint16_t secremain;
  165. uint16_t i;
  166. secpos=WriteAddr/4096;//扇区地址 0~511 for w25x16
  167. secoff=WriteAddr%4096;//在扇区内的偏移
  168. secremain=4096-secoff;//扇区剩余空间大小
  169. if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
  170. while(1)
  171. {
  172. BSP_W25QXX_Read(secpos*4096,flash_data_buffer,4096);
  173. for(i=0;i<secremain;i++)//校验数据
  174. {
  175. if(flash_data_buffer[secoff+i]!=0XFF)break;//需要擦除
  176. }
  177. if(i<secremain)//需要擦除
  178. {
  179. BSP_W25QXX_EraseSector(secpos);//擦除这个扇区
  180. for(i=0;i<secremain;i++) //复制
  181. {
  182. flash_data_buffer[i+secoff]=pBuffer[i];
  183. }
  184. W25QXX_Write_NoCheck(flash_data_buffer,secpos*4096,4096);//写入整个扇区
  185. }
  186. else
  187. {
  188. W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);//写已经擦除了的,直接写入扇区剩余区间.
  189. }
  190. if(NumByteToWrite==secremain)break;//写入结束了
  191. else//写入未结束
  192. {
  193. secpos++;//扇区地址增1
  194. secoff=0;//偏移位置为0
  195. pBuffer+=secremain; //指针偏移
  196. WriteAddr+=secremain;//写地址偏移
  197. NumByteToWrite-=secremain; //字节数递减
  198. if(NumByteToWrite>4096)secremain=4096; //下一个扇区还是写不完
  199. else secremain=NumByteToWrite; //下一个扇区可以写完了
  200. }
  201. }
  202. }
  203. int32_t spi_flash_read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead)
  204. {
  205. DDL_ASSERT((u32Addr + u32NumByteToRead) <= W25Q64_MAX_ADDR);
  206. return W25QXX_ReadData(&m_stcW25qxxLL, u32Addr, pu8Data, u32NumByteToRead);
  207. }
  208. int32_t BSP_W25QXX_Read(uint32_t u32Addr, uint8_t *pu8Data, uint32_t u32NumByteToRead)
  209. {
  210. DDL_ASSERT((u32Addr + u32NumByteToRead) <= W25Q64_MAX_ADDR);
  211. return W25QXX_ReadData(&m_stcW25qxxLL, u32Addr, pu8Data, u32NumByteToRead);
  212. }
  213. int32_t BSP_W25QXX_EraseSector(uint32_t u32Addr)
  214. {
  215. DDL_ASSERT(u32Addr < W25Q64_MAX_ADDR);
  216. return W25QXX_EraseSector(&m_stcW25qxxLL, u32Addr);
  217. }
  218. int32_t BSP_W25QXX_EraseChip(void)
  219. {
  220. return W25QXX_EraseChip(&m_stcW25qxxLL);
  221. }