void tScStartup(uint32_t arg1)
{
/* Selecting the clock source for the UART0. We are selecting the MCGFLLCLK, 24Mhz */
SIM_SOPT2 |= (SIM_SOPT5_UART0RXSRC(0x01) | SIM_SOPT5_UART1RXSRC(0x01)); // Set UART Source
/* Make sure the clocks are enabled to all UARTs */
SIM_SCGC4 |= (SIM_SCGC4_UART0_MASK | SIM_SCGC4_UART1_MASK | SIM_SCGC4_UART3_MASK);
SIM_SCGC1 |= (SIM_SCGC1_UART5_MASK);
/* Enable a full set of UART signals for each of the UART modules */
/* Enable UART0 functions on PTD */
PORTD_PCR6 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
PORTD_PCR7 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
/* Enable the UART1 functions on PTC */
PORTC_PCR3 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
PORTC_PCR4 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
/* Enable the UART3 functions on PTC */
PORTC_PCR16 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
PORTC_PCR17 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
/* Enable the UART5 functions on PTE */
PORTE_PCR8 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
PORTE_PCR9 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
uart_init (UART0_BASE_PTR, 120000, 9600);
uart_init (UART1_BASE_PTR, 120000, 9600);
uart_init (UART3_BASE_PTR, 60000, 115200);
uart_init (UART5_BASE_PTR, 60000, 115200);
/* Enable receiver and transmitter and enable DMA on UART0*/
UART_C2_REG(UART0_BASE_PTR) |= UART_C2_RIE_MASK;
/* Enable DMA*/
UART_C5_REG(UART0_BASE_PTR) |= (UART_C5_RDMAS_MASK);
UART_PFIFO_REG(UART0_BASE_PTR) = 0;
/* Enable receiver and transmitter and enable DMA on UART1*/
UART_C2_REG(UART1_BASE_PTR) |= UART_C2_RIE_MASK;
/* Enable DMA*/
UART_C5_REG(UART1_BASE_PTR) |= (UART_C5_RDMAS_MASK);
UART_PFIFO_REG(UART1_BASE_PTR) = 0;
/* Enable receiver and transmitter and enable DMA on UART3*/
UART_C2_REG(UART3_BASE_PTR) |= UART_C2_RIE_MASK;
/* Enable DMA*/
UART_C5_REG(UART3_BASE_PTR) |= (UART_C5_RDMAS_MASK);
UART_PFIFO_REG(UART3_BASE_PTR) = 0;
/* Enable receiver and transmitter and enable DMA on UART5*/
UART_C2_REG(UART5_BASE_PTR) |= UART_C2_RIE_MASK;
/* Enable DMA*/
UART_C5_REG(UART5_BASE_PTR) |= (UART_C5_RDMAS_MASK);
UART_PFIFO_REG(UART5_BASE_PTR) = 0;
SIM_SCGC6 |= SIM_SCGC6_DMAMUX_MASK;
SIM_SCGC7 |= SIM_SCGC7_DMA_MASK;
UART0_to_UART1_DMA_init();
UART1_to_UART0_DMA_init();
UART3_to_UART5_DMA_init();
UART5_to_UART3_DMA_init();
while(1)
{
_time_delay(10000000);
}
}// end void tScStartup(uint_32 arg1)
void UART0_to_UART1_DMA_init(void)
{
/* Let us DISABLE DMA channel so that we can change the source and then ENABLE it */
DMAMUX_CHCFG0 = 0;
/*Enable DMA MUX ch 0 For DMAMUX channel Cfg Src, Please see Page:94 of USer Manual of K64 here: http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K64P144M120SF5RM.pdf#page=94 */
DMAMUX_CHCFG0 |= DMAMUX_CHCFG_SOURCE(2); //Enable channel 2, Request souurce = UART 0 Receive
DMAMUX_CHCFG0 |= DMAMUX_CHCFG_ENBL_MASK;
/*** Initialize CH0 -> 1 byte received from UART 0 ***/
/* Set the Source Address*/
DMA_TCD0_SADDR = (uint32_t)(&UART0_D);
/* Destination address */
DMA_TCD0_DADDR = (uint32_t)(&UART1_D);
/* Source offset*/
DMA_TCD0_SOFF = 0; // No offset
/*Modulo off and port sizes*/
//DMA_TCD0_ATTR = DMA_ATTR_SSIZE(4) | DMA_ATTR_SMOD(0) | DMA_ATTR_DSIZE(4) | DMA_ATTR_DMOD(0); //16-byte burst
DMA_TCD0_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); //source and destination size 0 = 8 bits
/* Transfer size */
DMA_TCD0_NBYTES_MLNO = 1; //1 byte
/* No adjustment to source address */
DMA_TCD0_SLAST = 0;//-32
/* Destination offset*/
DMA_TCD0_DOFF = 0;
/* No link channel, transactions */
DMA_TCD0_CITER_ELINKNO = 1; //1 major loop
/* Adjustment to destination address */
DMA_TCD0_DLASTSGA = 0;//-32
/* No link channel, transactions */
DMA_TCD0_BITER_ELINKNO = 1; //1 major loop
/*...*/
//DMA_TCD0_CSR = DMA_CSR_DREQ_MASK; //Clear channel's ERQ bit when major loop is complete
DMA_TCD0_CSR = 0; //Never clear channel's ERQ bit
/*Start the sequence*/
DMA_ERQ |= DMA_ERQ_ERQ0_MASK;
}
void UART1_to_UART0_DMA_init(void)
{
/* Let us DISABLE DMA channel so that we can change the source and then ENABLE it */
DMAMUX_CHCFG1 = 0;
/*Enable DMA MUX ch 0 For DMAMUX channel Cfg Src, Please see Page:94 of USer Manual of K64 here: http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K64P144M120SF5RM.pdf#page=94 */
DMAMUX_CHCFG1 |= DMAMUX_CHCFG_SOURCE(4); //Enable channel 4, Request souurce = UART 1 Receive
DMAMUX_CHCFG1 |= DMAMUX_CHCFG_ENBL_MASK;
/*** Initialize CH0 -> 1 byte received from UART 0 ***/
/* Set the Source Address*/
DMA_TCD1_SADDR = (uint32_t)(&UART1_D);
/* Destination address */
DMA_TCD1_DADDR = (uint32_t)(&UART0_D);
/* Source offset*/
DMA_TCD1_SOFF = 0; // No offset
/*Modulo off and port sizes*/
//DMA_TCD0_ATTR = DMA_ATTR_SSIZE(4) | DMA_ATTR_SMOD(0) | DMA_ATTR_DSIZE(4) | DMA_ATTR_DMOD(0); //16-byte burst
DMA_TCD1_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); //source and destination size 0 = 8 bits
/* Transfer size */
DMA_TCD1_NBYTES_MLNO = 1; //1 byte
/* No adjustment to source address */
DMA_TCD1_SLAST = 0;//-32
/* Destination offset*/
DMA_TCD1_DOFF = 0;
/* No link channel, transactions */
DMA_TCD1_CITER_ELINKNO = 1; //1 major loop
/* Adjustment to destination address */
DMA_TCD1_DLASTSGA = 0;//-32
/* No link channel, transactions */
DMA_TCD1_BITER_ELINKNO = 1; //1 major loop
/*...*/
//DMA_TCD0_CSR = DMA_CSR_DREQ_MASK; //Clear channel's ERQ bit when major loop is complete
DMA_TCD1_CSR = 0; //Never clear channel's ERQ bit
/*Start the sequence*/
DMA_ERQ |= DMA_ERQ_ERQ1_MASK;
}
void UART3_to_UART5_DMA_init(void)
{
/* Let us DISABLE DMA channel so that we can change the source and then ENABLE it */
DMAMUX_CHCFG2 = 0;
/*Enable DMA MUX ch 0 For DMAMUX channel Cfg Src, Please see Page:94 of USer Manual of K64 here: http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K64P144M120SF5RM.pdf#page=94 */
DMAMUX_CHCFG2 |= DMAMUX_CHCFG_SOURCE(8); //Enable channel 8, Request souurce = UART 3 Receive
DMAMUX_CHCFG2 |= DMAMUX_CHCFG_ENBL_MASK;
/*** Initialize CH0 -> 1 byte received from UART 0 ***/
/* Set the Source Address*/
DMA_TCD2_SADDR = (uint32_t)(&UART3_D);
/* Destination address */
DMA_TCD2_DADDR = (uint32_t)(&UART5_D);
/* Source offset*/
DMA_TCD2_SOFF = 0; // No offset
/*Modulo off and port sizes*/
//DMA_TCD0_ATTR = DMA_ATTR_SSIZE(4) | DMA_ATTR_SMOD(0) | DMA_ATTR_DSIZE(4) | DMA_ATTR_DMOD(0); //16-byte burst
DMA_TCD2_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); //source and destination size 0 = 8 bits
/* Transfer size */
DMA_TCD2_NBYTES_MLNO = 1; //1 byte
/* No adjustment to source address */
DMA_TCD2_SLAST = 0;//-32
/* Destination offset*/
DMA_TCD2_DOFF = 0;
/* No link channel, transactions */
DMA_TCD2_CITER_ELINKNO = 1; //1 major loop
/* Adjustment to destination address */
DMA_TCD2_DLASTSGA = 0;//-32
/* No link channel, transactions */
DMA_TCD2_BITER_ELINKNO = 1; //1 major loop
/*...*/
//DMA_TCD0_CSR = DMA_CSR_DREQ_MASK; //Clear channel's ERQ bit when major loop is complete
DMA_TCD2_CSR = 0; //Never clear channel's ERQ bit
/*Start the sequence*/
DMA_ERQ |= DMA_ERQ_ERQ2_MASK;
}
void UART5_to_UART3_DMA_init(void)
{
/* Let us DISABLE DMA channel so that we can change the source and then ENABLE it */
DMAMUX_CHCFG3 = 0;
/*Enable DMA MUX ch 0 For DMAMUX channel Cfg Src, Please see Page:94 of USer Manual of K64 here: http://cache.freescale.com/files/microcontrollers/doc/ref_manual/K64P144M120SF5RM.pdf#page=94 */
DMAMUX_CHCFG3 |= DMAMUX_CHCFG_SOURCE(11); //Enable channel 11, Request souurce = UART 5 Receive
DMAMUX_CHCFG3 |= DMAMUX_CHCFG_ENBL_MASK;
/*** Initialize CH0 -> 1 byte received from UART 0 ***/
/* Set the Source Address*/
DMA_TCD3_SADDR = (uint32_t)(&UART5_D);
/* Destination address */
DMA_TCD3_DADDR = (uint32_t)(&UART3_D);
/* Source offset*/
DMA_TCD3_SOFF = 0; // No offset
/*Modulo off and port sizes*/
//DMA_TCD0_ATTR = DMA_ATTR_SSIZE(4) | DMA_ATTR_SMOD(0) | DMA_ATTR_DSIZE(4) | DMA_ATTR_DMOD(0); //16-byte burst
DMA_TCD3_ATTR = DMA_ATTR_SSIZE(0) | DMA_ATTR_DSIZE(0); //source and destination size 0 = 8 bits
/* Transfer size */
DMA_TCD3_NBYTES_MLNO = 1; //1 byte
/* No adjustment to source address */
DMA_TCD3_SLAST = 0;//-32
/* Destination offset*/
DMA_TCD3_DOFF = 0;
/* No link channel, transactions */
DMA_TCD3_CITER_ELINKNO = 1; //1 major loop
/* Adjustment to destination address */
DMA_TCD3_DLASTSGA = 0;//-32
/* No link channel, transactions */
DMA_TCD3_BITER_ELINKNO = 1; //1 major loop
/*...*/
//DMA_TCD0_CSR = DMA_CSR_DREQ_MASK; //Clear channel's ERQ bit when major loop is complete
DMA_TCD3_CSR = 0; //Never clear channel's ERQ bit
/*Start the sequence*/
DMA_ERQ |= DMA_ERQ_ERQ3_MASK;
}
void uart_init (UART_MemMapPtr uartch, int sysclk, int baud)
{
register uint16 ubd, brfa;
uint8 temp;
/* Enable the clock to the selected UART */
if(uartch == UART0_BASE_PTR)
{
SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
}
else
{
if (uartch == UART1_BASE_PTR)
{
SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
}
else
{
if (uartch == UART2_BASE_PTR)
{
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;
}
else
{
if(uartch == UART3_BASE_PTR)
{
SIM_SCGC4 |= SIM_SCGC4_UART3_MASK;
}
else
{
if(uartch == UART4_BASE_PTR)
{
SIM_SCGC1 |= SIM_SCGC1_UART4_MASK;
}
else
{
SIM_SCGC1 |= SIM_SCGC1_UART5_MASK;
}
}
}
}
}
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
/* We need all default settings, so entire register is cleared */
UART_C1_REG(uartch) = 0;
/* Calculate baud settings */
ubd = (uint16)((sysclk*1000)/(baud * 16));
/* Save off the current value of the UARTx_BDH except for the SBR */
temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
UART_BDH_REG(uartch) = temp | UART_BDH_SBR(((ubd & 0x1F00) >> 8));
UART_BDL_REG(uartch) = (uint8)(ubd & UART_BDL_SBR_MASK);
/* Determine if a fractional divider is needed to get closer to the baud rate */
brfa = (((sysclk*32000)/(baud * 16)) - (ubd * 32));
/* Save off the current value of the UARTx_C4 register except for the BRFA */
temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
UART_C4_REG(uartch) = temp | UART_C4_BRFA(brfa);
/* Enable receiver and transmitter */
UART_C2_REG(uartch) |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
}