Esempio n. 1
0
/*!
 * @brief Check send/receive blocking with DMA
 *
 */
int main(void)
{
    uint8_t rxChar = 0, txChar = 0;
    uint32_t byteCountBuff = 0;

    dma_state_t state;
    lpsci_dma_state_t lpsciStateDma;

    lpsci_dma_user_config_t lpsciConfig = {
#if defined(KL02Z4_SERIES)
        .clockSource     = kClockLpsciSrcFll,
#else
        .clockSource     = kClockLpsciSrcPllFllSel,
#endif
        .bitCountPerChar = kLpsci8BitsPerChar,
        .parityMode      = kLpsciParityDisabled,
        .stopBitCount    = kLpsciOneStopBit,
        .baudRate        = BOARD_DEBUG_UART_BAUD
    };

    // Enable clock for PORTs, setup board clock source, config pin
    hardware_init();

    // Call OSA_Init to setup LP Timer for timeout
    OSA_Init();

    // Initialize DMA module for LPSCI
    DMA_DRV_Init(&state);
    LPSCI_DRV_DmaInit(BOARD_DEBUG_UART_INSTANCE, &lpsciStateDma, &lpsciConfig);

    // Inform to start polling example
    byteCountBuff = sizeof(buffStart);
    LPSCI_DRV_DmaSendDataBlocking(BOARD_DEBUG_UART_INSTANCE, buffStart, byteCountBuff, 1000u);

    // Inform user of what to do
    byteCountBuff = sizeof(bufferData1);
    LPSCI_DRV_DmaSendDataBlocking(BOARD_DEBUG_UART_INSTANCE, bufferData1, byteCountBuff, 1000u);

    while(true)
    {
        // Wait to receive input data
        if (kStatus_LPSCI_Success == LPSCI_DRV_DmaReceiveDataBlocking(BOARD_DEBUG_UART_INSTANCE, &rxChar, 1u, OSA_WAIT_FOREVER))
        {
            // Echo received character
            txChar = rxChar;
            LPSCI_DRV_DmaSendDataBlocking(BOARD_DEBUG_UART_INSTANCE, &txChar, 1u, 1000u);
        }
    }

}
Esempio n. 2
0
/*!
 * @brief SPI master DMA non-blocking.
 *
 * Thid function uses SPI master to send an array to slave
 * and receive the array back from slave,
 * then compare whether the two buffers are the same.
 */
int main (void)
{
    uint8_t loopCount = 0;
    uint32_t j;
    uint32_t failCount = 0;
    uint32_t calculatedBaudRate;
    spi_dma_master_state_t spiDmaMasterState;
    dma_state_t state;
    spi_dma_master_user_config_t userDmaConfig =
    {
#if FSL_FEATURE_SPI_16BIT_TRANSFERS
        .bitCount       = kSpi8BitMode,
#endif
        .polarity       = kSpiClockPolarity_ActiveHigh,
        .phase          = kSpiClockPhase_FirstEdge,
        .direction      = kSpiMsbFirst,
        .bitsPerSec     = TRANSFER_BAUDRATE
    };

    // init the hardware, this also sets up up the SPI pins for each specific SoC
    hardware_init();
    // Init OSA layer.
    OSA_Init();

    PRINTF("\r\nSPI board to board dma-non-blocking example");
    PRINTF("\r\nThis example run on instance %d", (uint32_t)SPI_MASTER_INSTANCE);
    PRINTF("\r\nBe sure master's SPI%d and slave's SPI%d are connected\r\n",
                    (uint32_t)SPI_MASTER_INSTANCE, (uint32_t)SPI_MASTER_INSTANCE);

    // Set up and init the master
    DMA_DRV_Init(&state);
    // Init the dspi module for eDMA operation
    SPI_DRV_DmaMasterInit(SPI_MASTER_INSTANCE, &spiDmaMasterState);
    SPI_DRV_DmaMasterConfigureBus(SPI_MASTER_INSTANCE,
                                    &userDmaConfig,
                                    &calculatedBaudRate);

    if (calculatedBaudRate > userDmaConfig.bitsPerSec)
    {
        PRINTF("\r\n**Something failed in the master bus config \r\n");
        return -1;
    }
    else
    {
        PRINTF("\r\nBaud rate in Hz is: %d\r\n", calculatedBaudRate);
    }

    while(1)
    {
        // Initialize the source buffer
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            s_spiSourceBuffer[j] = j + loopCount;
        }

        // Reset the sink buffer
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            s_spiSinkBuffer[j] = 0;
        }

        // Start the transfer
        SPI_DRV_DmaMasterTransferBlocking(SPI_MASTER_INSTANCE, NULL, s_spiSourceBuffer,
                                        NULL, TRANSFER_SIZE, MASTER_TRANSFER_TIMEOUT);
        while (SPI_DRV_DmaMasterGetTransferStatus(SPI_MASTER_INSTANCE, NULL) == kStatus_SPI_Busy)
        {
        }

        // Delay sometime to wait slave receive and send back data
        OSA_TimeDelay(500U);

        //Receive data from slave
        SPI_DRV_DmaMasterTransfer(SPI_MASTER_INSTANCE, NULL, NULL,
                            s_spiSinkBuffer, TRANSFER_SIZE);
        while (SPI_DRV_DmaMasterGetTransferStatus(SPI_MASTER_INSTANCE, NULL) == kStatus_SPI_Busy)
        {
        }

        // Verify the contents of the master sink buffer
        // refer to the slave driver for the expected data pattern
        failCount = 0; // reset failCount variable

        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            if (s_spiSinkBuffer[j] != s_spiSourceBuffer[j])
            {
                 failCount++;
            }
        }

        // Print out transmit buffer.
        PRINTF("\r\nMaster transmit:");
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            // Print 16 numbers in a line.
            if ((j & 0x0F) == 0)
            {
                PRINTF("\r\n    ");
            }
            PRINTF(" %02X", s_spiSourceBuffer[j]);
        }
        // Print out receive buffer.
        PRINTF("\r\nMaster receive:");
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            // Print 16 numbers in a line.
            if ((j & 0x0F) == 0)
            {
                PRINTF("\r\n    ");
            }
            PRINTF(" %02X", s_spiSinkBuffer[j]);
        }

        if (failCount == 0)
        {
            PRINTF("\r\n Spi master transfer succeed! \r\n");
        }
        else
        {
            PRINTF("\r\n **failures detected in Spi master transfer! \r\n");
        }

        // Wait for press any key.
        PRINTF("\r\nPress any key to run again\r\n");
        GETCHAR();
        loopCount++;
    }
}
Esempio n. 3
0
/*!
 * @brief Main function
 */
int main (void)
{
    /* enable clock for PORTs */
    CLOCK_SYS_EnablePortClock(PORTA_IDX);
    //CLOCK_SYS_EnablePortClock(PORTB_IDX);
    CLOCK_SYS_EnablePortClock(PORTC_IDX);
    CLOCK_SYS_EnablePortClock(PORTD_IDX);
    CLOCK_SYS_EnablePortClock(PORTE_IDX);

    /* Set allowed power mode, allow all. */
    SMC_HAL_SetProtection(SMC, kAllowPowerModeAll);

    /* Set system clock configuration. */
    CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr);

    /* Initialize LPTMR */
    lptmr_state_t lptmrState;
    LPTMR_DRV_Init(LPTMR0_IDX, &lptmrState, &g_lptmrConfig);
    LPTMR_DRV_SetTimerPeriodUs(LPTMR0_IDX, 100000);
    LPTMR_DRV_InstallCallback(LPTMR0_IDX, lptmr_call_back);

    /* Initialize DMA */
    dma_state_t dma_state;
    DMA_DRV_Init(&dma_state);

    /* Initialize PIT */
    PIT_DRV_Init(0, false);
    PIT_DRV_InitChannel(0, 0, &g_pitChan0);

    /* Initialize CMP */
    CMP_DRV_Init(0, &g_cmpState, &g_cmpConf);
    CMP_DRV_ConfigDacChn(0, &g_cmpDacConf);
    PORT_HAL_SetMuxMode(g_portBase[GPIOC_IDX], 0, kPortMuxAlt5);
    CMP_DRV_Start(0);

    /* Buttons */
    GPIO_DRV_InputPinInit(&g_switch1);
    GPIO_DRV_InputPinInit(&g_switch2);
    GPIO_DRV_InputPinInit(&g_switchUp);
    GPIO_DRV_InputPinInit(&g_switchDown);
    GPIO_DRV_InputPinInit(&g_switchLeft);
    GPIO_DRV_InputPinInit(&g_switchRight);
    GPIO_DRV_InputPinInit(&g_switchSelect);

    /* Start LPTMR */
    LPTMR_DRV_Start(LPTMR0_IDX);

    /* Setup LPUART1 */
    LPUART_DRV_Init(1, &g_lpuartState, &g_lpuartConfig);
    LPUART_DRV_InstallRxCallback(1, lpuartRxCallback, rxBuff, NULL, true);
    LPUART_DRV_InstallTxCallback(1, lpuartTxCallback, NULL, NULL);
    LPUART_BWR_CTRL_TXINV(g_lpuartBase[1], 1);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 0, kPortMuxAlt3);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 1, kPortMuxAlt3);

    /* Setup FlexIO for the WS2812B */
    FLEXIO_Type *fiobase = g_flexioBase[0];
    CLOCK_SYS_SetFlexioSrc(0, kClockFlexioSrcMcgIrClk);
    FLEXIO_DRV_Init(0, &g_flexioConfig);
    FLEXIO_HAL_ConfigureTimer(fiobase, 0, &g_timerConfig);
    FLEXIO_HAL_ConfigureShifter(fiobase, 0, &g_shifterConfig);
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 20, kPortMuxAlt6);
    FLEXIO_DRV_Start(0);

    FLEXIO_HAL_SetShifterStatusDmaCmd(fiobase, 1, true);
    DMA_DRV_RequestChannel(kDmaAnyChannel, kDmaRequestMux0FlexIOChannel0,
            &g_fioChan);
    DMA_DRV_RegisterCallback(&g_fioChan, fioDmaCallback, NULL);

    /* Connect buzzer to TPM0_CH3 */
    PORT_HAL_SetMuxMode(g_portBase[GPIOE_IDX], 30, kPortMuxAlt3);
    tpm_general_config_t tmpConfig = {
        .isDBGMode = false,
        .isGlobalTimeBase = false,
        .isTriggerMode = false,
        .isStopCountOnOveflow = false,
        .isCountReloadOnTrig = false,
        .triggerSource = kTpmTrigSel0,
    };
    TPM_DRV_Init(0, &tmpConfig);
    TPM_DRV_SetClock(0, kTpmClockSourceModuleMCGIRCLK, kTpmDividedBy1);

    /* Blank LED just in case, saves power */
    led(0x00, 0x00, 0x00);

    /* Init e-paper display */
    EPD_Init();

    /* Throw up first image */
    int ret = EPD_Draw(NULL, images[current_image]);
    if (-1 == ret) {
        led(0xff, 0x00, 0x00);
    } else if (-2 == ret) {
        led(0xff, 0xff, 0x00);
    } else if (-3 == ret) {
        led(0x00, 0x00, 0xff);
    } else {
        led(0x00, 0xff, 0x00);
    }
    blank_led = 30;

    /* Deinit so we can mess around on the bus pirate */
    //EPD_Deinit();

    /* We're done, everything else is triggered through interrupts */
    for(;;) {
        if (cue_next_image) {
            int old_image = current_image;
            current_image = (current_image + 1) % image_count;
            EPD_Draw(images[old_image], images[current_image]);
            cue_next_image = 0;
        }
#ifndef DEBUG
        SMC_HAL_SetMode(SMC, &g_idlePowerMode);
#endif
    }
}
Esempio n. 4
0
int main (void)
{
    uint32_t j;
    spi_status_t spiResult;
    spi_slave_state_t spiSlaveState;

    spi_dma_slave_state_t spiDmaSlaveState;
    spi_slave_user_config_t slaveUserConfig;
    spi_dma_slave_user_config_t userDmaConfig =
    {
    #if FSL_FEATURE_SPI_16BIT_TRANSFERS
        .bitCount       = kSpi8BitMode,
    #endif
        .polarity       = kSpiClockPolarity_ActiveHigh,
        .phase          = kSpiClockPhase_FirstEdge,
        .direction      = kSpiMsbFirst
    };

    dma_state_t dmaState;

    // Init the DMA module
    DMA_DRV_Init(&dmaState);

    // init the hardware, this also sets up up the SPI pins for each specific SoC
    hardware_init();

    dbg_uart_init();

    OSA_Init();

    printf("\r\n SPI board to board dma non-blocking example");
    printf("\r\n This example run on instance 0 ");
    printf("\r\n Be sure master's SPI0 and slave's SPI0 are connected ");

    // USER CONFIGURABLE OPTION FOR SPI INSTANCE (if applicable)

    // Configure SPI pin
    configure_spi_pins(SPI_SLAVE_INSTANCE);

    if (SPI_DRV_DmaSlaveInit(SPI_SLAVE_INSTANCE, &spiDmaSlaveState, &userDmaConfig) != kStatus_SPI_Success)
    {
        printf("\r\nError in slave DMA init \r\n");
    }

    while(1)
    {
        printf("\r\nSlave example is running...");
        spiResult = SPI_DRV_DmaSlaveTransfer(SPI_SLAVE_INSTANCE, NULL,
                                      s_spiSinkBuffer, TRANSFER_SIZE);
        while(kStatus_SPI_Success != SPI_DRV_DmaSlaveGetTransferStatus(SPI_SLAVE_INSTANCE, NULL));
        if (spiResult != kStatus_SPI_Success)
        {
            printf("\r\nERROR: slave receives error ");
            return -1;
        }
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            s_spiSourceBuffer[j] = s_spiSinkBuffer[j];
        }

        spiResult = SPI_DRV_DmaSlaveTransfer(SPI_SLAVE_INSTANCE, s_spiSourceBuffer,
                                          NULL, TRANSFER_SIZE);
        while(kStatus_SPI_Success != SPI_DRV_DmaSlaveGetTransferStatus(SPI_SLAVE_INSTANCE, NULL));
        if (spiResult != kStatus_SPI_Success)
        {
            printf("\r\nERROR: slave sends error ");
            return -1;
        }
        // Print out receive buffer
        printf("\r\nSlave receive:");
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            if (j%16 == 0)
            {
                printf("\r\n    ");
            }
            printf(" %02X", s_spiSinkBuffer[j]);
        }
    }
}
Esempio n. 5
0
// Setup the board as a spi master
int main (void)
{
    uint8_t loopCount = 0;
    uint32_t j;
    uint32_t failCount = 0;
    uint8_t * pRxBuff;
    uint32_t calculatedBaudRate;
    spi_master_state_t spiMasterState;
    spi_dma_master_state_t spiDmaMasterState;
    dma_state_t state;
    spi_dma_master_user_config_t userDmaConfig =
    {
    #if FSL_FEATURE_SPI_16BIT_TRANSFERS
        .bitCount       = kSpi8BitMode,
    #endif
        .polarity       = kSpiClockPolarity_ActiveHigh,
        .phase          = kSpiClockPhase_FirstEdge,
        .direction      = kSpiMsbFirst,
        .bitsPerSec     = TRANSFER_BAUDRATE
    };

    // init the hardware, this also sets up up the SPI pins for each specific SoC
    hardware_init();

    dbg_uart_init();

    OSA_Init();

    printf("\r\n SPI board to board dma-blocking example");
    printf("\r\n This example run on instance 0 ");
    printf("\r\n Be sure master's SPI0 and slave's SPI0 are connected ");

    //USER CONFIGURABLE OPTION FOR SPI INSTANCE

    // Configure SPI pin
    configure_spi_pins(SPI_MASTER_INSTANCE);

    printf("\rIMPORTANT, MAKE SURE TO FIRST SET UP THE SPI SLAVE BOARD!\r\n");

    //USER CONFIGURABLE OPTION FOR RXBUFF
    pRxBuff = s_spiSinkBuffer;
    // Set up and init the master
    DMA_DRV_Init(&state);
    // Init the dspi module for eDMA operation
    SPI_DRV_DmaMasterInit(SPI_MASTER_INSTANCE, &spiDmaMasterState);

    SPI_DRV_DmaMasterConfigureBus(SPI_MASTER_INSTANCE,
                                    &userDmaConfig,
                                    &calculatedBaudRate);


    if (calculatedBaudRate > userDmaConfig.bitsPerSec)
    {
        printf("\r**Something failed in the master bus config \r\n");
        return -1;
    }


    printf("\r\nSpi master SYNC test with various baud rates  \r\n");

    printf("\r\nBaudRate set to %dHz\r\n", calculatedBaudRate);
    while(1)
    {
        // Initialize the source buffer
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            s_spiSourceBuffer[j] = j + loopCount;
        }

        // Reset the sink buffer
        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            s_spiSinkBuffer[j] = 0;
        }
        /* Start the transfer */
        if (SPI_DRV_DmaMasterTransferBlocking(SPI_MASTER_INSTANCE, NULL, s_spiSourceBuffer,
                            NULL, TRANSFER_SIZE, MASTER_TRANSFER_TIMEOUT) == kStatus_SPI_Timeout)
        {
            printf("\r**Sync transfer timed-out \r\n");
        }

        // Delay sometime to wait slave receive and send back data
        OSA_TimeDelay(500U);

        if (SPI_DRV_DmaMasterTransferBlocking(SPI_MASTER_INSTANCE, NULL, NULL,
                            s_spiSinkBuffer, TRANSFER_SIZE, MASTER_TRANSFER_TIMEOUT) == kStatus_SPI_Timeout)
        {
            printf("\r**Sync transfer timed-out \r\n");
        }
        // Verify the contents of the master sink buffer
        // refer to the slave driver for the expected data pattern
        failCount = 0; // reset failCount variable

        for (j = 0; j < TRANSFER_SIZE; j++)
        {
            if (s_spiSinkBuffer[j] != s_spiSourceBuffer[j])
            {
                 failCount++;
            }
        }

        if (failCount == 0)
        {
            printf("\r\nMaster transmit:");
            for (j = 0; j < TRANSFER_SIZE; j++)
            {
                if (j%16 == 0)
                {
                    printf("\r\n    ");
                }
                printf(" %02X", s_spiSourceBuffer[j]);
            }
            printf("\r\nMaster receive:");
            for (j = 0; j < TRANSFER_SIZE; j++)
            {
                if (j%16 == 0)
                {
                    printf("\r\n    ");
                }
                printf(" %02X", s_spiSinkBuffer[j]);
            }
            printf("\r\n");
            printf("\r Spi master blocking transfer succeed! \r\n");
        }
        else
        {
            printf("\r\nSource buffer:");
            for (j = 0; j < TRANSFER_SIZE; j++)
            {
                if (j%16 == 0)
                {
                    printf("\r\n    ");
                }
                printf(" %02X", s_spiSourceBuffer[j]);
            }
            printf("\r\nSink buffer:");
            for (j = 0; j < TRANSFER_SIZE; j++)
            {
                if (j%16 == 0)
                {
                    printf("\r\n    ");
                }
                printf(" %02X", s_spiSinkBuffer[j]);
            }
            printf("\r\n");
            printf("\r **failures detected in Spi master blocking transfer! \r\n");
        }

        // Wait for press any key.
        printf("\r\nPress any key to run again\r\n");
        getchar();
        loopCount++;
    }
}