//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(tBoolean bFast)
{
    unsigned long ulIdx;

    //
    // Enable the I2C and GPIO port B blocks as they are needed by this driver.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

#if (!(defined OSRAM_ONLY) && !(defined RIT_ONLY))
    //
    // Read SysCtl DID1 register to determine whether this is an older board
    // with the OSRAM display or a newer one with the RIT model.
    //
    g_ucDisplayIsRIT = (HWREG(SYSCTL_DID1) & (1 << 12)) ? 1 : 0;

    //
    // Set the correct number of non-displayed columns given the display type
    // we are using.
    //
    g_ucColumnAdjust = g_ucDisplayIsRIT ? 4 : 36;
#endif

    //
    // If using the RIT display, we need to enable power by pulling PD7 high.
    //
#ifndef OSRAM_ONLY
#ifndef RIT_ONLY
    if(g_ucDisplayIsRIT)
    {
#endif
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
        GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_7);
        GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_7, GPIO_PIN_7);
#ifndef RIT_ONLY
    }
#endif
#endif

    //
    // Configure the I2C SCL and SDA pins for I2C operation.
    //
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_2 | GPIO_PIN_3);

    //
    // Initialize the I2C master.
    //
    I2CMasterInitExpClk(I2C0_MASTER_BASE, SysCtlClockGet(), bFast);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < SIZE_INIT_CMDS; ulIdx += g_pucDisplayInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucDisplayInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucDisplayInit + ulIdx + 2,
                               g_pucDisplayInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucDisplayInit[ulIdx +
                                                  g_pucDisplayInit[ulIdx]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();
}
//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(uint8_t bFast)
{
    uint32_t ulTmp;

    //
    // Enable the I2C and GPIO port B blocks as they are needed by this driver.
    //
    SYSCTL->RCGC1 |= (1 << 12);                    /* enable clock to I2C0  */
    SYSCTL->RCGC2 |= (1 <<  1);                    /* enable clock to GPIOB */

#if (!(defined OSRAM_ONLY) && !(defined RIT_ONLY))
    //
    // Read SysCtl DID1 register to determine whether this is an older board
    // with the OSRAM display or a newer one with the RIT model.
    //
    g_ucDisplayIsRIT = (SYSCTL->DID1 & (1 << 12)) ? 1 : 0;

    //
    // Set the correct number of non-displayed columns given the display type
    // we are using.
    //
    g_ucColumnAdjust = g_ucDisplayIsRIT ? 4 : 36;
#endif

    //
    // If using the RIT display, we need to enable power by pulling PD7 high.
    //
#ifndef OSRAM_ONLY
#ifndef RIT_ONLY
    if(g_ucDisplayIsRIT)
    {
#endif
        SYSCTL->RCGC2 |= (1 <<  3);                /* enable clock to GPIOD */
        SysCtlDelay(1);            /* wait a tiny bit after enabling clocks */

        GPIOD->DIR |= (1 << 7);       /* set GPIOD-pin7 direction to output */
        GPIOD->DATA_Bits[1 << 7] = (1 << 7);       /* drive GPIOD-pin7 high */
#ifndef RIT_ONLY
    }
#endif
#endif

    //
    // Configure the I2C SCL and SDA pins for I2C operation.
    //
    ulTmp = (1 << 2) | (1 << 3);
    GPIOB->DIR   &= ~ulTmp;
    GPIOB->AFSEL |= ulTmp;
    GPIOB->DR2R  |= ulTmp;      /* set 2mA drive, DR4R and DR8R are cleared */
    GPIOB->SLR   &= ~ulTmp;
    GPIOB->ODR   |= ulTmp;
    GPIOB->PUR   |= ulTmp;              /* set weak pull-up; PDR is cleared */
    GPIOB->DEN   |= ulTmp;
    GPIOB->AMSEL &= ~ulTmp;

    //
    // Initialize the I2C master.
    //
    I2C0_MASTER->MCR |= (1 << 4);                      /* I2C master enable */
    if (bFast) {
        ulTmp = 400000;
    }
    else {
        ulTmp = 100000;
    }
    I2C0_MASTER->MTPR = ((SystemFrequency + (2 * 10 * ulTmp) - 1)
                          / (2 * 10 * ulTmp)) - 1;

    //
    // Compute the inter-byte delay for the display controller.  This delay is
    // dependent upon the I2C bus clock rate; the slower the clock the longer
    // the delay required.
    //
    // The derivation of this formula is based on a measured delay of
    // SysCtlDelay(1700) for a 100 kHz I2C bus with the CPU running at 50 MHz
    // (referred to as C).  To scale this to the delay for a different CPU
    // speed (since this is just a CPU-based delay loop) is:
    //
    //           f(CPU)
    //     C * ----------
    //         50,000,000
    //
    // To then scale this to the actual I2C rate (since it won't always be
    // precisely 100 kHz):
    //
    //           f(CPU)     100,000
    //     C * ---------- * -------
    //         50,000,000    f(I2C)
    //
    // This equation will give the inter-byte delay required for any
    // configuration of the I2C master.  But, as arranged it is impossible to
    // directly compute in 32-bit arithmetic (without loosing a lot of
    // accuracy).  So, the equation is simplified.
    //
    // Since f(I2C) is generated by dividing down from f(CPU), replace it with
    // the equivalent (where TPR is the value programmed into the Master Timer
    // Period Register of the I2C master, with the 1 added back):
    //
    //                        100,000
    //           f(CPU)       -------
    //     C * ---------- *    f(CPU)
    //         50,000,000   ------------
    //                      2 * 10 * TPR
    //
    // Inverting the dividend in the last term:
    //
    //           f(CPU)     100,000 * 2 * 10 * TPR
    //     C * ---------- * ----------------------
    //         50,000,000          f(CPU)
    //
    // The f(CPU) now cancels out.
    //
    //         100,000 * 2 * 10 * TPR
    //     C * ----------------------
    //               50,000,000
    //
    // Since there are no clock frequencies left in the equation, this equation
    // also works for 400 kHz bus operation as well, since the 100,000 in the
    // numerator becomes 400,000 but C is 1/4, which cancel out each other.
    // Reducing the constants gives:
    //
    //         TPR              TPR             TPR
    //     C * ---   =   1700 * ---   =   340 * ---   = 68 * TPR
    //         25               25               5
    //
    // Note that the constant C is actually a bit larger than it needs to be in
    // order to provide some safety margin.
    //
    g_ulDelay = 68 * (I2C0_MASTER->MTPR + 1);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulTmp = 0; ulTmp < SIZE_INIT_CMDS; ulTmp += g_pucDisplayInit[ulTmp] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucDisplayInit[ulTmp + 1]);
        Display96x16x1WriteArray(g_pucDisplayInit + ulTmp + 2,
                                 g_pucDisplayInit[ulTmp] - 2);
        Display96x16x1WriteFinal(g_pucDisplayInit[ulTmp
                                 + g_pucDisplayInit[ulTmp]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();
}
//*****************************************************************************
//
// This function is the display "task" that is called periodically by the
// Scheduler from the application main processing loop.
// This function displays a cycle of several messages on the display.
//
// Odd values are used for timeouts for some of the displayed messaged.  For
// example a message may be shown for 5.3 seconds.  This is done to keep the
// changes on the display out of sync with the LED blinking which occurs
// once per second.
//
//*****************************************************************************
void
DisplayTask(void *pvParam)
{
    static unsigned long ulState = 0;
    static unsigned long ulLastTick = 0;
    static unsigned long ulTimeout = 0;

    //
    // Check to see if the timeout has expired and it is time to change the
    // display.
    //
    if(SchedulerElapsedTicksGet(ulLastTick) > ulTimeout)
    {
        //
        // Save the current tick value to use for comparison next time through
        //
        ulLastTick = SchedulerTickCountGet();

        //
        // Switch based on the display task state
        //
        switch(ulState)
        {
            //
            // In this state, the scrolling TI logo is initialized, and the
            // state changed to next state.  A short timeout is used so that
            // the scrolling image will start next time through this task.
            //
            case 0:
            {
                ScrollImage(g_pucTILogo, sizeof(g_pucTILogo) / 2);
                ulTimeout = 1;
                ulState = 1;
                break;
            }

            //
            // In this state the TI logo is scrolled across the screen with
            // a scroll rate of this task period (each time this task is
            // called by the scheduler it advances the scroll by one pixel
            // column).
            //
            case 1:
            {
                if(ScrollImage(g_pucTILogo, 0))
                {
                    //
                    // If the scrolling is done, then advance the state and
                    // set the timeout for 1.3 seconds (next state will start
                    // in 1.3 seconds).
                    //
                    ulTimeout = 130;
                    ulState = 2;
                }
                break;
            }

            //
            // This state shows the text "STELLARIS" on the display for 5.3
            // seconds.
            //
            case 2:
            {
                Display96x16x1StringDraw("STELLARIS", 21, 0);
                ulTimeout = 530;
                ulState = 3;
                break;
            }

            //
            // This state clears the screen for 1.3 seconds.
            //
            case 3:
            {
                Display96x16x1Clear();
                ulTimeout = 130;
                ulState = 4;
                break;
            }

            //
            // This state shows "EVALBOT" for 5.3 seconds.
            //
            case 4:
            {
                Display96x16x1StringDraw("EVALBOT", 27, 0);
                ulTimeout = 530;
                ulState = 5;
                break;
            }

            //
            // This state clears the screen for 1.3 seconds.
            //
            case 5:
            {
                Display96x16x1Clear();
                ulTimeout = 130;
                ulState = 0;
                break;
            }

            //
            // The default state should not occur, but if it does, then reset
            // back to the beginning state.
            //
            default:
            {
                ulTimeout = 130;
                ulState = 0;
                break;
            }
        }
    }
}
//*****************************************************************************
//
// The main application.  It configures the board and then enters a loop
// to show messages on the display and blink the LEDs.
//
//*****************************************************************************
int
main(void)
{
    unsigned long ulPHYMR0;
    unsigned long ulNextTickLED = 0;
    unsigned long ulNextTickDisplay = 0;
    unsigned long ulDisplayState = 0;

    //
    // Set the clocking to directly from the crystal
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    //
    // Since the Ethernet is not used, power down the PHY to save battery.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ETH);
    ulPHYMR0 = ROM_EthernetPHYRead(ETH_BASE, PHY_MR0);
    ROM_EthernetPHYWrite(ETH_BASE, PHY_MR0, ulPHYMR0 | PHY_MR0_PWRDN);

    //
    // Initialize the board display
    //
    Display96x16x1Init(true);

    //
    // Initialize the GPIO used for the LEDs, and then turn one LED on
    // and the other off
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_5);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4, 0);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_5, GPIO_PIN_5);

    //
    // Set up and enable the SysTick timer to use as a time reference.
    // It will be set up for a 100 ms tick.
    //
    ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 10);
    ROM_SysTickEnable();
    ROM_SysTickIntEnable();

    //
    // Enter loop to run forever, blinking LEDs and printing messages to
    // the display
    //
    for(;;)
    {
        //
        // If LED blink period has timed out, then toggle LEDs.
        //
        if(g_ulTickCount >= ulNextTickLED)
        {
            //
            // Set next LED toggle timeout for 1 second
            //
            ulNextTickLED += 10;

            //
            // Toggle the state of each LED
            //
            ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_4 | GPIO_PIN_5,
                             ~ROM_GPIOPinRead(GPIO_PORTF_BASE,
                                              GPIO_PIN_4 | GPIO_PIN_5));
        }

        //
        // If display interval has elapsed, then update display state
        //
        if(g_ulTickCount >= ulNextTickDisplay)
        {
            //
            // Process the display state.  Each state, the display does
            // something different.
            //
            // Odd time intervals are used, like 5.3 seconds, to keep the
            // display updates out of sync with the LED blinking which is
            // happening on a 1 second interval.  This is just for cosmetic
            // effect, there is no technical reason it needs to be that way.
            //
            switch(ulDisplayState)
            {
                //
                // Initial state, show TEXAS INSTRUMENTS for 5.3 seconds
                //
                case 0:
                {
                    Display96x16x1StringDraw("TEXAS", 29, 0);
                    Display96x16x1StringDraw("INSTRUMENTS", 11, 1);
                    ulNextTickDisplay += 53;
                    ulDisplayState = 1;
                    break;
                }

                //
                // Next, clear display for 1.3 seconds
                //
                case 1:
                {
                    Display96x16x1Clear();
                    ulNextTickDisplay += 13;
                    ulDisplayState = 2;
                    break;
                }

                //
                // Show STELLARIS for 5.3 seconds
                //
                case 2:
                {
                    Display96x16x1StringDraw("STELLARIS", 21, 0);
                    ulNextTickDisplay += 53;
                    ulDisplayState = 3;
                    break;
                }

                //
                // Clear the previous message and then show EVALBOT on
                // the second line for 5.3 seconds
                //
                case 3:
                {
                    Display96x16x1Clear();
                    Display96x16x1StringDraw("EVALBOT", 27, 1);
                    ulNextTickDisplay += 53;
                    ulDisplayState = 4;
                    break;
                }

                //
                // Clear display for 1.3 seconds, then go back to start
                //
                case 4:
                {
                    Display96x16x1Clear();
                    ulNextTickDisplay += 13;
                    ulDisplayState = 0;
                    break;
                }

                //
                // Default state.  Should never get here, but if so just
                // go back to starting state.
                //
                default:
                {
                    ulDisplayState = 0;
                    break;
                }
            } // end switch
        } // end if
    } // end for(;;)
}
//*****************************************************************************
//
//! Initialize the OLED display.
//!
//! \param bFast is a boolean that is \e true if the I2C interface should be
//! run at 400 kbps and \e false if it should be run at 100 kbps.
//!
//! This function initializes the I2C interface to the OLED display and
//! configures the SSD0303 or SSD1300 controller on the panel.
//!
//! \return None.
//
//*****************************************************************************
void
Display96x16x1Init(tBoolean bFast)
{
    unsigned long ulIdx;

    //
    // The power supply for the OLED display comes from the motor power
    // supply, which must be turned on.  If the application is using the
    // motor then this is taken care of when the motor driver is initialized.
    // But if the motor driver is not used, then the motor power supply needs
    // to be turned on here so the OLED works properly.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_5);
    GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_5, GPIO_PIN_5);

    //
    // Enable the I2C and GPIO peripherals needed for the display.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);

    //
    // Deassert the display controller reset signal (active low)
    //
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);

    //
    // Wait a short delay, then drive the pin low to reset the controller
    //
    SysCtlDelay(32);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, 0);

    //
    // Leave it is reset for a short delay, then drive it high to deassert
    // reset.  Then the controller should be out of reset.
    //
    SysCtlDelay(32);
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0);

    //
    // Configure the GPIO pins needed for the display as I2C
    //
    GPIOPinConfigure(GPIO_PG0_I2C1SCL);
    GPIOPinConfigure(GPIO_PG1_I2C1SDA);
    GPIOPinTypeI2C(GPIO_PORTG_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Reset the I2C1 peripheral.
    //
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C1);

    //
    // Initialize the I2C master.
    //
    I2CMasterInitExpClk(I2C1_MASTER_BASE, SysCtlClockGet(), bFast);

    //
    // Initialize the display controller.  Loop through the initialization
    // sequence doing a single I2C transfer for each command.
    //
    for(ulIdx = 0; ulIdx < sizeof(g_pucRITInit);
        ulIdx += g_pucRITInit[ulIdx] + 1)
    {
        //
        // Send this command.
        //
        Display96x16x1WriteFirst(g_pucRITInit[ulIdx + 1]);
        Display96x16x1WriteArray(g_pucRITInit + ulIdx + 2,
                                 g_pucRITInit[ulIdx] - 2);
        Display96x16x1WriteFinal(g_pucRITInit[ulIdx + g_pucRITInit[ulIdx]]);
    }

    //
    // Clear the frame buffer.
    //
    Display96x16x1Clear();

    //
    // Turn the display on.
    //
    Display96x16x1DisplayOn();
}