/******************************************************************************/ /* LOBO Initialization Routines */ /******************************************************************************/ //#INIT void InitLOBO(void) { // SimSetFlashWaits(0); // SimSetRAMWaits(0); SimSetFSys( CLOCK_FREQ ); SetTickRate( TICKS_PER_SECOND ); SerSetBaud( CONSOLE_BAUD, 0L ); InitCF8(CF8StdCS, CF8StdAddr); // enable the CF8 file system if (errno != OK ) // look out for No Hardware or No Media { printf("\n\n Failure initializing Compact Flash (InitCF8), error %d\n", errno); switch( errno ) { case PiDosNoHardware: printf("Persistor CFM reader not found\n"); break; case PiDosNoMedia: printf("WARNING! No CFM card in the Persistor reader!\n"); break; default: Reset(); } if( !QueryYesNo("Continue anyway?", FALSE) ) Reset(); printf("\nWarning -- Won't log data to disk!!\n"); } execstr("OPT ON"); //enable PicoDOS optimaztion InitLatch_IO(); // Init Memory Mapped Latches and their Chip Selects, CLEARS modem_pwr, SETS watchdog_event_flag watchdog_flag = CheckWatchDog(); //MUST be called early on, but after InitLatch_IO() InitUSB_IO(); // Init Memory Mapped USB Module and its Chip Select InitPortE_IO(); // Init CPU PortE, Radio MODEM bits in lower nibble, Expansion Bits in upper nibble InitPortQS_IO(); // Init CPU PortQS, SHT75 Temperature/Humidity and Dallas One-Wire Touchmem Devices // Initialize TPU channels for serial i/o if ( tserInit() != OK ){ printf("\n Error in initializing TPU serial ports\n"); } RtcDevice = FALSE; RamDevice = FALSE; TempDevices = FALSE; TMemDevices = TouchRomSearch(); InitVars(); if(RtcDevice != FALSE){ DS1994_pcc = (ulong)RdDS1994Long((ushort)PCCVAL_PTR, DS1994_ROM); DS1994_elp = (ulong)RdDS1994Long((ushort)ELPTVAL_PTR, DS1994_ROM); DS1994_secs = (time_t)RdDS1994Long((ushort)TIMEVAL_PTR, DS1994_ROM); } else{ DS1994_pcc = 0L; DS1994_elp = 0L; DS1994_secs = 946684800L; //default to Y2K just to know what happened! } SetTimeSecs(DS1994_secs, NULL); sys_reset_time = DS1994_secs; if(watchdog_enable == TRUE){ // Initialize TPU channel for 400Hz watchdog keep alive InitWatchDogKeepAlive(); EnableWatchDog(); } else{ TPUSetPin(12, 0); //set LOBO Watchdog KeepAlive pin line low TPUSetPin(15, 0); //set T2CLK Input and F6 low. } reset_flag = TRUE; //gets cleared in datalogging section... if( RtcDevice != FALSE ){ ++sys_reset_ctr; WrDS1994Long((ushort)SYSRESETCTR_PTR , sys_reset_ctr, DS1994_ROM); WrDS1994Long((ushort)SYSRESETTIME_PTR , sys_reset_time, DS1994_ROM); } if( (watchdog_flag == TRUE) && (RtcDevice != FALSE) ){ ++watchdog_ctr; WrDS1994Long((ushort)WATCHDOGCTR_PTR , watchdog_ctr, DS1994_ROM); } send_again = FALSE; //used in telemetry DisplayInitMsg(); InitModem(); } void InitVars(void) { int i = 0; if(RtcDevice != FALSE){ if( (RdDS1994Byte((ushort)CNTLREG_PTR, DS1994_ROM)) != 0x50 ) WrDS1994Byte((ushort)CNTLREG_PTR, (uchar)0x50, DS1994_ROM); //set control reg if( (RdDS1994Byte((ushort)STATREG_PTR, DS1994_ROM) & 0x38) != 0x38 ) WrDS1994Byte((ushort)STATREG_PTR, (uchar)0x38, DS1994_ROM); //set status reg if( (RdDS1994Long(0x0211, DS1994_ROM)) != 0xFFFFFFF0 ) WrDS1994Long(0x0211, 0xFFFFFFF0, DS1994_ROM); //set alarm regs if( (RdDS1994Long(0x0216, DS1994_ROM)) != 0xFFFFFFF0 ) WrDS1994Long(0x0216, 0xFFFFFFF0, DS1994_ROM); if( (RdDS1994Long(0x021A, DS1994_ROM)) != 0xFFFFFFF0 ) WrDS1994Long(0x021A, 0xFFFFFFF0, DS1994_ROM); while( (RdDS1994Byte((ushort)STATREG_PTR, DS1994_ROM) & 0x07) != 0){ RdDS1994Byte((ushort)STATREG_PTR, DS1994_ROM); //clear status reg alarm flags ++i; if(i > 10) break; } } if(RtcDevice != FALSE){ if( (RdDS1994Byte(0x0000, DS1994_ROM) != 0x55) ) InitVarsNV(); //write default values into RTC SRAM } LoadVarsNV(RtcDevice); SleepOpts.sleepAt3V = TRUE; // reduce power by reducing voltage SleepOpts.sleepADshdn = TRUE; // force the AD into shutdown SleepOpts.sleepRSshdn = TRUE; // force the RS232 driver to shutdown SleepOpts.sleepZeroEvenSec = TRUE; // sleep zero waits for next second SleepOpts.sleepSpuriousRupt = TRUE; // break on (catch) spurious interrupts SleepOpts.sleepHandleIRQ1 = TRUE; // break on IRQ1 (RS232-1) interrupt SleepOpts.sleepHandleIRQ2 = TRUE; // break on IRQ2 (RS232-2/ExtWakup) interrupt SleepOpts.sleepHandleIRQ4 = FALSE; // break on IRQ4 PwrSupply interrupt SleepOpts.sleepHandleIRQ6 = FALSE; // break on IRQ6/TP15 interrupt } void LoadVarsNV(uchar NVSRAM) { if(NVSRAM != FALSE){ //load the UCHAR vals from the DS1994 SRAM pages 0-3 //page 0 FOO_enable = (uchar)RdDS1994Byte((ushort)fooENABLE_PTR, DS1994_ROM); slog_mode = (uchar)RdDS1994Byte((ushort)SLOGMODE_PTR, DS1994_ROM); deploy_mode = (uchar)RdDS1994Byte((ushort)DEPLMODE_PTR, DS1994_ROM); data_mode = (uchar)RdDS1994Byte((ushort)DATAMODE_PTR, DS1994_ROM); LED_enable = (uchar)RdDS1994Byte((ushort)LEDENABLE_PTR, DS1994_ROM); watchdog_enable = (uchar)RdDS1994Byte((ushort)WDOGENABLE_PTR, DS1994_ROM); sample_enable = (uchar)RdDS1994Byte((ushort)SAMPENABLE_PTR, DS1994_ROM); //load the USHORT vals from the DS1994 SRAM pages 4-6 //page 4 //init the USHORT vals from predefined values lf_limit = (ushort)RdDS1994Short((ushort)LFLIMIT_PTR, DS1994_ROM); sf_limit = (ushort)RdDS1994Short((ushort)SFLIMIT_PTR, DS1994_ROM); sync_hour = (ushort)RdDS1994Short((ushort)SYNCHR_PTR, DS1994_ROM); sync_min = (ushort)RdDS1994Short((ushort)SYNCMIN_PTR, DS1994_ROM); last_doy = (ushort)RdDS1994Short((ushort)LASTDOY_PTR, DS1994_ROM); //load the INT vals from the DS1994 SRAM pages 7 //page 7 tzoffset = (int)RdDS1994Short( (ushort)TZOFFSET_PTR, DS1994_ROM); //load the ULONG AND LONG vals from the DS1994 SRAM pages 8-11 //page 8 lobo_sleep = (ulong)RdDS1994Long((ushort)LOBOSLP_PTR, DS1994_ROM); samps_per_xmit = (ulong)RdDS1994Long((ushort)SAMPSPERTX_PTR, DS1994_ROM); file_ctr = (ulong)RdDS1994Long((ushort)FILECTR_PTR, DS1994_ROM); modem_baud = RdDS1994Long((ushort)MODEMBAUD_PTR, DS1994_ROM); sys_reset_ctr = (ulong)RdDS1994Long((ushort)SYSRESETCTR_PTR, DS1994_ROM); sys_reset_time = (ulong)RdDS1994Long((ushort)SYSRESETTIME_PTR, DS1994_ROM); watchdog_ctr = (ulong)RdDS1994Long((ushort)WATCHDOGCTR_PTR, DS1994_ROM); connect_fail_ctr = (ulong)RdDS1994Long((ushort)CONNFAILCTR_PTR, DS1994_ROM); connect_fail_time = (ulong)RdDS1994Long((ushort)CONNFAILTIME_PTR, DS1994_ROM); carrier_drop_ctr = (ulong)RdDS1994Long((ushort)CARRDROPCTR_PTR, DS1994_ROM); carrier_drop_time = (ulong)RdDS1994Long((ushort)CARRDROPTIME_PTR, DS1994_ROM); samp_ctr = (ulong)RdDS1994Long((ushort)SAMPCTR_PTR, DS1994_ROM); } else{ //init the UCHAR vals from DEFINED values FOO_enable = (uchar)fooENABLE_VAL; slog_mode = (uchar)SLOGMODE_VAL; deploy_mode = (uchar)DEPLMODE_VAL; data_mode = (uchar)DATAMODE_VAL; LED_enable = (uchar)LEDENABLE_VAL; watchdog_enable = (uchar)WDOGENABLE_VAL; sample_enable = (uchar)SAMPENABLE_VAL; //init the USHORT vals from DEFINED values lf_limit = (ushort)LFLIMIT_VAL; sf_limit = (ushort)SFLIMIT_VAL; sync_hour = (ushort)SYNCHR_VAL; sync_min = (ushort)SYNCMIN_VAL; last_doy = 0; //init the INT vals from DEFINED values tzoffset = (int)TZOFFSET_VAL; //init the ULONG and LONG vals from DEFINED values lobo_sleep = (ulong)LOBOSLP_VAL; samps_per_xmit = (ulong)SAMPSPERTX_VAL; file_ctr = (ulong)FILECTR_VAL; //REF LogFileCount() modem_baud = (long)MODEMBAUD_VAL; sys_reset_ctr = (ulong)SYSRESETCTR_VAL; sys_reset_time = (ulong)SYSRESETTIME_VAL; watchdog_ctr = (ulong)WATCHDOGCTR_VAL; connect_fail_ctr = (ulong)CONNFAILCTR_VAL; connect_fail_time = (ulong)CONNFAILTIME_VAL; carrier_drop_ctr = (ulong)CARRDROPCTR_VAL; carrier_drop_time = (ulong)CARRDROPTIME_VAL; samp_ctr = (ulong)SAMPCTR_VAL; //init the LONG vals from DEFINED values } } void InitVarsNV(void) { //init the UCHAR vals into the DS1994 SRAM pages 0-3 //page 0 WrDS1994Byte(0x0000, (uchar)0x55, DS1994_ROM); WrDS1994Byte((ushort)fooENABLE_PTR, (uchar)fooENABLE_VAL, DS1994_ROM); WrDS1994Byte((ushort)SLOGMODE_PTR, (uchar)SLOGMODE_VAL, DS1994_ROM); WrDS1994Byte((ushort)DEPLMODE_PTR, (uchar)DEPLMODE_VAL, DS1994_ROM); WrDS1994Byte((ushort)DATAMODE_PTR, (uchar)DATAMODE_VAL, DS1994_ROM); WrDS1994Byte((ushort)LEDENABLE_PTR, (uchar)LEDENABLE_VAL, DS1994_ROM); WrDS1994Byte((ushort)WDOGENABLE_PTR, (uchar)WDOGENABLE_VAL, DS1994_ROM); WrDS1994Byte((ushort)SAMPENABLE_PTR, (uchar)SAMPENABLE_VAL, DS1994_ROM); //init the USHORT vals into the DS1994 SRAM pages 4-6 //page 4 WrDS1994Short((ushort)LFLIMIT_PTR, (ushort)LFLIMIT_VAL, DS1994_ROM); WrDS1994Short((ushort)SFLIMIT_PTR, (ushort)SFLIMIT_VAL, DS1994_ROM); WrDS1994Short((ushort)SYNCHR_PTR, (ushort)SYNCHR_VAL, DS1994_ROM); WrDS1994Short((ushort)SYNCMIN_PTR, (ushort)SYNCMIN_VAL, DS1994_ROM); //init the INT vals into the DS1994 SRAM pages 7 //page 7 WrDS1994Short((ushort)TZOFFSET_PTR, (int)TZOFFSET_VAL, DS1994_ROM); //init the ULONG AND LONG vals into the DS1994 SRAM pages 8-11 //page 8 WrDS1994Long((ushort)LOBOSLP_PTR, (ulong)LOBOSLP_VAL, DS1994_ROM); WrDS1994Long((ushort)SAMPSPERTX_PTR, (ulong)SAMPSPERTX_VAL, DS1994_ROM); WrDS1994Long((ushort)FILECTR_PTR, (ulong)FILECTR_VAL, DS1994_ROM); WrDS1994Long((ushort)MODEMBAUD_PTR, (long)MODEMBAUD_VAL, DS1994_ROM); WrDS1994Long((ushort)SYSRESETCTR_PTR, (ulong)SYSRESETCTR_VAL, DS1994_ROM); WrDS1994Long((ushort)SYSRESETTIME_PTR, (ulong)SYSRESETTIME_VAL, DS1994_ROM); WrDS1994Long((ushort)WATCHDOGCTR_PTR, (ulong)WATCHDOGCTR_VAL, DS1994_ROM); WrDS1994Long((ushort)CONNFAILCTR_PTR, (ulong)CONNFAILCTR_VAL, DS1994_ROM); WrDS1994Long((ushort)CONNFAILTIME_PTR, (ulong)CONNFAILTIME_VAL, DS1994_ROM); WrDS1994Long((ushort)CARRDROPCTR_PTR, (ulong)CARRDROPCTR_VAL, DS1994_ROM); WrDS1994Long((ushort)CARRDROPTIME_PTR, (ulong)CARRDROPTIME_VAL, DS1994_ROM); WrDS1994Long((ushort)SAMPCTR_PTR, (ulong)SAMPCTR_VAL, DS1994_ROM); //page 16 WrDS1994Long((ushort)TIMEVAL_PTR, 946684800L, DS1994_ROM); printf("\n Vars were initialized into RTC SRAM!\n"); } void DisplayInitMsg(void) { ushort relays; float battv, battsd; printf("\n\n LOBO Build: %s Date: %s %s\n", strupr(&bfile[0]), bdate, btime); if( UsbActive() == TRUE ) printfUSB("\n\n LOBO Build: %s Date: %s %s\n", strupr(&bfile[0]), bdate, btime); printf("\n Persistor S/N - PicoDOS Ver: %s\n", picodosver()); pdcfinfo("A:", &size, &avail); printf(" CF Card: Size=%ld Free=%ld \n", size, avail); DisplayTimes(DS1994_ROM, tzoffset); print_sht75(1); RdBattVoltage(&battv, &battsd); printf("\n System Voltage = %.2f StdDev = %.2lf\n", battv, battsd); relays = Get_K1_K8_State(); DisplayByteVal(" Comm Relay State", relays, TTY_DEVICE); printf("\n Touchmem Devices = %d\n", TMemDevices); if(RamDevice != FALSE){ printf(" NVRAM Loaded\n"); } if(RtcDevice != FALSE){ printf(" System Power Cycles = %lu\n", DS1994_pcc); printf(" System Reset Cycles = %lu\n", sys_reset_ctr); printf(" Watchdog RST Cycles = %lu\n", watchdog_ctr); printf("\n Dropped Connections = %lu\n", carrier_drop_ctr); printf(" Last Drop Time = %s", ctime(&carrier_drop_time)); printf(" Connection Failures = %lu\n", connect_fail_ctr); printf(" Last Failure Time = %s", ctime(&connect_fail_time)); hdiv = ldiv(DS1994_elp, 3600L); mdiv = ldiv(hdiv.rem, 60L); printf(" Connection Odometer = %02ld:%02ld:%02ld = %lu secs\n", hdiv.quot, mdiv.quot, mdiv.rem, DS1994_elp); } } int ioPwrUp( void ) { if(watchdog_enable == TRUE){ // Initialize TPU channel for 400Hz watchdog keep alive InitWatchDogKeepAlive(); EnableWatchDog(); } Set_K1_K8_State( (RdLatch1() & 0xFF00) ); //open any closed relays return( OK ); } int ioPwrDown( void ) { Set_K1_K8_State( (RdLatch1() & 0xFF00) ); //open any closed relays Set_Bit_WrLatch2(WR_LATCH2_DEFAULTS); //disable RS232 receivers /* ** 68332 PORT F (I/O) */ // the following is used as an interrupt in LowPowerSleep() PConfInp(F,3); // IRQ3- (has pullup) // the following can be used as interrupts in LowPowerSleep(), see SleepOpts PConfInp(F,1); // IRQ1- via RXD line PConfInp(F,2); // IRQ2- via LOBO has pullup on LOBO board PConfInp(F,4); // IRQ4- via LOBO has pullup on LOBO board PConfOutp(F,7); PClear(F,7); // A-D shutdown PConfInp(F,6); // connected to T2CLK and TPU15, TPU15 asserts PConfInp(F,5); // PICIRQ (PIC asserts) PConfInp(F,0); // SEL5V (has pullup, PIC asserts) /* ** 68332 PORT E (BUS SIGNALS OR I/O) */ InitPortE_IO(); // set MODEM I2C pins and unused upper nibble /* ** 68332 PORT D (QSM OR I/O) */ InitPortQS_IO(); // set SHT75 Data and Clk and Dallas 1-Wire PConfBus(D,7); // TXD RS-232 PConfOutp(D,6); PSet(D,6); // pcs3, a-d cs active low PConfOutp(D,2); PClear(D,2); // sck PConfOutp(D,1); PClear(D,1); // mosi PInpToOut(D,0); // miso /* ** 68332 TPU PINS */ TPUSetPin(12, 0); //set LOBO Watchdog KeepAlive pin line low TPUSetPin(15, 0); //set T2CLK and F6 low. TPUSetPin(13, 1); // TXD2 TPUGetPin(14); // RXD2 (typ. active in shutdown) TPUSetPin(0, 1); // TXD3 TPUGetPin(1); // RXD3 (typ. inactive in shutdown, MAX3222 drives) TPUSetPin(2, 1); // TXD4 TPUGetPin(3); // RXD4 (typ. inactive in shutdown, MAX3222 drives) TPUSetPin(4, 1); // TXD5 TPUGetPin(5); // RXD5 (typ. inactive in shutdown, MAX3222 drives) TPUSetPin(6, 1); // TXD6 TPUGetPin(7); // RXD6 (typ. inactive in shutdown, MAX3222 drives) TPUSetPin(8, 1); // TXD7 TPUGetPin(9); // RXD7 (typ. inactive in shutdown, MAX3222 drives) TPUSetPin(10, 1); // TXD8 TPUGetPin(11); // RXD8 (typ. inactive in shutdown, MAX3222 drives) return( OK ); }