hal_ec600g.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "project_config.h"
  2. //#define EXAMPLE_SPI_BUF_LEN (256UL)
  3. uint8 u8TxSPIBuf[SPI_BUF_LEN] = {0};
  4. uint8 u8RxSPIBuf[SPI_BUF_LEN] = {0};
  5. uint16 SPI_SEND_INDEX = 5566;
  6. __IO uint16_t txflag_od = 0, rxflag_od = 0;
  7. __IO uint16_t u16TxIndex = 0U, u16RxIndex = 0U;
  8. void SPI_TransCompleteCallback(void);
  9. SemaphoreHandle_t spi_communication_decode = NULL;
  10. /*******************************************************************************
  11. * Function implementation - global ('extern') and local ('static')
  12. ******************************************************************************/
  13. /**
  14. * @brief SPI transmit complete callback.
  15. * @param None
  16. * @retval None
  17. */
  18. /**
  19. * @brief SPI receive complete callback.
  20. * @param None
  21. * @retval None
  22. */
  23. static void SPI_ReceiveCompleteCallback(void)
  24. {
  25. u8RxSPIBuf[u16RxIndex++] = (char)SPI_ReadData(MPU_SPI_UNIT);
  26. if(u16RxIndex >= 256)
  27. {
  28. u16RxIndex = 0;
  29. xSemaphoreGiveFromISR(spi_communication_decode, NULL);
  30. }
  31. }
  32. /**
  33. * @brief SPI configurate.
  34. * @param None
  35. * @retval None
  36. */
  37. void spi_mpu_init(void)
  38. {
  39. stc_spi_init_t stcSpiInit;
  40. stc_irq_signin_config_t stcIrqSignConfig;
  41. /* Configure Port */
  42. //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
  43. GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
  44. GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
  45. GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
  46. /* Configuration SPI */
  47. FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
  48. SPI_StructInit(&stcSpiInit);
  49. stcSpiInit.u32WireMode = SPI_3_WIRE;
  50. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  51. stcSpiInit.u32MasterSlave = SPI_SLAVE;
  52. stcSpiInit.u32Parity = SPI_PARITY_INVD;
  53. stcSpiInit.u32SpiMode = SPI_MD_3;
  54. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV40;
  55. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  56. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  57. stcSpiInit.u32FrameLevel = SPI_1_FRAME;
  58. (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
  59. SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_RX_BUF_FULL, ENABLE);
  60. SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_TX_BUF_EMPTY, ENABLE);
  61. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPRI;
  62. stcIrqSignConfig.enIRQn = INT007_IRQn;
  63. stcIrqSignConfig.pfnCallback = &SPI_ReceiveCompleteCallback;
  64. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  65. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  66. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
  67. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  68. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPTI;
  69. stcIrqSignConfig.enIRQn = INT008_IRQn;
  70. stcIrqSignConfig.pfnCallback = &SPI_TransCompleteCallback;
  71. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  72. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  73. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
  74. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  75. SPI_Cmd(MPU_SPI_UNIT, ENABLE);
  76. }
  77. void DMA_TransCompleteCallback(void);
  78. static void DMA_ReceiveCallback(void)
  79. {
  80. //enRxCompleteFlag = SET;
  81. DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
  82. //DMA_SetSrcAddr(DMA_UNIT, DMA_TX_CH, (uint32_t)(&u8TxSPIBuf[0]));
  83. // DMA_SetTransCount(DMA_UNIT, DMA_TX_CH, SPI_BUF_LEN);
  84. DMA_SetDestAddr(DMA_UNIT, DMA_RX_CH, (uint32_t)(&u8RxSPIBuf[0]));
  85. DMA_SetTransCount(DMA_UNIT, DMA_RX_CH, SPI_BUF_LEN);
  86. /* Enable DMA channel */
  87. //DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
  88. DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  89. }
  90. static void SPI_IDLECallback(void)
  91. {
  92. __nop();
  93. //enRxCompleteFlag = SET;
  94. //DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
  95. }
  96. void spi_mpu_init_hc(void)
  97. {
  98. stc_spi_init_t stcSpiInit;
  99. stc_dma_init_t stcDmaInit;
  100. stc_irq_signin_config_t stcIrqSignConfig;
  101. /* Configure Port */
  102. //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
  103. GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
  104. GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
  105. GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
  106. /* Configuration SPI */
  107. FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
  108. SPI_StructInit(&stcSpiInit);
  109. stcSpiInit.u32WireMode = SPI_3_WIRE;
  110. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  111. stcSpiInit.u32MasterSlave = SPI_SLAVE;
  112. stcSpiInit.u32Parity = SPI_PARITY_INVD;
  113. stcSpiInit.u32SpiMode = SPI_MD_3;
  114. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV40;
  115. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  116. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  117. stcSpiInit.u32FrameLevel = SPI_1_FRAME;
  118. (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
  119. //RX
  120. /*SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_RX_BUF_FULL, ENABLE);
  121. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPRI;
  122. stcIrqSignConfig.enIRQn = INT008_IRQn;
  123. stcIrqSignConfig.pfnCallback = &SPI_ReceiveCompleteCallback;
  124. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  125. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  126. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
  127. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn); */
  128. //TX
  129. SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_TX_BUF_EMPTY, ENABLE);
  130. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPTI;
  131. stcIrqSignConfig.enIRQn = INT007_IRQn;
  132. stcIrqSignConfig.pfnCallback = &SPI_TransCompleteCallback;
  133. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  134. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  135. NVIC_SetPriority(stcIrqSignConfig.enIRQn, 0);
  136. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  137. SPI_Cmd(MPU_SPI_UNIT, ENABLE);
  138. //DMA
  139. FCG_Fcg0PeriphClockCmd(DMA_CLK, ENABLE);
  140. (void)DMA_StructInit(&stcDmaInit);
  141. stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  142. stcDmaInit.u32BlockSize = 1UL;
  143. stcDmaInit.u32TransCount = 256;
  144. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  145. /* Configure TX */
  146. /* stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  147. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  148. stcDmaInit.u32SrcAddr = (uint32_t)(&u8TxSPIBuf[0]);
  149. stcDmaInit.u32DestAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
  150. if (LL_OK != DMA_Init(DMA_UNIT, DMA_TX_CH, &stcDmaInit)) {
  151. for (;;) {
  152. }
  153. }
  154. AOS_SetTriggerEventSrc(DMA_TX_TRIG_CH, SPI_TX_EVT_SRC);*/
  155. /* Configure RX */
  156. //stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  157. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  158. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
  159. stcDmaInit.u32SrcAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
  160. stcDmaInit.u32DestAddr = (uint32_t)(&u8RxSPIBuf[0]);
  161. if (LL_OK != DMA_Init(DMA_UNIT, DMA_RX_CH, &stcDmaInit)) {
  162. for (;;) {
  163. }
  164. }
  165. AOS_SetTriggerEventSrc(DMA_RX_TRIG_CH, SPI_RX_EVT_SRC);
  166. /* DMA receive NVIC configure */
  167. stcIrqSignConfig.enIntSrc = INT_SRC_DMA1_TC1;
  168. stcIrqSignConfig.enIRQn = INT006_IRQn;
  169. stcIrqSignConfig.pfnCallback = &DMA_TransCompleteCallback;
  170. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  171. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  172. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  173. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  174. DMA_Cmd(DMA_UNIT, ENABLE);
  175. //DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
  176. DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  177. }
  178. static void DMA_ReloadConfig(void)
  179. {
  180. DMA_SetSrcAddr(DMA_UNIT, DMA_TX_CH, (uint32_t)(&u8TxSPIBuf[0]));
  181. DMA_SetTransCount(DMA_UNIT, DMA_TX_CH, 256);
  182. // DMA_SetDestAddr(DMA_UNIT, DMA_RX_CH, (uint32_t)(&u8RxSPIBuf[0]));
  183. // DMA_SetTransCount(DMA_UNIT, DMA_RX_CH, 36);
  184. /* Enable DMA channel */
  185. DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
  186. //DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  187. }
  188. #include "FreeRTOS.h"
  189. #include "task.h"
  190. uint8_t data_decode_mpu(uint8_t *input_data, uint32_t input_data_len, uint8_t* output, uint32_t* output_data_len);
  191. void SPI_TransCompleteCallback(void)
  192. {
  193. if(SPI_SEND_INDEX >= FRAME_MAX_LENGTH)
  194. SPI_WriteData(MPU_SPI_UNIT, 0XBB);
  195. else /**/
  196. SPI_WriteData(MPU_SPI_UNIT, u8TxSPIBuf[txflag_od + SPI_SEND_INDEX++]);
  197. }
  198. void DMA_TransCompleteCallback(void)
  199. {
  200. rxflag_od = rxflag_od == FRAME_MAX_LENGTH ? 0 : FRAME_MAX_LENGTH;
  201. DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
  202. DMA_SetDestAddr(DMA_UNIT, DMA_RX_CH, (uint32_t)(&u8RxSPIBuf[rxflag_od]));
  203. DMA_SetTransCount(DMA_UNIT, DMA_RX_CH, FRAME_MAX_LENGTH);
  204. DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  205. xSemaphoreGiveFromISR(spi_communication_decode, NULL);
  206. }
  207. void ec600g_task(void *argv)
  208. {
  209. //SPI_ClearStatus(MPU_SPI_UNIT, SPI_FLAG_MD_FAULT | SPI_FLAG_UNDERLOAD);
  210. //SPI_WriteData(MPU_SPI_UNIT, 0);
  211. spi_communication_decode = xSemaphoreCreateBinary();
  212. uint32_t output_data_len_hc = 0;
  213. while(1)
  214. {
  215. xSemaphoreTake(spi_communication_decode, portMAX_DELAY);
  216. //vTaskDelay(10);
  217. txflag_od = 0 == txflag_od ? FRAME_MAX_LENGTH : 0;
  218. memset(u8TxSPIBuf + txflag_od, 0, FRAME_MAX_LENGTH);
  219. output_data_len_hc = 0;
  220. data_decode_mpu(u8RxSPIBuf + (rxflag_od == FRAME_MAX_LENGTH ? 0 : FRAME_MAX_LENGTH), FRAME_MAX_LENGTH,\
  221. u8TxSPIBuf + txflag_od, &output_data_len_hc);/**/
  222. memset(u8RxSPIBuf + (rxflag_od == FRAME_MAX_LENGTH ? 0 : FRAME_MAX_LENGTH), 0, FRAME_MAX_LENGTH);
  223. SPI_SEND_INDEX = output_data_len_hc > 0 ? 0 : 0X5566;
  224. }
  225. }