物联网开发笔记(60)- 使用Micropython开发ESP32开发板之SPI接口控制Micro SD卡TF卡模块

news2024/11/19 1:33:03

一、目的

        这一节我们学习如何使用我们的ESP32开发板来通过SPI接口控制Micro SD卡TF卡模块。

二、环境

        ESP32 + SPI接口控制Micro SD卡TF卡模块 + Thonny IDE + 几根杜邦线

接线方法:

Soft SPI接线说明

# 接线说明:
# MISO -> GPTO13
# MOSI -> GPIO12
# SCK -> GPIO 14
# CS -> GPIO27

    SPI通讯

1.GND-for the ground pins.
2.VCC-for the supply voltage.
3.MISO-for the SPI Master Input Slave Output pin.
4.MOSI-for the SPI Master Output Slave Input pin.
5.SCK-for the SPI Serial Clock pin.
6.CS-for the SPI Chip Select pin.

    gnd -用于接地插脚。
    vcc- 电源电压。
    miso-用于SPI主输入从输出引脚。
    mosi -用于SPI主输出从输入引脚。
    sck -用于SPI串行时钟引脚。
    cs -用于SPI芯片选择引脚。
 

 

 三、ESP固件版本

 esp32固件版本MicroPython v1.19.1 on 2022-06-18;

Micropython ESP32对TF卡容量可支持128GB,本人没有对此做过验证,目前手上没有这么大容量的卡,但是需要注意的是,对某些未知的TF不支持,不管容量多少就是识别不到。另外需要注意的是,目前只支持 FAT/FAT32格式的卡.。

128GB容量TF卡相关验证请看:

四、演示效果

  sdcard模块可以在MicroPython源码中找到。

MicroPython源码:https://github.com/micropython/micropython

"""
MicroPython driver for SD cards using SPI bus.

Requires an SPI bus and a CS pin.  Provides readblocks and writeblocks
methods so the device can be mounted as a filesystem.

Example usage on pyboard:

    import pyb, sdcard, os
    sd = sdcard.SDCard(pyb.SPI(1), pyb.Pin.board.X5)
    pyb.mount(sd, '/sd2')
    os.listdir('/')

Example usage on ESP8266:

    import machine, sdcard, os
    sd = sdcard.SDCard(machine.SPI(1), machine.Pin(15))
    os.mount(sd, '/sd')
    os.listdir('/')

"""

from micropython import const
import time


_CMD_TIMEOUT = const(100)

_R1_IDLE_STATE = const(1 << 0)
# R1_ERASE_RESET = const(1 << 1)
_R1_ILLEGAL_COMMAND = const(1 << 2)
# R1_COM_CRC_ERROR = const(1 << 3)
# R1_ERASE_SEQUENCE_ERROR = const(1 << 4)
# R1_ADDRESS_ERROR = const(1 << 5)
# R1_PARAMETER_ERROR = const(1 << 6)
_TOKEN_CMD25 = const(0xFC)
_TOKEN_STOP_TRAN = const(0xFD)
_TOKEN_DATA = const(0xFE)


class SDCard:
    def __init__(self, spi, cs, baudrate=1320000):
        self.spi = spi
        self.cs = cs

        self.cmdbuf = bytearray(6)
        self.dummybuf = bytearray(512)
        self.tokenbuf = bytearray(1)
        for i in range(512):
            self.dummybuf[i] = 0xFF
        self.dummybuf_memoryview = memoryview(self.dummybuf)

        # initialise the card
        self.init_card(baudrate)

    def init_spi(self, baudrate):
        try:
            master = self.spi.MASTER
        except AttributeError:
            # on ESP8266
            self.spi.init(baudrate=baudrate, phase=0, polarity=0)
        else:
            # on pyboard
            self.spi.init(master, baudrate=baudrate, phase=0, polarity=0)

    def init_card(self, baudrate):

        # init CS pin
        self.cs.init(self.cs.OUT, value=1)

        # init SPI bus; use low data rate for initialisation
        self.init_spi(100000)

        # clock card at least 100 cycles with cs high
        for i in range(16):
            self.spi.write(b"\xff")

        # CMD0: init card; should return _R1_IDLE_STATE (allow 5 attempts)
        for _ in range(5):
            if self.cmd(0, 0, 0x95) == _R1_IDLE_STATE:
                break
        else:
            raise OSError("no SD card")

        # CMD8: determine card version
        r = self.cmd(8, 0x01AA, 0x87, 4)
        if r == _R1_IDLE_STATE:
            self.init_card_v2()
        elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND):
            self.init_card_v1()
        else:
            raise OSError("couldn't determine SD card version")

        # get the number of sectors
        # CMD9: response R2 (R1 byte + 16-byte block read)
        if self.cmd(9, 0, 0, 0, False) != 0:
            raise OSError("no response from SD card")
        csd = bytearray(16)
        self.readinto(csd)
        if csd[0] & 0xC0 == 0x40:  # CSD version 2.0
            self.sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024
        elif csd[0] & 0xC0 == 0x00:  # CSD version 1.0 (old, <=2GB)
            c_size = (csd[6] & 0b11) << 10 | csd[7] << 2 | csd[8] >> 6
            c_size_mult = (csd[9] & 0b11) << 1 | csd[10] >> 7
            read_bl_len = csd[5] & 0b1111
            capacity = (c_size + 1) * (2 ** (c_size_mult + 2)) * (2**read_bl_len)
            self.sectors = capacity // 512
        else:
            raise OSError("SD card CSD format not supported")
        # print('sectors', self.sectors)

        # CMD16: set block length to 512 bytes
        if self.cmd(16, 512, 0) != 0:
            raise OSError("can't set 512 block size")

        # set to high data rate now that it's initialised
        self.init_spi(baudrate)

    def init_card_v1(self):
        for i in range(_CMD_TIMEOUT):
            self.cmd(55, 0, 0)
            if self.cmd(41, 0, 0) == 0:
                # SDSC card, uses byte addressing in read/write/erase commands
                self.cdv = 512
                # print("[SDCard] v1 card")
                return
        raise OSError("timeout waiting for v1 card")

    def init_card_v2(self):
        for i in range(_CMD_TIMEOUT):
            time.sleep_ms(50)
            self.cmd(58, 0, 0, 4)
            self.cmd(55, 0, 0)
            if self.cmd(41, 0x40000000, 0) == 0:
                self.cmd(58, 0, 0, -4)  # 4-byte response, negative means keep the first byte
                ocr = self.tokenbuf[0]  # get first byte of response, which is OCR
                if not ocr & 0x40:
                    # SDSC card, uses byte addressing in read/write/erase commands
                    self.cdv = 512
                else:
                    # SDHC/SDXC card, uses block addressing in read/write/erase commands
                    self.cdv = 1
                # print("[SDCard] v2 card")
                return
        raise OSError("timeout waiting for v2 card")

    def cmd(self, cmd, arg, crc, final=0, release=True, skip1=False):
        self.cs(0)

        # create and send the command
        buf = self.cmdbuf
        buf[0] = 0x40 | cmd
        buf[1] = arg >> 24
        buf[2] = arg >> 16
        buf[3] = arg >> 8
        buf[4] = arg
        buf[5] = crc
        self.spi.write(buf)

        if skip1:
            self.spi.readinto(self.tokenbuf, 0xFF)

        # wait for the response (response[7] == 0)
        for i in range(_CMD_TIMEOUT):
            self.spi.readinto(self.tokenbuf, 0xFF)
            response = self.tokenbuf[0]
            if not (response & 0x80):
                # this could be a big-endian integer that we are getting here
                # if final<0 then store the first byte to tokenbuf and discard the rest
                if final < 0:
                    self.spi.readinto(self.tokenbuf, 0xFF)
                    final = -1 - final
                for j in range(final):
                    self.spi.write(b"\xff")
                if release:
                    self.cs(1)
                    self.spi.write(b"\xff")
                return response

        # timeout
        self.cs(1)
        self.spi.write(b"\xff")
        return -1

    def readinto(self, buf):
        self.cs(0)

        # read until start byte (0xff)
        for i in range(_CMD_TIMEOUT):
            self.spi.readinto(self.tokenbuf, 0xFF)
            if self.tokenbuf[0] == _TOKEN_DATA:
                break
            time.sleep_ms(1)
        else:
            self.cs(1)
            raise OSError("timeout waiting for response")

        # read data
        mv = self.dummybuf_memoryview
        if len(buf) != len(mv):
            mv = mv[: len(buf)]
        self.spi.write_readinto(mv, buf)

        # read checksum
        self.spi.write(b"\xff")
        self.spi.write(b"\xff")

        self.cs(1)
        self.spi.write(b"\xff")

    def write(self, token, buf):
        self.cs(0)

        # send: start of block, data, checksum
        self.spi.read(1, token)
        self.spi.write(buf)
        self.spi.write(b"\xff")
        self.spi.write(b"\xff")

        # check the response
        if (self.spi.read(1, 0xFF)[0] & 0x1F) != 0x05:
            self.cs(1)
            self.spi.write(b"\xff")
            return

        # wait for write to finish
        while self.spi.read(1, 0xFF)[0] == 0:
            pass

        self.cs(1)
        self.spi.write(b"\xff")

    def write_token(self, token):
        self.cs(0)
        self.spi.read(1, token)
        self.spi.write(b"\xff")
        # wait for write to finish
        while self.spi.read(1, 0xFF)[0] == 0x00:
            pass

        self.cs(1)
        self.spi.write(b"\xff")

    def readblocks(self, block_num, buf):
        nblocks = len(buf) // 512
        assert nblocks and not len(buf) % 512, "Buffer length is invalid"
        if nblocks == 1:
            # CMD17: set read address for single block
            if self.cmd(17, block_num * self.cdv, 0, release=False) != 0:
                # release the card
                self.cs(1)
                raise OSError(5)  # EIO
            # receive the data and release card
            self.readinto(buf)
        else:
            # CMD18: set read address for multiple blocks
            if self.cmd(18, block_num * self.cdv, 0, release=False) != 0:
                # release the card
                self.cs(1)
                raise OSError(5)  # EIO
            offset = 0
            mv = memoryview(buf)
            while nblocks:
                # receive the data and release card
                self.readinto(mv[offset : offset + 512])
                offset += 512
                nblocks -= 1
            if self.cmd(12, 0, 0xFF, skip1=True):
                raise OSError(5)  # EIO

    def writeblocks(self, block_num, buf):
        nblocks, err = divmod(len(buf), 512)
        assert nblocks and not err, "Buffer length is invalid"
        if nblocks == 1:
            # CMD24: set write address for single block
            if self.cmd(24, block_num * self.cdv, 0) != 0:
                raise OSError(5)  # EIO

            # send the data
            self.write(_TOKEN_DATA, buf)
        else:
            # CMD25: set write address for first block
            if self.cmd(25, block_num * self.cdv, 0) != 0:
                raise OSError(5)  # EIO
            # send the data
            offset = 0
            mv = memoryview(buf)
            while nblocks:
                self.write(_TOKEN_CMD25, mv[offset : offset + 512])
                offset += 512
                nblocks -= 1
            self.write_token(_TOKEN_STOP_TRAN)

    def ioctl(self, op, arg):
        if op == 4:  # get number of blocks
            return self.sectors
        if op == 5:  # get block size in bytes
            return 512

五、代码(SoftSPI即软SPI通知)

        测试代码1,test1.py。运行代码前,需要先将sdcard模块保存到MicroPython设备当中。

import os
from machine import Pin, SoftSPI
from sdcard import SDCard
# 接线说明:
# MISO -> GPTO13
# MOSI -> GPIO12
# SCK -> GPIO 14
# CS -> GPIO27
spisd=SoftSPI(-1, miso=Pin(13), mosi=Pin(12), sck=Pin(14))
sd=SDCard(spisd, Pin(27))
print('Root directory:{}'.format(os.listdir()))
vfs=os.VfsFat(sd)
os.mount(vfs,'/sd')
print('Root directory:{}'.format(os.listdir()))
os.chdir('sd')
print('SD Card contains:{}'.format(os.listdir()))

 

 

测试代码2,test2.py。添加对SD卡容量信息读取

import os
from machine import Pin, SoftSPI
from sdcard import SDCard
# 接线说明:
# MISO -> GPTO13
# MOSI -> GPIO12
# SCK -> GPIO 14
# CS -> GPIO27
spisd=SoftSPI(-1, miso=Pin(13), mosi=Pin(12), sck=Pin(14))
sd=SDCard(spisd, Pin(27))
print('Root directory:{}'.format(os.listdir()))
vfs=os.VfsFat(sd)
os.mount(vfs,'/sd')
r = os.statvfs('/sd')
print('SD capacity: {} B / {} M'.format(r[0] * r[2], r[0] * r[2]/1024/1024))
print('free space: {} B / {} M'.format(r[0] * r[3], r[0] * r[3]/1024/1024))
print('Root directory:{}'.format(os.listdir()))
os.chdir('sd')
print('SD Card contains:{}'.format(os.listdir()))

六、代码(SPI即硬SPI通知)

        这几个信号线的接线位置更换如下:

sck=Pin(17), 
mosi=Pin(23),
miso=Pin(19)
SD_CS= Pin(5)

引脚接线说明

  • 使用的是VSPI总线
SPIMOSIMISOCLKCS
VSPI2319185

测试代码3,test3.py:

import machine, sdcard, os
from machine import SPI
from machine import Pin
SD_CS = Pin(5)
sd = sdcard.SDCard(SPI(2,sck=Pin(17), mosi=Pin(23),miso=Pin(19)), SD_CS)
# 初始化⽂件系统
vfs = os.VfsFat(sd)# fat挂载卡到⽬录下
os.mount(sd,"/sd")# SD/sd
dirs=os.listdir('/sd')
for file in dirs:   
    print(file)

 测试代码4,test4.py:

import os, sdcard, machine
from machine import SPI
from machine import Pin

def sdtest():
    SD_CS = Pin(5)
    sd = sdcard.SDCard(SPI(2,sck=Pin(17), mosi=Pin(23),miso=Pin(19)), SD_CS)  
    vfs = os.VfsFat(sd)
    os.mount(vfs, "/fc")
    print("Filesystem check")
    print(os.listdir("/fc"))

    line = "abcdefghijklmnopqrstuvwxyz\n"
    lines = line * 200  # 5400 chars
    short = "1234567890\n"

    fn = "/fc/rats.txt"
    print()
    print("Multiple block read/write")
    with open(fn, "w") as f:
        n = f.write(lines)
        print(n, "bytes written")
        n = f.write(short)
        print(n, "bytes written")
        n = f.write(lines)
        print(n, "bytes written")

    with open(fn, "r") as f:
        result1 = f.read()
        print(len(result1), "bytes read")

    fn = "/fc/rats1.txt"
    print()
    print("Single block read/write")
    with open(fn, "w") as f:
        n = f.write(short)  # one block
        print(n, "bytes written")

    with open(fn, "r") as f:
        result2 = f.read()
        print(len(result2), "bytes read")

    os.umount("/fc")

    print()
    print("Verifying data read back")
    success = True
    if result1 == "".join((lines, short, lines)):
        print("Large file Pass")
    else:
        print("Large file Fail")
        success = False
    if result2 == short:
        print("Small file Pass")
    else:
        print("Small file Fail")
        success = False
    print()
    print("Tests", "passed" if success else "failed")
    
if __name__ == '__main__':
    sdtest()

五、购买

某宝链接如下:
https://item.taobao.com/item.htm?spm=a1z09.2.0.0.6cde2e8dg7IWOL&id=617159768229&_u=bp01rchc8d5

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/81045.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[附源码]Python计算机毕业设计SSM基于的楼盘销售系统(程序+LW)

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

SpringCloud入门实战-Ribbon

SpringCloud入门实战-Ribbon使用 原创目录概述需求&#xff1a;设计思路实现思路分析1.Ribbon原理2.Ribbon负载均衡策略参考资料和推荐阅读Survive by day and develop by night. talk for import biz , show your perfect code,full busy&#xff0c;skip hardness,make a bet…

计算机软技术,如何画好一张架构图?

什么是架构图&#xff1f; 如何画好一张架构图&#xff0c;要做好这件事情首先要回答的就是什么是架构图。我们日常工作中经常能看到各种各样的架构图&#xff0c;而且经常会发现大家对架构图的理解各有侧重。深入追究到这个问题&#xff0c;可能一下子还很难有一个具象的定义…

动态路由协议RIP

数据来源 一、动态路由 基于某种协议实现 1&#xff09;动态路由拓补图 2&#xff09;动态路由特点 减少了管理任务占用了网络带宽 3&#xff09;动态路由协议概述 路由器之间用来交换信息的语言 4&#xff09;度量值 跳数、带宽、负载、时延、可靠性、成本 跳数&#xff1a…

JavaScript数据结构【数组---for...of循环迭代】

继for循环&#xff0c;和forEach方法迭代数组后&#xff0c;要想迭代数组的值还可以用for...of循环 使用&#xff1a; // for...of循环示例 let array [1, 2, 3] for (let key of array) {console.log(key); } /* 输出&#xff1a;123 */ 可以看到&#xff1a;使用for...of…

嵌入式介绍与应用

嵌入式介绍与应用1 概念桌面对比2 特点3 发展历史3.1 计算机发展3.2 嵌入式发展4 开发能力要求5 应用6 规模参考1 概念 嵌入式系统由硬件和软件组成。是能够独立进行运作的器件。其软件内容只包括软件运行环境及其操作系统。硬件内容包括信号处理器、存储器、通信模块等在内的…

构建过程:从源码到dist文件

问题 有没有好奇过&#xff0c;自己写的前端代码是怎么变成上线可用的代码的&#xff1f; 前言 目前实现从源码到可用的静态文件&#xff0c;我们都是借助打包工具实现的&#xff0c;目前用的比较多的是webpack、rollup、vite..., 那么以上问题也可以描述为“构建工具是如何…

ChatGPT教程之 03 ChatGPT 中构建 Python 解释器

这个故事的灵感来自于一个类似的故事,在 ChatGPT 中构建虚拟机。我印象深刻并决定尝试类似的东西,但这次不是 Linux 命令行工具,而是让 ChatGPT 成为我们的 Python 解释器。 这是初始化 ChatGPT 的初始命令: I want you to act as a Python interpreter. I will type com…

<<两万字通关Java IO流>>

✨✨hello&#xff0c;愿意点进来的小伙伴们&#xff0c;你们好呐&#xff01; &#x1f43b;&#x1f43b;系列专栏&#xff1a;【JavaEE】 &#x1f432;&#x1f432;本篇内容&#xff1a;详解Java IO流 &#x1f42f;&#x1f42f;作者简介:一名现大二的三非编程小白&#…

python----函数、文件、以及高级特性

文章目录前言一、函数的基本概念二、文件OS模块json模块高级特性生成式生成器闭包装饰器前言 一、函数的基本概念 **全局变量&#xff1a;**在函数外边定义的变量&#xff0c;全局生效 **局部变量&#xff1a;**在函数里边定义的变量&#xff0c;局部生效 如果要在函数中修改全…

【BL808】缘起:M1s开发板的第一个示例-LVGL

一、sipeed M1s介绍 1.1 M1s开发板介绍 1.1.1 开发板特性 板载两个USB口&#xff08;一个用于USB-TTL&#xff0c;一个用于通过模拟U盘的方式烧录c906的固件&#xff09;板载1.69 inch的触摸屏和摄像头接口板载MIC、LED和TF卡座板载一个BL702做成的集USB-TTL和JTAG的调试器。…

面试收集汇总

最近的工作情况&#xff0c;难度比较大的项目。 http servlet生命周期&#xff0c;在springmvc对原生servlet做了一个怎么样的包装来实现一个自己的mvc能力的&#xff1f; 1.加载和实例化。Servlet容器负责加载和实例化Servlet。当Servlet容器启动时&#xff0c;或者在容器检…

[附源码]JAVA毕业设计养老院老人日常生活管理系统(系统+LW)

[附源码]JAVA毕业设计养老院老人日常生活管理系统&#xff08;系统LW&#xff09; 项目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。…

尝试使用CubeMX做stm32开发之十五:FatFs的移植方法

一、数据类型 FatFs使用的数据类型在ff.h中定义&#xff0c;适用于绝大多数平台&#xff1a; BYTE 8-bit无符号整形数据&#xff0c;范围0~28-1 WORD 16-bit无符号整形数据&#xff0c;范围0~216-1 DWORD 32-bit无符号整形数据&#xff0c;范围0~232-1 QWORD 64-bit无符…

【C#基础学习】第十八章、接口

目录 接口 1.接口 1.1 声明接口 1.2 实现接口&调用接口 1.2.1 显式接口实现方式 1.2.2 显式接口实现&隐式接口实现的使用场景 1.3 as运算符 2.接口继承接口 接口 1.接口 接口的意义&#xff1a;C#的继承不支持一个子类拥有多个父类。而接口的存在就是为了弥补这…

C# 数组的声明与分配空间

一 数组的概述 数组是多个相同类型数据的组合&#xff1b;数组属引用类型&#xff1b; 1 一维数组声明 一维数组的声明方式&#xff1a; int[] a1;注意方括号写到变量名的签名&#xff1b; double [] b mydate[] c; C# 语言中声明的数组时不能指定其长度(数组中元素的个数)…

【RPA进阶】 一文了解使用Visual Studio扩展UiPath Activity Creator创建自定义Activity

&#x1f40b;作者简介&#xff1a;博主是一位.Net开发者&#xff0c;同时也是RPA和低代码平台的践行者。 &#x1f42c;个人主页&#xff1a;会敲键盘的肘子 &#x1f430;系列专栏&#xff1a;UiPath &#x1f980;专栏简介&#xff1a;UiPath在传统的RPA&#xff08;Robotic…

Dockerfile构建Springboot镜像

Dockerfile构建Springboot镜像 文章目录Dockerfile构建Springboot镜像简介实例演示前期准备Docker环境Springboot项目Dockerfile文件Windows要求构建镜像启动测试Linux要求构建镜像启动测试简介 容器技术大流行的时代&#xff0c;也是docker大流行的时代。 此文章以一个简单的实…

无线网卡收包流程

环境 x86_64 Linux&#xff0c; AR9462 无线网卡&#xff0c;PCIe 接口 ath9k 驱动 收包过程 ① 无线网卡从空气中捕获到无线数据包 【物理层】 ② 无线网卡把帧 DMA 到内存的 Ring Buffer ③ 无线网卡向 CPU 发起中断请求 ④ CPU 响应中断&#xff0c;执行 ISR&#xff0c;…

fatfs相关宏定义说明

fat16文件系统DBR 宏定义如下&#xff1a; #define BS_JmpBoot 0 //跳转指令。3字节。/* x86 jump instruction (3-byte) */ #define BS_OEMName 3 //OEM名称。8字节。/* OEM name (8-byte) */ #define BPB_BytsPerSec 11 //扇区字节数。2字节。/* Sector size [byte…