mpconfigport.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2017 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. // Options to control how MicroPython is built for this port,
  27. // overriding defaults in py/mpconfig.h.
  28. // board specific definitions
  29. #include "mpconfigboard.h"
  30. #include "mpconfigboard_common.h"
  31. // memory allocation policies
  32. #define MICROPY_ALLOC_PATH_MAX (128)
  33. // emitters
  34. #define MICROPY_PERSISTENT_CODE_LOAD (1)
  35. #ifndef MICROPY_EMIT_THUMB
  36. #define MICROPY_EMIT_THUMB (1)
  37. #endif
  38. #ifndef MICROPY_EMIT_INLINE_THUMB
  39. #define MICROPY_EMIT_INLINE_THUMB (1)
  40. #endif
  41. // compiler configuration
  42. #define MICROPY_COMP_MODULE_CONST (1)
  43. #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (1)
  44. #define MICROPY_COMP_RETURN_IF_EXPR (1)
  45. // optimisations
  46. #define MICROPY_OPT_COMPUTED_GOTO (1)
  47. #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (0)
  48. #define MICROPY_OPT_MPZ_BITWISE (1)
  49. // Python internal features
  50. #define MICROPY_READER_VFS (1)
  51. #define MICROPY_ENABLE_GC (1)
  52. #define MICROPY_ENABLE_FINALISER (1)
  53. #define MICROPY_STACK_CHECK (1)
  54. #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
  55. #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
  56. #define MICROPY_KBD_EXCEPTION (1)
  57. #define MICROPY_HELPER_REPL (1)
  58. #define MICROPY_REPL_EMACS_KEYS (1)
  59. #define MICROPY_REPL_AUTO_INDENT (1)
  60. #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
  61. #define MICROPY_ENABLE_SOURCE_LINE (1)
  62. #ifndef MICROPY_FLOAT_IMPL // can be configured by each board via mpconfigboard.mk
  63. #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
  64. #endif
  65. #define MICROPY_STREAMS_NON_BLOCK (1)
  66. #define MICROPY_MODULE_WEAK_LINKS (1)
  67. #define MICROPY_CAN_OVERRIDE_BUILTINS (1)
  68. #define MICROPY_USE_INTERNAL_ERRNO (1)
  69. #define MICROPY_ENABLE_SCHEDULER (1)
  70. #define MICROPY_SCHEDULER_DEPTH (8)
  71. #define MICROPY_VFS (1)
  72. #ifndef MICROPY_VFS_FAT
  73. #define MICROPY_VFS_FAT (1)
  74. #endif
  75. // control over Python builtins
  76. #define MICROPY_PY_FUNCTION_ATTRS (1)
  77. #define MICROPY_PY_DESCRIPTORS (1)
  78. #define MICROPY_PY_DELATTR_SETATTR (1)
  79. #define MICROPY_PY_BUILTINS_STR_UNICODE (1)
  80. #define MICROPY_PY_BUILTINS_STR_CENTER (1)
  81. #define MICROPY_PY_BUILTINS_STR_PARTITION (1)
  82. #define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
  83. #define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
  84. #define MICROPY_PY_BUILTINS_FROZENSET (1)
  85. #define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
  86. #define MICROPY_PY_BUILTINS_ROUND_INT (1)
  87. #define MICROPY_PY_ALL_SPECIAL_METHODS (1)
  88. #define MICROPY_PY_BUILTINS_COMPILE (1)
  89. #define MICROPY_PY_BUILTINS_EXECFILE (1)
  90. #define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
  91. #define MICROPY_PY_BUILTINS_INPUT (1)
  92. #define MICROPY_PY_BUILTINS_POW3 (1)
  93. #define MICROPY_PY_BUILTINS_HELP (1)
  94. #define MICROPY_PY_BUILTINS_HELP_TEXT stm32_help_text
  95. #define MICROPY_PY_BUILTINS_HELP_MODULES (1)
  96. #define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
  97. #define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
  98. #define MICROPY_PY_COLLECTIONS_DEQUE (1)
  99. #define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
  100. #define MICROPY_PY_MATH_SPECIAL_FUNCTIONS (1)
  101. #define MICROPY_PY_CMATH (1)
  102. #define MICROPY_PY_IO (1)
  103. #define MICROPY_PY_IO_IOBASE (1)
  104. #define MICROPY_PY_IO_FILEIO (MICROPY_VFS_FAT) // because mp_type_fileio/textio point to fatfs impl
  105. #define MICROPY_PY_SYS_MAXSIZE (1)
  106. #define MICROPY_PY_SYS_EXIT (1)
  107. #define MICROPY_PY_SYS_STDFILES (1)
  108. #define MICROPY_PY_SYS_STDIO_BUFFER (1)
  109. #ifndef MICROPY_PY_SYS_PLATFORM // let boards override it if they want
  110. #define MICROPY_PY_SYS_PLATFORM "pyboard"
  111. #endif
  112. #define MICROPY_PY_UERRNO (1)
  113. #ifndef MICROPY_PY_THREAD
  114. #define MICROPY_PY_THREAD (0)
  115. #endif
  116. // extended modules
  117. #define MICROPY_PY_UCTYPES (1)
  118. #define MICROPY_PY_UZLIB (1)
  119. #define MICROPY_PY_UJSON (1)
  120. #define MICROPY_PY_URE (1)
  121. #define MICROPY_PY_URE_SUB (1)
  122. #define MICROPY_PY_UHEAPQ (1)
  123. #define MICROPY_PY_UHASHLIB (1)
  124. #define MICROPY_PY_UBINASCII (1)
  125. #define MICROPY_PY_URANDOM (1)
  126. #define MICROPY_PY_URANDOM_EXTRA_FUNCS (1)
  127. #define MICROPY_PY_USELECT (1)
  128. #define MICROPY_PY_UTIMEQ (1)
  129. #define MICROPY_PY_UTIME_MP_HAL (1)
  130. #define MICROPY_PY_OS_DUPTERM (1)
  131. #define MICROPY_PY_MACHINE (1)
  132. #define MICROPY_PY_MACHINE_PULSE (1)
  133. #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
  134. #define MICROPY_PY_MACHINE_I2C (1)
  135. #if MICROPY_HW_ENABLE_HW_I2C
  136. #define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new
  137. #endif
  138. #define MICROPY_PY_MACHINE_SPI (1)
  139. #define MICROPY_PY_MACHINE_SPI_MSB (SPI_FIRSTBIT_MSB)
  140. #define MICROPY_PY_MACHINE_SPI_LSB (SPI_FIRSTBIT_LSB)
  141. #define MICROPY_PY_MACHINE_SPI_MAKE_NEW machine_hard_spi_make_new
  142. #define MICROPY_HW_SOFTSPI_MIN_DELAY (0)
  143. #define MICROPY_HW_SOFTSPI_MAX_BAUDRATE (HAL_RCC_GetSysClockFreq() / 48)
  144. #define MICROPY_PY_FRAMEBUF (1)
  145. #ifndef MICROPY_PY_USOCKET
  146. #define MICROPY_PY_USOCKET (1)
  147. #endif
  148. #ifndef MICROPY_PY_NETWORK
  149. #define MICROPY_PY_NETWORK (1)
  150. #endif
  151. // fatfs configuration used in ffconf.h
  152. #define MICROPY_FATFS_ENABLE_LFN (1)
  153. #define MICROPY_FATFS_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
  154. #define MICROPY_FATFS_USE_LABEL (1)
  155. #define MICROPY_FATFS_RPATH (2)
  156. #define MICROPY_FATFS_MULTI_PARTITION (1)
  157. // TODO these should be generic, not bound to fatfs
  158. #define mp_type_fileio mp_type_vfs_fat_fileio
  159. #define mp_type_textio mp_type_vfs_fat_textio
  160. // use vfs's functions for import stat and builtin open
  161. #define mp_import_stat mp_vfs_import_stat
  162. #define mp_builtin_open mp_vfs_open
  163. #define mp_builtin_open_obj mp_vfs_open_obj
  164. // extra built in names to add to the global namespace
  165. #define MICROPY_PORT_BUILTINS \
  166. { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
  167. // extra built in modules to add to the list of known ones
  168. extern const struct _mp_obj_module_t machine_module;
  169. extern const struct _mp_obj_module_t pyb_module;
  170. extern const struct _mp_obj_module_t stm_module;
  171. extern const struct _mp_obj_module_t mp_module_ubinascii;
  172. extern const struct _mp_obj_module_t mp_module_ure;
  173. extern const struct _mp_obj_module_t mp_module_uzlib;
  174. extern const struct _mp_obj_module_t mp_module_ujson;
  175. extern const struct _mp_obj_module_t mp_module_uheapq;
  176. extern const struct _mp_obj_module_t mp_module_uhashlib;
  177. extern const struct _mp_obj_module_t mp_module_uos;
  178. extern const struct _mp_obj_module_t mp_module_utime;
  179. extern const struct _mp_obj_module_t mp_module_usocket;
  180. extern const struct _mp_obj_module_t mp_module_network;
  181. extern const struct _mp_obj_module_t mp_module_onewire;
  182. #if MICROPY_PY_STM
  183. #define STM_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_stm), MP_ROM_PTR(&stm_module) },
  184. #else
  185. #define STM_BUILTIN_MODULE
  186. #endif
  187. #if MICROPY_PY_USOCKET && MICROPY_PY_LWIP
  188. // usocket implementation provided by lwIP
  189. #define SOCKET_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_usocket), MP_ROM_PTR(&mp_module_lwip) },
  190. #define SOCKET_BUILTIN_MODULE_WEAK_LINKS { MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_module_lwip) },
  191. #define SOCKET_POLL extern void pyb_lwip_poll(void); pyb_lwip_poll();
  192. #elif MICROPY_PY_USOCKET
  193. // usocket implementation provided by skeleton wrapper
  194. #define SOCKET_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_usocket), MP_ROM_PTR(&mp_module_usocket) },
  195. #define SOCKET_BUILTIN_MODULE_WEAK_LINKS { MP_ROM_QSTR(MP_QSTR_socket), MP_ROM_PTR(&mp_module_usocket) },
  196. #define SOCKET_POLL
  197. #else
  198. // no usocket module
  199. #define SOCKET_BUILTIN_MODULE
  200. #define SOCKET_BUILTIN_MODULE_WEAK_LINKS
  201. #define SOCKET_POLL
  202. #endif
  203. #if MICROPY_PY_NETWORK
  204. #define NETWORK_BUILTIN_MODULE { MP_ROM_QSTR(MP_QSTR_network), MP_ROM_PTR(&mp_module_network) },
  205. #else
  206. #define NETWORK_BUILTIN_MODULE
  207. #endif
  208. #define MICROPY_PORT_BUILTIN_MODULES \
  209. { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&machine_module) }, \
  210. { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, \
  211. STM_BUILTIN_MODULE \
  212. { MP_ROM_QSTR(MP_QSTR_uos), MP_ROM_PTR(&mp_module_uos) }, \
  213. { MP_ROM_QSTR(MP_QSTR_utime), MP_ROM_PTR(&mp_module_utime) }, \
  214. SOCKET_BUILTIN_MODULE \
  215. NETWORK_BUILTIN_MODULE \
  216. { MP_ROM_QSTR(MP_QSTR__onewire), MP_ROM_PTR(&mp_module_onewire) }, \
  217. #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \
  218. { MP_ROM_QSTR(MP_QSTR_binascii), MP_ROM_PTR(&mp_module_ubinascii) }, \
  219. { MP_ROM_QSTR(MP_QSTR_collections), MP_ROM_PTR(&mp_module_collections) }, \
  220. { MP_ROM_QSTR(MP_QSTR_re), MP_ROM_PTR(&mp_module_ure) }, \
  221. { MP_ROM_QSTR(MP_QSTR_zlib), MP_ROM_PTR(&mp_module_uzlib) }, \
  222. { MP_ROM_QSTR(MP_QSTR_json), MP_ROM_PTR(&mp_module_ujson) }, \
  223. { MP_ROM_QSTR(MP_QSTR_heapq), MP_ROM_PTR(&mp_module_uheapq) }, \
  224. { MP_ROM_QSTR(MP_QSTR_hashlib), MP_ROM_PTR(&mp_module_uhashlib) }, \
  225. { MP_ROM_QSTR(MP_QSTR_io), MP_ROM_PTR(&mp_module_io) }, \
  226. { MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&mp_module_uos) }, \
  227. { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mp_module_urandom) }, \
  228. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_module_utime) }, \
  229. { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&mp_module_uselect) }, \
  230. SOCKET_BUILTIN_MODULE_WEAK_LINKS \
  231. { MP_ROM_QSTR(MP_QSTR_struct), MP_ROM_PTR(&mp_module_ustruct) }, \
  232. { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, \
  233. { MP_ROM_QSTR(MP_QSTR_errno), MP_ROM_PTR(&mp_module_uerrno) }, \
  234. // extra constants
  235. #define MICROPY_PORT_CONSTANTS \
  236. { MP_ROM_QSTR(MP_QSTR_umachine), MP_ROM_PTR(&machine_module) }, \
  237. { MP_ROM_QSTR(MP_QSTR_machine), MP_ROM_PTR(&machine_module) }, \
  238. { MP_ROM_QSTR(MP_QSTR_pyb), MP_ROM_PTR(&pyb_module) }, \
  239. STM_BUILTIN_MODULE \
  240. #define MP_STATE_PORT MP_STATE_VM
  241. #define MICROPY_PORT_ROOT_POINTERS \
  242. const char *readline_hist[8]; \
  243. \
  244. mp_obj_t pyb_hid_report_desc; \
  245. \
  246. mp_obj_t pyb_config_main; \
  247. \
  248. mp_obj_t pyb_switch_callback; \
  249. \
  250. mp_obj_t pin_class_mapper; \
  251. mp_obj_t pin_class_map_dict; \
  252. \
  253. mp_obj_t pyb_extint_callback[PYB_EXTI_NUM_VECTORS]; \
  254. \
  255. /* pointers to all Timer objects (if they have been created) */ \
  256. struct _pyb_timer_obj_t *pyb_timer_obj_all[MICROPY_HW_MAX_TIMER]; \
  257. \
  258. /* stdio is repeated on this UART object if it's not null */ \
  259. struct _pyb_uart_obj_t *pyb_stdio_uart; \
  260. \
  261. /* pointers to all UART objects (if they have been created) */ \
  262. struct _pyb_uart_obj_t *pyb_uart_obj_all[MICROPY_HW_MAX_UART]; \
  263. \
  264. /* pointers to all CAN objects (if they have been created) */ \
  265. struct _pyb_can_obj_t *pyb_can_obj_all[2]; \
  266. \
  267. /* list of registered NICs */ \
  268. mp_obj_list_t mod_network_nic_list; \
  269. // type definitions for the specific machine
  270. #define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1))
  271. #define MP_SSIZE_MAX (0x7fffffff)
  272. #define UINT_FMT "%u"
  273. #define INT_FMT "%d"
  274. typedef int mp_int_t; // must be pointer size
  275. typedef unsigned int mp_uint_t; // must be pointer size
  276. typedef long mp_off_t;
  277. #define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)
  278. // We have inlined IRQ functions for efficiency (they are generally
  279. // 1 machine instruction).
  280. //
  281. // Note on IRQ state: you should not need to know the specific
  282. // value of the state variable, but rather just pass the return
  283. // value from disable_irq back to enable_irq. If you really need
  284. // to know the machine-specific values, see irq.h.
  285. static inline void enable_irq(mp_uint_t state) {
  286. __set_PRIMASK(state);
  287. }
  288. static inline mp_uint_t disable_irq(void) {
  289. mp_uint_t state = __get_PRIMASK();
  290. __disable_irq();
  291. return state;
  292. }
  293. #define MICROPY_BEGIN_ATOMIC_SECTION() disable_irq()
  294. #define MICROPY_END_ATOMIC_SECTION(state) enable_irq(state)
  295. #if MICROPY_PY_THREAD
  296. #define MICROPY_EVENT_POLL_HOOK \
  297. do { \
  298. extern void mp_handle_pending(void); \
  299. mp_handle_pending(); \
  300. SOCKET_POLL \
  301. if (pyb_thread_enabled) { \
  302. MP_THREAD_GIL_EXIT(); \
  303. pyb_thread_yield(); \
  304. MP_THREAD_GIL_ENTER(); \
  305. } else { \
  306. __WFI(); \
  307. } \
  308. } while (0);
  309. #define MICROPY_THREAD_YIELD() pyb_thread_yield()
  310. #else
  311. #define MICROPY_EVENT_POLL_HOOK \
  312. do { \
  313. extern void mp_handle_pending(void); \
  314. mp_handle_pending(); \
  315. SOCKET_POLL \
  316. __WFI(); \
  317. } while (0);
  318. #define MICROPY_THREAD_YIELD()
  319. #endif
  320. // We need an implementation of the log2 function which is not a macro
  321. #define MP_NEED_LOG2 (1)
  322. // There is no classical C heap in bare-metal ports, only Python
  323. // garbage-collected heap. For completeness, emulate C heap via
  324. // GC heap. Note that MicroPython core never uses malloc() and friends,
  325. // so these defines are mostly to help extension module writers.
  326. #define malloc(n) m_malloc(n)
  327. #define free(p) m_free(p)
  328. #define realloc(p, n) m_realloc(p, n)
  329. // We need to provide a declaration/definition of alloca()
  330. #include <alloca.h>