Added remote(int) and initRemote functions. remote is linked to the interruption for USART 1 and is called everytime we receive a data.

This commit is contained in:
Yohan Boujon 2023-04-04 14:34:09 +02:00
parent 820a3db260
commit d9a4ae852d
6 changed files with 78 additions and 111 deletions

View file

@ -2,30 +2,11 @@
#include "gpio.h" #include "gpio.h"
//faire GPIO //faire GPIO
char data1 = 0; void (* pFncUART) (uint16_t data); /* d<>claration d<>un pointeur de fonction */
char data2 = 0;
char data3 = 0;
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){ void MyUART_Init_Periph (void (* ptrFonction)(uint16_t))
MyUART_setClockBit(UARTStructPtr); //Clock enable and setting the baud. {
UARTStructPtr->UART->CR1 = 0x00; // Clear ALL pFncUART = ptrFonction; /* affectation du pointeur */
UARTStructPtr->UART->CR1 |= USART_CR1_UE;
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->length)<<12); //Setting the Length of the data transmitted
UARTStructPtr->UART->CR1 &= ~(0x3<<9); //reset CR1 9-10 bits, Parity Selection
if(UARTStructPtr->parity != parityNone) //if parity is enabled
{
UARTStructPtr->UART->CR1 |= (UARTStructPtr->parity<<9); //depending on the parity changing the 9th bit, and set 10th bit to 1
}
UARTStructPtr->UART->CR2 &= ~(0x3<<12); //reset CR2 13-12 bits, Stop bits
if(UARTStructPtr->stop != stopBit1) //if stop bits > 1
{
UARTStructPtr->UART->CR2 |= (UARTStructPtr->stop<<12); //depending on the stop changing the 12th and 13th bit.
}
//TxD Enable, RxD Enable, USART Global Enable
UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE);
MyUART_ActiveIT(UARTStructPtr->UART,1);
} }
int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr) int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr)
@ -53,6 +34,25 @@ int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr)
} }
} }
uint8_t MyUART_GetInterruptNum(USART_TypeDef * UART)
{
if(UART == USART1)
{
return USART1_IRQn;
}
else if(UART == USART2)
{
return USART2_IRQn;
}
else if(UART == USART3)
{
return USART3_IRQn;
}
else{
return 0;
}
}
void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio) void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio)
{ {
uint32_t IRQNumber = MyUART_GetInterruptNum(UART); uint32_t IRQNumber = MyUART_GetInterruptNum(UART);
@ -61,6 +61,28 @@ void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio)
NVIC->ISER[1] |= (0x1<<(IRQNumber-32)); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0]) NVIC->ISER[1] |= (0x1<<(IRQNumber-32)); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0])
} }
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){
MyUART_setClockBit(UARTStructPtr); //Clock enable and setting the baud.
UARTStructPtr->UART->CR1 = 0x00; // Clear ALL
UARTStructPtr->UART->CR1 |= USART_CR1_UE;
UARTStructPtr->UART->CR1 |= ((UARTStructPtr->length)<<12); //Setting the Length of the data transmitted
UARTStructPtr->UART->CR1 &= ~(0x3<<9); //reset CR1 9-10 bits, Parity Selection
if(UARTStructPtr->parity != parityNone) //if parity is enabled
{
UARTStructPtr->UART->CR1 |= (UARTStructPtr->parity<<9); //depending on the parity changing the 9th bit, and set 10th bit to 1
}
UARTStructPtr->UART->CR2 &= ~(0x3<<12); //reset CR2 13-12 bits, Stop bits
if(UARTStructPtr->stop != stopBit1) //if stop bits > 1
{
UARTStructPtr->UART->CR2 |= (UARTStructPtr->stop<<12); //depending on the stop changing the 12th and 13th bit.
}
//TxD Enable, RxD Enable, USART Global Enable
UARTStructPtr->UART->CR1 |= (USART_CR1_TE | USART_CR1_RE);
MyUART_ActiveIT(UARTStructPtr->UART,1);
}
void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr) void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr)
{ {
MyGPIO_Struct_TypeDef rxd,txd; MyGPIO_Struct_TypeDef rxd,txd;
@ -87,8 +109,8 @@ void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr)
void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data) void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data)
{ {
UART->UART->DR = data; UART->UART->DR = data;
//du DR au Registre de Transmission, on attend que le bit de stop soit envoyé //du DR au Registre de Transmission, on attend que le shift register ai récupéré le DR
while (!(UART->UART->SR & USART_SR_TC)); while (!(UART->UART->SR & USART_SR_TXE));
} }
uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART) uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART)
@ -99,30 +121,12 @@ uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART)
return data; return data;
} }
uint8_t MyUART_GetInterruptNum(USART_TypeDef * UART)
{
if(UART == USART1)
{
return USART1_IRQn;
}
else if(UART == USART2)
{
return USART2_IRQn;
}
else if(UART == USART3)
{
return USART3_IRQn;
}
else{
return 0;
}
}
void USART1_IRQHandler(void) void USART1_IRQHandler(void)
{ {
if((USART1->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag if((USART1->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag
{ {
data1 = USART1->DR; // read received data if (pFncUART != 0)
(*pFncUART) (USART1->DR); /* appel indirect de la fonction */
USART1->SR &= ~USART_SR_RXNE; //flag to 0 USART1->SR &= ~USART_SR_RXNE; //flag to 0
} }
} }
@ -131,7 +135,8 @@ void USART2_IRQHandler(void)
{ {
if((USART2->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag if((USART2->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag
{ {
data2 = USART2->DR; // read received data if (pFncUART != 0)
(*pFncUART) (USART2->DR); /* appel indirect de la fonction */
USART2->SR &= ~USART_SR_RXNE; //flag to 0 USART2->SR &= ~USART_SR_RXNE; //flag to 0
} }
} }
@ -141,26 +146,12 @@ void USART3_IRQHandler(void)
// Check if receive data register not empty // Check if receive data register not empty
if((USART3->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag if((USART3->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag
{ {
data3 = USART3->DR; // read received data if (pFncUART != 0)
// do something with received data (*pFncUART) (USART3->DR); /* appel indirect de la fonction */
USART2->SR &= ~USART_SR_RXNE; //flag to 0 USART2->SR &= ~USART_SR_RXNE; //flag to 0
} }
} }
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 : /* exemple utilisation fonction :
UART1.UART = USART1; // choix UART (USART1, USART2, USART3) UART1.UART = USART1; // choix UART (USART1, USART2, USART3)

View file

@ -30,10 +30,7 @@ typedef struct {
void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr); void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr);
void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr); void MyUART_InitGPIO(MyUART_Struct_Typedef * UARTStructPtr);
int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr);
void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data); void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data);
uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART); uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART);
void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio); void MyUART_Init_Periph (void (* ptrFonction)(uint16_t));
uint8_t MyUART_GetInterruptNum(USART_TypeDef * UART);
char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr);
#endif #endif

View file

@ -1 +1,18 @@
#include "remote.h"
#include "../driver/gpio.h"
MyUART_Struct_Typedef uartCool = {USART1,9600,lengthBit8,parityNone,stopBit1};
void remote(uint16_t data)
{
//MyGPIO_Set(GPIOA,5);
MyUART_Send(&uartCool,data);
}
void initRemote(void)
{
MyUART_InitGPIO(&uartCool);
MyUART_Init(&uartCool);
MyUART_Init_Periph(remote);
}

View file

@ -4,4 +4,7 @@
//XBEE 9600 baud, zero parite, 1 bit de stop, //XBEE 9600 baud, zero parite, 1 bit de stop,
void remote(uint16_t data);
void initRemote(void);
#endif #endif

View file

@ -6,20 +6,11 @@
int main (void) int main (void)
{ {
//MyGPIO_Struct_TypeDef led = {GPIOA,5,Out_PullUp}; //led MyGPIO_Struct_TypeDef led = {GPIOA,5,Out_PullUp}; //led
//MyGPIO_Init(&led); //test des leds pour ignorer les contraintes liées aux différents ports MyGPIO_Init(&led); //test des leds pour ignorer les contraintes liées aux différents ports
MyUART_Struct_Typedef uartCool = {USART2,9600,lengthBit8,parityNone,stopBit1}; initRemote();
MyUART_InitGPIO(&uartCool);
MyUART_Init(&uartCool);
while(1){ while(1){
if(MyUART_Read(&uartCool) == 0x61)
{
MyGPIO_Set(GPIOA,5);
MyUART_Send(&uartCool,'o');
MyUART_Send(&uartCool,'k');
}
}; };
} }

View file

@ -436,39 +436,7 @@
<Bp> <Bp>
<Number>3</Number> <Number>3</Number>
<Type>0</Type> <Type>0</Type>
<LineNumber>17</LineNumber> <LineNumber>14</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>.\Source\Principale.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>4</Number>
<Type>0</Type>
<LineNumber>19</LineNumber>
<EnabledFlag>1</EnabledFlag>
<Address>0</Address>
<ByteObject>0</ByteObject>
<HtxType>0</HtxType>
<ManyObjects>0</ManyObjects>
<SizeOfObject>0</SizeOfObject>
<BreakByAccess>0</BreakByAccess>
<BreakIfRCount>0</BreakIfRCount>
<Filename>.\Source\Principale.c</Filename>
<ExecCommand></ExecCommand>
<Expression></Expression>
</Bp>
<Bp>
<Number>5</Number>
<Type>0</Type>
<LineNumber>20</LineNumber>
<EnabledFlag>1</EnabledFlag> <EnabledFlag>1</EnabledFlag>
<Address>0</Address> <Address>0</Address>
<ByteObject>0</ByteObject> <ByteObject>0</ByteObject>