#include "uart.h" #include "gpio.h" //faire GPIO char data1 = 0; char data2 = 0; char data3 = 0; void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){ MyUART_setClockBit(UARTStructPtr); //Clock enable and setting the baud. UARTStructPtr->UART->CR1 = 0x00; // Clear ALL UARTStructPtr->UART->CR1 |= USART_CR1_UE; UARTStructPtr->UART->CR1 |= ((UARTStructPtr->length)<<12); //Setting the Length of the data transmitted UARTStructPtr->UART->CR1 &= ~(0x3<<9); //reset CR1 9-10 bits, Parity Selection if(UARTStructPtr->parity != parityNone) //if parity is enabled { UARTStructPtr->UART->CR1 |= (UARTStructPtr->parity<<9); //depending on the parity changing the 9th bit, and set 10th bit to 1 } UARTStructPtr->UART->CR2 &= ~(0x3<<12); //reset CR2 13-12 bits, Stop bits if(UARTStructPtr->stop != stopBit1) //if stop bits > 1 { UARTStructPtr->UART->CR2 |= (UARTStructPtr->stop<<12); //depending on the stop changing the 12th and 13th bit. } //TxD Enable, RxD Enable, USART Global Enable UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE); MyUART_ActiveIT(UARTStructPtr->UART,1); } int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr) { if (UARTStructPtr->UART == USART1) { RCC->APB2ENR |= RCC_APB2ENR_USART1EN; USART1->BRR = 72000000/(UARTStructPtr->BaudRate); //Calculating the baudrate depending on the clock frequency return 0; } else if (UARTStructPtr->UART == USART2) { RCC->APB1ENR |= RCC_APB1ENR_USART2EN; USART2->BRR = 36000000/(UARTStructPtr->BaudRate); return 0; } else if (UARTStructPtr->UART == USART3) { RCC->APB1ENR |= RCC_APB1ENR_USART3EN; USART3->BRR = 36000000/(UARTStructPtr->BaudRate); return 0; } else { return 1; } } void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio) { uint32_t IRQNumber = MyUART_GetInterruptNum(UART); UART->CR1 |= (USART_CR1_RXNEIE); //Interruption active pour la reception UNIQUEMENT NVIC->IP[IRQNumber] |= (Prio << 0x4); //Prio de l'interruption (p.197 manuel reference RM0008 pour ADC1_IRQn) NVIC->ISER[1] |= (0x1<<(IRQNumber-32)); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0]) } void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data) { UART->UART->DR = data; //du DR au Registre de Transmission, on attend que le bit de stop soit envoyé while (!(UART->UART->SR & USART_SR_TC)); } uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART) { while (!(UART->UART->SR & USART_SR_RXNE)); // Si RXNE est mis à 1, alors on vient de recevoir une donnée ! uint8_t data = UART->UART->DR; // Read the data. UART->UART->SR &= ~USART_SR_RXNE; //flag to 0 return data; } uint8_t MyUART_GetInterruptNum(USART_TypeDef * UART) { if(UART == USART1) { return USART1_IRQn; } else if(UART == USART2) { return USART2_IRQn; } else if(UART == USART3) { return USART3_IRQn; } else{ return 0; } } void USART1_IRQHandler(void) { if((USART1->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag { data1 = USART1->DR; // read received data USART1->SR &= ~USART_SR_RXNE; //flag to 0 } } void USART2_IRQHandler(void) { if((USART2->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag { data2 = USART2->DR; // read received data USART2->SR &= ~USART_SR_RXNE; //flag to 0 } } void USART3_IRQHandler(void) { // Check if receive data register not empty if((USART3->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag { data3 = USART3->DR; // read received data // do something with received data USART2->SR &= ~USART_SR_RXNE; //flag to 0 } } char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr) { if(UARTStructPtr->UART == USART1) return data1; else if(UARTStructPtr->UART == USART2) return data2; else if(UARTStructPtr->UART == USART3) return data3; else return 0xFF; } /* exemple utilisation fonction : UART1.UART = USART1; // choix UART (USART1, USART2, USART3) UART1.BaudRate = 9600; // Vitesse de communication MAX 118200 UART1.Wlengh = Wlengh8; // longeur du mot (Wlengh8 ,Wlengh9) UART1.Wparity = parity_even; // parit� (parity_even, parity_odd, parity_none) UART1.Wstop = stop1b; // nombre bit de stop (stop1b, stop2b) MyUART_Init(&UART1); // initialisation */