Initgpio: Void Void
Initgpio: Void Void
Initgpio: Void Void
Module
main.c
Description
starter main() function for Events and Services Framework applications
Notes
History
When Who What/Why
-------------- --- --------
08/21/17 12:53 jec added this header as part of coding standard and
added
code to enable as GPIO the port poins that come out
of
reset locked or in an alternate function.
*****************************************************************************
/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_Port.h"
#include "termio.h"
#include "EnablePA25_PB23_PD7_PF0.h"
#include "ADMulti.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "inc/hw_ssi.h"
#include "inc/hw_nvic.h"
#include "inc/hw_pwm.h"
#include "inc/hw_gpio.h"
#include "inc/hw_timer.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#define TestPWM
//Initializing interrupts
#define TicksPerMS 40000
#define OVERTIME_DURATION 100000
void InitInterrupts(void);
//Initializing SPI
#define BitsPerNibble 4
#define PreScaler 54
#define ClockRate 50
void InitSPI(void);
//Initializing PWM for motors, frequency of 500 Hz, 50Hz for servos
#define LOAD_VALUE 1250
#define LOAD_VALUE_SERVO 12500
#define SERVO_CMP_CENTER 937 //1.5ms high time
#define SERVO_CMP_RIGHT 1562 //2.5ms high time
#define GenA_Normal (PWM_0_GENA_ACTCMPAU_ONE | PWM_0_GENA_ACTCMPAD_ZERO)
#define GenB_Normal (PWM_0_GENB_ACTCMPBU_ONE | PWM_0_GENB_ACTCMPBD_ZERO)
#define FLYWHEEL_DUTY_CYCLE 40
#define RETROREFLECTIVE_SEND_PERIOD 300 //2.5 ms; 400 Hz
void InitPWM(void);
int main(void)
{
ES_Return_t ErrorType;
// Set the clock to run at 40MhZ using the PLL and 16MHz external crystal
SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN
| SYSCTL_XTAL_16MHZ);
TERMIO_Init();
clrScrn();
//Init everything
InitGPIO();
InitInterrupts();
InitPWM();
InitSPI();
void InitGPIO(void) {
//Port A in SPI init
//Port B
//Enable the clock to Port B
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R1;
//Set as digital
HWREG(GPIO_PORTB_BASE + GPIO_O_DEN) |= (BIT0HI | BIT1HI | BIT2HI | BIT3HI);
//Set as outputs
HWREG(GPIO_PORTB_BASE + GPIO_O_DIR) |= (BIT0HI | BIT1HI);
//Set as inputs
HWREG(GPIO_PORTB_BASE + GPIO_O_DIR) &= (BIT2LO & BIT3LO);
//Port C
//Enable the clock to Port C
HWREG(SYSCTL_RCGCGPIO) |= SYSCTL_RCGCGPIO_R2;
//Set as digital
HWREG(GPIO_PORTC_BASE + GPIO_O_DEN) |= (BIT6HI);
//Set as outputs
//Set as inputs
HWREG(GPIO_PORTC_BASE + GPIO_O_DIR) &= (BIT6LO);
//Port D
//Set as digital
HWREG(GPIO_PORTD_BASE + GPIO_O_DEN) |= (BIT4HI | BIT6HI | BIT7HI);
//Set as outputs
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) |= (BIT4HI | BIT6HI);
//Set as inputs
HWREG(GPIO_PORTD_BASE + GPIO_O_DIR) &= (BIT7LO);
//Port E
//Clock enable for Port E included within function
//Init analog inputs for magnetic line following and Sharp sensor
ADC_MultiInit(NumAnalog);
//Init input capture interrupt using wide timers 0A, 0B, 3A, 3B
void InitInterrupts(void) {
//WIDE TIMER 0A
// start by enabling the clock to the timer (Wide Timer 0)
HWREG(SYSCTL_RCGCWTIMER) |= SYSCTL_RCGCWTIMER_R0;
// we want to use the full 32 bit count, so initialize the Interval Load
// register to 0xffff.ffff (its default value :-)
HWREG(WTIMER0_BASE+TIMER_O_TAILR) = 0xffffffff;
// To set the event to rising edge, we need to modify the TAEVENT bits
// in GPTMCTL. Rising edge = 00, so we clear the TAEVENT bits
HWREG(WTIMER0_BASE+TIMER_O_CTL) &= ~TIMER_CTL_TAEVENT_M;
// Now Set up the port to do the capture (clock was enabled earlier)
// start by setting the alternate function for Port C bit 4 (WT0CCP0)
HWREG(GPIO_PORTC_BASE+GPIO_O_AFSEL) |= BIT4HI;
// now kick the timer off by enabling it and enabling the timer to
// stall while stopped by the debugger
HWREG(WTIMER0_BASE+TIMER_O_CTL) |= (TIMER_CTL_TAEN | TIMER_CTL_TASTALL);
// now kick the timer off by enabling it and enabling the timer to
// stall while stopped by the debugger
HWREG(WTIMER1_BASE+TIMER_O_CTL) |= (TIMER_CTL_TAEN | TIMER_CTL_TASTALL);
// we want to use the full 32 bit count, so initialize the Interval Load
// register to 0xffff.ffff (its default value :-)
HWREG(WTIMER1_BASE+TIMER_O_TBILR) = 0xffffffff;
// To set the event to rising edge, we need to modify the TBEVENT bits
// in GPTMCTL. Rising edge = 00, so we clear the TBEVENT bits
HWREG(WTIMER1_BASE+TIMER_O_CTL) &= ~TIMER_CTL_TBEVENT_M;
// Now Set up the port to do the capture (clock was enabled earlier)
// start by setting the alternate function for Port C bit 7 (WT1CCP1)
HWREG(GPIO_PORTC_BASE+GPIO_O_AFSEL) |= BIT7HI;
// now kick the timer off by enabling it and enabling the timer to
// stall while stopped by the debugger
HWREG(WTIMER1_BASE+TIMER_O_CTL) |= (TIMER_CTL_TBEN | TIMER_CTL_TBSTALL);
// we want to use the full 32 bit count, so initialize the Interval Load
// register to 0xffff.ffff (its default value :-)
HWREG(WTIMER3_BASE+TIMER_O_TAILR) = 0xffffffff;
// we want to use the full 32 bit count, so initialize the Interval Load
// register to 0xffff.ffff (its default value :-)
HWREG(WTIMER3_BASE+TIMER_O_TBILR) = 0xffffffff;
// To set the event to rising edge, we need to modify the TBEVENT bits
// in GPTMCTL. Rising edge = 00, so we clear the TBEVENT bits
HWREG(WTIMER3_BASE+TIMER_O_CTL) &= ~TIMER_CTL_TBEVENT_M;
// Now Set up the port to do the capture (clock was enabled earlier)
// start by setting the alternate function for Port D bit 2 (WT3CCP0)
HWREG(GPIO_PORTD_BASE+GPIO_O_AFSEL) |= BIT2HI;
// Now Set up the port to do the capture (clock was enabled earlier)
// start by setting the alternate function for Port D bit 3 (WT3CCP1)
HWREG(GPIO_PORTD_BASE+GPIO_O_AFSEL) |= BIT3HI;
// now kick the timer off by enabling it and enabling the timer to
// stall while stopped by the debugger
HWREG(WTIMER3_BASE+TIMER_O_CTL) |= (TIMER_CTL_TAEN | TIMER_CTL_TASTALL);
// now kick the timer off by enabling it and enabling the timer to
// stall while stopped by the debugger
HWREG(WTIMER3_BASE+TIMER_O_CTL) |= (TIMER_CTL_TBEN | TIMER_CTL_TBSTALL);
}
//Init PWM to the motors (PB6 and PB7), reload emitter (PB4), retroreflective
emitter (PB5),
//flywheel (PD0), servo for ball storage/release mechanism (PE5), servo for
flags (PE4),
//servo for phototransistor(s) (PD1)
void InitPWM(void) {
//Enable the clock to Module 0 of PWM
HWREG(SYSCTL_RCGCPWM) |= SYSCTL_RCGCPWM_R0;
//Select the PWM clock as System Clock / 32
HWREG(SYSCTL_RCC) = (HWREG(SYSCTL_RCC) & ~SYSCTL_RCC_PWMDIV_M) |
(SYSCTL_RCC_USEPWMDIV | SYSCTL_RCC_PWMDIV_32);
//Wait until the clock has started
while ((HWREG(SYSCTL_PRPWM) & SYSCTL_PRPWM_R0) != SYSCTL_PRPWM_R0) {
}
#ifdef TestPWM
//Disable PWM3 (PD0 and PD1) while initializing NOT USING PD1
HWREG(PWM0_BASE + PWM_O_3_CTL) = 0;
// program generators to go to 1 at rising compare A/B, 0 on falling
compare A/B
HWREG(PWM0_BASE+PWM_O_3_GENA) = GenA_Normal;
HWREG(PWM0_BASE+PWM_O_3_GENB) = GenB_Normal;
//Set period
HWREG(PWM0_BASE + PWM_O_3_LOAD) = LOAD_VALUE_SERVO;
//Set value at which pin changes state
uint32_t DesiredHighTime = (LOAD_VALUE_SERVO * FLYWHEEL_DUTY_CYCLE) / 100;
// Set the Duty cycle on A by programming the compare value
// to the required duty cycle of Period/2 - DesiredHighTime/2
HWREG(PWM0_BASE + PWM_O_3_CMPA) = (uint16_t)(LOAD_VALUE_SERVO -
DesiredHighTime);
HWREG(PWM0_BASE + PWM_O_3_CMPB) = LOAD_VALUE_SERVO - SERVO_CMP_RIGHT;
//Don't enable PWM output to flywheel (0V across leads when MOSFET is off)
//Select alternate functions for PB6 and PB7 and PB4 and PB5
HWREG(GPIO_PORTB_BASE + GPIO_O_AFSEL) |= (BIT4HI | BIT5HI | BIT6HI |
BIT7HI);
//Map PWM to PB6 and PB7 and PB4 and PB5. 4 comes from Table 23-5 on Page
1351 of TIVA datasheet
HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL) = (HWREG(GPIO_PORTB_BASE+GPIO_O_PCTL)
& 0x0000ffff) + (4<<(7*BitsPerNibble)) + (4<<(6*BitsPerNibble)) +
(4<<(5*BitsPerNibble)) + (4<<(4*BitsPerNibble));
//Set PB6 and PB7 and PB4 and PB5 as digital
HWREG(GPIO_PORTB_BASE + GPIO_O_DEN) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);
//Set PB6 and PB7 and PB4 and PB5 as outputs
HWREG(GPIO_PORTB_BASE + GPIO_O_DIR) |= (BIT4HI | BIT5HI | BIT6HI | BIT7HI);
#endif