From b2f17850529f40ff730c1f39414b40477f30f5e5 Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sun, 2 Apr 2023 22:53:49 +0200 Subject: [PATCH] UART Working (test with rspi). Interruption for Receiving. Pinout for the different UARTS --- driver/uart.c | 126 ++++++++++++------ driver/uart.h | 45 ++++--- .../Device/STM32F103RB/startup_stm32f10x_md.s | 2 +- keilproject/RTE/_R_el/RTE_Components.h | 2 +- keilproject/Source/Principale.c | 30 ++++- keilproject/voilier.uvoptx | 122 +++++++++-------- keilproject/voilier.uvprojx | 17 ++- 7 files changed, 216 insertions(+), 128 deletions(-) diff --git a/driver/uart.c b/driver/uart.c index 55222b5..7fe717f 100644 --- a/driver/uart.c +++ b/driver/uart.c @@ -2,49 +2,50 @@ #include "gpio.h" //faire GPIO -char data1 = 0x00; -char data2 = 0x00; -char data3 = 0x00; +char data1 = 0; +char data2 = 0; +char data3 = 0; void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr){ - MyUART_setClockBit(UARTStructPtr->UART); - UARTStructPtr->UART->BRR = 72000000/(UARTStructPtr->BaudRate); - UARTStructPtr->UART->CR1 |= ((UARTStructPtr->Wlengh)<<12); - UARTStructPtr->UART->CR1 |= (0x1<<10); + 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 - 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 &= ~(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->CR1 |= (USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE); - NVIC_EnableIRQ(USART1_IRQn); + 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(USART_TypeDef * UART) +int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr) { - if (UART == USART1) + if (UARTStructPtr->UART == USART1) { - RCC->APB2ENR |= RCC_APB2ENR_USART1EN; //72MHz + RCC->APB2ENR |= RCC_APB2ENR_USART1EN; + USART1->BRR = 72000000/(UARTStructPtr->BaudRate); //Calculating the baudrate depending on the clock frequency return 0; } - else if (UART == USART2) + else if (UARTStructPtr->UART == USART2) { - RCC->APB1ENR |= RCC_APB1ENR_USART2EN; //36MHz + RCC->APB1ENR |= RCC_APB1ENR_USART2EN; + USART2->BRR = 36000000/(UARTStructPtr->BaudRate); return 0; } - else if (UART == USART3) + else if (UARTStructPtr->UART == USART3) { - RCC->APB1ENR |= RCC_APB1ENR_USART3EN; //36MHz //Maybe return the clock base mhz ? + RCC->APB1ENR |= RCC_APB1ENR_USART3EN; + USART3->BRR = 36000000/(UARTStructPtr->BaudRate); return 0; } else { @@ -52,36 +53,79 @@ int MyUART_setClockBit(USART_TypeDef * UART) } } +void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio) +{ + uint32_t IRQNumber = MyUART_GetInterruptNum(UART); + UART->CR1 |= (USART_CR1_RXNEIE); //Interruption active pour la reception UNIQUEMENT + NVIC->IP[IRQNumber] |= (Prio << 0x4); //Prio de l'interruption (p.197 manuel reference RM0008 pour ADC1_IRQn) + NVIC->ISER[1] |= (0x1<<(IRQNumber-32)); //Active l'interruption au niveau NVIC (p.119 manuel programming pour ISER[0]) +} + +void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data) +{ + UART->UART->DR = data; + //du DR au Registre de Transmission, on attend que le bit de stop soit envoyé + while (!(UART->UART->SR & USART_SR_TC)); +} + +uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART) +{ + while (!(UART->UART->SR & USART_SR_RXNE)); // Si RXNE est mis à 1, alors on vient de recevoir une donnée ! + uint8_t data = UART->UART->DR; // Read the data. + UART->UART->SR &= ~USART_SR_RXNE; //flag to 0 + 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) { - // Check if receive data register not empty - if(USART1->SR & USART_SR_RXNE) - { - data1 = USART1->DR; // read received data - // do something with received data - } + if((USART1->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag + { + data1 = USART1->DR; // read received data + USART1->SR &= ~USART_SR_RXNE; //flag to 0 + } } 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 - } + if((USART2->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag + { + data2 = USART2->DR; // read received data + USART2->SR &= ~USART_SR_RXNE; //flag to 0 + } } void USART3_IRQHandler(void) { // Check if receive data register not empty - if(USART3->SR & USART_SR_RXNE) + if((USART3->SR & USART_SR_RXNE) == USART_SR_RXNE) //verify the flag { data3 = USART3->DR; // read received data // do something with received data + USART2->SR &= ~USART_SR_RXNE; //flag to 0 } } + + char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr) { if(UARTStructPtr->UART == USART1) diff --git a/driver/uart.h b/driver/uart.h index 0693b2c..b116c41 100644 --- a/driver/uart.h +++ b/driver/uart.h @@ -2,28 +2,37 @@ #define UART_H #include "stm32f10x.h" -typedef struct { - USART_TypeDef * UART; - unsigned int BaudRate; - unsigned char Wlengh; - unsigned char Wparity; - unsigned char Wstop; -}MyUART_Struct_Typedef; +typedef enum { + lengthBit8, + lengthBit9 +} MyUART_Enum_Length; typedef enum { - //DIV_Fraction -> - //1D4C -}MyUART_BRR + parityNone, + parityEven = 0b10, + parityOdd = 0b11 +} MyUART_Enum_Parity; -#define parity_none 0x0 -#define parity_even 0x1 -#define parity_odd 0x2 -#define Wlengh8 0x0 -#define Wlengh9 0X1 -#define stop1b 0x0 -#define stop2b 0x2 +typedef enum { + stopBit1, + stopBit0d5, + stopBit2, + stopBit1d5 +} MyUART_Enum_StopBits; + +typedef struct { + USART_TypeDef * UART; + uint32_t BaudRate; + MyUART_Enum_Length length; + MyUART_Enum_Parity parity; + MyUART_Enum_StopBits stop; +}MyUART_Struct_Typedef; void MyUART_Init(MyUART_Struct_Typedef * UARTStructPtr); -int MyUART_setClockBit(USART_TypeDef * UART); +int MyUART_setClockBit(MyUART_Struct_Typedef * UARTStructPtr); +void MyUART_Send(MyUART_Struct_Typedef *UART, uint8_t data); +uint8_t MyUART_Receive(MyUART_Struct_Typedef *UART); +void MyUART_ActiveIT(USART_TypeDef * UART, uint8_t Prio); +uint8_t MyUART_GetInterruptNum(USART_TypeDef * UART); char MyUART_Read(MyUART_Struct_Typedef * UARTStructPtr); #endif diff --git a/keilproject/RTE/Device/STM32F103RB/startup_stm32f10x_md.s b/keilproject/RTE/Device/STM32F103RB/startup_stm32f10x_md.s index 1ab7096..0ac2873 100644 --- a/keilproject/RTE/Device/STM32F103RB/startup_stm32f10x_md.s +++ b/keilproject/RTE/Device/STM32F103RB/startup_stm32f10x_md.s @@ -132,7 +132,7 @@ Reset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT __main IMPORT SystemInit - LDR R0, =SystemInit +a LDR R0, =SystemInit BLX R0 LDR R0, =__main BX R0 diff --git a/keilproject/RTE/_R_el/RTE_Components.h b/keilproject/RTE/_R_el/RTE_Components.h index 849a0e6..d0354e4 100644 --- a/keilproject/RTE/_R_el/RTE_Components.h +++ b/keilproject/RTE/_R_el/RTE_Components.h @@ -3,7 +3,7 @@ * Auto generated Run-Time-Environment Configuration File * *** Do not modify ! *** * - * Project: 'gpiodriver' + * Project: 'voilier' * Target: 'Réel' */ diff --git a/keilproject/Source/Principale.c b/keilproject/Source/Principale.c index 82be6d1..d94ffca 100644 --- a/keilproject/Source/Principale.c +++ b/keilproject/Source/Principale.c @@ -2,8 +2,36 @@ #include "../../driver/MyI2C.h" #include "../../driver/MySPI.h" #include "../../implementation/remote.h" +#include "../../driver/gpio.h" int main (void) { - while(1){}; + //rxd input pull up, txd output none + MyGPIO_Struct_TypeDef led = {GPIOA,5,Out_PullUp}; //led + MyGPIO_Struct_TypeDef rxd1 = {GPIOA,10,In_PullUp}; + MyGPIO_Struct_TypeDef txd1 = {GPIOA,9,AltOut_Ppull}; + MyGPIO_Struct_TypeDef rxd2 = {GPIOA,3,In_Floating}; + MyGPIO_Struct_TypeDef txd2 = {GPIOA,2,AltOut_Ppull}; + MyGPIO_Struct_TypeDef rxd3 = {GPIOB,11,In_PullUp}; + MyGPIO_Struct_TypeDef txd3 = {GPIOB,10,AltOut_Ppull}; + MyUART_Struct_Typedef uartCool = {USART3,9600,lengthBit8,parityNone,stopBit1}; + + MyGPIO_Init(&led); + MyGPIO_Init(&rxd1); + MyGPIO_Init(&txd1); + MyGPIO_Init(&rxd2); + MyGPIO_Init(&txd2); + MyGPIO_Init(&rxd3); + MyGPIO_Init(&txd3); + MyUART_Init(&uartCool); + + while(1){ + if(MyUART_Read(&uartCool) == 0x61) + { + MyGPIO_Set(GPIOA,5); + MyUART_Send(&uartCool,'k'); + MyUART_Send(&uartCool,'o'); + MyUART_Send(&uartCool,'\n'); + } + }; } diff --git a/keilproject/voilier.uvoptx b/keilproject/voilier.uvoptx index 99771c9..bd34282 100644 --- a/keilproject/voilier.uvoptx +++ b/keilproject/voilier.uvoptx @@ -75,7 +75,7 @@ 1 0 - 1 + 0 18 @@ -125,7 +125,7 @@ 0 DLGDARM - (1010=-1,-1,-1,-1,0)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=1468,53,1889,480,1)(121=1469,437,1890,864,1)(122=875,109,1296,536,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=-1,-1,-1,-1,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=1285,87,1879,838,1)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=-1,-1,-1,-1,0)(161=-1,-1,-1,-1,0)(162=-1,-1,-1,-1,0)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) + (1010=1460,461,1836,1018,1)(1007=-1,-1,-1,-1,0)(1008=-1,-1,-1,-1,0)(1009=-1,-1,-1,-1,0)(100=-1,-1,-1,-1,0)(110=-1,-1,-1,-1,0)(111=-1,-1,-1,-1,0)(1011=-1,-1,-1,-1,0)(180=-1,-1,-1,-1,0)(120=1009,499,1430,926,1)(121=1469,437,1890,864,0)(122=875,109,1296,536,0)(123=-1,-1,-1,-1,0)(140=-1,-1,-1,-1,0)(240=105,137,504,482,0)(190=-1,-1,-1,-1,0)(200=-1,-1,-1,-1,0)(170=-1,-1,-1,-1,0)(130=-1,-1,-1,-1,0)(131=879,71,1473,822,0)(132=-1,-1,-1,-1,0)(133=-1,-1,-1,-1,0)(160=1014,43,1462,457,1)(161=568,150,1016,564,1)(162=1351,117,1799,531,1)(210=-1,-1,-1,-1,0)(211=-1,-1,-1,-1,0)(220=-1,-1,-1,-1,0)(221=-1,-1,-1,-1,0)(230=-1,-1,-1,-1,0)(234=-1,-1,-1,-1,0)(231=-1,-1,-1,-1,0)(232=-1,-1,-1,-1,0)(233=-1,-1,-1,-1,0)(150=-1,-1,-1,-1,0)(151=-1,-1,-1,-1,0) 0 @@ -142,9 +142,9 @@ 0 0 - 15 + 29 1 -
134218740
+
134219444
0 0 0 @@ -153,71 +153,39 @@ 1 .\Source\Principale.c - \\cool_Simule\Source/Principale.c\15 + \\cool_Simule\Source/Principale.c\29
1 0 - 19 + 28 1 -
0
+
134219442
0 0 0 0 0 - 0 + 1 .\Source\Principale.c - + \\cool_Simule\Source/Principale.c\28
2 0 - 7 - 1 -
0
- 0 - 0 - 0 - 0 - 0 - 0 - .\Source\Principale.c - - -
- - 3 - 0 8 1 -
0
+
134219308
0 0 0 0 0 - 0 + 1 .\Source\Principale.c - -
- - 4 - 0 - 9 - 1 -
0
- 0 - 0 - 0 - 0 - 0 - 0 - .\Source\Principale.c - - + \\cool_Simule\Source/Principale.c\8
@@ -274,16 +242,6 @@ - - - System Viewer\GPIOA - 35905 - - - System Viewer\GPIOB - 35904 - - 1 1 @@ -348,7 +306,7 @@ 1 0 - 0 + 1 18 @@ -403,7 +361,7 @@ 0 ST-LINKIII-KEIL_SWO - -U066FFF504955857567155843 -O206 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) + -U066FFF504955857567155843 -O206 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(1BA01477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103RB$Flash\STM32F10x_128.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2 0 @@ -430,7 +388,39 @@ 0 0 - 6 + 31 + 1 +
134219614
+ 0 + 0 + 0 + 0 + 0 + 1 + .\Source\Principale.c + + \\cool_reel\Source/Principale.c\31 +
+ + 1 + 0 + 28 + 1 +
134219606
+ 0 + 0 + 0 + 0 + 0 + 1 + .\Source\Principale.c + + \\cool_reel\Source/Principale.c\28 +
+ + 2 + 0 + 30 1
0
0 @@ -498,6 +488,20 @@ + + + System Viewer\USART1 + 35905 + + + System Viewer\USART2 + 35904 + + + System Viewer\USART3 + 35903 + + 1 0 @@ -562,7 +566,7 @@ 2 4 1 - 0 + 1 0 0 ..\driver\adc.c @@ -574,7 +578,7 @@ 2 5 1 - 0 + 1 0 0 ..\driver\gpio.c diff --git a/keilproject/voilier.uvprojx b/keilproject/voilier.uvprojx index 5a8530a..d5e4a4f 100644 --- a/keilproject/voilier.uvprojx +++ b/keilproject/voilier.uvprojx @@ -10,13 +10,14 @@ Simulé 0x4 ARM-ADS - 5060960::V5.06 update 7 (build 960)::.\ARMCC + 5060960::V5.06 update 7 (build 960)::.\ARM_Compiler_5.06u7 + 5060960::V5.06 update 7 (build 960)::.\ARM_Compiler_5.06u7 0 STM32F103RB STMicroelectronics - Keil.STM32F1xx_DFP.2.3.0 + Keil.STM32F1xx_DFP.2.4.0 http://www.keil.com/pack/ IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00020000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE @@ -186,6 +187,7 @@ 0 0 0 + 0 0 0 8 @@ -437,13 +439,13 @@ Réel 0x4 ARM-ADS - 5060960::V5.06 update 7 (build 960)::.\ARMCC + 5060960::V5.06 update 7 (build 960)::.\ARM_Compiler_5.06u7 0 STM32F103RB STMicroelectronics - Keil.STM32F1xx_DFP.2.3.0 + Keil.STM32F1xx_DFP.2.4.0 http://www.keil.com/pack/ IRAM(0x20000000,0x00005000) IROM(0x08000000,0x00020000) CPUTYPE("Cortex-M3") CLOCK(12000000) ELITTLE @@ -613,6 +615,7 @@ 0 0 0 + 0 0 0 8 @@ -884,7 +887,7 @@ RTE\Device\STM32F103RB\RTE_Device.h - + @@ -893,7 +896,7 @@ RTE\Device\STM32F103RB\startup_stm32f10x_md.s - + @@ -902,7 +905,7 @@ RTE\Device\STM32F103RB\system_stm32f10x.c - +