PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/esp8266/modpybhspi.c

https://gitlab.com/jiangming1399/micropython-mirror
C | 258 lines | 198 code | 28 blank | 32 comment | 34 complexity | bda5ffd49c9215b83d309d5381ec7ba6 MD5 | raw file
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2016 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. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "ets_sys.h"
  30. #include "etshal.h"
  31. #include "ets_alt_task.h"
  32. #include "py/runtime.h"
  33. #include "py/stream.h"
  34. #include "py/mphal.h"
  35. #include "hspi.h"
  36. typedef struct _pyb_hspi_obj_t {
  37. mp_obj_base_t base;
  38. uint32_t baudrate;
  39. uint8_t polarity;
  40. uint8_t phase;
  41. } pyb_hspi_obj_t;
  42. /******************************************************************************/
  43. // MicroPython bindings for HSPI
  44. STATIC void pyb_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  45. pyb_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
  46. mp_printf(print, "HSPI(baudrate=%u, polarity=%u, phase=%u)",
  47. self->baudrate, self->polarity, self->phase);
  48. }
  49. STATIC void pyb_hspi_init_helper(pyb_hspi_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  50. enum { ARG_baudrate, ARG_polarity, ARG_phase };
  51. static const mp_arg_t allowed_args[] = {
  52. { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
  53. { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
  54. { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
  55. };
  56. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  57. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
  58. allowed_args, args);
  59. if (args[ARG_baudrate].u_int != -1) {
  60. self->baudrate = args[ARG_baudrate].u_int;
  61. }
  62. if (args[ARG_polarity].u_int != -1) {
  63. self->polarity = args[ARG_polarity].u_int;
  64. }
  65. if (args[ARG_phase].u_int != -1) {
  66. self->phase = args[ARG_phase].u_int;
  67. }
  68. if (self->baudrate == 80000000L) {
  69. // Special case for full speed.
  70. spi_init_gpio(HSPI, SPI_CLK_80MHZ_NODIV);
  71. spi_clock(HSPI, 0, 0);
  72. } else if (self->baudrate > 40000000L) {
  73. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
  74. "impossible baudrate"));
  75. } else {
  76. uint32_t divider = 40000000L / self->baudrate;
  77. uint16_t prediv = MIN(divider, SPI_CLKDIV_PRE + 1);
  78. uint16_t cntdiv = (divider / prediv) * 2; // cntdiv has to be even
  79. if (cntdiv > SPI_CLKCNT_N + 1 || cntdiv == 0 || prediv == 0) {
  80. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
  81. "impossible baudrate"));
  82. }
  83. self->baudrate = 80000000L / (prediv * cntdiv);
  84. spi_init_gpio(HSPI, SPI_CLK_USE_DIV);
  85. spi_clock(HSPI, prediv, cntdiv);
  86. }
  87. // TODO: Make the byte order configurable too (discuss param names)
  88. spi_tx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW);
  89. spi_rx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW);
  90. CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE | SPI_USR_MISO |
  91. SPI_USR_ADDR | SPI_USR_COMMAND | SPI_USR_DUMMY);
  92. // Clear Dual or Quad lines transmission mode
  93. CLEAR_PERI_REG_MASK(SPI_CTRL(HSPI), SPI_QIO_MODE | SPI_DIO_MODE |
  94. SPI_DOUT_MODE | SPI_QOUT_MODE);
  95. spi_mode(HSPI, self->phase, self->polarity);
  96. }
  97. mp_obj_t pyb_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  98. mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
  99. pyb_hspi_obj_t *self = m_new_obj(pyb_hspi_obj_t);
  100. self->base.type = &pyb_hspi_type;
  101. // set defaults
  102. self->baudrate = 80000000L;
  103. self->polarity = 0;
  104. self->phase = 0;
  105. mp_map_t kw_args;
  106. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  107. pyb_hspi_init_helper(self, n_args, args, &kw_args);
  108. return MP_OBJ_FROM_PTR(self);
  109. }
  110. STATIC mp_obj_t pyb_hspi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  111. pyb_hspi_init_helper(args[0], n_args - 1, args + 1, kw_args);
  112. return mp_const_none;
  113. }
  114. MP_DEFINE_CONST_FUN_OBJ_KW(pyb_hspi_init_obj, 1, pyb_hspi_init);
  115. STATIC mp_obj_t pyb_hspi_read(size_t n_args, const mp_obj_t *args) {
  116. vstr_t dest_buf;
  117. vstr_init_len(&dest_buf, mp_obj_get_int(args[1]));
  118. uint8_t write_byte = 0;
  119. if (n_args == 3) {
  120. write_byte = mp_obj_get_int(args[2]);
  121. }
  122. // Process data in chunks, let the pending tasks run in between
  123. size_t chunk_size = 1024; // TODO this should depend on baudrate
  124. size_t count = dest_buf.len / chunk_size;
  125. size_t i = 0;
  126. for (size_t j = 0; j < count; ++j) {
  127. for (size_t k = 0; k < chunk_size; ++k) {
  128. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  129. (uint32_t)write_byte, 8, 0);
  130. ++i;
  131. }
  132. ets_loop_iter();
  133. }
  134. while (i < dest_buf.len) {
  135. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  136. (uint32_t)write_byte, 8, 0);
  137. ++i;
  138. }
  139. return mp_obj_new_str_from_vstr(&mp_type_bytes, &dest_buf);
  140. }
  141. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_hspi_read_obj, 2, 3, pyb_hspi_read);
  142. STATIC mp_obj_t pyb_hspi_readinto(size_t n_args, const mp_obj_t *args) {
  143. mp_buffer_info_t dest_buf;
  144. mp_get_buffer_raise(args[1], &dest_buf, MP_BUFFER_WRITE);
  145. uint8_t write_byte = 0;
  146. if (n_args == 3) {
  147. write_byte = mp_obj_get_int(args[2]);
  148. }
  149. size_t chunk_size = 1024;
  150. size_t count = dest_buf.len / chunk_size;
  151. size_t i = 0;
  152. for (size_t j = 0; j < count; ++j) {
  153. for (size_t k = 0; k < chunk_size; ++k) {
  154. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  155. (uint32_t)write_byte, 8, 0);
  156. ++i;
  157. }
  158. ets_loop_iter();
  159. }
  160. while (i < dest_buf.len) {
  161. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  162. (uint32_t)write_byte, 8, 0);
  163. ++i;
  164. }
  165. return mp_const_none;
  166. }
  167. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_hspi_readinto_obj, 2, 3, pyb_hspi_readinto);
  168. STATIC mp_obj_t pyb_hspi_write(mp_obj_t self_in, mp_obj_t wr_buf_in) {
  169. mp_buffer_info_t src_buf;
  170. mp_get_buffer_raise(wr_buf_in, &src_buf, MP_BUFFER_READ);
  171. size_t chunk_size = 1024;
  172. size_t count = src_buf.len / chunk_size;
  173. size_t i = 0;
  174. for (size_t j = 0; j < count; ++j) {
  175. for (size_t k = 0; k < chunk_size; ++k) {
  176. spi_tx8fast(HSPI, ((const uint8_t*)src_buf.buf)[i]);
  177. ++i;
  178. }
  179. ets_loop_iter();
  180. }
  181. while (i < src_buf.len) {
  182. spi_tx8fast(HSPI, ((const uint8_t*)src_buf.buf)[i]);
  183. ++i;
  184. }
  185. return mp_const_none;
  186. }
  187. MP_DEFINE_CONST_FUN_OBJ_2(pyb_hspi_write_obj, pyb_hspi_write);
  188. STATIC mp_obj_t pyb_hspi_write_readinto(mp_obj_t self_in, mp_obj_t wr_buf_in, mp_obj_t rd_buf_in) {
  189. mp_buffer_info_t src_buf;
  190. mp_get_buffer_raise(wr_buf_in, &src_buf, MP_BUFFER_READ);
  191. mp_buffer_info_t dest_buf;
  192. mp_get_buffer_raise(rd_buf_in, &dest_buf, MP_BUFFER_WRITE);
  193. if (src_buf.len != dest_buf.len) {
  194. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  195. "buffers must be the same length"));
  196. }
  197. size_t chunk_size = 1024;
  198. size_t count = src_buf.len / chunk_size;
  199. size_t i = 0;
  200. for (size_t j = 0; j < count; ++j) {
  201. for (size_t k = 0; k < chunk_size; ++k) {
  202. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  203. (uint32_t)(((const uint8_t*)src_buf.buf)[i]), 8, 0);
  204. ++i;
  205. }
  206. ets_loop_iter();
  207. }
  208. while (i < src_buf.len) {
  209. ((uint8_t*)dest_buf.buf)[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8,
  210. (uint32_t)(((const uint8_t*)src_buf.buf)[i]), 8, 0);
  211. ++i;
  212. }
  213. return mp_const_none;
  214. }
  215. MP_DEFINE_CONST_FUN_OBJ_3(pyb_hspi_write_readinto_obj, pyb_hspi_write_readinto);
  216. STATIC const mp_rom_map_elem_t pyb_hspi_locals_dict_table[] = {
  217. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_hspi_init_obj) },
  218. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_hspi_read_obj) },
  219. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&pyb_hspi_readinto_obj) },
  220. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_hspi_write_obj) },
  221. { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&pyb_hspi_write_readinto_obj) },
  222. };
  223. STATIC MP_DEFINE_CONST_DICT(pyb_hspi_locals_dict, pyb_hspi_locals_dict_table);
  224. const mp_obj_type_t pyb_hspi_type = {
  225. { &mp_type_type },
  226. .name = MP_QSTR_HSPI,
  227. .print = pyb_hspi_print,
  228. .make_new = pyb_hspi_make_new,
  229. .locals_dict = (mp_obj_dict_t*)&pyb_hspi_locals_dict,
  230. };