//***************************************************************************** // (C) Automotive Lighting Reutlingen GmbH // Tuebinger Strasse 123, 72762 Reutlingen, Germany // // Automotive Lighting Reutlingen GmbH owns all the rights to this work. // This work shall not be copied, reproduced, used, modified, transferred // or its information shall not be disclosed without the prior written // authorization of Automotive Lighting Reutlingen GmbH. //----------------------------------------------------------------------------- /// \file CddUart.c /// /// \brief /// //----------------------------------------------------------------------------- //#include #include #include /* LPUART - Peripheral instance base addresses */ #define UART_BASE 0x4006B000u /** Peripheral LPUART base pointer */ //#define CDDUARTBASE ((volatile UART_NXP_S32K146_UnionType *)UART_BASE) #define CDDUART_BUFFERSIZE_MAX 50 static volatile UART_NXP_S32K146_UnionType* CddMlcUart_pst_UART_Register = (volatile UART_NXP_S32K146_UnionType*)UART_BASE; static uint8 CddUart_aucLocalBuffer[CDDUART_BUFFERSIZE_MAX]; //----------------------------------------------------------------------------- /// \brief CddUart_Init /// /// \descr /// /// \param - /// /// \return void //----------------------------------------------------------------------------- void CddUart_Init(void) { CddMlcUart_pst_UART_Register->uCTRL.sBit = (tsCddMlcUart_NXP_CTRL) { .u1RE = 0UL, // receiver disabled .u1TE = 0UL, // transmitter disabled }; /* Configure the UART interface - 8 bits of data, 1 stop bit*/ CddMlcUart_pst_UART_Register->uBAUD.sBit = (tsCddMlcUart_NXP_BAUD){ .u13SBR = 3UL, // SBR= 3 =0x03 (baud divisor= ~12 (= 8Mhz / 115 200 /3 ~= 23 = OSR) .u1SBNS = 0UL, // One stop bit .u1RXEDGIE = 0UL, // RX input active angle interrupt disabled .u1LBKDIE = 0UL, // LIN Break Detect Interrupt Disabled .u1BOTHEDGE = 0UL, // BOTHEDGE=0: receiver samples only on rising edge .u2MATCFG = 0UL, // Match configuration disabled .u4OSR = 22UL, // OSR = 22 = 0x16 : Over sampling ratio = 22 + 1 = 23 .u1M10 = 0UL, // M10=0: Rx and Tx use 7 to 9 bit data characters .u1MAEN1 = 0UL, // Match address disabled .u1MAEN2 = 0UL, // Match address disabled .u1RDMAE = 1UL, // Receiver DMA request enabled .u1TDMAE = 1UL, // Transmitter DMA request enabled }; /* Configure the UART Control - enable transmitter/receiver, No parity, 8 bit data character*/ CddMlcUart_pst_UART_Register->uCTRL.sBit = (tsCddMlcUart_NXP_CTRL){ .u1PE = 0UL, // no hardware parity generation or checking .u1M = 0UL, // Receiver and transmitter use 8-bit data characters .u1DOZEEN = 0UL, // LPUART is enabled in Doze mode (=low power) .u1LOOPS = 0UL, // TX and RX use different pins .u1M7 = 0UL, // receiver and transmitter use 8-bit to 10-bit data characters .u1SBK = 0UL, // normal transmitter operation - no break character .u1TIE = 0UL, // transmit interrupt disabled (hardware interrupt disabled) .u1TCIE = 0UL, // transmission complete interrupt disabled (hardware interrupt disabled) }; /* Configure the UART Status*/ CddMlcUart_pst_UART_Register->uSTAT.sBit = (tsCddMlcUart_NXP_STAT){ .u1RXINV = 0UL, // Receive Data not inverted .u1MSBF = 0UL, // LSB (least significant bit) first .u1BRK13 = 0UL, }; CddMlcUart_pst_UART_Register->uFIFO.sBit = (tsCddMlcUart_NXP_FIFO){ .u1TXFE = 1UL, // Transmit FIFO is enabled. Buffer is depth indicated by TXFIFOSIZE. .u1RXFE = 1UL, // Receive FIFO is enabled. Buffer is depth indicated by RXFIFOSIZE. (= read-only field = 4 bytes) }; CddMlcUart_pst_UART_Register->uWATER.sBit = (tsCddMlcUart_NXP_WATER){ .u2TXWATER = 0UL, // When the number of datawords in the receive FIFO/buffer is greater than the value in this register field, an interrupt is generated .u2RXWATER = 0UL, }; CddMlcUart_pst_UART_Register->uCTRL.sBit = (tsCddMlcUart_NXP_CTRL){ .u1RE = 1UL, // receiver enabled .u1TE = 1UL, // transmitter enabled }; } //----------------------------------------------------------------------------- /// \brief CddUart_SendByte /// /// \descr this functions sends a byte via uart tx /// /// \param ucByte byte to send /// /// \return void //----------------------------------------------------------------------------- void CddUart_SendByte(uint8 ucByte) { //wait until transmission is finished //TDRE indicates another character may be written to the transmit data buffer at the data register : TDRE =0 buffer full while ((CddMlcUart_pst_UART_Register->uSTAT.u32Register & LPUART_STAT_TDRE_MASK) >> LPUART_STAT_TDRE_SHIFT == 0) {} CddMlcUart_pst_UART_Register->uDATA.u32Register = ucByte; // Send data } //----------------------------------------------------------------------------- /// \brief CddUart_SendBytesSync /// /// \descr this function sends bytes direct over uart /// /// \param aucBytes /// \param ucSize /// /// \return void //----------------------------------------------------------------------------- void CddUart_SendBytesSync(uint8 aucBytes[], uint8 ucSize) { uint8 ucIDx; for (ucIDx = 0; ucIDx < ucSize; ucIDx++) { CddUart_SendByte(aucBytes[ucIDx]); } //wait until transmission is finished while ((CddMlcUart_pst_UART_Register->uSTAT.u32Register & LPUART_STAT_TDRE_MASK) >> LPUART_STAT_TDRE_SHIFT == 0){} } //----------------------------------------------------------------------------- /// \brief CddUart_SendBytes /// /// \descr this functions sends bytes via dma over uart /// /// \param aucBytes /// \param ucSize /// /// \return void //----------------------------------------------------------------------------- void CddUart_SendBytes(uint8 aucBytes[], uint8 ucSize) { uint8 ucIdx; //check size if (CDDUART_BUFFERSIZE_MAX >= ucSize) { //wait until transmission is finished while ((CddMlcUart_pst_UART_Register->uSTAT.u32Register & LPUART_STAT_TDRE_MASK) >> LPUART_STAT_TDRE_SHIFT == 0) {} //wait until last transmission is finished - condition for DMA //CddDma_vpst__Register->Reg_TCDn[en__DmaChannelName].tsTCD_CSR.u1DONE = 0U; //??? //CddDma_vpst__Register->Reg_TCDn[1].tsTCD_CSR.u1DONE = 0U; /*copy to local buffer*/ for (ucIdx = 0; ucIdx < ucSize; ucIdx++) { CddUart_aucLocalBuffer[ucIdx] = aucBytes[ucIdx]; } //Initialize DMA for sending bytes DMA->TCD[1].CSR = 0U; //disable DMA /* TCDn_CITER = TCDn_BITER = 1; // one iteration of major count TCDn_NBYTES = ucSize - 1; //number of bytes to be transferred TCDn_SADDR = (uint32)&CddUart_aucLocalBuffer[1u]; TCDn_SOFF = 1 TCDn_ATTR[SSIZE] = 0 TCDn_SLAST = -16 TCDn_DADDR = (uint32)&(CddMlcUart_pst_UART_Register->uDATA.u32Register) TCDn_DOFF = 4 TCDn_ATTR[DSIZE] = 2 TCDn_DLAST_SGA = –16 TCDn_CSR[INT_MAJ] = 1 TCDn_CSR[START] = 1 */ /* Start transmission */ CddMlcUart_pst_UART_Register->uDATA.u32Register = CddUart_aucLocalBuffer[0]; } }