|
| 1 | +/* |
| 2 | + Power down all parts of the board |
| 3 | + Wake after three seconds |
| 4 | + Write to a file |
| 5 | + Power back down |
| 6 | +*/ |
| 7 | + |
| 8 | +//#include <Wire.h> |
| 9 | + |
| 10 | +//microSD Interface |
| 11 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 12 | +#include <SPI.h> |
| 13 | +#include <SdFat.h> //We use SdFat-Beta from Bill Greiman for increased read/write speed |
| 14 | + |
| 15 | +const byte PIN_MICROSD_CHIP_SELECT = 10; |
| 16 | +const byte PIN_MICROSD_POWER = 15; //x04 |
| 17 | + |
| 18 | +#define SD_CONFIG SdSpiConfig(PIN_MICROSD_CHIP_SELECT, SHARED_SPI, SD_SCK_MHZ(24)) //Max of 24MHz |
| 19 | +#define SD_CONFIG_MAX_SPEED SdSpiConfig(PIN_MICROSD_CHIP_SELECT, DEDICATED_SPI, SD_SCK_MHZ(24)) //Max of 24MHz |
| 20 | + |
| 21 | +SdFat sd; |
| 22 | +File sensorDataFile; //File that all sensor data is written to |
| 23 | +File serialDataFile; //File that all incoming serial data is written to |
| 24 | + |
| 25 | +char sensorDataFileName[30] = ""; //We keep a record of this file name so that we can re-open it upon wakeup from sleep |
| 26 | +char serialDataFileName[30] = ""; //We keep a record of this file name so that we can re-open it upon wakeup from sleep |
| 27 | +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 28 | + |
| 29 | +uint32_t msToSleep = 3000; //This is the user editable number of ms to sleep between RTC checks |
| 30 | +#define TIMER_FREQ 32768L //Counter/Timer 6 will use the 32kHz clock |
| 31 | +uint32_t sysTicksToSleep = msToSleep * TIMER_FREQ / 1000; |
| 32 | + |
| 33 | +const byte PIN_QWIIC_POWER = 18; |
| 34 | +const byte PIN_STAT_LED = 19; |
| 35 | +const byte PIN_IMU_POWER = 22; |
| 36 | + |
| 37 | +void setup(void) { |
| 38 | + Serial.begin(115200); |
| 39 | + Serial.println(); |
| 40 | + Serial.println("SD Write Test"); |
| 41 | + |
| 42 | + qwiicPowerOff(); |
| 43 | + microSDPowerOff(); |
| 44 | + imuPowerOff(); |
| 45 | + |
| 46 | + SPI.begin(); |
| 47 | + |
| 48 | + beginSD(); |
| 49 | + |
| 50 | + beginDataLogging(); |
| 51 | + |
| 52 | + Serial.println("Setup done"); |
| 53 | +} |
| 54 | + |
| 55 | +void loop(void) |
| 56 | +{ |
| 57 | + String outputData = "Test string\n"; |
| 58 | + |
| 59 | + char temp[512]; |
| 60 | + outputData.toCharArray(temp, 512); //Convert string to char array so sdfat can record it |
| 61 | + sensorDataFile.write(temp, strlen(temp)); //Record the buffer to the card |
| 62 | + sensorDataFile.sync(); |
| 63 | + |
| 64 | + Serial.println("File recorded"); |
| 65 | + |
| 66 | + goToSleep(); |
| 67 | +} |
| 68 | + |
| 69 | +//Power everything down and wait for RTC interrupt wakeup |
| 70 | +void goToSleep() |
| 71 | +{ |
| 72 | + //digitalWrite(LOGIC_DEBUG, HIGH); |
| 73 | + |
| 74 | + Serial.flush(); |
| 75 | + Serial.end(); //Power down UART |
| 76 | + |
| 77 | + power_adc_disable(); //Power down ADC |
| 78 | + |
| 79 | + for (int x = 0 ; x < 50 ; x++) |
| 80 | + am_hal_gpio_pinconfig(x , g_AM_HAL_GPIO_DISABLE); |
| 81 | + |
| 82 | + qwiicPowerOff(); |
| 83 | + microSDPowerOff(); |
| 84 | + imuPowerOff(); |
| 85 | + |
| 86 | + //We use counter/timer 6 to cause us to wake up from sleep but 0 to 7 are available |
| 87 | + //CT 7 is used for Software Serial. All CTs are used for Servo. |
| 88 | + am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG); //Clear CT6 |
| 89 | + am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREG); //Enable C/T G=6 |
| 90 | + |
| 91 | + //Use the lower power 32kHz clock |
| 92 | + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); |
| 93 | + am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ | AM_HAL_STIMER_CFG_COMPARE_G_ENABLE); |
| 94 | + |
| 95 | + //Right here we need to reduce the sleep sys ticks by the amount we've been awake so that we hit exactly 0.12Hz |
| 96 | + |
| 97 | + //Setup interrupt to trigger when the number of ms have elapsed |
| 98 | + am_hal_stimer_compare_delta_set(6, sysTicksToSleep); |
| 99 | + |
| 100 | + // Turn OFF Flash1 |
| 101 | + am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEM_FLASH_512K); |
| 102 | + |
| 103 | + // Power down SRAM |
| 104 | + // Nathan seems to have gone a little off script here and isn't using |
| 105 | + // am_hal_pwrctrl_memory_deepsleep_powerdown or |
| 106 | + // am_hal_pwrctrl_memory_deepsleep_retain. I wonder why? |
| 107 | + //PWRCTRL->MEMPWDINSLEEP_b.SRAMPWDSLP = PWRCTRL_MEMPWDINSLEEP_SRAMPWDSLP_ALLBUTLOWER32K; |
| 108 | + PWRCTRL->MEMPWDINSLEEP_b.SRAMPWDSLP = PWRCTRL_MEMPWDINSLEEP_SRAMPWDSLP_ALLBUTLOWER64K; |
| 109 | + |
| 110 | + //Enable the timer interrupt in the NVIC. |
| 111 | + NVIC_EnableIRQ(STIMER_CMPR6_IRQn); |
| 112 | + |
| 113 | + // Go to Deep Sleep. |
| 114 | + am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP); |
| 115 | + |
| 116 | + //Turn off interrupt |
| 117 | + NVIC_DisableIRQ(STIMER_CMPR6_IRQn); |
| 118 | + |
| 119 | + //We're BACK! |
| 120 | + wakeFromSleep(); |
| 121 | +} |
| 122 | + |
| 123 | +//Power everything up gracefully |
| 124 | +void wakeFromSleep() |
| 125 | +{ |
| 126 | + // Set the clock frequency. (redundant?) |
| 127 | + am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX, 0); |
| 128 | + |
| 129 | + // Set the default cache configuration. (redundant?) |
| 130 | + am_hal_cachectrl_config(&am_hal_cachectrl_defaults); |
| 131 | + am_hal_cachectrl_enable(); |
| 132 | + |
| 133 | + // Note: because we called setupRTC earlier, |
| 134 | + // we do NOT want to call am_bsp_low_power_init() here. |
| 135 | + // It would configure the board for low power operation |
| 136 | + // and calls am_hal_pwrctrl_low_power_init() |
| 137 | + // but it also stops the RTC oscillator! |
| 138 | + // (BSP = Board Support Package) |
| 139 | + |
| 140 | + // Initialize for low power in the power control block. (redundant?) |
| 141 | + am_hal_pwrctrl_low_power_init(); |
| 142 | + |
| 143 | + // Power up SRAM |
| 144 | + PWRCTRL->MEMPWDINSLEEP_b.SRAMPWDSLP = PWRCTRL_MEMPWDINSLEEP_SRAMPWDSLP_NONE; |
| 145 | + |
| 146 | + // Turn on Flash |
| 147 | + // There is a chance this could fail but I guess we should move on regardless and not do a while(1); |
| 148 | + am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEM_ALL); |
| 149 | + |
| 150 | + // Go back to using the main clock |
| 151 | + am_hal_stimer_int_enable(AM_HAL_STIMER_INT_OVERFLOW); // (posssibly redundant?) |
| 152 | + NVIC_EnableIRQ(STIMER_IRQn); // (posssibly redundant?) |
| 153 | + am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE); |
| 154 | + am_hal_stimer_config(AM_HAL_STIMER_HFRC_3MHZ); |
| 155 | + |
| 156 | + // Restore the TX/RX connections between the Artemis module and the CH340S |
| 157 | + am_hal_gpio_pinconfig(48 /* TXO-0 */, g_AM_BSP_GPIO_COM_UART_TX); |
| 158 | + am_hal_gpio_pinconfig(49 /* RXI-0 */, g_AM_BSP_GPIO_COM_UART_RX); |
| 159 | + |
| 160 | + // Reenable the debugger GPIOs |
| 161 | + am_hal_gpio_pinconfig(20 /* SWDCLK */, g_AM_BSP_GPIO_SWDCK); |
| 162 | + am_hal_gpio_pinconfig(21 /* SWDIO */, g_AM_BSP_GPIO_SWDIO); |
| 163 | + |
| 164 | + // Turn on ADC |
| 165 | + ap3_adc_setup(); |
| 166 | + |
| 167 | + // Start the console serial port again (zzz will have ended it) |
| 168 | + Serial.begin(115200); |
| 169 | + Serial.println("Back on"); |
| 170 | + |
| 171 | +} |
| 172 | + |
| 173 | +//Called once number of milliseconds has passed |
| 174 | +extern "C" void am_stimer_cmpr6_isr(void) |
| 175 | +{ |
| 176 | + uint32_t ui32Status = am_hal_stimer_int_status_get(false); |
| 177 | + if (ui32Status & AM_HAL_STIMER_INT_COMPAREG) |
| 178 | + { |
| 179 | + am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREG); |
| 180 | + } |
| 181 | +} |
0 commit comments