2016-06-07 15:40:56 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "ets_sys.h"
|
|
|
|
#include "etshal.h"
|
|
|
|
#include "ets_alt_task.h"
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stream.h"
|
|
|
|
#include "py/mphal.h"
|
2016-09-01 01:29:20 -04:00
|
|
|
#include "extmod/machine_spi.h"
|
2016-12-07 22:41:58 -05:00
|
|
|
#include "modmachine.h"
|
2016-06-07 15:40:56 -04:00
|
|
|
#include "hspi.h"
|
|
|
|
|
2018-05-17 09:37:12 -04:00
|
|
|
#if MICROPY_PY_MACHINE_SPI
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
typedef struct _machine_hspi_obj_t {
|
2016-06-07 15:40:56 -04:00
|
|
|
mp_obj_base_t base;
|
|
|
|
uint32_t baudrate;
|
|
|
|
uint8_t polarity;
|
|
|
|
uint8_t phase;
|
2016-12-07 22:41:58 -05:00
|
|
|
} machine_hspi_obj_t;
|
2016-06-07 15:40:56 -04:00
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
STATIC void machine_hspi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
|
2016-09-01 01:29:20 -04:00
|
|
|
(void)self_in;
|
|
|
|
|
2016-10-02 21:39:31 -04:00
|
|
|
if (dest == NULL) {
|
2016-09-01 01:29:20 -04:00
|
|
|
// fast case when we only need to write data
|
|
|
|
size_t chunk_size = 1024;
|
2016-10-02 21:39:31 -04:00
|
|
|
size_t count = len / chunk_size;
|
2016-09-01 01:29:20 -04:00
|
|
|
size_t i = 0;
|
|
|
|
for (size_t j = 0; j < count; ++j) {
|
|
|
|
for (size_t k = 0; k < chunk_size; ++k) {
|
2016-10-02 21:39:31 -04:00
|
|
|
spi_tx8fast(HSPI, src[i]);
|
2016-09-01 01:29:20 -04:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
ets_loop_iter();
|
|
|
|
}
|
2016-10-02 21:39:31 -04:00
|
|
|
while (i < len) {
|
|
|
|
spi_tx8fast(HSPI, src[i]);
|
2016-09-01 01:29:20 -04:00
|
|
|
++i;
|
|
|
|
}
|
2017-12-13 18:40:35 -05:00
|
|
|
// wait for SPI transaction to complete
|
|
|
|
while (spi_busy(HSPI)) {
|
|
|
|
}
|
2016-09-01 01:29:20 -04:00
|
|
|
} else {
|
|
|
|
// we need to read and write data
|
|
|
|
|
|
|
|
// Process data in chunks, let the pending tasks run in between
|
|
|
|
size_t chunk_size = 1024; // TODO this should depend on baudrate
|
2016-10-02 21:39:31 -04:00
|
|
|
size_t count = len / chunk_size;
|
2016-09-01 01:29:20 -04:00
|
|
|
size_t i = 0;
|
|
|
|
for (size_t j = 0; j < count; ++j) {
|
|
|
|
for (size_t k = 0; k < chunk_size; ++k) {
|
2016-10-02 21:39:31 -04:00
|
|
|
dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
|
2016-09-01 01:29:20 -04:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
ets_loop_iter();
|
|
|
|
}
|
2016-10-02 21:39:31 -04:00
|
|
|
while (i < len) {
|
|
|
|
dest[i] = spi_transaction(HSPI, 0, 0, 0, 0, 8, src[i], 8, 0);
|
2016-09-01 01:29:20 -04:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:40:56 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
// MicroPython bindings for HSPI
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
STATIC void machine_hspi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
machine_hspi_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2016-09-04 13:33:11 -04:00
|
|
|
mp_printf(print, "HSPI(id=1, baudrate=%u, polarity=%u, phase=%u)",
|
2016-06-07 15:40:56 -04:00
|
|
|
self->baudrate, self->polarity, self->phase);
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
STATIC void machine_hspi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2020-02-26 23:36:53 -05:00
|
|
|
machine_hspi_obj_t *self = (machine_hspi_obj_t *)self_in;
|
2016-12-07 22:41:58 -05:00
|
|
|
|
|
|
|
enum { ARG_baudrate, ARG_polarity, ARG_phase };
|
2016-06-07 15:40:56 -04:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
|
|
|
|
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
|
|
|
|
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args),
|
2020-02-26 23:36:53 -05:00
|
|
|
allowed_args, args);
|
2016-06-07 15:40:56 -04:00
|
|
|
|
|
|
|
if (args[ARG_baudrate].u_int != -1) {
|
|
|
|
self->baudrate = args[ARG_baudrate].u_int;
|
|
|
|
}
|
|
|
|
if (args[ARG_polarity].u_int != -1) {
|
|
|
|
self->polarity = args[ARG_polarity].u_int;
|
|
|
|
}
|
|
|
|
if (args[ARG_phase].u_int != -1) {
|
|
|
|
self->phase = args[ARG_phase].u_int;
|
|
|
|
}
|
|
|
|
if (self->baudrate == 80000000L) {
|
|
|
|
// Special case for full speed.
|
|
|
|
spi_init_gpio(HSPI, SPI_CLK_80MHZ_NODIV);
|
|
|
|
spi_clock(HSPI, 0, 0);
|
|
|
|
} else if (self->baudrate > 40000000L) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("impossible baudrate"));
|
2016-06-07 15:40:56 -04:00
|
|
|
} else {
|
|
|
|
uint32_t divider = 40000000L / self->baudrate;
|
|
|
|
uint16_t prediv = MIN(divider, SPI_CLKDIV_PRE + 1);
|
|
|
|
uint16_t cntdiv = (divider / prediv) * 2; // cntdiv has to be even
|
|
|
|
if (cntdiv > SPI_CLKCNT_N + 1 || cntdiv == 0 || prediv == 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("impossible baudrate"));
|
2016-06-07 15:40:56 -04:00
|
|
|
}
|
|
|
|
self->baudrate = 80000000L / (prediv * cntdiv);
|
|
|
|
spi_init_gpio(HSPI, SPI_CLK_USE_DIV);
|
|
|
|
spi_clock(HSPI, prediv, cntdiv);
|
|
|
|
}
|
|
|
|
// TODO: Make the byte order configurable too (discuss param names)
|
|
|
|
spi_tx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW);
|
|
|
|
spi_rx_byte_order(HSPI, SPI_BYTE_ORDER_HIGH_TO_LOW);
|
|
|
|
CLEAR_PERI_REG_MASK(SPI_USER(HSPI), SPI_FLASH_MODE | SPI_USR_MISO |
|
2020-02-26 23:36:53 -05:00
|
|
|
SPI_USR_ADDR | SPI_USR_COMMAND | SPI_USR_DUMMY);
|
2016-06-07 15:40:56 -04:00
|
|
|
// Clear Dual or Quad lines transmission mode
|
|
|
|
CLEAR_PERI_REG_MASK(SPI_CTRL(HSPI), SPI_QIO_MODE | SPI_DIO_MODE |
|
2020-02-26 23:36:53 -05:00
|
|
|
SPI_DOUT_MODE | SPI_QOUT_MODE);
|
2016-06-07 15:40:56 -04:00
|
|
|
spi_mode(HSPI, self->phase, self->polarity);
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
mp_obj_t machine_hspi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2020-09-25 00:47:34 -04:00
|
|
|
MP_MACHINE_SPI_CHECK_FOR_LEGACY_SOFTSPI_CONSTRUCTION(n_args, n_kw, args);
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
// args[0] holds the id of the peripheral
|
|
|
|
if (args[0] != MP_OBJ_NEW_SMALL_INT(1)) {
|
2016-09-04 13:33:11 -04:00
|
|
|
// FlashROM is on SPI0, so far we don't support its usage
|
2017-10-24 07:39:36 -04:00
|
|
|
mp_raise_ValueError(NULL);
|
2016-09-04 13:33:11 -04:00
|
|
|
}
|
|
|
|
|
2022-04-22 03:09:15 -04:00
|
|
|
machine_hspi_obj_t *self = mp_obj_malloc(machine_hspi_obj_t, &machine_hspi_type);
|
2016-06-07 15:40:56 -04:00
|
|
|
// set defaults
|
|
|
|
self->baudrate = 80000000L;
|
|
|
|
self->polarity = 0;
|
|
|
|
self->phase = 0;
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
2020-02-26 23:36:53 -05:00
|
|
|
machine_hspi_init((mp_obj_base_t *)self, n_args - 1, args + 1, &kw_args);
|
2016-06-07 15:40:56 -04:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2016-12-07 22:41:58 -05:00
|
|
|
STATIC const mp_machine_spi_p_t machine_hspi_p = {
|
|
|
|
.init = machine_hspi_init,
|
|
|
|
.transfer = machine_hspi_transfer,
|
2016-09-01 01:29:20 -04:00
|
|
|
};
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
machine_hspi_type,
|
|
|
|
MP_QSTR_HSPI,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, machine_hspi_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, machine_hspi_print,
|
|
|
|
protocol, &machine_hspi_p,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &mp_machine_spi_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|
2018-05-17 09:37:12 -04:00
|
|
|
|
|
|
|
#endif // MICROPY_PY_MACHINE_SPI
|