/*
 * LPUART_UartDrv.c
 *
 *  Created on: 2020Äê10ÔÂ16ÈÕ
 *      Author: zhou_l4
 */

#include "S32K148.h"
#include "S32K148_features.h"
#include "LPUART_UartDrv.h"

void LPUART0_init(void)  /* Init. summary: 9600 baud, 1 stop bit, 8 bit format, no parity */
{
	PCC->PCCn[PCC_LPUART0_INDEX] &= ~PCC_PCCn_CGC_MASK;    /* Ensure clk disabled for config */
	PCC->PCCn[PCC_LPUART0_INDEX] |= PCC_PCCn_PCS(02)    /* Clock Src = 2 (SIRCDIV2_CLK) */
                            	 |  PCC_PCCn_CGC_MASK;     /* Enable clock for LPUART1 regs */

	LPUART0->BAUD = LPUART_BAUD_SBR(0x11)  	/* Initialize for 9600 baud, 1 stop: */
                	|LPUART_BAUD_OSR(3);  	/* SBR=52 (0x34): baud divisor = 8M/9600/16 = ~52 */
											/* OSR=15: Over sampling ratio = 15+1=16 */
											/* SBNS=0: One stop bit */
											/* BOTHEDGE=0: receiver samples only on rising edge */
											/* M10=0: Rx and Tx use 7 to 9 bit data characters */
											/* RESYNCDIS=0: Resync during rec'd data word supported */
											/* LBKDIE, RXEDGIE=0: interrupts disable */
											/* TDMAE, RDMAE, TDMAE=0: DMA requests disabled */
											/* MAEN1, MAEN2,  MATCFG=0: Match disabled */

	LPUART0->CTRL =	LPUART_CTRL_RE_MASK
					|LPUART_CTRL_TE_MASK;   	/* Enable transmitter & receiver, no parity, 8 bit char: */
												/* RE=1: Receiver enabled */
												/* TE=1: Transmitter enabled */
												/* PE,PT=0: No hw parity generation or checking */
												/* M7,M,R8T9,R9T8=0: 8-bit data characters*/
												/* DOZEEN=0: LPUART enabled in Doze mode */
												/* ORIE,NEIE,FEIE,PEIE,TIE,TCIE,RIE,ILIE,MA1IE,MA2IE=0: no IRQ*/
												/* TxDIR=0: TxD pin is input if in single-wire mode */
												/* TXINV=0: TRansmit data not inverted */
												/* RWU,WAKE=0: normal operation; rcvr not in statndby */
												/* IDLCFG=0: one idle character */
												/* ILT=0: Idle char bit count starts after start bit */
												/* SBK=0: Normal transmitter operation - no break char */
												/* LOOPS,RSRC=0: no loop back */
}

void LPUART0_transmit_char(char send) {    /* Function to Transmit single Char */
	while((LPUART0->STAT & LPUART_STAT_TDRE_MASK)>>LPUART_STAT_TDRE_SHIFT==0);
	/* Wait for transmit buffer to be empty */
	LPUART0->DATA=send;              /* Send data */
}

void LPUART0_transmit_string(char *data_string,uint32_t len)  {  /* Function to Transmit whole string */
	uint32_t i=0;
	//while(data_string[i] != '\0')  {           /* Send chars one at a time */
	while(i<len ){
		LPUART0_transmit_char(data_string[i]);
		i++;
	//}
	}
}
void Uart_PORT_init (void)
{
	/*!
	 * Pins definitions
	 * ===================================================
	 *
	 * Pin number        | Function
	 * ----------------- |------------------
	 * PTC6              | UART1 TX
	 * PTC7              | UART1 RX
	 */
  PCC->PCCn[PCC_PORTC_INDEX ]|=PCC_PCCn_CGC_MASK; /* Enable clock for PORTC */
  PORTC->PCR[2]|=PORT_PCR_MUX(4);	/* Port C6: MUX = ALT2, UART1 TX */
  PORTC->PCR[3]|=PORT_PCR_MUX(4);   /* Port C7: MUX = ALT2, UART1 RX */
  //PTD->PSOR |= 1<<8; //debug uart en
}
