machine_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 <string.h>
  28. #include "py/runtime.h"
  29. #include "extmod/machine_spi.h"
  30. #if MICROPY_PY_MACHINE_SPI
  31. // if a port didn't define MSB/LSB constants then provide them
  32. #ifndef MICROPY_PY_MACHINE_SPI_MSB
  33. #define MICROPY_PY_MACHINE_SPI_MSB (0)
  34. #define MICROPY_PY_MACHINE_SPI_LSB (1)
  35. #endif
  36. /******************************************************************************/
  37. // MicroPython bindings for generic machine.SPI
  38. STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
  39. mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  40. // check the id argument, if given
  41. if (n_args > 0) {
  42. if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
  43. #if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW)
  44. // dispatch to port-specific constructor
  45. extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args);
  46. return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args);
  47. #else
  48. mp_raise_ValueError("invalid SPI peripheral");
  49. #endif
  50. }
  51. --n_args;
  52. ++args;
  53. }
  54. // software SPI
  55. return mp_machine_soft_spi_make_new(type, n_args, n_kw, args);
  56. }
  57. STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  58. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
  59. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  60. spi_p->init(s, n_args - 1, args + 1, kw_args);
  61. return mp_const_none;
  62. }
  63. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
  64. STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
  65. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
  66. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  67. if (spi_p->deinit != NULL) {
  68. spi_p->deinit(s);
  69. }
  70. return mp_const_none;
  71. }
  72. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
  73. STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
  74. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
  75. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  76. spi_p->transfer(s, len, src, dest);
  77. }
  78. STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
  79. vstr_t vstr;
  80. vstr_init_len(&vstr, mp_obj_get_int(args[1]));
  81. memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
  82. mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
  83. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  84. }
  85. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
  86. STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
  87. mp_buffer_info_t bufinfo;
  88. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
  89. memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
  90. mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
  91. return mp_const_none;
  92. }
  93. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
  94. STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
  95. mp_buffer_info_t src;
  96. mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
  97. mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
  98. return mp_const_none;
  99. }
  100. MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
  101. STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
  102. mp_buffer_info_t src;
  103. mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
  104. mp_buffer_info_t dest;
  105. mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
  106. if (src.len != dest.len) {
  107. mp_raise_ValueError("buffers must be the same length");
  108. }
  109. mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
  110. return mp_const_none;
  111. }
  112. MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
  113. STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
  114. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
  115. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
  116. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
  117. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
  118. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
  119. { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
  120. { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
  121. { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
  122. };
  123. MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
  124. /******************************************************************************/
  125. // Implementation of soft SPI
  126. STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
  127. #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
  128. if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
  129. return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
  130. } else
  131. #endif
  132. {
  133. return 500000 / delay_half;
  134. }
  135. }
  136. STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
  137. #ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
  138. if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
  139. return MICROPY_HW_SOFTSPI_MIN_DELAY;
  140. } else
  141. #endif
  142. {
  143. uint32_t delay_half = 500000 / baudrate;
  144. // round delay_half up so that: actual_baudrate <= requested_baudrate
  145. if (500000 % baudrate != 0) {
  146. delay_half += 1;
  147. }
  148. return delay_half;
  149. }
  150. }
  151. STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  152. mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
  153. mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
  154. " sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
  155. baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase,
  156. mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
  157. }
  158. STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  159. enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
  160. static const mp_arg_t allowed_args[] = {
  161. { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
  162. { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  163. { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  164. { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
  165. { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
  166. { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  167. { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  168. { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  169. };
  170. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  171. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  172. // create new object
  173. mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
  174. self->base.type = &mp_machine_soft_spi_type;
  175. // set parameters
  176. self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
  177. self->spi.polarity = args[ARG_polarity].u_int;
  178. self->spi.phase = args[ARG_phase].u_int;
  179. if (args[ARG_bits].u_int != 8) {
  180. mp_raise_ValueError("bits must be 8");
  181. }
  182. if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
  183. mp_raise_ValueError("firstbit must be MSB");
  184. }
  185. if (args[ARG_sck].u_obj == MP_OBJ_NULL
  186. || args[ARG_mosi].u_obj == MP_OBJ_NULL
  187. || args[ARG_miso].u_obj == MP_OBJ_NULL) {
  188. mp_raise_ValueError("must specify all of sck/mosi/miso");
  189. }
  190. self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
  191. self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
  192. self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
  193. // configure bus
  194. mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
  195. return MP_OBJ_FROM_PTR(self);
  196. }
  197. STATIC void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  198. mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
  199. enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
  200. static const mp_arg_t allowed_args[] = {
  201. { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
  202. { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
  203. { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
  204. { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  205. { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  206. { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  207. };
  208. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  209. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  210. if (args[ARG_baudrate].u_int != -1) {
  211. self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
  212. }
  213. if (args[ARG_polarity].u_int != -1) {
  214. self->spi.polarity = args[ARG_polarity].u_int;
  215. }
  216. if (args[ARG_phase].u_int != -1) {
  217. self->spi.phase = args[ARG_phase].u_int;
  218. }
  219. if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
  220. self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
  221. }
  222. if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
  223. self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
  224. }
  225. if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
  226. self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
  227. }
  228. // configure bus
  229. mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
  230. }
  231. STATIC void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
  232. mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
  233. mp_soft_spi_transfer(&self->spi, len, src, dest);
  234. }
  235. const mp_machine_spi_p_t mp_machine_soft_spi_p = {
  236. .init = mp_machine_soft_spi_init,
  237. .deinit = NULL,
  238. .transfer = mp_machine_soft_spi_transfer,
  239. };
  240. const mp_obj_type_t mp_machine_soft_spi_type = {
  241. { &mp_type_type },
  242. .name = MP_QSTR_SoftSPI,
  243. .print = mp_machine_soft_spi_print,
  244. .make_new = mp_machine_spi_make_new, // delegate to master constructor
  245. .protocol = &mp_machine_soft_spi_p,
  246. .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
  247. };
  248. #endif // MICROPY_PY_MACHINE_SPI