main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "py/compile.h"
  5. #include "py/runtime.h"
  6. #include "py/repl.h"
  7. #include "py/gc.h"
  8. #include "py/mperrno.h"
  9. #include "lib/utils/pyexec.h"
  10. #if MICROPY_ENABLE_COMPILER
  11. void do_str(const char *src, mp_parse_input_kind_t input_kind) {
  12. nlr_buf_t nlr;
  13. if (nlr_push(&nlr) == 0) {
  14. mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
  15. qstr source_name = lex->source_name;
  16. mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
  17. mp_obj_t module_fun = mp_compile(&parse_tree, source_name, MP_EMIT_OPT_NONE, true);
  18. mp_call_function_0(module_fun);
  19. nlr_pop();
  20. } else {
  21. // uncaught exception
  22. mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
  23. }
  24. }
  25. #endif
  26. static char *stack_top;
  27. #if MICROPY_ENABLE_GC
  28. static char heap[2048];
  29. #endif
  30. int main(int argc, char **argv) {
  31. int stack_dummy;
  32. stack_top = (char*)&stack_dummy;
  33. #if MICROPY_ENABLE_GC
  34. gc_init(heap, heap + sizeof(heap));
  35. #endif
  36. mp_init();
  37. #if MICROPY_ENABLE_COMPILER
  38. #if MICROPY_REPL_EVENT_DRIVEN
  39. pyexec_event_repl_init();
  40. for (;;) {
  41. int c = mp_hal_stdin_rx_chr();
  42. if (pyexec_event_repl_process_char(c)) {
  43. break;
  44. }
  45. }
  46. #else
  47. pyexec_friendly_repl();
  48. #endif
  49. //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
  50. //do_str("for i in range(10):\r\n print(i)", MP_PARSE_FILE_INPUT);
  51. #else
  52. pyexec_frozen_module("frozentest.py");
  53. #endif
  54. mp_deinit();
  55. return 0;
  56. }
  57. void gc_collect(void) {
  58. // WARNING: This gc_collect implementation doesn't try to get root
  59. // pointers from CPU registers, and thus may function incorrectly.
  60. void *dummy;
  61. gc_collect_start();
  62. gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
  63. gc_collect_end();
  64. gc_dump_info();
  65. }
  66. mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
  67. mp_raise_OSError(MP_ENOENT);
  68. }
  69. mp_import_stat_t mp_import_stat(const char *path) {
  70. return MP_IMPORT_STAT_NO_EXIST;
  71. }
  72. mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
  73. return mp_const_none;
  74. }
  75. MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
  76. void nlr_jump_fail(void *val) {
  77. while (1);
  78. }
  79. void NORETURN __fatal_error(const char *msg) {
  80. while (1);
  81. }
  82. #ifndef NDEBUG
  83. void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
  84. printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
  85. __fatal_error("Assertion failed");
  86. }
  87. #endif
  88. #if MICROPY_MIN_USE_CORTEX_CPU
  89. // this is a minimal IRQ and reset framework for any Cortex-M CPU
  90. extern uint32_t _estack, _sidata, _sdata, _edata, _sbss, _ebss;
  91. void Reset_Handler(void) __attribute__((naked));
  92. void Reset_Handler(void) {
  93. // set stack pointer
  94. __asm volatile ("ldr sp, =_estack");
  95. // copy .data section from flash to RAM
  96. for (uint32_t *src = &_sidata, *dest = &_sdata; dest < &_edata;) {
  97. *dest++ = *src++;
  98. }
  99. // zero out .bss section
  100. for (uint32_t *dest = &_sbss; dest < &_ebss;) {
  101. *dest++ = 0;
  102. }
  103. // jump to board initialisation
  104. void _start(void);
  105. _start();
  106. }
  107. void Default_Handler(void) {
  108. for (;;) {
  109. }
  110. }
  111. const uint32_t isr_vector[] __attribute__((section(".isr_vector"))) = {
  112. (uint32_t)&_estack,
  113. (uint32_t)&Reset_Handler,
  114. (uint32_t)&Default_Handler, // NMI_Handler
  115. (uint32_t)&Default_Handler, // HardFault_Handler
  116. (uint32_t)&Default_Handler, // MemManage_Handler
  117. (uint32_t)&Default_Handler, // BusFault_Handler
  118. (uint32_t)&Default_Handler, // UsageFault_Handler
  119. 0,
  120. 0,
  121. 0,
  122. 0,
  123. (uint32_t)&Default_Handler, // SVC_Handler
  124. (uint32_t)&Default_Handler, // DebugMon_Handler
  125. 0,
  126. (uint32_t)&Default_Handler, // PendSV_Handler
  127. (uint32_t)&Default_Handler, // SysTick_Handler
  128. };
  129. void _start(void) {
  130. // when we get here: stack is initialised, bss is clear, data is copied
  131. // SCB->CCR: enable 8-byte stack alignment for IRQ handlers, in accord with EABI
  132. *((volatile uint32_t*)0xe000ed14) |= 1 << 9;
  133. // initialise the cpu and peripherals
  134. #if MICROPY_MIN_USE_STM32_MCU
  135. void stm32_init(void);
  136. stm32_init();
  137. #endif
  138. // now that we have a basic system up and running we can call main
  139. main(0, NULL);
  140. // we must not return
  141. for (;;) {
  142. }
  143. }
  144. #endif
  145. #if MICROPY_MIN_USE_STM32_MCU
  146. // this is minimal set-up code for an STM32 MCU
  147. typedef struct {
  148. volatile uint32_t CR;
  149. volatile uint32_t PLLCFGR;
  150. volatile uint32_t CFGR;
  151. volatile uint32_t CIR;
  152. uint32_t _1[8];
  153. volatile uint32_t AHB1ENR;
  154. volatile uint32_t AHB2ENR;
  155. volatile uint32_t AHB3ENR;
  156. uint32_t _2;
  157. volatile uint32_t APB1ENR;
  158. volatile uint32_t APB2ENR;
  159. } periph_rcc_t;
  160. typedef struct {
  161. volatile uint32_t MODER;
  162. volatile uint32_t OTYPER;
  163. volatile uint32_t OSPEEDR;
  164. volatile uint32_t PUPDR;
  165. volatile uint32_t IDR;
  166. volatile uint32_t ODR;
  167. volatile uint16_t BSRRL;
  168. volatile uint16_t BSRRH;
  169. volatile uint32_t LCKR;
  170. volatile uint32_t AFR[2];
  171. } periph_gpio_t;
  172. typedef struct {
  173. volatile uint32_t SR;
  174. volatile uint32_t DR;
  175. volatile uint32_t BRR;
  176. volatile uint32_t CR1;
  177. } periph_uart_t;
  178. #define USART1 ((periph_uart_t*) 0x40011000)
  179. #define GPIOA ((periph_gpio_t*) 0x40020000)
  180. #define GPIOB ((periph_gpio_t*) 0x40020400)
  181. #define RCC ((periph_rcc_t*) 0x40023800)
  182. // simple GPIO interface
  183. #define GPIO_MODE_IN (0)
  184. #define GPIO_MODE_OUT (1)
  185. #define GPIO_MODE_ALT (2)
  186. #define GPIO_PULL_NONE (0)
  187. #define GPIO_PULL_UP (0)
  188. #define GPIO_PULL_DOWN (1)
  189. void gpio_init(periph_gpio_t *gpio, int pin, int mode, int pull, int alt) {
  190. gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | (mode << (2 * pin));
  191. // OTYPER is left as default push-pull
  192. // OSPEEDR is left as default low speed
  193. gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin));
  194. gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
  195. }
  196. #define gpio_get(gpio, pin) ((gpio->IDR >> (pin)) & 1)
  197. #define gpio_set(gpio, pin, value) do { gpio->ODR = (gpio->ODR & ~(1 << (pin))) | (value << pin); } while (0)
  198. #define gpio_low(gpio, pin) do { gpio->BSRRH = (1 << (pin)); } while (0)
  199. #define gpio_high(gpio, pin) do { gpio->BSRRL = (1 << (pin)); } while (0)
  200. void stm32_init(void) {
  201. // basic MCU config
  202. RCC->CR |= (uint32_t)0x00000001; // set HSION
  203. RCC->CFGR = 0x00000000; // reset all
  204. RCC->CR &= (uint32_t)0xfef6ffff; // reset HSEON, CSSON, PLLON
  205. RCC->PLLCFGR = 0x24003010; // reset PLLCFGR
  206. RCC->CR &= (uint32_t)0xfffbffff; // reset HSEBYP
  207. RCC->CIR = 0x00000000; // disable IRQs
  208. // leave the clock as-is (internal 16MHz)
  209. // enable GPIO clocks
  210. RCC->AHB1ENR |= 0x00000003; // GPIOAEN, GPIOBEN
  211. // turn on an LED! (on pyboard it's the red one)
  212. gpio_init(GPIOA, 13, GPIO_MODE_OUT, GPIO_PULL_NONE, 0);
  213. gpio_high(GPIOA, 13);
  214. // enable UART1 at 9600 baud (TX=B6, RX=B7)
  215. gpio_init(GPIOB, 6, GPIO_MODE_ALT, GPIO_PULL_NONE, 7);
  216. gpio_init(GPIOB, 7, GPIO_MODE_ALT, GPIO_PULL_NONE, 7);
  217. RCC->APB2ENR |= 0x00000010; // USART1EN
  218. USART1->BRR = (104 << 4) | 3; // 16MHz/(16*104.1875) = 9598 baud
  219. USART1->CR1 = 0x0000200c; // USART enable, tx enable, rx enable
  220. }
  221. #endif