在这份项目里,Bootloader使用之前Bootloader学习笔记里做的样例。
可以说是为了这碗醋,包了这碗饺子
简易桌面闹钟
FreeRTOS移植:超详细的FreeRTOS移植全教程——基于stm32_freertos移植教程-CSDN博客
时钟方面需要改用FreeRTOS的SysTick,否则系统会卡死
实物

整体结构

OLED示意图
此处OTA的值存储在AT24C02的0xF0-0XF3字节处(AT24C02总共256字节)
简易模式

- PA2进入多功能模式
- PA0调整城市显示
- PC13调整OTA状态
- PB10可以重启设备
- 支持震动自动切换多功能模式
多功能模式

-
PA0进入简易模式
-
温湿度数据1s刷新一次
-
加速度0.5s刷新一次
-
10s无操作自动进入简易模式
SysTick、SVC、PendSV
SysTick_Handler - 系统时钟
-
就像心跳,定期检查是否需要切换任务
-
频率固定,系统运行期间持续工作
-
负责时间管理和调度触发
SVC_Handler - 系统启动器
-
就像点火器,只负责启动第一个任务
-
只调用一次,启动后就不工作了
-
从特权模式切换到用户模式
PendSV_Handler - 任务切换器
-
就像交通指挥员,负责安排哪个任务先运行
-
需要切换时才工作,优先级最低
-
在用户模式下进行任务切换
Q:为什么使用PendSV_Handler来切换任务?
Pend → 可挂起的
SV → 系统服务
- 中断优先级最低;不影响其他中断
- 只能软件触发,可控;CPU不会意外触发它
- 硬件自动保存部分上下文;减少软件负担
| 特性 |
SysTick_Handler |
SVC_Handler |
PendSV_Handler |
| 全称 |
System Tick Handler |
Supervisor Call Handler |
Pending Service Handler |
| 中文名 |
系统滴答中断 |
超级调用中断 |
挂起服务中断 |
| 触发方式 |
硬件定时器 |
软件调用SVC指令 |
软件触发PendSV |
新添内容
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
| uint32_t AT24C02_ReadOTAFlag(void) { uint8_t addr = 0xF0; uint32_t flag = 0; uint8_t *pflag = (uint8_t *)&flag; for(int i = 0; i < 4; i++) { uint8_t data = 0; AT24C02_ReadData(addr + i, &data, 1); if(data == 0xFF) { return 0; } pflag[i] = data; } return flag; }
void Bootloader_Jump(void) { uint32_t OTA_flag = AT24C02_ReadOTAFlag(); uprintf("OTA_flag: %d \r\n", OTA_flag);
if(OTA_flag == 1) { uprintf("Direct jump to APP\r\n"); LOAD_A(FLASH_A_SADDR); return; } if(Bootloader_Enter(20) == 0) { if(OTA_flag == OTA_SET_FLAG) { uprintf("OTA update \r\n"); BootState |= UPDATA_A_FLAG; Updata_A.W25Q64_BlockNM = 0; } else { uprintf("Jump to Area A \r\n"); LOAD_A(FLASH_A_SADDR); } } uprintf("Enter Bootloader command line \r\n"); Bootloader_Info(); }
|
one-wire协议
One Wire(单线)总线协议,它只需要一根数据线就能实现双向通信,大大简化了硬件连接。
【通信协议】单总线协议详解——以DHT11为例_单总线通信协议-CSDN博客
delay
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
| static uint8_t fac_us = 0; static uint16_t fac_ms = 0;
void Delay_init(void) { uint32_t reload; SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); fac_us = SystemCoreClock / 1000000; reload = SystemCoreClock / 1000000; reload *= 1000000 / configTICK_RATE_HZ; fac_ms = 1000 / configTICK_RATE_HZ;
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk; SysTick->LOAD = reload; SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk; }
void Delay_us(uint32_t us) { uint32_t ticks; uint32_t told, tnow, tcnt = 0; uint32_t reload = SysTick->LOAD; ticks = us * fac_us; told = SysTick->VAL; while (1) { tnow = SysTick->VAL; if (tnow != told) { if (tnow < told) tcnt += told - tnow; else tcnt += reload - tnow + told; told = tnow; if (tcnt >= ticks) break; } }; }
void Delay_ms(uint32_t ms) { if (xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED) { if (ms >= fac_ms) { vTaskDelay(ms / fac_ms); } ms %= fac_ms; } Delay_us((uint32_t)(ms * 1000)); }
void Delay_s(uint32_t s) { Delay_ms(s * 1000); }
void Delay_xms(uint16_t ms) { uint32_t i; for (i = 0; i < ms; i++) Delay_us(1000); }
|
裁剪FreeRTOS
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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
| #ifndef FREERTOS_CONFIG_H #define FREERTOS_CONFIG_H
#include "stm32f10x.h"
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) #include <stdint.h> extern uint32_t SystemCoreClock; #endif
#define configASSERT(x) \ if ((x) == 0) \ { \ taskDISABLE_INTERRUPTS(); \ while (1) \ ; \ }
#define configUSE_PREEMPTION 1
#define configUSE_TIME_SLICING 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1
#define configUSE_TICKLESS_IDLE 0
#define configCPU_CLOCK_HZ (SystemCoreClock)
#define configTICK_RATE_HZ ((TickType_t)1000)
#define configMAX_PRIORITIES (16)
#define configMINIMAL_STACK_SIZE ((unsigned short)64)
#define configMAX_TASK_NAME_LEN (12)
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
#define configUSE_QUEUE_SETS 0
#define configUSE_TASK_NOTIFICATIONS 1
#define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_EVENT_GROUPS 1
#define configQUEUE_REGISTRY_SIZE 6
#define configUSE_APPLICATION_TASK_TAG 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
#define configSUPPORT_STATIC_ALLOCATION 0
#define configTOTAL_HEAP_SIZE ((size_t)(14 * 1024))
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configUSE_MALLOC_FAILED_HOOK 0
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES (2)
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1)
#define configTIMER_QUEUE_LENGTH 5
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE)
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 0
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#ifdef __NVIC_PRIO_BITS #define configPRIO_BITS __NVIC_PRIO_BITS #else #define configPRIO_BITS 4 #endif
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 1
#define configKERNEL_INTERRUPT_PRIORITY (configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
#define configMAX_SYSCALL_INTERRUPT_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS))
#define xPortPendSVHandler PendSV_Handler
#define vPortSVCHandler SVC_Handler
#endif
|
FreeRTOS
FreeRTOS高优先级优先,具有优先级反转机制,注意config里的总优先级不能低于4。
Ucos和RT-Thread是低优先级优先

通信路径表

运行效果
Xmodem工具使用xcom(这东西老是丢包,下载操作还有BUG,丢包了再下载进去的是错的,以后再修改吧)
在简易模式和多功能模式均可以调整OTA_flag;若为0,可以进行OTA更新,若为1,则自动进入系统。
本实验通过【5】向外部FLASH的第1块下载简易模式(MODE=0),第2块下载简易+多功能模式(MODE=1)
后续借助【7】重启、PB10重启和【6】下载外部FLASH等功能,对第1块和第2块外部Flash片区内容进行实验测试,均符合预期效果。
【1】擦除和【2】下载,测得没有问题;【3】和【4】也是测试通过。

总结
整体代码太冗长了,这里就只把核心部分贴上去了。
还有,记得调整两个0x5000,不然程序是运行不了的。