123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- #include "project_config.h"
-
- #define EXAMPLE_SPI_BUF_LEN (256UL)
- char u8TxSPIBuf[EXAMPLE_SPI_BUF_LEN] = "SPI Master/Slave example: Communication between two boards!";
- char u8RxSPIBuf[EXAMPLE_SPI_BUF_LEN];
- __IO uint16_t u16TxIndex = 0U, u16RxIndex = 0U;
- /*******************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- /**
- * @brief SPI transmit complete callback.
- * @param None
- * @retval None
- */
- static void SPI_TransCompleteCallback(void)
- {
- if (u16TxIndex < EXAMPLE_SPI_BUF_LEN) {
- SPI_WriteData(MPU_SPI_UNIT, u8TxSPIBuf[u16TxIndex++]);
- }
- }
- /**
- * @brief SPI receive complete callback.
- * @param None
- * @retval None
- */
- static void SPI_ReceiveCompleteCallback(void)
- {
- if (u16RxIndex < EXAMPLE_SPI_BUF_LEN) {
- u8RxSPIBuf[u16RxIndex++] = (char)SPI_ReadData(MPU_SPI_UNIT);
- }
- }
- /**
- * @brief SPI configurate.
- * @param None
- * @retval None
- */
-
- void mup_spi_init(void)
- {
- stc_spi_init_t stcSpiInit;
- stc_irq_signin_config_t stcIrqSignConfig;
-
- /* Configure Port */
- //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
- /* Configuration SPI */
- FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
- SPI_StructInit(&stcSpiInit);
- stcSpiInit.u32WireMode = SPI_3_WIRE;
- stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
- stcSpiInit.u32MasterSlave = SPI_SLAVE;
- stcSpiInit.u32Parity = SPI_PARITY_INVD;
- stcSpiInit.u32SpiMode = SPI_MD_0;
- stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV208;
- stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
- stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
- stcSpiInit.u32FrameLevel = SPI_1_FRAME;
- (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
- SPI_IntCmd(MPU_SPI_UNIT,SPI_INT_RX_BUF_FULL, ENABLE);
-
- stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPRI;
- stcIrqSignConfig.enIRQn = INT007_IRQn;
- stcIrqSignConfig.pfnCallback = &SPI_ReceiveCompleteCallback;
- (void)INTC_IrqSignIn(&stcIrqSignConfig);
- NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
- NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
-
- SPI_Cmd(MPU_SPI_UNIT, ENABLE);
- }
- static void DMA_TransCompleteCallback(void)
- {
- //enRxCompleteFlag = SET;
- DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
- }
- static void SPI_IDLECallback(void)
- {
- __nop();
- //enRxCompleteFlag = SET;
- //DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_RX_INT_CH);
- }
- void spi_mpu_init_hc(void)
- {
- stc_spi_init_t stcSpiInit;
- stc_dma_init_t stcDmaInit;
- stc_irq_signin_config_t stcIrqSignConfig;
- /* Configure Port */
- //GPIO_SetFunc(MPU_SPI_CS_PORT, MPU_SPI_CS_PIN, MPU_SPI_CS_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_SCK_PORT, MPU_SPI_SCK_PIN, MPU_SPI_SCK_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_MOSI_PORT, MPU_SPI_MOSI_PIN, MPU_SPI_MOSI_PIN_FUNC);
- GPIO_SetFunc(MPU_SPI_MISO_PORT, MPU_SPI_MISO_PIN, MPU_SPI_MISO_PIN_FUNC);
- /* Configuration SPI */
- FCG_Fcg1PeriphClockCmd(MPU_SPI_PERIPH_CLK, ENABLE);
- SPI_StructInit(&stcSpiInit);
- stcSpiInit.u32WireMode = SPI_3_WIRE;
- stcSpiInit.u32TransMode = SPI_FULL_DUPLEX;
- stcSpiInit.u32MasterSlave = SPI_SLAVE;
- stcSpiInit.u32Parity = SPI_PARITY_INVD;
- stcSpiInit.u32SpiMode = SPI_MD_0;
- stcSpiInit.u32BaudRatePrescaler = SPI_BR_CLK_DIV104;
- stcSpiInit.u32DataBits = SPI_DATA_SIZE_8BIT;
- stcSpiInit.u32FirstBit = SPI_FIRST_MSB;
- stcSpiInit.u32FrameLevel = SPI_1_FRAME;
-
- /* IDLE NVIC configure */
- stcIrqSignConfig.enIntSrc = INT_SRC_SPI3_SPII;
- stcIrqSignConfig.enIRQn = (uint32_t)MPU_IDLE_IRQ_NUM;
- stcIrqSignConfig.pfnCallback = &SPI_IDLECallback;
- (void)INTC_IrqSignIn(&stcIrqSignConfig);
- NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT - 1);
- NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
-
- (void)SPI_Init(MPU_SPI_UNIT, &stcSpiInit);
- /* DMA configuration */
- FCG_Fcg0PeriphClockCmd(DMA_CLK, ENABLE);
- (void)DMA_StructInit(&stcDmaInit);
- stcDmaInit.u32BlockSize = 1UL;
- stcDmaInit.u32TransCount = SPI_BUF_LEN;
- stcDmaInit.u32DataWidth = DMA_DATAWIDTH_8BIT;
- /* Configure TX */
- stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
- stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_FIX;
- stcDmaInit.u32SrcAddr = (uint32_t)(&u8TxSPIBuf[0]);
- stcDmaInit.u32DestAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
- if (LL_OK != DMA_Init(DMA_UNIT, DMA_TX_CH, &stcDmaInit)) {
- for (;;) {
- }
- }
- AOS_SetTriggerEventSrc(DMA_TX_TRIG_CH, SPI_TX_EVT_SRC);
- /* Configure RX */
- stcDmaInit.u32IntEn = DMA_INT_ENABLE;
- stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_FIX;
- stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
- stcDmaInit.u32SrcAddr = (uint32_t)(&MPU_SPI_UNIT->DR);
- stcDmaInit.u32DestAddr = (uint32_t)(&u8RxSPIBuf[0]);
- if (LL_OK != DMA_Init(DMA_UNIT, DMA_RX_CH, &stcDmaInit)) {
- for (;;) {
- }
- }
- AOS_SetTriggerEventSrc(DMA_RX_TRIG_CH, SPI_RX_EVT_SRC);
- /* DMA receive NVIC configure */
- stcIrqSignConfig.enIntSrc = DMA_RX_INT_SRC;
- stcIrqSignConfig.enIRQn = (uint32_t)MPU_SPI_DMA_RX_IRQ_NUM;
- stcIrqSignConfig.pfnCallback = &DMA_TransCompleteCallback;
- (void)INTC_IrqSignIn(&stcIrqSignConfig);
- NVIC_ClearPendingIRQ(stcIrqSignConfig.enIRQn);
- NVIC_SetPriority(stcIrqSignConfig.enIRQn, DDL_IRQ_PRIO_DEFAULT);
- NVIC_EnableIRQ(stcIrqSignConfig.enIRQn);
- /* Enable DMA and channel */
- DMA_Cmd(DMA_UNIT, ENABLE);
- DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
- DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
- }
- static void DMA_ReloadConfig(void)
- {
- DMA_SetSrcAddr(DMA_UNIT, DMA_TX_CH, (uint32_t)(&u8TxSPIBuf[0]));
- DMA_SetTransCount(DMA_UNIT, DMA_TX_CH, SPI_BUF_LEN);
- DMA_SetDestAddr(DMA_UNIT, DMA_RX_CH, (uint32_t)(&u8RxSPIBuf[0]));
- DMA_SetTransCount(DMA_UNIT, DMA_RX_CH, SPI_BUF_LEN);
- /* Enable DMA channel */
- DMA_ChCmd(DMA_UNIT, DMA_TX_CH, ENABLE);
- DMA_ChCmd(DMA_UNIT, DMA_RX_CH, ENABLE);
- }
-
- #include "FreeRTOS.h"
- #include "task.h"
-
- void ec600g_task(void *argv)
- {
- uint8_t tx_buf[16];
- for(int i= 0;i < sizeof(tx_buf);i++)
- {
- tx_buf[i] = i*2;
- }
- DMA_ReloadConfig();
- while(1)
- {
- //SPI_ClearStatus(MPU_SPI_UNIT, SPI_FLAG_MD_FAULT | SPI_FLAG_UNDERLOAD);
- //SPI_Trans(MPU_SPI_UNIT, tx_buf, sizeof(tx_buf), 0xff);
- vTaskDelay(500);
- }
- }
-
-
|