2023-03-31 10:12:56 +02:00
|
|
|
|
#include "uart.h"
|
2023-03-31 09:37:30 +02:00
|
|
|
|
#include "gpio.h"
|
|
|
|
|
//faire GPIO
|
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
char data1 = 0;
|
|
|
|
|
char data2 = 0;
|
|
|
|
|
char data3 = 0;
|
2023-03-31 09:37:30 +02:00
|
|
|
|
|
|
|
|
|
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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
|
2023-03-31 09:37:30 +02:00
|
|
|
|
}
|
2023-03-31 12:18:42 +02:00
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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);
|
2023-03-31 09:37:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr)
|
2023-03-31 12:18:42 +02:00
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
if (UARTStructPtr->UART == USART1)
|
2023-03-31 12:18:42 +02:00
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
|
|
|
|
USART1->BRR = 72000000/(UARTStructPtr->BaudRate); //Calculating the baudrate depending on the clock frequency
|
2023-03-31 12:18:42 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-04-02 22:53:49 +02:00
|
|
|
|
else if (UARTStructPtr->UART == USART2)
|
2023-03-31 12:18:42 +02:00
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
|
|
|
|
|
USART2->BRR = 36000000/(UARTStructPtr->BaudRate);
|
2023-03-31 12:18:42 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-04-02 22:53:49 +02:00
|
|
|
|
else if (UARTStructPtr->UART == USART3)
|
2023-03-31 12:18:42 +02:00
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
|
|
|
|
|
USART3->BRR = 36000000/(UARTStructPtr->BaudRate);
|
2023-03-31 12:18:42 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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])
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-04 13:14:09 +02:00
|
|
|
|
void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr)
|
|
|
|
|
{
|
|
|
|
|
MyGPIO_Struct_TypeDef rxd,txd;
|
|
|
|
|
if(UARTStructPtr->UART == USART1)
|
|
|
|
|
{
|
|
|
|
|
rxd = (MyGPIO_Struct_TypeDef){GPIOA,10,In_PullUp};
|
|
|
|
|
txd = (MyGPIO_Struct_TypeDef){GPIOA,9,AltOut_Ppull};
|
|
|
|
|
}
|
|
|
|
|
else if(UARTStructPtr->UART == USART2) {
|
|
|
|
|
rxd = (MyGPIO_Struct_TypeDef){GPIOA,3,In_Floating};
|
|
|
|
|
txd = (MyGPIO_Struct_TypeDef){GPIOA,2,AltOut_Ppull};
|
|
|
|
|
}
|
|
|
|
|
else if(UARTStructPtr->UART == USART3) {
|
|
|
|
|
rxd = (MyGPIO_Struct_TypeDef){GPIOB,11,In_PullUp};
|
|
|
|
|
txd = (MyGPIO_Struct_TypeDef){GPIOB,10,AltOut_Ppull};
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MyGPIO_Init(&rxd);
|
|
|
|
|
MyGPIO_Init(&txd);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
void USART1_IRQHandler(void)
|
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-03-31 09:37:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART2_IRQHandler(void)
|
|
|
|
|
{
|
2023-04-02 22:53:49 +02:00
|
|
|
|
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
|
|
|
|
|
}
|
2023-03-31 09:37:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART3_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
// Check if receive data register not empty
|
2023-04-02 22:53:49 +02:00
|
|
|
|
if((USART3->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag
|
2023-03-31 09:37:30 +02:00
|
|
|
|
{
|
|
|
|
|
data3 = USART3->DR; // read received data
|
|
|
|
|
// do something with received data
|
2023-04-02 22:53:49 +02:00
|
|
|
|
USART2->SR &= ~USART_SR_RXNE; //flag to 0
|
2023-03-31 09:37:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 22:53:49 +02:00
|
|
|
|
|
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
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)
|
2023-03-31 10:12:56 +02:00
|
|
|
|
UART1.Wparity = parity_even; // parit<69> (parity_even, parity_odd, parity_none)
|
2023-03-31 09:37:30 +02:00
|
|
|
|
UART1.Wstop = stop1b; // nombre bit de stop (stop1b, stop2b)
|
|
|
|
|
MyUART_Init(&UART1); // initialisation
|
|
|
|
|
*/
|
|
|
|
|
|