summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--examples/unix/ffi_example.py1
-rw-r--r--py/binary.c126
-rw-r--r--py/binary.h7
-rw-r--r--py/objarray.c110
-rw-r--r--py/objexcept.c1
-rw-r--r--py/objint.h4
-rw-r--r--py/objint_longlong.c2
-rw-r--r--py/py.mk1
-rw-r--r--stm/main.c22
-rw-r--r--stm/sdcard.c6
-rw-r--r--stm/sdcard.h1
-rw-r--r--stm/stmusbd/usbd_storage_msd.c376
-rw-r--r--stm/stmusbd/usbd_storage_msd.h6
-rw-r--r--unix/ffi.c21
14 files changed, 397 insertions, 287 deletions
diff --git a/examples/unix/ffi_example.py b/examples/unix/ffi_example.py
index 0ac12203e6..bc3919d40a 100644
--- a/examples/unix/ffi_example.py
+++ b/examples/unix/ffi_example.py
@@ -19,6 +19,7 @@ print()
perror("ffi before error")
open("somethingnonexistent__", 0)
print(errno)
+print(errno.get())
perror("ffi after error")
print()
diff --git a/py/binary.c b/py/binary.c
new file mode 100644
index 0000000000..b28eb6426c
--- /dev/null
+++ b/py/binary.c
@@ -0,0 +1,126 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <assert.h>
+
+#include "misc.h"
+#include "mpconfig.h"
+#include "qstr.h"
+#include "obj.h"
+#include "objint.h"
+#include "binary.h"
+
+// Helpers to work with binary-encoded data
+
+int mp_binary_get_size(char typecode) {
+ // This assumes that unsigned and signed types are of the same type,
+ // which is invariant for [u]intN_t.
+ switch (typecode) {
+ case BYTEARRAY_TYPECODE:
+ case 'b':
+ case 'B':
+ return sizeof(int8_t);
+ case 'h':
+ case 'H':
+ return sizeof(int16_t);
+ case 'i':
+ case 'I':
+ return sizeof(int32_t);
+ case 'l':
+ case 'L':
+ return sizeof(int32_t);
+ case 'q':
+ case 'Q':
+ return sizeof(long long);
+#if MICROPY_ENABLE_FLOAT
+ case 'f':
+ return sizeof(float);
+ case 'd':
+ return sizeof(double);
+#endif
+ }
+ return -1;
+}
+
+mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
+ machine_int_t val = 0;
+ switch (typecode) {
+ case 'b':
+ val = ((int8_t*)p)[index];
+ break;
+ case BYTEARRAY_TYPECODE:
+ case 'B':
+ val = ((uint8_t*)p)[index];
+ break;
+ case 'h':
+ val = ((int16_t*)p)[index];
+ break;
+ case 'H':
+ val = ((uint16_t*)p)[index];
+ break;
+ case 'i':
+ case 'l':
+ return mp_obj_new_int(((int32_t*)p)[index]);
+ case 'I':
+ case 'L':
+ return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+ case 'q':
+ case 'Q':
+ // TODO: Explode API more to cover signedness
+ return mp_obj_new_int_from_ll(((long long*)p)[index]);
+#endif
+#if MICROPY_ENABLE_FLOAT
+ case 'f':
+ return mp_obj_new_float(((float*)p)[index]);
+ case 'd':
+ return mp_obj_new_float(((double*)p)[index]);
+#endif
+ }
+ return MP_OBJ_NEW_SMALL_INT(val);
+}
+
+void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
+ machine_int_t val = 0;
+ if (MP_OBJ_IS_INT(val_in)) {
+ val = mp_obj_int_get(val_in);
+ }
+
+ switch (typecode) {
+ case 'b':
+ ((int8_t*)p)[index] = val;
+ break;
+ case BYTEARRAY_TYPECODE:
+ case 'B':
+ val = ((uint8_t*)p)[index] = val;
+ break;
+ case 'h':
+ val = ((int16_t*)p)[index] = val;
+ break;
+ case 'H':
+ val = ((uint16_t*)p)[index] = val;
+ break;
+ case 'i':
+ case 'l':
+ ((int32_t*)p)[index] = val;
+ break;
+ case 'I':
+ case 'L':
+ ((uint32_t*)p)[index] = val;
+ break;
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+ case 'q':
+ case 'Q':
+ assert(0);
+ ((long long*)p)[index] = val;
+ break;
+#endif
+#if MICROPY_ENABLE_FLOAT
+ case 'f':
+ ((float*)p)[index] = mp_obj_float_get(val_in);
+ break;
+ case 'd':
+ ((double*)p)[index] = mp_obj_float_get(val_in);
+ break;
+#endif
+ }
+}
diff --git a/py/binary.h b/py/binary.h
new file mode 100644
index 0000000000..0bd6ad17ac
--- /dev/null
+++ b/py/binary.h
@@ -0,0 +1,7 @@
+// Use special typecode to differentiate repr() of bytearray vs array.array('B')
+// (underlyingly they're same).
+#define BYTEARRAY_TYPECODE 0
+
+int mp_binary_get_size(char typecode);
+mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
+void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);
diff --git a/py/objarray.c b/py/objarray.c
index b4cea6c8d1..d1bc08b150 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -12,10 +12,7 @@
#include "map.h"
#include "runtime0.h"
#include "runtime.h"
-
-// Use special typecode to differentiate repr() of bytearray vs array.array('B')
-// (underlyingly they're same).
-#define BYTEARRAY_TYPECODE 0
+#include "binary.h"
typedef struct _mp_obj_array_t {
mp_obj_base_t base;
@@ -36,91 +33,6 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
/******************************************************************************/
/* array */
-STATIC machine_int_t array_get_el_size(char typecode) {
- // This assumes that unsigned and signed types are of the same type,
- // which is invariant for [u]intN_t.
- switch (typecode) {
- case BYTEARRAY_TYPECODE:
- case 'b':
- case 'B':
- return sizeof(int8_t);
- case 'h':
- case 'H':
- return sizeof(int16_t);
- case 'i':
- case 'I':
- return sizeof(int32_t);
- case 'l':
- case 'L':
- return sizeof(int32_t);
- }
- return -1;
-}
-
-STATIC machine_int_t array_get_el(mp_obj_array_t *o, int index) {
- machine_int_t val = 0;
- switch (o->typecode) {
- case 'b':
- val = ((int8_t*)o->items)[index];
- break;
- case BYTEARRAY_TYPECODE:
- case 'B':
- val = ((uint8_t*)o->items)[index];
- break;
- case 'h':
- val = ((int16_t*)o->items)[index];
- break;
- case 'H':
- val = ((uint16_t*)o->items)[index];
- break;
- case 'i':
- val = ((int32_t*)o->items)[index];
- break;
- case 'I':
- val = ((uint32_t*)o->items)[index];
- break;
- case 'l':
- val = ((int32_t*)o->items)[index];
- break;
- case 'L':
- val = ((uint32_t*)o->items)[index];
- break;
- }
- return val;
-}
-
-STATIC void array_set_el(mp_obj_array_t *o, int index, mp_obj_t val_in) {
- machine_int_t val = mp_obj_int_get(val_in);
- switch (o->typecode) {
- case 'b':
- ((int8_t*)o->items)[index] = val;
- break;
- case BYTEARRAY_TYPECODE:
- case 'B':
- ((uint8_t*)o->items)[index] = val;
- break;
- case 'h':
- ((int16_t*)o->items)[index] = val;
- break;
- case 'H':
- ((uint16_t*)o->items)[index] = val;
- break;
- case 'i':
- ((int32_t*)o->items)[index] = val;
- break;
- case 'I':
- ((uint32_t*)o->items)[index] = val;
- break;
- case 'l':
- ((int32_t*)o->items)[index] = val;
- break;
- case 'L':
- ((uint32_t*)o->items)[index] = val;
- break;
- }
-}
-
-
STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
mp_obj_array_t *o = o_in;
if (o->typecode == BYTEARRAY_TYPECODE) {
@@ -134,7 +46,7 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
if (i > 0) {
print(env, ", ");
}
- print(env, "%d", array_get_el(o, i));
+ mp_obj_print_helper(print, env, mp_binary_get_val(o->typecode, o->items, i), PRINT_REPR);
}
print(env, "]");
}
@@ -161,7 +73,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
if (len == 0) {
array_append(array, item);
} else {
- array_set_el(array, i++, item);
+ mp_binary_set_val(typecode, array->items, i++, item);
}
}
@@ -204,8 +116,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
case RT_BINARY_OP_SUBSCR:
{
uint index = mp_get_index(o->base.type, o->len, rhs);
- machine_int_t val = array_get_el(o, index);
- return mp_obj_new_int(val);
+ return mp_binary_get_val(o->typecode, o->items, index);
}
default:
@@ -218,12 +129,12 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
assert(MP_OBJ_IS_TYPE(self_in, &array_type));
mp_obj_array_t *self = self_in;
if (self->free == 0) {
- int item_sz = array_get_el_size(self->typecode);
+ int item_sz = mp_binary_get_size(self->typecode);
// TODO: alloc policy
self->free = 8;
self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
}
- array_set_el(self, self->len++, arg);
+ mp_binary_set_val(self->typecode, self->items, self->len++, arg);
self->free--;
return mp_const_none; // return None, as per CPython
}
@@ -232,14 +143,14 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(array_append_obj, array_append);
STATIC bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
mp_obj_array_t *o = self_in;
uint index = mp_get_index(o->base.type, o->len, index_in);
- array_set_el(o, index, value);
+ mp_binary_set_val(o->typecode, o->items, index, value);
return true;
}
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
mp_obj_array_t *o = o_in;
bufinfo->buf = o->items;
- bufinfo->len = o->len * array_get_el_size(o->typecode);
+ bufinfo->len = o->len * mp_binary_get_size(o->typecode);
return 0;
}
@@ -267,7 +178,7 @@ STATIC mp_obj_array_t *array_new(char typecode, uint n) {
o->typecode = typecode;
o->free = 0;
o->len = n;
- o->items = m_malloc(array_get_el_size(typecode) * o->len);
+ o->items = m_malloc(mp_binary_get_size(typecode) * o->len);
return o;
}
@@ -304,8 +215,7 @@ typedef struct _mp_obj_array_it_t {
mp_obj_t array_it_iternext(mp_obj_t self_in) {
mp_obj_array_it_t *self = self_in;
if (self->cur < self->array->len) {
- machine_int_t val = array_get_el(self->array, self->cur++);
- return mp_obj_new_int(val);
+ return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
} else {
return mp_const_stop_iteration;
}
diff --git a/py/objexcept.c b/py/objexcept.c
index 90e2cc73ac..48d3de841e 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -53,6 +53,7 @@ STATIC mp_obj_t exception_call(mp_obj_t self_in, uint n_args, uint n_kw, const m
mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args);
o->base.type = &exception_type;
+ o->traceback = MP_OBJ_NULL;
o->id = base->id;
o->msg = NULL;
o->args.len = n_args;
diff --git a/py/objint.h b/py/objint.h
index 6662be1ef3..df3b81b79f 100644
--- a/py/objint.h
+++ b/py/objint.h
@@ -8,3 +8,7 @@ typedef struct _mp_obj_int_t {
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
mp_obj_t int_unary_op(int op, mp_obj_t o_in);
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
+
+#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
+mp_obj_t mp_obj_new_int_from_ll(long long val);
+#endif
diff --git a/py/objint_longlong.c b/py/objint_longlong.c
index f637807905..7412cc09c3 100644
--- a/py/objint_longlong.c
+++ b/py/objint_longlong.c
@@ -13,8 +13,6 @@
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
-STATIC mp_obj_t mp_obj_new_int_from_ll(long long val);
-
// Python3 no longer has "l" suffix for long ints. We allow to use it
// for debugging purpose though.
#ifdef DEBUG
diff --git a/py/py.mk b/py/py.mk
index e29d7b6cad..1d9eb535ae 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -63,6 +63,7 @@ PY_O_BASENAME = \
objzip.o \
sequence.o \
stream.o \
+ binary.o \
builtin.o \
builtinimport.o \
builtinevex.o \
diff --git a/stm/main.c b/stm/main.c
index 3425435942..3484fd28d0 100644
--- a/stm/main.c
+++ b/stm/main.c
@@ -10,6 +10,7 @@
#include <stm32f4xx_rtc.h>
#include <stm32f4xx_usart.h>
#include <stm32f4xx_rng.h>
+#include <usbd_storage_msd.h>
#include <stm_misc.h>
#include "std.h"
@@ -815,14 +816,6 @@ soft_reset:
flash_error(4);
}
-#ifdef USE_HOST_MODE
- // USB host
- pyb_usb_host_init();
-#elif defined(USE_DEVICE_MODE)
- // USB device
- pyb_usb_dev_init();
-#endif
-
if (first_soft_reset) {
#if MICROPY_HW_HAS_MMA7660
// MMA: init and reset address to zero
@@ -839,10 +832,23 @@ soft_reset:
FRESULT res = f_mount(&fatfs1, "1:", 1);
if (res != FR_OK) {
printf("[SD] could not mount SD card\n");
+ } else {
+ if (first_soft_reset) {
+ // use SD card as medium for the USB MSD
+ usbd_storage_select_medium(USBD_STORAGE_MEDIUM_SDCARD);
+ }
}
}
#endif
+#ifdef USE_HOST_MODE
+ // USB host
+ pyb_usb_host_init();
+#elif defined(USE_DEVICE_MODE)
+ // USB device
+ pyb_usb_dev_init();
+#endif
+
// run main script
{
vstr_t *vstr = vstr_new();
diff --git a/stm/sdcard.c b/stm/sdcard.c
index 119027efa7..d0ec45a236 100644
--- a/stm/sdcard.c
+++ b/stm/sdcard.c
@@ -95,6 +95,12 @@ void sdcard_power_off(void) {
SD_DeInit();
}
+uint64_t sdcard_get_capacity_in_bytes(void) {
+ SD_CardInfo SDCardInfo;
+ SD_GetCardInfo(&SDCardInfo);
+ return SDCardInfo.CardCapacity;
+}
+
bool sdcard_read_block(uint8_t *dest, uint32_t block_num) {
// TODO return error if not powered on
diff --git a/stm/sdcard.h b/stm/sdcard.h
index 6461d0f9b5..da7dbddabb 100644
--- a/stm/sdcard.h
+++ b/stm/sdcard.h
@@ -5,6 +5,7 @@ void sdcard_init(void);
bool sdcard_is_present(void);
bool sdcard_power_on(void);
void sdcard_power_off(void);
+uint64_t sdcard_get_capacity_in_bytes(void);
bool sdcard_read_block(uint8_t *dest, uint32_t block_num);
bool sdcard_write_block(const uint8_t *src, uint32_t block_num);
diff --git a/stm/stmusbd/usbd_storage_msd.c b/stm/stmusbd/usbd_storage_msd.c
index c22abbd11d..d8650280c7 100644
--- a/stm/stmusbd/usbd_storage_msd.c
+++ b/stm/stmusbd/usbd_storage_msd.c
@@ -22,142 +22,166 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
+ * Heavily modified by dpgeorge for Micro Python.
+ *
******************************************************************************
- */
+ */
-/* Includes ------------------------------------------------------------------*/
#include "usbd_msc_mem.h"
#include "usb_conf.h"
+#include "usbd_storage_msd.h"
#include "misc.h"
#include "storage.h"
#include "diskio.h"
+#include "sdcard.h"
+
+/******************************************************************************/
+// Callback functions for when the internal flash is the mass storage device
+
+static const int8_t FLASH_STORAGE_Inquirydata[] = { // 36 bytes
+ /* LUN 0 */
+ 0x00,
+ 0x00, // 0x00 for a fixed drive, 0x80 for a removable drive
+ 0x02,
+ 0x02,
+ (USBD_STD_INQUIRY_LENGTH - 5),
+ 0x00,
+ 0x00,
+ 0x00,
+ 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
+ 'm', 'i', 'c', 'r', 'o', 'S', 'D', ' ', /* Product : 16 Bytes */
+ 'F', 'l', 'a', 's', 'h', ' ', ' ', ' ',
+ '1', '.', '0' ,'0', /* Version : 4 Bytes */
+};
-/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
- * @{
+/**
+ * @brief Initialize the storage medium
+ * @param lun : logical unit number
+ * @retval Status
*/
+int8_t FLASH_STORAGE_Init(uint8_t lun) {
+ storage_init();
+ return 0;
+}
-
-/** @defgroup STORAGE
- * @brief media storage application module
- * @{
- */
-
-/** @defgroup STORAGE_Private_TypesDefinitions
- * @{
- */
/**
- * @}
- */
-
-
-/** @defgroup STORAGE_Private_Defines
- * @{
- */
+ * @brief return medium capacity and block size
+ * @param lun : logical unit number
+ * @param block_num : number of physical block
+ * @param block_size : size of a physical block
+ * @retval Status
+ */
+int8_t FLASH_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint32_t *block_size) {
+ *block_size = storage_get_block_size();
+ *block_num = storage_get_block_count();
+ return 0;
+}
-#define STORAGE_LUN_NBR 1
/**
- * @}
- */
-
+ * @brief check whether the medium is ready
+ * @param lun : logical unit number
+ * @retval Status
+ */
+int8_t FLASH_STORAGE_IsReady(uint8_t lun) {
+ return 0;
+}
-/** @defgroup STORAGE_Private_Macros
- * @{
- */
/**
- * @}
- */
-
-
-/** @defgroup STORAGE_Private_Variables
- * @{
- */
-/* USB Mass storage Standard Inquiry Data */
-const int8_t STORAGE_Inquirydata[] = {//36
-
- /* LUN 0 */
- 0x00,
- 0x00, // make it 0x80 for a removable drive
- 0x02,
- 0x02,
- (USBD_STD_INQUIRY_LENGTH - 5),
- 0x00,
- 0x00,
- 0x00,
- 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
- 'm', 'i', 'c', 'r', 'o', 'S', 'D', ' ', /* Product : 16 Bytes */
- 'F', 'l', 'a', 's', 'h', ' ', ' ', ' ',
- '1', '.', '0' ,'0', /* Version : 4 Bytes */
-};
+ * @brief check whether the medium is write-protected
+ * @param lun : logical unit number
+ * @retval Status
+ */
+int8_t FLASH_STORAGE_IsWriteProtected(uint8_t lun) {
+ return 0;
+}
/**
- * @}
- */
-
-
-/** @defgroup STORAGE_Private_FunctionPrototypes
- * @{
- */
-int8_t STORAGE_Init (uint8_t lun);
-
-int8_t STORAGE_GetCapacity (uint8_t lun,
- uint32_t *block_num,
- uint32_t *block_size);
-
-int8_t STORAGE_IsReady (uint8_t lun);
-
-int8_t STORAGE_IsWriteProtected (uint8_t lun);
-
-int8_t STORAGE_Read (uint8_t lun,
- uint8_t *buf,
- uint32_t blk_addr,
- uint16_t blk_len);
-
-int8_t STORAGE_Write (uint8_t lun,
- uint8_t *buf,
- uint32_t blk_addr,
- uint16_t blk_len);
-
-int8_t STORAGE_GetMaxLun (void);
-
-
-USBD_STORAGE_cb_TypeDef USBD_MICRO_SDIO_fops =
-{
- STORAGE_Init,
- STORAGE_GetCapacity,
- STORAGE_IsReady,
- STORAGE_IsWriteProtected,
- STORAGE_Read,
- STORAGE_Write,
- STORAGE_GetMaxLun,
- (int8_t *)STORAGE_Inquirydata,
-};
-
-USBD_STORAGE_cb_TypeDef *USBD_STORAGE_fops = &USBD_MICRO_SDIO_fops;
-/*
-#ifndef USE_STM3210C_EVAL
-extern SD_CardInfo SDCardInfo;
-#endif
-*/
+ * @brief Read data from the medium
+ * @param lun : logical unit number
+ * @param buf : Pointer to the buffer to save data
+ * @param blk_addr : address of 1st block to be read
+ * @param blk_len : nmber of blocks to be read
+ * @retval Status
+ */
+int8_t FLASH_STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
+ disk_read(0, buf, blk_addr, blk_len);
+ /*
+ for (int i = 0; i < blk_len; i++) {
+ if (!storage_read_block(buf + i * FLASH_BLOCK_SIZE, blk_addr + i)) {
+ return -1;
+ }
+ }
+ */
+ return 0;
+}
/**
- * @}
- */
+ * @brief Write data to the medium
+ * @param lun : logical unit number
+ * @param buf : Pointer to the buffer to write from
+ * @param blk_addr : address of 1st block to be written
+ * @param blk_len : nmber of blocks to be read
+ * @retval Status
+ */
+int8_t FLASH_STORAGE_Write (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
+ disk_write(0, buf, blk_addr, blk_len);
+ /*
+ for (int i = 0; i < blk_len; i++) {
+ if (!storage_write_block(buf + i * FLASH_BLOCK_SIZE, blk_addr + i)) {
+ return -1;
+ }
+ }
+ */
+ storage_flush(); // XXX hack for now so that the cache is always flushed
+ return 0;
+}
+/**
+ * @brief Return number of supported logical unit
+ * @param None
+ * @retval number of logical unit
+ */
+int8_t FLASH_STORAGE_GetMaxLun(void) {
+ return 0;
+}
-/** @defgroup STORAGE_Private_Functions
- * @{
- */
+static const USBD_STORAGE_cb_TypeDef USBD_FLASH_STORAGE_fops = {
+ FLASH_STORAGE_Init,
+ FLASH_STORAGE_GetCapacity,
+ FLASH_STORAGE_IsReady,
+ FLASH_STORAGE_IsWriteProtected,
+ FLASH_STORAGE_Read,
+ FLASH_STORAGE_Write,
+ FLASH_STORAGE_GetMaxLun,
+ (int8_t *)FLASH_STORAGE_Inquirydata,
+};
+/******************************************************************************/
+// Callback functions for when the SD card is the mass storage device
+
+static const int8_t SDCARD_STORAGE_Inquirydata[] = { // 36 bytes
+ /* LUN 0 */
+ 0x00,
+ 0x80, // 0x00 for a fixed drive, 0x80 for a removable drive
+ 0x02,
+ 0x02,
+ (USBD_STD_INQUIRY_LENGTH - 5),
+ 0x00,
+ 0x00,
+ 0x00,
+ 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
+ 'm', 'i', 'c', 'r', 'o', 'S', 'D', ' ', /* Product : 16 Bytes */
+ 'S', 'D', ' ', 'c', 'a', 'r', 'd', ' ',
+ '1', '.', '0' ,'0', /* Version : 4 Bytes */
+};
/**
* @brief Initialize the storage medium
* @param lun : logical unit number
* @retval Status
*/
-
-int8_t STORAGE_Init (uint8_t lun)
-{
+int8_t SDCARD_STORAGE_Init(uint8_t lun) {
/*
#ifndef USE_STM3210C_EVAL
NVIC_InitTypeDef NVIC_InitStructure;
@@ -172,8 +196,11 @@ int8_t STORAGE_Init (uint8_t lun)
return (-1);
}
*/
+ if (!sdcard_power_on()) {
+ return -1;
+ }
- return (0);
+ return 0;
}
@@ -184,29 +211,22 @@ int8_t STORAGE_Init (uint8_t lun)
* @param block_size : size of a physical block
* @retval Status
*/
-int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
-{
+int8_t SDCARD_STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint32_t *block_size) {
/*
#ifdef USE_STM3210C_EVAL
SD_CardInfo SDCardInfo;
-
SD_GetCardInfo(&SDCardInfo);
-
#else
- if(SD_GetStatus() != 0 )
- {
+ if(SD_GetStatus() != 0 ) {
return (-1);
}
#endif
*/
-
- *block_size = storage_get_block_size();
- //*block_num = SDCardInfo.CardCapacity / 512;
- *block_num = storage_get_block_count();
-
- return (0);
-
+ *block_size = SDCARD_BLOCK_SIZE;
+ *block_num = sdcard_get_capacity_in_bytes() / SDCARD_BLOCK_SIZE;
+
+ return 0;
}
/**
@@ -214,8 +234,7 @@ int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_si
* @param lun : logical unit number
* @retval Status
*/
-int8_t STORAGE_IsReady (uint8_t lun)
-{
+int8_t SDCARD_STORAGE_IsReady(uint8_t lun) {
/*
#ifndef USE_STM3210C_EVAL
@@ -239,7 +258,7 @@ int8_t STORAGE_IsReady (uint8_t lun)
}
#endif
*/
- return (0);
+ return 0;
}
/**
@@ -247,9 +266,8 @@ int8_t STORAGE_IsReady (uint8_t lun)
* @param lun : logical unit number
* @retval Status
*/
-int8_t STORAGE_IsWriteProtected (uint8_t lun)
-{
- return 0;
+int8_t SDCARD_STORAGE_IsWriteProtected(uint8_t lun) {
+ return 0;
}
/**
@@ -260,27 +278,25 @@ int8_t STORAGE_IsWriteProtected (uint8_t lun)
* @param blk_len : nmber of blocks to be read
* @retval Status
*/
-int8_t STORAGE_Read (uint8_t lun,
- uint8_t *buf,
- uint32_t blk_addr,
- uint16_t blk_len)
-{
+int8_t SDCARD_STORAGE_Read(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
+ // TODO replace with call to sdcard_read_multi_blocks
+ for (int i = 0; i < blk_len; i++) {
+ if (!sdcard_read_block(buf + i * SDCARD_BLOCK_SIZE, blk_addr + i)) {
+ return -1;
+ }
+ }
/*
- if( SD_ReadMultiBlocks (buf,
- blk_addr * 512,
- 512,
- blk_len) != 0)
- {
- return -1;
- }
-#ifndef USE_STM3210C_EVAL
- SD_WaitReadOperation();
- while (SD_GetStatus() != SD_TRANSFER_OK);
-#endif
+ if (SD_ReadMultiBlocks(buf, blk_addr * 512, 512, blk_len) != 0) {
+ return -1;
+ }
+#ifndef USE_STM3210C_EVAL
+ SD_WaitReadOperation();
+ while (SD_GetStatus() != SD_TRANSFER_OK);
+#endif
*/
- disk_read(0, buf, blk_addr, blk_len);
- return 0;
+ return 0;
}
+
/**
* @brief Write data to the medium
* @param lun : logical unit number
@@ -289,28 +305,23 @@ int8_t STORAGE_Read (uint8_t lun,
* @param blk_len : nmber of blocks to be read
* @retval Status
*/
-int8_t STORAGE_Write (uint8_t lun,
- uint8_t *buf,
- uint32_t blk_addr,
- uint16_t blk_len)
-{
-
+int8_t SDCARD_STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) {
+ // TODO replace with call to sdcard_write_multi_blocks
+ for (int i = 0; i < blk_len; i++) {
+ if (!sdcard_write_block(buf + i * SDCARD_BLOCK_SIZE, blk_addr + i)) {
+ return -1;
+ }
+ }
/*
- if( SD_WriteMultiBlocks (buf,
- blk_addr * 512,
- 512,
- blk_len) != 0)
- {
+ if( SD_WriteMultiBlocks (buf, blk_addr * 512, 512, blk_len) != 0) {
return -1;
}
-#ifndef USE_STM3210C_EVAL
+#ifndef USE_STM3210C_EVAL
SD_WaitWriteOperation();
- while (SD_GetStatus() != SD_TRANSFER_OK);
-#endif
+ while (SD_GetStatus() != SD_TRANSFER_OK);
+#endif
*/
- disk_write(0, buf, blk_addr, blk_len);
- storage_flush(); // XXX hack for now so that the cache is always flushed
- return (0);
+ return 0;
}
/**
@@ -318,24 +329,35 @@ int8_t STORAGE_Write (uint8_t lun,
* @param None
* @retval number of logical unit
*/
-
-int8_t STORAGE_GetMaxLun (void)
-{
- return (STORAGE_LUN_NBR - 1);
+int8_t SDCARD_STORAGE_GetMaxLun(void) {
+ return 0;
}
-/**
- * @}
- */
-
-/**
- * @}
- */
+static const USBD_STORAGE_cb_TypeDef USBD_SDCARD_STORAGE_fops = {
+ SDCARD_STORAGE_Init,
+ SDCARD_STORAGE_GetCapacity,
+ SDCARD_STORAGE_IsReady,
+ SDCARD_STORAGE_IsWriteProtected,
+ SDCARD_STORAGE_Read,
+ SDCARD_STORAGE_Write,
+ SDCARD_STORAGE_GetMaxLun,
+ (int8_t *)SDCARD_STORAGE_Inquirydata,
+};
+/******************************************************************************/
+// Callback functions for when the SD card is the mass storage device
-/**
- * @}
- */
+// default to flash as the storage device
+USBD_STORAGE_cb_TypeDef *USBD_STORAGE_fops = (USBD_STORAGE_cb_TypeDef*)&USBD_FLASH_STORAGE_fops;
-/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
+void usbd_storage_select_medium(usbd_storage_medium_kind_t medium_kind) {
+ switch (medium_kind) {
+ case USBD_STORAGE_MEDIUM_FLASH:
+ USBD_STORAGE_fops = (USBD_STORAGE_cb_TypeDef*)&USBD_FLASH_STORAGE_fops;
+ break;
+ case USBD_STORAGE_MEDIUM_SDCARD:
+ USBD_STORAGE_fops = (USBD_STORAGE_cb_TypeDef*)&USBD_SDCARD_STORAGE_fops;
+ break;
+ }
+}
diff --git a/stm/stmusbd/usbd_storage_msd.h b/stm/stmusbd/usbd_storage_msd.h
new file mode 100644
index 0000000000..fac7b0469c
--- /dev/null
+++ b/stm/stmusbd/usbd_storage_msd.h
@@ -0,0 +1,6 @@
+typedef enum {
+ USBD_STORAGE_MEDIUM_FLASH,
+ USBD_STORAGE_MEDIUM_SDCARD,
+} usbd_storage_medium_kind_t;
+
+void usbd_storage_select_medium(usbd_storage_medium_kind_t medium_kind);
diff --git a/unix/ffi.c b/unix/ffi.c
index b83fc120a9..a82e4e3386 100644
--- a/unix/ffi.c
+++ b/unix/ffi.c
@@ -10,6 +10,7 @@
#include "qstr.h"
#include "obj.h"
#include "runtime.h"
+#include "binary.h"
typedef struct _mp_obj_opaque_t {
mp_obj_base_t base;
@@ -295,10 +296,30 @@ static void ffivar_print(void (*print)(void *env, const char *fmt, ...), void *e
print(env, "<ffivar @%p: 0x%x>", self->var, *(int*)self->var);
}
+static mp_obj_t ffivar_get(mp_obj_t self_in) {
+ mp_obj_ffivar_t *self = self_in;
+ return mp_binary_get_val(self->type, self->var, 0);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(ffivar_get_obj, ffivar_get);
+
+static mp_obj_t ffivar_set(mp_obj_t self_in, mp_obj_t val_in) {
+ mp_obj_ffivar_t *self = self_in;
+ mp_binary_set_val(self->type, self->var, 0, val_in);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(ffivar_set_obj, ffivar_set);
+
+static const mp_method_t ffivar_type_methods[] = {
+ { "get", &ffivar_get_obj },
+ { "set", &ffivar_set_obj },
+ { NULL, NULL },
+};
+
static const mp_obj_type_t ffivar_type = {
{ &mp_const_type },
"ffivar",
.print = ffivar_print,
+ .methods = ffivar_type_methods,
};
// Generic opaque storage object