summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorKamil Tomaszewski <kamil.tomaszewski@sony.com>2020-07-02 15:24:59 +0200
committerKamil Tomaszewski <kamil.tomaszewski@sony.com>2020-07-02 15:26:59 +0200
commitf4a24744477abe0d924ef08781d6e30118cc9982 (patch)
tree26ee2d3e03e79a5d1381d8a034bb6d4c38b9a18a /ports
parentfcddfd0f3953b3633912f6aa963af8353075fc76 (diff)
spresense: Add support for sdioio
Diffstat (limited to 'ports')
-rw-r--r--ports/cxd56/boards/spresense/pins.c16
-rw-r--r--ports/cxd56/common-hal/microcontroller/Pin.c6
-rw-r--r--ports/cxd56/common-hal/microcontroller/Pin.h6
-rw-r--r--ports/cxd56/common-hal/sdioio/SDCard.c141
-rw-r--r--ports/cxd56/common-hal/sdioio/SDCard.h47
-rw-r--r--ports/cxd56/common-hal/sdioio/__init__.c0
-rw-r--r--ports/cxd56/mpconfigport.mk3
7 files changed, 219 insertions, 0 deletions
diff --git a/ports/cxd56/boards/spresense/pins.c b/ports/cxd56/boards/spresense/pins.c
index fcc854590..5028d9155 100644
--- a/ports/cxd56/boards/spresense/pins.c
+++ b/ports/cxd56/boards/spresense/pins.c
@@ -24,8 +24,21 @@
* THE SOFTWARE.
*/
+#include "py/objtuple.h"
+
#include "shared-bindings/board/__init__.h"
+STATIC const mp_rom_obj_tuple_t sdio_data_tuple = {
+ {&mp_type_tuple},
+ 4,
+ {
+ MP_ROM_PTR(&pin_SDIO_DATA0),
+ MP_ROM_PTR(&pin_SDIO_DATA1),
+ MP_ROM_PTR(&pin_SDIO_DATA2),
+ MP_ROM_PTR(&pin_SDIO_DATA3),
+ }
+};
+
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_UART2_RXD) },
{ MP_ROM_QSTR(MP_QSTR_D1), MP_ROM_PTR(&pin_UART2_TXD) },
@@ -76,5 +89,8 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
+ { MP_ROM_QSTR(MP_QSTR_SDIO_CLOCK), MP_ROM_PTR(&pin_SDIO_CLK) },
+ { MP_ROM_QSTR(MP_QSTR_SDIO_COMMAND), MP_ROM_PTR(&pin_SDIO_CMD) },
+ { MP_ROM_QSTR(MP_QSTR_SDIO_DATA), MP_ROM_PTR(&sdio_data_tuple) },
};
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);
diff --git a/ports/cxd56/common-hal/microcontroller/Pin.c b/ports/cxd56/common-hal/microcontroller/Pin.c
index 23377197c..7aba1ad54 100644
--- a/ports/cxd56/common-hal/microcontroller/Pin.c
+++ b/ports/cxd56/common-hal/microcontroller/Pin.c
@@ -72,6 +72,12 @@ const mcu_pin_obj_t pin_I2S1_BCK = PIN(PIN_I2S1_BCK, false);
const mcu_pin_obj_t pin_I2S1_LRCK = PIN(PIN_I2S1_LRCK, false);
const mcu_pin_obj_t pin_I2S1_DATA_IN = PIN(PIN_I2S1_DATA_IN, false);
const mcu_pin_obj_t pin_I2S1_DATA_OUT = PIN(PIN_I2S1_DATA_OUT, false);
+const mcu_pin_obj_t pin_SDIO_CLK = PIN(PIN_SDIO_CLK, false);
+const mcu_pin_obj_t pin_SDIO_CMD = PIN(PIN_SDIO_CMD, false);
+const mcu_pin_obj_t pin_SDIO_DATA0 = PIN(PIN_SDIO_DATA0, false);
+const mcu_pin_obj_t pin_SDIO_DATA1 = PIN(PIN_SDIO_DATA1, false);
+const mcu_pin_obj_t pin_SDIO_DATA2 = PIN(PIN_SDIO_DATA2, false);
+const mcu_pin_obj_t pin_SDIO_DATA3 = PIN(PIN_SDIO_DATA3, false);
const mcu_pin_obj_t pin_LPADC0 = PIN(0, true);
const mcu_pin_obj_t pin_LPADC1 = PIN(1, true);
const mcu_pin_obj_t pin_LPADC2 = PIN(2, true);
diff --git a/ports/cxd56/common-hal/microcontroller/Pin.h b/ports/cxd56/common-hal/microcontroller/Pin.h
index fe6524edb..6759a2dca 100644
--- a/ports/cxd56/common-hal/microcontroller/Pin.h
+++ b/ports/cxd56/common-hal/microcontroller/Pin.h
@@ -77,6 +77,12 @@ extern const mcu_pin_obj_t pin_I2S1_BCK;
extern const mcu_pin_obj_t pin_I2S1_LRCK;
extern const mcu_pin_obj_t pin_I2S1_DATA_IN;
extern const mcu_pin_obj_t pin_I2S1_DATA_OUT;
+extern const mcu_pin_obj_t pin_SDIO_CLK;
+extern const mcu_pin_obj_t pin_SDIO_CMD;
+extern const mcu_pin_obj_t pin_SDIO_DATA0;
+extern const mcu_pin_obj_t pin_SDIO_DATA1;
+extern const mcu_pin_obj_t pin_SDIO_DATA2;
+extern const mcu_pin_obj_t pin_SDIO_DATA3;
extern const mcu_pin_obj_t pin_LPADC0;
extern const mcu_pin_obj_t pin_LPADC1;
extern const mcu_pin_obj_t pin_LPADC2;
diff --git a/ports/cxd56/common-hal/sdioio/SDCard.c b/ports/cxd56/common-hal/sdioio/SDCard.c
new file mode 100644
index 000000000..cf7de422c
--- /dev/null
+++ b/ports/cxd56/common-hal/sdioio/SDCard.c
@@ -0,0 +1,141 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * 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 <fcntl.h>
+#include <unistd.h>
+#include <arch/chip/pin.h>
+
+#include "py/mperrno.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/sdioio/SDCard.h"
+#include "shared-bindings/util.h"
+
+#define DATA_PINS_NUM 4
+
+void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
+ const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command,
+ uint8_t num_data, mcu_pin_obj_t **data, uint32_t frequency) {
+ struct geometry geo;
+
+ if (clock->number != PIN_SDIO_CLK || command->number != PIN_SDIO_CMD) {
+ mp_raise_ValueError(translate("Invalid pins"));
+ }
+
+ uint8_t data_pins_num = 0;
+ for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
+ if (data[i]->number != PIN_SDIO_DATA0 || data[i]->number != PIN_SDIO_DATA1 ||
+ data[i]->number != PIN_SDIO_DATA2 || data[i]->number != PIN_SDIO_DATA3) {
+ data_pins_num++;
+ }
+ }
+
+ if (data_pins_num != DATA_PINS_NUM) {
+ mp_raise_ValueError(translate("Invalid pins"));
+ }
+
+ if (open_blockdriver("/dev/mmcsd0", 0, &self->inode) < 0) {
+ mp_raise_ValueError(translate("Could not initialize SDCard"));
+ }
+
+ self->inode->u.i_bops->geometry(self->inode, &geo);
+
+ claim_pin(clock);
+ claim_pin(command);
+ self->clock_pin = clock;
+ self->command_pin = command;
+ for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
+ claim_pin(data[i]);
+ self->data_pins[i] = data[i];
+ }
+
+ self->count = geo.geo_nsectors;
+ self->frequency = frequency;
+ self->width = num_data;
+}
+
+void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) {
+ close_blockdriver(self->inode);
+ self->inode = NULL;
+
+ reset_pin_number(self->clock_pin->number);
+ reset_pin_number(self->command_pin->number);
+ for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
+ reset_pin_number(self->data_pins[i]->number);
+ }
+}
+
+bool common_hal_sdioio_sdcard_deinited(sdioio_sdcard_obj_t *self) {
+ return self->inode == NULL;
+}
+
+bool common_hal_sdioio_sdcard_configure(sdioio_sdcard_obj_t *self, uint32_t baudrate, uint8_t width) {
+ return true;
+}
+
+uint32_t common_hal_sdioio_sdcard_get_frequency(sdioio_sdcard_obj_t* self) {
+ return self->frequency;
+}
+
+uint8_t common_hal_sdioio_sdcard_get_width(sdioio_sdcard_obj_t* self) {
+ return self->width;
+}
+
+uint32_t common_hal_sdioio_sdcard_get_count(sdioio_sdcard_obj_t* self) {
+ return self->count;
+}
+
+STATIC void check_whole_block(mp_buffer_info_t *bufinfo) {
+ if (bufinfo->len % 512) {
+ mp_raise_ValueError(translate("Buffer length must be a multiple of 512"));
+ }
+}
+
+int common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) {
+ if (common_hal_sdioio_sdcard_deinited(self)) {
+ raise_deinited_error();
+ }
+ check_whole_block(bufinfo);
+
+ return self->inode->u.i_bops->read(self->inode, bufinfo->buf, start_block, bufinfo->len / 512);
+}
+
+int common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) {
+ if (common_hal_sdioio_sdcard_deinited(self)) {
+ raise_deinited_error();
+ }
+ check_whole_block(bufinfo);
+
+ return self->inode->u.i_bops->write(self->inode, bufinfo->buf, start_block, bufinfo->len / 512);;
+}
+
+void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) {
+ never_reset_pin_number(self->clock_pin->number);
+ never_reset_pin_number(self->command_pin->number);
+ for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
+ never_reset_pin_number(self->data_pins[i]->number);
+ }
+}
diff --git a/ports/cxd56/common-hal/sdioio/SDCard.h b/ports/cxd56/common-hal/sdioio/SDCard.h
new file mode 100644
index 000000000..cbdcd4706
--- /dev/null
+++ b/ports/cxd56/common-hal/sdioio/SDCard.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * 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.
+ */
+
+#ifndef MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H
+#define MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H
+
+#include <nuttx/fs/fs.h>
+
+#include "py/obj.h"
+
+#include "common-hal/microcontroller/Pin.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ struct inode* inode;
+ uint32_t frequency;
+ uint32_t count;
+ uint8_t width;
+ const mcu_pin_obj_t *command_pin;
+ const mcu_pin_obj_t *clock_pin;
+ const mcu_pin_obj_t *data_pins[4];
+} sdioio_sdcard_obj_t;
+
+#endif // MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H
diff --git a/ports/cxd56/common-hal/sdioio/__init__.c b/ports/cxd56/common-hal/sdioio/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/ports/cxd56/common-hal/sdioio/__init__.c
diff --git a/ports/cxd56/mpconfigport.mk b/ports/cxd56/mpconfigport.mk
index 23f60f8a7..eb077c07b 100644
--- a/ports/cxd56/mpconfigport.mk
+++ b/ports/cxd56/mpconfigport.mk
@@ -7,6 +7,8 @@ USB_CDC_EP_NUM_DATA_IN = 1
USB_MSC_EP_NUM_OUT = 5
USB_MSC_EP_NUM_IN = 4
+MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
+
CIRCUITPY_AUDIOBUSIO = 0
CIRCUITPY_AUDIOIO = 0
CIRCUITPY_COUNTIO = 0
@@ -18,6 +20,7 @@ CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
CIRCUITPY_ROTARYIO = 0
+CIRCUITPY_SDIOIO = 1
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_USB_MIDI = 0