hal_ec600g.c 11 KB

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