65 lines
1.5 KiB
C
65 lines
1.5 KiB
C
#include "UART.h"
|
|
#include "GPIO.h"
|
|
|
|
void (*uart_rx_ptr_func)(void);
|
|
char buffer[1000]={0};
|
|
|
|
void MyUART_init(void) // que pour du 9600 bauds
|
|
{
|
|
MyGPIO_Struct_TypeDef RX_pin;
|
|
MyGPIO_Struct_TypeDef TX_pin;
|
|
|
|
RX_pin.GPIO = GPIOA;
|
|
RX_pin.GPIO_Conf = In_Floating;
|
|
RX_pin.GPIO_Pin = 10;
|
|
|
|
TX_pin.GPIO = GPIOA;
|
|
TX_pin.GPIO_Conf = AltOut_Ppull;
|
|
TX_pin.GPIO_Pin = 9;
|
|
|
|
MyGPIO_Init (&RX_pin);
|
|
MyGPIO_Init (&TX_pin);
|
|
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // validation horloge USART1
|
|
USART1->CR1 |= USART_CR1_UE; // Activation de l'USART
|
|
USART1->CR1 &= ~USART_CR1_M; // Choix d'une taille de 8 bits de données
|
|
USART1->CR2 &= USART_CR2_STOP; // Choix d'un seul bit de stop
|
|
USART1->BRR |= 468 << 4; // Fixe le baud rate à 9600bps //valeurs trouvé p804
|
|
USART1->BRR |= 75; // Fixe le baud rate à 9600bps
|
|
USART1->CR1 |= USART_CR1_RE;
|
|
USART1->CR1 |= USART_CR1_TE;
|
|
}
|
|
|
|
void UART_send(char data)
|
|
{
|
|
while(!(USART1->SR & USART_SR_TXE))
|
|
{
|
|
}
|
|
USART1->DR = data; // Ecriture de la donnée dans le registre DR
|
|
}
|
|
|
|
void MyUART_ActiveIT(char Prio, void (*IT_function)(void))
|
|
{
|
|
//activer l'interrupt sur reception
|
|
USART1->CR1 |= USART_CR1_RXNEIE;
|
|
uart_rx_ptr_func=IT_function;
|
|
NVIC->ISER[1] |= 0x01<<(USART1_IRQn%32);
|
|
NVIC->IP[USART1_IRQn] |= Prio << 4;
|
|
}
|
|
|
|
void USART1_IRQHandler()
|
|
{
|
|
if (USART1->SR & USART_SR_RXNE)
|
|
{
|
|
USART1->SR &= ~USART_SR_RXNE;
|
|
if(uart_rx_ptr_func!=0)
|
|
{
|
|
(*uart_rx_ptr_func)();
|
|
}
|
|
}
|
|
}
|
|
|
|
char UART_receive()
|
|
{
|
|
return USART1->DR; // on recupere dans DR
|
|
}
|