voilier/Drivers/My_UART.c
2025-12-03 08:19:23 +01:00

87 lines
No EOL
1.9 KiB
C

#include "stm32f10x.h"
void (*pointeur_fonction_USART1)(void);
void (*pointeur_fonction_USART2)(void);
void (*pointeur_fonction_USART3)(void);
void MyUART_Init(USART_TypeDef *USART)
{
if (USART == USART1){
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
NVIC->ISER[1] |= (0x1 << 5);
NVIC->IPR[44]=10;
USART->BRR=72000000/9600;
}else if (USART == USART2){
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
NVIC->ISER[1] |= (0x1 << 6);
NVIC->IPR[44]=10;
USART->BRR=36000000/9600;
}else if (USART == USART3){
RCC->APB1ENR |= RCC_APB1ENR_USART3EN;
NVIC->ISER[1] |= (0x1 << 7);
NVIC->IPR[44]=10;
USART->BRR=36000000/9600;
}
USART -> CR1 |= USART_CR1_UE; //uart enable
// USART->CR1 |= USART_CR1_M; //donnee de 8 bits ????????????????
USART -> CR1 |= USART_CR1_TE; //transmission enable
USART -> CR1 |= USART_CR1_RE; //recp enable
USART -> CR1 |= USART_CR1_RXNEIE; //interruption recep enable
}
char MyUART_Get(USART_TypeDef *USART)
{
return USART->DR;
}
void MyUART_Activ_IT(USART_TypeDef *USART, char Prio, void (*interrupt) (void)){
if (USART == USART1){
NVIC->ISER[1] |= (0x1 << 5);
NVIC->IPR[44] = Prio<<4;
pointeur_fonction_USART1=interrupt;
}else if (USART == USART2){
NVIC->ISER[1] |= (0x1 << 6);
NVIC->IPR[45]=Prio<<4;
pointeur_fonction_USART2=interrupt;
}else if (USART == USART3){
NVIC->ISER[1] |= (0x1 << 7);
NVIC->IPR[46]=Prio<<4;
pointeur_fonction_USART3=interrupt;
}
}
void MyUART_Emission(USART_TypeDef *USART, char data){
USART->DR=data;
// int i = 0;
// while (data[i]!=0){
// USART->DR = data[i];
// while ((USART->SR & (0x1 << 0x7))==0){
// ;
// }
// i++;
// }
}
void USART1_IRQHandler (void)
{
pointeur_fonction_USART1();
}
void USART2_IRQHandler (void)
{
pointeur_fonction_USART2();
}
void USART3_IRQHandler (void)
{
pointeur_fonction_USART3();
}