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

📄 sleepmodesdemo.c

📁 jennic jn5139模块的睡眠模式代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	/* Not necessary to keep analogue regulator running to operate comparator,
       only for configuring it. So turn it off to save power in sleep
     */
	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);
#endif

	/* 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)
	{
		u32CalibratedIntervalTime = ( (INTERVAL_IN_32K_PERIODS * 10000)
									/ u32AHI_WakeTimerCalibrate() );
	}
}

/****************************************************************************
 *
 * NAME: vMain
 *
 * DESCRIPTION:
 * Main loop. Dozes until interrupt seen from timer, then toggles LED before
 * checking for button presses
 *
 * RETURNS:
 * void
 *
 ***************************************************************************/
PRIVATE void vMain(void)
{

   	while(1)
    {
		vAHI_CpuDoze();

        if (u8AHI_TimerFired(E_AHI_TIMER_0) & E_AHI_TIMER_INT_PERIOD)
		{
			vToggleLed();
		}

        vProcessButtons();
	}
}

/****************************************************************************
 *
 * NAME: vProcessButtons
 *
 * DESCRIPTION:
 * Checks current button state and executes appropriate action.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vProcessButtons(void)
{
	teSleepState eSleepType = E_NO_SLEEP;

	switch(u8ButtonReadFfd())
	{
	case BUTTON_0_MASK:
		eSleepType = E_WITH_MEMORY_HOLD;
    //eSleepType = E_DEEP_SLEEP;
        break;

	case BUTTON_1_MASK:
		eSleepType = E_NO_MEMORY_HOLD;
		break;

	case BUTTON_2_MASK:
		eSleepType = E_DEEP_SLEEP;
		break;

	default:
		eSleepType = E_NO_SLEEP;
		break;
	}

	if (eSleepType != E_NO_SLEEP)
	{
		vSleep(eSleepType);
	}
}

/****************************************************************************
 *
 * NAME: vCheckWakeSource
 *
 * DESCRIPTION:
 * Determines which of the possible wake sources was actually responsible for
 * the wake event
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vCheckWakeSource(void)
{
    #ifdef UART0_DEBUG
    uint32 u32Result;

	u32Result = u32AHI_DioInterruptStatus();
	vUtils_DisplayMsg("DIO wake status: ", u32Result);
	if (u32Result  & (BUTTON_3_PIN))
	{
		vUtils_Debug("Woken by DIO: Button 4");
	}
	else
	{
    	u32Result = u8AHI_CompWakeStatus();
    	vUtils_DisplayMsg("Comparator wake status: ", u32Result);
	    if (u32Result)
	    {
		    vUtils_Debug("Woken by Comparator");
	    }
	    else
	    {
	        u32Result = u8AHI_WakeTimerFiredStatus();
	        vUtils_DisplayMsg("Wake Timer Status: ", u32Result);
	        if (u32Result & E_AHI_WAKE_TIMER_MASK_0)
	        {
		        /* must be from Wake Timer if not from DIO or Comparator */
    		    vUtils_Debug("Woken by Wake Timer 0");
	        }
	    }
	}
    #else
    // clear any pending interrupts
    (void)u32AHI_DioWakeStatus();
    (void)u8AHI_CompWakeStatus();
    (void)u8AHI_WakeTimerFiredStatus();
    #endif
}

/****************************************************************************
 *
 * NAME: vToggleLed
 *
 * DESCRIPTION:
 * Toggles the state of LED2,
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vToggleLed(void)
{
	if (bLedState)
	{
		vLedControl(1, FALSE);
		bLedState = FALSE;
	}
	else
	{
		vLedControl(1, TRUE);
		bLedState = TRUE;
	}
}

/****************************************************************************
 *
 * NAME: vSleep
 *
 * DESCRIPTION:
 * Waits for a short time to debounce the buttons. Clears any outstanding
 * interrupt events before enabling interrupts from those sources. Starts
 * the wake timer, then goes to sleep.
 *
 * PARAMETERS:      Name            RW  Usage
 *                  eSleepType    R   indicates whether to use RAM retention
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PRIVATE void vSleep(teSleepState eSleepType)
{
	/* ensure LED off */
	vAHI_TimerStop(E_AHI_TIMER_0);
	vLedControl(1, FALSE);
	bLedState = FALSE;

    #ifdef UART0_DEBUG
	vUtils_Debug("Sleeping....\r\n\n");
	/* wait for uart to send message */
	while ((u8AHI_UartReadLineStatus(E_AHI_UART_0) & E_AHI_UART_LS_THRE) == 0);
    vAHI_UartDisable(E_AHI_UART_0);
    #endif

	/* ensure that all existing interrupt events have been cleared, then enable
	interrupts from these sources. */
	(void)u8AHI_TimerFired(E_AHI_TIMER_0);
	(void)u32AHI_DioInterruptStatus();
	(void)u8AHI_WakeTimerFiredStatus();
	(void)u8AHI_CompWakeStatus();
	vConfigureWakeSource(ENABLE_INTS);

	/* Start wake timer */
	vAHI_WakeTimerStart(E_AHI_WAKE_TIMER_0, u32CalibratedIntervalTime);

    /* Minimise the sensor currents */
    #ifdef JN5121
    vAHI_DioSetOutput(0x00040508, 0);
    vAHI_DioSetDirection(0x00040508, 0);
    #else
    /* Must be JN5139 */
    vAHI_DioSetOutput(0x0000f000, 0);
    vAHI_DioSetDirection(0x0000f000, 0);
    #endif


    #ifdef FLASH_SLEEP
	/* send FLASH device to deep sleep; note that it can only be woken by sending
	the correct code word or by cycling the power */
	if ((eSleepType == E_WITH_MEMORY_HOLD) || (eSleepType == E_DEEP_SLEEP))
	{
		vFlashSleep(FLASH_SLEEP_CODE);
	}
    #endif

	/* Go to sleep */
    #ifdef JN5121
    /* select whether RAM retention is required */
	if (eSleepType == E_WITH_MEMORY_HOLD)
	{
		vAHI_MemoryHold(TRUE);
	}
	else
	{
		vAHI_MemoryHold(FALSE);
	}
	/* Go to sleep */
	if (eSleepType == E_DEEP_SLEEP)
	{
		vAHI_PowerDown(TRUE);
	}
	else
	{
		vAHI_PowerDown(FALSE);
	}
    #else
    /* JN513x */
    if (eSleepType == E_DEEP_SLEEP)
    {
        vAHI_Sleep(E_AHI_SLEEP_DEEP);
    }
    else
    {
        if (eSleepType == E_WITH_MEMORY_HOLD)
        {
           vAHI_Sleep(E_AHI_SLEEP_OSCON_RAMON);
           //vAHI_Sleep(E_AHI_SLEEP_DEEP);
        }
        else
        {
            vAHI_Sleep(E_AHI_SLEEP_OSCON_RAMOFF);

        }
    }
    #endif
	while(1);
}

/****************************************************************************
 *
 * NAME: vFlashSleep
 *
 * DESCRIPTION:
 * Sends flash device to sleep, or wakes it up
 *
 * PARAMETERS:      Name            RW  Usage
 *                  u8Sleep    		R   Send FLASH to sleep, or wake it up
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
#ifdef FLASH_SLEEP
PRIVATE void vFlashSleep(uint8 u8CodeWord)
{
	vAHI_SpiConfigure(0, FALSE, FALSE, TRUE, 1, FALSE, TRUE);
   	vAHI_SpiSelect(FLASH_SLAVE_MASK);
   	vAHI_SpiStartTransfer8(u8CodeWord);
   	/* ensure transfer complete before proceeeding */
   	vAHI_SpiWaitBusy();
}
#endif

/****************************************************************************/
/***        END OF FILE                                                   ***/
/****************************************************************************/

⌨️ 快捷键说明

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