hal_ec600g.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "project_config.h"
  2. #define EXAMPLE_SPI_BUF_LEN (256UL)
  3. char u8TxSPIBuf[EXAMPLE_SPI_BUF_LEN] = "SPI Master/Slave example: Communication between two boards!";
  4. char u8RxSPIBuf[EXAMPLE_SPI_BUF_LEN];
  5. __IO uint16_t u16TxIndex = 0U, u16RxIndex = 0U;
  6. /*******************************************************************************
  7. * Function implementation - global ('extern') and local ('static')
  8. ******************************************************************************/
  9. /**
  10. * @brief SPI transmit complete callback.
  11. * @param None
  12. * @retval None
  13. */
  14. static void SPI_TransCompleteCallback(void)
  15. {
  16. if (u16TxIndex < EXAMPLE_SPI_BUF_LEN) {
  17. SPI_WriteData(MPU_SPI_UNIT, u8TxSPIBuf[u16TxIndex++]);
  18. }
  19. }
  20. /**
  21. * @brief SPI receive complete callback.
  22. * @param None
  23. * @retval None
  24. */
  25. static void SPI_ReceiveCompleteCallback(void)
  26. {
  27. if (u16RxIndex < EXAMPLE_SPI_BUF_LEN) {
  28. u8RxSPIBuf[u16RxIndex++] = (char)SPI_ReadData(MPU_SPI_UNIT);
  29. }
  30. }
  31. /**
  32. * @brief SPI configurate.
  33. * @param None
  34. * @retval None
  35. */
  36. void mup_spi_init(void)
  37. {
  38. stc_spi_init_t stcSpiInit;
  39. stc_irq_signin_config_t stcIrqSignConfig;
  40. /* Configure Port */
  41. //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
  42. GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
  43. GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
  44. GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
  45. /* Configuration SPI */
  46. FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
  47. SPI_StructInit(&stcSpiInit);
  48. stcSpiInit.u32WireMode = SPI_3_WIRE;
  49. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  50. stcSpiInit.u32MasterSlave = SPI_SLAVE;
  51. stcSpiInit.u32Parity = SPI_PARITY_INVD;
  52. stcSpiInit.u32SpiMode = SPI_MD_0;
  53. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV208;
  54. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  55. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  56. stcSpiInit.u32FrameLevel = SPI_1_FRAME;
  57. (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
  58. SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_RX_BUF_FULL, ENABLE);
  59. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPRI;
  60. stcIrqSignConfig.enIRQn = INT007_IRQn;
  61. stcIrqSignConfig.pfnCallback = &SPI_ReceiveCompleteCallback;
  62. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  63. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  64. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
  65. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  66. SPI_Cmd(MPU_SPI_UNIT, ENABLE);
  67. }
  68. static void DMA_TransCompleteCallback(void)
  69. {
  70. //enRxCompleteFlag = SET;
  71. DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
  72. }
  73. static void SPI_IDLECallback(void)
  74. {
  75. __nop();
  76. //enRxCompleteFlag = SET;
  77. //DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
  78. }
  79. void spi_mpu_init_hc(void)
  80. {
  81. stc_spi_init_t stcSpiInit;
  82. stc_dma_init_t stcDmaInit;
  83. stc_irq_signin_config_t stcIrqSignConfig;
  84. /* Configure Port */
  85. //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
  86. GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
  87. GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
  88. GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
  89. /* Configuration SPI */
  90. FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
  91. SPI_StructInit(&stcSpiInit);
  92. stcSpiInit.u32WireMode = SPI_3_WIRE;
  93. stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
  94. stcSpiInit.u32MasterSlave = SPI_SLAVE;
  95. stcSpiInit.u32Parity = SPI_PARITY_INVD;
  96. stcSpiInit.u32SpiMode = SPI_MD_0;
  97. stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV104;
  98. stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
  99. stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
  100. stcSpiInit.u32FrameLevel = SPI_1_FRAME;
  101. /* IDLE NVIC configure */
  102. stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPII;
  103. stcIrqSignConfig.enIRQn = (uint32_t)MPU_IDLE_IRQ_NUM;
  104. stcIrqSignConfig.pfnCallback = &SPI_IDLECallback;
  105. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  106. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  107. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
  108. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  109. (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
  110. /* DMA configuration */
  111. FCG_Fcg0PeriphClockCmd(DMA_CLK, ENABLE);
  112. (void)DMA_StructInit(&stcDmaInit);
  113. stcDmaInit.u32BlockSize = 1UL;
  114. stcDmaInit.u32TransCount = SPI_BUF_LEN;
  115. stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
  116. /* Configure TX */
  117. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  118. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
  119. stcDmaInit.u32SrcAddr = (uint32_t)(&u8TxSPIBuf[0]);
  120. stcDmaInit.u32DestAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
  121. if (LL_OK != DMA_Init(DMA_UNIT, DMA_TX_CH, &stcDmaInit)) {
  122. for (;;) {
  123. }
  124. }
  125. AOS_SetTriggerEventSrc(DMA_TX_TRIG_CH, SPI_TX_EVT_SRC);
  126. /* Configure RX */
  127. stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  128. stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
  129. stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
  130. stcDmaInit.u32SrcAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
  131. stcDmaInit.u32DestAddr = (uint32_t)(&u8RxSPIBuf[0]);
  132. if (LL_OK != DMA_Init(DMA_UNIT, DMA_RX_CH, &stcDmaInit)) {
  133. for (;;) {
  134. }
  135. }
  136. AOS_SetTriggerEventSrc(DMA_RX_TRIG_CH, SPI_RX_EVT_SRC);
  137. /* DMA receive NVIC configure */
  138. stcIrqSignConfig.enIntSrc = DMA_RX_INT_SRC;
  139. stcIrqSignConfig.enIRQn = (uint32_t)MPU_SPI_DMA_RX_IRQ_NUM;
  140. stcIrqSignConfig.pfnCallback = &DMA_TransCompleteCallback;
  141. (void)INTC_IrqSignIn(&stcIrqSignConfig);
  142. NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
  143. NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
  144. NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
  145. /* Enable DMA and channel */
  146. DMA_Cmd(DMA_UNIT, ENABLE);
  147. DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
  148. DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  149. }
  150. static void DMA_ReloadConfig(void)
  151. {
  152. DMA_SetSrcAddr(DMA_UNIT, DMA_TX_CH, (uint32_t)(&u8TxSPIBuf[0]));
  153. DMA_SetTransCount(DMA_UNIT, DMA_TX_CH, SPI_BUF_LEN);
  154. DMA_SetDestAddr(DMA_UNIT, DMA_RX_CH, (uint32_t)(&u8RxSPIBuf[0]));
  155. DMA_SetTransCount(DMA_UNIT, DMA_RX_CH, SPI_BUF_LEN);
  156. /* Enable DMA channel */
  157. DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
  158. DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
  159. }
  160. #include "FreeRTOS.h"
  161. #include "task.h"
  162. void ec600g_task(void *argv)
  163. {
  164. uint8_t tx_buf[16];
  165. for(int i= 0;i < sizeof(tx_buf);i++)
  166. {
  167. tx_buf[i] = i*2;
  168. }
  169. DMA_ReloadConfig();
  170. while(1)
  171. {
  172. //SPI_ClearStatus(MPU_SPI_UNIT, SPI_FLAG_MD_FAULT | SPI_FLAG_UNDERLOAD);
  173. //SPI_Trans(MPU_SPI_UNIT, tx_buf, sizeof(tx_buf), 0xff);
  174. vTaskDelay(500);
  175. }
  176. }