SPI FLASH 波形测量演示实例

通常MCU,MPU,FPGA等控制类芯片都会外挂FLASH芯片存储程序,这也是非常常见的。一般的,控制芯片和Flash之间采用最多通信方式是SPI协议。SPI分为二线,三线,四线,具体根据实际情况选择。那么四线制使用是比较广泛的。

SPI:Serial Peripheral Interface,即串行外设接口协议。

SPI Flash四线制信号解释:

①CS:Chip select;也写作NSS或SS(Slave select),表示从设备选择信号,低电平有效。

②MOSI:Master output Slave input;也叫SDI(Serial data input),表示从设备的数据输入。

③MISO:Master input Slave output;也叫SDO(Serial data output),表示从设备的数据输出。

④SCLK:Serial clock;也写作SCK,表示串行时钟。

为了初步了解SPI-FLASH实际通讯波形和时序,以下图为例,采用四线制测量了上电瞬间控制芯片和flash之间的通讯信号:即CS,MOSI,MISO,SCLK。目的是为了验证程序启动时的工作情况,从而进一步了解SPI通信的工作过程。

电路原理图:

分别用示波器测量CS、SCLK、MISO(DO)、MOSI(DI)波形如下,

其中在MOSI引脚没有测试到波形,这是因为主设备是向Flash中读取数据即代码。如果是主设备往从设备Flash中写数据的话,在MOSI引脚上是可以测试到一连串的数据的。

如果以上对大家有些帮助的话,请点赞支持一下或分享,谢谢~

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的SPI Flash读写驱动程序示例,使用的是Linux内核自带的SPI驱动接口。 #include <linux/module.h> #include <linux/spi/spi.h> #include <linux/mtd/mtd.h> #include <linux/mtd/partitions.h> #define FLASH_PAGE_SIZE 256 #define FLASH_SECTOR_SIZE 4096 #define FLASH_BLOCK_SIZE 65536 struct spi_flash { struct mtd_info mtd; struct spi_device *spi; }; static int spi_flash_probe(struct spi_device *spi) { struct spi_flash *flash; struct mtd_partition *parts; int nparts, err; flash = kzalloc(sizeof(struct spi_flash), GFP_KERNEL); if (!flash) { dev_err(&spi->dev, "Failed to allocate memory for spi_flash\n"); return -ENOMEM; } flash->spi = spi; /* Set up MTD structure */ flash->mtd.name = spi->modalias; flash->mtd.owner = THIS_MODULE; flash->mtd.type = MTD_NORFLASH; flash->mtd.flags = MTD_CAP_NORFLASH; flash->mtd.erasesize = FLASH_BLOCK_SIZE; flash->mtd.writesize = FLASH_PAGE_SIZE; flash->mtd.writebufsize = FLASH_PAGE_SIZE; /* Register MTD device */ err = mtd_device_register(&flash->mtd, NULL, 0); if (err) { dev_err(&spi->dev, "Failed to register MTD device, error %d\n", err); kfree(flash); return err; } /* Set up partition table */ nparts = get_mtd_device_partitions(&flash->mtd, &parts, 0); if (nparts <= 0) { dev_err(&spi->dev, "Failed to create partition table\n"); mtd_device_unregister(&flash->mtd); kfree(flash); return -EINVAL; } /* Print information about the device */ dev_info(&spi->dev, "SPI Flash device detected, %d partitions.\n", nparts); dev_info(&spi->dev, "Flash device size %llu bytes, erase size %d bytes, write size %d bytes.\n", (unsigned long long)flash->mtd.size, flash->mtd.erasesize, flash->mtd.writesize); return 0; } static int spi_flash_remove(struct spi_device *spi) { struct spi_flash *flash = spi_get_drvdata(spi); mtd_device_unregister(&flash->mtd); kfree(flash); return 0; } static const struct of_device_id spi_flash_of_match[] = { { .compatible = "spansion, s25fl064k", }, { /* end of table */ } }; MODULE_DEVICE_TABLE(of, spi_flash_of_match); static struct spi_driver spi_flash_driver = { .driver = { .name = "spi_flash", .owner = THIS_MODULE, .of_match_table = spi_flash_of_match, }, .probe = spi_flash_probe, .remove = spi_flash_remove, }; static int __init spi_flash_init(void) { return spi_register_driver(&spi_flash_driver); } static void __exit spi_flash_exit(void) { spi_unregister_driver(&spi_flash_driver); } module_init(spi_flash_init); module_exit(spi_flash_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("SPI Flash driver"); MODULE_ALIAS("spi:spi_flash"); 以上代码定义了一个名为spi_flash的驱动程序,它基于Linux内核自带的SPI驱动接口,并提供了一个MTD设备,可以进行SPI Flash的读写操作。在probe函数中,驱动程序会初始化MTD设备,并创建分区表。在remove函数中,驱动程序会注销MTD设备。在spi_flash_init函数中,驱动程序会注册SPI驱动程序。在spi_flash_exit函数中,驱动程序会注销SPI驱动程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值