Skip to content

Dragonfly Nano(MTQN): power on/off so soft_power_on() fix #13935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ using namespace mbed;

ONBOARD_SARA4_PPP::ONBOARD_SARA4_PPP(FileHandle *fh) : SARA4_PPP(fh)
{
initialized = 0;
}

nsapi_error_t ONBOARD_SARA4_PPP::hard_power_on()
Expand All @@ -45,6 +46,8 @@ nsapi_error_t ONBOARD_SARA4_PPP::hard_power_off()

nsapi_error_t ONBOARD_SARA4_PPP::soft_power_on()
{
// See base function description. This function is for power on but reset too.
onboard_modem_power_down();
onboard_modem_power_up();
return NSAPI_ERROR_OK;
}
Expand Down Expand Up @@ -80,55 +83,105 @@ void ONBOARD_SARA4_PPP::onboard_modem_init()
{
gpio_t gpio;

// Take us out of reset
// Enable radio power regulator and buffer, configure RESET_N and PWR_ON.
gpio_init_inout(&gpio, RADIO_PWR, PIN_OUTPUT, PushPullNoPull, 1);
gpio_init_inout(&gpio, BUF_EN, PIN_OUTPUT, OpenDrainNoPull, 0);
gpio_init_out_ex(&gpio, MDMRST, 0);
gpio_init_out_ex(&gpio, MDMPWRON, 0);
gpio_init_inout(&gpio, RADIO_DTR, PIN_OUTPUT, OpenDrainNoPull, 0);
initialized = 1;
}

void ONBOARD_SARA4_PPP::onboard_modem_deinit()
{
// Make sure to power down before removing power!
onboard_modem_power_down();
gpio_t gpio;

// Back into reset
gpio_init_out_ex(&gpio, MDMRST, 1);
gpio_init_out_ex(&gpio, MDMPWRON, 1);
gpio_init_inout(&gpio, BUF_EN, PIN_OUTPUT, OpenDrainNoPull, 1);
gpio_init_inout(&gpio, RADIO_PWR, PIN_OUTPUT, PushPullNoPull, 0);
gpio_init_inout(&gpio, RADIO_DTR, PIN_OUTPUT, OpenDrainNoPull, 1);
// Set all to input no pull. Let pull resistors do their thing. Allows for lowest power draw.
// Disable radio power regulator and buffer.
gpio_init_inout(&gpio, MDMRST, PIN_INPUT, PullNone, 1);
gpio_init_inout(&gpio, MDMPWRON, PIN_INPUT, PullNone, 1);
gpio_init_inout(&gpio, BUF_EN, PIN_INPUT, PullNone, 1);
gpio_init_inout(&gpio, RADIO_PWR, PIN_INPUT, PullNone, 0);
gpio_init_inout(&gpio, RADIO_DTR, PIN_INPUT, PullNone, 1);
initialized = 0;
}

void ONBOARD_SARA4_PPP::onboard_modem_power_up()
{
onboard_modem_init();
// Make sure the radio is initialized so it can be powered on.
if (!initialized){
onboard_modem_init();
}

gpio_t gpio;
gpio_init_in(&gpio, MON_1V8);

if(gpio_is_connected(&gpio) && !gpio_read(&gpio)) {
unsigned int i = 0;
while(i < 3)
{
press_power_button(150000);
thread_sleep_for(250);

if(gpio_read(&gpio))
{
break;
}
i++;
}
gpio_t radioOn;
gpio_init_in(&radioOn, MON_1V8);

// Need volatile so 1.8v check is not optimized out.
volatile int v1_8 = gpio_read(&radioOn);

// If radio is on, do nothing.
if (v1_8) {
return;
}

uint8_t retries = 5;
do {
// If it hasn't turned on after multiple tries, exit and return.
if(0 == retries--) {
return;
}
// Activate PWR_ON for 150ms-3.2s to switch on. Reference ublox SARA-R4 data sheet.
press_power_button(160000);
v1_8 = gpio_read(&radioOn);
} while (!v1_8);
// Takes ~4.5s to power on. Reference SARA-R4 system integration manual... module power-on.
// Pad a few more seconds to be sure.
thread_sleep_for(8000);
}

void ONBOARD_SARA4_PPP::onboard_modem_power_down()
{
/* Activate PWR_ON for 1.5s to switch off */
press_power_button(1500000);
// check for 1.8v low if not, take reset low for 10s
gpio_t radioOn;
gpio_init_in(&radioOn, MON_1V8);
// Need volatile so 1.8v check is not optimized out.
volatile int v1_8 = gpio_read(&radioOn);
// Do nothing if it's already off.
if (!v1_8) {
return;
}

// Make sure the I/O are properly initialized.
if (!initialized){
onboard_modem_init();
}
// Activate PWR_ON for at least 1.5s to switch off. Reference ublox SARA-R4 data sheet.
press_power_button(1600000);

// wait a max of 40s for 1.8v to go low. Reference AT command guide +CPWROFF estimated response.
uint8_t timeout = 40;
do {
thread_sleep_for(1000);
v1_8 = gpio_read(&radioOn);
} while (v1_8 && timeout--);

// hold RESET_N low (inverted) at least 10s to power down the radio.
if (v1_8) {
gpio_t gpio;
gpio_init_out_ex(&gpio, RADIO_RESET, 1);
thread_sleep_for(11000);
gpio_write(&gpio, 0);
}

v1_8 = gpio_read(&radioOn);
// Cut power to the radio if it's still not powered off.
if (v1_8) {
gpio_t gpio;
gpio_init_inout(&gpio, RADIO_PWR, PIN_INPUT, PullNone, 0);
thread_sleep_for(1000);
initialized = 0;
}
}

#endif // MBED_CONF_NSAPI_PRESENT
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ONBOARD_SARA4_PPP : public SARA4_PPP {
virtual nsapi_error_t soft_power_off();

private:
uint8_t initialized;

void press_power_button(int time_ms);

void onboard_modem_init();
Expand Down