Vous êtes sur la page 1sur 11

/****************************************************************************

Header file for GeneralBase module


based on the Gen2 Events and Services Framework
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
#ifndef GeneralBase_H
#define GeneralBase_H
// Public Function Prototypes
void pause(int);
void write(char, uint8_t, uint8_t);
uint8_t read(char, uint8_t);
void pulse(char, uint8_t);
void delay(int);
void InitSystemClock(char);
void InitPinIO(char, uint8_t);
void InitPinDir(char, uint8_t, uint8_t);
#endif
//#define test
/****************************************************************************
Module
GeneralBase.c
Description
This is the general base function will be adapted through the project
History
When
Who
-------------- --11/4/14 23:00 dongao

What/Why
--------

Final Edit: Dongao Yang 11/25/14


****************************************************************************/
// the common headers for I/O, C99 types
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
// the headers to access the GPIO subsystem
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
// the header to get the timing functions
#include "ES_Port.h"
#include "termio.h"
#include "ES_Timers.h"
#include "GeneralBase.h"

#define clrScrn()
printf("\x1b[2J")
#define ALL_BITS (0xff<<2)
/****************************************************************************
Function
pause
Parameters
int: t, a number stands for the wait time
Returns
nothing
Description
pause function, take an int input, with unit of uS, upper limit is
30000 uS
EX: wait 10uS, pause(10)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void pause(int t)
{
// multiply t by 10, as 10 dummy loop needs about 1 uS
t*=10;
// execute dummy loops
for (int i =0;i<t;i++);
}
/****************************************************************************
Function
write
Parameters
Take 3 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
third is the value indicate if the pin will be set as high or low
Returns
nothing
Description
if third param is 0, write this pin low, otherwise write hi, assuming
the pin intialized before
EX: make PF4 hi, write('F',BIT4HI,1)
EX: make PC2 lo, write('C',BIT2HI,0)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void write(char port, uint8_t pin, uint8_t level)
{
// if port is B
if (port == 'B')

{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is C
else if (port == 'C')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is D
else if (port == 'D')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is E
else if (port == 'E')
{
// if level is not 0
if (level != 0)
// write this pin to high
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
else
// write this pin to low
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
// if port is F
else if (port == 'F')
{
// if level is not 0
if (level != 0)
{
// write this pin to high
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
}
else
{
// write this pin to low
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
}
}

}
/****************************************************************************
Function
read
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
read logical value from the corresponding pin
EX: read from PF4, read('F',BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
uint8_t read(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is C
else if (port == 'C')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is D
else if (port == 'D')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is E
else if (port == 'E')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS))
pin);
}
// if port is F
else if (port == 'F')
{
// return 1 if that pin is high, 0 in other case
return ((HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))
pin);

& pin) ==

& pin) ==

& pin) ==

& pin) ==

& pin) ==

}
// return error number
else return 2;
}
/****************************************************************************
Function
pulse
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
Create a pulse output on indicated pin
EX: pulse PF4, pulse('F',BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void pulse(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
// write this pin to high
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTB_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
// wait 20 uS
pause(20);
}
// if port is C
else if(port == 'C')
{
// write this pin to high
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTC_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
// wait 20 uS
pause(20);
}
// if port is D
else if(port == 'D')
{
// write this pin to high
HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) |= pin;
// wait 20 uS
pause(20);

// write this pin to low


HWREG(GPIO_PORTD_BASE+(GPIO_O_DATA+ALL_BITS)) &= ~pin;
// wait 20 uS
pause(20);
}
// if port is E
else if(port == 'E')
{
// write this pin to high
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS))
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTE_BASE+(GPIO_O_DATA+ALL_BITS))
// wait 20 uS
pause(20);
}
// if port is F
else if(port == 'F')
{
// write this pin to high
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))
// wait 20 uS
pause(20);
// write this pin to low
HWREG(GPIO_PORTF_BASE+(GPIO_O_DATA+ALL_BITS))
// wait 20 uS
pause(20);
}
else

|= pin;

&= ~pin;

|= pin;

&= ~pin;

{
// print the error message
puts("Error: you indicate a wrong port for pulse \n");
}
}
/****************************************************************************
Function
delay
Parameters
int: t, a number stands for the wait time
Returns
nothing
Description
delay function, take an int input, with unit of mS, upper limit is
30000 mS
EX: wait 10mS, delay(10)
!!!: blocking code, don't use in the state machine or event checker
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void delay(int t)
{

// store current time by ES get time


int i = ES_Timer_GetTime();
// entering the loop, exit the loop while time reached
for(;(ES_Timer_GetTime()-i)<t;);
}
/****************************************************************************
Function
InitSystemClock
Parameters
Take 1 parameters, the port character needed to be activated with upper
case
Returns
nothing
Description
activate the system clock for the port
EX: to activate port F, InitSystemClock('F')
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void InitSystemClock(char port)
{
// if port is B
if (port == 'B')
{
// Activate the system clock for port B
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
// dummy code to wait some time for execution
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
// if port is C
else if (port == 'C')
{
// Activate the system clock for port C
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
// dummy code to wait some time for execution
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
// if port is D
else if (port == 'D')
{
// Activate the system clock for port D
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R3;
// dummy code to wait some time for execution
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
// if port is E
else if (port == 'E')
{
// Activate the system clock for port E
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R4;
// dummy code to wait some time for execution

int dummy=HWREG(SYSCTL_RCGCGPIO);
}
// if port is F
else if (port == 'F')
{
// Activate the system clock for port F
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R5;
// dummy code to wait some time for execution
int dummy=HWREG(SYSCTL_RCGCGPIO);
}
else
{
// print the error message
puts("Try to activate system clock for an invalid port");
}
}
/****************************************************************************
Function
InitPinIO
Parameters
Take 2 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
Returns
nothing
Description
Initialize the indicated pin as IO pin
// EX: activate PF4 as IO pin, InitPinIO('F',BIT4HI)
// EX: to make PF2,PF4 IO in one time, InitPinIO('F',BIT2HI|BIT4HI)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void InitPinIO(char port, uint8_t pin)
{
// if port is B
if (port == 'B')
{
//activate pin to IO
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= pin;
}
// if port is C
else if (port == 'C')
{
//activate pin to IO
HWREG(GPIO_PORTC_BASE+GPIO_O_DEN) |= pin;
}
// if port is D
else if (port == 'D')
{
//activate pin to IO
HWREG(GPIO_PORTD_BASE+GPIO_O_DEN) |= pin;

}
// if port is E
else if (port == 'E')
{
//activate pin to IO
HWREG(GPIO_PORTE_BASE+GPIO_O_DEN) |= pin;
}
// if port is F
else if (port == 'F')
{
//activate pin to IO
HWREG(GPIO_PORTF_BASE+GPIO_O_DEN) |= pin;
}
else
{
//print the error message
puts("try to set an invalid pin as IO");
}
}
/****************************************************************************
Function
InitPinDir
Parameters
Take 3 parameters, first is the port character with upper case,
second is the pin number indicated by a 8 bits number with
corresponding digit as hi
third is the value indicate if the pin will be set as output or input
Returns
nothing
Description
if third param is 0, write this pin input, otherwise output, assuming
the pin intialized before
// EX: make PF4 output, InitPinDir('F',BIT4HI,1)
// EX: make PC2 input, InitPinDir('C',BIT2HI,0)
// EX: to make PF2,PF4 in output in one time,
InitPinDir('F',BIT2HI|BIT4HI,1)
Final Editing: Dongao Yang 11/25/2014
****************************************************************************/
void InitPinDir(char port, uint8_t pin, uint8_t level)
{
// if port is B
if (port == 'B')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= pin;
else
// set this pin to input
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) &= ~pin;
}

// if port is C
else if (port == 'C')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTC_BASE+GPIO_O_DIR)
}
// if port is D
else if (port == 'D')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTD_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTD_BASE+GPIO_O_DIR)
}
// if port is E
else if (port == 'E')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTE_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTE_BASE+GPIO_O_DIR)
}
// if port is F
else if (port == 'F')
{
// if level is not 0
if (level != 0)
// set this pin to output
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR)
else
// set this pin to input
HWREG(GPIO_PORTF_BASE+GPIO_O_DIR)
}

|= pin;
&= ~pin;

|= pin;
&= ~pin;

|= pin;
&= ~pin;

|= pin;
&= ~pin;

}
// test harness
#ifdef test
int main(void)
{
_HW_Timer_Init(ES_Timer_RATE_1mS);
TERMIO_Init();
clrScrn();
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
int dummy=HWREG(SYSCTL_RCGCGPIO);
HWREG(GPIO_PORTB_BASE+GPIO_O_DEN) |= (BIT6HI|BIT7HI);
HWREG(GPIO_PORTB_BASE+GPIO_O_DIR) |= (BIT6HI|BIT7HI);

for(;;)
pulse('B',BIT6HI);
}
#endif

Vous aimerez peut-être aussi