• Announcements

    • bluehash

      Forum Upgrade   03/11/2017

      Hello Everyone, Thanks for being patient while the forums were being fixed and upgraded. Please see details and report issues in this thread. Thanks!
JRDavisUF

Using RTC_C alarm and WiFi at the same time

14 posts in this topic

Posted (edited)

Windows 7 / Code Composer Studio 7 / Energia 18 / MSP 432 LaunchPad + CC3100 WifI

 

I'd like to use a RTC_C calendar alarm to turn on/off my some streaming data collection, however, it doesn't seem to work.  That is, once I register the RTC_C interrupt via RTC_C_registerInterrupt,  I'm unable to init the WifI.  Specifically it hangs here in WiFi.cpp:

        int iRet = sl_Start(NULL, NULL, NULL);

 

I've whittled my code down to what appears below.  Does anyone have any ideas?

 

jrd

 

-----


 

// Header files
#include <WiFi.h>                         // Energia
#include <driverlib/MSP432P4xx/rtc_c.h>

#ifndef cWiFiSSID
cWiFiSSID should be #define as the name of your WiFi SSID
#endif
#ifndef cWiFiPassword
cWiFiPassword should be #define as the name of your WiFi password
#endif

void vRtcIsr() {
    // Clear the flag, but don't actually do anything
    RTC_C_clearInterruptFlag(RTC_C_CLOCK_ALARM_INTERRUPT);
}

// Main setup section
void setup() {

    // Turn on serial output
    Serial.begin(115200);
    Serial.println("");

    // Enable the clock
    RTC_C_startClock();

    // Set an alarm
    RTC_C_setCalendarAlarm( 0, RTC_C_ALARMCONDITION_OFF, RTC_C_ALARMCONDITION_OFF, RTC_C_ALARMCONDITION_OFF);
    RTC_C_registerInterrupt(vRtcIsr);
    RTC_C_enableInterrupt(RTC_C_CLOCK_ALARM_INTERRUPT);

}

// Main loop section
void loop() {

    // Start WiFi
    Serial.println("Attempting to turn on Wifi");
    WiFi.begin(cWiFiSSID,cWiFiPassword);
    Serial.println("Success");

    // Hang forever
    for(;;

}

 

Edited by bluehash
[ADMIN] Please use code tags<> next time. Thank you!

Share this post


Link to post
Share on other sites

@JRDavisUF

 

Just a cursory review but, do you want your wifi login to be in the loop? Shouldn't it be in setup(), or is the plan to repeatedly log in?

Share this post


Link to post
Share on other sites

I want the device to wake up once an hour, stream some data and then go back to sleep...So, I DO want the login to be inside a loop.  For purposes of this issue, it doesn't matter though...all commands can be in the setup, all in the loop, or a mixture...regardless, the same problem occurs.  For the code I provided, I was just trying to minimize things to just the bare-bones level.

 

In the real code, I have a simple handler *.ino loop which is waiting on an Event.  When the calendar ISR get's triggered, all it does is send the Event which wakes up the handler loop which then starts/stops data collection depending on the time.

Share this post


Link to post
Share on other sites

We are not sure which of WiFi or Serial is the culprit. So try with WiFi only and with Serial only. You might need to initialise Serial each time.

Share this post


Link to post
Share on other sites

OK, I got rid of the Serial stuff and used the green led to indicate success or failure.  The code again hangs when attempting to start wifi.

I then got rid of the the Wifi part and the code runs fine.  As such the problem still seems to be some combination of RTC_C and WiFi

Share this post


Link to post
Share on other sites

Made some progress, but am still having the same problem.

 

 First, I tried this code with CCSv6.2/E17 and had the same problems.

 

Second, I put a WiFi.begin/WiFi.disconnect in front of the RTC stuff in addition to after the RTC stuff....and this allowed the WiFi to start after the RTC stuff had been called.  So it seems happier letting the WiFi stuff go first....*However*, I really need more than a WiFi.disconnect, for power consumption purposes, I need to shutdown the WiFi module completely, as such I do a "sl_Stop(0)"   This works great in terms of turning off the power to the wifi module (CC3100); however, when I do a WiFi.begin/WiFi.disconnect/sl_Stop  before the RTC stuff, I can no longer re-start the WiFi after the RTC stuff again...So I'm sort of back to square one...

Share this post


Link to post
Share on other sites

Oh ya, a couple other things I tried:

 

1) I disabled the RTC alarm and unregistered the interrupt immediately after I set it. If I do that, the WiFi still will not start.  So registering the interrupt seems to foo-bar something.

 

2) I ran the RTC_C demo code from the driverlib example code that I found within the resource explorer (MSP432ware).  That stuff compiled and seemed to run fine..  I do note that the example code does not use a function call to register the interrupt for the RTC_C alarm like I am doing.  They have some "startup" code which explicitly defines the list of all the interrupts.  Does energia have this somewhere?  Perhaps my use of the interrupt register function is incompatible with startup-type code????

Share this post


Link to post
Share on other sites

Still trying to figure this out.  I output the hw/sw versions, do these look correct:

 

      H/W and S/W Versions
         CHIP: 67108864
         MAC: 31.1.4.0.1
         PHY: 1.0.3.34
         NWP: 2.7.0.0
         ROM: 13107
         HOST: 1.0.0.10

Share this post


Link to post
Share on other sites

[ Cross posting this that what I posted on your e2e post ]

 

I do have low power Sketches for the CC3200 but low power handling on the CC3200 is very different from what it is on the MSP432. The MSP432 Wiring implementation is based on TI-RTOS. So hence a mix of driverlib/TI-RTOS API's need to be used to get the RTC going. I will follow up with the WiFi code later this week but below is a skeleton Sketch for the RTC:
 
// Header files
#include <driverlib/MSP432P4xx/rtc_c.h>
#include <driverlib/MSP432P4xx/interrupt.h>
#include <driverlib/MSP432P4xx/wdt_a.h>
#include <ti/drivers/power/PowerMSP432.h>
#include <ti/sysbios/family/arm/m3/Hwi.h>
#include <ti/drivers/Power.h>


volatile bool flag = false;


void vRtcIsr(UArg arg) {
  // Clear the flag, but don't actually do anything
  RTC_C_clearInterruptFlag(RTC_C_CLOCK_ALARM_INTERRUPT);
}


/* Time is November 12th 1955 10:03:00 PM */
const RTC_C_Calendar currentTime =
{
  0x00,
  0x03,
  0x22,
  0x12,
  0x11,
  0x1955
};


uint32_t x = 0x04;
Hwi_Params params;


void setup() {
  // Initialize Serial
  Serial.begin(115200);
  Serial.println("Setting up RTC");


  // Set current time
  RTC_C_initCalendar(&currentTime, RTC_C_FORMAT_BCD);
  // Set an alarm
  RTC_C_setCalendarAlarm(x, 0x22, RTC_C_ALARMCONDITION_OFF, RTC_C_ALARMCONDITION_OFF);


  // Register interrupt
  Hwi_Params_init(&params);
  Hwi_create(INT_RTC_C, vRtcIsr, &params, NULL);
  // RTC_C_registerInterrupt(vRtcIsr);
  Interrupt_enableInterrupt(INT_RTC_C);


  // Clear the interrupt
  RTC_C_clearInterruptFlag(RTC_C_CLOCK_ALARM_INTERRUPT);
  // Enable the interrupt
  RTC_C_enableInterrupt(RTC_C_CLOCK_ALARM_INTERRUPT);
  // Enable the clock
  RTC_C_startClock();


}


void loop() {
  // Small delay to allow for Serial to be flushed.
  delay(1000);
  // End serial to remove power constraints
  Serial.end();


  // Stop the watchdog and remove power constraints
  stopWatchdog();
  
  /* go to DEEPSLEEP_1 */
  Power_sleep(PowerMSP432_DEEPSLEEP_0);


  startWatchdog();
  // Reinitialize Serial 
  Serial.begin(115200);
  Serial.println("Woken up");
  
  x++;


  // Set the alarm for now + 1 min
  RTC_C_setCalendarAlarm(x, 0x22, RTC_C_ALARMCONDITION_OFF, RTC_C_ALARMCONDITION_OFF);
}


void startWatchdog() {
  // Clear the watchdog timer
  WDT_A_clearTimer();
  // Start the watchdog timer
  WDT_A_startTimer();
}


void stopWatchdog() {
  // Hold the watchdog timer 
  WDT_A_holdTimer();


  // Remove power constraints. Do not allow LPM4 since this kills the RTC
  Power_setConstraint(PowerMSP432_DISALLOW_DEEPSLEEP_1);
  Power_releaseConstraint(PowerMSP432_DISALLOW_DEEPSLEEP_0);
}
Rei Vilo and NurseBob like this

Share this post


Link to post
Share on other sites

Thanks!   Switching to the Hwi stuff to register the interrupt (and adding arguments into the ISR) was exactly what I needed. RTC + WiFi now works fine...Now if I can just figure out why my RTC clock accuracy is only 3000ppm :)  ...

 

Share this post


Link to post
Share on other sites

FWIW, I did figure out why my RTC clock accuracy was so bad.  By default, the RTC clock sources the internal 32k crystal, not the external LFXT as BCLK.  As such, to get it to work:

1) Define frequency of LFXT to 32k:   MAP_CS_setExternalClockSourceFrequency(32768, 48000000); // LF, HF

(why energia doesn't do this already, I don't know.  I see how starting it might consume power unnecessarily, but why not set the value atleast???)

This step might not be needed, but since I wanted to query my clock speeds later, I added it.

2) Turn on LFXT (I loop on this since it doesn't always take):  bool status = MAP_CS_startLFXTWithTimeout(CS_LFXT_DRIVE3, 10);

3) Tell the RTC to use LFXT:  MAP_CS_initClockSignal(CS_BCLK,CS_LFXTCLK_SELECT,CS_CLOCK_DIVIDER_1);

With my msp432 launchpad, this gives me about 5ppm  (testing every 15s for about 2 weeks).

Hope this helps anyone else who might have had issues...

jrd

PS  Of course, you probably don't need to use the MAP_ versions...but I figured why not :)

PPS  This is how I was getting my actual clock speeds...ie, why I added step 1.

    // Get the values
    uint32_t aclk   = MAP_CS_getACLK();
    uint32_t mclk   = MAP_CS_getMCLK();
    uint32_t smclk  = MAP_CS_getSMCLK();
    uint32_t hsmclk = MAP_CS_getHSMCLK();
    uint32_t bclk   = MAP_CS_getBCLK();

bluehash likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now