#include #include #include void (*ptr_fct_u1) (void); void (*ptr_fct_u2) (void); void (*ptr_fct_u3) (void); void My_USART_Init(USART_TypeDef *USART){ if (USART == USART1) { RCC->APB2ENR |= RCC_APB2ENR_USART1EN; MyGPIO_Init(GPIOA,9,AltOut_Ppull); MyGPIO_Init(GPIOA,10, In_Floating); USART->BRR = 72000000/9600; } else if (USART == USART2) { RCC->APB1ENR |= RCC_APB1ENR_USART2EN; MyGPIO_Init(GPIOA,2,AltOut_Ppull); MyGPIO_Init(GPIOA,3,In_Floating); USART->BRR = 36000000/9600; } else if (USART == USART3) { RCC->APB1ENR |= RCC_APB1ENR_USART3EN; MyGPIO_Init(GPIOB,10,AltOut_Ppull); MyGPIO_Init(GPIOB,11,In_Floating); USART->BRR = 36000000/9600; } USART->CR1 |= USART_CR1_UE; // Activation de l'USART USART->CR1 &= ~USART_CR1_M; // Choix d'une taille de 8 bits de données USART->CR2 &=~ USART_CR2_STOP; // Choix d'un seul bit de stop USART->CR1 |= USART_CR1_TE; // Transmission validée USART->CR1 |= USART_CR1_RE; // réception validée } void send_USART(USART_TypeDef *USART, char data){ USART->DR = data; // Ecriture de la donnée dans le registre DR while(!(USART->SR & USART_SR_TXE)) {} // Attente de la fin de transmission --TC ????? Inverser 2 lignes +TC a la fin } char read_USART(USART_TypeDef *USART){ while (!(USART->SR & USART_SR_RXNE)){} return USART->DR; } void MyUSART_ActiveIT(USART_TypeDef *USART, int Prio, void (*IT_function) (void)){ USART->CR1 |= USART_CR1_RXNEIE; if (USART == USART1){ ptr_fct_u1 = IT_function; NVIC->ISER[1] |= 1<<5; NVIC->IPR[44] = Prio<<4; } else if (USART == USART2){ ptr_fct_u2 = IT_function; NVIC->ISER[1] |= 1<<6; NVIC->IPR[45] = Prio<<4; } else if (USART == USART3){ ptr_fct_u3 = IT_function; NVIC->ISER[1] |= 1<<7; NVIC->IPR[46] = Prio<<4; } } void USART2_IRQHandler(void){ USART2->SR&=~USART_SR_RXNE; ptr_fct_u2(); } void USART1_IRQHandler(void){ USART1->SR&=~USART_SR_RXNE; ptr_fct_u1(); } void USART3_IRQHandler(void){ USART3->SR&=~USART_SR_RXNE; ptr_fct_u3(); }