pybspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. * Copyright (c) 2015 Daniel Campora
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "py/runtime.h"
  30. #include "py/mperrno.h"
  31. #include "bufhelper.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_mcspi.h"
  34. #include "inc/hw_ints.h"
  35. #include "inc/hw_memmap.h"
  36. #include "rom_map.h"
  37. #include "pin.h"
  38. #include "prcm.h"
  39. #include "spi.h"
  40. #include "pybspi.h"
  41. #include "mpexception.h"
  42. #include "pybsleep.h"
  43. #include "pybpin.h"
  44. #include "pins.h"
  45. /// \moduleref pyb
  46. /// \class SPI - a master-driven serial protocol
  47. /******************************************************************************
  48. DEFINE TYPES
  49. ******************************************************************************/
  50. typedef struct _pyb_spi_obj_t {
  51. mp_obj_base_t base;
  52. uint baudrate;
  53. uint config;
  54. byte polarity;
  55. byte phase;
  56. byte submode;
  57. byte wlen;
  58. } pyb_spi_obj_t;
  59. /******************************************************************************
  60. DEFINE CONSTANTS
  61. ******************************************************************************/
  62. #define PYBSPI_FIRST_BIT_MSB 0
  63. /******************************************************************************
  64. DECLARE PRIVATE DATA
  65. ******************************************************************************/
  66. STATIC pyb_spi_obj_t pyb_spi_obj = {.baudrate = 0};
  67. STATIC const mp_obj_t pyb_spi_def_pin[3] = {&pin_GP14, &pin_GP16, &pin_GP30};
  68. /******************************************************************************
  69. DEFINE PRIVATE FUNCTIONS
  70. ******************************************************************************/
  71. // only master mode is available for the moment
  72. STATIC void pybspi_init (const pyb_spi_obj_t *self) {
  73. // enable the peripheral clock
  74. MAP_PRCMPeripheralClkEnable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  75. MAP_PRCMPeripheralReset(PRCM_GSPI);
  76. MAP_SPIReset(GSPI_BASE);
  77. // configure the interface (only master mode supported)
  78. MAP_SPIConfigSetExpClk (GSPI_BASE, MAP_PRCMPeripheralClockGet(PRCM_GSPI),
  79. self->baudrate, SPI_MODE_MASTER, self->submode, self->config);
  80. // enable the interface
  81. MAP_SPIEnable(GSPI_BASE);
  82. }
  83. STATIC void pybspi_tx (pyb_spi_obj_t *self, const void *data) {
  84. uint32_t txdata;
  85. switch (self->wlen) {
  86. case 1:
  87. txdata = (uint8_t)(*(char *)data);
  88. break;
  89. case 2:
  90. txdata = (uint16_t)(*(uint16_t *)data);
  91. break;
  92. case 4:
  93. txdata = (uint32_t)(*(uint32_t *)data);
  94. break;
  95. default:
  96. return;
  97. }
  98. MAP_SPIDataPut (GSPI_BASE, txdata);
  99. }
  100. STATIC void pybspi_rx (pyb_spi_obj_t *self, void *data) {
  101. uint32_t rxdata;
  102. MAP_SPIDataGet (GSPI_BASE, &rxdata);
  103. if (data) {
  104. switch (self->wlen) {
  105. case 1:
  106. *(char *)data = rxdata;
  107. break;
  108. case 2:
  109. *(uint16_t *)data = rxdata;
  110. break;
  111. case 4:
  112. *(uint32_t *)data = rxdata;
  113. break;
  114. default:
  115. return;
  116. }
  117. }
  118. }
  119. STATIC void pybspi_transfer (pyb_spi_obj_t *self, const char *txdata, char *rxdata, uint32_t len, uint32_t *txchar) {
  120. if (!self->baudrate) {
  121. mp_raise_OSError(MP_EPERM);
  122. }
  123. // send and receive the data
  124. MAP_SPICSEnable(GSPI_BASE);
  125. for (int i = 0; i < len; i += self->wlen) {
  126. pybspi_tx(self, txdata ? (const void *)&txdata[i] : txchar);
  127. pybspi_rx(self, rxdata ? (void *)&rxdata[i] : NULL);
  128. }
  129. MAP_SPICSDisable(GSPI_BASE);
  130. }
  131. /******************************************************************************/
  132. /* MicroPython bindings */
  133. /******************************************************************************/
  134. STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  135. pyb_spi_obj_t *self = self_in;
  136. if (self->baudrate > 0) {
  137. mp_printf(print, "SPI(0, baudrate=%u, bits=%u, polarity=%u, phase=%u, firstbit=SPI.MSB)",
  138. self->baudrate, (self->wlen * 8), self->polarity, self->phase);
  139. } else {
  140. mp_print_str(print, "SPI(0)");
  141. }
  142. }
  143. STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, const mp_arg_val_t *args) {
  144. uint bits;
  145. switch (args[1].u_int) {
  146. case 8:
  147. bits = SPI_WL_8;
  148. break;
  149. case 16:
  150. bits = SPI_WL_16;
  151. break;
  152. case 32:
  153. bits = SPI_WL_32;
  154. break;
  155. default:
  156. goto invalid_args;
  157. break;
  158. }
  159. uint polarity = args[2].u_int;
  160. uint phase = args[3].u_int;
  161. if (polarity > 1 || phase > 1) {
  162. goto invalid_args;
  163. }
  164. uint firstbit = args[4].u_int;
  165. if (firstbit != PYBSPI_FIRST_BIT_MSB) {
  166. goto invalid_args;
  167. }
  168. // build the configuration
  169. self->baudrate = args[0].u_int;
  170. self->wlen = args[1].u_int >> 3;
  171. self->config = bits | SPI_CS_ACTIVELOW | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF;
  172. self->polarity = polarity;
  173. self->phase = phase;
  174. self->submode = (polarity << 1) | phase;
  175. // assign the pins
  176. mp_obj_t pins_o = args[5].u_obj;
  177. if (pins_o != mp_const_none) {
  178. mp_obj_t *pins;
  179. if (pins_o == MP_OBJ_NULL) {
  180. // use the default pins
  181. pins = (mp_obj_t *)pyb_spi_def_pin;
  182. } else {
  183. mp_obj_get_array_fixed_n(pins_o, 3, &pins);
  184. }
  185. pin_assign_pins_af (pins, 3, PIN_TYPE_STD_PU, PIN_FN_SPI, 0);
  186. }
  187. // init the bus
  188. pybspi_init((const pyb_spi_obj_t *)self);
  189. // register it with the sleep module
  190. pyb_sleep_add((const mp_obj_t)self, (WakeUpCB_t)pybspi_init);
  191. return mp_const_none;
  192. invalid_args:
  193. mp_raise_ValueError(mpexception_value_invalid_arguments);
  194. }
  195. static const mp_arg_t pyb_spi_init_args[] = {
  196. { MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
  197. { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} }, // 1MHz
  198. { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
  199. { MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  200. { MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
  201. { MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBSPI_FIRST_BIT_MSB} },
  202. { MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
  203. };
  204. STATIC mp_obj_t pyb_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
  205. // parse args
  206. mp_map_t kw_args;
  207. mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
  208. mp_arg_val_t args[MP_ARRAY_SIZE(pyb_spi_init_args)];
  209. mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_spi_init_args, args);
  210. // check the peripheral id
  211. if (args[0].u_int != 0) {
  212. mp_raise_OSError(MP_ENODEV);
  213. }
  214. // setup the object
  215. pyb_spi_obj_t *self = &pyb_spi_obj;
  216. self->base.type = &pyb_spi_type;
  217. // start the peripheral
  218. pyb_spi_init_helper(self, &args[1]);
  219. return self;
  220. }
  221. STATIC mp_obj_t pyb_spi_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  222. // parse args
  223. mp_arg_val_t args[MP_ARRAY_SIZE(pyb_spi_init_args) - 1];
  224. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_spi_init_args[1], args);
  225. return pyb_spi_init_helper(pos_args[0], args);
  226. }
  227. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_init_obj, 1, pyb_spi_init);
  228. /// \method deinit()
  229. /// Turn off the spi bus.
  230. STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) {
  231. // disable the peripheral
  232. MAP_SPIDisable(GSPI_BASE);
  233. MAP_PRCMPeripheralClkDisable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  234. // invalidate the baudrate
  235. pyb_spi_obj.baudrate = 0;
  236. // unregister it with the sleep module
  237. pyb_sleep_remove((const mp_obj_t)self_in);
  238. return mp_const_none;
  239. }
  240. STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_spi_deinit_obj, pyb_spi_deinit);
  241. STATIC mp_obj_t pyb_spi_write (mp_obj_t self_in, mp_obj_t buf) {
  242. // parse args
  243. pyb_spi_obj_t *self = self_in;
  244. // get the buffer to send from
  245. mp_buffer_info_t bufinfo;
  246. uint8_t data[1];
  247. pyb_buf_get_for_send(buf, &bufinfo, data);
  248. // just send
  249. pybspi_transfer(self, (const char *)bufinfo.buf, NULL, bufinfo.len, NULL);
  250. // return the number of bytes written
  251. return mp_obj_new_int(bufinfo.len);
  252. }
  253. STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_write_obj, pyb_spi_write);
  254. STATIC mp_obj_t pyb_spi_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  255. static const mp_arg_t allowed_args[] = {
  256. { MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
  257. { MP_QSTR_write, MP_ARG_INT, {.u_int = 0x00} },
  258. };
  259. // parse args
  260. pyb_spi_obj_t *self = pos_args[0];
  261. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  262. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
  263. // get the buffer to receive into
  264. vstr_t vstr;
  265. pyb_buf_get_for_recv(args[0].u_obj, &vstr);
  266. // just receive
  267. uint32_t write = args[1].u_int;
  268. pybspi_transfer(self, NULL, vstr.buf, vstr.len, &write);
  269. // return the received data
  270. return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
  271. }
  272. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_read_obj, 1, pyb_spi_read);
  273. STATIC mp_obj_t pyb_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  274. static const mp_arg_t allowed_args[] = {
  275. { MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
  276. { MP_QSTR_write, MP_ARG_INT, {.u_int = 0x00} },
  277. };
  278. // parse args
  279. pyb_spi_obj_t *self = pos_args[0];
  280. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  281. mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
  282. // get the buffer to receive into
  283. vstr_t vstr;
  284. pyb_buf_get_for_recv(args[0].u_obj, &vstr);
  285. // just receive
  286. uint32_t write = args[1].u_int;
  287. pybspi_transfer(self, NULL, vstr.buf, vstr.len, &write);
  288. // return the number of bytes received
  289. return mp_obj_new_int(vstr.len);
  290. }
  291. STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_readinto_obj, 1, pyb_spi_readinto);
  292. STATIC mp_obj_t pyb_spi_write_readinto (mp_obj_t self, mp_obj_t writebuf, mp_obj_t readbuf) {
  293. // get buffers to write from/read to
  294. mp_buffer_info_t bufinfo_write;
  295. uint8_t data_send[1];
  296. mp_buffer_info_t bufinfo_read;
  297. if (writebuf == readbuf) {
  298. // same object for writing and reading, it must be a r/w buffer
  299. mp_get_buffer_raise(writebuf, &bufinfo_write, MP_BUFFER_RW);
  300. bufinfo_read = bufinfo_write;
  301. } else {
  302. // get the buffer to write from
  303. pyb_buf_get_for_send(writebuf, &bufinfo_write, data_send);
  304. // get the read buffer
  305. mp_get_buffer_raise(readbuf, &bufinfo_read, MP_BUFFER_WRITE);
  306. if (bufinfo_read.len != bufinfo_write.len) {
  307. mp_raise_ValueError(mpexception_value_invalid_arguments);
  308. }
  309. }
  310. // send and receive
  311. pybspi_transfer(self, (const char *)bufinfo_write.buf, bufinfo_read.buf, bufinfo_write.len, NULL);
  312. // return the number of transferred bytes
  313. return mp_obj_new_int(bufinfo_write.len);
  314. }
  315. STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_spi_write_readinto_obj, pyb_spi_write_readinto);
  316. STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = {
  317. // instance methods
  318. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_spi_init_obj) },
  319. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_spi_deinit_obj) },
  320. { MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_spi_write_obj) },
  321. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_spi_read_obj) },
  322. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&pyb_spi_readinto_obj) },
  323. { MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&pyb_spi_write_readinto_obj) },
  324. // class constants
  325. { MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(PYBSPI_FIRST_BIT_MSB) },
  326. };
  327. STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
  328. const mp_obj_type_t pyb_spi_type = {
  329. { &mp_type_type },
  330. .name = MP_QSTR_SPI,
  331. .print = pyb_spi_print,
  332. .make_new = pyb_spi_make_new,
  333. .locals_dict = (mp_obj_t)&pyb_spi_locals_dict,
  334. };