/*
 * pwm.h
 *
 *  Created on: 16 Jun 2020
 *      Author: tm
 */

#ifndef MPHOX_PWM_H_
#define MPHOX_PWM_H_

#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <ctype.h>


#define ST_PWM_IDLE    0
#define ST_PWM_LO      1
#define ST_PWM_HI      2
#define ST_PWM_PERMIL_LO   3
#define ST_PWM_PERMIL_HI   4


struct pwmDriver
{
    uint32_t        state;      // PG1,PG3 0=off, 1=on, 2=percent, 3=hiZ(future)
    uint32_t        cnt;        // count variable for pwm switching
    uint32_t        gpio;       // variable holding state of gpio
    uint32_t        period;     // 1msec hardcoded in first implementation. inverse of frequency.
    uint32_t        percent;     // 0 to 100
};

void pwm_init(void);
void pwmSoln_set(unsigned int pwmVal);      //10mA when pwm is running at 10Khz
void pwmSoln_stop(unsigned int gpioState);   //pwm is stopped and gpio is left in hi or lo state

int pwm_stateMachine(int pwmState);



#ifdef NOCODE_TRYINGTOFIGUREOUTPWMPINMUX

    This TM4C123GH6PM (64pin used on nanoFET) has two PWM blocks, PWM0 and PWM1.  The same is true for TM4C123GH6PGE (144 pin used on MFET and MPHOX)

    Note: need to figure out the difference between 'block' and 'module'

    MPHOX is the only board as of 2021 that has a solenoid driver that requires PWM (hit and hold).   PG1 and PG3 are used on MPHOX v1 to connect to the solenoid.
    A resistor is used to route the GPIO to the solenoid output.ulPeriod

    For starters, the goal is for a 10khz PWM freqency with 100% for hit and approx. 50% duty cycle for the hold (needs to be verified with chosen solenoid valve.)

    PG3 has alternate function 5 for M1PWM1.   Most example code is for the 64pin TM4C123GH6PM and PD1 has alt function for M1PWM1

    GPIOPCTL PMCx bit field encoding is 5 for PG3 to have M1PWM1 pin mux.

    Future hardware should look at PG6, PG7 as options for PWM output

    Each PWM block contains four PWM generators and control blocks.
    Each PWM generator provides two PWM outputs, pwmA and pwmB. These tow PWM(A/B) signals share the frequency / counter blocks.
    This means, two PWM outputs from the same generator will have same frequency but can have different duty cycle or can be used as a complementary output to drive half bridge and full bridge in motor control applications.
    These complementary outputs also provide programmable dead-band delays.

    PWM generator blocks are used to generate PWM signals. On the other hand, control blocks are used to send PWM output signals to the GPIO pins of TM4C123 microcontroller.

    Summary: each generator provides 8 PWM channels, for a total of 16 PWM outputs.
    These are named: M0PWM_n and M1PWM_n where n = 0 - 7 and M0 and M1 are for module 0 and module 1.


    PWM Channel TM4C123 64pin GPIO Pins   PWM Generator
        M0PWM0      PB6         Module 0 Generator 0
        M0PWM1      PB7
        M0PWM2      PB4         Module 0 Generator 1
        M0PWM3      PB5
        M0PWM4      PE4         Module 0 Generator 2
        M0PWM5      PE5
        M0PWM6      PC4/PD0     Module 0 Generator 3
        M0PWM7      PC5/PD1
        M1PWM0      PF0         Module 1 Generator 0
        M1PWM1      PD1
        M1PWM2      PA6/PE4     Module 1 Generator 1
        M1PWM3      PA7/PE5
        M1PWM4      PF0         Module 1 Generator 2
        M1PWM5      PF1
        M1PWM6      PF2         Module 1 Generator 3
        M1PWM7      PF3

Programming TM4C123 PWM Modules:
    At power on / reset, system clock is disabled to all peripherals, need to enable clock source for PWM peripheral

    RCGCPWM register is used to enable clock source to both PWM modules.
    Setting Bit0 and Bit1 of RCGCPWM register enables the clock and clearing these bits disable the clock to both modules.

    SYSCTL->RCGCPWM |= (1<<0); /*enable clock source to PWM0 module */
    SYSCTL->RCGCPWM |= (1<<0); /* enable clock source to PWM0 module */

    Run-Mode clock configuration (RCC) register is used to scale down frequency for PWM modules.

    RCC register acts as a divider between system clock frequency and input frequency to PWM modules.
    The clock divisor options can be selected with bit20 (USEPWMDIV) and bits 19-17 (PWMDIV) of RCC register.
    bit20 (USEPWMDIV) selects either we want to use direct system frequency or pass it through a frequency divisor.
    Setting Bit20 to 1, selects the frequency divisor option. The bits 19-17 selects the divided by options from 2, 4, 8, 16, 32, and 64.

    PWMDIV Bits   Divisor   PWM Module Frequency
        000         2           8MHz
        001         4           4MHz
        010         8           2MHz
        100         16          1MHz
        101         32          500kHz
        111         64          250kHz


For a 10Khz output on PG3 (M1PWM1)
    For 16Mhz cpu freq
        PWM1 Module Clock Period = 1/8Mhz = 0.125us
        M1PWM1 Timer Period = 1/10KHz = 100us
        PWM4LOAD Register value = 100us / 0.125us = 800

    For 48Mhz cpu freq and PWMDIV by 8 = 6MHz
        PWM1 Module Clock Period = 1/6Mhz = 0.167us
        M1PWM1 Timer Period = 1/10KHz = 100us
        PWM4LOAD Register value = 100us / 0.167us = 600


Setting PWM Duty Cycle
        Two registers are used to set duty cycle of PWM signal. These registers are compare and generator output enable registers.
        Each generator block provides two PWM output signals such as pwmA and pwmB.
        For each PWM output, there is one compare register such as PWMxCMPA and PWMxCMPB. These compare registers are used to set the duty cycle PWM signals.

        Compare Registers:
        The value of compare register (PWMxCMPA and PWMxCMPB) is compared with the value of counter.
        When the value counter matches with the value of counter, we can configure the PWM pin to do one of the following things:
         - Toggle
         - Go active high
         - Make transition to active low

        Generator PWM Output Action Registers:
        Each generator block also has two PWM generator registers such as PWMxGENA and PWMxGENB.
        These registers are used to select one of the following actions for the PWM output signal when the counter value reaches zero or matches with compare register value.

        Bit[1:0]: Action to be taken when the counter is zero
        Bit[2:3]: Action to be taken when the counter matches the value in the PWMnLOAD register
        Bit[4:5]: the action to be taken when the counter matches comparator A while counting up.
        Bit[6:7]: action to be taken when the counter matches comparator A while counting down
        Bit[8:9]: action to be taken when the counter matches comparator B while counting up.
        Bit[10:11]: action to be taken when the counter matches comparator B while counting down


        To generate a square wave, we must set PWM duty cycle to 50%. But first we configure the PWMxGENx in such as way that PWM output goes high when the counter reloaded, and output signal goes low when the counter values matches with the compare register value (PWMxCMPx).

        To set 50% duty cycle, we need to upload value to compare register. To calculate compare register value, first subtract the required duty cycle from 100. After that take the load register value percentage. It will be the value of compare register in down count mode.

        Load register value for 50Hz square = 40, 000
        Compare register value = (100% - 50%) of 40,000 = 50% of 40,000 = 20,000
        The formula to calculate compare register value for required duty cycle in down count mode is:

            PWMxCMPx = (100% - required duty cycle in %) of PWMxLOAD

         In case of count-up the formula to calculate compare register value for required duty cycle is:

             PWMxCMPx = required duty cycle in % of PWMxLOAD

Steps to Enable TM4C123 PWM Output
        1)  Enable system clock to PWM module using SYSCTL->RCGCPWM by setting D0 bit for PWM0 and D1 bit for PWM1 module.
        2) Enable the clock to GPIO port through which PWM output appears on the related GPIO pin (   SYSCTL->RCGCGPIO) .
        3) Select the system clock divisor function before feeding it to PWM module. Disable it if you don?t want scale down frequency for PWM module (SYSCTL->RCC).
        4) Set the PWM output GPIO pin as a digital output pin, select its alternate function for PWM module and assign PWM pin to applicable GPIO pin using GPIOF->AFSEL, GPIOF->PCTL and GPIOF->DEN registers, respectively.
        5) Disable PWM generator counter before configuring PWM channel by clearing bit D0 of PWMn->_x_CTL register. Here n is module number which is 0 or 1 and x is generator block which is x=0,1,2,3.
        6) After that, select the counter mode either count-up or count down mode by setting or clearing bit1 of PWMn->_x_CTL register.
        7) Select PWM channel and select the actions of output signal when counter reloaded or counter matches with PWMxCMPx register.
        8) Calculate load value according to required frequency and load this value to  PWMx->_x_LOAD register.
        9) Calculate compare register value for required duty cycle and place the value in  PWMx->_x_CMPx
        10) At the end enable the counter of PWM generator by setting D0 bit of PWMx->_x_CTL register and also enable the PWM output on GPIO pin with PWMx->ENABLE register.


#endif


#ifdef NOCODE

    https://microcontrollerslab.com/pwm-tm4c123-example-codes-tiva-c-launchpad/

    1) Enable Clock to PWM module
        SYSCTL->RCGCPWM |= (1<<0); /*enable clock source to PWM0 module */   TODO figure out how to do this for PWM1

    2) PWM Clock Configuration  RCGCPWM

    3) Enable TM4C123 PWM Generator  PWMnCTL registers

    4) Setting TM4C123 PWM Frequency
        PWM0 Module Clock Period = 1/8Mhz = 0.125?s
        M0PWM4 Timer Period = 1/10KHz = 100?s
        PWM4LOAD Register value = 100?s / 0.125?s = 800

    5) Setting PWM Duty Cycle
        PWM Module Clock Period = 1 / 2MHz = 0.5?s
        PWM Signal Period = 1 / 50Hz = 20ms
        Load register value = 20ms/0.5?s = 40,000

        PWMxCMPx = (100% - required duty cycle in %) of PWMxLOAD

    6) Enable TM4C123 PWM Output
        Enable system clock to PWM module using SYSCTL->RCGCPWM by setting D0 bit for PWM0 and D1 bit for PWM1 module.
        Enable the clock to GPIO port through which PWM output appears on the related GPIO pin (   SYSCTL->RCGCGPIO) .
        Select the system clock divisor function before feeding it to PWM module. Disable it if you don?t want scale down frequency for PWM module (SYSCTL->RCC).
        Set the PWM output GPIO pin as a digital output pin, select its alternate function for PWM module and assign PWM pin to applicable GPIO pin using GPIOF->AFSEL, GPIOF->PCTL and GPIOF->DEN registers, respectively.
        Disable PWM generator counter before configuring PWM channel by clearing bit D0 of PWMn->_x_CTL register. Here n is module number which is 0 or 1 and x is generator block which is x=0,1,2,3.
        After that, select the counter mode either count-up or count down mode by setting or clearing bit1 of PWMn->_x_CTL register.
        Select PWM channel and select the actions of output signal when counter reloaded or counter matches with PWMxCMPx register.
        Calculate load value according to required frequency and load this value to  PWMx->_x_LOAD register.
        Calculate compare register value for required duty cycle and place the value in  PWMx->_x_CMPx
        At the end enable the counter of PWM generator by setting D0 bit of PWMx->_x_CTL register and also enable the PWM output on GPIO pin with PWMx->ENABLE register.

#define SYSCTL_RCGCGPIO_R6      0x00000040  // GPIO Port G Run Mode Clock
                                            // Gating Control

    /* Enable Peripheral Clocks */
    SYSCTL_RCGCPWM_R |= 2;       /* enable clock to PWM0 is 1, PWM1 is 2 */
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R6;   /* enable clock to PORTG */
    SYSCTL_RCC_R &= ~0x00100000; /* no pre-divide for PWM clock */

    /* Enable port PG3 for PWM1 M1PWM7 */
    GPIO_PORTG_AFSEL_R = 0x08;      /* PG3 uses alternate function */
    GPIO_PORTG_PCTL_R &= ~0x00000000; /* make PG3 PWM output pin */
    GPIO_PORTG_PCTL_R |= 0x00000020;            //M1PWM1 is bit.5 of GPIO_PORTG_PCTL_R Digital Function (GPIOPCTL PMCx Bit Field Encoding)
    GPIO_PORTG_DEN_R |= 8;       /* pin digital */



    //
    // Configure the GPIO Pin Mux for PG3
    // for M1PWM1
    //
    MAP_GPIOPinConfigure(GPIO_PG3_M1PWM1);
    MAP_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_3);


    //Configure PWM Clock to match system
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

    // Enable the peripherals used by this program.
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);  //The Tiva Launchpad has two modules (0 and 1). Module 1 covers the LED pins
    ulPeriod = SysCtlClockGet() / 200; //PWM frequency 200HZ

    //Configure PF1,PF2,PF3 Pins as PWM
    GPIOPinConfigure(GPIO_PG3_M1PWM1);

    GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_3);  // GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);



    //Configure PWM Options
    //PWM_GEN_2 Covers M1PWM4 and M1PWM5
    //PWM_GEN_3 Covers M1PWM6 and M1PWM7 See page 207 4/11/13 DriverLib doc
    //PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);
    //PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC);

    //Set the Period (expressed in clock ticks)
    PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, ulPeriod);
    PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, ulPeriod);

    //Set PWM duty-50% (Period /2)
    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,ulPeriod/2);
    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,ulPeriod/2);
    PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,ulPeriod/2);

    // Enable the PWM generator
    PWMGenEnable(PWM1_BASE, PWM_GEN_2);
    PWMGenEnable(PWM1_BASE, PWM_GEN_3);

    // Turn on the Output pins
    PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT |PWM_OUT_6_BIT|PWM_OUT_7_BIT, true);

#endif

#ifdef INPROGRESS

    Notes:
    PG3 uses M1PWM1

    //Configure PWM Clock to match system
    SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

    // Enable the peripherals used by this program.
     SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
     SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);  //The Tiva Launchpad has two modules (0 and 1). Module 1 covers the LED pins
     ulPeriod = SysCtlClockGet() / 200; //PWM frequency 200HZ

     //Configure PF1,PF2,PF3 Pins as PWM
     GPIOPinConfigure(GPIO_PG3_M1PWM1);



#define SYSCTL_RCGCGPIO_R6      0x00000040  // GPIO Port G Run Mode Clock
                                            // Gating Control

    /* Enable Peripheral Clocks */
    SYSCTL_RCGCPWM_R |= 2;       /* enable clock to PWM0 is 1, PWM1 is 2 */
    SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_R6;   /* enable clock to PORTG */
    SYSCTL_RCC_R &= ~0x00100000; /* no pre-divide for PWM clock */

    /* Enable port PF3 for PWM1 M1PWM7 */
    GPIO_PORTG_AFSEL_R = 0x08;      /* PG3 uses alternate function */
    GPIO_PORTG_PCTL_R &= ~0x00000000; /* make PG3 PWM output pin */
    GPIO_PORTG_PCTL_R |= 0x00000020;            //M1PWM1 is bit.5 of GPIO_PORTG_PCTL_R Digital Function (GPIOPCTL PMCx Bit Field Encoding)
    GPIO_PORTG_DEN_R |= 8;       /* pin digital */



    //
    // Configure the GPIO Pin Mux for PG3
    // for M1PWM1
    //
    MAP_GPIOPinConfigure(GPIO_PG3_M1PWM1);
    MAP_GPIOPinTypePWM(GPIO_PORTG_BASE, GPIO_PIN_3);



    -----------------------------------------------------



    /* Generates 10KHz and 50% duty cycle on PF2 pin of TM4C123 Tiva C Launchpad */
    /* PWM1 module and PWM generator 3 of PWM1 module is used */
    #include "TM4C123GH6PM.h"

    int main(void)
    {

    void Delay_ms(int n);
    /* Clock setting for PWM and GPIO PORT */
    SYSCTL->RCGCPWM |= 2;       /* Enable clock to PWM1 module */
    SYSCTL->RCGCGPIO |= 0x20;   /* Enable system clock to PORTF */
    SYSCTL->RCC &= ~0x00100000; /* Directly feed clock to PWM1 module without pre-divider */

    /* Setting of PF2 pin for M1PWM6 channel output pin */
    GPIOF->AFSEL |= (1<<2);     /* PF2 sets a alternate function */
    GPIOF->PCTL &= ~0x00000F00; /*set PF2 as output pin */
    GPIOF->PCTL |= 0x00000500; /* make PF2 PWM output pin */
    GPIOF->DEN |= (1<<2);      /* set PF2 as a digital pin */

    /* PWM1 channel 6 setting */
    PWM1->_3_CTL &= ~(1<<0);   /* Disable Generator 3 counter */
    PWM1->_3_CTL &= ~(1<<1);   /* select down count mode of counter 3*/
    PWM1->_3_GENA = 0x0000008C; /* Set PWM output when counter reloaded and clear when matches PWMCMPA */
    PWM1->_3_LOAD = 16000;   /* set load value for 1kHz (16MHz/16000) */
    PWM1->_3_CMPA = 8000-1;  /* set duty cyle to 50% by loading of 16000 to PWM1CMPA */
    PWM1->_3_CTL = 1;         /* Enable Generator 3 counter */
    PWM1->ENABLE = 0x40;      /* Enable PWM1 channel 6 output */

        while(1)
        {
                //do nothing
        }
    }


    /* This function generates delay in ms */
    /* calculations are based on 16MHz system clock frequency */

    void Delay_ms(int time_ms)
    {
        int i, j;
        for(i = 0 ; i < time_ms; i++)
            for(j = 0; j < 3180; j++)
                {}  /* excute NOP for 1ms */
    }

    void SystemInit(void)
    {
        /* use this only if you are using old versions of Keil uvision */
        SCB->CPACR |= 0x00f00000;
    }




    --------------------------------------------------------------


    /* Generates 50HZ and variable duty cycle on PF2 pin of TM4C123 Tiva C Launchpad */
    /* PWM1 module and PWM generator 3 of PWM1 module is used. Hence PWM channel*/
    #include "TM4C123GH6PM.h"

    int main(void)
    {
    void Delay_ms(int n);
    int duty_cycle = 4999;

    /* Clock setting for PWM and GPIO PORT */
    SYSCTL->RCGCPWM |= 2;       /* Enable clock to PWM1 module */
    SYSCTL->RCGCGPIO |= 0x20;  /* Enable system clock to PORTF *
    SYSCTL->RCC |= (1<<20);    /* Enable System Clock Divisor function  */
    SYSCTL->RCC |= 0x000E0000; /* Use pre-divider valur of 64 and after that feed clock to PWM1 module*/

     /* Setting of PF2 pin for M1PWM6 channel output pin */
    GPIOF->AFSEL |= (1<<2);  /* PF2 sets a alternate function */
    GPIOF->PCTL &= ~0x00000F00; /*set PF2 as output pin */
    GPIOF->PCTL |= 0x00000500; /* make PF2 PWM output pin */
    GPIOF->DEN |= (1<<2);      /* set PF2 as a digital pin */

    PWM1->_3_CTL &= ~(1<<0);   /* Disable Generator 3 counter */
    PWM1->_3_CTL &= ~(1<<1);   /* select down count mode of counter 3*/
    PWM1->_3_GENA = 0x0000008C;  /* Set PWM output when counter reloaded and clear when matches PWMCMPA */
    PWM1->_3_LOAD = 5000;     /* set load value for 50Hz 16MHz/65 = 250kHz and (250KHz/5000) */
    PWM1->_3_CMPA = 4999;     /* set duty cyle to to minumum value*/
    PWM1->_3_CTL = 1;           /* Enable Generator 3 counter */
    PWM1->ENABLE = 0x40;      /* Enable PWM1 channel 6 output */

        while(1)
        {
            duty_cycle = duty_cycle - 10;
            if (duty_cycle <= 0)
           duty_cycle = 5000;
            PWM1->_3_CMPA = duty_cycle;
            Delay_ms(100);
        }
    }


    /* This function generates delay in ms */
    /* calculations are based on 16MHz system clock frequency */

    void Delay_ms(int time_ms)
    {
        int i, j;
        for(i = 0 ; i < time_ms; i++)
            for(j = 0; j < 3180; j++)
                {}  /* excute NOP for 1ms */
    }

    void SystemInit(void)
    {
        /* use this only if you are using old versions of Keil uvision */
        SCB->CPACR |= 0x00f00000;
    }
#endif

    //-----------------------------------------
#ifdef EXAMPLECODE
    #include "driverlib/pin_map.h"
    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_gpio.h"
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/gpio.h"
    #include "driverlib/pwm.h"

    void delayMS(int ms) {
        SysCtlDelay( (SysCtlClockGet()/(3*1000))*ms ) ;
    }

    int
    main(void)
    {
        //Set the clock
       SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC |   SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

       //Configure PWM Clock to match system
       SysCtlPWMClockSet(SYSCTL_PWMDIV_1);

       // Enable the peripherals used by this program.
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);  //The Tiva Launchpad has two modules (0 and 1). Module 1 covers the LED pins

        //Configure PF1,PF2,PF3 Pins as PWM
        GPIOPinConfigure(GPIO_PF1_M1PWM5);
        GPIOPinConfigure(GPIO_PF2_M1PWM6);
        GPIOPinConfigure(GPIO_PF3_M1PWM7);
        GPIOPinTypePWM(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);

        //Configure PWM Options
        //PWM_GEN_2 Covers M1PWM4 and M1PWM5
        //PWM_GEN_3 Covers M1PWM6 and M1PWM7 See page 207 4/11/13 DriverLib doc
        PWMGenConfigure(PWM1_BASE, PWM_GEN_2, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);
        PWMGenConfigure(PWM1_BASE, PWM_GEN_3, PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);

        //Set the Period (expressed in clock ticks)
        PWMGenPeriodSet(PWM1_BASE, PWM_GEN_2, 320);
        PWMGenPeriodSet(PWM1_BASE, PWM_GEN_3, 320);

        //Set PWM duty-50% (Period /2)
        PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,100);
        PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,100);
        PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,100);

        // Enable the PWM generator
        PWMGenEnable(PWM1_BASE, PWM_GEN_2);
        PWMGenEnable(PWM1_BASE, PWM_GEN_3);

        // Turn on the Output pins
        PWMOutputState(PWM1_BASE, PWM_OUT_5_BIT | PWM_OUT_6_BIT | PWM_OUT_7_BIT, true);

        //Fade
        bool fadeUp = true;
        unsigned long increment = 10;
        unsigned long pwmNow = 100;
        while(1)
        {
            delayMS(20);
            if (fadeUp) {
                pwmNow += increment;
                if (pwmNow >= 320) { fadeUp = false; }
            }
            else {
                pwmNow -= increment;
                if (pwmNow <= 10) { fadeUp = true; }
            }
            PWMPulseWidthSet(PWM1_BASE, PWM_OUT_5,pwmNow);
            PWMPulseWidthSet(PWM1_BASE, PWM_OUT_6,pwmNow);
            PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,pwmNow);
        }

    }
#endif



#endif /* MPHOX_PWM_H_ */
