//###########################################################################
//
// FILE:    Example_2803xGpioSetup.c
//
// TITLE:   GPIO Toggle Test example
//
//! \addtogroup f2803x_example_list
//! <h1>GPIO Toggle Test (gpio_toggle)</h1>
//!
//!  \note ALL OF THE I/O'S TOGGLE IN THIS PROGRAM.  MAKE SURE
//!  THIS WILL NOT DAMAGE YOUR HARDWARE BEFORE RUNNING THIS
//!  EXAMPLE.
//!
//!  Three different examples are included. Select the example
//!  (data, set/clear or toggle) to execute before compiling using
//!  the macros found at the top of the code.
//!
//!  Each example toggles all the GPIOs in a different way, the first
//!  through writing values to the GPIO DATA registers, the second through
//!  the SET/CLEAR registers and finally the last through the TOGGLE register
//!
//!  The pins can be observed using Oscilloscope.
//
//###########################################################################
// $TI Release: F2803x Support Library v2.02.00.00 $
// $Release Date: Tue May 26 17:11:32 IST 2020 $
// $Copyright:
// Copyright (C) 2009-2020 Texas Instruments Incorporated - http://www.ti.com/
//
// Redistribution and use in source and binary forms, with or without 
// modification, are permitted provided that the following conditions 
// are met:
// 
//   Redistributions of source code must retain the above copyright 
//   notice, this list of conditions and the following disclaimer.
// 
//   Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the 
//   documentation and/or other materials provided with the   
//   distribution.
// 
//   Neither the name of Texas Instruments Incorporated nor the names of
//   its contributors may be used to endorse or promote products derived
//   from this software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// $
//
//###########################################################################

//
// Included Files
//
#include "DSP28x_Project.h"     // Device Headerfile and Examples Include File

void delay_loop(void);
void Gpio_select(void);
void Gpio_example1(void);
void Gpio_example2(void);
void Gpio_example3(void);

void main(void)
{

    InitSysCtrl();
    Gpio_select();

    DINT;
    InitPieCtrl();
    IER = 0x0000;
    IFR = 0x0000;
    InitPieVectTable();
    Gpio_example1();

    Gpio_example2();
    Gpio_example3();
}

//
//delay_loop - 
//
void delay_loop()
{
    double      i;
    for (i = 0; i < 100000; i++)
    {
        
    }
}

//
// Gpio_example1 - Gpio example 1 that toggle's I/Os using DATA registers
//
void Gpio_example1(void)
{
    for(;;)
    {
        GpioDataRegs.GPADAT.bit.GPIO12 =0x01;
        GpioDataRegs.GPADAT.bit.GPIO24 =0x00;
        GpioDataRegs.GPBDAT.bit.GPIO34 =0x00;
        delay_loop();
        GpioDataRegs.GPADAT.bit.GPIO24 =0x01;
        GpioDataRegs.GPBDAT.bit.GPIO34 =0x01;
        GpioDataRegs.GPADAT.bit.GPIO12 =0x00;
        delay_loop();
        GpioDataRegs.GPADAT.bit.GPIO24 =0x01;
        GpioDataRegs.GPBDAT.bit.GPIO34 =0x01;
        GpioDataRegs.GPADAT.bit.GPIO12 =0x00;
    }
}

//
// Gpio_example2 - Gpio example 2 that toggle's I/Os using SET/CLEAR registers
//
void Gpio_example2(void)
{    
    for(;;)
    {
        GpioDataRegs.GPASET.all    =0xAAAAAAAA;
        GpioDataRegs.GPACLEAR.all  =0x55555555;

        GpioDataRegs.GPBSET.all    =0x0000000A;  
        GpioDataRegs.GPBCLEAR.all  =0x00000005;

        delay_loop();

        GpioDataRegs.GPACLEAR.all    =0xAAAAAAAA;
        GpioDataRegs.GPASET.all      =0x55555555;

        GpioDataRegs.GPBCLEAR.all    =0x0000000A;
        GpioDataRegs.GPBSET.all      =0x00000005;

        delay_loop();
    }   
}

//
// Gpio_example3 - Gpio example 2 that toggle's I/Os using TOGGLE registers
//
void Gpio_example3(void)
{ 
    //
    // Set pins to a known state
    //
    GpioDataRegs.GPASET.all    =0xAAAAAAAA;
    GpioDataRegs.GPACLEAR.all  =0x55555555;

    GpioDataRegs.GPBSET.all    =0x0000000A;
    GpioDataRegs.GPBCLEAR.all  =0x00000005;

    //
    // Use TOGGLE registers to flip the state of
    // the pins.
    // Any bit set to a 1 will flip state (toggle)
    // Any bit set to a 0 will not toggle.
    //
    for(;;)
    {
        GpioDataRegs.GPATOGGLE.all =0xFFFFFFFF;
        GpioDataRegs.GPBTOGGLE.all =0x0000000F;
        delay_loop();
    }
}

//
// Gpio_select - Sets the gpios functionality
//
void Gpio_select(void)
{
    EALLOW;
    GpioCtrlRegs.GPAMUX1.all = 0x00<<24;  // All GPIO
    GpioCtrlRegs.GPAMUX2.all = 0x00<<16;
    GpioCtrlRegs.GPBMUX1.all = 0x00<<4;
    GpioCtrlRegs.GPADIR.bit.GPIO12 = 0x01;   // All outputs
    GpioCtrlRegs.GPADIR.bit.GPIO24 = 0x01;
    GpioCtrlRegs.GPBDIR.bit.GPIO34 = 0x01;
    EDIS;
}

//
// End of File
//

