📄 sleepmodesdemo.c
字号:
u32CalibratedIntervalTime = ((INTERVAL_IN_32K_PERIODS * 10000)
/ u32AHI_WakeTimerCalibrate());
}
}
/****************************************************************************
*
* NAME: vMain
*
* DESCRIPTION:
* Main loop. Dozes until interrupt seen from timer, then checks for button
* presses
*
*
* RETURNS:
* void
*
****************************************************************************/
PRIVATE void vMain(void)
{
while(1)
{
vAHI_CpuDoze();
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;
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: vHandleWakeInterrupt
*
* DESCRIPTION:
* Determines which wake source was responsible for the actual interrupt
*
* PARAMETERS: Name RW Usage
* u32DeviceId R Peripheral which caused interrupt
* u32ItemBitmap R Type of interrupt from that peripheral
*
* RETURNS:
* void
*
****************************************************************************/
PRIVATE void vHandleWakeInterrupt(uint32 u32DeviceId, uint32 u32ItemBitmap)
{
#ifdef UART0_DEBUG
vUtils_DisplayMsg("u32DeviceId: ", u32DeviceId);
vUtils_DisplayMsg("u32ItemBitmap: ", u32ItemBitmap);
if (u32ItemBitmap & E_AHI_SYSCTRL_COMP1_MASK)
{
vUtils_Debug("Interrupt from Comparator");
}
else if (u32ItemBitmap & (BUTTON_3_PIN))
{
vUtils_Debug("Interrupt from DIO: Button 4");
}
else
{
if (u32ItemBitmap & E_AHI_SYSCTRL_WK0_MASK)
{
/* must be from wake timer if not form DIO or comparator */
vUtils_Debug("Interrupt from Wake Timer 0");
}
}
#endif
}
/****************************************************************************
*
* NAME: vHandleTimerInterrupt
*
* DESCRIPTION:
* Handles the interrupt generated by the timer, toggling the LED as a result
*
* PARAMETERS: Name RW Usage
* u32DeviceId R Peripheral which caused interrupt
* u32ItemBitmap R Type of interrupt from that peripheral
*
* RETURNS:
* void
*
****************************************************************************/
PRIVATE void vHandleTimerInterrupt(uint32 u32DeviceId, uint32 u32ItemBitmap)
{
if (u32DeviceId != E_AHI_DEVICE_TIMER0)
{
return;
}
vToggleLed();
}
/****************************************************************************
*
* 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 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
#ifdef UART0_DEBUG
vUtils_Debug("Sleeping....\r\n\r\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
/* Minimise the sensor currents */
#ifdef JN5121
// standard JN5121
vAHI_DioSetOutput(0x00040508, 0);
vAHI_DioSetDirection(0x00040508,0);
#else
/* Must be Jn5139 */
vAHI_DioSetOutput(0x0000f000, 0);
vAHI_DioSetDirection(0x0000f000,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);
#ifdef HIGH_POWER
#ifdef JN513x
/* Turn off pull ups on DIO 2 & 3 */
//vAHI_DioSetPullup(0, 0x0c);
#endif
#endif
/* Go to sleep */
#ifdef JN5121
/* select whether RAMhn 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
/* Must be JN5139 */
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 + -