cc3200_hal.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  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. /******************************************************************************
  27. IMPORTS
  28. ******************************************************************************/
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include "py/mphal.h"
  33. #include "py/runtime.h"
  34. #include "py/objstr.h"
  35. #include "inc/hw_types.h"
  36. #include "inc/hw_ints.h"
  37. #include "inc/hw_nvic.h"
  38. #include "hw_memmap.h"
  39. #include "rom_map.h"
  40. #include "interrupt.h"
  41. #include "systick.h"
  42. #include "prcm.h"
  43. #include "pin.h"
  44. #include "mpexception.h"
  45. #include "telnet.h"
  46. #include "pybuart.h"
  47. #include "utils.h"
  48. #include "irq.h"
  49. #include "moduos.h"
  50. #ifdef USE_FREERTOS
  51. #include "FreeRTOS.h"
  52. #include "task.h"
  53. #include "semphr.h"
  54. #endif
  55. /******************************************************************************
  56. DECLARE PRIVATE FUNCTIONS
  57. ******************************************************************************/
  58. #ifndef USE_FREERTOS
  59. static void hal_TickInit (void);
  60. #endif
  61. /******************************************************************************
  62. DECLARE LOCAL DATA
  63. ******************************************************************************/
  64. static volatile uint32_t HAL_tickCount;
  65. /******************************************************************************
  66. DECLARE IMPORTED DATA
  67. ******************************************************************************/
  68. extern void (* const g_pfnVectors[256])(void);
  69. /******************************************************************************
  70. DEFINE PUBLIC FUNCTIONS
  71. ******************************************************************************/
  72. __attribute__ ((section (".boot")))
  73. void HAL_SystemInit (void) {
  74. MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]);
  75. // in the case of a release image, these steps are already performed by
  76. // the bootloader so we can skip it and gain some code space
  77. #ifdef DEBUG
  78. MAP_IntMasterEnable();
  79. PRCMCC3200MCUInit();
  80. #endif
  81. #ifndef USE_FREERTOS
  82. hal_TickInit();
  83. #endif
  84. }
  85. void HAL_SystemDeInit (void) {
  86. }
  87. void HAL_IncrementTick(void) {
  88. HAL_tickCount++;
  89. }
  90. mp_uint_t mp_hal_ticks_ms(void) {
  91. return HAL_tickCount;
  92. }
  93. // The SysTick timer counts down at HAL_FCPU_HZ, so we can use that knowledge
  94. // to grab a microsecond counter.
  95. mp_uint_t mp_hal_ticks_us(void) {
  96. mp_uint_t irq_state = disable_irq();
  97. uint32_t counter = SysTickValueGet();
  98. uint32_t milliseconds = mp_hal_ticks_ms();
  99. enable_irq(irq_state);
  100. uint32_t load = SysTickPeriodGet();
  101. counter = load - counter; // Convert from decrementing to incrementing
  102. return (milliseconds * 1000) + ((counter * 1000) / load);
  103. }
  104. void mp_hal_delay_ms(mp_uint_t delay) {
  105. // only if we are not within interrupt context and interrupts are enabled
  106. if ((HAL_NVIC_INT_CTRL_REG & HAL_VECTACTIVE_MASK) == 0 && query_irq() == IRQ_STATE_ENABLED) {
  107. MP_THREAD_GIL_EXIT();
  108. #ifdef USE_FREERTOS
  109. vTaskDelay (delay / portTICK_PERIOD_MS);
  110. #else
  111. uint32_t start = HAL_tickCount;
  112. // wraparound of tick is taken care of by 2's complement arithmetic.
  113. while (HAL_tickCount - start < delay) {
  114. // enter sleep mode, waiting for (at least) the SysTick interrupt.
  115. __WFI();
  116. }
  117. #endif
  118. MP_THREAD_GIL_ENTER();
  119. } else {
  120. for (int ms = 0; ms < delay; ms++) {
  121. UtilsDelay(UTILS_DELAY_US_TO_COUNT(1000));
  122. }
  123. }
  124. }
  125. void mp_hal_stdout_tx_str(const char *str) {
  126. mp_hal_stdout_tx_strn(str, strlen(str));
  127. }
  128. void mp_hal_stdout_tx_strn(const char *str, size_t len) {
  129. if (MP_STATE_PORT(os_term_dup_obj)) {
  130. if (MP_OBJ_IS_TYPE(MP_STATE_PORT(os_term_dup_obj)->stream_o, &pyb_uart_type)) {
  131. uart_tx_strn(MP_STATE_PORT(os_term_dup_obj)->stream_o, str, len);
  132. } else {
  133. MP_STATE_PORT(os_term_dup_obj)->write[2] = mp_obj_new_str_of_type(&mp_type_str, (const byte *)str, len);
  134. mp_call_method_n_kw(1, 0, MP_STATE_PORT(os_term_dup_obj)->write);
  135. }
  136. }
  137. // and also to telnet
  138. telnet_tx_strn(str, len);
  139. }
  140. void mp_hal_stdout_tx_strn_cooked (const char *str, size_t len) {
  141. int32_t nslen = 0;
  142. const char *_str = str;
  143. for (int i = 0; i < len; i++) {
  144. if (str[i] == '\n') {
  145. mp_hal_stdout_tx_strn(_str, nslen);
  146. mp_hal_stdout_tx_strn("\r\n", 2);
  147. _str += nslen + 1;
  148. nslen = 0;
  149. } else {
  150. nslen++;
  151. }
  152. }
  153. if (_str < str + len) {
  154. mp_hal_stdout_tx_strn(_str, nslen);
  155. }
  156. }
  157. int mp_hal_stdin_rx_chr(void) {
  158. for ( ;; ) {
  159. // read telnet first
  160. if (telnet_rx_any()) {
  161. return telnet_rx_char();
  162. } else if (MP_STATE_PORT(os_term_dup_obj)) { // then the stdio_dup
  163. if (MP_OBJ_IS_TYPE(MP_STATE_PORT(os_term_dup_obj)->stream_o, &pyb_uart_type)) {
  164. if (uart_rx_any(MP_STATE_PORT(os_term_dup_obj)->stream_o)) {
  165. return uart_rx_char(MP_STATE_PORT(os_term_dup_obj)->stream_o);
  166. }
  167. } else {
  168. MP_STATE_PORT(os_term_dup_obj)->read[2] = mp_obj_new_int(1);
  169. mp_obj_t data = mp_call_method_n_kw(1, 0, MP_STATE_PORT(os_term_dup_obj)->read);
  170. // data len is > 0
  171. if (mp_obj_is_true(data)) {
  172. mp_buffer_info_t bufinfo;
  173. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  174. return ((int *)(bufinfo.buf))[0];
  175. }
  176. }
  177. }
  178. mp_hal_delay_ms(1);
  179. }
  180. }
  181. /******************************************************************************
  182. DEFINE PRIVATE FUNCTIONS
  183. ******************************************************************************/
  184. #ifndef USE_FREERTOS
  185. static void hal_TickInit (void) {
  186. HAL_tickCount = 0;
  187. MAP_SysTickIntRegister(HAL_IncrementTick);
  188. MAP_IntEnable(FAULT_SYSTICK);
  189. MAP_SysTickIntEnable();
  190. MAP_SysTickPeriodSet(HAL_FCPU_HZ / HAL_SYSTICK_PERIOD_US);
  191. // Force a reload of the SysTick counter register
  192. HWREG(NVIC_ST_CURRENT) = 0;
  193. MAP_SysTickEnable();
  194. }
  195. #endif