Advertisement
Guest User

Untitled

a guest
Nov 4th, 2020
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include "board.hpp"
  2. #include "pin_mux.h"
  3. #include "clock_config.h"
  4.  
  5. #include "fsl_dmamux.h"
  6. #include "fsl_edma.h"
  7. #include "fsl_uart.h"
  8. #include "fsl_uart_edma.h"
  9. #include "fsl_pit.h"
  10.  
  11. static uint8_t buff[513];
  12.  
  13. static volatile uint32_t dma_complete_count = 0;
  14. static volatile uint32_t transfer_done_count = 0;
  15.  
  16. static void dma_irq(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  17. {
  18.     dma_complete_count++;
  19.     if (transferDone)
  20.     {
  21.         transfer_done_count++;
  22.     }
  23. }
  24.  
  25. int main(void)
  26. {
  27.     BOARD_InitBootPins();
  28.     BOARD_BootClockRUN();
  29.     BOARD_InitDebugConsole();
  30.  
  31.     // ==================================================================================
  32.     // UART config
  33.     // ==================================================================================
  34.  
  35.     uart_config_t uartConfig;
  36.     UART_GetDefaultConfig(&uartConfig);
  37.     uartConfig.baudRate_Bps = 250000;
  38.     uartConfig.enableRx = false;
  39.     uartConfig.enableTx = false;
  40.  
  41.     if(kStatus_Success != UART_Init(UART1, &uartConfig, CLOCK_GetFreq(UART1_CLK_SRC)))
  42.     {
  43.         MSG("Failed to init UART1\n");
  44.     }
  45.  
  46.     // Disable FIFO buffer (enabled by default)
  47.     UART1->PFIFO &= ~(UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK);
  48.  
  49.     UART_EnableTx(UART1, true);
  50.  
  51.     // ==================================================================================
  52.     // PIT config
  53.     // ==================================================================================
  54.  
  55.     pit_config_t config;
  56.     PIT_GetDefaultConfig(&config);
  57.     PIT_Init(base, &config);
  58.  
  59.     // disable interrupts for our channel
  60.     PIT_DisableInterrupts(PIT, kPIT_Chnl_2, kPIT_TimerInterruptEnable);
  61.  
  62.     // stops the channel if it's already running
  63.     PIT_StopTimer(PIT, kPIT_Chnl_2);
  64.  
  65.     // clear the interrupt flag
  66.     PIT_ClearStatusFlags(PIT, kPIT_Chnl_2, kPIT_TimerFlag);
  67.  
  68.     // ==================================================================================
  69.     // DMA config
  70.     // ==================================================================================
  71.  
  72.     // configure our DMA
  73.     DMAMUX_Init(DMAMUX);
  74.     edma_config_t edma_config;
  75.     EDMA_GetDefaultConfig(&edma_config);
  76.     EDMA_Init(DMA0, &edma_config);
  77.  
  78.     edma_handle_t edmaHandleTx;
  79.  
  80.     DMAMUX_DisableChannel(DMAMUX, 2);
  81.     DMAMUX_SetSource(DMAMUX, 2, kDmaRequestMux0UART1Tx);
  82.     DMAMUX_EnableChannel(DMAMUX, 2);
  83.     EDMA_CreateHandle(&edmaHandleTx, DMA0, 2);
  84.  
  85.     EDMA_SetCallback(&edmaHandleTx, dma_irq, NULL);
  86.  
  87.     // ==================================================================================
  88.     // Do Send
  89.     // ==================================================================================
  90.  
  91.     for (int i = 0; i < 10; i++)
  92.     {
  93.         buff[i] = i;
  94.     }
  95.  
  96.     edma_transfer_config_t xferConfig;
  97.     EDMA_PrepareTransfer(&xferConfig, buff, sizeof(uint8_t),
  98.                         (void *)UART_GetDataRegisterAddress(UART1),
  99.                         sizeof(uint8_t), sizeof(uint8_t), 10, kEDMA_MemoryToPeripheral);
  100.  
  101.     EDMA_SubmitTransfer(&edmaHandleTx, &xferConfig);
  102.  
  103.     EDMA_StartTransfer(&edmaHandleTx);
  104.  
  105.     // Setup the timer for data send rate
  106.     PIT_SetTimerPeriod(PIT, kPIT_Chnl_2, 10800);  // 300us (bus clock 36MHz)
  107.     PIT_StartTimer(PIT, kPIT_Chnl_2);
  108.  
  109.     // Use PIT to trigger DMA
  110.     DMAMUX_EnablePeriodTrigger(DMAMUX, 2);
  111.  
  112.     // Enable UART TX EDMA
  113.     // DMA will start sending data once the running timer elapses
  114.     UART_EnableTxDMA(UART1, true);
  115.  
  116.     while(1);
  117.  
  118.     return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement