55#include " sequans_controller.h"
66
77#include < Arduino.h>
8- #include < avr/cpufunc .h>
8+ #include < Wire .h>
99#include < avr/io.h>
1010#include < avr/sleep.h>
1111
5656
5757#ifdef __AVR_AVR128DB48__ // MINI
5858
59- #define RING_PIN_bm PIN6_bm
59+ #define RING_PIN_bm PIN6_bm
60+ #define LOWQ_PIN PIN_PB4
61+ #define VOLTAGE_MEASURE_PIN PIN_PB3
62+
63+ #define DEBUGGER_TX_PIN PIN_PB0
64+ #define DEBUGGER_RX_PIN PIN_PB1
65+ #define DEBUGGER_LED_PIN PIN_PB2
66+ #define DEBUGGER_SW0_PIN PIN_PD2
67+ #define DEBUGGER_USART USART3
68+
69+ #define I2C0_SDA_PIN PIN_PC2
70+ #define I2C0_SCL_PIN PIN_PC3
71+ #define I2C1_SDA_PIN PIN_PF2
72+ #define I2C1_SCL_PIN PIN_PF3
6073
6174#else
6275
@@ -302,24 +315,92 @@ static void disablePIT(void) {
302315
303316static void powerDownPeripherals (void ) {
304317
318+ // LEDs
305319 cell_led_state = digitalRead (LedCtrl.getLedPin (Led::CELL));
306320 con_led_state = digitalRead (LedCtrl.getLedPin (Led::CON));
307321
308- LedCtrl.off (Led::CELL, true );
309- LedCtrl.off (Led::CON, true );
310- LedCtrl.off (Led::DATA, true );
311- LedCtrl.off (Led::ERROR, true );
312- LedCtrl.off (Led::USER, true );
322+ pinConfigure (LedCtrl.getLedPin (Led::CELL),
323+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
324+ pinConfigure (LedCtrl.getLedPin (Led::CON),
325+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
326+ pinConfigure (LedCtrl.getLedPin (Led::DATA),
327+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
328+ pinConfigure (LedCtrl.getLedPin (Led::ERROR),
329+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
330+ pinConfigure (LedCtrl.getLedPin (Led::USER),
331+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
332+
333+ // Make sure that I2C pins are pulled up and there won't be a voltage drop
334+ // over them
335+ pinConfigure (I2C0_SDA_PIN, PIN_DIR_INPUT | PIN_INPUT_DISABLE);
336+ pinConfigure (I2C0_SCL_PIN, PIN_DIR_INPUT | PIN_INPUT_DISABLE);
337+ pinConfigure (I2C1_SDA_PIN, PIN_DIR_INPUT | PIN_INPUT_DISABLE);
338+ pinConfigure (I2C1_SCL_PIN, PIN_DIR_INPUT | PIN_INPUT_DISABLE);
339+
340+ // Voltage measure
341+ digitalWrite (VOLTAGE_MEASURE_PIN, LOW);
342+
343+ // Debugger
344+ DEBUGGER_USART.CTRLB &= ~(USART_RXEN_bm | USART_TXEN_bm);
345+ pinConfigure (DEBUGGER_TX_PIN,
346+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
347+ pinConfigure (DEBUGGER_RX_PIN,
348+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
349+ pinConfigure (DEBUGGER_LED_PIN,
350+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
351+ pinConfigure (DEBUGGER_SW0_PIN,
352+ PIN_DIR_INPUT | PIN_PULLUP_ON | PIN_INPUT_DISABLE);
313353}
314354
315355static void powerUpPeripherals (void ) {
356+
357+ pinConfigure (LedCtrl.getLedPin (Led::CELL), PIN_DIR_OUTPUT);
358+ pinConfigure (LedCtrl.getLedPin (Led::CON), PIN_DIR_OUTPUT);
359+ pinConfigure (LedCtrl.getLedPin (Led::DATA), PIN_DIR_OUTPUT);
360+ pinConfigure (LedCtrl.getLedPin (Led::ERROR), PIN_DIR_OUTPUT);
361+ pinConfigure (LedCtrl.getLedPin (Led::USER), PIN_DIR_OUTPUT);
362+
316363 if (cell_led_state) {
317364 LedCtrl.on (Led::CELL, true );
318365 }
319366
320367 if (con_led_state) {
321368 LedCtrl.on (Led::CON, true );
322369 }
370+
371+ // Make sure that I2C pins are pulled up and there won't be a voltage drop
372+ // over them
373+ pinConfigure (I2C0_SDA_PIN, PIN_DIR_OUTPUT);
374+ pinConfigure (I2C0_SCL_PIN, PIN_DIR_OUTPUT);
375+ pinConfigure (I2C1_SDA_PIN, PIN_DIR_OUTPUT);
376+ pinConfigure (I2C1_SCL_PIN, PIN_DIR_OUTPUT);
377+
378+ // Voltage measure
379+ digitalWrite (VOLTAGE_MEASURE_PIN, HIGH);
380+
381+ // Debugger
382+ DEBUGGER_USART.CTRLB |= (USART_RXEN_bm | USART_TXEN_bm);
383+ pinConfigure (DEBUGGER_TX_PIN, PIN_DIR_OUTPUT);
384+ pinConfigure (DEBUGGER_RX_PIN, PIN_DIR_INPUT | PIN_INPUT_ENABLE);
385+ pinConfigure (DEBUGGER_LED_PIN, PIN_DIR_OUTPUT);
386+ pinConfigure (DEBUGGER_SW0_PIN, PIN_DIR_INPUT | PIN_INPUT_ENABLE);
387+ }
388+
389+ static void enableLDO (void ) {
390+
391+ pinConfigure (LOWQ_PIN, PIN_DIR_OUTPUT);
392+ digitalWrite (LOWQ_PIN, HIGH);
393+
394+ // Wait a little to let LDO mode settle
395+ delay (10 );
396+ }
397+
398+ static void disableLDO (void ) {
399+ pinConfigure (LOWQ_PIN, PIN_DIR_OUTPUT);
400+ digitalWrite (LOWQ_PIN, LOW);
401+
402+ // Wait a little to let PWM mode settle
403+ delay (10 );
323404}
324405
325406bool LowPowerClass::configurePowerDown (void ) {
@@ -449,7 +530,9 @@ void LowPowerClass::powerSave(void) {
449530 while (!attemptToEnterPowerSaveModeForModem (30000 ) &&
450531 millis () - start_time_ms < period * 1000 ) {}
451532
533+ enableLDO ();
452534 sleep_cpu ();
535+ disableLDO ();
453536
454537 if (modem_is_in_power_save) {
455538 modem_is_in_power_save = false ;
@@ -469,6 +552,7 @@ void LowPowerClass::powerDown(const uint32_t power_down_time_seconds) {
469552
470553 Lte.end ();
471554 enablePIT ();
555+ enableLDO ();
472556
473557 uint32_t remaining_time_seconds =
474558 power_down_time_seconds -
@@ -487,6 +571,7 @@ void LowPowerClass::powerDown(const uint32_t power_down_time_seconds) {
487571 }
488572 }
489573
574+ disableLDO ();
490575 disablePIT ();
491576 Lte.begin ();
492577
0 commit comments