//***************************************************************************** // // This function is called to start an acquisition running. It determines // which channels are to be logged, enables the ADC sequencers, and computes // the first RTC match value. This will start the acquisition running. // //***************************************************************************** void AcquireStart(tConfigState *psConfig) { uint32_t ui32Idx, pui32RTC[2], ui32SelectedMask; // // Check the parameters // ASSERT(psConfig); if(!psConfig) { return; } // // Update the config state pointer, save the selected item mask // g_psConfigState = psConfig; ui32SelectedMask = psConfig->ui16SelectedMask; // // Get the logging period from the logger configuration. Split the // period into seconds and subseconds pieces and save for later use in // generating RTC match values. // g_pui32MatchPeriod[0] = psConfig->ui32Period >> 8; g_pui32MatchPeriod[1] = (psConfig->ui32Period & 0xFF) << 8; // // Determine how many channels are to be logged // ui32Idx = ui32SelectedMask; g_ui32NumItems = 0; while(ui32Idx) { if(ui32Idx & 1) { g_ui32NumItems++; } ui32Idx >>= 1; } // // Initialize the strip chart manager for a new run. Don't bother with // the strip chart if we are using viewer mode, or sleep-logging. // if((psConfig->ui8Storage != CONFIG_STORAGE_VIEWER) && !psConfig->ui32SleepLogging) { StripChartMgrInit(); StripChartMgrConfigure(ui32SelectedMask); } // // Configure USB for memory stick if USB storage is chosen // if(psConfig->ui8Storage == CONFIG_STORAGE_USB) { USBStickOpenLogFile(0); } else if(psConfig->ui8Storage == CONFIG_STORAGE_FLASH) { // // Flash storage is to be used, prepare the flash storage module. // If already sleep-logging, then pass in the saved flash address // so it does not need to be searched. // if(psConfig->ui32SleepLogging) { FlashStoreOpenLogFile(psConfig->ui32FlashStore); } else { // // Otherwise not sleep logging, so just initialize the flash store, // this will cause it to search for the starting storage address. // FlashStoreOpenLogFile(0); } } // // Enable the ADC sequencers // MAP_ADCSequenceEnable(ADC0_BASE, 0); MAP_ADCSequenceEnable(ADC1_BASE, 0); // // Flush the ADC sequencers to be sure there is no lingering data. // MAP_ADCSequenceDataGet(ADC0_BASE, 0, g_pui32ADCData); MAP_ADCSequenceDataGet(ADC1_BASE, 0, g_pui32ADCData); // // Enable ADC interrupts // MAP_ADCIntClear(ADC0_BASE, 0); MAP_ADCIntClear(ADC1_BASE, 0); MAP_ADCIntEnable(ADC0_BASE, 0); MAP_IntEnable(INT_ADC0SS0); // // If we are not already sleep-logging, then initialize the RTC match. // If we are sleep logging then this does not need to be set up. // if(!psConfig->ui32SleepLogging) { // // Get the current RTC value // do { pui32RTC[0] = HibernateRTCGet(); pui32RTC[1] = HibernateRTCSSGet(); } while(pui32RTC[0] != HibernateRTCGet()); // // Set an initial next match value. Start with the subseconds always // 0 so the first match value will always be an even multiple of the // subsecond match. Add 2 seconds to the current RTC just to be clear // of an imminent rollover. This means that the first match will occur // between 1 and 2 seconds from now. // g_pui32NextMatch[0] = pui32RTC[0] + 2; g_pui32NextMatch[1] = 0; // // Now set the match value // HibernateRTCMatchSet(0, g_pui32NextMatch[0]); HibernateRTCSSMatchSet(0, g_pui32NextMatch[1]); } // // If we are configured to sleep, but not sleeping yet, then enter sleep // logging mode if allowed. // if(psConfig->bSleep && !psConfig->ui32SleepLogging) { // // Allow sleep logging if storing to flash at a period of 1 second // or greater. // if((psConfig->ui8Storage == CONFIG_STORAGE_FLASH) && (psConfig->ui32Period >= 0x100)) { psConfig->ui32SleepLogging = 1; } } // // Enable the RTC interrupts from the hibernate module // HibernateIntClear(HibernateIntStatus(0)); HibernateIntEnable(HIBERNATE_INT_RTC_MATCH_0 | HIBERNATE_INT_PIN_WAKE); MAP_IntEnable(INT_HIBERNATE); // // Logging data should now start running // }
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. }