Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
mtb-example-xmc-uart-dma/main.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
214 lines (185 sloc)
7.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
* File Name: main.c | |
* | |
* Description: This is the source code for the XMC MCU: UART DMA Example | |
* for ModusToolbox. This example demonstrates how to stream data | |
* using DMA via a Universal Serial Interface Channel (USIC) | |
* based UART to external terminal. | |
* | |
* Related Document: See README.md | |
* | |
****************************************************************************** | |
* | |
* Copyright (c) 2015-2024, Infineon Technologies AG | |
* All rights reserved. | |
* | |
* Boost Software License - Version 1.0 - August 17th, 2003 | |
* | |
* Permission is hereby granted, free of charge, to any person or organization | |
* obtaining a copy of the software and accompanying documentation covered by | |
* this license (the "Software") to use, reproduce, display, distribute, | |
* execute, and transmit the Software, and to prepare derivative works of the | |
* Software, and to permit third-parties to whom the Software is furnished to | |
* do so, all subject to the following: | |
* | |
* The copyright notices in the Software and this entire statement, including | |
* the above license grant, this restriction and the following disclaimer, | |
* must be included in all copies of the Software, in whole or in part, and | |
* all derivative works of the Software, unless such copies or derivative | |
* works are solely in the form of machine-executable object code generated by | |
* a source language processor. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | |
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | |
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | |
* DEALINGS IN THE SOFTWARE. | |
* | |
*****************************************************************************/ | |
#include "cybsp.h" | |
#include "cy_utils.h" | |
#include "cy_retarget_io.h" | |
#include <stdio.h> | |
/******************************************************************************* | |
* Macros | |
*******************************************************************************/ | |
/* Define macro to enable/disable printing of debug messages */ | |
#define ENABLE_XMC_DEBUG_PRINT (0) | |
/* Size of source buffer(s) for the DMA to read from */ | |
#if ENABLE_XMC_DEBUG_PRINT | |
#define BUFFER_LENGTH 26 | |
#else | |
#define BUFFER_LENGTH 256 | |
#endif | |
/* Preemptive priority value (starting from 0) of NVIC CPU Subsystem */ | |
#define DEBUG_GPDMA_RECEIVE_EVENT_PRIORITY 63 | |
/* Subpriority value (starting from 0) of NVIC CPU Subsystem */ | |
#define DEBUG_GPDMA_RECEIVE_EVENT_SUB_PRIORITY 0 | |
/******************************************************************************* | |
* Global Variables | |
*******************************************************************************/ | |
/* Source buffers for DMA */ | |
uint8_t buffer1[BUFFER_LENGTH]; | |
uint8_t buffer2[BUFFER_LENGTH]; | |
/* Destination address pointer*/ | |
uint32_t * dst_ptr = (uint32_t *) & (CYBSP_DEBUG_UART_HW->TBUF[0]); | |
/* Flag variable to register when transfer is finished */ | |
volatile bool transfer_done = false; | |
/******************************************************************************* | |
* Function Name: custom_delay | |
******************************************************************************** | |
* Summary: | |
* This function sets custom delay for easier printing data on Serial Terminal | |
* | |
* Parameters: | |
* delay_cyc - Number of NOP commands | |
* | |
* Return: | |
* void | |
* | |
*******************************************************************************/ | |
void custom_delay(int32_t delay_cyc) | |
{ | |
while (delay_cyc--) | |
asm("NOP"); | |
} | |
/******************************************************************************* | |
* Function Name: GPDMA0_INTERRUPT_HANDLER | |
******************************************************************************** | |
* Summary: | |
* Interrupt handler called when transfer has completed | |
* | |
* Parameters: | |
* void | |
* | |
* Return: | |
* void | |
* | |
*******************************************************************************/ | |
void GPDMA0_INTERRUPT_HANDLER(void) | |
{ | |
/* Set global variable to register event and notify main-loop */ | |
transfer_done = true; | |
/* Clear event status */ | |
XMC_DMA_CH_ClearEventStatus(DMA_HW, DMA_NUM, XMC_DMA_CH_EVENT_BLOCK_TRANSFER_COMPLETE); | |
} | |
/******************************************************************************* | |
* Function Name: main | |
******************************************************************************** | |
* Summary: | |
* This is the main function. It initializes the UART interface and configures | |
* DMA for streaming via UART. | |
* Waits for the transfer to finish and finally idles inside main-loop. | |
* | |
* Parameters: | |
* void | |
* | |
* Return: | |
* int | |
* | |
*******************************************************************************/ | |
int main(void) | |
{ | |
cy_rslt_t result; | |
/* Initialize the device and board peripherals */ | |
result = cybsp_init(); | |
if (result != CY_RSLT_SUCCESS) | |
{ | |
CY_ASSERT(0); | |
} | |
/* Initialize printf retarget */ | |
cy_retarget_io_init(CYBSP_DEBUG_UART_HW); | |
#if ENABLE_XMC_DEBUG_PRINT | |
printf("Initialization done\r\n"); | |
#endif | |
/* Prepare source buffers with ASCII-codes A..Z and a..z */ | |
for (uint32_t i = 0; i < BUFFER_LENGTH; ++i) | |
{ | |
buffer1[i] = i%26 + 'A'; | |
buffer2[i] = i%26 + 'a'; | |
} | |
/* Event/interrupt configuration */ | |
NVIC_SetPriority(GPDMA0_0_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), DEBUG_GPDMA_RECEIVE_EVENT_PRIORITY, DEBUG_GPDMA_RECEIVE_EVENT_SUB_PRIORITY)); | |
NVIC_EnableIRQ(GPDMA0_0_IRQn); | |
/* Initial trigger for DMA transfer */ | |
XMC_USIC_CH_TriggerServiceRequest(CYBSP_DEBUG_UART_HW, 0); | |
#if ENABLE_XMC_DEBUG_PRINT | |
printf("A USIC interrupt service request triggered\r\n"); | |
custom_delay(200000); | |
#endif | |
transfer_done = false; | |
/* Set Block size of the next transfer */ | |
XMC_DMA_CH_SetBlockSize(DMA_HW, DMA_NUM, BUFFER_LENGTH); | |
/* Set Source address of the next transfer */ | |
XMC_DMA_CH_SetSourceAddress(DMA_HW, DMA_NUM, (uint32_t)&buffer1[0]); | |
/* Start transfer */ | |
XMC_DMA_CH_Enable(DMA_HW, DMA_NUM); | |
/* Wait for completion of DMA transfer */ | |
while (transfer_done == false) | |
{ | |
} | |
#if ENABLE_XMC_DEBUG_PRINT | |
printf("DMA transfer of buffer 1 completed\r\n"); | |
custom_delay(200000); | |
#endif | |
/* Execute DMA transfer of buffer 2 */ | |
transfer_done = false; | |
/* Set Block size of the next transfer */ | |
XMC_DMA_CH_SetBlockSize(DMA_HW, DMA_NUM, BUFFER_LENGTH); | |
/* Set Source address of the next transfer */ | |
XMC_DMA_CH_SetSourceAddress(DMA_HW, DMA_NUM, (uint32_t)&buffer2[0]); | |
/* Start transfer */ | |
XMC_DMA_CH_Enable(DMA_HW, DMA_NUM); | |
/* Wait for completion of DMA transfer */ | |
while (transfer_done == false) | |
{ | |
} | |
#if ENABLE_XMC_DEBUG_PRINT | |
printf("DMA transfer of buffer 2 completed\r\n"); | |
custom_delay(200000); | |
#endif | |
while(1); | |
} | |
/* [] END OF FILE */ |