#include "uart.h"


	void DBGUART_init(void)
	{
		RCC->APB1ENR1  |=   RCC_APB1ENR1_USART2EN;																															//clock for UART																											
		RCC->AHB2ENR   |=   RCC_AHB2ENR_GPIOAEN;																																//clock for GPIO
		GPIOA->AFR[0]  &= ~(0x0FU<<8|0x0FU<<12);																																//set UART Rx Tx on GPIO
		GPIOA->AFR[0]  |=  (0x07U<<8|0x07U<<12); 
		GPIOA->MODER   &= ~(0x03U<<4|0x03U<<6);
		GPIOA->MODER   |=  (0x02U<<4|0x02U<<6);
		GPIOA->OTYPER  &= ~(0x01U<<2|0x01U<<3);
		GPIOA->OSPEEDR |=  (0x03U<<4|0x03U<<6);
		GPIOA->PUPDR   &= ~(0x03U<<4|0x03U<<6);
		GPIOA->PUPDR   |=  (0x02U<<6);																																					//Rx pulldown
		USART2->CR1    &= ~(USART_CR1_UE|USART_CR1_M0|USART_CR1_M1);
		USART2->CR2    &= ~(USART_CR2_STOP);
		
		(void) USART2->RDR;
		(void) USART2->TDR;
		
		USART2->CR1    |=  USART_CR1_TE;																																				//no parity, 1 start and 1 stop bit
		USART2->BRR     = 	0x271U;																																							//set baud rate to 115200	
		USART2->CR1    |=   USART_CR1_UE;																																				//UART enable
	}

	void NBUART_init(void)
	{
		RCC->APB2ENR   |=   RCC_APB2ENR_USART1EN;																															//clock for UART																											
		RCC->AHB2ENR   |=   RCC_AHB2ENR_GPIOAEN;																															//clock for GPIO
		//RCC->CCIPR   |=   RCC_CCIPR_USART1SEL_1;																															//clock select
		GPIOA->AFR[1]  &= ~(0x0FU<<4|0x0FU<<8);																																//set UART Rx Tx on GPIO
		GPIOA->AFR[1]  |=  (0x07U<<4|0x07U<<8); 
		GPIOA->MODER   &= ~(0x03U<<18|0x03U<<20);
		GPIOA->MODER   |=  (0x02U<<18|0x02U<<20);
		GPIOA->OTYPER  &= ~(0x01U<<9|0x01U<<10);
		GPIOA->OTYPER  |=  (0x01U<<10);
		GPIOA->OSPEEDR |=  (0x03U<<18|0x03U<<20);
		GPIOA->PUPDR   &= ~(0x03U<<18|0x03U<<20);
		USART1->CR1    &= ~(USART_CR1_UE|USART_CR1_M0|USART_CR1_M1);
		USART1->CR2    &= ~(USART_CR2_STOP);
		
		USART1->ICR    =   USART_ICR_IDLECF|USART_ICR_ORECF|USART_ICR_PECF|USART_ICR_LBDCF|USART_ICR_NCF|USART_ICR_FECF|USART_ICR_CMCF;
		(void) USART1->RDR;
		(void) USART1->TDR;
		
		USART1->CR1    |=  (USART_CR1_RE|USART_CR1_TE|USART_CR1_RXNEIE);																				//no parity, 1 start and 1 stop bit
		USART1->BRR     = 	0x271U;																																							//set baud rate to 115200	
	//USART1->BRR     = 	139;																																							//set baud rate to 115200	
		NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0,1));
		NVIC_EnableIRQ(USART1_IRQn);
		USART1->CR1    |=   USART_CR1_UE;																																				//UART enable
	}
	
	
		void DBGUART_transmit(const uint8_t *data, uint8_t cnt)																										//transmit function
	{
		while (cnt--)																																															//sending data
		{
			while(!(USART2->ISR & USART_ISR_TXE));
			USART2->TDR = *data++;
		}
	}
		
	
		void NBUART_transmit(const uint8_t *data, uint8_t cnt)																										//transmit function
	{
		while (cnt--)																																															//sending data
		{
			while(!(USART1->ISR & USART_ISR_TXE));
			USART1->TDR = *data++;
		}
	}
