芯片选择STM32F072C8T6,使用了HAL库与LL库,FreeRTOS框架:
硬件是一块工业打出来的协议盒测试板,自己找人贴的器件,上面主要是IEBUS收发器、CAN收发器、几个12V输出控制,以及转5V(CAN收发器使用)、转3.3V(MCU使用)电路。
以下是部分程序:
1.进入休眠的函数:
void IntoDeepSleep(void)
{
HAL_CAN_DeInit(&hcan);
/* Configure wakeup condition */
CanBusCANWakeupConfig();
DebugPrint("Into sleep ......");
DebugPrint("Into sleep ......");
/* Deinitialize chip */
FlyGPIODeInit();
/* 低功耗模式下调试器是否继续连接使用 */
#if(DEBUG_ENABLE)
DBGMCU->CR |= DBGMCU_CR_DBG_STOP | DBGMCU_CR_DBG_STANDBY;
#endif
/* Enter stop mode */
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
// HAL_PWR_EnterSTANDBYMode();
/* After wakeup, restart from bootloader or system */
#if 0
GotoWhatStatus(USER_SYSTEM_ENTRY);
#else
GotoWhatStatus(BOOTLOADER_ENTRY);
#endif
}
2.禁掉外设函数:
void FlyGPIODeInit(void)
{
/* Deinitialize comunication interface */
LL_USART_DeInit(USART1);
LL_USART_DeInit(USART2);
// HAL_CAN_DeInit(&hcan);
SystemPin_Config();
/* Disable systick */
SysTick->CTRL &= ~(SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk);
NVIC_DisableIRQ(SysTick_IRQn);
// RCC->APB1ENR = RCC_APB1ENR_PWREN;
}
3.IO口配置函数
/**
* @brief System Power Configuration
* The system Power is configured as follow :
* + No IWDG
* + Wakeup using EXTI Line (User PA.xx)
* @param None
* @retval None
*/
static void SystemPin_Config(void)
{
GPIO_InitTypeDef gpio_initstruct;
/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();
/* Set all GPIO in analog state to reduce power consumption, except Px.xx */
/* to keep user button interrupt enabled */
/* Note: Debug using ST-Link is not possible during the execution of this */
/* example because communication between ST-link and the device */
/* under test is done through UART. All GPIO pins are disabled (set */
/* to analog input mode) including UART I/O pins. */
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA |
LL_AHB1_GRP1_PERIPH_GPIOB |
LL_AHB1_GRP1_PERIPH_GPIOC |
LL_AHB1_GRP1_PERIPH_GPIOD |
LL_AHB1_GRP1_PERIPH_GPIOE |
LL_AHB1_GRP1_PERIPH_GPIOF);
/* In HAL, following codes can be called instead of LL:
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();*/
gpio_initstruct.Mode = GPIO_MODE_ANALOG;
gpio_initstruct.Speed = GPIO_SPEED_FREQ_LOW;
gpio_initstruct.Pull = GPIO_NOPULL;
gpio_initstruct.Pin = GPIO_PIN_All;
HAL_GPIO_Init(GPIOB, &gpio_initstruct);
HAL_GPIO_Init(GPIOC, &gpio_initstruct);
HAL_GPIO_Init(GPIOD, &gpio_initstruct);
HAL_GPIO_Init(GPIOE, &gpio_initstruct);
HAL_GPIO_Init(GPIOF, &gpio_initstruct);
/* Set all the GPIOC pin to analog except PA.11-12 */
gpio_initstruct.Pin &= ~(CAN_RX_Pin | CAN_TX_Pin );
HAL_GPIO_Init(GPIOA, &gpio_initstruct);
}
/*######################################################################*/
低功耗分析:
1.三种休眠模式,stop模式好用,standby模式功耗最低;
2.DeInit问题:把之前开过的外设全部关掉!
3.IO口配置问题(这个最要命),擦除芯片后一个个检测IO的电平,进入低功耗之前做好相应的匹配;
4.硬件功耗(主要是部分器件耗电流!!):
5. stop模式:6.5uA(最低)
standby模式: 4.0uA(最低)
注:
*配置GPIO为输出时,根据输出的常态选择上拉/下拉,如闲置无连接输出为0,则配置为下拉,输出闲置为1,则配置上拉;我则是把除了唤醒引脚之外的IO口全部设为模拟输入,无上下拉模式。其实设置为浮空模式的也有。
/*######################################################################*/
参考网站:
分享一个低功耗项目小小心得-STM32-F0/F1/F2专区-OpenEdv-开源电子网
http://www.openedv.com/posts/list/18372.htm
STM32F103C8T6低功耗至25uA,再也下不去了,请教高手 - ST MCU - 21ic电子技术论坛
http://bbs.21ic.com/icview-558242-1-1.html