mphalport.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <string.h>
  2. #include "py/runtime.h"
  3. #include "py/mperrno.h"
  4. #include "py/mphal.h"
  5. #include "extmod/misc.h"
  6. #include "usb.h"
  7. #include "uart.h"
  8. // this table converts from HAL_StatusTypeDef to POSIX errno
  9. const byte mp_hal_status_to_errno_table[4] = {
  10. [HAL_OK] = 0,
  11. [HAL_ERROR] = MP_EIO,
  12. [HAL_BUSY] = MP_EBUSY,
  13. [HAL_TIMEOUT] = MP_ETIMEDOUT,
  14. };
  15. NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
  16. mp_raise_OSError(mp_hal_status_to_errno_table[status]);
  17. }
  18. MP_WEAK int mp_hal_stdin_rx_chr(void) {
  19. for (;;) {
  20. #if 0
  21. #ifdef USE_HOST_MODE
  22. pyb_usb_host_process();
  23. int c = pyb_usb_host_get_keyboard();
  24. if (c != 0) {
  25. return c;
  26. }
  27. #endif
  28. #endif
  29. #if MICROPY_HW_ENABLE_USB
  30. byte c;
  31. if (usb_vcp_recv_byte(&c) != 0) {
  32. return c;
  33. }
  34. #endif
  35. if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
  36. return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart));
  37. }
  38. int dupterm_c = mp_uos_dupterm_rx_chr();
  39. if (dupterm_c >= 0) {
  40. return dupterm_c;
  41. }
  42. MICROPY_EVENT_POLL_HOOK
  43. }
  44. }
  45. void mp_hal_stdout_tx_str(const char *str) {
  46. mp_hal_stdout_tx_strn(str, strlen(str));
  47. }
  48. MP_WEAK void mp_hal_stdout_tx_strn(const char *str, size_t len) {
  49. if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
  50. uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
  51. }
  52. #if 0 && defined(USE_HOST_MODE) && MICROPY_HW_HAS_LCD
  53. lcd_print_strn(str, len);
  54. #endif
  55. #if MICROPY_HW_ENABLE_USB
  56. if (usb_vcp_is_enabled()) {
  57. usb_vcp_send_strn(str, len);
  58. }
  59. #endif
  60. mp_uos_dupterm_tx_strn(str, len);
  61. }
  62. // Efficiently convert "\n" to "\r\n"
  63. void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
  64. const char *last = str;
  65. while (len--) {
  66. if (*str == '\n') {
  67. if (str > last) {
  68. mp_hal_stdout_tx_strn(last, str - last);
  69. }
  70. mp_hal_stdout_tx_strn("\r\n", 2);
  71. ++str;
  72. last = str;
  73. } else {
  74. ++str;
  75. }
  76. }
  77. if (str > last) {
  78. mp_hal_stdout_tx_strn(last, str - last);
  79. }
  80. }
  81. #if __CORTEX_M >= 0x03
  82. void mp_hal_ticks_cpu_enable(void) {
  83. if (!(DWT->CTRL & DWT_CTRL_CYCCNTENA_Msk)) {
  84. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  85. #if defined(__CORTEX_M) && __CORTEX_M == 7
  86. // on Cortex-M7 we must unlock the DWT before writing to its registers
  87. DWT->LAR = 0xc5acce55;
  88. #endif
  89. DWT->CYCCNT = 0;
  90. DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
  91. }
  92. }
  93. #endif
  94. void mp_hal_gpio_clock_enable(GPIO_TypeDef *gpio) {
  95. #if defined(STM32L476xx) || defined(STM32L496xx)
  96. if (gpio == GPIOG) {
  97. // Port G pins 2 thru 15 are powered using VddIO2 on these MCUs.
  98. HAL_PWREx_EnableVddIO2();
  99. }
  100. #endif
  101. // This logic assumes that all the GPIOx_EN bits are adjacent and ordered in one register
  102. #if defined(STM32F0)
  103. #define AHBxENR AHBENR
  104. #define AHBxENR_GPIOAEN_Pos RCC_AHBENR_GPIOAEN_Pos
  105. #elif defined(STM32F4) || defined(STM32F7)
  106. #define AHBxENR AHB1ENR
  107. #define AHBxENR_GPIOAEN_Pos RCC_AHB1ENR_GPIOAEN_Pos
  108. #elif defined(STM32H7)
  109. #define AHBxENR AHB4ENR
  110. #define AHBxENR_GPIOAEN_Pos RCC_AHB4ENR_GPIOAEN_Pos
  111. #elif defined(STM32L4)
  112. #define AHBxENR AHB2ENR
  113. #define AHBxENR_GPIOAEN_Pos RCC_AHB2ENR_GPIOAEN_Pos
  114. #endif
  115. uint32_t gpio_idx = ((uint32_t)gpio - GPIOA_BASE) / (GPIOB_BASE - GPIOA_BASE);
  116. RCC->AHBxENR |= 1 << (AHBxENR_GPIOAEN_Pos + gpio_idx);
  117. volatile uint32_t tmp = RCC->AHBxENR; // Delay after enabling clock
  118. (void)tmp;
  119. }
  120. void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, uint32_t alt) {
  121. GPIO_TypeDef *gpio = pin_obj->gpio;
  122. uint32_t pin = pin_obj->pin;
  123. mp_hal_gpio_clock_enable(gpio);
  124. gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | ((mode & 3) << (2 * pin));
  125. #if defined(GPIO_ASCR_ASC0)
  126. // The L4 has a special analog switch to connect the GPIO to the ADC
  127. gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | (((mode >> 2) & 1) << pin);
  128. gpio->ASCR = (gpio->ASCR & ~(1 << pin)) | ((mode >> 3) & 1) << pin;
  129. #else
  130. gpio->OTYPER = (gpio->OTYPER & ~(1 << pin)) | ((mode >> 2) << pin);
  131. #endif
  132. gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (2 << (2 * pin)); // full speed
  133. gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin));
  134. gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
  135. }
  136. bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit) {
  137. const pin_af_obj_t *af = pin_find_af(pin, fn, unit);
  138. if (af == NULL) {
  139. return false;
  140. }
  141. mp_hal_pin_config(pin, mode, pull, af->idx);
  142. return true;
  143. }
  144. void mp_hal_pin_config_speed(mp_hal_pin_obj_t pin_obj, uint32_t speed) {
  145. GPIO_TypeDef *gpio = pin_obj->gpio;
  146. uint32_t pin = pin_obj->pin;
  147. gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (speed << (2 * pin));
  148. }