micropython_spiFlash_w25qxx

w25qxx.py

python 复制代码
from machine import Pin,SoftSPI
import time

class W25QXX:
    def __init__(self, sck_pin, mosi_pin, miso_pin, cs_pin, capacity):
        self.spi = SoftSPI(-1, sck=Pin(sck_pin), mosi=Pin(mosi_pin), miso=Pin(miso_pin))
        self.cs = Pin(cs_pin, Pin.OUT)
        self.capacity = capacity
        self._write_enable()

    def _write_enable(self):
        self.cs.value(0)
        self.spi.write(bytes([0x06]))
        self.cs.value(1)
        time.sleep_us(10)

    def _write_disable(self):
        self.cs.value(0)
        self.spi.write(bytes([0x04]))
        self.cs.value(1)
        time.sleep_us(10)

    def _wait_busy(self):
        while True:
            self.cs.value(0)
            self.spi.write(bytes([0x05]))
            status = self.spi.read(1)[0]
            self.cs.value(1)
            if (status & 0x01) == 0:
                break

    def read_id(self):
        self.cs.value(0)
        self.spi.write(bytes([0x9F]))
        data = self.spi.read(3)
        self.cs.value(1)
        return data

    def erase_chip(self):
        self._write_enable()
        self.cs.value(0)
        self.spi.write(bytes([0xC7]))
        self.cs.value(1)
        self._wait_busy()
        
    def _is_sector_erase_required(self, address, data_length):
        sector = address // 4096
        sector_address = sector * 4096
        sector_end = sector_address + 4095
        data_end = address + data_length
        return data_end > sector_end

    def erase_sector(self, sector):
        sector_address = sector * 4096
        self._write_enable()
        self.cs.value(0)
        self.spi.write(bytes([0x20, (sector_address >> 16) & 0xFF, (sector_address >> 8) & 0xFF, sector_address & 0xFF]))
        self.cs.value(1)
        self._wait_busy()   


    def write_data(self, address, data):
        sector = address // 4096 
        # 检查目标扇区是否需要擦除
        #if self._is_sector_erase_required(address, len(data)):
        self.erase_sector(sector)
        self._write_enable()
        self.cs.value(0)
        self.spi.write(bytes([0x02, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF]))
        self.spi.write(data)
        self.cs.value(1)
        self._wait_busy()

    def read_data(self, address, length):
        self.cs.value(0)
        self.spi.write(bytes([0x03, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF]))
        data = self.spi.read(length)
        self.cs.value(1)
        return data

    def write_string(self, address, string):
        data = bytearray(string, "utf-8")
        self.write_data(address, data)

    def read_string(self, address, length):
        data = self.read_data(address, length)
        string = data.decode()
        return string

mian.py

python 复制代码
from machine import Pin,SoftSPI
import time
from w25qxx import W25QXX
#mosi_pin=D1
#miso_pin=D0
w25q16 = W25QXX(sck_pin=18, mosi_pin=23, miso_pin=19, cs_pin=5, capacity=16)

id = w25q16.read_id()
print("Flash ID:", id)
w25q16.erase_chip()
w25q16.write_string(0x000000, "44444, World!")
read_string = w25q16.read_string(0x000000, 13)
print("Read String:", read_string)
相关推荐
haosend43 分钟前
AI时代,传统网络运维人员的转型指南
python·数据网络·网络自动化
曲幽1 小时前
不止于JWT:用FastAPI的Depends实现细粒度权限控制
python·fastapi·web·jwt·rbac·permission·depends·abac
IVEN_19 小时前
只会Python皮毛?深入理解这几点,轻松进阶全栈开发
python·全栈
Ray Liang20 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
AI攻城狮20 小时前
如何给 AI Agent 做"断舍离":OpenClaw Session 自动清理实践
python
千寻girling20 小时前
一份不可多得的 《 Python 》语言教程
人工智能·后端·python
AI攻城狮1 天前
用 Playwright 实现博客一键发布到稀土掘金
python·自动化运维
曲幽1 天前
FastAPI分布式系统实战:拆解分布式系统中常见问题及解决方案
redis·python·fastapi·web·httpx·lock·asyncio
孟健2 天前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python