{"id":1027,"date":"2024-09-28T09:52:24","date_gmt":"2024-09-28T01:52:24","guid":{"rendered":"https:\/\/aoofoo.com\/?p=1027"},"modified":"2024-10-02T16:55:04","modified_gmt":"2024-10-02T08:55:04","slug":"2-37-%e8%bd%af%e4%bb%b6spi%e8%af%bb%e5%86%99w25q64","status":"publish","type":"post","link":"https:\/\/aoofoo.com\/index.php\/2024\/09\/28\/2-37-%e8%bd%af%e4%bb%b6spi%e8%af%bb%e5%86%99w25q64\/","title":{"rendered":"2.37 \u8f6f\u4ef6SPI\u8bfb\u5199W25Q64"},"content":{"rendered":"<h2>W25Q64\u63a5\u7ebf\u56fe<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1030\" src=\"https:\/\/aoofoo.com\/wp-content\/uploads\/2024\/09\/1-58.png\" alt=\"\" width=\"974\" height=\"337\" \/><\/p>\n<p>PS\uff1a\u56e0\u4e3a\u6211\u7684W25Q64\u6a21\u5757\u7684\u5f15\u811a\u865a\u710a\u4e86\uff0c\u6240\u4ee5\u672c\u8282\u7684\u4ee3\u7801\uff0c\u6211\u4f7f\u7528\u7684\u662fSTM32F407\u7684\u5f00\u53d1\u677f\u6240\u5b9e\u73b0\u7684\uff0c\u5728\u8be5\u5f00\u53d1\u677f\u4e0a\u9762\uff0c\u4f7f\u7528\u7684\u662fW25Q128\u6a21\u5757\uff0c\u548cW25Q64\u5927\u5dee\u4e0d\u5dee\u3002<\/p>\n<h2>SPI\u901a\u4fe1\u5c42<\/h2>\n<pre class=\"corepress-code-pre\"><code>#include \"stm32f4xx.h\"                  \/\/ Device header\r\n\r\nvoid MySPI_W_SS(uint8_t BitValue)\r\n{\r\n    GPIO_WriteBit(GPIOB, GPIO_Pin_14, (BitAction)(BitValue ? Bit_SET : Bit_RESET));\r\n}\r\n\r\nvoid MySPI_W_SCK(uint8_t BitValue)\r\n{\r\n    GPIO_WriteBit(GPIOB, GPIO_Pin_3, (BitAction)(BitValue ? Bit_SET : Bit_RESET));\r\n}\r\n\r\nvoid MySPI_W_MOSI(uint8_t BitValue)\r\n{\r\n    GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)(BitValue ? Bit_SET : Bit_RESET));\r\n}\r\n\r\nuint8_t MySPI_R_MISO(void)\r\n{\r\n    return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4);\r\n}\r\n\r\nvoid MySPI_Init(void)\r\n{\r\n    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);\r\n\r\n    GPIO_InitTypeDef GPIO_InitStruct;\r\n\r\n    \/\/ \u914d\u7f6e SCK, MOSI, CS \u5f15\u811a\u4e3a\u8f93\u51fa\r\n    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5 | GPIO_Pin_14;  \/\/ SCK, MOSI, CS\r\n    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;             \/\/ \u8f93\u51fa\u6a21\u5f0f\r\n    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;            \/\/ \u63a8\u633d\u8f93\u51fa\r\n    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;         \/\/ \u901f\u5ea650MHz\r\n    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;          \/\/ \u65e0\u4e0a\u4e0b\u62c9\r\n    GPIO_Init(GPIOB, &amp;GPIO_InitStruct);\r\n\r\n    \/\/ \u914d\u7f6e MISO \u5f15\u811a\u4e3a\u8f93\u5165\r\n    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;                 \/\/ MISO\r\n    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;              \/\/ \u8f93\u5165\u6a21\u5f0f\r\n    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;              \/\/ \u4e0a\u62c9\r\n    GPIO_Init(GPIOB, &amp;GPIO_InitStruct);\r\n\r\n    \/\/ \u521d\u59cb\u5316 SPI \u5f15\u811a\u72b6\u6001\r\n    MySPI_W_SS(1);    \/\/ SS \u62c9\u9ad8\uff0c\u672a\u9009\u4e2d\r\n    MySPI_W_SCK(0);   \/\/ SCK \u521d\u59cb\u4e3a\u4f4e\u7535\u5e73\r\n}\r\n\r\nvoid MySPI_Start(void)\r\n{\r\n    MySPI_W_SS(0);   \/\/ \u5c06 CS \u62c9\u4f4e\uff0c\u9009\u4e2d\u4ece\u8bbe\u5907\r\n}\r\n\r\nvoid MySPI_Stop(void)\r\n{\r\n    MySPI_W_SS(1);   \/\/ \u5c06 CS \u62c9\u9ad8\uff0c\u91ca\u653e\u4ece\u8bbe\u5907\r\n}\r\n\r\nvoid Delay(void)\r\n{\r\n    volatile uint32_t i;\r\n    for (i = 0; i &lt; 50; i++);  \/\/ \u7b80\u5355\u5ef6\u65f6\uff0c\u5177\u4f53\u503c\u53ef\u6839\u636e\u9700\u8981\u8c03\u6574\r\n}\r\n\r\nuint8_t MySPI_SwapByte(uint8_t ByteSend)\r\n{\r\n    uint8_t i, ByteReceive = 0x00;\r\n\r\n    for (i = 0; i &lt; 8; i++)\r\n    {\r\n        \/\/ \u53d1\u9001\u4f4d\uff08\u5148\u53d1\u9001\u9ad8\u4f4d\uff09\r\n        MySPI_W_MOSI((ByteSend &amp; 0x80) ? 1 : 0);\r\n        ByteSend &lt;&lt;= 1;\r\n\r\n        \/\/ \u62c9\u9ad8\u65f6\u949f\r\n        MySPI_W_SCK(1);\r\n        Delay();\r\n\r\n        \/\/ \u63a5\u6536\u4f4d\r\n        ByteReceive &lt;&lt;= 1;\r\n        if (MySPI_R_MISO())\r\n        {\r\n            ByteReceive |= 0x01;\r\n        }\r\n\r\n        \/\/ \u62c9\u4f4e\u65f6\u949f\r\n        MySPI_W_SCK(0);\r\n        Delay();\r\n    }\r\n    return ByteReceive;\r\n}<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u786c\u4ef6\u9a71\u52a8\u5c42<\/h2>\n<p>\u53ef\u4ee5\u5148\u505a\u4e00\u4e2aReadID\u51fd\u6570\u6765\u8bfb\u53d6\u82af\u7247\u7684ID\uff0c\u8fd9\u6837\u505a\u662f\u4e3a\u4e86\u6d4b\u8bd5\u901a\u4fe1\u662f\u5426\u6210\u529f\u3002<\/p>\n<pre class=\"corepress-code-pre\"><code>#include \"stm32f4xx.h\"                  \/\/ Device header\r\n#include \"MySPI.h\"\r\n\r\nvoid W25Q128_Init(void)\r\n{\r\n\tMySPI_Init();\r\n}\r\n\r\n\/\/\u8bfb\u53d6ID\u53f7\u65f6\u5e8f\uff1a\u8d77\u59cb-\u968f\u540e\u8fde\u7eed\u4ea4\u63623\u4e2a\u5b57\u8282-\u505c\u6b62\u3002\r\n\/\/\u7b2c\u4e00\u4e2a\u5b57\u8282\u662f\u5382\u5546ID\uff0c\u540e\u4e24\u4e2a\u5b57\u8282\u662f\u8bbe\u5907ID\uff0c\u5176\u4e2d\u8bbe\u5907ID\u7684\u9ad88\u4f4d\u662f\u5b58\u50a8\u5668\u7c7b\u578b\u3001\u4f4e8\u4f4d\u8868\u793a\u5bb9\u91cf\u3002\r\nvoid W25Q128_ReadID(uint8_t *MID,uint16_t *DID)\r\n{\r\n\tMySPI_Start();\r\n\tMySPI_SwapByte(0x9F);\r\n\t*MID = MySPI_SwapByte(0xFF);\r\n\t*DID = MySPI_SwapByte(0XFF);\r\n\t*DID &lt;&lt;= 8;\r\n\t*DID |= MySPI_SwapByte(0xFF);\r\n\tMySPI_Stop();\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"corepress-code-pre\"><code>\/\/\u83b7\u53d6Busy\u4f4d\uff0c\u67e5\u8be2\u82af\u7247\u662f\u5426\u7e41\u5fd9\u3002\r\nvoid W25Q128_WaitBusy(void)\r\n{\r\n\tuint32_t Timeout;\r\n\tMySPI_Start();\r\n\tMySPI_SwapByte(W25Q128_READ_STATUS_REGISTER_1);\r\n\tTimeout = 100000;\r\n\t\/\/\u5982\u679cBusy\u4f4d\u4e3a1\uff0c\u5c31\u4f1a\u8fdb\u5165\u5faa\u73af\uff0c\u518d\u6b21\u8bfb\u51fa\u4e00\u6b21\u72b6\u6001\u5bc4\u5b58\u5668\r\n\twhile((MySPI_SwapByte(W25Q128_DUMMY_BYTE) &amp; 0x01) == 0x01)\r\n\t{\r\n\t\tTimeout--;\r\n\t\tif(Timeout == 0)\r\n\t\t{\r\n\t\t\tbreak;\r\n\t\t}\r\n\t}\r\n\tMySPI_Stop();\r\n}\r\n\r\n\/\/\u9875\u7f16\u7a0b\r\nvoid W25Q128_Pageprogram(uint32_t Address,uint8_t *DataArray,uint16_t Count)\r\n{\r\n\t\/\/\u5199\u4f7f\u80fd\r\n\tW25Q128_WriteEnable();\r\n\t\r\n\tuint16_t i;\r\n\tMySPI_Start();\r\n\t\/\/\u53d1\u9001\u5730\u5740\r\n\tMySPI_SwapByte(W25Q128_PAGE_PROGRAM);\r\n\tMySPI_SwapByte(Address &gt;&gt; 16);\r\n\tMySPI_SwapByte(Address &gt;&gt; 8);\r\n\tMySPI_SwapByte(Address);\r\n\t\/\/\u53d1\u9001\u6570\u636e\u5230\u82af\u7247\u4e2d\r\n\tfor(i=0 ; i&lt;Count ;i++)\r\n\t{\r\n\t\tMySPI_SwapByte(DataArray[i]);\r\n\t}\r\n\t\r\n\tMySPI_Stop();\r\n\t\/\/\u7b49\u5f85\u5199\u5165\uff0c\u5199\u5165\u6210\u529f\u540e\uff0cBusy\u4f4d\u7f6e0\r\n\tW25Q128_WaitBusy();\r\n\r\n}\r\n\r\n\/\/\u64e6\u9664\u6240\u5728\u5730\u5740\u6247\u533a\uff084K\uff09\r\nvoid W25Q128_SectorErase(uint32_t Address)\r\n{\r\n\t\/\/\u5199\u4f7f\u80fd\r\n\tW25Q128_WriteEnable();\r\n\t\r\n\tMySPI_Start();\r\n\t\/\/\u53d1\u9001\u5730\u5740\r\n\tMySPI_SwapByte(W25Q128_SECTOR_ERASE_4KB);\r\n\tMySPI_SwapByte(Address &gt;&gt; 16);\r\n\tMySPI_SwapByte(Address &gt;&gt; 8);\r\n\tMySPI_SwapByte(Address);\r\n\t\r\n\tMySPI_Stop();\r\n\t\r\n\t\/\/\u7b49\u5f85\u5199\u5165\uff0c\u5199\u5165\u6210\u529f\u540e\uff0cBusy\u4f4d\u7f6e0\r\n\tW25Q128_WaitBusy();\r\n}\r\n\r\n\/\/\u8bfb\u53d6\u6570\u636e\r\nvoid W25Q128_ReadData(uint32_t Address,uint8_t *DataArray,uint32_t Count)\r\n{\r\n\tuint32_t i;\r\n\tMySPI_Start();\r\n\t\/\/\u53d1\u9001\u5730\u5740\r\n\tMySPI_SwapByte(W25Q128_READ_DATA);\r\n\tMySPI_SwapByte(Address &gt;&gt; 16);\r\n\tMySPI_SwapByte(Address &gt;&gt; 8);\r\n\tMySPI_SwapByte(Address);\r\n\t\r\n\t\/\/\u63a5\u6536\u6570\u636e\u5230\u6570\u7ec4\u4e2d\r\n\tfor(i=0;i&lt;Count;i++)\r\n\t{\r\n\t\tDataArray[i] = MySPI_SwapByte(W25Q128_DUMMY_BYTE);\r\n\t}\r\n\tMySPI_Stop();\r\n}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<div class=\"sc-collapse\"><div class=\"sc-collapse__header\"><svg class=\"sc-collapse__arrow\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\"><polyline points=\"6 9 12 15 18 9\"\/><\/svg>W25Q128\u6307\u4ee4<\/div><div class=\"sc-collapse__body\"><div class=\"sc-collapse__content\"><\/p>\n<pre class=\"corepress-code-pre\"><code>W25Q128_WRITE_ENABLE\t\t\t\t\t\t0x06\r\nW25Q128_WRITE_DISABLE\t\t\t\t\t\t0x04\r\nW25Q128_READ_STATUS_REGISTER_1\t\t\t\t0x05\r\nW25Q128_READ_STATUS_REGISTER_2\t\t\t\t0x35\r\nW25Q128_WRITE_STATUS_REGISTER\t\t\t\t0x01\r\nW25Q128_PAGE_PROGRAM\t\t\t\t\t\t0x02\r\nW25Q128_QUAD_PAGE_PROGRAM\t\t\t\t\t0x32\r\n\t\r\nW25Q128_BLOCK_ERASE_64KB\t\t\t\t\t0xD8\r\nW25Q128_BLOCK_ERASE_32KB\t\t\t\t\t0x52\r\nW25Q128_SECTOR_ERASE_4KB\t\t\t\t\t0x20\r\n\t\r\nW25Q128_CHIP_ERASE\t\t\t\t\t\t\t0xC7\r\nW25Q128_ERASE_SUSPEND\t\t\t\t\t\t0x75\r\nW25Q128_ERASE_RESUME\t\t\t\t\t\t0x7A\r\n\t\r\nW25Q128_POWER_DOWN\t\t\t\t\t\t\t0xB9\r\n\t\r\nW25Q128_HIGH_PERFORMANCE_MODE\t\t\t\t0xA3\r\nW25Q128_CONTINUOUS_READ_MODE_RESET\t\t\t0xFF\r\n\t\r\nW25Q128_RELEASE_POWER_DOWN_HPM_DEVICE_ID\t0xAB\r\nW25Q128_MANUFACTURER_DEVICE_ID\t\t\t\t0x90\r\nW25Q128_READ_UNIQUE_ID\t\t\t\t\t\t0x4B\r\nW25Q128_JEDEC_ID\t\t\t\t\t\t\t0x9F\r\nW25Q128_READ_DATA\t\t\t\t\t\t\t0x03\r\nW25Q128_FAST_READ\t\t\t\t\t\t\t0x0B\r\nW25Q128_FAST_READ_DUAL_OUTPUT\t\t\t\t0x3B\r\nW25Q128_FAST_READ_DUAL_IO\t\t\t\t\t0xBB\r\nW25Q128_FAST_READ_QUAD_OUTPUT\t\t\t\t0x6B\r\nW25Q128_FAST_READ_QUAD_IO\t\t\t\t\t0xEB\r\nW25Q128_OCTAL_WORD_READ_QUAD_IO\t\t\t\t0xE3\r\n\t\r\nW25Q128_DUMMY_BYTE\t\t\t\t\t\t\t0xFF\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p><\/div><\/div><\/div>\n<p>&nbsp;<\/p>\n<p>\u5199\u4ee3\u7801\u65f6\u7684\u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<p>\u4e00\u5b9a\u8981\u8bb0\u4f4f\uff0c\u5199\u5165\u6570\u636e\uff08\u64e6\u9664\u5b9e\u9645\u4e0a\u4e5f\u662f\u5199\u5165\uff09\u7684\u65f6\u5019\uff0c\u8981\u8bb0\u5f97\u7b49\u5f85\u5199\u5165\u5b8c\u6210Busy\u4f4d\uff0c\u8981\u52a0\u4e00\u4e2a\u7b49\u5f85Busy\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>W25Q64\u63a5\u7ebf\u56fe PS\uff1a\u56e0\u4e3a\u6211\u7684W25Q64\u6a21\u5757\u7684\u5f15\u811a\u865a\u710a\u4e86\uff0c\u6240\u4ee5\u672c\u8282\u7684\u4ee3\u7801\uff0c\u6211\u4f7f\u7528\u7684\u662fSTM32F407\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-stm32"],"_links":{"self":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":6,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"predecessor-version":[{"id":1034,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/posts\/1027\/revisions\/1034"}],"wp:attachment":[{"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aoofoo.com\/index.php\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}