machine_spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
  37. mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
  38. uint32_t delay_half = self->delay_half;
  39. // only MSB transfer is implemented
  40. // If a port defines MICROPY_PY_MACHINE_SPI_MIN_DELAY, and the configured
  41. // delay_half is equal to this value, then the software SPI implementation
  42. // will run as fast as possible, limited only by CPU speed and GPIO time.
  43. #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
  44. if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
  45. for (size_t i = 0; i < len; ++i) {
  46. uint8_t data_out = src[i];
  47. uint8_t data_in = 0;
  48. for (int j = 0; j < 8; ++j, data_out <<= 1) {
  49. mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
  50. mp_hal_pin_write(self->sck, 1 - self->polarity);
  51. data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
  52. mp_hal_pin_write(self->sck, self->polarity);
  53. }
  54. if (dest != NULL) {
  55. dest[i] = data_in;
  56. }
  57. }
  58. return;
  59. }
  60. #endif
  61. for (size_t i = 0; i < len; ++i) {
  62. uint8_t data_out = src[i];
  63. uint8_t data_in = 0;
  64. for (int j = 0; j < 8; ++j, data_out <<= 1) {
  65. mp_hal_pin_write(self->mosi, (data_out >> 7) & 1);
  66. if (self->phase == 0) {
  67. mp_hal_delay_us_fast(delay_half);
  68. mp_hal_pin_write(self->sck, 1 - self->polarity);
  69. } else {
  70. mp_hal_pin_write(self->sck, 1 - self->polarity);
  71. mp_hal_delay_us_fast(delay_half);
  72. }
  73. data_in = (data_in << 1) | mp_hal_pin_read(self->miso);
  74. if (self->phase == 0) {
  75. mp_hal_delay_us_fast(delay_half);
  76. mp_hal_pin_write(self->sck, self->polarity);
  77. } else {
  78. mp_hal_pin_write(self->sck, self->polarity);
  79. mp_hal_delay_us_fast(delay_half);
  80. }
  81. }
  82. if (dest != NULL) {
  83. dest[i] = data_in;
  84. }
  85. }
  86. }
  87. /******************************************************************************/
  88. // MicroPython bindings for generic machine.SPI
  89. 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);
  90. 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) {
  91. // check the id argument, if given
  92. if (n_args > 0) {
  93. if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
  94. #if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW)
  95. // dispatch to port-specific constructor
  96. 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);
  97. return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, n_kw, args);
  98. #else
  99. mp_raise_ValueError("invalid SPI peripheral");
  100. #endif
  101. }
  102. --n_args;
  103. ++args;
  104. }
  105. // software SPI
  106. return mp_machine_soft_spi_make_new(type, n_args, n_kw, args);
  107. }
  108. STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
  109. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
  110. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  111. spi_p->init(s, n_args - 1, args + 1, kw_args);
  112. return mp_const_none;
  113. }
  114. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
  115. STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
  116. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
  117. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  118. if (spi_p->deinit != NULL) {
  119. spi_p->deinit(s);
  120. }
  121. return mp_const_none;
  122. }
  123. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
  124. STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
  125. mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
  126. mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)s->type->protocol;
  127. spi_p->transfer(s, len, src, dest);
  128. }
  129. STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
  130. vstr_t vstr;
  131. vstr_init_len(&vstr, mp_obj_get_int(args[1]));
  132. memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
  133. mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
  134. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  135. }
  136. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
  137. STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
  138. mp_buffer_info_t bufinfo;
  139. mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
  140. memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
  141. mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
  142. return mp_const_none;
  143. }
  144. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
  145. STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
  146. mp_buffer_info_t src;
  147. mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
  148. mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
  149. return mp_const_none;
  150. }
  151. MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
  152. STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
  153. mp_buffer_info_t src;
  154. mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
  155. mp_buffer_info_t dest;
  156. mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
  157. if (src.len != dest.len) {
  158. mp_raise_ValueError("buffers must be the same length");
  159. }
  160. mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
  161. return mp_const_none;
  162. }
  163. MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
  164. STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
  165. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
  166. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
  167. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
  168. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
  169. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
  170. { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
  171. { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
  172. { MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
  173. };
  174. MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
  175. /******************************************************************************/
  176. // Implementation of soft SPI
  177. STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
  178. #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
  179. if (delay_half == MICROPY_PY_MACHINE_SPI_MIN_DELAY) {
  180. return MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE;
  181. } else
  182. #endif
  183. {
  184. return 500000 / delay_half;
  185. }
  186. }
  187. STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
  188. #ifdef MICROPY_PY_MACHINE_SPI_MIN_DELAY
  189. if (baudrate >= MICROPY_PY_MACHINE_SPI_MAX_BAUDRATE) {
  190. return MICROPY_PY_MACHINE_SPI_MIN_DELAY;
  191. } else
  192. #endif
  193. {
  194. uint32_t delay_half = 500000 / baudrate;
  195. // round delay_half up so that: actual_baudrate <= requested_baudrate
  196. if (500000 % baudrate != 0) {
  197. delay_half += 1;
  198. }
  199. return delay_half;
  200. }
  201. }
  202. STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  203. mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
  204. mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
  205. " sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
  206. baudrate_from_delay_half(self->delay_half), self->polarity, self->phase,
  207. mp_hal_pin_name(self->sck), mp_hal_pin_name(self->mosi), mp_hal_pin_name(self->miso));
  208. }
  209. 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) {
  210. enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
  211. static const mp_arg_t allowed_args[] = {
  212. { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
  213. { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  214. { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  215. { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
  216. { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
  217. { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  218. { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  219. { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  220. };
  221. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  222. mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  223. // create new object
  224. mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
  225. self->base.type = &mp_machine_soft_spi_type;
  226. // set parameters
  227. self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
  228. self->polarity = args[ARG_polarity].u_int;
  229. self->phase = args[ARG_phase].u_int;
  230. if (args[ARG_bits].u_int != 8) {
  231. mp_raise_ValueError("bits must be 8");
  232. }
  233. if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
  234. mp_raise_ValueError("firstbit must be MSB");
  235. }
  236. if (args[ARG_sck].u_obj == MP_OBJ_NULL
  237. || args[ARG_mosi].u_obj == MP_OBJ_NULL
  238. || args[ARG_miso].u_obj == MP_OBJ_NULL) {
  239. mp_raise_ValueError("must specify all of sck/mosi/miso");
  240. }
  241. self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
  242. self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
  243. self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
  244. // configure pins
  245. mp_hal_pin_write(self->sck, self->polarity);
  246. mp_hal_pin_output(self->sck);
  247. mp_hal_pin_output(self->mosi);
  248. mp_hal_pin_input(self->miso);
  249. return MP_OBJ_FROM_PTR(self);
  250. }
  251. 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) {
  252. mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
  253. enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
  254. static const mp_arg_t allowed_args[] = {
  255. { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
  256. { MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
  257. { MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
  258. { MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  259. { MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  260. { MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  261. };
  262. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  263. mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  264. if (args[ARG_baudrate].u_int != -1) {
  265. self->delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
  266. }
  267. if (args[ARG_polarity].u_int != -1) {
  268. self->polarity = args[ARG_polarity].u_int;
  269. }
  270. if (args[ARG_phase].u_int != -1) {
  271. self->phase = args[ARG_phase].u_int;
  272. }
  273. if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
  274. self->sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
  275. }
  276. if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
  277. self->mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
  278. }
  279. if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
  280. self->miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
  281. }
  282. // configure pins
  283. mp_hal_pin_write(self->sck, self->polarity);
  284. mp_hal_pin_output(self->sck);
  285. mp_hal_pin_output(self->mosi);
  286. mp_hal_pin_input(self->miso);
  287. }
  288. STATIC const mp_machine_spi_p_t mp_machine_soft_spi_p = {
  289. .init = mp_machine_soft_spi_init,
  290. .deinit = NULL,
  291. .transfer = mp_machine_soft_spi_transfer,
  292. };
  293. const mp_obj_type_t mp_machine_soft_spi_type = {
  294. { &mp_type_type },
  295. .name = MP_QSTR_SoftSPI,
  296. .print = mp_machine_soft_spi_print,
  297. .make_new = mp_machine_spi_make_new, // delegate to master constructor
  298. .protocol = &mp_machine_soft_spi_p,
  299. .locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
  300. };
  301. #endif // MICROPY_PY_MACHINE_SPI