143 lines
3.8 KiB
C
143 lines
3.8 KiB
C
#include "Driver_UART.h"
|
|
#include "Driver_GPIO.h"
|
|
|
|
|
|
char received_data1, received_data2, received_data3;
|
|
|
|
void UART_init(MyUART_Struct_Typedef * UART)
|
|
{
|
|
if(UART->UART ==USART1)
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
|
else if(UART->UART ==USART2)
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
|
|
else if(UART->UART ==USART3)
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
|
|
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // Validation horloge USART1
|
|
USART1->CR1 |= USART_CR1_UE; // Activer l'USART
|
|
USART1->CR1 &= ~USART_CR1_M; // Choisir la taille 8bits de donnée
|
|
USART1->CR2 |= USART_CR2_STOP; // 1 seul bit de stop
|
|
//USART1->BRR |= 468 << 4; // Fixe le baud rate à 9600bps partie entière
|
|
//USART1->BRR |= 75; // Fixe le baud rate à 9600bps partie fractionnaire
|
|
UART->UART->BRR = 72000000/(UART->baudrate);
|
|
USART1->CR1 |= USART_CR1_TE; // Autoriser la transmission
|
|
USART1->CR1 |= USART_CR1_RE; // Activer la réception
|
|
// USART1->CR1 |= USART_CR1_TCIE; // Activer l'interruption de transmission
|
|
USART1->CR1 |= USART_CR1_RXNEIE; // Activer l'interruption de réception
|
|
UART_interruption(UART);
|
|
|
|
}
|
|
|
|
void UART_send(char data)
|
|
{
|
|
while(!(USART1->SR & USART_SR_TXE) | !(USART2->SR & USART_SR_TXE)){} //Attendre l'autorisation de transmission
|
|
USART1->DR |= data;
|
|
while(!(USART1->SR & USART_SR_TC) | !(USART2->SR & USART_SR_TC)){} //Attendre la fin de transmission
|
|
}
|
|
|
|
char UART_read(char data, MyUART_Struct_Typedef * UART)
|
|
{
|
|
if(UART->UART == USART1)
|
|
return received_data1;
|
|
else if (UART->UART == USART2)
|
|
return received_data2;
|
|
else if (UART->UART == USART3)
|
|
return received_data3;
|
|
else
|
|
return 0;
|
|
}
|
|
|
|
|
|
void USART1_IRQHandler(void)
|
|
{
|
|
if (USART1->SR & USART_SR_RXNE) // si une donnée a été reçue
|
|
{
|
|
received_data1 = USART1->DR; // lire la donnée reçue
|
|
}
|
|
|
|
if (USART1->SR & USART_SR_TC) // si la transmission est terminée
|
|
{
|
|
USART1->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
|
}
|
|
}
|
|
|
|
void USART2_IRQHandler(void)
|
|
{
|
|
if (USART2->SR & USART_SR_RXNE) // si une donnée a été reçue
|
|
{
|
|
received_data2 = USART2->DR; // lire la donnée reçue
|
|
}
|
|
|
|
if (USART2->SR & USART_SR_TC) // si la transmission est terminée
|
|
{
|
|
USART2->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
|
}
|
|
}
|
|
|
|
void USART3_IRQHandler(void)
|
|
{
|
|
if (USART3->SR & USART_SR_RXNE) // si une donnée a été reçue
|
|
{
|
|
received_data3 = USART3->DR; // lire la donnée reçue
|
|
}
|
|
|
|
if (USART3->SR & USART_SR_TC) // si la transmission est terminée
|
|
{
|
|
USART3->SR &= ~USART_SR_TC; // effacer le bit de transmission terminée
|
|
}
|
|
}
|
|
|
|
void UART_interruption (MyUART_Struct_Typedef * UART)
|
|
{
|
|
UART->UART->CR1 |= USART_CR1_RXNEIE;
|
|
|
|
if (UART->UART==USART1)
|
|
{
|
|
NVIC->ISER[1] |= (1<<(USART1_IRQn-32));
|
|
}
|
|
if (UART->UART==USART2)
|
|
{
|
|
NVIC->ISER[1] |= (1<<(USART2_IRQn-32));
|
|
}
|
|
if (UART->UART==USART3)
|
|
{
|
|
NVIC->ISER[1] |= (1<<(USART3_IRQn-32));
|
|
}
|
|
}
|
|
|
|
void UART_InitGPIO(MyUART_Struct_Typedef * UART)
|
|
{
|
|
if(UART->UART == USART1)
|
|
{
|
|
MyUART_Struct_Typedef UART1 = {USART1,9600};
|
|
MyGPIO_Struct_TypeDef PA9 = {GPIOA,9,AltOut_Ppull};
|
|
MyGPIO_Struct_TypeDef PA10 = {GPIOA,10,In_PullUp};
|
|
MyGPIO_Init (&PA9);
|
|
MyGPIO_Init (&PA10);
|
|
UART_init(&UART1);
|
|
}
|
|
else if(UART->UART == USART2) {
|
|
MyUART_Struct_Typedef UART2 = {USART2,9600};
|
|
MyGPIO_Struct_TypeDef PA2 = {GPIOA,2,AltOut_Ppull};
|
|
MyGPIO_Struct_TypeDef PA3 = {GPIOA,3,In_Floating};
|
|
MyGPIO_Init (&PA2);
|
|
MyGPIO_Init (&PA3);
|
|
UART_init(&UART2);
|
|
}
|
|
else if(UART->UART == USART3) {
|
|
MyUART_Struct_Typedef UART3 = {USART3,9600};
|
|
MyGPIO_Struct_TypeDef PA10 = {GPIOA,10,AltOut_Ppull};
|
|
MyGPIO_Struct_TypeDef PA11 = {GPIOA,11,In_PullUp};
|
|
MyGPIO_Init (&PA10);
|
|
MyGPIO_Init (&PA11);
|
|
UART_init(&UART3);
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|