Please be advised that in the following code comments might not be correct, but the code itself is working.
// General defines
#define SSS_OFFSET 512
#define UARTLIN_MSCR0 (712 - SSS_OFFSET)
// LinFlex0@ PB2/PB3
#define UARTNUM 0
#define UARTLIN LINFlexD_0
#define UARTLIN_TXGIO PB2 // #define PB2 18
#define UARTLIN_TXSSS 1
#define UARTLIN_RXGIO PB3 // #define PB3 19
#define UARTLIN_RXSSS 2
void config_sci(uint32_t speed)
{
uint32_t prediv;
// clear buffers
//memset( &sci_rx_buffer[0][0],0,SCI_RX_BUFER_LINES*SCI_RX_BUFER_CHARPERLINE );
//memset( &sci_tx_buffer[0][0],0,SCI_TX_BUFER_LINES*SCI_TX_BUFER_CHARPERLINE );
// config LINLFEX
UARTLIN.LINCR1.B.SLEEP = 0; // LINFLEX in init mode
UARTLIN.LINCR1.B.INIT = 1; // LINFLEX in init mode
UARTLIN.UARTCR.B.UART = 1; // UART
UARTLIN.UARTCR.B.TDFL_TFC = 0; // 1byte buffer //change!
UARTLIN.UARTCR.B.RDFL_RFC = 0; // 1byte buffer
UARTLIN.UARTCR.B.RFBM = 0; // Rx buffer-mode
UARTLIN.UARTCR.B.TFBM = 0; // Tx buffer-mode
UARTLIN.UARTCR.B.RxEn = 1; // rx enable
UARTLIN.UARTCR.B.TxEn = 1; // tx enable
UARTLIN.UARTCR.B.PC1 = 0; //
UARTLIN.UARTCR.B.PC0 = 0; //
UARTLIN.UARTCR.B.PCE = 0; // no Parity
UARTLIN.UARTCR.B.WL1 = 0; // 8bit data
UARTLIN.UARTCR.B.WL0 = 1; // 8bit data
// Pre-division
if (UARTNUM == 0) {
// Special case, LIN 0 clock is F40(40MHz), not F80(80MHz)
// UARTLIN(0) => F40 | 40MHz / speed
prediv = 40000000 / speed;
} else {
// UARTLIN(1-17) => F80 | 80MHz / speed
prediv = 80000000 / speed;
}
// FBRR = Fxx / speed / 16, fraction part only
UARTLIN.LINFBRR.R = (uint16_t)(prediv & 15); /* Fractional divider. */
// IBRR = Fxx / speed / 16, integer part only
UARTLIN.LINIBRR.R = (uint16_t)(prediv >> 4); /* Integer divider. */
UARTLIN.LINCR1.B.INIT = 0; // LINFLEX in init mode
UARTLIN.LINIER.B.DRIE = 1; // enable SCI.RX interrupt
UARTLIN.LINIER.B.DTIE = 1; // enable SCI.TX interrupt
/* LINx_TX */
SIUL2.MSCR[UARTLIN_TXGIO].B.OBE = 1;
SIUL2.MSCR[UARTLIN_TXGIO].B.SSS = UARTLIN_TXSSS ;
/* LINx_RX */
SIUL2.MSCR[UARTLIN_RXGIO].B.OBE = 0;
SIUL2.MSCR[UARTLIN_RXGIO].B.IBE = 1;
SIUL2.IMCR[UARTLIN_MSCR0 + UARTNUM].B.SSS = UARTLIN_RXSSS ;
}
void write_char(char value)
{
UARTLIN.BDRL.B.DATA0 = value;
do {} while ( UARTLIN.UARTSR.B.DTFTFF == 0 );
UARTLIN.UARTSR.R = 0x00000002; // clear DTF bit, only
if ( value == '\n' ) write_char('\r');
}