YIQI-EVB-01 开发板(裸系统)低功耗演示(2019年1月5日更新)

网友投稿 454 2022-05-30

***本例程只演示裸系统工程,不接外部任何传感器,不使用NB模组,便于读者从开始了解低功耗的工作模式和实际功耗体验。更高级的功能演示将在后期逐步推出,敬请期待!***

一、准备工作

1.1、硬件准备

1.2、软件准备

1.3、知识准备

嵌入式知识,如:硬件识别,连接线,传感器,IIC,SPI,UART等等;

C语言知识,用于开发基础嵌入式程序;

STM32 标准库函数和HAL库函数知识,用于开发基于ST MCU芯片的程序;

eclipse 集成开发环境基础使用,如:编译程序,烧写程序等等。(搜索引擎搜索关键字:eclipse 开发STM32 );

NB-IoT 基础知识,用于调试NB-IoT模组,本例程用海思系列NB模组,可以参考上海移远公司官方文档;

华为OceanConnect IoT 平台知识,用于数据传输和获取;

其他高级特性所需知识。

二、项目工程管理

2.1、STM32CubeMX创建工程

选择本开发板对应的MCU,创建工程:Start Project

(可选项)勾选晶振,外部高速低速在板子上都支持了。

配置时钟,启用8MHz外部晶振,采用PLL倍频方式,全速运行32MHz

项目管理器配置,文件位置,Toolchian / IDE 类型,我们用SW4STM32

配置代码生成器,生成代码

生成完毕,目录结构如下

2.2、SW4STM32编译烧写

导入刚刚生成的工程

选择路径

选择已存在的项目

导入成功后,目录如下

编译工程,成功

配置烧写功能,选择Debug Configurations...

在Debugger中将Mode Setup中Rest Mode改成:Software system reset

在Stratup中,将Reset and Delay 和 Halt 勾选取消,Apply后保存

将ST-LINK连接到开发板,注意线序,然后烧写到设备中

选择Debug 模块,选中“Reset after program”,OK点击后,开始烧写

烧写(下载)成功,会看到如下提示信息。

三、低功耗测试

本例程采用RTC+STOP模式,所以需要开启RTC功能。

3.1、开启RTC

3.2、代码例程

main.c

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "rtc.h"

#include "gpio.h"

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

/* USER CODE BEGIN PFP */

void SystemClockConfig_STOP(void);

void SystemPower_Pre_STOP(void);

void SystemPower_Post_STOP(void);

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**

* @brief  The application entry point.

* @retval int

*/

int main(void)

{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */

SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_RTC_Init();

/* USER CODE BEGIN 2 */

//开启低功耗模式

HAL_PWREx_EnableUltraLowPower();

HAL_PWREx_EnableFastWakeUp();

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1) {

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

//重置RTC服务

HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);

//重要!!!!关闭与NB通讯的串口,如有必要

//SystemPower_Pre_STOP();

//开启RTC唤醒中断

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xffff,

RTC_WAKEUPCLOCK_RTCCLK_DIV16);

//进入低功耗模式STOP

HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);

//重要!!!!打开与NB通讯的串口,如有必要

//SystemPower_Post_STOP();

//退出STOP模式后重新初始化时钟

SystemClockConfig_STOP();

//延时几秒

HAL_Delay(5000);

}

/* USER CODE END 3 */

}

/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

/**Configure the main internal regulator output voltage

*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;

RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

{

Error_Handler();

}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;

PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

{

Error_Handler();

}

}

/* USER CODE BEGIN 4 */

/**

* @brief  Configures system clock after wake-up from STOP: enable HSI, PLL

*         and select PLL as system clock source.

* @param  None

* @retval None

*/

void SystemClockConfig_STOP(void) {

RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };

/* Enable Power Control clock */

__HAL_RCC_PWR_CLK_ENABLE()

;

/* The voltage scaling allows optimizing the power consumption when the device is

clocked below the maximum system frequency, to update the voltage scaling value

regarding system frequency refer to product datasheet.  */

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/* Poll VOSF bit of in PWR_CSR. Wait until it is reset to 0 */

while (__HAL_PWR_GET_FLAG(PWR_FLAG_VOS) != RESET) {

};

/* Get the Oscillators configuration according to the internal RCC registers */

HAL_RCC_GetOscConfig(&RCC_OscInitStruct);

/* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE

| RCC_OSCILLATORTYPE_LSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;

RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {

Error_Handler();

}

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2

clocks dividers */

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {

Error_Handler();

YIQI-EVB-01 开发板(裸系统)低功耗演示(2019年1月5日更新)

}

}

/**

* 进入低功耗模式前处理

*/

void SystemPower_Pre_STOP(void) {

GPIO_InitTypeDef GPIO_InitStruct;

/*Configure GPIO pin : USART1=> PA9, PA10 */

GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;

GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/**

* 退出低功耗模式后处理

*/

void SystemPower_Post_STOP(void) {

GPIO_InitTypeDef GPIO_InitStruct;

/*Configure GPIO pin : USART1=> PA9, PA10 */

GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_PULLUP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF7_USART1;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE END 4 */

/**

* @brief  This function is executed in case of error occurrence.

* @retval None

*/

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

/* USER CODE END Error_Handler_Debug */

}

#ifdef  USE_FULL_ASSERT

/**

* @brief  Reports the name of the source file and the source line number

*         where the assert_param error has occurred.

* @param  file: pointer to the source file name

* @param  line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t *file, uint32_t line)

{

/* USER CODE BEGIN 6 */

/* User can add his own implementation to report the file name and line number,

tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

/* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

其中,0xffff,内部RTC最大16位,大概32s时间,要延长唤醒时间,需要做一个循环。

1

2

3

//开启RTC唤醒中断

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xffff,

RTC_WAKEUPCLOCK_RTCCLK_DIV16);

**注意** 由于初始化时,所有的IO都设置成模拟输入,所以SWD下载口也被禁用了,所以,下载程序指令下达后,要按住开发板RESET键不放,灯ST-LINK指示灯开始闪烁,松开按键,即可下载程序。

3.3、测试验证结果

实验验证,3.3V和4.2V时功耗一致。

4.2V供电:

3.3V供电:

四、优化建议

我们发现裸系统情况下,没有任何传感器挂在,功耗也在240uA,是不是有点高了?的确,经过我们分析,是由于开发板上的LDO不够紧密,具体可以参考下面的帖子:【亿琪软件】NB-IoT 终端设备低功耗--硬件设计(2018.12.20更新),里面有硬件设计的详细内容。

五、源代码下载

GitHub:https://github.com/jiekechoo/yiqi-evb-01-lowpower

嵌入式 单片机 IoT 硬件开发

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:5分钟APIG实战| 使用Rust语言快速构建API能力开放
下一篇:由一个简单的Python合并字典问题引发的思考,如何优化我们的代码?
相关文章