2015-03-19 14:17:39 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-03-19 14:17:39 +00:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2017-02-22 01:36:23 +00:00
|
|
|
#include "py/mperrno.h"
|
2015-03-19 14:17:39 +00:00
|
|
|
#include "bufhelper.h"
|
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "inc/hw_mcspi.h"
|
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "rom_map.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "prcm.h"
|
|
|
|
#include "spi.h"
|
|
|
|
#include "pybspi.h"
|
|
|
|
#include "pybsleep.h"
|
2015-09-13 20:44:09 +01:00
|
|
|
#include "pybpin.h"
|
|
|
|
#include "pins.h"
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class SPI - a master-driven serial protocol
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE TYPES
|
|
|
|
******************************************************************************/
|
|
|
|
typedef struct _pyb_spi_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
uint baudrate;
|
|
|
|
uint config;
|
2015-05-26 09:32:29 +01:00
|
|
|
byte polarity;
|
|
|
|
byte phase;
|
2015-03-19 14:17:39 +00:00
|
|
|
byte submode;
|
|
|
|
byte wlen;
|
|
|
|
} pyb_spi_obj_t;
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DEFINE CONSTANTS
|
|
|
|
******************************************************************************/
|
2015-09-13 20:44:09 +01:00
|
|
|
#define PYBSPI_FIRST_BIT_MSB 0
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
|
|
|
STATIC pyb_spi_obj_t pyb_spi_obj = {.baudrate = 0};
|
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC const mp_obj_t pyb_spi_def_pin[3] = {&pin_GP14, &pin_GP16, &pin_GP30};
|
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
// only master mode is available for the moment
|
|
|
|
STATIC void pybspi_init (const pyb_spi_obj_t *self) {
|
|
|
|
// enable the peripheral clock
|
|
|
|
MAP_PRCMPeripheralClkEnable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
MAP_PRCMPeripheralReset(PRCM_GSPI);
|
|
|
|
MAP_SPIReset(GSPI_BASE);
|
|
|
|
|
|
|
|
// configure the interface (only master mode supported)
|
|
|
|
MAP_SPIConfigSetExpClk (GSPI_BASE, MAP_PRCMPeripheralClockGet(PRCM_GSPI),
|
|
|
|
self->baudrate, SPI_MODE_MASTER, self->submode, self->config);
|
|
|
|
|
|
|
|
// enable the interface
|
|
|
|
MAP_SPIEnable(GSPI_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void pybspi_tx (pyb_spi_obj_t *self, const void *data) {
|
2015-09-13 20:44:09 +01:00
|
|
|
uint32_t txdata;
|
|
|
|
switch (self->wlen) {
|
|
|
|
case 1:
|
|
|
|
txdata = (uint8_t)(*(char *)data);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
txdata = (uint16_t)(*(uint16_t *)data);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
txdata = (uint32_t)(*(uint32_t *)data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
MAP_SPIDataPut (GSPI_BASE, txdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void pybspi_rx (pyb_spi_obj_t *self, void *data) {
|
|
|
|
uint32_t rxdata;
|
|
|
|
MAP_SPIDataGet (GSPI_BASE, &rxdata);
|
|
|
|
if (data) {
|
|
|
|
switch (self->wlen) {
|
|
|
|
case 1:
|
|
|
|
*(char *)data = rxdata;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*(uint16_t *)data = rxdata;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*(uint32_t *)data = rxdata;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC void pybspi_transfer (pyb_spi_obj_t *self, const char *txdata, char *rxdata, uint32_t len, uint32_t *txchar) {
|
|
|
|
if (!self->baudrate) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EPERM);
|
2015-09-13 20:44:09 +01:00
|
|
|
}
|
2015-03-19 14:17:39 +00:00
|
|
|
// send and receive the data
|
|
|
|
MAP_SPICSEnable(GSPI_BASE);
|
2015-09-13 20:44:09 +01:00
|
|
|
for (int i = 0; i < len; i += self->wlen) {
|
|
|
|
pybspi_tx(self, txdata ? (const void *)&txdata[i] : txchar);
|
2015-03-19 14:17:39 +00:00
|
|
|
pybspi_rx(self, rxdata ? (void *)&rxdata[i] : NULL);
|
|
|
|
}
|
|
|
|
MAP_SPICSDisable(GSPI_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
/* MicroPython bindings */
|
2015-03-19 14:17:39 +00:00
|
|
|
/******************************************************************************/
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-03-19 14:17:39 +00:00
|
|
|
pyb_spi_obj_t *self = self_in;
|
|
|
|
if (self->baudrate > 0) {
|
2016-10-25 23:11:14 +01:00
|
|
|
mp_printf(print, "SPI(0, baudrate=%u, bits=%u, polarity=%u, phase=%u, firstbit=SPI.MSB)",
|
2015-09-13 20:44:09 +01:00
|
|
|
self->baudrate, (self->wlen * 8), self->polarity, self->phase);
|
2015-08-02 19:20:03 +01:00
|
|
|
} else {
|
2015-09-13 20:44:09 +01:00
|
|
|
mp_print_str(print, "SPI(0)");
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-16 13:09:51 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, const mp_arg_val_t *args) {
|
2015-03-19 14:17:39 +00:00
|
|
|
uint bits;
|
2016-10-25 23:11:14 +01:00
|
|
|
switch (args[1].u_int) {
|
2015-03-19 14:17:39 +00:00
|
|
|
case 8:
|
|
|
|
bits = SPI_WL_8;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
bits = SPI_WL_16;
|
|
|
|
break;
|
|
|
|
case 32:
|
|
|
|
bits = SPI_WL_32;
|
|
|
|
break;
|
|
|
|
default:
|
2015-05-26 11:27:13 +01:00
|
|
|
goto invalid_args;
|
2015-03-19 14:17:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-25 23:11:14 +01:00
|
|
|
uint polarity = args[2].u_int;
|
|
|
|
uint phase = args[3].u_int;
|
2015-05-27 08:42:32 +01:00
|
|
|
if (polarity > 1 || phase > 1) {
|
|
|
|
goto invalid_args;
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 23:11:14 +01:00
|
|
|
uint firstbit = args[4].u_int;
|
2015-09-13 20:44:09 +01:00
|
|
|
if (firstbit != PYBSPI_FIRST_BIT_MSB) {
|
2015-05-26 11:27:13 +01:00
|
|
|
goto invalid_args;
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// build the configuration
|
2016-10-25 23:11:14 +01:00
|
|
|
self->baudrate = args[0].u_int;
|
|
|
|
self->wlen = args[1].u_int >> 3;
|
2015-09-13 20:44:09 +01:00
|
|
|
self->config = bits | SPI_CS_ACTIVELOW | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF;
|
2015-05-26 09:32:29 +01:00
|
|
|
self->polarity = polarity;
|
|
|
|
self->phase = phase;
|
2015-05-27 08:42:32 +01:00
|
|
|
self->submode = (polarity << 1) | phase;
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
// assign the pins
|
2016-10-25 23:11:14 +01:00
|
|
|
mp_obj_t pins_o = args[5].u_obj;
|
2015-09-13 20:44:09 +01:00
|
|
|
if (pins_o != mp_const_none) {
|
|
|
|
mp_obj_t *pins;
|
|
|
|
if (pins_o == MP_OBJ_NULL) {
|
|
|
|
// use the default pins
|
|
|
|
pins = (mp_obj_t *)pyb_spi_def_pin;
|
|
|
|
} else {
|
2015-09-27 08:28:27 +01:00
|
|
|
mp_obj_get_array_fixed_n(pins_o, 3, &pins);
|
2015-09-13 20:44:09 +01:00
|
|
|
}
|
2015-09-27 08:28:27 +01:00
|
|
|
pin_assign_pins_af (pins, 3, PIN_TYPE_STD_PU, PIN_FN_SPI, 0);
|
2015-09-13 20:44:09 +01:00
|
|
|
}
|
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// init the bus
|
|
|
|
pybspi_init((const pyb_spi_obj_t *)self);
|
|
|
|
|
|
|
|
// register it with the sleep module
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_add((const mp_obj_t)self, (WakeUpCB_t)pybspi_init);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
2015-05-26 11:27:13 +01:00
|
|
|
|
|
|
|
invalid_args:
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value"));
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
static const mp_arg_t pyb_spi_init_args[] = {
|
|
|
|
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} }, // 1MHz
|
|
|
|
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
|
|
|
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBSPI_FIRST_BIT_MSB} },
|
|
|
|
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
};
|
2017-01-04 13:10:42 +00:00
|
|
|
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) {
|
2015-09-13 20:44:09 +01:00
|
|
|
// parse args
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_spi_init_args)];
|
|
|
|
mp_arg_parse_all(n_args, all_args, &kw_args, MP_ARRAY_SIZE(args), pyb_spi_init_args, args);
|
|
|
|
|
|
|
|
// check the peripheral id
|
|
|
|
if (args[0].u_int != 0) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_ENODEV);
|
2015-09-13 20:44:09 +01:00
|
|
|
}
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
// setup the object
|
2015-03-19 14:17:39 +00:00
|
|
|
pyb_spi_obj_t *self = &pyb_spi_obj;
|
|
|
|
self->base.type = &pyb_spi_type;
|
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
// start the peripheral
|
|
|
|
pyb_spi_init_helper(self, &args[1]);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-13 20:44:09 +01:00
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_spi_init_args) - 1];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), &pyb_spi_init_args[1], args);
|
|
|
|
return pyb_spi_init_helper(pos_args[0], args);
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_init_obj, 1, pyb_spi_init);
|
|
|
|
|
|
|
|
/// \method deinit()
|
|
|
|
/// Turn off the spi bus.
|
|
|
|
STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) {
|
2015-03-26 09:25:28 +00:00
|
|
|
// disable the peripheral
|
|
|
|
MAP_SPIDisable(GSPI_BASE);
|
|
|
|
MAP_PRCMPeripheralClkDisable(PRCM_GSPI, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
// invalidate the baudrate
|
|
|
|
pyb_spi_obj.baudrate = 0;
|
2015-03-19 14:17:39 +00:00
|
|
|
// unregister it with the sleep module
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_remove((const mp_obj_t)self_in);
|
2015-03-19 14:17:39 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_spi_deinit_obj, pyb_spi_deinit);
|
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_write (mp_obj_t self_in, mp_obj_t buf) {
|
2015-08-02 19:20:03 +01:00
|
|
|
// parse args
|
2015-09-13 20:44:09 +01:00
|
|
|
pyb_spi_obj_t *self = self_in;
|
2015-08-02 19:20:03 +01:00
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// get the buffer to send from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
2015-09-13 20:44:09 +01:00
|
|
|
pyb_buf_get_for_send(buf, &bufinfo, data);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
// just send
|
2015-09-13 20:44:09 +01:00
|
|
|
pybspi_transfer(self, (const char *)bufinfo.buf, NULL, bufinfo.len, NULL);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
// return the number of bytes written
|
|
|
|
return mp_obj_new_int(bufinfo.len);
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_spi_write_obj, pyb_spi_write);
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_read(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-08-02 19:20:03 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2015-09-13 20:44:09 +01:00
|
|
|
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
2016-10-18 00:16:46 +01:00
|
|
|
{ MP_QSTR_write, MP_ARG_INT, {.u_int = 0x00} },
|
2015-08-02 19:20:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
pyb_spi_obj_t *self = pos_args[0];
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2015-09-13 20:44:09 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
|
2015-08-02 19:20:03 +01:00
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// get the buffer to receive into
|
|
|
|
vstr_t vstr;
|
2015-09-13 20:44:09 +01:00
|
|
|
pyb_buf_get_for_recv(args[0].u_obj, &vstr);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
// just receive
|
2015-09-13 20:44:09 +01:00
|
|
|
uint32_t write = args[1].u_int;
|
|
|
|
pybspi_transfer(self, NULL, vstr.buf, vstr.len, &write);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
// return the received data
|
2015-09-13 20:44:09 +01:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_read_obj, 1, pyb_spi_read);
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-08-02 19:20:03 +01:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2015-09-13 20:44:09 +01:00
|
|
|
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
2016-10-18 00:16:46 +01:00
|
|
|
{ MP_QSTR_write, MP_ARG_INT, {.u_int = 0x00} },
|
2015-08-02 19:20:03 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
pyb_spi_obj_t *self = pos_args[0];
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2015-09-13 20:44:09 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
|
|
|
|
|
|
|
|
// get the buffer to receive into
|
|
|
|
vstr_t vstr;
|
|
|
|
pyb_buf_get_for_recv(args[0].u_obj, &vstr);
|
|
|
|
|
|
|
|
// just receive
|
|
|
|
uint32_t write = args[1].u_int;
|
|
|
|
pybspi_transfer(self, NULL, vstr.buf, vstr.len, &write);
|
|
|
|
|
|
|
|
// return the number of bytes received
|
|
|
|
return mp_obj_new_int(vstr.len);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_spi_readinto_obj, 1, pyb_spi_readinto);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC mp_obj_t pyb_spi_write_readinto (mp_obj_t self, mp_obj_t writebuf, mp_obj_t readbuf) {
|
|
|
|
// get buffers to write from/read to
|
|
|
|
mp_buffer_info_t bufinfo_write;
|
2015-03-19 14:17:39 +00:00
|
|
|
uint8_t data_send[1];
|
2015-09-13 20:44:09 +01:00
|
|
|
mp_buffer_info_t bufinfo_read;
|
|
|
|
|
|
|
|
if (writebuf == readbuf) {
|
|
|
|
// same object for writing and reading, it must be a r/w buffer
|
|
|
|
mp_get_buffer_raise(writebuf, &bufinfo_write, MP_BUFFER_RW);
|
|
|
|
bufinfo_read = bufinfo_write;
|
2015-03-19 14:17:39 +00:00
|
|
|
} else {
|
2015-09-13 20:44:09 +01:00
|
|
|
// get the buffer to write from
|
|
|
|
pyb_buf_get_for_send(writebuf, &bufinfo_write, data_send);
|
|
|
|
|
|
|
|
// get the read buffer
|
|
|
|
mp_get_buffer_raise(readbuf, &bufinfo_read, MP_BUFFER_WRITE);
|
|
|
|
if (bufinfo_read.len != bufinfo_write.len) {
|
2020-03-02 11:35:22 +00:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid argument(s) value"));
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send and receive
|
2015-09-13 20:44:09 +01:00
|
|
|
pybspi_transfer(self, (const char *)bufinfo_write.buf, bufinfo_read.buf, bufinfo_write.len, NULL);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2015-09-13 20:44:09 +01:00
|
|
|
// return the number of transferred bytes
|
|
|
|
return mp_obj_new_int(bufinfo_write.len);
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
2015-09-13 20:44:09 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_3(pyb_spi_write_readinto_obj, pyb_spi_write_readinto);
|
2015-03-19 14:17:39 +00:00
|
|
|
|
2017-08-21 12:34:23 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_spi_locals_dict_table[] = {
|
2015-03-19 14:17:39 +00:00
|
|
|
// instance methods
|
2017-08-21 12:34:23 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_spi_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_spi_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&pyb_spi_write_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&pyb_spi_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&pyb_spi_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&pyb_spi_write_readinto_obj) },
|
2015-03-19 14:17:39 +00:00
|
|
|
|
|
|
|
// class constants
|
2017-08-21 12:34:23 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(PYBSPI_FIRST_BIT_MSB) },
|
2015-03-19 14:17:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_spi_locals_dict, pyb_spi_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t pyb_spi_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_SPI,
|
|
|
|
.print = pyb_spi_print,
|
|
|
|
.make_new = pyb_spi_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_spi_locals_dict,
|
|
|
|
};
|