mphalport.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Glenn Ruben Bakke
  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 <string.h>
  27. #include "py/mpstate.h"
  28. #include "py/mphal.h"
  29. #include "py/mperrno.h"
  30. #include "py/runtime.h"
  31. #include "uart.h"
  32. #include "nrfx_errors.h"
  33. #include "nrfx_config.h"
  34. // this table converts from HAL_StatusTypeDef to POSIX errno
  35. const byte mp_hal_status_to_errno_table[4] = {
  36. [HAL_OK] = 0,
  37. [HAL_ERROR] = MP_EIO,
  38. [HAL_BUSY] = MP_EBUSY,
  39. [HAL_TIMEOUT] = MP_ETIMEDOUT,
  40. };
  41. NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
  42. mp_raise_OSError(mp_hal_status_to_errno_table[status]);
  43. }
  44. #if !MICROPY_KBD_EXCEPTION
  45. void mp_hal_set_interrupt_char(int c) {
  46. }
  47. #endif
  48. #if !MICROPY_PY_BLE_NUS
  49. int mp_hal_stdin_rx_chr(void) {
  50. for (;;) {
  51. if (MP_STATE_PORT(board_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(board_stdio_uart))) {
  52. return uart_rx_char(MP_STATE_PORT(board_stdio_uart));
  53. }
  54. }
  55. return 0;
  56. }
  57. void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
  58. if (MP_STATE_PORT(board_stdio_uart) != NULL) {
  59. uart_tx_strn(MP_STATE_PORT(board_stdio_uart), str, len);
  60. }
  61. }
  62. void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
  63. if (MP_STATE_PORT(board_stdio_uart) != NULL) {
  64. uart_tx_strn_cooked(MP_STATE_PORT(board_stdio_uart), str, len);
  65. }
  66. }
  67. #endif
  68. void mp_hal_stdout_tx_str(const char *str) {
  69. mp_hal_stdout_tx_strn(str, strlen(str));
  70. }
  71. void mp_hal_delay_us(mp_uint_t us)
  72. {
  73. if (us == 0) {
  74. return;
  75. }
  76. register uint32_t delay __ASM ("r0") = us;
  77. __ASM volatile (
  78. #ifdef NRF51
  79. ".syntax unified\n"
  80. #endif
  81. "1:\n"
  82. " SUBS %0, %0, #1\n"
  83. " NOP\n"
  84. " NOP\n"
  85. " NOP\n"
  86. " NOP\n"
  87. " NOP\n"
  88. " NOP\n"
  89. " NOP\n"
  90. " NOP\n"
  91. " NOP\n"
  92. " NOP\n"
  93. " NOP\n"
  94. " NOP\n"
  95. #ifdef NRF52
  96. " NOP\n"
  97. " NOP\n"
  98. " NOP\n"
  99. " NOP\n"
  100. " NOP\n"
  101. " NOP\n"
  102. " NOP\n"
  103. " NOP\n"
  104. " NOP\n"
  105. " NOP\n"
  106. " NOP\n"
  107. " NOP\n"
  108. " NOP\n"
  109. " NOP\n"
  110. " NOP\n"
  111. " NOP\n"
  112. " NOP\n"
  113. " NOP\n"
  114. " NOP\n"
  115. " NOP\n"
  116. " NOP\n"
  117. " NOP\n"
  118. " NOP\n"
  119. " NOP\n"
  120. " NOP\n"
  121. " NOP\n"
  122. " NOP\n"
  123. " NOP\n"
  124. " NOP\n"
  125. " NOP\n"
  126. " NOP\n"
  127. " NOP\n"
  128. " NOP\n"
  129. " NOP\n"
  130. " NOP\n"
  131. " NOP\n"
  132. " NOP\n"
  133. " NOP\n"
  134. " NOP\n"
  135. " NOP\n"
  136. " NOP\n"
  137. " NOP\n"
  138. " NOP\n"
  139. " NOP\n"
  140. " NOP\n"
  141. " NOP\n"
  142. #endif
  143. " BNE 1b\n"
  144. : "+r" (delay));
  145. }
  146. void mp_hal_delay_ms(mp_uint_t ms)
  147. {
  148. for (mp_uint_t i = 0; i < ms; i++)
  149. {
  150. mp_hal_delay_us(999);
  151. }
  152. }
  153. #if defined(NRFX_LOG_ENABLED) && (NRFX_LOG_ENABLED == 1)
  154. static const char nrfx_error_unknown[1] = "";
  155. static const char nrfx_error_success[] = "NRFX_SUCCESS";
  156. static const char nrfx_error_internal[] = "NRFX_ERROR_INTERNAL";
  157. static const char nrfx_error_no_mem[] = "NRFX_ERROR_NO_MEM";
  158. static const char nrfx_error_not_supported[] = "NRFX_ERROR_NOT_SUPPORTED";
  159. static const char nrfx_error_invalid_param[] = "NRFX_ERROR_INVALID_PARAM";
  160. static const char nrfx_error_invalid_state[] = "NRFX_ERROR_INVALID_STATE";
  161. static const char nrfx_error_invalid_length[] = "NRFX_ERROR_INVALID_LENGTH";
  162. static const char nrfx_error_timeout[] = "NRFX_ERROR_TIMEOUT";
  163. static const char nrfx_error_forbidden[] = "NRFX_ERROR_FORBIDDEN";
  164. static const char nrfx_error_null[] = "NRFX_ERROR_NULL";
  165. static const char nrfx_error_invalid_addr[] = "NRFX_ERROR_INVALID_ADDR";
  166. static const char nrfx_error_busy[] = "NRFX_ERROR_BUSY";
  167. static const char nrfx_error_already_initalized[] = "NRFX_ERROR_ALREADY_INITIALIZED";
  168. static const char * nrfx_error_strings[13] = {
  169. nrfx_error_success,
  170. nrfx_error_internal,
  171. nrfx_error_no_mem,
  172. nrfx_error_not_supported,
  173. nrfx_error_invalid_param,
  174. nrfx_error_invalid_state,
  175. nrfx_error_invalid_length,
  176. nrfx_error_timeout,
  177. nrfx_error_forbidden,
  178. nrfx_error_null,
  179. nrfx_error_invalid_addr,
  180. nrfx_error_busy,
  181. nrfx_error_already_initalized
  182. };
  183. static const char nrfx_drv_error_twi_err_overrun[] = "NRFX_ERROR_DRV_TWI_ERR_OVERRUN";
  184. static const char nrfx_drv_error_twi_err_anack[] = "NRFX_ERROR_DRV_TWI_ERR_ANACK";
  185. static const char nrfx_drv_error_twi_err_dnack[] = "NRFX_ERROR_DRV_TWI_ERR_DNACK";
  186. static const char * nrfx_drv_error_strings[3] = {
  187. nrfx_drv_error_twi_err_overrun,
  188. nrfx_drv_error_twi_err_anack,
  189. nrfx_drv_error_twi_err_dnack
  190. };
  191. const char * nrfx_error_code_lookup(uint32_t err_code) {
  192. if (err_code >= NRFX_ERROR_BASE_NUM && err_code <= NRFX_ERROR_BASE_NUM + 13) {
  193. return nrfx_error_strings[err_code - NRFX_ERROR_BASE_NUM];
  194. } else if (err_code >= NRFX_ERROR_DRIVERS_BASE_NUM && err_code <= NRFX_ERROR_DRIVERS_BASE_NUM + 3) {
  195. return nrfx_drv_error_strings[err_code - NRFX_ERROR_DRIVERS_BASE_NUM];
  196. }
  197. return nrfx_error_unknown;
  198. }
  199. #endif // NRFX_LOG_ENABLED