Skip to content

Commit

Permalink
added UART driver - fixes #70
Browse files Browse the repository at this point in the history
  • Loading branch information
gehadelkoumy committed Jun 26, 2023
1 parent 1ae9cbf commit 9ec48cb
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 0 deletions.
28 changes: 28 additions & 0 deletions ARM-communication-protocol/USART2/include/USART_config.h
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions ARM-communication-protocol/USART2/include/USART_interface.h
Original file line number Diff line number Diff line change
@@ -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






27 changes: 27 additions & 0 deletions ARM-communication-protocol/USART2/include/USART_private.h
Original file line number Diff line number Diff line change
@@ -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
97 changes: 97 additions & 0 deletions ARM-communication-protocol/USART2/src/USART_program.c
Original file line number Diff line number Diff line change
@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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);
}




















38 changes: 38 additions & 0 deletions ARM-communication-protocol/USART2/src/main.c
Original file line number Diff line number Diff line change
@@ -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++;

}



}

0 comments on commit 9ec48cb

Please sign in to comment.