hal_ec600g.c 10 KB

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