modmachine.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2015 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "modmachine.h"
  29. #include "py/gc.h"
  30. #include "py/runtime.h"
  31. #include "py/mphal.h"
  32. #include "extmod/machine_mem.h"
  33. #include "extmod/machine_signal.h"
  34. #include "extmod/machine_pulse.h"
  35. #include "extmod/machine_i2c.h"
  36. #include "lib/utils/pyexec.h"
  37. #include "lib/oofatfs/ff.h"
  38. #include "extmod/vfs.h"
  39. #include "extmod/vfs_fat.h"
  40. #include "gccollect.h"
  41. #include "irq.h"
  42. #include "pybthread.h"
  43. #include "rng.h"
  44. #include "storage.h"
  45. #include "pin.h"
  46. #include "timer.h"
  47. #include "usb.h"
  48. #include "rtc.h"
  49. #include "i2c.h"
  50. #include "spi.h"
  51. #include "uart.h"
  52. #include "wdt.h"
  53. #include "genhdr/pllfreqtable.h"
  54. #if defined(STM32L4)
  55. // L4 does not have a POR, so use BOR instead
  56. #define RCC_CSR_PORRSTF RCC_CSR_BORRSTF
  57. #endif
  58. #if defined(STM32H7)
  59. #define RCC_SR RSR
  60. #define RCC_SR_IWDGRSTF RCC_RSR_IWDG1RSTF
  61. #define RCC_SR_WWDGRSTF RCC_RSR_WWDG1RSTF
  62. #define RCC_SR_PORRSTF RCC_RSR_PORRSTF
  63. #define RCC_SR_BORRSTF RCC_RSR_BORRSTF
  64. #define RCC_SR_PINRSTF RCC_RSR_PINRSTF
  65. #define RCC_SR_RMVF RCC_RSR_RMVF
  66. #else
  67. #define RCC_SR CSR
  68. #define RCC_SR_IWDGRSTF RCC_CSR_IWDGRSTF
  69. #define RCC_SR_WWDGRSTF RCC_CSR_WWDGRSTF
  70. #define RCC_SR_PORRSTF RCC_CSR_PORRSTF
  71. #define RCC_SR_BORRSTF RCC_CSR_BORRSTF
  72. #define RCC_SR_PINRSTF RCC_CSR_PINRSTF
  73. #define RCC_SR_RMVF RCC_CSR_RMVF
  74. #endif
  75. #define PYB_RESET_SOFT (0)
  76. #define PYB_RESET_POWER_ON (1)
  77. #define PYB_RESET_HARD (2)
  78. #define PYB_RESET_WDT (3)
  79. #define PYB_RESET_DEEPSLEEP (4)
  80. STATIC uint32_t reset_cause;
  81. void machine_init(void) {
  82. #if defined(STM32F4)
  83. if (PWR->CSR & PWR_CSR_SBF) {
  84. // came out of standby
  85. reset_cause = PYB_RESET_DEEPSLEEP;
  86. PWR->CR |= PWR_CR_CSBF;
  87. } else
  88. #elif defined(STM32F7)
  89. if (PWR->CSR1 & PWR_CSR1_SBF) {
  90. // came out of standby
  91. reset_cause = PYB_RESET_DEEPSLEEP;
  92. PWR->CR1 |= PWR_CR1_CSBF;
  93. } else
  94. #elif defined(STM32H7)
  95. if (PWR->CPUCR & PWR_CPUCR_SBF || PWR->CPUCR & PWR_CPUCR_STOPF) {
  96. // came out of standby or stop mode
  97. reset_cause = PYB_RESET_DEEPSLEEP;
  98. PWR->CPUCR |= PWR_CPUCR_CSSF;
  99. } else
  100. #endif
  101. {
  102. // get reset cause from RCC flags
  103. uint32_t state = RCC->RCC_SR;
  104. if (state & RCC_SR_IWDGRSTF || state & RCC_SR_WWDGRSTF) {
  105. reset_cause = PYB_RESET_WDT;
  106. } else if (state & RCC_SR_PORRSTF
  107. #if !defined(STM32F0)
  108. || state & RCC_SR_BORRSTF
  109. #endif
  110. ) {
  111. reset_cause = PYB_RESET_POWER_ON;
  112. } else if (state & RCC_SR_PINRSTF) {
  113. reset_cause = PYB_RESET_HARD;
  114. } else {
  115. // default is soft reset
  116. reset_cause = PYB_RESET_SOFT;
  117. }
  118. }
  119. // clear RCC reset flags
  120. RCC->RCC_SR |= RCC_SR_RMVF;
  121. }
  122. void machine_deinit(void) {
  123. // we are doing a soft-reset so change the reset_cause
  124. reset_cause = PYB_RESET_SOFT;
  125. }
  126. // machine.info([dump_alloc_table])
  127. // Print out lots of information about the board.
  128. STATIC mp_obj_t machine_info(size_t n_args, const mp_obj_t *args) {
  129. // get and print unique id; 96 bits
  130. {
  131. byte *id = (byte*)MP_HAL_UNIQUE_ID_ADDRESS;
  132. printf("ID=%02x%02x%02x%02x:%02x%02x%02x%02x:%02x%02x%02x%02x\n", id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7], id[8], id[9], id[10], id[11]);
  133. }
  134. // get and print clock speeds
  135. // SYSCLK=168MHz, HCLK=168MHz, PCLK1=42MHz, PCLK2=84MHz
  136. {
  137. #if defined(STM32F0)
  138. printf("S=%u\nH=%u\nP1=%u\n",
  139. (unsigned int)HAL_RCC_GetSysClockFreq(),
  140. (unsigned int)HAL_RCC_GetHCLKFreq(),
  141. (unsigned int)HAL_RCC_GetPCLK1Freq());
  142. #else
  143. printf("S=%u\nH=%u\nP1=%u\nP2=%u\n",
  144. (unsigned int)HAL_RCC_GetSysClockFreq(),
  145. (unsigned int)HAL_RCC_GetHCLKFreq(),
  146. (unsigned int)HAL_RCC_GetPCLK1Freq(),
  147. (unsigned int)HAL_RCC_GetPCLK2Freq());
  148. #endif
  149. }
  150. // to print info about memory
  151. {
  152. printf("_etext=%p\n", &_etext);
  153. printf("_sidata=%p\n", &_sidata);
  154. printf("_sdata=%p\n", &_sdata);
  155. printf("_edata=%p\n", &_edata);
  156. printf("_sbss=%p\n", &_sbss);
  157. printf("_ebss=%p\n", &_ebss);
  158. printf("_estack=%p\n", &_estack);
  159. printf("_ram_start=%p\n", &_ram_start);
  160. printf("_heap_start=%p\n", &_heap_start);
  161. printf("_heap_end=%p\n", &_heap_end);
  162. printf("_ram_end=%p\n", &_ram_end);
  163. }
  164. // qstr info
  165. {
  166. size_t n_pool, n_qstr, n_str_data_bytes, n_total_bytes;
  167. qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes);
  168. printf("qstr:\n n_pool=%u\n n_qstr=%u\n n_str_data_bytes=%u\n n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes);
  169. }
  170. // GC info
  171. {
  172. gc_info_t info;
  173. gc_info(&info);
  174. printf("GC:\n");
  175. printf(" %u total\n", info.total);
  176. printf(" %u : %u\n", info.used, info.free);
  177. printf(" 1=%u 2=%u m=%u\n", info.num_1block, info.num_2block, info.max_block);
  178. }
  179. // free space on flash
  180. {
  181. #if MICROPY_VFS_FAT
  182. for (mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); vfs != NULL; vfs = vfs->next) {
  183. if (strncmp("/flash", vfs->str, vfs->len) == 0) {
  184. // assumes that it's a FatFs filesystem
  185. fs_user_mount_t *vfs_fat = MP_OBJ_TO_PTR(vfs->obj);
  186. DWORD nclst;
  187. f_getfree(&vfs_fat->fatfs, &nclst);
  188. printf("LFS free: %u bytes\n", (uint)(nclst * vfs_fat->fatfs.csize * 512));
  189. break;
  190. }
  191. }
  192. #endif
  193. }
  194. #if MICROPY_PY_THREAD
  195. pyb_thread_dump();
  196. #endif
  197. if (n_args == 1) {
  198. // arg given means dump gc allocation table
  199. gc_dump_alloc_table();
  200. }
  201. return mp_const_none;
  202. }
  203. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj, 0, 1, machine_info);
  204. // Returns a string of 12 bytes (96 bits), which is the unique ID for the MCU.
  205. STATIC mp_obj_t machine_unique_id(void) {
  206. byte *id = (byte*)MP_HAL_UNIQUE_ID_ADDRESS;
  207. return mp_obj_new_bytes(id, 12);
  208. }
  209. MP_DEFINE_CONST_FUN_OBJ_0(machine_unique_id_obj, machine_unique_id);
  210. // Resets the pyboard in a manner similar to pushing the external RESET button.
  211. STATIC mp_obj_t machine_reset(void) {
  212. NVIC_SystemReset();
  213. return mp_const_none;
  214. }
  215. MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset);
  216. STATIC mp_obj_t machine_soft_reset(void) {
  217. pyexec_system_exit = PYEXEC_FORCED_EXIT;
  218. nlr_raise(mp_obj_new_exception(&mp_type_SystemExit));
  219. }
  220. MP_DEFINE_CONST_FUN_OBJ_0(machine_soft_reset_obj, machine_soft_reset);
  221. // Activate the bootloader without BOOT* pins.
  222. STATIC NORETURN mp_obj_t machine_bootloader(void) {
  223. #if MICROPY_HW_ENABLE_USB
  224. pyb_usb_dev_deinit();
  225. #endif
  226. #if MICROPY_HW_ENABLE_STORAGE
  227. storage_flush();
  228. #endif
  229. HAL_RCC_DeInit();
  230. HAL_DeInit();
  231. #if (__MPU_PRESENT == 1)
  232. // MPU must be disabled for bootloader to function correctly
  233. HAL_MPU_Disable();
  234. #endif
  235. #if defined(STM32F7) || defined(STM32H7)
  236. // arm-none-eabi-gcc 4.9.0 does not correctly inline this
  237. // MSP function, so we write it out explicitly here.
  238. //__set_MSP(*((uint32_t*) 0x1FF00000));
  239. __ASM volatile ("movw r3, #0x0000\nmovt r3, #0x1FF0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
  240. ((void (*)(void)) *((uint32_t*) 0x1FF00004))();
  241. #else
  242. __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH();
  243. // arm-none-eabi-gcc 4.9.0 does not correctly inline this
  244. // MSP function, so we write it out explicitly here.
  245. //__set_MSP(*((uint32_t*) 0x00000000));
  246. __ASM volatile ("movs r3, #0\nldr r3, [r3, #0]\nMSR msp, r3\n" : : : "r3", "sp");
  247. ((void (*)(void)) *((uint32_t*) 0x00000004))();
  248. #endif
  249. while (1);
  250. }
  251. MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader);
  252. #if !(defined(STM32F0) || defined(STM32L4))
  253. // get or set the MCU frequencies
  254. STATIC mp_uint_t machine_freq_calc_ahb_div(mp_uint_t wanted_div) {
  255. if (wanted_div <= 1) { return RCC_SYSCLK_DIV1; }
  256. else if (wanted_div <= 2) { return RCC_SYSCLK_DIV2; }
  257. else if (wanted_div <= 4) { return RCC_SYSCLK_DIV4; }
  258. else if (wanted_div <= 8) { return RCC_SYSCLK_DIV8; }
  259. else if (wanted_div <= 16) { return RCC_SYSCLK_DIV16; }
  260. else if (wanted_div <= 64) { return RCC_SYSCLK_DIV64; }
  261. else if (wanted_div <= 128) { return RCC_SYSCLK_DIV128; }
  262. else if (wanted_div <= 256) { return RCC_SYSCLK_DIV256; }
  263. else { return RCC_SYSCLK_DIV512; }
  264. }
  265. STATIC mp_uint_t machine_freq_calc_apb_div(mp_uint_t wanted_div) {
  266. if (wanted_div <= 1) { return RCC_HCLK_DIV1; }
  267. else if (wanted_div <= 2) { return RCC_HCLK_DIV2; }
  268. else if (wanted_div <= 4) { return RCC_HCLK_DIV4; }
  269. else if (wanted_div <= 8) { return RCC_HCLK_DIV8; }
  270. else { return RCC_SYSCLK_DIV16; }
  271. }
  272. #endif
  273. STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
  274. if (n_args == 0) {
  275. // get
  276. mp_obj_t tuple[] = {
  277. mp_obj_new_int(HAL_RCC_GetSysClockFreq()),
  278. mp_obj_new_int(HAL_RCC_GetHCLKFreq()),
  279. mp_obj_new_int(HAL_RCC_GetPCLK1Freq()),
  280. #if !defined(STM32F0)
  281. mp_obj_new_int(HAL_RCC_GetPCLK2Freq()),
  282. #endif
  283. };
  284. return mp_obj_new_tuple(MP_ARRAY_SIZE(tuple), tuple);
  285. } else {
  286. // set
  287. #if defined(STM32F0) || defined(STM32L4)
  288. mp_raise_NotImplementedError("machine.freq set not supported yet");
  289. #else
  290. mp_int_t wanted_sysclk = mp_obj_get_int(args[0]) / 1000000;
  291. // default PLL parameters that give 48MHz on PLL48CK
  292. uint32_t m = HSE_VALUE / 1000000, n = 336, p = 2, q = 7;
  293. uint32_t sysclk_source;
  294. // search for a valid PLL configuration that keeps USB at 48MHz
  295. for (const uint16_t *pll = &pll_freq_table[MP_ARRAY_SIZE(pll_freq_table) - 1]; pll >= &pll_freq_table[0]; --pll) {
  296. uint32_t sys = *pll & 0xff;
  297. if (sys <= wanted_sysclk) {
  298. m = (*pll >> 10) & 0x3f;
  299. p = ((*pll >> 7) & 0x6) + 2;
  300. if (m == 0) {
  301. // special entry for using HSI directly
  302. sysclk_source = RCC_SYSCLKSOURCE_HSI;
  303. goto set_clk;
  304. } else if (m == 1) {
  305. // special entry for using HSE directly
  306. sysclk_source = RCC_SYSCLKSOURCE_HSE;
  307. goto set_clk;
  308. } else {
  309. // use PLL
  310. sysclk_source = RCC_SYSCLKSOURCE_PLLCLK;
  311. uint32_t vco_out = sys * p;
  312. n = vco_out * m / (HSE_VALUE / 1000000);
  313. q = vco_out / 48;
  314. goto set_clk;
  315. }
  316. }
  317. }
  318. mp_raise_ValueError("can't make valid freq");
  319. set_clk:
  320. //printf("%lu %lu %lu %lu %lu\n", sysclk_source, m, n, p, q);
  321. // let the USB CDC have a chance to process before we change the clock
  322. mp_hal_delay_ms(5);
  323. // desired system clock source is in sysclk_source
  324. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  325. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  326. if (sysclk_source == RCC_SYSCLKSOURCE_PLLCLK) {
  327. // set HSE as system clock source to allow modification of the PLL configuration
  328. // we then change to PLL after re-configuring PLL
  329. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  330. } else {
  331. // directly set the system clock source as desired
  332. RCC_ClkInitStruct.SYSCLKSource = sysclk_source;
  333. }
  334. wanted_sysclk *= 1000000;
  335. if (n_args >= 2) {
  336. // note: AHB freq required to be >= 14.2MHz for USB operation
  337. RCC_ClkInitStruct.AHBCLKDivider = machine_freq_calc_ahb_div(wanted_sysclk / mp_obj_get_int(args[1]));
  338. } else {
  339. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  340. }
  341. if (n_args >= 3) {
  342. RCC_ClkInitStruct.APB1CLKDivider = machine_freq_calc_apb_div(wanted_sysclk / mp_obj_get_int(args[2]));
  343. } else {
  344. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  345. }
  346. if (n_args >= 4) {
  347. RCC_ClkInitStruct.APB2CLKDivider = machine_freq_calc_apb_div(wanted_sysclk / mp_obj_get_int(args[3]));
  348. } else {
  349. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  350. }
  351. #if defined(MICROPY_HW_CLK_LAST_FREQ) && MICROPY_HW_CLK_LAST_FREQ
  352. uint32_t h = RCC_ClkInitStruct.AHBCLKDivider >> 4;
  353. uint32_t b1 = RCC_ClkInitStruct.APB1CLKDivider >> 10;
  354. uint32_t b2 = RCC_ClkInitStruct.APB2CLKDivider >> 10;
  355. #endif
  356. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
  357. goto fail;
  358. }
  359. // re-configure PLL
  360. // even if we don't use the PLL for the system clock, we still need it for USB, RNG and SDIO
  361. RCC_OscInitTypeDef RCC_OscInitStruct;
  362. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  363. RCC_OscInitStruct.HSEState = MICROPY_HW_CLK_HSE_STATE;
  364. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  365. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  366. RCC_OscInitStruct.PLL.PLLM = m;
  367. RCC_OscInitStruct.PLL.PLLN = n;
  368. RCC_OscInitStruct.PLL.PLLP = p;
  369. RCC_OscInitStruct.PLL.PLLQ = q;
  370. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  371. goto fail;
  372. }
  373. // set PLL as system clock source if wanted
  374. if (sysclk_source == RCC_SYSCLKSOURCE_PLLCLK) {
  375. uint32_t flash_latency;
  376. #if defined(STM32F7)
  377. // if possible, scale down the internal voltage regulator to save power
  378. // the flash_latency values assume a supply voltage between 2.7V and 3.6V
  379. uint32_t volt_scale;
  380. if (wanted_sysclk <= 90000000) {
  381. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE3;
  382. flash_latency = FLASH_LATENCY_2;
  383. } else if (wanted_sysclk <= 120000000) {
  384. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE3;
  385. flash_latency = FLASH_LATENCY_3;
  386. } else if (wanted_sysclk <= 144000000) {
  387. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE3;
  388. flash_latency = FLASH_LATENCY_4;
  389. } else if (wanted_sysclk <= 180000000) {
  390. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE2;
  391. flash_latency = FLASH_LATENCY_5;
  392. } else if (wanted_sysclk <= 210000000) {
  393. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE1;
  394. flash_latency = FLASH_LATENCY_6;
  395. } else {
  396. volt_scale = PWR_REGULATOR_VOLTAGE_SCALE1;
  397. flash_latency = FLASH_LATENCY_7;
  398. }
  399. if (HAL_PWREx_ControlVoltageScaling(volt_scale) != HAL_OK) {
  400. goto fail;
  401. }
  402. #endif
  403. #if !defined(STM32F7)
  404. #if !defined(MICROPY_HW_FLASH_LATENCY)
  405. #define MICROPY_HW_FLASH_LATENCY FLASH_LATENCY_5
  406. #endif
  407. flash_latency = MICROPY_HW_FLASH_LATENCY;
  408. #endif
  409. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  410. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  411. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency) != HAL_OK) {
  412. goto fail;
  413. }
  414. }
  415. #if defined(MICROPY_HW_CLK_LAST_FREQ) && MICROPY_HW_CLK_LAST_FREQ
  416. #if defined(STM32F7)
  417. #define FREQ_BKP BKP31R
  418. #else
  419. #define FREQ_BKP BKP19R
  420. #endif
  421. // qqqqqqqq pppppppp nnnnnnnn nnmmmmmm
  422. // qqqqQQQQ ppppppPP nNNNNNNN NNMMMMMM
  423. // 222111HH HHQQQQPP nNNNNNNN NNMMMMMM
  424. p = (p / 2) - 1;
  425. RTC->FREQ_BKP = m
  426. | (n << 6) | (p << 16) | (q << 18)
  427. | (h << 22)
  428. | (b1 << 26)
  429. | (b2 << 29);
  430. #endif
  431. return mp_const_none;
  432. fail:;
  433. void NORETURN __fatal_error(const char *msg);
  434. __fatal_error("can't change freq");
  435. #endif
  436. }
  437. }
  438. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_freq_obj, 0, 4, machine_freq);
  439. STATIC mp_obj_t machine_sleep(void) {
  440. #if defined(STM32L4)
  441. // Enter Stop 1 mode
  442. __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI);
  443. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  444. // reconfigure system clock after wakeup
  445. // Enable Power Control clock
  446. __HAL_RCC_PWR_CLK_ENABLE();
  447. // Get the Oscillators configuration according to the internal RCC registers
  448. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  449. HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
  450. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  451. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  452. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  453. // Get the Clocks configuration according to the internal RCC registers
  454. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  455. uint32_t pFLatency = 0;
  456. HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
  457. // Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clock dividers
  458. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  459. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  460. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
  461. #else
  462. #if !defined(STM32F0)
  463. // takes longer to wake but reduces stop current
  464. HAL_PWREx_EnableFlashPowerDown();
  465. #endif
  466. # if defined(STM32F7)
  467. HAL_PWR_EnterSTOPMode((PWR_CR1_LPDS | PWR_CR1_LPUDS | PWR_CR1_FPDS | PWR_CR1_UDEN), PWR_STOPENTRY_WFI);
  468. # else
  469. HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  470. #endif
  471. // reconfigure the system clock after waking up
  472. // enable HSE
  473. __HAL_RCC_HSE_CONFIG(MICROPY_HW_CLK_HSE_STATE);
  474. while (!__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY)) {
  475. }
  476. // enable PLL
  477. __HAL_RCC_PLL_ENABLE();
  478. while (!__HAL_RCC_GET_FLAG(RCC_FLAG_PLLRDY)) {
  479. }
  480. // select PLL as system clock source
  481. MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_SYSCLKSOURCE_PLLCLK);
  482. #if defined(STM32H7)
  483. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL1) {
  484. #else
  485. while (__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL) {
  486. #endif
  487. }
  488. #endif
  489. return mp_const_none;
  490. }
  491. MP_DEFINE_CONST_FUN_OBJ_0(machine_sleep_obj, machine_sleep);
  492. STATIC mp_obj_t machine_deepsleep(void) {
  493. rtc_init_finalise();
  494. #if defined(STM32L4)
  495. printf("machine.deepsleep not supported yet\n");
  496. #else
  497. // We need to clear the PWR wake-up-flag before entering standby, since
  498. // the flag may have been set by a previous wake-up event. Furthermore,
  499. // we need to disable the wake-up sources while clearing this flag, so
  500. // that if a source is active it does actually wake the device.
  501. // See section 5.3.7 of RM0090.
  502. // Note: we only support RTC ALRA, ALRB, WUT and TS.
  503. // TODO support TAMP and WKUP (PA0 external pin).
  504. #if defined(STM32F0)
  505. #define CR_BITS (RTC_CR_ALRAIE | RTC_CR_WUTIE | RTC_CR_TSIE)
  506. #define ISR_BITS (RTC_ISR_ALRAF | RTC_ISR_WUTF | RTC_ISR_TSF)
  507. #else
  508. #define CR_BITS (RTC_CR_ALRAIE | RTC_CR_ALRBIE | RTC_CR_WUTIE | RTC_CR_TSIE)
  509. #define ISR_BITS (RTC_ISR_ALRAF | RTC_ISR_ALRBF | RTC_ISR_WUTF | RTC_ISR_TSF)
  510. #endif
  511. // save RTC interrupts
  512. uint32_t save_irq_bits = RTC->CR & CR_BITS;
  513. // disable RTC interrupts
  514. RTC->CR &= ~CR_BITS;
  515. // clear RTC wake-up flags
  516. RTC->ISR &= ~ISR_BITS;
  517. #if defined(STM32F7)
  518. // disable wake-up flags
  519. PWR->CSR2 &= ~(PWR_CSR2_EWUP6 | PWR_CSR2_EWUP5 | PWR_CSR2_EWUP4 | PWR_CSR2_EWUP3 | PWR_CSR2_EWUP2 | PWR_CSR2_EWUP1);
  520. // clear global wake-up flag
  521. PWR->CR2 |= PWR_CR2_CWUPF6 | PWR_CR2_CWUPF5 | PWR_CR2_CWUPF4 | PWR_CR2_CWUPF3 | PWR_CR2_CWUPF2 | PWR_CR2_CWUPF1;
  522. #elif defined(STM32H7)
  523. // TODO
  524. #else
  525. // clear global wake-up flag
  526. PWR->CR |= PWR_CR_CWUF;
  527. #endif
  528. // enable previously-enabled RTC interrupts
  529. RTC->CR |= save_irq_bits;
  530. // enter standby mode
  531. HAL_PWR_EnterSTANDBYMode();
  532. // we never return; MCU is reset on exit from standby
  533. #endif
  534. return mp_const_none;
  535. }
  536. MP_DEFINE_CONST_FUN_OBJ_0(machine_deepsleep_obj, machine_deepsleep);
  537. STATIC mp_obj_t machine_reset_cause(void) {
  538. return MP_OBJ_NEW_SMALL_INT(reset_cause);
  539. }
  540. STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause);
  541. STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
  542. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) },
  543. { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&machine_info_obj) },
  544. { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) },
  545. { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) },
  546. { MP_ROM_QSTR(MP_QSTR_soft_reset), MP_ROM_PTR(&machine_soft_reset_obj) },
  547. { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) },
  548. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) },
  549. #if MICROPY_HW_ENABLE_RNG
  550. { MP_ROM_QSTR(MP_QSTR_rng), MP_ROM_PTR(&pyb_rng_get_obj) },
  551. #endif
  552. { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&pyb_wfi_obj) },
  553. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&machine_sleep_obj) },
  554. { MP_ROM_QSTR(MP_QSTR_deepsleep), MP_ROM_PTR(&machine_deepsleep_obj) },
  555. { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) },
  556. #if 0
  557. { MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) },
  558. #endif
  559. { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&pyb_disable_irq_obj) },
  560. { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&pyb_enable_irq_obj) },
  561. { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
  562. { MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },
  563. { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) },
  564. { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) },
  565. { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pin_type) },
  566. { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) },
  567. #if 0
  568. { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) },
  569. { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&pyb_adc_type) },
  570. #endif
  571. #if MICROPY_PY_MACHINE_I2C
  572. { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) },
  573. #endif
  574. { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&machine_hard_spi_type) },
  575. { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) },
  576. { MP_ROM_QSTR(MP_QSTR_WDT), MP_ROM_PTR(&pyb_wdt_type) },
  577. #if 0
  578. { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&pyb_timer_type) },
  579. { MP_ROM_QSTR(MP_QSTR_HeartBeat), MP_ROM_PTR(&pyb_heartbeat_type) },
  580. { MP_ROM_QSTR(MP_QSTR_SD), MP_ROM_PTR(&pyb_sd_type) },
  581. // class constants
  582. { MP_ROM_QSTR(MP_QSTR_IDLE), MP_ROM_INT(PYB_PWR_MODE_ACTIVE) },
  583. { MP_ROM_QSTR(MP_QSTR_SLEEP), MP_ROM_INT(PYB_PWR_MODE_LPDS) },
  584. { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP), MP_ROM_INT(PYB_PWR_MODE_HIBERNATE) },
  585. #endif
  586. { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_ROM_INT(PYB_RESET_POWER_ON) },
  587. { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_ROM_INT(PYB_RESET_HARD) },
  588. { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_ROM_INT(PYB_RESET_WDT) },
  589. { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_ROM_INT(PYB_RESET_DEEPSLEEP) },
  590. { MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_ROM_INT(PYB_RESET_SOFT) },
  591. #if 0
  592. { MP_ROM_QSTR(MP_QSTR_WLAN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_WLAN) },
  593. { MP_ROM_QSTR(MP_QSTR_PIN_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_GPIO) },
  594. { MP_ROM_QSTR(MP_QSTR_RTC_WAKE), MP_ROM_INT(PYB_SLP_WAKED_BY_RTC) },
  595. #endif
  596. };
  597. STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
  598. const mp_obj_module_t machine_module = {
  599. .base = { &mp_type_module },
  600. .globals = (mp_obj_dict_t*)&machine_module_globals,
  601. };