Skip to content

Commit 11d2108

Browse files
author
Daniel Campora
committed
cc3200: Rework SD API. Increase heap to avoid malloc failures.
1 parent 34c290b commit 11d2108

11 files changed

Lines changed: 161 additions & 110 deletions

File tree

cc3200/fatfs/src/drivers/sd_diskio.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,6 @@ DSTATUS sd_disk_init (void) {
279279
sd_disk_info.bStatus = 0;
280280
}
281281
}
282-
// Set card rd/wr block len
283-
MAP_SDHostBlockSizeSet(SDHOST_BASE, SD_SECTOR_SIZE);
284282
}
285283

286284
return sd_disk_info.bStatus;
@@ -315,15 +313,6 @@ DSTATUS sd_disk_status (void) {
315313
return sd_disk_info.bStatus;
316314
}
317315

318-
//*****************************************************************************
319-
//
320-
//! Returns wether the sd card is ready to be accessed or not
321-
//
322-
//*****************************************************************************
323-
bool sd_disk_ready (void) {
324-
return (!sd_disk_info.bStatus);
325-
}
326-
327316
//*****************************************************************************
328317
//
329318
//! Reads sector(s) from the disk drive.

cc3200/fatfs/src/drivers/sd_diskio.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extern DiskInfo_t sd_disk_info;
2222
DSTATUS sd_disk_init (void);
2323
void sd_disk_deinit (void);
2424
DSTATUS sd_disk_status (void);
25-
bool sd_disk_ready (void);
2625
DRESULT sd_disk_read (BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount);
2726
DRESULT sd_disk_write (const BYTE* pBuffer, DWORD ulSectorNumber, UINT bSectorCount);
2827

cc3200/ftp/ftp.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
#include "ff.h"
4848
#include "fifo.h"
4949
#include "socketfifo.h"
50-
#include "diskio.h"
51-
#include "sd_diskio.h"
50+
#include "pybsd.h"
5251
#include "updater.h"
5352
#include "timeutils.h"
5453

@@ -193,7 +192,7 @@ static const ftp_month_t ftp_month[] = { { "Jan" }, { "Feb" }, { "Mar" }, { "Apr
193192
{ "May" }, { "Jun" }, { "Jul" }, { "Ago" },
194193
{ "Sep" }, { "Oct" }, { "Nov" }, { "Dec" } };
195194

196-
static SocketFifoElement_t *ftp_fifoelements;
195+
static SocketFifoElement_t ftp_fifoelements[FTP_SOCKETFIFO_ELEMENTS_MAX];
197196
static FIFO_t ftp_socketfifo;
198197

199198
/******************************************************************************
@@ -233,7 +232,6 @@ void ftp_init (void) {
233232
ASSERT ((ftp_path = mem_Malloc(FTP_MAX_PARAM_SIZE)) != NULL);
234233
ASSERT ((ftp_scratch_buffer = mem_Malloc(FTP_MAX_PARAM_SIZE)) != NULL);
235234
ASSERT ((ftp_cmd_buffer = mem_Malloc(FTP_MAX_PARAM_SIZE + FTP_CMD_SIZE_MAX)) != NULL);
236-
ASSERT ((ftp_fifoelements = mem_Malloc(FTP_SOCKETFIFO_ELEMENTS_MAX * sizeof(SocketFifoElement_t))) != NULL);
237235
SOCKETFIFO_Init (&ftp_socketfifo, (void *)ftp_fifoelements, FTP_SOCKETFIFO_ELEMENTS_MAX);
238236
ftp_data.c_sd = -1;
239237
ftp_data.d_sd = -1;
@@ -987,7 +985,7 @@ static ftp_result_t ftp_open_dir_for_listing (const char *path, char *list, uint
987985
if (path[0] == '/' && path[1] == '\0') {
988986
next += ftp_print_eplf_drive((list + next), (maxlistsize - next), "flash");
989987
#if MICROPY_HW_HAS_SDCARD
990-
if (sd_disk_ready()) {
988+
if (pybsd_is_mounted()) {
991989
next += ftp_print_eplf_drive((list + next), (maxlistsize - next), "sd");
992990
}
993991
#endif

cc3200/mods/moduos.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "sflash_diskio.h"
4040
#include "file.h"
4141
#include "random.h"
42-
#include "sd_diskio.h"
42+
#include "pybsd.h"
4343
#include "mpexception.h"
4444
#include "version.h"
4545
#include "timeutils.h"
@@ -60,13 +60,6 @@
6060
/******************************************************************************
6161
DEFINE PRIVATE FUNCTIONS
6262
******************************************************************************/
63-
STATIC bool sd_in_root(void) {
64-
#if MICROPY_HW_HAS_SDCARD
65-
return sd_disk_ready();
66-
#else
67-
return false;
68-
#endif
69-
}
7063

7164
/******************************************************************************/
7265
// Micro Python bindings
@@ -151,7 +144,7 @@ STATIC mp_obj_t os_listdir(mp_uint_t n_args, const mp_obj_t *args) {
151144
mp_obj_t dir_list = mp_obj_new_list(0, NULL);
152145
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_flash));
153146
#if MICROPY_HW_HAS_SDCARD
154-
if (sd_in_root()) {
147+
if (pybsd_is_mounted()) {
155148
mp_obj_list_append(dir_list, MP_OBJ_NEW_QSTR(MP_QSTR_sd));
156149
}
157150
#endif
@@ -280,7 +273,11 @@ STATIC mp_obj_t os_stat(mp_obj_t path_in) {
280273

281274
if (path_equal(path, "/") || path_equal(path, "/flash") || path_equal(path, "/sd")) {
282275
// stat built-in directory
283-
if (path[1] == 's' && !sd_in_root()) {
276+
#if MICROPY_HW_HAS_SDCARD
277+
if (path[1] == 's' && !pybsd_is_mounted()) {
278+
#else
279+
if (path[1] == 's') {
280+
#endif
284281
// no /sd directory
285282
res = FR_NO_PATH;
286283
goto error;
@@ -353,11 +350,21 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
353350
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
354351
#endif
355352

356-
/// \function mkfs('path')
357-
/// Formats the selected drive, useful when the filesystem has been damaged beyond repair
353+
/// \function mkfs('drive')
354+
/// Formats the selected drive, useful when the filesystem has been damaged beyond repair.
355+
/// Path must be either '/sd' or '/flash'
358356
STATIC mp_obj_t os_mkfs(mp_obj_t path_o) {
359357
const char *path = mp_obj_str_get_str(path_o);
360-
if (FR_OK != f_mkfs(path, 1, 0)) {
358+
uint8_t sfd;
359+
360+
if (!strcmp(path, "/flash")) {
361+
sfd = 1;
362+
} else if (!strcmp(path, "/sd")) {
363+
sfd = 0;
364+
} else {
365+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
366+
}
367+
if (FR_OK != f_mkfs(path, sfd, 0)) {
361368
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
362369
}
363370
return mp_const_none;

cc3200/mods/pybsd.c

Lines changed: 107 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,23 @@ typedef struct {
5858
pin_obj_t *pin_clk;
5959
bool pinsset;
6060
bool enabled;
61+
bool mounted;
6162
} pybsd_obj_t;
6263

6364
/******************************************************************************
6465
DECLARE PRIVATE DATA
6566
******************************************************************************/
66-
STATIC pybsd_obj_t pybsd_obj;
67+
STATIC pybsd_obj_t pybsd_obj = {.pinsset = false, .enabled = false, .mounted = false};
6768

6869
/******************************************************************************
6970
DECLARE PRIVATE FUNCTIONS
7071
******************************************************************************/
72+
STATIC void pybsd_hw_init (pybsd_obj_t *self);
7173
STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
72-
STATIC mp_obj_t pybsd_disable (mp_obj_t self_in);
73-
STATIC mp_obj_t pybsd_enable (mp_obj_t self_in);
74+
STATIC mp_obj_t pybsd_init (uint n_args, const mp_obj_t *args);
75+
STATIC mp_obj_t pybsd_deinit (mp_obj_t self_in);
76+
STATIC mp_obj_t pybsd_mount (mp_obj_t self_in);
77+
STATIC mp_obj_t pybsd_unmount (mp_obj_t self_in);
7478

7579
/******************************************************************************
7680
DEFINE PUBLIC FUNCTIONS
@@ -81,15 +85,19 @@ void pybsd_init0 (void) {
8185
ASSERT ((pybsd_obj.fatfs = mem_Malloc(sizeof(FATFS))) != NULL);
8286
}
8387

84-
void pybsd_deinit (void) {
85-
pybsd_disable ((mp_obj_t)&pybsd_obj);
88+
void pybsd_disable (void) {
89+
pybsd_deinit ((mp_obj_t)&pybsd_obj);
90+
}
91+
92+
bool pybsd_is_mounted (void) {
93+
return pybsd_obj.mounted;
8694
}
8795

8896
/******************************************************************************
8997
DEFINE PRIVATE FUNCTIONS
9098
******************************************************************************/
91-
/// Initalizes the sd card driver
92-
STATIC void pybsd_init (pybsd_obj_t *self) {
99+
/// Initalizes the sd card hardware driver
100+
STATIC void pybsd_hw_init (pybsd_obj_t *self) {
93101
// Configure the clock pin as output only
94102
MAP_PinDirModeSet(self->pin_clk->pin_num, PIN_DIR_MODE_OUT);
95103
// Enable SD peripheral clock
@@ -100,102 +108,140 @@ STATIC void pybsd_init (pybsd_obj_t *self) {
100108
MAP_SDHostInit(SDHOST_BASE);
101109
// Configure the card clock
102110
MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), PYBSD_FREQUENCY_HZ);
111+
// Set card rd/wr block len
112+
MAP_SDHostBlockSizeSet(SDHOST_BASE, SD_SECTOR_SIZE);
113+
}
114+
115+
STATIC mp_obj_t pybsd_init_helper (pybsd_obj_t *self, uint n_args, const mp_obj_t *args) {
116+
if (n_args > 0) {
117+
if (mp_obj_get_type(args[0]) == &mp_type_tuple) {
118+
mp_obj_t *items;
119+
mp_obj_get_array_fixed_n(args[0], 6, &items);
120+
121+
// save the clock pin for later use
122+
self->pin_clk = (pin_obj_t *)pin_find(items[2]);
123+
124+
// configure the data pin with pull-up enabled
125+
pin_config ((pin_obj_t *)pin_find(items[0]), mp_obj_get_int(items[1]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
126+
// configure the clock pin
127+
pin_config (self->pin_clk, mp_obj_get_int(items[3]), 0, PIN_TYPE_STD, PIN_STRENGTH_4MA);
128+
// configure the command pin with pull-up enabled
129+
pin_config ((pin_obj_t *)pin_find(items[4]), mp_obj_get_int(items[5]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
130+
self->pinsset = true;
131+
} else {
132+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
133+
}
134+
}
135+
136+
if (!self->enabled) {
137+
if (!self->pinsset) {
138+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
139+
}
140+
pybsd_hw_init (self);
141+
// mark as enabled and register it with the sleep module
142+
self->enabled = true;
143+
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)pybsd_hw_init);
144+
}
145+
return mp_const_none;
103146
}
104147

105148
/******************************************************************************/
106149
// Micro Python bindings
107150
//
108151

109152
/// \classmethod \constructor()
110-
/// Configure the pins used for the sd card.
111-
/// May receive 0, or 6 arguments.
153+
/// Creates an SD card object.
154+
/// Accepts a tuple of pins an alternate functions to configure the SD card interface.
155+
/// When called with no arguments it returns the previoulsy created SD card object.
112156
///
113157
/// Usage:
114158
/// sd = pyb.SD()
115-
////
116-
/// sd = pyb.SD(d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af)
159+
/// Or:
160+
/// sd = pyb.SD((d0_pin, d0_af, clk_pin, clk_af, cmd_pin, cmd_af))
117161
///
118162
STATIC mp_obj_t pybsd_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
119-
mp_arg_check_num(n_args, n_kw, 0, 6, false);
163+
mp_arg_check_num(n_args, n_kw, 0, 1, false);
164+
mp_obj_t self = &pybsd_obj;
165+
pybsd_obj.base.type = &pyb_sd_type;
120166

121-
if (n_args == 6) {
122-
// save the clock pin for later use
123-
pybsd_obj.pin_clk = (pin_obj_t *)pin_find(args[2]);
124-
125-
// configure the data pin with pull-up enabled
126-
pin_config ((pin_obj_t *)pin_find(args[0]), mp_obj_get_int(args[1]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
127-
// configure the clock pin
128-
pin_config (pybsd_obj.pin_clk, mp_obj_get_int(args[3]), 0, PIN_TYPE_STD, PIN_STRENGTH_4MA);
129-
// configure the command pin with pull-up enabled
130-
pin_config ((pin_obj_t *)pin_find(args[4]), mp_obj_get_int(args[5]), 0, PIN_TYPE_STD_PU, PIN_STRENGTH_4MA);
131-
132-
pybsd_obj.pinsset = true;
133-
pybsd_obj.base.type = &pyb_sd_type;
134-
}
135-
else if (!pybsd_obj.pinsset) {
136-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
167+
if (n_args > 0) {
168+
pybsd_init_helper (self, n_args, args);
137169
}
170+
return self;
171+
}
138172

139-
return &pybsd_obj;
173+
/// \method init()
174+
/// Enables the sd card
175+
STATIC mp_obj_t pybsd_init (uint n_args, const mp_obj_t *args) {
176+
return pybsd_init_helper(args[0], n_args - 1, args + 1);
140177
}
178+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pybsd_init_obj, 1, 2, pybsd_init);
141179

142-
/// \method enable()
143-
/// Enables the sd card and mounts the file system
144-
STATIC mp_obj_t pybsd_enable (mp_obj_t self_in) {
180+
/// \method deinit()
181+
/// Disables the sd card
182+
STATIC mp_obj_t pybsd_deinit (mp_obj_t self_in) {
145183
pybsd_obj_t *self = self_in;
184+
if (self->enabled) {
185+
// unmounted in case not done yet
186+
pybsd_unmount (self);
187+
self->enabled = false;
188+
// disable the peripheral
189+
MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
190+
// de-initialze the sd card at diskio level
191+
sd_disk_deinit();
192+
// unregister it with the sleep module
193+
pybsleep_remove (self);
194+
}
195+
return mp_const_none;
196+
}
197+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pybsd_deinit_obj, pybsd_deinit);
146198

147-
if (!self->enabled) {
148-
// do the init first
149-
pybsd_init (self);
150-
199+
/// \method mount()
200+
/// Mount the sd card on /sd
201+
STATIC mp_obj_t pybsd_mount (mp_obj_t self_in) {
202+
pybsd_obj_t *self = self_in;
203+
if (!self->mounted) {
204+
if (!self->enabled) {
205+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
206+
}
151207
// try to mount the sd card on /sd
152208
if (FR_OK != f_mount(self->fatfs, "/sd", 1)) {
153209
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
154210
}
155-
156211
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
157212
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
158-
159-
// register it with the sleep module
160-
pybsleep_add ((const mp_obj_t)&pybsd_obj, (WakeUpCB_t)pybsd_init);
161-
self->enabled = true;
213+
self->mounted = true;
162214
}
163-
164215
return mp_const_none;
165216
}
166-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pybsd_enable_obj, pybsd_enable);
217+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pybsd_mount_obj, pybsd_mount);
167218

168-
/// \method disable()
169-
/// Disables the sd card and unmounts the file system
170-
STATIC mp_obj_t pybsd_disable (mp_obj_t self_in) {
219+
/// \method unmount()
220+
/// Unmount the sd card
221+
STATIC mp_obj_t pybsd_unmount (mp_obj_t self_in) {
171222
pybsd_obj_t *self = self_in;
172-
if (self->enabled) {
173-
self->enabled = false;
223+
if (self->mounted) {
224+
if (!self->enabled) {
225+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
226+
}
174227
// unmount the sd card
175228
f_mount (NULL, "/sd", 1);
176229
// remove sd paths from mp_sys_path
177230
mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd));
178231
mp_obj_list_remove(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_sd_slash_lib));
179-
180-
// disable the peripheral
181-
MAP_PRCMPeripheralClkDisable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
182-
183-
// de-initialze de sd card at diskio level
184-
sd_disk_deinit();
185-
186-
// unregister it with the sleep module
187-
pybsleep_remove (self);
188-
232+
self->mounted = false;
189233
// change the drive in case it was /sd
190234
f_chdrive("/flash");
191235
}
192236
return mp_const_none;
193237
}
194-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pybsd_disable_obj, pybsd_disable);
238+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pybsd_unmount_obj, pybsd_unmount);
195239

196240
STATIC const mp_map_elem_t pybsd_locals_dict_table[] = {
197-
{ MP_OBJ_NEW_QSTR(MP_QSTR_enable), (mp_obj_t)&pybsd_enable_obj },
198-
{ MP_OBJ_NEW_QSTR(MP_QSTR_disable), (mp_obj_t)&pybsd_disable_obj },
241+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pybsd_init_obj },
242+
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pybsd_deinit_obj },
243+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&pybsd_mount_obj },
244+
{ MP_OBJ_NEW_QSTR(MP_QSTR_unmount), (mp_obj_t)&pybsd_unmount_obj },
199245
};
200246
STATIC MP_DEFINE_CONST_DICT(pybsd_locals_dict, pybsd_locals_dict_table);
201247

cc3200/mods/pybsd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
extern const mp_obj_type_t pyb_sd_type;
3131

3232
void pybsd_init0 (void);
33-
void pybsd_deinit (void);
33+
void pybsd_disable (void);
34+
bool pybsd_is_mounted (void);
3435
#endif
3536

3637
#endif // PYBSD_H_

0 commit comments

Comments
 (0)