cancel
Showing results for 
Search instead for 
Did you mean: 

WICED Smart Bluetooth

Anonymous
Not applicable

1.how to config RTC use external 32.768?

   Find in rtc.h,but i can`t find how to config.

2.Is there a function that i can regeist the  RTC interupt for seconds function?

0 Likes
1 Solution
Anonymous
Not applicable

Hello dongzaiq,

Are you using the BCM20732 or BCM20736?

Here is the rtc sample.

  1. You need the external 32K xtal hooked up to P26/P27 or comment out the use 32K #define to use the internal 128K instead – This is not very accurate so it will drift within minutes).
  2. Backup and replace Wiced-Smart/tier2 directory with the files in the attached .zip file.
  3. Create <SDK>/Apps/rtc_sample/ and place the rtc_sample.c and makefile.mk in it.
  4. Create a make target called rtc_sample-BCM92073xTAG_Q32, then build and download.
  5. Hook up P0 to GND

Here is the code:

$ Copyright Broadcom Corporation $
*/

/** @file
*
* RTC Sample
*
* This application provides the sample code for interfacing with
* the on-chip RTC clock. It is highly recommended that an external 32K
* xtal be used when accurate time is to be maintained. The accuracy
* of the time maintained with the an external LPO is an order of magnitude
* better than the internal LPO.
*
*
* Features demonstrated
*  - Use of the on-chip RTC interface.
*  - Use of the external 32 KHz LPO when available for sleep and deep sleep.
*  - Entering deep sleep on demand.
*  - Using the internal or the external LPO (32KHz xtal) for timed wake from deep sleep.
*
* To demonstrate the app, work through the following steps.
* 1. Plug the WICED eval board into your computer
* 2. Build and download the application (to the WICED board)
* 3. Application initializes and RTC and prints time every second.
* 3.a Application programs and enters deepsleep, optionally configuring for a timed wake.
*/
#define CONFIG_IN_NVRAM 1

// Uncomment #define RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE below to enable using
// the external 32K LPO (make sure that R102 and R103 on BCM920737TAG_Q32 is populated
// with a 0 Ohm resistor, else the the behavior is undefined).
#define RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE

// Uncomment the #define RTC_SAMPLE_ENTER_DEEP_SLEEP_AND_ENABLE_TIMED_WAKE below to
// see how to enter deep sleep on demand and also program timed wake using either
// the internal LPO or the external 32KHz xtal (see
// RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE above).
#define RTC_SAMPLE_ENTER_DEEP_SLEEP_AND_ENABLE_TIMED_WAKE

#include "bleprofile.h"
#include "bleapp.h"
#include "gpiodriver.h"
#include "string.h"
#include "stdio.h"
#include "platform.h"
#include "bleappconfig.h"
#include "cfa.h"
#include "rtc.h"
#include "bleapputils.h"
#include "bleapp.h"
#include "devicelpm.h"
#include "miadriver.h"

/******************************************************
*                      Constants
******************************************************/

/******************************************************
*               Function Prototypes
******************************************************/
static void rtc_sample_create(void);
static void rtc_sample_timeout(UINT32 arg);
static void rtc_sample_fine_timeout(UINT32 arg);

/******************************************************
*               Variables Definitions
******************************************************/

// Following structure defines UART configuration
const BLE_PROFILE_PUART_CFG rtc_sample_puart_cfg =
{
    /*.baudrate   =*/ 115200,
    /*.txpin      =*/ GPIO_PIN_UART_TX,
    /*.rxpin      =*/ GPIO_PIN_UART_RX,
};

/******************************************************
*               Function Definitions
******************************************************/

// Application initialization
APPLICATION_INIT()
{
    bleapp_set_cfg(NULL, 0, NULL, (void *)&rtc_sample_puart_cfg, NULL, rtc_sample_create);
}

// Create the RTC sample.
void rtc_sample_create(void)
{
    RtcTime current_time;
char buffer[64];

    ble_trace0("rtc_sample_create()\n");

    if (!mia_isResetReasonPor())
    {
     ble_trace0("Waking from deep sleep because the timer went off or a GPIO triggered while waiting for timer to expire.");
    }
    else
    {
     ble_trace0("Not a timed wake.");
    }

    blecm_configFlag |= BLECM_DBGUART_LOG;

    bleprofile_Init(bleprofile_p_cfg);

#ifdef RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE
// If we need to use the external 32K, then configure the reference
rtcConfig.oscillatorFrequencykHz = RTC_REF_CLOCK_SRC_32KHZ;

// Since the 32K external LPO is connected tp P10, P11, P12, P26 and P27,
// input and putput disable all 5 GPIOs.
gpio_configurePin(0, 10, GPIO_INPUT_DISABLE, 0);
gpio_configurePin(0, 11, GPIO_INPUT_DISABLE, 0);
gpio_configurePin(0, 12, GPIO_INPUT_DISABLE, 0);
gpio_configurePin(1, 10, GPIO_INPUT_DISABLE, 0);
gpio_configurePin(1, 11, GPIO_INPUT_DISABLE, 0);
#endif

    // Initialize the RTC.
    rtc_init();

    memset(buffer, 0x00, sizeof(buffer));
ble_trace0("Time base is:");

// RtcTime of 0x00 is the start of RTC time.
    memset(&current_time, 0x00, sizeof(current_time));
rtc_ctime(&current_time, buffer);
ble_trace0(buffer);

// Let year = 2014.
current_time.year = 2014;
// Let month = april = 3 (jan = 0)
current_time.month = 3;
// Let day = 1st.
current_time.day = 1;
// Let current time be 12:00:00 Noon.
current_time.hour = 12;
current_time.minute = 00;
current_time.second = 0x00;

// If this is a power-on reset, we need to set up the reference time.
if (mia_isResetReasonPor())
{
  // Now set the on-chip RTC.
  if(rtc_setRTCTime(&current_time))
  {
   memset(buffer, 0x00, sizeof(buffer));

   ble_trace0("Power-on reset, set current time to:");
   rtc_ctime(&current_time, buffer);
   ble_trace0(buffer);
  }
  else
  {
   ble_trace0("Unable to set time.");
  }
}
else
{
  // Set up the original reference time instead of using 01/01/2010, 00:00:00 as the reference
  // because this is a wake from deep sleep. The HW clock keeps running in deep sleep so when
  // we wake up, the FW needs to know what was used as the original reference time.
  rtc_setReferenceTime(&current_time);
}

    bleprofile_regTimerCb(rtc_sample_fine_timeout, rtc_sample_timeout);
    bleprofile_StartTimer();

#ifdef RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE
    // Since we have an external 32 KHz LPO, switch to using this during sleep
    // because this will give us a more accurate sleep clock (lower drift than the
    // internal LPO, so the uncertainty window during a receive will be narrower).
    // In general, base sleep current will be lower too.
    // Switching to the external 32K with bleapputils_changeLPOSource without having
    // initialized the RTC, the bonded GPIOs and not having the 32KHz oscillator physically
    // connected to the chip will invoke undefined behavior.
    bleapputils_changeLPOSource(LPO_32KHZ_OSC, FALSE, 250);
#endif

    // Trace out number of bytes free.
    ble_trace1("Number of free bytes in RAM: %d",  cfa_mm_MemFreeBytes());
}

// One second timer expired. Read the time from RTC and print.
void rtc_sample_timeout(UINT32 arg)
{
RtcTime current_time;
UINT32 seconds_since_time_base;
char buffer[64];
tRTC_REAL_TIME_CLOCK raw_clock;

memset(buffer, 0x00, sizeof(buffer));

// Get and print current time.
rtc_getRTCTime(&current_time);
ble_trace0("Current date/time is:");
rtc_ctime(&current_time, buffer);
ble_trace0(buffer);

// Get and print time since time base in seconds.
rtc_RtcTime2Sec(&current_time, &seconds_since_time_base);
ble_trace1("Its been %d seconds since bigbang.", seconds_since_time_base);

// Get and print the raw 48 bit clock value.
rtc_getRTCRawClock(&raw_clock);

ble_trace2("Upper and lower 32 bit values: 0x%08X, 0x%08X\n", raw_clock.reg32map.rtc32[1], raw_clock.reg32map.rtc32[0]);

#ifdef RTC_SAMPLE_ENTER_DEEP_SLEEP_AND_ENABLE_TIMED_WAKE
{
  static UINT32 num_timeouts_since_boot = 0;

  // Demo going into deep sleep with wake after 5s.
  if (num_timeouts_since_boot++ >= 10)
  {
   // If its been ~10s of ADV, configure timed wake and
   // enter deep sleep right now.
   ble_trace0("Entering deep sleep.");

   gpio_configurePin(0, 0, 0x100, 0);

   // Configure the low power manager to enter deep sleep.
   devLpmConfig.disconnectedLowPowerMode = DEV_LPM_DISC_LOW_POWER_MODES_HID_OFF;

   // Configure the wake time in mS.
   devLpmConfig.wakeFromHidoffInMs = 5000;

   // Configure the reference clock to use.

#ifndef RTC_SAMPLE_USE_EXSTERNAL_32K_OSC_REFERENCE
   // Use internal LPO.
   devLpmConfig.wakeFromHidoffRefClk = HID_OFF_TIMED_WAKE_CLK_SRC_128KHZ;
#else
   // Use the external 32k.
   devLpmConfig.wakeFromHidoffRefClk = HID_OFF_TIMED_WAKE_CLK_SRC_32KHZ;
#endif

   gpio_configurePin(0, 0, 0x100, 0);

   // Enter deep-sleep now. Will not return.
   devlpm_enterLowPowerMode();
  }
}
#endif
}

void rtc_sample_fine_timeout(UINT32 arg)
{

}


Results:

  1. Once downloaded, hook up teraterm/hyperterm to HCI uart, disconnect RXd and reset.
  2. You should see something like this:

rtc_sample_create()
Not a timed wake.
020106030304000409bb8104
020a81
Time base is:
Jan 0 00:00:00 0000
Power-on reset, set current time to:
Apr 1 12:00:00 2014
Number of free bytes in RAM: 28436

blecm evt handler:
0e0401082000

blecm evt handler:
0e0401092000
@$*#04FF2EF7009201050000074A05197A732000074E05413E00000007362A0100000400073A2A500000000007522A50000100Current date/time is:
Apr 1 12:00:00 2014                Prints time every 1s.
Its been 134049600 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x00007506
Current date/time is:
Apr 1 12:00:01 2014
Its been 134049601 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x0000D9B7
Current date/time is:
Apr 1 12:00:02 2014
Its been 134049602 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x00015183
….                                             Continues to print time every 1s for 10s.
Current date/time is:
Apr 1 12:00:10 2014
Its been 134049610 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x00054DD0
Entering deep sleep.        After 10s of printing time, enters hid-off for 5s. Measure Vio and you should see ~5uA.
rtc_sample_create()
Waking from deep sleep because the timer went off or a GPIO triggered while waiting for timer to expire.         On wake up after 5s in hid-off, detects that this is a timed wake and not a PoR. So uses a different API to initialize reference time.
020106030304000409bb8104
020a81
Time base is:
Jan 0 00:00:00 0000
Number of free bytes in RAM: 28436

blecm evt handler:
0e0401082000

blecm evt handler:
0e0401092000
@$*#04FF2EF7009201050000074A05197A732000074E05413E00000007362A0100000400073A2A500000000007522A50000100Current date/time is:
Apr 1 12:00:16 2014          Continues to print time every 1s, starting from where we left off. Its off by 1s because there is a ~1s delay before the app timer goes off + boot time. Compare with wall clock and this will still be correct.
Its been 134049616 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x000850F4
Current date/time is:
Apr 1 12:00:17 2014
Its been 134049617 seconds since bigbang.
Upper and lower 32 bit values: 0x00000000, 0x0008C8C1
…              Continues same sequence forever.

Make file if you need it:

#
# $ Copyright Broadcom Corporation $
#

########################################################################
# Application sources.
########################################################################
APP_SRC = rtc_sample.c

########################################################################
################ DO NOT MODIFY FILE BELOW THIS LINE ####################
########################################################################
APP_PATCHES_AND_LIBS += rtc_api.a

Let me know if this helps.

JT

View solution in original post

0 Likes
18 Replies