2023-03-31 10:12:56 +02:00
|
|
|
|
#include "uart.h"
|
2023-03-31 09:37:30 +02:00
|
|
|
|
#include "gpio.h"
|
|
|
|
|
//faire GPIO
|
|
|
|
|
|
|
|
|
|
char data1 = 0x00;
|
|
|
|
|
char data2 = 0x00;
|
|
|
|
|
char data3 = 0x00;
|
|
|
|
|
|
|
|
|
|
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
|
2023-03-31 12:18:42 +02:00
|
|
|
|
MyUART_setClockBit(UARTStructPtr->UART);
|
|
|
|
|
UARTStructPtr->UART->BRR = 72000000/(UARTStructPtr->BaudRate);
|
|
|
|
|
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->Wlengh)<<12);
|
|
|
|
|
UARTStructPtr->UART->CR1 |= (0x1<<10);
|
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
if(UARTStructPtr->Wparity == parity_none)
|
|
|
|
|
UARTStructPtr->UART->CR1 &= ~(0x1<<10);
|
|
|
|
|
if(UARTStructPtr->Wparity == parity_odd)
|
|
|
|
|
UARTStructPtr->UART->CR1 |= (0x1<<9);
|
|
|
|
|
if(UARTStructPtr->Wparity == parity_even)
|
|
|
|
|
UARTStructPtr->UART->CR1 &= ~(0x1<<9);
|
2023-03-31 12:18:42 +02:00
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
if(UARTStructPtr->Wstop == stop1b)
|
|
|
|
|
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
|
|
|
|
|
if(UARTStructPtr->Wstop == stop2b){
|
|
|
|
|
UARTStructPtr->UART->CR2 &= ~(0x3<<12);
|
|
|
|
|
UARTStructPtr->UART->CR2 |= (0x1<<13);
|
|
|
|
|
}
|
|
|
|
|
UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE);
|
2023-03-31 12:18:42 +02:00
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
NVIC_EnableIRQ(USART1_IRQn);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 12:18:42 +02:00
|
|
|
|
int MyUART_setClockBit(USART_TypeDef * UART)
|
|
|
|
|
{
|
|
|
|
|
if (UART == USART1)
|
|
|
|
|
{
|
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; //72MHz
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (UART == USART2)
|
|
|
|
|
{
|
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; //36MHz
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else if (UART == USART3)
|
|
|
|
|
{
|
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART3EN; //36MHz //Maybe return the clock base mhz ?
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-31 09:37:30 +02:00
|
|
|
|
void USART1_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
// Check if receive data register not empty
|
|
|
|
|
if(USART1->SR & USART_SR_RXNE)
|
|
|
|
|
{
|
|
|
|
|
data1 = USART1->DR; // read received data
|
|
|
|
|
// do something with received data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART2_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
// Check if receive data register not empty
|
|
|
|
|
if(USART2->SR & USART_SR_RXNE)
|
|
|
|
|
{
|
|
|
|
|
data2 = USART2->DR; // read received data
|
|
|
|
|
// do something with received data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void USART3_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
// Check if receive data register not empty
|
|
|
|
|
if(USART3->SR & USART_SR_RXNE)
|
|
|
|
|
{
|
|
|
|
|
data3 = USART3->DR; // read received data
|
|
|
|
|
// do something with received data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|