Skip to content

Commit f4a2474

Browse files
committed
spresense: Add support for sdioio
1 parent fcddfd0 commit f4a2474

File tree

7 files changed

+219
-0
lines changed

7 files changed

+219
-0
lines changed

ports/cxd56/boards/spresense/pins.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,21 @@
2424
* THE SOFTWARE.
2525
*/
2626

27+
#include "py/objtuple.h"
28+
2729
#include "shared-bindings/board/__init__.h"
2830

31+
STATIC const mp_rom_obj_tuple_t sdio_data_tuple = {
32+
{&mp_type_tuple},
33+
4,
34+
{
35+
MP_ROM_PTR(&pin_SDIO_DATA0),
36+
MP_ROM_PTR(&pin_SDIO_DATA1),
37+
MP_ROM_PTR(&pin_SDIO_DATA2),
38+
MP_ROM_PTR(&pin_SDIO_DATA3),
39+
}
40+
};
41+
2942
STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
3043
{ MP_ROM_QSTR(MP_QSTR_D0), MP_ROM_PTR(&pin_UART2_RXD) },
3144
{ 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[] = {
7689
{ MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&board_i2c_obj) },
7790
{ MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) },
7891
{ MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) },
92+
{ MP_ROM_QSTR(MP_QSTR_SDIO_CLOCK), MP_ROM_PTR(&pin_SDIO_CLK) },
93+
{ MP_ROM_QSTR(MP_QSTR_SDIO_COMMAND), MP_ROM_PTR(&pin_SDIO_CMD) },
94+
{ MP_ROM_QSTR(MP_QSTR_SDIO_DATA), MP_ROM_PTR(&sdio_data_tuple) },
7995
};
8096
MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table);

ports/cxd56/common-hal/microcontroller/Pin.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ const mcu_pin_obj_t pin_I2S1_BCK = PIN(PIN_I2S1_BCK, false);
7272
const mcu_pin_obj_t pin_I2S1_LRCK = PIN(PIN_I2S1_LRCK, false);
7373
const mcu_pin_obj_t pin_I2S1_DATA_IN = PIN(PIN_I2S1_DATA_IN, false);
7474
const mcu_pin_obj_t pin_I2S1_DATA_OUT = PIN(PIN_I2S1_DATA_OUT, false);
75+
const mcu_pin_obj_t pin_SDIO_CLK = PIN(PIN_SDIO_CLK, false);
76+
const mcu_pin_obj_t pin_SDIO_CMD = PIN(PIN_SDIO_CMD, false);
77+
const mcu_pin_obj_t pin_SDIO_DATA0 = PIN(PIN_SDIO_DATA0, false);
78+
const mcu_pin_obj_t pin_SDIO_DATA1 = PIN(PIN_SDIO_DATA1, false);
79+
const mcu_pin_obj_t pin_SDIO_DATA2 = PIN(PIN_SDIO_DATA2, false);
80+
const mcu_pin_obj_t pin_SDIO_DATA3 = PIN(PIN_SDIO_DATA3, false);
7581
const mcu_pin_obj_t pin_LPADC0 = PIN(0, true);
7682
const mcu_pin_obj_t pin_LPADC1 = PIN(1, true);
7783
const mcu_pin_obj_t pin_LPADC2 = PIN(2, true);

ports/cxd56/common-hal/microcontroller/Pin.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ extern const mcu_pin_obj_t pin_I2S1_BCK;
7777
extern const mcu_pin_obj_t pin_I2S1_LRCK;
7878
extern const mcu_pin_obj_t pin_I2S1_DATA_IN;
7979
extern const mcu_pin_obj_t pin_I2S1_DATA_OUT;
80+
extern const mcu_pin_obj_t pin_SDIO_CLK;
81+
extern const mcu_pin_obj_t pin_SDIO_CMD;
82+
extern const mcu_pin_obj_t pin_SDIO_DATA0;
83+
extern const mcu_pin_obj_t pin_SDIO_DATA1;
84+
extern const mcu_pin_obj_t pin_SDIO_DATA2;
85+
extern const mcu_pin_obj_t pin_SDIO_DATA3;
8086
extern const mcu_pin_obj_t pin_LPADC0;
8187
extern const mcu_pin_obj_t pin_LPADC1;
8288
extern const mcu_pin_obj_t pin_LPADC2;
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
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+
27+
#include <fcntl.h>
28+
#include <unistd.h>
29+
#include <arch/chip/pin.h>
30+
31+
#include "py/mperrno.h"
32+
#include "py/runtime.h"
33+
34+
#include "shared-bindings/sdioio/SDCard.h"
35+
#include "shared-bindings/util.h"
36+
37+
#define DATA_PINS_NUM 4
38+
39+
void common_hal_sdioio_sdcard_construct(sdioio_sdcard_obj_t *self,
40+
const mcu_pin_obj_t *clock, const mcu_pin_obj_t *command,
41+
uint8_t num_data, mcu_pin_obj_t **data, uint32_t frequency) {
42+
struct geometry geo;
43+
44+
if (clock->number != PIN_SDIO_CLK || command->number != PIN_SDIO_CMD) {
45+
mp_raise_ValueError(translate("Invalid pins"));
46+
}
47+
48+
uint8_t data_pins_num = 0;
49+
for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
50+
if (data[i]->number != PIN_SDIO_DATA0 || data[i]->number != PIN_SDIO_DATA1 ||
51+
data[i]->number != PIN_SDIO_DATA2 || data[i]->number != PIN_SDIO_DATA3) {
52+
data_pins_num++;
53+
}
54+
}
55+
56+
if (data_pins_num != DATA_PINS_NUM) {
57+
mp_raise_ValueError(translate("Invalid pins"));
58+
}
59+
60+
if (open_blockdriver("/dev/mmcsd0", 0, &self->inode) < 0) {
61+
mp_raise_ValueError(translate("Could not initialize SDCard"));
62+
}
63+
64+
self->inode->u.i_bops->geometry(self->inode, &geo);
65+
66+
claim_pin(clock);
67+
claim_pin(command);
68+
self->clock_pin = clock;
69+
self->command_pin = command;
70+
for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
71+
claim_pin(data[i]);
72+
self->data_pins[i] = data[i];
73+
}
74+
75+
self->count = geo.geo_nsectors;
76+
self->frequency = frequency;
77+
self->width = num_data;
78+
}
79+
80+
void common_hal_sdioio_sdcard_deinit(sdioio_sdcard_obj_t *self) {
81+
close_blockdriver(self->inode);
82+
self->inode = NULL;
83+
84+
reset_pin_number(self->clock_pin->number);
85+
reset_pin_number(self->command_pin->number);
86+
for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
87+
reset_pin_number(self->data_pins[i]->number);
88+
}
89+
}
90+
91+
bool common_hal_sdioio_sdcard_deinited(sdioio_sdcard_obj_t *self) {
92+
return self->inode == NULL;
93+
}
94+
95+
bool common_hal_sdioio_sdcard_configure(sdioio_sdcard_obj_t *self, uint32_t baudrate, uint8_t width) {
96+
return true;
97+
}
98+
99+
uint32_t common_hal_sdioio_sdcard_get_frequency(sdioio_sdcard_obj_t* self) {
100+
return self->frequency;
101+
}
102+
103+
uint8_t common_hal_sdioio_sdcard_get_width(sdioio_sdcard_obj_t* self) {
104+
return self->width;
105+
}
106+
107+
uint32_t common_hal_sdioio_sdcard_get_count(sdioio_sdcard_obj_t* self) {
108+
return self->count;
109+
}
110+
111+
STATIC void check_whole_block(mp_buffer_info_t *bufinfo) {
112+
if (bufinfo->len % 512) {
113+
mp_raise_ValueError(translate("Buffer length must be a multiple of 512"));
114+
}
115+
}
116+
117+
int common_hal_sdioio_sdcard_readblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) {
118+
if (common_hal_sdioio_sdcard_deinited(self)) {
119+
raise_deinited_error();
120+
}
121+
check_whole_block(bufinfo);
122+
123+
return self->inode->u.i_bops->read(self->inode, bufinfo->buf, start_block, bufinfo->len / 512);
124+
}
125+
126+
int common_hal_sdioio_sdcard_writeblocks(sdioio_sdcard_obj_t* self, uint32_t start_block, mp_buffer_info_t *bufinfo) {
127+
if (common_hal_sdioio_sdcard_deinited(self)) {
128+
raise_deinited_error();
129+
}
130+
check_whole_block(bufinfo);
131+
132+
return self->inode->u.i_bops->write(self->inode, bufinfo->buf, start_block, bufinfo->len / 512);;
133+
}
134+
135+
void common_hal_sdioio_sdcard_never_reset(sdioio_sdcard_obj_t *self) {
136+
never_reset_pin_number(self->clock_pin->number);
137+
never_reset_pin_number(self->command_pin->number);
138+
for (uint8_t i = 0; i < DATA_PINS_NUM; i++) {
139+
never_reset_pin_number(self->data_pins[i]->number);
140+
}
141+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
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+
27+
#ifndef MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H
28+
#define MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H
29+
30+
#include <nuttx/fs/fs.h>
31+
32+
#include "py/obj.h"
33+
34+
#include "common-hal/microcontroller/Pin.h"
35+
36+
typedef struct {
37+
mp_obj_base_t base;
38+
struct inode* inode;
39+
uint32_t frequency;
40+
uint32_t count;
41+
uint8_t width;
42+
const mcu_pin_obj_t *command_pin;
43+
const mcu_pin_obj_t *clock_pin;
44+
const mcu_pin_obj_t *data_pins[4];
45+
} sdioio_sdcard_obj_t;
46+
47+
#endif // MICROPY_INCLUDED_CXD56_SDIOIO_SDCARD_H

ports/cxd56/common-hal/sdioio/__init__.c

Whitespace-only changes.

ports/cxd56/mpconfigport.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ USB_CDC_EP_NUM_DATA_IN = 1
77
USB_MSC_EP_NUM_OUT = 5
88
USB_MSC_EP_NUM_IN = 4
99

10+
MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz
11+
1012
CIRCUITPY_AUDIOBUSIO = 0
1113
CIRCUITPY_AUDIOIO = 0
1214
CIRCUITPY_COUNTIO = 0
@@ -18,6 +20,7 @@ CIRCUITPY_I2CPERIPHERAL = 0
1820
CIRCUITPY_NEOPIXEL_WRITE = 0
1921
CIRCUITPY_NVM = 0
2022
CIRCUITPY_ROTARYIO = 0
23+
CIRCUITPY_SDIOIO = 1
2124
CIRCUITPY_TOUCHIO = 0
2225
CIRCUITPY_USB_HID = 0
2326
CIRCUITPY_USB_MIDI = 0

0 commit comments

Comments
 (0)