From 9ec48cb3f9e7fc27a3c7261e0f31c6bbd4278cc9 Mon Sep 17 00:00:00 2001 From: gehadelkoumy Date: Mon, 26 Jun 2023 03:57:27 +0300 Subject: [PATCH] added UART driver - fixes #70 --- .../USART2/include/USART_config.h | 28 ++++++ .../USART2/include/USART_interface.h | 22 +++++ .../USART2/include/USART_private.h | 27 ++++++ .../USART2/src/USART_program.c | 97 +++++++++++++++++++ ARM-communication-protocol/USART2/src/main.c | 38 ++++++++ 5 files changed, 212 insertions(+) create mode 100755 ARM-communication-protocol/USART2/include/USART_config.h create mode 100755 ARM-communication-protocol/USART2/include/USART_interface.h create mode 100755 ARM-communication-protocol/USART2/include/USART_private.h create mode 100755 ARM-communication-protocol/USART2/src/USART_program.c create mode 100755 ARM-communication-protocol/USART2/src/main.c diff --git a/ARM-communication-protocol/USART2/include/USART_config.h b/ARM-communication-protocol/USART2/include/USART_config.h new file mode 100755 index 0000000..ebaa627 --- /dev/null +++ b/ARM-communication-protocol/USART2/include/USART_config.h @@ -0,0 +1,28 @@ +/*****************************************/ +/* Author : Gehad Elkoumy */ +/* Version : V01 */ +/* Date : 11 Jun 2023 */ +/*****************************************/ + +#ifndef USART_CONFIG_H +#define USART_CONFIG_H + +/* + + Baud rate to be implemented + + For 8MHz:- Baudrate = Fclk/(16*BRR) + 9600-> BRR = 52.083 :- + Fractionl = 1 + Mantissa = 34 + + 115200-> BRR = 4.34 :- + Fractionl = 5 + Mantissa = 4 + + + +*/ + + +#endif \ No newline at end of file diff --git a/ARM-communication-protocol/USART2/include/USART_interface.h b/ARM-communication-protocol/USART2/include/USART_interface.h new file mode 100755 index 0000000..d144d30 --- /dev/null +++ b/ARM-communication-protocol/USART2/include/USART_interface.h @@ -0,0 +1,22 @@ +/*****************************************/ +/* Author : Gehad Elkoumy */ +/* Version : V01 */ +/* Date : 11 Jun 2023 */ +/*****************************************/ + +#ifndef USART_INTERFACE_H +#define USART_INTERFACE_H + +void MUSART2_voidInit(void); +void MUSART2_voidSendData(u8 Copy_u16Data); +void MUSART2_voidSendString(u8 *Copy_u8String); +void MUSART2_voidSendNumbers(s32 Copy_s32Number); +u8 MUSART2_u8ReceiveData(void); + +#endif + + + + + + diff --git a/ARM-communication-protocol/USART2/include/USART_private.h b/ARM-communication-protocol/USART2/include/USART_private.h new file mode 100755 index 0000000..0c4dc5b --- /dev/null +++ b/ARM-communication-protocol/USART2/include/USART_private.h @@ -0,0 +1,27 @@ +/*****************************************/ +/* Author : Gehad Elkoumy */ +/* Version : V01 */ +/* Date : 11 Jun 2023 */ +/*****************************************/ + +#ifndef USART_PRIVATE_H +#define USART_PRIVATE_H + +typedef struct{ + volatile u32 SR; + volatile u32 DR; + volatile u32 BRR; + volatile u32 CR1; + volatile u32 CR2; + volatile u32 CR3; + volatile u32 GTPR; +}USART_t; + + +#define USART2 ((USART_t *) 0x40004400) + + +#define ZeroASCIICode 48 + + +#endif \ No newline at end of file diff --git a/ARM-communication-protocol/USART2/src/USART_program.c b/ARM-communication-protocol/USART2/src/USART_program.c new file mode 100755 index 0000000..c4af799 --- /dev/null +++ b/ARM-communication-protocol/USART2/src/USART_program.c @@ -0,0 +1,97 @@ +/*****************************************/ +/* Author : Gehad Elkoumy */ +/* Version : V01 */ +/* Date : 11 Jun 2023 */ +/*****************************************/ + +#include "STD_TYPES.h" +#include "BIT_MATH.h" + +#include "RCC_interface.h" +#include "DIO_interface.h" + +#include "USART_interface.h" +#include "USART_private.h" +#include "USART_config.h" + +#include +#include +#include + +void MUSART2_voidInit(void) +{ + /*Enable clock of GPIO, AFIO , USART2*/ + RCC_voidEnableClock(RCC_APB2,2); + //RCC_voidEnableClock(RCC_APB2,3); + RCC_voidEnableClock(RCC_APB2,0); + RCC_voidEnableClock(RCC_APB1,17); + + /*pin mode*/ + MGPIO_VoidSetPinDirection(GPIOA,PIN2,OUTPUT_10MHZ_AFPP); // TX + MGPIO_VoidSetPinDirection(GPIOA,PIN3,INPUT_FLOATING); // RX + + /* baud rate = 9600 */ + USART2 -> BRR = 0x341; + + SET_BIT((USART2-> CR1), 3); /* Enabling Transmitter */ + SET_BIT((USART2-> CR1), 2); /* Enabling Receiver */ + SET_BIT((USART2-> CR1), 13); /* Enabling USART */ + + USART2 -> SR = 0; /* Clearing status register */ +} + +void MUSART2_voidSendData(u8 Copy_u16Data) +{ + USART2 -> DR = Copy_u16Data; + while((GET_BIT((USART2 -> SR), 6)) == 0); + CLR_BIT(USART2->SR,6); + +} + +void MUSART2_voidSendString(u8 *Copy_u8String) +{ + while( (*Copy_u8String) != '\0' ) + { + MUSART2_voidSendData(*Copy_u8String); + Copy_u8String++; + } +} + +void MUSART2_voidSendNumbers(s32 Copy_s32Number) +{ + char arr[15]; + /* convert number to string */ + itoa(Copy_s32Number , arr , 10); + u8* string = (u8*)arr; + + MUSART2_voidSendString(string); +} + +u8 MUSART2_u8ReceiveData(void) +{ + u8 Loc_u8ReceivedData = 0; + /* wait until receive complete*/ + while((GET_BIT((USART2 -> SR), 5)) == 0); + Loc_u8ReceivedData = USART2 -> DR; + return (Loc_u8ReceivedData); +} + + + + + + + + + + + + + + + + + + + + diff --git a/ARM-communication-protocol/USART2/src/main.c b/ARM-communication-protocol/USART2/src/main.c new file mode 100755 index 0000000..1510d5e --- /dev/null +++ b/ARM-communication-protocol/USART2/src/main.c @@ -0,0 +1,38 @@ +/* + * main.c + * + * Created on: Jun 11, 2023 + * Author: Gehad Elkoumy + */ + +#include"STD_TYPES.h" +#include"BIT_MATH.h" + +#include"RCC_interface.h" +#include"DIO_interface.h" +#include"USART_interface.h" + +u8 x = 22; +void main (void) +{ + /*initialize clocks*/ + RCC_voidInitSysClock(); + + MUSART2_voidInit(); + //MUSART2_voidSendString("hello world"); + //s32 x=255; + + while(1) + { + //MUSART2_voidSendString((u8*)"hello"); + //MUSART2_voidSendString((u8*)"\r\n"); + MUSART2_voidSendNumbers(x); + MUSART2_voidSendString((u8*)"\r\n"); + //x++; + + } + + + +} +