AnsweredAssumed Answered

S32K FLEXIO UART

Question asked by lei liu on Apr 23, 2019
Latest reply on May 13, 2019 by lei liu

I use FLEXIO to simulate UART serial port, but I can only achieve the sending function but not the receiving function.

void FLEXIO_UART_port_init (void)
{
/*!
* Pins definitions
* ===================================================
*
* Pin number | Function
* ----------------- |------------------
* PTA11 | FXIO_D1 Rx (J6-01)
* PTA0 | FXIO_D2 Tx (J6-02)
*
*/
PCC->PCCn[PCC_PORTA_INDEX]|=PCC_PCCn_CGC_MASK; /* Enable clock for PORTD */

PORTA->PCR[0] = PORT_PCR_MUX(4); /* Port A0: MUX = FXIO_D2 */
PORTA->PCR[11] = PORT_PCR_MUX(4); /* Port A11: MUX = FXIO_D1 */
PTA->PDDR |= 1<<0; /* Port A0: Data Direction = output */
PTA->PDDR &= ~(1<<11); /* Port A11: Data Direction= input (default) */
}

void FLEXIO_UART_init(void)
{

PCC->PCCn[PCC_FlexIO_INDEX] &= ~PCC_PCCn_CGC_MASK; /* Disable FLEXIO clock for config. */
PCC->PCCn[PCC_FlexIO_INDEX] |= PCC_PCCn_PCS(2) /* Select clk option 2 = SIRCDIV2_CLK */
| PCC_PCCn_CGC_MASK; /* Enable FLEXIO clock */

/*Init Flexio*/
FLEXIO->CTRL &= (uint32_t)(~(FLEXIO_CTRL_SWRST_MASK));
FLEXIO->CTRL = FLEXIO_CTRL_SWRST(1);
FLEXIO->CTRL = 0x0U;
INT_SYS_EnableIRQ(FLEXIO_IRQn);
/*enable Flexio*/
FLEXIO->CTRL &= (uint32_t)(~(FLEXIO_CTRL_FLEXEN_MASK));
FLEXIO->CTRL |= FLEXIO_CTRL_FLEXEN(1);

/*!
* FlexIO Timer Configuration:
* =============================
*/
FLEXIO->TIMCFG[0] |=
FLEXIO_TIMCFG_TIMRST(6) /* Timer reset on trigger rising edge */
|FLEXIO_TIMCFG_TIMOUT(0) /* Output logic 1 when enabled */
|FLEXIO_TIMCFG_TIMDIS(2) /* Timer disabled on Timer compare */
|FLEXIO_TIMCFG_TIMENA(2) /* Timer enabled on Trigger high */
|FLEXIO_TIMCFG_TSTART(1) /* Start bit enabled */
|FLEXIO_TIMCFG_TSTOP(3); /* Stop bit is enabled on timer compare and timer disable */

/*!
* FlexIO Timer Control:
* ============================
*/
FLEXIO->TIMCTL[0] |=
FLEXIO_TIMCTL_TRGSEL(1) /* Trigger select: Shifter 0 status flag */
|FLEXIO_TIMCTL_TRGPOL(1) /* Trigger active low */
|FLEXIO_TIMCTL_TIMOD(1) /* Dual 8-bit counters baud mode */
|FLEXIO_TIMCTL_TRGSRC(1) /* Internal trigger */
|FLEXIO_TIMCTL_PINSEL(2) /* Select FXIO_D2 */
|FLEXIO_TIMCTL_PINCFG(0); /* timer pin output disabled */

/*!
* FlexIO Timer Compare Value:
* =============================
* In 8-bit baud counter mode, the lower 8-bits configure the baud rate
* divider equal to (CMP[7:0] + 1) * 2. The upper 8-bits configure the
* number of bits in each word equal to (CMP[15:8] + 1) / 2.
*/
FLEXIO->TIMCMP[0] = 0x0FCF; /* 8 bits Transfer */
/* Baud Rate = 9600/s */

/*!
* FlexIO Shifter Configuration:
* ===============================
*/
FLEXIO->SHIFTCFG[0] |=
FLEXIO_SHIFTCFG_SSTART(2) /* Start bit '0' */
|FLEXIO_SHIFTCFG_SSTOP(3); /* Stop bit '1' */

/*!
* FlexIO Shifter Control:
* =============================
*/
FLEXIO->SHIFTCTL[0] |=
FLEXIO_SHIFTCTL_TIMSEL(0) /* Select Timer 0 */
|FLEXIO_SHIFTCTL_PINSEL(2) /* Select FXIO_D2 */
|FLEXIO_SHIFTCTL_PINCFG(3) /* Shifter pin as Output */
|FLEXIO_SHIFTCTL_SMOD(2); /* Transmit mode */


/* setup FLEX_D1 to receive */

/*!
* FlexIO Timer Configuration:
* =============================
*/
FLEXIO->TIMCFG[1] |=
FLEXIO_TIMCFG_TIMOUT(2) /* Timer output is logic one when enabled and is not affected by timer reset */
/* FLEXIO_TIMER_INITOUT_ONE_RESET = 0x02U */
/* Tx/Rx */
|FLEXIO_TIMCFG_TIMDEC(0) /* Decrement counter on FlexIO clock, Shift clock equals Timer output. */
/*FLEXIO_TIMER_DECREMENT_CLK_SHIFT_TMR = 0x00U */
|FLEXIO_TIMCFG_TIMRST(2) /* Timer never reset */
/* FLEXIO_TIMER_INITOUT_ONE_RESET = 0x02U */
|FLEXIO_TIMCFG_TIMDIS(2) /* 2 - Timer disabled on Timer compare (upper 8-bits match and decrement) */
/* 3 - Timer disabled on Timer compare (upper 8-bits match and decrement) and Trigger Low */
/* FLEXIO_TIMER_DISABLE_TIM_CMP = 0x02U, */
|FLEXIO_TIMCFG_TIMENA(4) /* Timer enabled on Pin rising edge */
/* FLEXIO_TIMER_ENABLE_PIN_POSEDGE = 0x04U */
|FLEXIO_TIMCFG_TSTOP(2) /* Stop bit is enabled on timer disable;FLEXIO_TIMER_STOP_BIT_TIM_DIS = 0x02U */
|FLEXIO_TIMCFG_TSTART(1); /* Start bit enabled; FLEXIO_TIMER_START_BIT_ENABLED = 0x01U */

/*!
* FlexIO Timer Control:
* ============================
*/
FLEXIO->TIMCTL[1] |=
FLEXIO_TIMCTL_TIMOD(0) /* Dual 8-bit counters baud mode */
/* Tx/Rx */
|FLEXIO_TIMCTL_TRGSEL(0) /* External trigger D0 input FLEXIO_TRIGGER_SOURCE_EXTERNAL = 0x00U*/
|FLEXIO_TIMCTL_TRGPOL(0) /* Trigger active high FLEXIO_TRIGGER_POLARITY_HIGH = 0x00U,*/
|FLEXIO_TIMCTL_TRGSRC(0) /* External trigger FLEXIO_TRIGGER_SOURCE_EXTERNAL = 0x00U*/
|FLEXIO_TIMCTL_PINSEL(1) /* 1: Select FXIO_D1, 0: Select FXIO_D0 */
|FLEXIO_TIMCTL_PINCFG(0) /* timer pin output disabled */
|FLEXIO_TIMCTL_PINPOL(1);
/*!
* FlexIO Timer Compare Value:
* =============================
* In 8-bit baud counter mode, the lower 8-bits configure the baud rate
* divider equal to (CMP[7:0] + 1) * 2. The upper 8-bits configure the
* number of bits in each word equal to (CMP[15:8] + 1) / 2.
*/
FLEXIO->TIMCMP[1] = 0x0FCF; /* 8 bits Transfer */
/* Baud Rate = 19200b/s */

/*!
* FlexIO Shifter Configuration:
* ===============================
*/
FLEXIO->SHIFTCFG[1] |=
FLEXIO_SHIFTCFG_INSRC(0) /* Input source is selected pin */
|FLEXIO_SHIFTCFG_SSTART(2) /* Start bit '0';FLEXIO_SHIFTER_START_BIT_0 = 0x02U*/
|FLEXIO_SHIFTCFG_SSTOP(3); /* Stop bit '1'; FLEXIO_SHIFTER_STOP_BIT_1 = 0x03U */

/*!
* FlexIO Shifter Control:
* =============================
*/
FLEXIO->SHIFTCTL[1] |=
FLEXIO_SHIFTCTL_TIMSEL(1) /* Select Timer 1 */
|FLEXIO_SHIFTCTL_PINSEL(1) /* Select FXIO_D1*/
/* Tx/Rx */
|FLEXIO_SHIFTCTL_TIMPOL(1) /* Shift on negedge of Shift clock; FLEXIO_TIMER_POLARITY_NEGEDGE = 0x01U */
|FLEXIO_SHIFTCTL_PINCFG(0) /* Shifter pin output disabled; FLEXIO_PIN_CONFIG_DISABLED = 0x00U */
|FLEXIO_SHIFTCTL_PINPOL(0) /* Pin is active high ; FLEXIO_PIN_POLARITY_HIGH = 0x00U*/
|FLEXIO_SHIFTCTL_SMOD(1); /* Receive mode; FLEXIO_SHIFTER_MODE_RECEIVE = 0x01U */

}

This is the sending function.

void FLEXIO_UART_transmit_char(char send)
{
/*!
* Transmit a single char:
* =======================
*/
while(! (FLEXIO->SHIFTSTAT & 0x1) ); /* Wait for buffer empty */
FLEXIO->SHIFTBUF[0] = send; /* Send data to PTA0(Tx line) */
}

static int rx_char = 0;
static int rx_loop_cnt = 0;
char FLEXIO_UART_receive_char(void)
{
char byte;
/*!
* Receive a single char:
* =======================
*/
while( !(FLEXIO->SHIFTSTAT&(1<<(1)<<FLEXIO_SHIFTSTAT_SSF_SHIFT))){};
byte = FLEXIO->SHIFTBUFBYS[1];
if ((byte == 'g') ||(byte == 'G'))
{
rx_char++;
}
return byte;
}

Outcomes