I did not see a link to upload the file, so I am inserting it here:
//*****************************************************************************
//*****************************************************************************
// main.c
//*****************************************************************************
//*****************************************************************************
#include <mqx.h>
#include <bsp.h>
#include <serial.h>
#include <dma.h>
#if ! BSPCFG_ENABLE_IO_SUBSYSTEM
#error This application requires BSPCFG_ENABLE_IO_SUBSYSTEM defined non-zero in user_config.h. Please recompile BSP with this option.
#endif
#ifndef BSP_DEFAULT_IO_CHANNEL_DEFINED
#error This application requires BSP_DEFAULT_IO_CHANNEL to be not NULL. Please set corresponding BSPCFG_ENABLE_TTYx to non-zero in user_config.h and recompile BSP with this option.
#endif
// Task IDs
#define UART_TASK 8
extern void uart_task(uint32_t);
void uart1_dma_config(void);
void uart1_fio_config(void);
const TASK_TEMPLATE_STRUCT MQX_template_list[] =
{
// Task Index, Function, Stack, Priority, Name, Attributes, Param, Time Slice
{ UART_TASK, uart_task, 1024, 12, "uart", MQX_AUTO_START_TASK, 0, 0 },
{ 0 }
};
volatile uint8_t write_buf[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
volatile uint8_t read_buf[16] = {0};
//-----------------------------------------------------------
// UART1
// Port assignment: UART1 Tx = PTC4; UART1 Rx = PTC3
// TWR header pins: UART1 Tx = A69; UART1 Rx = A38,A64
// UART2
// Port assignment: UART2 Tx = PTD3; UART2 Rx = PTD2
// TWR header pins: UART2 Tx = A77; UART2 Rx = A76
//
// Jumper wire connected to TWR header pins A69 - A76
//-----------------------------------------------------------
void uart_task(uint32_t initial_data)
{
MQX_FILE_PTR uart2_serial_device;
uint32_t baud_rate = 115200;
int num_chars_rx;
printf("UART task started\n");
// UART2
// Configure PTD2 & PTD3 for ALT3 function
PORTD_PCR2 = PORT_PCR_MUX(3);
PORTD_PCR3 = PORT_PCR_MUX(3);
uart2_serial_device = fopen("ittyc:", (void *) NULL);
_io_ioctl(uart2_serial_device, IO_IOCTL_SERIAL_SET_BAUD, &baud_rate);
// Uncomment only one of these function calls...
//----------------------------------------------
uart1_dma_config(); // DMA method under test
//uart1_fio_config(); // Uses system calls fopen, fwrite
//----------------------------------------------
_time_delay(1000); // 1 sec delay
num_chars_rx = read(uart2_serial_device, (void *) &read_buf[0], 4);
printf("%d characters read by UART2\n", num_chars_rx);
printf("-->> UART2: %d %d %d %d\n", read_buf[0], read_buf[1], read_buf[2], read_buf[3]);
_task_block();
_mqx_exit(0);
}
//-----------------------------------------------------------
//
//
//-----------------------------------------------------------
void uart1_dma_config(void)
{
// Enable DMA clocks
SIM_SCGC6 |= SIM_SCGC6_DMAMUX0_MASK;
SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
// Configure UART TX & RX pins
SIM_SCGC5 |= SIM_SCGC5_PORTC_MASK;
PORTC_PCR3 = PORT_PCR_MUX(3);
PORTC_PCR4 = PORT_PCR_MUX(3);
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK; // Enable clock to UART1
// Set baud rate to 115200 (120 MHz system clock)
UART1_BDH = 0x00; // Divisor high byte
UART1_BDL = 0x41; // Divisor low byte
UART1_C4 = 0x03; // Fine adjustment
// Set FIFO water marks
UART1_PFIFO = UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK;
UART1_TWFIFO = 0;
UART1_RWFIFO = 0;
// Enable transmitter
UART1_C1 = 0;
UART1_C3 = 0;
UART1_S2 = 0;
UART1_C2 = UART_C2_TIE_MASK | UART_C2_TE_MASK;
UART1_C5 = UART_C5_TDMAS_MASK;
// Configure TX DMA
DMAMUX0_CHCFG0 = 0;
DMA_CR = DMA_CR_ERCA_MASK | DMA_CR_ERGA_MASK | DMA_CR_EMLM_MASK;
DMA_CERQ = DMA_CERQ_CERQ(0);
DMA_TCD0_CSR = DMA_CSR_DREQ_MASK;
DMA_TCD0_BITER_ELINKNO = 0;
DMA_TCD0_CITER_ELINKNO = 0;
DMA_TCD0_SADDR = (uint32_t) &write_buf[0];
DMA_TCD0_SOFF = 1;
DMA_TCD0_SLAST = -16;
DMA_TCD0_DADDR = (uint32_t) &(UART1_D);
DMA_TCD0_DOFF = 0;
DMA_TCD0_DLASTSGA = 0;
DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); // Source & destination data size = 8 bits
DMA_TCD0_NBYTES_MLNO = 1;
DMA_TCD0_BITER_ELINKNO = 0;
DMA_TCD0_CITER_ELINKNO = 0;
DMAMUX0_CHCFG0 = DMAMUX_CHCFG_SOURCE(5); // UART1 TX = 5 (DMA MUX request source, ref. manual pg. 101)
DMAMUX0_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK;
DMA_TCD0_BITER_ELINKNO = DMA_BITER_ELINKNO_BITER(16);
DMA_TCD0_CITER_ELINKNO = DMA_CITER_ELINKNO_CITER(16);
// Start DMA
DMA_TCD0_CSR |= DMA_CSR_START_MASK;
DMA_SERQ = DMA_SERQ_SERQ(0);
}
//-----------------------------------------------------------
//
//
//-----------------------------------------------------------
void uart1_fio_config(void)
{
MQX_FILE_PTR uart1_serial_device;
uint32_t baud_rate = 115200;
int num_chars_tx;
// Configure PTC3 & PTC4 for ALT3 function
PORTC_PCR3 = PORT_PCR_MUX(3);
PORTC_PCR4 = PORT_PCR_MUX(3);
uart1_serial_device = fopen("ittyb:", (void *) NULL);
_io_ioctl(uart1_serial_device, IO_IOCTL_SERIAL_SET_BAUD, &baud_rate);
num_chars_tx = write(uart1_serial_device, (void *) &write_buf[0], 16);
printf("%d characters written by UART1\n", num_chars_tx);
}
/* EOF */