This post was last edited by ddd on 2019-8-31 00:55
This content is originally created by EEWORLD forum user Chao Diao Xia. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source.
First, the schematic diagram and connection diagram ( [FatFs Learning] SD card summary-SPI mode , https://www.cnblogs.com/mr-bike/p/3546228.html)
The following is the experimental record:
#(sysname='pyboard', nodename='pyboard', release='1.11.0', version='v1.11-254-g1fe1ff935 on 2019-08-30', machine='F4DIY with STM32F407')
# 硬件示意图,https://www.cnblogs.com/mr-bike/p/3546228.html
# 闪迪8G, 红色, 曾经用来nanoPi Duo(运行ubuntu用的, A7核,由于认为直接拔电可能会损坏sd卡, 需要先关机的缘故,就凉那了)
from machine import SPI, Pin
from sdcard import SDCard
import os
import pyb
spi = SPI(-1, sck=Pin("PC12"), miso=Pin("PC8"), mosi=Pin("PD2"))
sdcard = SDCard(spi, cs=Pin("PC11"))
# 首先我们可以用os.mount挂载到除了“”以外的任意不存在目录
os.mount(sdcard, '/sd')
os.chdir("/sd") # 这样,当前路径就会变到/sd目录下
os.listdir("") # 可以看到有['flash', 'sd']
os.umount("/flash") # 这样可以卸载原来的flash目录
os.umount("/sd") # 同时也可以卸载当前所在的目录,
os.listdir("") # [] 什么都没有了
os.mount(sdcard, "/sd") # 重新挂载上去即可
os.mount(pyb.Flash, "/flash") # 原来的flash目录对象在pyb里面有,没的话得在源码编译里面加#define MICROPY_HW_HAS_FLASH (1)
# 调用os.chdir("/sd")后,open("xxx.xxx", 'w')就是直接在sdcard里创建了
f = os.open("/sd/xxx.xxx", 'w')
f.write("write test\n")
f.close()
os.open("/sd/xxx.xxx", 'r').read() # 对于
# 研究micropython的时候, 挂来挂去,最后可能就挂不上了,在卸载的情况下可以尝试调用下面两句
# 对了,尽量不要这种情况下拔卡。。。否则可能需要重新格式化卡
vfs = os.VfsFat(sdcard)
vfs.mkfs(sdcard) # 需要稍等一下, 如果是还挂载着,就会报错,大概是这样的错误
# OSError:
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "sdcard.py", line 262, in writeblocks
# File "sdcard.py", line 203, in write
# 对于中途拔出的情况,再次插入的时候会需要重新初始化,否则读写会报以下错误
# open("/sd/test.txt", 'w')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# File "sdcard.py", line 234, in readblocks
# OSError: [Errno 5] EIO
# 调用一下sd卡的初始化函数就好了
sdcard.init_card()
I found the source code on github, and modified it to fit the board. After two days of searching, I still couldn't mount the sd card on the firmware. For now, I'll just download it in the boot file. The sdcard module is in the source code micropython/drivers/scard/sdcard.py, which also contains a test file.
By the way, there are asm embedded assembly routines in the source code micropython/examples. In addition, I used asm to start the hardware external counter mode of STM32, ULN2003 stepper motor, NEC software orthogonal encoder counting, matboard matrix keyboard, PID, etc. I also ported MPU9250 a little, but failed to port DMP successfully. However, they are all very simple, just a few lines of code, so I didn’t post them, or find time to share them together.