91 lines
2.4 KiB
C
91 lines
2.4 KiB
C
#include "UART.h"
|
||
#include "gpio.h"
|
||
//faire GPIO
|
||
|
||
|
||
char data1 = 0x00;
|
||
char data2 = 0x00;
|
||
char data3 = 0x00;
|
||
|
||
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
|
||
if (UARTStructPtr->UART == USART1)
|
||
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
||
if (UARTStructPtr->UART == USART2)
|
||
RCC->APB2ENR |= RCC_APB1ENR_USART2EN;
|
||
if (UARTStructPtr->UART == USART3)
|
||
RCC->APB2ENR |= RCC_APB1ENR_USART3EN;
|
||
|
||
UARTStructPtr->UART->BRR = 72000000/(UARTStructPtr->BaudRate);
|
||
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->Wlengh)<<12);
|
||
UARTStructPtr->UART->CR1 |= (0x1<<10);
|
||
|
||
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);
|
||
|
||
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);
|
||
|
||
NVIC_EnableIRQ(USART1_IRQn);
|
||
}
|
||
|
||
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)
|
||
UART1.Wparity = parity_even; // parit<69> (parity_even, parity_odd, parity_none)
|
||
UART1.Wstop = stop1b; // nombre bit de stop (stop1b, stop2b)
|
||
MyUART_Init(&UART1); // initialisation
|
||
*/
|
||
|