//***************************************************************************** // // Uses the fColorWheelPos variable to update the color mix shown on the RGB // // This function is called when system has decided it is time to enter // Hibernate. This will prepare the hibernate peripheral, save the system // state and then enter hibernate mode. // //***************************************************************************** void AppHibernateEnter(void) { // // Alert UART command line users that we are going to hibernate // UARTprintf("Entering Hibernate...\n"); // // Prepare Hibernation Module // HibernateGPIORetentionEnable(); HibernateRTCSet(0); HibernateRTCEnable(); HibernateRTCMatchSet(0, 5); HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC); // // Store state information to battery backed memory // since sizeof returns number of bytes we convert to words and force // a rounding up to next whole word. // HibernateDataSet((uint32_t*)&g_sAppState, sizeof(tAppState)/4+1); // // Disable the LED for 100 milliseconds to let user know we are // ready for hibernate and will hibernate on relase of buttons // RGBDisable(); SysCtlDelay(SysCtlClockGet()/3/10); RGBEnable(); // // Wait for wake button to be released prior to going into hibernate // while(g_sAppState.ui32Buttons & RIGHT_BUTTON) { // //Delay for about 300 clock ticks to allow time for interrupts to //sense that button is released // SysCtlDelay(100); } // // Disable the LED for power savings and go to hibernate mode // RGBDisable(); HibernateRequest(); }
/***************************************************** * Function: init_Hibernation * Description: Initialize hibernation module * Input: NONE * Output: NONE *****************************************************/ void init_Hibernation(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); // Enable hibernation module HibernateEnableExpClk(SysCtlClockGet()); // Enable clocking to the hibernation module HibernateClockConfig(HIBERNATE_OSC_LOWDRIVE); // Configure the clock source for hibernation module HibernateGPIORetentionEnable(); // Retain GPIO state during hibernation // HibernateRTCEnable(); // Enable the RTC // HibernateRTCSet(0); // Clear the RTC time //HibernateRTCMatchSet(0, HibernateRTCGet() + 5); // Set the match 0 register for 5 seconds from now HibernateWakeSet(HIBERNATE_WAKE_PIN); // Configure to wake on button press or RTC match // HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC // Clear any pending status. // int status = HibernateIntStatus(0); // Clear any pending status HibernateIntClear(status); }
//***************************************************************************** // //! This is the hibernate module handler. //! When the RTC timer expires, an interrupt is generated and the the GPS //! data is parsed and logged. //! //! If the Wake button is pressed, low power mode is disabled. //! A reset/power cycle is required to re-enable low power mode after Wake has //! been pressed. // //***************************************************************************** void lowPowerMode(int delaySeconds) { uint32_t ui32Status; // // Set the RTC to 0 or an initial value. The RTC can be set once when the // system is initialized after the cold startup and then left to run. Or // it can be initialized before every hibernate. // HibernateRTCSet(0); // // Set the match 0 register for 30 seconds from now. // HibernateRTCMatchSet(0, HibernateRTCGet() + delaySeconds); // // Clear any pending status. // ui32Status = HibernateIntStatus(0); HibernateIntClear(ui32Status); // // Save the program state information. The state information is stored in // the pui32NVData[] array. It is not necessary to save the full 16 words // of data, only as much as is actually needed by the program. // HibernateDataSet(&lowPowerOn, 1); // // Configure to wake on RTC match or when wake button is pressed. // HibernateWakeSet(HIBERNATE_WAKE_RTC | HIBERNATE_WAKE_PIN); // // Request hibernation. The following call may return because it takes a // finite amount of time for power to be removed. // HibernateRequest(); // // Spin here to wait for the power to be removed. // for(;;) { } } // End function lowPowerMode
/************************** YOSEMIT XOLALPA ROSALES PROF. VENKI CPE403 TIVAC-LAB06 **************************/ int main(void) { SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0x08); SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); HibernateEnableExpClk(SysCtlClockGet()); HibernateGPIORetentionEnable(); SysCtlDelay(64000000); HibernateRTCSet(0); HibernateRTCEnable(); HibernateRTCMatchSet(0,5); HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC); GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_3, 0x00); HibernateRequest(); while(1) { } }
//***************************************************************************** // // Initialize and operate the data logger. // //***************************************************************************** int main(void) { tContext sDisplayContext, sBufferContext; uint32_t ui32HibIntStatus, ui32SysClock, ui32LastTickCount; bool bSkipSplash; uint8_t ui8ButtonState, ui8ButtonChanged; uint_fast8_t ui8X, ui8Y; // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // MAP_FPULazyStackingEnable(); // // Set the clocking to run at 50 MHz. // MAP_SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN); ui32SysClock = MAP_SysCtlClockGet(); // // Initialize locals. // bSkipSplash = false; ui32LastTickCount = 0; // // Initialize the data acquisition module. This initializes the ADC // hardware. // AcquireInit(); // // Enable access to the hibernate peripheral. If the hibernate peripheral // was already running then this will have no effect. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); // // Check to see if the hiberate module is already active and if so then // read the saved configuration state. If both are okay, then proceed // to check and see if we are logging data using sleep mode. // if(HibernateIsActive() && !GetSavedState(&g_sConfigState)) { // // Read the status of the hibernate module. // ui32HibIntStatus = HibernateIntStatus(1); // // If this is a pin wake, that means the user pressed the select // button and we should terminate the sleep logging. In this case // we will fall out of this conditional section, and go through the // normal startup below, but skipping the splash screen so the user // gets immediate response. // if(ui32HibIntStatus & HIBERNATE_INT_PIN_WAKE) { // // Clear the interrupt flag so it is not seen again until another // wake. // HibernateIntClear(HIBERNATE_INT_PIN_WAKE); bSkipSplash = true; } // // Otherwise if we are waking from hibernate and it was not a pin // wake, then it must be from RTC match. Check to see if we are // sleep logging and if so then go through an abbreviated startup // in order to collect the data and go back to sleep. // else if(g_sConfigState.ui32SleepLogging && (ui32HibIntStatus & HIBERNATE_INT_RTC_MATCH_0)) { // // Start logger and pass the configuration. The logger should // configure itself to take one sample. // AcquireStart(&g_sConfigState); g_iLoggerState = eSTATE_LOGGING; // // Enter a forever loop to run the acquisition. This will run // until a new sample has been taken and stored. // while(!AcquireRun()) { } // // Getting here means that a data acquisition was performed and we // can now go back to sleep. Save the configuration and then // activate the hibernate. // SetSavedState(&g_sConfigState); // // Set wake condition on pin-wake or RTC match. Then put the // processor in hibernation. // HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC); HibernateRequest(); // // Hibernating takes a finite amount of time to occur, so wait // here forever until hibernate activates and the processor // power is removed. // for(;;) { } } // // Otherwise, this was not a pin wake, and we were not sleep logging, // so just fall out of this conditional and go through the normal // startup below. // } else { // // In this case, either the hibernate module was not already active, or // the saved configuration was not valid. Initialize the configuration // to the default state and then go through the normal startup below. // GetDefaultState(&g_sConfigState); } // // Enable the Hibernate module to run. // HibernateEnableExpClk(SysCtlClockGet()); // // The hibernate peripheral trim register must be set per silicon // erratum 2.1 // HibernateRTCTrimSet(0x7FFF); // // Start the RTC running. If it was already running then this will have // no effect. // HibernateRTCEnable(); // // In case we were sleep logging and are now finished (due to user // pressing select button), then disable sleep logging so it doesnt // try to start up again. // g_sConfigState.ui32SleepLogging = 0; SetSavedState(&g_sConfigState); // // Initialize the display driver. // CFAL96x64x16Init(); // // Initialize the buttons driver. // ButtonsInit(); // // Pass the restored state to the menu system. // MenuSetState(&g_sConfigState); // // Enable the USB peripheral // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0); // // Configure the required pins for USB operation. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); MAP_GPIOPinConfigure(GPIO_PG4_USB0EPEN); MAP_GPIOPinTypeUSBDigital(GPIO_PORTG_BASE, GPIO_PIN_4); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOL); MAP_GPIOPinTypeUSBAnalog(GPIO_PORTL_BASE, GPIO_PIN_6 | GPIO_PIN_7); MAP_GPIOPinTypeUSBAnalog(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Erratum workaround for silicon revision A1. VBUS must have pull-down. // if(CLASS_IS_BLIZZARD && REVISION_IS_A1) { HWREG(GPIO_PORTB_BASE + GPIO_O_PDR) |= GPIO_PIN_1; } // // Initialize the USB stack mode and pass in a mode callback. // USBStackModeSet(0, eUSBModeOTG, ModeCallback); // // Initialize the stack to be used with USB stick. // USBStickInit(); // // Initialize the stack to be used as a serial device. // USBSerialInit(); // // Initialize the USB controller for dual mode operation with a 2ms polling // rate. // USBOTGModeInit(0, 2000, g_pui8HCDPool, HCD_MEMORY_SIZE); // // Initialize the menus module. This module will control the user // interface menuing system. // MenuInit(WidgetActivated); // // Configure SysTick to periodically interrupt. // g_ui32TickCount = 0; MAP_SysTickPeriodSet(ui32SysClock / CLOCK_RATE); MAP_SysTickIntEnable(); MAP_SysTickEnable(); // // Initialize the display context and another context that is used // as an offscreen drawing buffer for display animation effect // GrContextInit(&sDisplayContext, &g_sCFAL96x64x16); GrContextInit(&sBufferContext, &g_sOffscreenDisplayA); // // Show the splash screen if we are not skipping it. The only reason to // skip it is if the application was in sleep-logging mode and the user // just waked it up with the select button. // if(!bSkipSplash) { const uint8_t *pui8SplashLogo = g_pui8Image_TI_Black; // // Draw the TI logo on the display. Use an animation effect where the // logo will "slide" onto the screen. Allow select button to break // out of animation. // for(ui8X = 0; ui8X < 96; ui8X++) { if(ButtonsPoll(0, 0) & SELECT_BUTTON) { break; } GrImageDraw(&sDisplayContext, pui8SplashLogo, 95 - ui8X, 0); } // // Leave the logo on the screen for a long duration. Monitor the // buttons so that if the user presses the select button, the logo // display is terminated and the application starts immediately. // while(g_ui32TickCount < 400) { if(ButtonsPoll(0, 0) & SELECT_BUTTON) { break; } } // // Extended splash sequence // if(ButtonsPoll(0, 0) & UP_BUTTON) { for(ui8X = 0; ui8X < 96; ui8X += 4) { GrImageDraw(&sDisplayContext, g_ppui8Image_Splash[(ui8X / 4) & 3], (int32_t)ui8X - 96L, 0); GrImageDraw(&sDisplayContext, pui8SplashLogo, ui8X, 0); MAP_SysCtlDelay(ui32SysClock / 12); } MAP_SysCtlDelay(ui32SysClock / 3); pui8SplashLogo = g_ppui8Image_Splash[4]; GrImageDraw(&sDisplayContext, pui8SplashLogo, 0, 0); MAP_SysCtlDelay(ui32SysClock / 12); } // // Draw the initial menu into the offscreen buffer. // SlideMenuDraw(&g_sMenuWidget, &sBufferContext, 0); // // Now, draw both the TI logo splash screen (from above) and the initial // menu on the screen at the same time, moving the coordinates so that // the logo "slides" off the display and the menu "slides" onto the // display. // for(ui8Y = 0; ui8Y < 64; ui8Y++) { GrImageDraw(&sDisplayContext, pui8SplashLogo, 0, -ui8Y); GrImageDraw(&sDisplayContext, g_pui8OffscreenBufA, 0, 63 - ui8Y); } } // // Add the menu widget to the widget tree and send an initial paint // request. // WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sMenuWidget); WidgetPaint(WIDGET_ROOT); // // Set the focus handle to the menu widget. Any button events will be // sent to this widget // g_ui32KeyFocusWidgetHandle = (uint32_t)&g_sMenuWidget; // // Forever loop to run the application // while(1) { // // Each time the timer tick occurs, process any button events. // if(g_ui32TickCount != ui32LastTickCount) { // // Remember last tick count // ui32LastTickCount = g_ui32TickCount; // // Read the debounced state of the buttons. // ui8ButtonState = ButtonsPoll(&ui8ButtonChanged, 0); // // Pass any button presses through to the widget message // processing mechanism. The widget that has the button event // focus (probably the menu widget) will catch these button events. // if(BUTTON_PRESSED(SELECT_BUTTON, ui8ButtonState, ui8ButtonChanged)) { SendWidgetKeyMessage(WIDGET_MSG_KEY_SELECT); } if(BUTTON_PRESSED(UP_BUTTON, ui8ButtonState, ui8ButtonChanged)) { SendWidgetKeyMessage(WIDGET_MSG_KEY_UP); } if(BUTTON_PRESSED(DOWN_BUTTON, ui8ButtonState, ui8ButtonChanged)) { SendWidgetKeyMessage(WIDGET_MSG_KEY_DOWN); } if(BUTTON_PRESSED(LEFT_BUTTON, ui8ButtonState, ui8ButtonChanged)) { SendWidgetKeyMessage(WIDGET_MSG_KEY_LEFT); } if(BUTTON_PRESSED(RIGHT_BUTTON, ui8ButtonState, ui8ButtonChanged)) { SendWidgetKeyMessage(WIDGET_MSG_KEY_RIGHT); } } // // Tell the OTG library code how much time has passed in milliseconds // since the last call. // USBOTGMain(GetTickms()); // // Call functions as needed to keep the host or device mode running. // if(g_iCurrentUSBMode == eUSBModeDevice) { USBSerialRun(); } else if(g_iCurrentUSBMode == eUSBModeHost) { USBStickRun(); } // // If in the logging state, then call the logger run function. This // keeps the data acquisition running. // if((g_iLoggerState == eSTATE_LOGGING) || (g_iLoggerState == eSTATE_VIEWING)) { if(AcquireRun() && g_sConfigState.ui32SleepLogging) { // // If sleep logging is enabled, then at this point we have // stored the first data item, now save the state and start // hibernation. Wait for the power to be cut. // SetSavedState(&g_sConfigState); HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC); HibernateRequest(); for(;;) { } } // // If viewing instead of logging then request a repaint to keep // the viewing window updated. // if(g_iLoggerState == eSTATE_VIEWING) { WidgetPaint(WIDGET_ROOT); } } // // If in the saving state, then save data from flash storage to // USB stick. // if(g_iLoggerState == eSTATE_SAVING) { // // Save data from flash to USB // FlashStoreSave(); // // Return to idle state // g_iLoggerState = eSTATE_IDLE; } // // If in the erasing state, then erase the data stored in flash. // if(g_iLoggerState == eSTATE_ERASING) { // // Save data from flash to USB // FlashStoreErase(); // // Return to idle state // g_iLoggerState = eSTATE_IDLE; } // // If in the flash reporting state, then show the report of the amount // of used and free flash memory. // if(g_iLoggerState == eSTATE_FREEFLASH) { // // Report free flash space // FlashStoreReport(); // // Return to idle state // g_iLoggerState = eSTATE_IDLE; } // // If we are exiting the clock setting widget, that means that control // needs to be given back to the menu system. // if(g_iLoggerState == eSTATE_CLOCKEXIT) { // // Give the button event focus back to the menu system // g_ui32KeyFocusWidgetHandle = (uint32_t)&g_sMenuWidget; // // Send a button event to the menu widget that means the left // key was pressed. This signals the menu widget to deactivate // the current child widget (which was the clock setting wigdet). // This will cause the menu widget to slide the clock set widget // off the screen and resume control of the display. // SendWidgetKeyMessage(WIDGET_MSG_KEY_LEFT); g_iLoggerState = eSTATE_IDLE; } // // Process any new messages that are in the widget queue. This keeps // the user interface running. // WidgetMessageQueueProcess(); } }
//***************************************************************************** // // Run the hibernate example. Use a loop to put the microcontroller into // hibernate mode, and to wake up based on time. Also allow the user to cause // it to hibernate and/or wake up based on button presses. // //***************************************************************************** int main(void) { uint32_t ui32Idx; uint32_t ui32Status = 0; uint32_t ui32HibernateCount = 0; tContext sContext; tRectangle sRect; // // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. // ROM_FPULazyStackingEnable(); // // Set the clocking to run directly from the crystal. // ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // // Initialize the UART. // ConfigureUART(); // // Initialize the OLED display // CFAL96x64x16Init(); // // Initialize the graphics context. // GrContextInit(&sContext, &g_sCFAL96x64x16); // // Fill the top 24 rows of the screen with blue to create the banner. // sRect.i16XMin = 0; sRect.i16YMin = 0; sRect.i16XMax = GrContextDpyWidthGet(&sContext) - 1; sRect.i16YMax = 9; GrContextForegroundSet(&sContext, ClrDarkBlue); GrRectFill(&sContext, &sRect); // // Change foreground for white text. // GrContextForegroundSet(&sContext, ClrWhite); // // Put the application name in the middle of the banner. // GrContextFontSet(&sContext, g_psFontFixed6x8); GrStringDrawCentered(&sContext, "hibernate", -1, GrContextDpyWidthGet(&sContext) / 2, 4, 0); // // Initialize the buttons driver // ButtonsInit(); // // Set up systick to generate interrupts at 100 Hz. // ROM_SysTickPeriodSet(ROM_SysCtlClockGet() / 100); ROM_SysTickIntEnable(); ROM_SysTickEnable(); // // Enable the Hibernation module. // ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); // // Print wake cause message on display. // GrStringDrawCentered(&sContext, "Wake due to:", -1, GrContextDpyWidthGet(&sContext) / 2, Row(2) + 4, true); // // Check to see if Hibernation module is already active, which could mean // that the processor is waking from a hibernation. // if(HibernateIsActive()) { // // Read the status bits to see what caused the wake. // ui32Status = HibernateIntStatus(0); HibernateIntClear(ui32Status); // // Wake was due to the push button. // if(ui32Status & HIBERNATE_INT_PIN_WAKE) { GrStringDrawCentered(&sContext, "BUTTON", -1, GrContextDpyWidthGet(&sContext) / 2, Row(3) + 4, true); } // // Wake was due to RTC match // else if(ui32Status & HIBERNATE_INT_RTC_MATCH_0) { GrStringDrawCentered(&sContext, "TIMEOUT", -1, GrContextDpyWidthGet(&sContext) / 2, Row(3) + 4, true); } // // Wake is due to neither button nor RTC, so it must have been a hard // reset. // else { GrStringDrawCentered(&sContext, "RESET", -1, GrContextDpyWidthGet(&sContext) / 2, Row(3) + 4, true); } // // If the wake is due to button or RTC, then read the first location // from the battery backed memory, as the hibernation count. // if(ui32Status & (HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_RTC_MATCH_0)) { HibernateDataGet(&ui32HibernateCount, 1); } } // // Enable the Hibernation module. This should always be called, even if // the module was already enabled, because this function also initializes // some timing parameters. // HibernateEnableExpClk(ROM_SysCtlClockGet()); // // If the wake was not due to button or RTC match, then it was a reset. // if(!(ui32Status & (HIBERNATE_INT_PIN_WAKE | HIBERNATE_INT_RTC_MATCH_0))) { // // Configure the module clock source. // HibernateClockConfig(HIBERNATE_OSC_LOWDRIVE); // // Finish the wake cause message. // GrStringDrawCentered(&sContext, "RESET", -1, GrContextDpyWidthGet(&sContext) / 2, Row(3) + 4, true); // // Wait a couple of seconds in case we need to break in with the // debugger. // SysTickWait(3 * 100); // // Allow time for the crystal to power up. This line is separated from // the above to make it clear this is still needed, even if the above // delay is removed. // SysTickWait(15); } // // Print the count of times that hibernate has occurred. // usnprintf(g_pcBuf, sizeof(g_pcBuf), "Hib count=%4u", ui32HibernateCount); GrStringDrawCentered(&sContext, g_pcBuf, -1, GrContextDpyWidthGet(&sContext) / 2, Row(1) + 4, true); // // Print messages on the screen about hibernation. // GrStringDrawCentered(&sContext, "Select to Hib", -1, GrContextDpyWidthGet(&sContext) / 2, Row(4) + 4, true); GrStringDrawCentered(&sContext, "Wake in 5 s,", -1, GrContextDpyWidthGet(&sContext) / 2, Row(5) + 4, true); GrStringDrawCentered(&sContext, "or press Select", -1, GrContextDpyWidthGet(&sContext) / 2, Row(6) + 4, true); GrStringDrawCentered(&sContext, "for immed. wake.", -1, GrContextDpyWidthGet(&sContext) / 2, Row(7) + 4, true); // // Clear the button pressed flag, in case it was held down at the // beginning. // bSelectPressed = 0; // // Wait for user to press the button. // while(!bSelectPressed) { // // Wait a bit before looping again. // SysTickWait(10); } // // Tell user to release the button. // GrStringDrawCentered(&sContext, " ", -1, GrContextDpyWidthGet(&sContext) / 2, Row(4) + 4, true); GrStringDrawCentered(&sContext, " ", -1, GrContextDpyWidthGet(&sContext) / 2, Row(5) + 4, true); GrStringDrawCentered(&sContext, " ", -1, GrContextDpyWidthGet(&sContext) / 2, Row(6) + 4, true); GrStringDrawCentered(&sContext, " ", -1, GrContextDpyWidthGet(&sContext) / 2, Row(7) + 4, true); GrStringDrawCentered(&sContext, "Release the", -1, GrContextDpyWidthGet(&sContext) / 2, Row(5) + 4, true); GrStringDrawCentered(&sContext, "button.", -1, GrContextDpyWidthGet(&sContext) / 2, Row(6) + 4, true); GrStringDrawCentered(&sContext, " ", -1, GrContextDpyWidthGet(&sContext) / 2, Row(7) + 4, true); // // Wait for user to release the button. // while(bSelectPressed) { } // // If hibernation count is very large, it may be that there was already // a value in the hibernate memory, so reset the count. // ui32HibernateCount = (ui32HibernateCount > 10000) ? 0 : ui32HibernateCount; // // Increment the hibernation count, and store it in the battery backed // memory. // ui32HibernateCount++; HibernateDataSet(&ui32HibernateCount, 1); // // Clear and enable the RTC and set the match registers to 5 seconds in the // future. Set both to same, though they could be set differently, the // first to match will cause a wake. // HibernateRTCSet(0); HibernateRTCEnable(); HibernateRTCMatchSet(0, 5); // // Set wake condition on pin or RTC match. Board will wake when 5 seconds // elapses, or when the button is pressed. // HibernateWakeSet(HIBERNATE_WAKE_PIN | HIBERNATE_WAKE_RTC); // // Request hibernation. // HibernateRequest(); // // Give it time to activate, it should never get past this wait. // SysTickWait(100); // // Should not have got here, something is wrong. Print an error message to // the user. // sRect.i16XMin = 0; sRect.i16XMax = 95; sRect.i16YMin = 0; sRect.i16YMax = 63; GrContextForegroundSet(&sContext, ClrBlack); GrRectFill(&sContext, &sRect); GrContextForegroundSet(&sContext, ClrWhite); ui32Idx = 0; while(g_pcErrorText[ui32Idx]) { GrStringDraw(&sContext, g_pcErrorText[ui32Idx], -1, Col(0), Row(ui32Idx), true); ui32Idx++; } // // Wait for the user to press the button, then restart the app. // bSelectPressed = 0; while(!bSelectPressed) { } // // Reset the processor. // ROM_SysCtlReset(); // // Finished. // while(1) { } }
int main(void) { char stringbuffer[17]; int distance = 0; // Enable lazy stacking for interrupt handlers. This allows floating-point // instructions to be used within interrupt handlers, but at the expense of // extra stack usage. ROM_FPUEnable(); ROM_FPULazyStackingEnable(); ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3); ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_EEPROM0); //This wasn't clear at all. Note to self, everything needs enabling on this chip. ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_HIBERNATE); ROM_GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, WAKEPIN); ROM_GPIOPinTypeGPIOInput(GPIO_PORTA_BASE, REEDPIN); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V5POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, V3POWER); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, SERVO); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1); // ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2); ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0xFF); #ifdef EASYOPEN ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); #endif GPIOPinConfigure(GPIO_PA0_U0RX); GPIOPinConfigure(GPIO_PA1_U0TX); ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1); GPIOPinConfigure(GPIO_PC6_U3RX); GPIOPinConfigure(GPIO_PC7_U3TX); ROM_GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7); ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_UARTConfigSetExpClk(UART3_BASE, ROM_SysCtlClockGet(), GPSBAUD, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); ROM_IntEnable(INT_UART0); ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT); ROM_IntEnable(INT_UART3); ROM_UARTIntEnable(UART3_BASE, UART_INT_RX | UART_INT_RT); SysTickPeriodSet(SysCtlClockGet()/10000); SysTickIntRegister(&ServoDriver); SysTickIntEnable(); SysTickEnable(); ROM_IntMasterEnable(); GPIOIntTypeSet(GPIO_PORTA_BASE, REEDPIN, GPIO_FALLING_EDGE); GPIOPinIntClear(GPIO_PORTA_BASE, REEDPIN); GPIOPinIntEnable(GPIO_PORTA_BASE, REEDPIN); IntEnable(INT_GPIOA); // while(1){} /* SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0xFF); //Turn on the 5V power to LCD + servo. SysCtlDelay(SysCtlClockGet()/1000);//Make sure the servo is going to get a pulse soon.*/ EEPROMInit(); initLCD(); LCDCommand(0x0c); #ifdef LOOPBACKUART while(1){} #endif #ifdef FIRSTRUN //First run, sets the eeprom to have as many tries as is desired. EEPROMMassErase(); EEPROMProgram(&initialNumTries,eepromAddress,sizeof(initialNumTries)); LCDWriteText("Setup Complete. ", 0, 0); LCDWriteText("Reflash Firmware", 1, 0); while (1){} //Don't want to do anything else now. #endif EEPROMRead(&numTrieslong,eepromAddress,sizeof(numTrieslong)); // numTries=(int)numTrieslong; // openLock(); // numTrieslong=0; if (numTrieslong > initialNumTries-3) //Has already opened once, so just open as needed if stuck. { openLock(); numTrieslong--; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. } else { distance = getDistance(); if(distance==99999){ //No fix :/ LCDWriteText("Location unknown", 0, 0); LCDWriteText("Take me outside ", 1, 0); SysCtlDelay(SysCtlClockGet()); //Waits 3 seconds. } else if (distance>NEARENOUGH) //Valid fix, too far away. { if ((int)numTrieslong>0) //Any attempts remaining? { usnprintf(stringbuffer,17,"Distance: %4dm ",distance); LCDWriteText(stringbuffer, 0, 0); numTrieslong--; // numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(numTrieslong)); //Decrement EEPROM counter. usnprintf(stringbuffer,17,"%2d Attempts left",(int)numTrieslong); LCDWriteText(stringbuffer, 1, 0); SysCtlDelay(SysCtlClockGet()*2); } else { LCDWriteText("Oh dear... ", 0, 0); //Not really sure what to do, hopefully this code never runs. LCDWriteText("Opening anyway. ", 1, 0); // numTrieslong=initialNumTries+1; // EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Set to big value SysCtlDelay(10*SysCtlClockGet()/3); openLock(); } } else //Found the location! { openLock(); numTrieslong=initialNumTries+1; //numTries=(int)numTrieslong; EEPROMProgram(&numTrieslong,eepromAddress,sizeof(initialNumTries)); //Lock will now open straight away. } } // BLINK(RED); HibernateEnableExpClk(SysCtlClockGet()); HibernateGPIORetentionEnable(); //Enables GPIO retention after wake from hibernate. HibernateClockSelect(HIBERNATE_CLOCK_SEL_RAW); HibernateWakeSet(HIBERNATE_WAKE_PIN); HibernateIntRegister(&HibernateInterrupt); HibernateIntEnable(HIBERNATE_INT_PIN_WAKE); //BLINK(BLUE); ROM_GPIOPinWrite(GPIO_PORTE_BASE, V5POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTE_BASE, V3POWER, 0); //GPIO pins keep state on hibernate, so make sure to power everything else down. ROM_GPIOPinWrite(GPIO_PORTB_BASE, RS|E|D4|D5|D6|D7, 0xFF); //Pull all data pins to LCD high so we're not phantom powering it through ESD diodes. ROM_GPIOPinWrite(GPIO_PORTF_BASE, SERVO, 0xFF); //Likewise for the servo SysCtlDelay(SysCtlClockGet()/6); HibernateRequest();// we want to be looping'n'shit. while(1){} //Lalala, I'm a sleeping right now. }