⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sleepmodesdemo.c

📁 jennic jn5139模块的睡眠模式代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 * MODULE:             Sleep Modes Demo - Callbacks
 *
 * COMPONENT:          $RCSfile:  $
 *
 * VERSION:            $Name:  $
 *
 * REVISION:           $Revision: $
 *
 * DATED:              $Date: $
 *
 * STATUS:             $State: $
 *
 * AUTHOR:             gpfef
 *
 * DESCRIPTION:
 *
 *
 * LAST MODIFIED BY:   $Author:  $
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 *  (c) Copyright 2005 JENNIC Ltd
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include <jendefs.h>
#include <AppHardwareApi.h>
#include <AppApi.h>
#include <LedControl.h>
#include <button.h>
#include "Utils.h"

#include <gdb.h>

//#define FLASH_SLEEP         // for JN5121 flash deep power down
//#define HIGH_POWER          // for JN5139 High power module

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
#define INTERVAL_IN_32K_PERIODS			160000

#define DISABLE_INTS					FALSE
#define ENABLE_INTS						TRUE

#define FLASH_SLAVE_MASK				1
#define FLASH_SLEEP_CODE				0xb9
#define	FLASH_WAKE_CODE					0xab

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/
typedef enum
{
	E_NO_SLEEP			= 0x00,
	E_NO_MEMORY_HOLD,
	E_WITH_MEMORY_HOLD,
	E_DEEP_SLEEP
}teSleepState;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void vInit(void);
PRIVATE void vInitSystem(void);
PRIVATE void vInitButtonsLeds(void);
PRIVATE void vConfigureWakeSource(bool_t bEnableInts);
PRIVATE void vMain(void);
PRIVATE void vProcessButtons(void);
PRIVATE void vHandleWakeInterrupt(uint32 u32DeviceId, uint32 u32ItemBitmap);
PRIVATE void vHandleTimerInterrupt(uint32 u32DeviceId, uint32 u32ItemBitmap);
PRIVATE void vToggleLed(void);
PRIVATE void vSleep(teSleepState eSleepType);

#ifdef FLASH_SLEEP
PRIVATE void vFlashSleep(uint8 u8CodeWord);
#endif

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/
PRIVATE uint32 u32CalibratedIntervalTime;
PRIVATE bool_t bLedState = TRUE;

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for application.
 *
 * RETURNS:
 * void, never returns
 *
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* Debug hooks: include these regardless of whether debugging or not */
    HAL_GDB_INIT();
    HAL_BREAKPOINT();

    vInit();

    vInitButtonsLeds();

    #ifdef UART0_DEBUG
    vUtils_Debug("Woke from sleep or reset in AppColdStart");
    #endif

    vMain();
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader when sleep with memory hold
 * is used
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    vInit();

    #ifdef FLASH_SLEEP
	/* ensure FLASH device is awake */
	vFlashSleep(FLASH_WAKE_CODE);
    #endif

    #ifdef UART0_DEBUG
    vUtils_Debug("Woke from sleep with memory hold in AppWarmStart");
    #endif

    vMain();
}

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: vInit
 *
 * DESCRIPTION:
 * Initialises hardware.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInit(void)
{
#ifdef UART0_DEBUG
    vUtils_Init();
#endif

    vInitSystem();
}

/****************************************************************************
 *
 * NAME: vInitSystem
 *
 * DESCRIPTION:
 * Initialises Timers and Interrupts.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitSystem(void)
{
#ifdef JN5121
    /* Initialise hardware interface */
    (void)u32AHI_Init();

    /* make sure interrupt callback function is registered BEFORE enabling
    the programmable interrupt controller, as there may be interrupts waiting
    which will require servicing. If this step was not done first, any
    existing interrupts which required servicing would cause a callback to
    an undefined function */
    vAHI_SysCtrlRegisterCallback(vHandleWakeInterrupt);
    vAHI_Timer0RegisterCallback(vHandleTimerInterrupt);
#else
    // Must be a JN5139
    /* make sure interrupt callback function is registered BEFORE enabling
    the programmable interrupt controller, as there may be interrupts waiting
    which will require servicing. If this step was not done first, any
    existing interrupts which required servicing would cause a callback to
    an undefined function */
    vAHI_SysCtrlRegisterCallback(vHandleWakeInterrupt);
    vAHI_Timer0RegisterCallback(vHandleTimerInterrupt);

    /* Initialise hardware interface */
    (void)u32AHI_Init();
#endif

    #ifdef HIGH_POWER
    #ifdef JN513x
    // Enable JN5139 High power module
    vAHI_HighPowerModuleEnable(TRUE, TRUE);
    #endif
    #endif

    /* Enable the programmable interrupt controller */
    (void)u32AppApiInit(NULL,NULL,NULL,NULL,NULL,NULL);

	/* set timer up for four cycles per second */
   	vAHI_TimerEnable(E_AHI_TIMER_0, 10, FALSE, TRUE, FALSE);
    vAHI_TimerClockSelect(E_AHI_TIMER_0, FALSE, TRUE);
    vAHI_TimerStartRepeat(E_AHI_TIMER_0, 2000, 4000);

    vConfigureWakeSource(DISABLE_INTS);
}

/****************************************************************************
 *
 * NAME: vInitButtonsLeds
 *
 * DESCRIPTION:
 * Initialises buttons and leds.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vInitButtonsLeds(void)
{
    vLedInitFfd();
	vLedControl(0, FALSE);
	vLedControl(1, TRUE);
	vLedControl(2, FALSE);
	vLedControl(3, FALSE);

	vButtonInitFfd();
}

/****************************************************************************
 *
 * NAME: vConfigureWakeSource
 *
 * DESCRIPTION:
 * Enables the comparator, DIO and wake timers, either enabling or
 * disabling the interrupts on each of them
 *
 * PARAMETERS:      Name            RW  Usage
 *                  bEnableInts     R   TRUE to enable interrupts
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vConfigureWakeSource(bool_t bEnableInts)
{
	/* Initialise buttons */

	vAHI_DioInterruptEnable(0, BUTTON_ALL_MASK_FFD_PIN );
	if (bEnableInts)
	{
		vAHI_DioInterruptEnable(BUTTON_3_PIN, 0);
		vAHI_DioInterruptEdge(0, BUTTON_3_PIN);
	}

	/* turn on regulator so we can configure the comparator */
	vAHI_ApConfigure(E_AHI_AP_REGULATOR_ENABLE, E_AHI_AP_INT_DISABLE,E_AHI_AP_SAMPLE_8,
		E_AHI_AP_CLOCKDIV_500KHZ, E_AHI_AP_INTREF);
	while (!bAHI_APRegulatorEnabled());

	/* configure comp */
	vAHI_CompEnable(E_AHI_COMP_HYSTERESIS_20MV, E_AHI_COMP_SEL_EXT);
	vAHI_CompIntEnable(bEnableInts, bEnableInts);

	/* not necessary to keep analogue regulator running to operate comparator,
	only for configuring it */
	vAHI_ApConfigure(E_AHI_AP_REGULATOR_DISABLE, E_AHI_AP_INT_DISABLE,E_AHI_AP_SAMPLE_8,
		E_AHI_AP_CLOCKDIV_500KHZ, E_AHI_AP_INTREF);

	/* Enable and calibrate wake timer 0 */
	vAHI_WakeTimerEnable(E_AHI_WAKE_TIMER_0, bEnableInts);
	/* only calibrate wake timer with interrupts disabled, as otherwise it will
	generate unwanted interrupts */
	if(!bEnableInts)
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -