Skip to content

Commit

Permalink
samd/mcu/samd51: Enable FAT support for SAMD51.
Browse files Browse the repository at this point in the history
Tested with a SD card connected to a SAMD51 board.  The SEEED WIO terminal
has a SD-Card reader built-in.

Also a side change to remove a few obsolete lines from Makefile.
  • Loading branch information
robert-hh authored and dpgeorge committed Oct 25, 2022
1 parent fe31fca commit 4c9e4c3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
4 changes: 0 additions & 4 deletions ports/samd/Makefile
Expand Up @@ -61,7 +61,6 @@ CFLAGS_MCU_SAMD51 = -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-a
CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU_$(MCU_SERIES)) -fsingle-precision-constant -Wdouble-promotion
CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__
CFLAGS += $(CFLAGS_EXTRA)
CFLAGS += -DMPCONFIG_MCU_H='<boards/mpconfig_$(MCU_SERIES_LOWER).h>'

LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref

Expand Down Expand Up @@ -169,9 +168,6 @@ CFLAGS += -DMICROPY_MODULE_FROZEN_STR
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
endif

# Workaround for bug in older gcc, warning on "static usbd_device_t _usbd_dev = { 0 };"
$(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces

all: $(BUILD)/firmware.uf2

$(BUILD)/firmware.elf: $(OBJ)
Expand Down
41 changes: 41 additions & 0 deletions ports/samd/fatfs_port.c
@@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
* Copyright (c) 2021 Robert Hammelrath
*
* 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 "mphalport.h"
#include "py/runtime.h"
#include "shared/timeutils/timeutils.h"
#include "lib/oofatfs/ff.h"

extern uint32_t time_offset;

MP_WEAK DWORD get_fattime(void) {
timeutils_struct_time_t tm;

timeutils_seconds_since_epoch_to_struct_time(mp_hal_ticks_ms_64() / 1000 + time_offset, &tm);
return ((tm.tm_year - 1980) << 25) | ((tm.tm_mon) << 21) | ((tm.tm_mday) << 16) |
((tm.tm_hour) << 11) | ((tm.tm_min) << 5) | (tm.tm_sec / 2);
}
6 changes: 6 additions & 0 deletions ports/samd/mcu/samd51/mpconfigmcu.h
Expand Up @@ -22,6 +22,12 @@ unsigned long trng_random_u32(void);
// Due to a limitation in the TC counter for us, the ticks period is 2**29
#define MICROPY_PY_UTIME_TICKS_PERIOD (0x20000000)

// fatfs configuration used in ffconf.h
#define MICROPY_FATFS_ENABLE_LFN (1)
#define MICROPY_FATFS_RPATH (2)
#define MICROPY_FATFS_MAX_SS (4096)
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */

#define VFS_BLOCK_SIZE_BYTES (1536) //

#define MICROPY_HW_UART_TXBUF (1)
Expand Down
5 changes: 4 additions & 1 deletion ports/samd/mcu/samd51/mpconfigmcu.mk
@@ -1,8 +1,11 @@
MICROPY_VFS_LFS2 ?= 1
MICROPY_VFS_FAT ?= 1

SRC_S += shared/runtime/gchelper_m3.s

SRC_C += drivers/dht/dht.c \
SRC_C += \
fatfs_port.c \
drivers/dht/dht.c \

LIBM_SRC_C += $(addprefix lib/libm/,\
acoshf.c \
Expand Down
2 changes: 1 addition & 1 deletion ports/samd/modutime.c
Expand Up @@ -29,7 +29,7 @@
#include "shared/timeutils/timeutils.h"
#include "mphalport.h"

static uint32_t time_offset = 0;
uint32_t time_offset = 0;

// localtime([secs])
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
Expand Down

0 comments on commit 4c9e4c3

Please sign in to comment.