{"id":43176,"date":"2025-06-11T08:56:02","date_gmt":"2025-06-11T00:56:02","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/43176.html"},"modified":"2025-06-11T08:56:02","modified_gmt":"2025-06-11T00:56:02","slug":"stm32-hal%e5%ba%93spi%e8%af%bb%e5%86%99w25q128%ef%bc%88%e8%bd%af%e4%bb%b6%e6%a8%a1%e6%8b%9f%e7%a1%ac%e4%bb%b6spi","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/43176.html","title":{"rendered":"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi)"},"content":{"rendered":"<h4>1. \u5f15\u8a00<\/h4>\n<p>\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u4e2d&#xff0c;SPI&#xff08;Serial Peripheral Interface&#xff09;\u603b\u7ebf\u662f\u4e00\u79cd\u5e38\u7528\u7684\u4e32\u884c\u901a\u4fe1\u534f\u8bae&#xff0c;\u7528\u4e8e\u5728\u5fae\u63a7\u5236\u5668\u548c\u5916\u90e8\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u9ad8\u901f\u6570\u636e\u4f20\u8f93\u3002W25Q128 \u662f\u4e00\u6b3e\u5e38\u89c1\u7684 SPI Flash \u82af\u7247&#xff0c;\u5177\u6709 128Mbit&#xff08;16MB&#xff09;\u7684\u5b58\u50a8\u5bb9\u91cf&#xff0c;\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5b58\u50a8\u548c\u7a0b\u5e8f\u4ee3\u7801\u5b58\u50a8\u7b49\u573a\u666f\u3002STM32F407 \u662f\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684 ARM Cortex &#8211; M4 \u5185\u6838\u5fae\u63a7\u5236\u5668&#xff0c;\u5b83\u652f\u6301\u786c\u4ef6 SPI \u63a5\u53e3&#xff0c;\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62df SPI \u901a\u4fe1\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u57fa\u4e8e STM32F407 HAL \u5e93\u5b9e\u73b0\u8f6f\u4ef6\u6a21\u62df SPI \u548c\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u65b9\u6cd5\u3002<\/p>\n<h4>2. \u786c\u4ef6\u8fde\u63a5<\/h4>\n<h5>2.1 STM32F407 \u4e0e W25Q128 \u7684\u786c\u4ef6\u8fde\u63a5<\/h5>\n<table>\n<tr>STM32F407 \u5f15\u811aW25Q128 \u5f15\u811a\u529f\u80fd\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>PA5&#xff08;SCK&#xff09;<\/td>\n<td>CLK<\/td>\n<td>SPI \u65f6\u949f\u4fe1\u53f7<\/td>\n<\/tr>\n<tr>\n<td>PA6&#xff08;MISO&#xff09;<\/td>\n<td>DO<\/td>\n<td>\u4e3b\u8bbe\u5907\u8f93\u5165\u4ece\u8bbe\u5907\u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td>PA7&#xff08;MOSI&#xff09;<\/td>\n<td>DI<\/td>\n<td>\u4e3b\u8bbe\u5907\u8f93\u51fa\u4ece\u8bbe\u5907\u8f93\u5165<\/td>\n<\/tr>\n<tr>\n<td>PA4&#xff08;NSS&#xff09;<\/td>\n<td>CS<\/td>\n<td>\u7247\u9009\u4fe1\u53f7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3. \u8f6f\u4ef6\u6a21\u62df SPI \u8bfb\u5199 W25Q128<\/h4>\n<h5>3.1 \u521d\u59cb\u5316 GPIO \u5f15\u811a<\/h5>\n<p>\u5728\u8f6f\u4ef6\u6a21\u62df SPI \u901a\u4fe1\u4e2d&#xff0c;\u9700\u8981\u5c06\u76f8\u5e94\u7684 GPIO \u5f15\u811a\u914d\u7f6e\u4e3a\u8f93\u51fa\u6216\u8f93\u5165\u6a21\u5f0f\u3002\u4ee5\u4e0b\u662f\u521d\u59cb\u5316 GPIO \u5f15\u811a\u7684\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &#034;stm32f4xx_hal.h&#034;<\/p>\n<p>\/\/ \u5b9a\u4e49SPI\u901a\u4fe1\u6240\u9700\u7684GPIO\u5f15\u811a<br \/>\n\/\/ \u65f6\u949f\u4fe1\u53f7\u5f15\u811a<br \/>\n#define SPI_SCK_PIN GPIO_PIN_5<br \/>\n\/\/ \u4e3b\u8bbe\u5907\u8f93\u5165\u4ece\u8bbe\u5907\u8f93\u51fa\u5f15\u811a<br \/>\n#define SPI_MISO_PIN GPIO_PIN_6<br \/>\n\/\/ \u4e3b\u8bbe\u5907\u8f93\u51fa\u4ece\u8bbe\u5907\u8f93\u5165\u5f15\u811a<br \/>\n#define SPI_MOSI_PIN GPIO_PIN_7<br \/>\n\/\/ \u7247\u9009\u4fe1\u53f7\u5f15\u811a<br \/>\n#define SPI_NSS_PIN GPIO_PIN_4<br \/>\n\/\/ \u8fd9\u4e9b\u5f15\u811a\u6240\u5728\u7684GPIO\u7aef\u53e3<br \/>\n#define SPI_GPIO_PORT GPIOA<\/p>\n<p>\/\/ \u521d\u59cb\u5316SPI\u901a\u4fe1\u6240\u9700\u7684GPIO\u5f15\u811a<br \/>\nvoid SPI_GPIO_Init(void) {<br \/>\n    \/\/ \u5b9a\u4e49\u4e00\u4e2aGPIO\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u53d8\u91cf&#xff0c;\u7528\u4e8e\u914d\u7f6eGPIO\u5f15\u811a<br \/>\n    GPIO_InitTypeDef GPIO_InitStruct &#061; {0};<\/p>\n<p>    \/\/ \u4f7f\u80fdGPIOA\u7aef\u53e3\u7684\u65f6\u949f&#xff0c;\u56e0\u4e3aSPI\u901a\u4fe1\u4f7f\u7528\u7684\u5f15\u811a\u4f4d\u4e8eGPIOA\u7aef\u53e3<br \/>\n    __HAL_RCC_GPIOA_CLK_ENABLE();<\/p>\n<p>    \/\/ \u914d\u7f6e\u65f6\u949f\u4fe1\u53f7\u3001\u4e3b\u8bbe\u5907\u8f93\u51fa\u4ece\u8bbe\u5907\u8f93\u5165\u548c\u7247\u9009\u4fe1\u53f7\u5f15\u811a<br \/>\n    \/\/ \u5c06\u8fd9\u4e9b\u5f15\u811a\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u50a8\u5728GPIO_InitStruct\u7ed3\u6784\u4f53\u4e2d<br \/>\n    GPIO_InitStruct.Pin &#061; SPI_SCK_PIN | SPI_MOSI_PIN | SPI_NSS_PIN;<br \/>\n    \/\/ \u8bbe\u7f6e\u8fd9\u4e9b\u5f15\u811a\u4e3a\u63a8\u633d\u8f93\u51fa\u6a21\u5f0f<br \/>\n    GPIO_InitStruct.Mode &#061; GPIO_MODE_OUTPUT_PP;<br \/>\n    \/\/ \u4e0d\u4f7f\u7528\u4e0a\u62c9\u6216\u4e0b\u62c9\u7535\u963b<br \/>\n    GPIO_InitStruct.Pull &#061; GPIO_NOPULL;<br \/>\n    \/\/ \u8bbe\u7f6e\u5f15\u811a\u7684\u8f93\u51fa\u901f\u5ea6\u4e3a\u4f4e\u9891<br \/>\n    GPIO_InitStruct.Speed &#061; GPIO_SPEED_FREQ_LOW;<br \/>\n    \/\/ \u6839\u636e\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u521d\u59cb\u5316SPI_GPIO_PORT&#xff08;\u5373GPIOA&#xff09;\u7aef\u53e3\u7684\u76f8\u5e94\u5f15\u811a<br \/>\n    HAL_GPIO_Init(SPI_GPIO_PORT, &amp;GPIO_InitStruct);<\/p>\n<p>    \/\/ \u914d\u7f6e\u4e3b\u8bbe\u5907\u8f93\u5165\u4ece\u8bbe\u5907\u8f93\u51fa\u5f15\u811a<br \/>\n    \/\/ \u91cd\u65b0\u8bbe\u7f6e\u5f15\u811a\u4e3aSPI_MISO_PIN<br \/>\n    GPIO_InitStruct.Pin &#061; SPI_MISO_PIN;<br \/>\n    \/\/ \u8bbe\u7f6e\u8be5\u5f15\u811a\u4e3a\u8f93\u5165\u6a21\u5f0f<br \/>\n    GPIO_InitStruct.Mode &#061; GPIO_MODE_INPUT;<br \/>\n    \/\/ \u4e0d\u4f7f\u7528\u4e0a\u62c9\u6216\u4e0b\u62c9\u7535\u963b<br \/>\n    GPIO_InitStruct.Pull &#061; GPIO_NOPULL;<br \/>\n    \/\/ \u6839\u636e\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u521d\u59cb\u5316SPI_GPIO_PORT&#xff08;\u5373GPIOA&#xff09;\u7aef\u53e3\u7684SPI_MISO_PIN\u5f15\u811a<br \/>\n    HAL_GPIO_Init(SPI_GPIO_PORT, &amp;GPIO_InitStruct);<\/p>\n<p>    \/\/ \u521d\u59cb\u65f6\u5c06\u7247\u9009\u4fe1\u53f7\u5f15\u811a\u62c9\u9ad8&#xff0c;\u7981\u7528\u4ece\u8bbe\u5907<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n} <\/p>\n<h5>3.2 \u8f6f\u4ef6\u6a21\u62df SPI \u4f4d\u64cd\u4f5c<\/h5>\n<p>\u5b9e\u73b0 SPI \u7684\u4f4d\u64cd\u4f5c\u51fd\u6570&#xff0c;\u5305\u62ec\u65f6\u949f\u4fe1\u53f7\u7684\u4ea7\u751f\u548c\u6570\u636e\u7684\u53d1\u9001\u4e0e\u63a5\u6536\u3002<\/p>\n<p>\/**<br \/>\n * &#064;brief \u53d1\u9001\u4e00\u4e2aSPI\u4f4d\u6570\u636e<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62dfSPI\u534f\u8bae\u53d1\u9001\u4e00\u4e2a\u4f4d\u7684\u6570\u636e\u3002<br \/>\n * \u5b83\u9996\u5148\u5c06\u5f85\u53d1\u9001\u7684\u4f4d\u6570\u636e\u5199\u5165MOSI\u5f15\u811a&#xff0c;\u7136\u540e\u901a\u8fc7\u64cd\u4f5cSCK\u5f15\u811a\u4ea7\u751f\u4e00\u4e2a\u65f6\u949f\u8109\u51b2\u3002<br \/>\n *<br \/>\n * &#064;param bit \u5f85\u53d1\u9001\u7684\u4f4d\u6570\u636e&#xff0c;\u53d6\u503c\u4e3a0\u62161<br \/>\n *\/<br \/>\nvoid SPI_SendBit(uint8_t bit) {<br \/>\n    \/\/ \u5c06\u5f85\u53d1\u9001\u7684\u4f4d\u6570\u636e\u5199\u5165MOSI\u5f15\u811a&#xff0c;\u5c06bit\u5f3a\u5236\u8f6c\u6362\u4e3aGPIO_PinState\u7c7b\u578b<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_MOSI_PIN, (GPIO_PinState)bit);<br \/>\n    \/\/ \u5c06SCK\u5f15\u811a\u7f6e\u9ad8&#xff0c;\u4ea7\u751f\u65f6\u949f\u4fe1\u53f7\u7684\u4e0a\u5347\u6cbf<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_SET);<br \/>\n    \/\/ \u5c06SCK\u5f15\u811a\u7f6e\u4f4e&#xff0c;\u4ea7\u751f\u65f6\u949f\u4fe1\u53f7\u7684\u4e0b\u964d\u6cbf&#xff0c;\u5b8c\u6210\u4e00\u4e2a\u65f6\u949f\u5468\u671f<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_RESET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u63a5\u6536\u4e00\u4e2aSPI\u4f4d\u6570\u636e<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u7528\u4e8e\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62dfSPI\u534f\u8bae\u63a5\u6536\u4e00\u4e2a\u4f4d\u7684\u6570\u636e\u3002<br \/>\n * \u5b83\u901a\u8fc7\u64cd\u4f5cSCK\u5f15\u811a\u4ea7\u751f\u4e00\u4e2a\u65f6\u949f\u8109\u51b2&#xff0c;\u5728\u65f6\u949f\u4e0a\u5347\u6cbf\u65f6\u8bfb\u53d6MISO\u5f15\u811a\u7684\u6570\u636e\u3002<br \/>\n *<br \/>\n * &#064;return uint8_t \u63a5\u6536\u5230\u7684\u4f4d\u6570\u636e&#xff0c;\u53d6\u503c\u4e3a0\u62161<br \/>\n *\/<br \/>\nuint8_t SPI_ReceiveBit(void) {<br \/>\n    \/\/ \u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\u7528\u4e8e\u5b58\u50a8\u63a5\u6536\u5230\u7684\u4f4d\u6570\u636e<br \/>\n    uint8_t bit;<br \/>\n    \/\/ \u5c06SCK\u5f15\u811a\u7f6e\u9ad8&#xff0c;\u4ea7\u751f\u65f6\u949f\u4fe1\u53f7\u7684\u4e0a\u5347\u6cbf<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_SET);<br \/>\n    \/\/ \u8bfb\u53d6MISO\u5f15\u811a\u7684\u7535\u5e73\u72b6\u6001&#xff0c;\u5c06\u5176\u8d4b\u503c\u7ed9bit\u53d8\u91cf<br \/>\n    bit &#061; HAL_GPIO_ReadPin(SPI_GPIO_PORT, SPI_MISO_PIN);<br \/>\n    \/\/ \u5c06SCK\u5f15\u811a\u7f6e\u4f4e&#xff0c;\u4ea7\u751f\u65f6\u949f\u4fe1\u53f7\u7684\u4e0b\u964d\u6cbf&#xff0c;\u5b8c\u6210\u4e00\u4e2a\u65f6\u949f\u5468\u671f<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_SCK_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u8fd4\u56de\u63a5\u6536\u5230\u7684\u4f4d\u6570\u636e<br \/>\n    return bit;<br \/>\n} <\/p>\n<h5>3.3 \u8f6f\u4ef6\u6a21\u62df SPI \u5b57\u8282\u64cd\u4f5c<\/h5>\n<p>\u57fa\u4e8e\u4f4d\u64cd\u4f5c\u51fd\u6570&#xff0c;\u5b9e\u73b0\u5b57\u8282\u7684\u53d1\u9001\u548c\u63a5\u6536\u3002<\/p>\n<p>\/**<br \/>\n * &#064;brief \u901a\u8fc7\u8f6f\u4ef6\u6a21\u62dfSPI\u534f\u8bae\u53d1\u9001\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e&#xff0c;\u5e76\u540c\u65f6\u63a5\u6536\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u4f1a\u5faa\u73af8\u6b21&#xff0c;\u6bcf\u6b21\u53d1\u9001\u4e00\u4f4d\u6570\u636e\u5e76\u63a5\u6536\u4e00\u4f4d\u6570\u636e&#xff0c;\u6700\u7ec8\u7ec4\u5408\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u8282\u3002<br \/>\n * \u53d1\u9001\u6570\u636e\u65f6&#xff0c;\u4ece\u6700\u9ad8\u4f4d\u5f00\u59cb\u9010\u4f4d\u53d1\u9001&#xff1b;\u63a5\u6536\u6570\u636e\u65f6&#xff0c;\u540c\u6837\u4ece\u6700\u9ad8\u4f4d\u5f00\u59cb\u9010\u4f4d\u63a5\u6536\u5e76\u7ec4\u5408\u3002<br \/>\n *<br \/>\n * &#064;param byte \u8981\u53d1\u9001\u7684\u5b57\u8282\u6570\u636e<br \/>\n * &#064;return uint8_t \u63a5\u6536\u5230\u7684\u5b57\u8282\u6570\u636e<br \/>\n *\/<br \/>\nuint8_t SPI_SendByte(uint8_t byte) {<br \/>\n    \/\/ \u5b9a\u4e49\u5faa\u73af\u53d8\u91cfi\u7528\u4e8e\u5faa\u73af\u53d1\u9001\u548c\u63a5\u6536\u6bcf\u4e00\u4f4d\u6570\u636e<br \/>\n    \/\/ \u5b9a\u4e49\u53d8\u91cfreceived_byte\u7528\u4e8e\u5b58\u50a8\u63a5\u6536\u5230\u7684\u5b57\u8282\u6570\u636e&#xff0c;\u521d\u59cb\u5316\u4e3a0<br \/>\n    uint8_t i, received_byte &#061; 0;<br \/>\n    \/\/ \u5faa\u73af8\u6b21&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u5b57\u8282\u67098\u4f4d<br \/>\n    for (i &#061; 0; i &lt; 8; i&#043;&#043;) {<br \/>\n        \/\/ \u53d1\u9001\u5f53\u524d\u4f4d\u7684\u6570\u636e<br \/>\n        \/\/ (byte &gt;&gt; (7 &#8211; i))\u5c06byte\u53f3\u79fb(7 &#8211; i)\u4f4d&#xff0c;\u4f7f\u5f97\u8981\u53d1\u9001\u7684\u4f4d\u79fb\u52a8\u5230\u6700\u4f4e\u4f4d<br \/>\n        \/\/ &amp; 0x01\u5c06\u8be5\u4f4d\u4e0e1\u8fdb\u884c\u6309\u4f4d\u4e0e\u64cd\u4f5c&#xff0c;\u63d0\u53d6\u51fa\u8be5\u4f4d\u7684\u503c<br \/>\n        \/\/ \u8c03\u7528SPI_SendBit\u51fd\u6570\u53d1\u9001\u8be5\u4f4d<br \/>\n        SPI_SendBit((byte &gt;&gt; (7 &#8211; i)) &amp; 0x01);<br \/>\n        \/\/ \u63a5\u6536\u4e00\u4f4d\u6570\u636e\u5e76\u7ec4\u5408\u5230received_byte\u4e2d<br \/>\n        \/\/ \u8c03\u7528SPI_ReceiveBit\u51fd\u6570\u63a5\u6536\u4e00\u4f4d\u6570\u636e<br \/>\n        \/\/ \u5c06\u63a5\u6536\u5230\u7684\u4f4d\u5de6\u79fb(7 &#8211; i)\u4f4d&#xff0c;\u79fb\u52a8\u5230\u5408\u9002\u7684\u4f4d\u7f6e<br \/>\n        \/\/ \u7136\u540e\u4e0ereceived_byte\u8fdb\u884c\u6309\u4f4d\u6216\u64cd\u4f5c&#xff0c;\u5c06\u8be5\u4f4d\u7ec4\u5408\u5230received_byte\u4e2d<br \/>\n        received_byte |&#061; (SPI_ReceiveBit() &lt;&lt; (7 &#8211; i));<br \/>\n    }<br \/>\n    \/\/ \u8fd4\u56de\u63a5\u6536\u5230\u7684\u5b8c\u6574\u5b57\u8282\u6570\u636e<br \/>\n    return received_byte;<br \/>\n} <\/p>\n<h5>3.4 \u8bfb\u5199 W25Q128 \u51fd\u6570<\/h5>\n<p>\u5b9e\u73b0\u5bf9 W25Q128 \u7684\u8bfb\u5199\u64cd\u4f5c&#xff0c;\u5305\u62ec\u8bfb\u53d6\u8bbe\u5907 ID\u3001\u5199\u4f7f\u80fd\u3001\u64e6\u9664\u6247\u533a\u548c\u5199\u5165\u6570\u636e\u7b49\u529f\u80fd\u3002<\/p>\n<p>\/**<br \/>\n * &#064;brief \u8bfb\u53d6W25Q128\u7684\u8bbe\u5907ID<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u8bfb\u53d6\u8bbe\u5907ID\u7684\u547d\u4ee4&#xff0c;\u7136\u540e\u63a5\u6536\u5e76\u8fd4\u56de\u8bbe\u5907ID\u3002<br \/>\n *<br \/>\n * &#064;return uint16_t \u8bfb\u53d6\u5230\u7684W25Q128\u8bbe\u5907ID<br \/>\n *\/<br \/>\nuint16_t W25Q128_ReadID(void) {<br \/>\n    \/\/ \u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u5230\u7684\u8bbe\u5907ID<br \/>\n    uint16_t device_id;<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u53d1\u9001\u8bfb\u53d6\u8bbe\u5907ID\u7684\u547d\u4ee4\u78010x90<br \/>\n    SPI_SendByte(0x90);<br \/>\n    \/\/ \u53d1\u9001\u5730\u5740\u5b57\u8282&#xff0c;\u8fd9\u91cc\u5730\u5740\u4e3a0x000000<br \/>\n    SPI_SendByte(0x00);<br \/>\n    SPI_SendByte(0x00);<br \/>\n    SPI_SendByte(0x00);<br \/>\n    \/\/ \u5148\u63a5\u6536\u9ad88\u4f4d\u6570\u636e&#xff0c;\u5e76\u5de6\u79fb8\u4f4d\u5b58\u50a8\u5230device_id\u7684\u9ad88\u4f4d<br \/>\n    device_id &#061; (uint16_t)SPI_SendByte(0xFF) &lt;&lt; 8;<br \/>\n    \/\/ \u518d\u63a5\u6536\u4f4e8\u4f4d\u6570\u636e&#xff0c;\u5e76\u4e0edevice_id\u8fdb\u884c\u6309\u4f4d\u6216\u64cd\u4f5c&#xff0c;\u7ec4\u5408\u6210\u5b8c\u6574\u768416\u4f4d\u8bbe\u5907ID<br \/>\n    device_id |&#061; SPI_SendByte(0xFF);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n    \/\/ \u8fd4\u56de\u8bfb\u53d6\u5230\u7684\u8bbe\u5907ID<br \/>\n    return device_id;<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u80fdW25Q128\u7684\u5199\u64cd\u4f5c<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u5199\u4f7f\u80fd\u547d\u4ee4&#xff0c;\u5141\u8bb8\u540e\u7eed\u7684\u5199\u64cd\u4f5c\u3002<br \/>\n *\/<br \/>\nvoid W25Q128_WriteEnable(void) {<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u53d1\u9001\u5199\u4f7f\u80fd\u547d\u4ee4\u78010x06<br \/>\n    SPI_SendByte(0x06);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u64e6\u9664W25Q128\u7684\u6307\u5b9a\u6247\u533a<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u5148\u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u6247\u533a\u64e6\u9664\u547d\u4ee4\u548c\u6247\u533a\u5730\u5740&#xff0c;\u5b8c\u6210\u6247\u533a\u64e6\u9664\u64cd\u4f5c\u3002<br \/>\n *<br \/>\n * &#064;param sector_address \u8981\u64e6\u9664\u7684\u6247\u533a\u5730\u5740<br \/>\n *\/<br \/>\nvoid W25Q128_SectorErase(uint32_t sector_address) {<br \/>\n    \/\/ \u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u5141\u8bb8\u540e\u7eed\u7684\u64e6\u9664\u64cd\u4f5c<br \/>\n    W25Q128_WriteEnable();<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u53d1\u9001\u6247\u533a\u64e6\u9664\u547d\u4ee4\u78010x20<br \/>\n    SPI_SendByte(0x20);<br \/>\n    \/\/ \u53d1\u9001\u6247\u533a\u5730\u5740\u7684\u9ad88\u4f4d<br \/>\n    SPI_SendByte((sector_address &gt;&gt; 16) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u6247\u533a\u5730\u5740\u7684\u4e2d\u95f48\u4f4d<br \/>\n    SPI_SendByte((sector_address &gt;&gt; 8) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u6247\u533a\u5730\u5740\u7684\u4f4e8\u4f4d<br \/>\n    SPI_SendByte(sector_address &amp; 0xFF);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u5411W25Q128\u7684\u6307\u5b9a\u5730\u5740\u5199\u5165\u6570\u636e<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u5148\u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u9875\u7f16\u7a0b\u547d\u4ee4\u548c\u5199\u5165\u5730\u5740&#xff0c;\u6700\u540e\u5c06\u6570\u636e\u9010\u5b57\u8282\u5199\u5165\u3002<br \/>\n *<br \/>\n * &#064;param address \u5199\u5165\u6570\u636e\u7684\u8d77\u59cb\u5730\u5740<br \/>\n * &#064;param data \u8981\u5199\u5165\u7684\u6570\u636e\u6570\u7ec4<br \/>\n * &#064;param length \u8981\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6<br \/>\n *\/<br \/>\nvoid W25Q128_PageProgram(uint32_t address, uint8_t *data, uint16_t length) {<br \/>\n    \/\/ \u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u5141\u8bb8\u540e\u7eed\u7684\u5199\u5165\u64cd\u4f5c<br \/>\n    W25Q128_WriteEnable();<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u53d1\u9001\u9875\u7f16\u7a0b\u547d\u4ee4\u78010x02<br \/>\n    SPI_SendByte(0x02);<br \/>\n    \/\/ \u53d1\u9001\u5199\u5165\u5730\u5740\u7684\u9ad88\u4f4d<br \/>\n    SPI_SendByte((address &gt;&gt; 16) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u5199\u5165\u5730\u5740\u7684\u4e2d\u95f48\u4f4d<br \/>\n    SPI_SendByte((address &gt;&gt; 8) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u5199\u5165\u5730\u5740\u7684\u4f4e8\u4f4d<br \/>\n    SPI_SendByte(address &amp; 0xFF);<br \/>\n    \/\/ \u5faa\u73af\u5c06\u6570\u636e\u9010\u5b57\u8282\u5199\u5165W25Q128<br \/>\n    for (uint16_t i &#061; 0; i &lt; length; i&#043;&#043;) {<br \/>\n        SPI_SendByte(data[i]);<br \/>\n    }<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4eceW25Q128\u7684\u6307\u5b9a\u5730\u5740\u8bfb\u53d6\u6570\u636e<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u8bfb\u53d6\u6570\u636e\u547d\u4ee4\u548c\u8bfb\u53d6\u5730\u5740&#xff0c;\u7136\u540e\u5c06\u6570\u636e\u9010\u5b57\u8282\u8bfb\u53d6\u5230\u6307\u5b9a\u6570\u7ec4\u4e2d\u3002<br \/>\n *<br \/>\n * &#064;param address \u8bfb\u53d6\u6570\u636e\u7684\u8d77\u59cb\u5730\u5740<br \/>\n * &#064;param data \u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u6570\u636e\u7684\u6570\u7ec4<br \/>\n * &#064;param length \u8981\u8bfb\u53d6\u7684\u6570\u636e\u957f\u5ea6<br \/>\n *\/<br \/>\nvoid W25Q128_ReadData(uint32_t address, uint8_t *data, uint16_t length) {<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_RESET);<br \/>\n    \/\/ \u53d1\u9001\u8bfb\u53d6\u6570\u636e\u547d\u4ee4\u78010x03<br \/>\n    SPI_SendByte(0x03);<br \/>\n    \/\/ \u53d1\u9001\u8bfb\u53d6\u5730\u5740\u7684\u9ad88\u4f4d<br \/>\n    SPI_SendByte((address &gt;&gt; 16) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u8bfb\u53d6\u5730\u5740\u7684\u4e2d\u95f48\u4f4d<br \/>\n    SPI_SendByte((address &gt;&gt; 8) &amp; 0xFF);<br \/>\n    \/\/ \u53d1\u9001\u8bfb\u53d6\u5730\u5740\u7684\u4f4e8\u4f4d<br \/>\n    SPI_SendByte(address &amp; 0xFF);<br \/>\n    \/\/ \u5faa\u73af\u5c06\u6570\u636e\u9010\u5b57\u8282\u4eceW25Q128\u8bfb\u53d6\u5230data\u6570\u7ec4\u4e2d<br \/>\n    for (uint16_t i &#061; 0; i &lt; length; i&#043;&#043;) {<br \/>\n        data[i] &#061; SPI_SendByte(0xFF);<br \/>\n    }<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128<br \/>\n    HAL_GPIO_WritePin(SPI_GPIO_PORT, SPI_NSS_PIN, GPIO_PIN_SET);<br \/>\n} <\/p>\n<h4>4. \u786c\u4ef6 SPI \u8bfb\u5199 W25Q128<\/h4>\n<h5>4.1 \u521d\u59cb\u5316\u786c\u4ef6 SPI<\/h5>\n<p>\u4f7f\u7528 STM32F407 \u7684\u786c\u4ef6 SPI \u63a5\u53e3\u8fdb\u884c\u901a\u4fe1&#xff0c;\u9700\u8981\u521d\u59cb\u5316 SPI \u5916\u8bbe\u3002<\/p>\n<p>\/\/ \u5b9a\u4e49\u4e00\u4e2aSPI\u53e5\u67c4\u7ed3\u6784\u4f53\u53d8\u91cf&#xff0c;\u7528\u4e8e\u914d\u7f6e\u548c\u64cd\u4f5cSPI1\u5916\u8bbe<br \/>\nSPI_HandleTypeDef hspi1;<\/p>\n<p>\/**<br \/>\n * &#064;brief \u521d\u59cb\u5316SPI1\u5916\u8bbe<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u7528\u4e8e\u5bf9SPI1\u5916\u8bbe\u8fdb\u884c\u914d\u7f6e&#xff0c;\u8bbe\u7f6e\u5176\u5de5\u4f5c\u6a21\u5f0f\u3001\u6570\u636e\u65b9\u5411\u3001\u6570\u636e\u5927\u5c0f\u7b49\u53c2\u6570&#xff0c;<br \/>\n * \u5e76\u8c03\u7528HAL\u5e93\u7684\u521d\u59cb\u5316\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\u3002\u82e5\u521d\u59cb\u5316\u5931\u8d25&#xff0c;\u8c03\u7528\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002<br \/>\n *\/<br \/>\nvoid SPI1_Init(void) {<br \/>\n    \/\/ \u6307\u5b9a\u4f7f\u7528\u7684SPI\u5916\u8bbe\u5b9e\u4f8b\u4e3aSPI1<br \/>\n    hspi1.Instance &#061; SPI1;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u5de5\u4f5c\u6a21\u5f0f\u4e3a\u4e3b\u6a21\u5f0f&#xff0c;\u5373STM32\u4f5c\u4e3a\u4e3b\u8bbe\u5907\u63a7\u5236\u901a\u4fe1<br \/>\n    hspi1.Init.Mode &#061; SPI_MODE_MASTER;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u6570\u636e\u4f20\u8f93\u65b9\u5411\u4e3a\u53cc\u7ebf\u6a21\u5f0f&#xff0c;\u5373\u540c\u65f6\u652f\u6301\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e<br \/>\n    hspi1.Init.Direction &#061; SPI_DIRECTION_2LINES;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u6570\u636e\u4f20\u8f93\u5927\u5c0f\u4e3a8\u4f4d&#xff0c;\u5373\u6bcf\u6b21\u4f20\u8f93\u4e00\u4e2a\u5b57\u8282\u7684\u6570\u636e<br \/>\n    hspi1.Init.DataSize &#061; SPI_DATASIZE_8BIT;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u65f6\u949f\u6781\u6027\u4e3a\u4f4e\u7535\u5e73&#xff0c;\u5373\u7a7a\u95f2\u72b6\u6001\u4e0b\u65f6\u949f\u4fe1\u53f7\u4e3a\u4f4e\u7535\u5e73<br \/>\n    hspi1.Init.CLKPolarity &#061; SPI_POLARITY_LOW;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u65f6\u949f\u76f8\u4f4d\u4e3a\u7b2c\u4e00\u4e2a\u8fb9\u6cbf\u91c7\u6837\u6570\u636e&#xff0c;\u5373\u6570\u636e\u5728\u65f6\u949f\u4fe1\u53f7\u7684\u7b2c\u4e00\u4e2a\u8fb9\u6cbf\u88ab\u91c7\u6837<br \/>\n    hspi1.Init.CLKPhase &#061; SPI_PHASE_1EDGE;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u7247\u9009\u4fe1\u53f7\u4e3a\u8f6f\u4ef6\u63a7\u5236&#xff0c;\u5373\u901a\u8fc7\u8f6f\u4ef6\u6765\u63a7\u5236\u7247\u9009\u5f15\u811a\u7684\u7535\u5e73<br \/>\n    hspi1.Init.NSS &#061; SPI_NSS_SOFT;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u6ce2\u7279\u7387\u9884\u5206\u9891\u7cfb\u6570\u4e3a256&#xff0c;\u7528\u4e8e\u964d\u4f4eSPI\u901a\u4fe1\u7684\u65f6\u949f\u9891\u7387<br \/>\n    hspi1.Init.BaudRatePrescaler &#061; SPI_BAUDRATEPRESCALER_256;<br \/>\n    \/\/ \u8bbe\u7f6eSPI\u6570\u636e\u4f20\u8f93\u7684\u4f4d\u987a\u5e8f\u4e3a\u9ad8\u4f4d\u5728\u524d&#xff0c;\u5373\u5148\u4f20\u8f93\u6570\u636e\u7684\u6700\u9ad8\u4f4d<br \/>\n    hspi1.Init.FirstBit &#061; SPI_FIRSTBIT_MSB;<br \/>\n    \/\/ \u7981\u7528SPI\u7684TI\u6a21\u5f0f&#xff0c;TI\u6a21\u5f0f\u901a\u5e38\u7528\u4e8e\u7279\u5b9a\u7684\u901a\u4fe1\u534f\u8bae<br \/>\n    hspi1.Init.TIMode &#061; SPI_TIMODE_DISABLE;<br \/>\n    \/\/ \u7981\u7528SPI\u7684CRC\u6821\u9a8c\u529f\u80fd&#xff0c;CRC\u6821\u9a8c\u7528\u4e8e\u6570\u636e\u4f20\u8f93\u7684\u9519\u8bef\u68c0\u6d4b<br \/>\n    hspi1.Init.CRCCalculation &#061; SPI_CRCCALCULATION_DISABLE;<br \/>\n    \/\/ \u8bbe\u7f6eCRC\u591a\u9879\u5f0f\u7684\u503c\u4e3a7&#xff0c;\u7531\u4e8eCRC\u6821\u9a8c\u5df2\u7981\u7528&#xff0c;\u6b64\u503c\u65e0\u5b9e\u9645\u4f5c\u7528<br \/>\n    hspi1.Init.CRCPolynomial &#061; 7;<br \/>\n    \/\/ \u8c03\u7528HAL\u5e93\u7684SPI\u521d\u59cb\u5316\u51fd\u6570\u8fdb\u884cSPI1\u5916\u8bbe\u7684\u521d\u59cb\u5316<br \/>\n    if (HAL_SPI_Init(&amp;hspi1) !&#061; HAL_OK) {<br \/>\n        \/\/ \u82e5\u521d\u59cb\u5316\u5931\u8d25&#xff0c;\u8c03\u7528\u9519\u8bef\u5904\u7406\u51fd\u6570\u8fdb\u884c\u5904\u7406<br \/>\n        Error_Handler();<br \/>\n    }<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief SPI\u5916\u8bbe\u7684\u5e95\u5c42\u786c\u4ef6\u521d\u59cb\u5316\u51fd\u6570<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u7528\u4e8e\u5bf9SPI\u5916\u8bbe\u6240\u4f7f\u7528\u7684GPIO\u5f15\u811a\u8fdb\u884c\u521d\u59cb\u5316\u914d\u7f6e&#xff0c;<br \/>\n * \u5305\u62ec\u4f7f\u80fd\u76f8\u5173\u65f6\u949f\u3001\u8bbe\u7f6e\u5f15\u811a\u6a21\u5f0f\u3001\u901f\u5ea6\u548c\u590d\u7528\u529f\u80fd\u7b49\u3002<br \/>\n *<br \/>\n * &#064;param spiHandle \u6307\u5411SPI\u53e5\u67c4\u7ed3\u6784\u4f53\u7684\u6307\u9488&#xff0c;\u7528\u4e8e\u5224\u65ad\u662f\u54ea\u4e2aSPI\u5916\u8bbe<br \/>\n *\/<br \/>\nvoid HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) {<br \/>\n    \/\/ \u5b9a\u4e49\u4e00\u4e2aGPIO\u521d\u59cb\u5316\u7ed3\u6784\u4f53\u53d8\u91cf&#xff0c;\u7528\u4e8e\u914d\u7f6eGPIO\u5f15\u811a<br \/>\n    GPIO_InitTypeDef GPIO_InitStruct &#061; {0};<br \/>\n    \/\/ \u5224\u65ad\u8981\u521d\u59cb\u5316\u7684SPI\u5916\u8bbe\u5b9e\u4f8b\u662f\u5426\u4e3aSPI1<br \/>\n    if(spiHandle-&gt;Instance&#061;&#061;SPI1) {<br \/>\n        \/\/ \u4f7f\u80fdSPI1\u5916\u8bbe\u7684\u65f6\u949f&#xff0c;\u4ee5\u4fbf\u53ef\u4ee5\u5bf9\u5176\u8fdb\u884c\u914d\u7f6e\u548c\u4f7f\u7528<br \/>\n        __HAL_RCC_SPI1_CLK_ENABLE();<br \/>\n        \/\/ \u4f7f\u80fdGPIOA\u7aef\u53e3\u7684\u65f6\u949f&#xff0c;\u56e0\u4e3aSPI1\u4f7f\u7528\u7684\u5f15\u811a\u4f4d\u4e8eGPIOA\u7aef\u53e3<br \/>\n        __HAL_RCC_GPIOA_CLK_ENABLE();<\/p>\n<p>        \/\/ \u914d\u7f6eSPI1\u7684SCK&#xff08;\u65f6\u949f&#xff09;\u3001MISO&#xff08;\u4e3b\u8bbe\u5907\u8f93\u5165\u4ece\u8bbe\u5907\u8f93\u51fa&#xff09;\u3001MOSI&#xff08;\u4e3b\u8bbe\u5907\u8f93\u51fa\u4ece\u8bbe\u5907\u8f93\u5165&#xff09;\u5f15\u811a<br \/>\n        \/\/ \u5c06\u8fd9\u4e9b\u5f15\u811a\u7684\u914d\u7f6e\u4fe1\u606f\u5b58\u50a8\u5728GPIO_InitStruct\u7ed3\u6784\u4f53\u4e2d<br \/>\n        GPIO_InitStruct.Pin &#061; GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;<br \/>\n        \/\/ \u8bbe\u7f6e\u8fd9\u4e9b\u5f15\u811a\u4e3a\u590d\u7528\u63a8\u633d\u8f93\u51fa\u6a21\u5f0f&#xff0c;\u7528\u4e8eSPI\u901a\u4fe1<br \/>\n        GPIO_InitStruct.Mode &#061; GPIO_MODE_AF_PP;<br \/>\n        \/\/ \u4e0d\u4f7f\u7528\u4e0a\u62c9\u6216\u4e0b\u62c9\u7535\u963b<br \/>\n        GPIO_InitStruct.Pull &#061; GPIO_NOPULL;<br \/>\n        \/\/ \u8bbe\u7f6e\u5f15\u811a\u7684\u8f93\u51fa\u901f\u5ea6\u4e3a\u975e\u5e38\u9ad8&#xff0c;\u4ee5\u9002\u5e94\u9ad8\u901fSPI\u901a\u4fe1<br \/>\n        GPIO_InitStruct.Speed &#061; GPIO_SPEED_FREQ_VERY_HIGH;<br \/>\n        \/\/ \u8bbe\u7f6e\u8fd9\u4e9b\u5f15\u811a\u7684\u590d\u7528\u529f\u80fd\u4e3aSPI1&#xff0c;\u5373\u4f5c\u4e3aSPI1\u7684\u76f8\u5173\u4fe1\u53f7\u5f15\u811a<br \/>\n        GPIO_InitStruct.Alternate &#061; GPIO_AF5_SPI1;<br \/>\n        \/\/ \u6839\u636e\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u521d\u59cb\u5316GPIOA\u7aef\u53e3\u7684\u76f8\u5e94\u5f15\u811a<br \/>\n        HAL_GPIO_Init(GPIOA, &amp;GPIO_InitStruct);<\/p>\n<p>        \/\/ \u914d\u7f6eSPI1\u7684NSS&#xff08;\u7247\u9009&#xff09;\u5f15\u811a<br \/>\n        \/\/ \u91cd\u65b0\u8bbe\u7f6e\u5f15\u811a\u4e3aGPIO_PIN_4<br \/>\n        GPIO_InitStruct.Pin &#061; GPIO_PIN_4;<br \/>\n        \/\/ \u8bbe\u7f6e\u8be5\u5f15\u811a\u4e3a\u63a8\u633d\u8f93\u51fa\u6a21\u5f0f&#xff0c;\u7528\u4e8e\u8f6f\u4ef6\u63a7\u5236\u7247\u9009\u4fe1\u53f7<br \/>\n        GPIO_InitStruct.Mode &#061; GPIO_MODE_OUTPUT_PP;<br \/>\n        \/\/ \u4e0d\u4f7f\u7528\u4e0a\u62c9\u6216\u4e0b\u62c9\u7535\u963b<br \/>\n        GPIO_InitStruct.Pull &#061; GPIO_NOPULL;<br \/>\n        \/\/ \u8bbe\u7f6e\u5f15\u811a\u7684\u8f93\u51fa\u901f\u5ea6\u4e3a\u4f4e\u9891<br \/>\n        GPIO_InitStruct.Speed &#061; GPIO_SPEED_FREQ_LOW;<br \/>\n        \/\/ \u6839\u636e\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u521d\u59cb\u5316GPIOA\u7aef\u53e3\u7684SPI_NSS_PIN\u5f15\u811a<br \/>\n        HAL_GPIO_Init(GPIOA, &amp;GPIO_InitStruct);<br \/>\n        \/\/ \u521d\u59cb\u65f6\u5c06\u7247\u9009\u4fe1\u53f7\u5f15\u811a\u62c9\u9ad8&#xff0c;\u7981\u7528\u4ece\u8bbe\u5907<br \/>\n        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n    }<br \/>\n} <\/p>\n<h5>4.2 \u8bfb\u5199 W25Q128 \u51fd\u6570<\/h5>\n<p>\u4f7f\u7528\u786c\u4ef6 SPI \u5b9e\u73b0\u5bf9 W25Q128 \u7684\u8bfb\u5199\u64cd\u4f5c\u3002<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u7528\u786c\u4ef6SPI\u8bfb\u53d6W25Q128\u7684\u8bbe\u5907ID<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7\u786c\u4ef6SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u8bfb\u53d6\u8bbe\u5907ID\u7684\u547d\u4ee4&#xff0c;\u7136\u540e\u63a5\u6536\u5e76\u8fd4\u56de\u8bbe\u5907ID\u3002<br \/>\n *<br \/>\n * &#064;return uint16_t \u8bfb\u53d6\u5230\u7684W25Q128\u8bbe\u5907ID<br \/>\n *\/<br \/>\nuint16_t W25Q128_ReadID_HardwareSPI(void) {<br \/>\n    \/\/ \u5b9a\u4e49\u53d1\u9001\u6570\u636e\u7684\u6570\u7ec4&#xff0c;\u5305\u542b\u8bfb\u53d6\u8bbe\u5907ID\u7684\u547d\u4ee4\u548c\u5730\u5740\u4fe1\u606f<br \/>\n    uint8_t tx_data[4] &#061; {0x90, 0x00, 0x00, 0x00};<br \/>\n    \/\/ \u5b9a\u4e49\u63a5\u6536\u6570\u636e\u7684\u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u5230\u7684\u8bbe\u5907ID<br \/>\n    uint8_t rx_data[2];<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001tx_data\u6570\u7ec4\u4e2d\u76844\u4e2a\u5b57\u8282\u6570\u636e<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, tx_data, 4, HAL_MAX_DELAY);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u63a5\u65362\u4e2a\u5b57\u8282\u7684\u6570\u636e\u5230rx_data\u6570\u7ec4\u4e2d<br \/>\n    HAL_SPI_Receive(&amp;hspi1, rx_data, 2, HAL_MAX_DELAY);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n    \/\/ \u5c06\u63a5\u6536\u5230\u7684\u4e24\u4e2a\u5b57\u8282\u6570\u636e\u7ec4\u5408\u6210\u4e00\u4e2a16\u4f4d\u7684\u8bbe\u5907ID<br \/>\n    return (uint16_t)rx_data[0] &lt;&lt; 8 | rx_data[1];<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u7528\u786c\u4ef6SPI\u4f7f\u80fdW25Q128\u7684\u5199\u64cd\u4f5c<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7\u786c\u4ef6SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u5199\u4f7f\u80fd\u547d\u4ee4&#xff0c;\u4ee5\u5141\u8bb8\u540e\u7eed\u7684\u5199\u64cd\u4f5c\u3002<br \/>\n *\/<br \/>\nvoid W25Q128_WriteEnable_HardwareSPI(void) {<br \/>\n    \/\/ \u5b9a\u4e49\u8981\u53d1\u9001\u7684\u5199\u4f7f\u80fd\u547d\u4ee4<br \/>\n    uint8_t tx_data &#061; 0x06;<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001\u5199\u4f7f\u80fd\u547d\u4ee4<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, &amp;tx_data, 1, HAL_MAX_DELAY);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u7528\u786c\u4ef6SPI\u64e6\u9664W25Q128\u7684\u6307\u5b9a\u6247\u533a<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u5148\u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7\u786c\u4ef6SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u6247\u533a\u64e6\u9664\u547d\u4ee4\u548c\u6247\u533a\u5730\u5740\u3002<br \/>\n *<br \/>\n * &#064;param sector_address \u8981\u64e6\u9664\u7684\u6247\u533a\u5730\u5740<br \/>\n *\/<br \/>\nvoid W25Q128_SectorErase_HardwareSPI(uint32_t sector_address) {<br \/>\n    \/\/ \u8c03\u7528\u5199\u4f7f\u80fd\u51fd\u6570&#xff0c;\u5141\u8bb8\u540e\u7eed\u7684\u64e6\u9664\u64cd\u4f5c<br \/>\n    W25Q128_WriteEnable_HardwareSPI();<br \/>\n    \/\/ \u5b9a\u4e49\u53d1\u9001\u6570\u636e\u7684\u6570\u7ec4&#xff0c;\u5305\u542b\u6247\u533a\u64e6\u9664\u547d\u4ee4\u548c\u6247\u533a\u5730\u5740\u4fe1\u606f<br \/>\n    uint8_t tx_data[4] &#061; {0x20, (uint8_t)(sector_address &gt;&gt; 16), (uint8_t)(sector_address &gt;&gt; 8), (uint8_t)sector_address};<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001tx_data\u6570\u7ec4\u4e2d\u76844\u4e2a\u5b57\u8282\u6570\u636e<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, tx_data, 4, HAL_MAX_DELAY);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u7528\u786c\u4ef6SPI\u5411W25Q128\u7684\u6307\u5b9a\u5730\u5740\u5199\u5165\u6570\u636e<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u5148\u4f7f\u80fd\u5199\u64cd\u4f5c&#xff0c;\u7136\u540e\u901a\u8fc7\u786c\u4ef6SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u9875\u7f16\u7a0b\u547d\u4ee4\u3001\u5199\u5165\u5730\u5740\u548c\u6570\u636e\u3002<br \/>\n *<br \/>\n * &#064;param address \u5199\u5165\u6570\u636e\u7684\u8d77\u59cb\u5730\u5740<br \/>\n * &#064;param data \u8981\u5199\u5165\u7684\u6570\u636e\u6570\u7ec4\u6307\u9488<br \/>\n * &#064;param length \u8981\u5199\u5165\u7684\u6570\u636e\u957f\u5ea6<br \/>\n *\/<br \/>\nvoid W25Q128_PageProgram_HardwareSPI(uint32_t address, uint8_t *data, uint16_t length) {<br \/>\n    \/\/ \u8c03\u7528\u5199\u4f7f\u80fd\u51fd\u6570&#xff0c;\u5141\u8bb8\u540e\u7eed\u7684\u5199\u5165\u64cd\u4f5c<br \/>\n    W25Q128_WriteEnable_HardwareSPI();<br \/>\n    \/\/ \u5b9a\u4e49\u53d1\u9001\u6570\u636e\u7684\u6570\u7ec4&#xff0c;\u5305\u542b\u9875\u7f16\u7a0b\u547d\u4ee4\u548c\u5199\u5165\u5730\u5740\u4fe1\u606f<br \/>\n    uint8_t tx_data[4] &#061; {0x02, (uint8_t)(address &gt;&gt; 16), (uint8_t)(address &gt;&gt; 8), (uint8_t)address};<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001tx_data\u6570\u7ec4\u4e2d\u76844\u4e2a\u5b57\u8282\u6570\u636e<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, tx_data, 4, HAL_MAX_DELAY);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001\u8981\u5199\u5165\u7684\u6570\u636e<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, data, length, HAL_MAX_DELAY);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4f7f\u7528\u786c\u4ef6SPI\u4eceW25Q128\u7684\u6307\u5b9a\u5730\u5740\u8bfb\u53d6\u6570\u636e<br \/>\n *<br \/>\n * \u8be5\u51fd\u6570\u901a\u8fc7\u786c\u4ef6SPI\u63a5\u53e3\u5411W25Q128\u53d1\u9001\u8bfb\u53d6\u6570\u636e\u547d\u4ee4\u548c\u8bfb\u53d6\u5730\u5740&#xff0c;\u7136\u540e\u63a5\u6536\u6570\u636e\u3002<br \/>\n *<br \/>\n * &#064;param address \u8bfb\u53d6\u6570\u636e\u7684\u8d77\u59cb\u5730\u5740<br \/>\n * &#064;param data \u7528\u4e8e\u5b58\u50a8\u8bfb\u53d6\u6570\u636e\u7684\u6570\u7ec4\u6307\u9488<br \/>\n * &#064;param length \u8981\u8bfb\u53d6\u7684\u6570\u636e\u957f\u5ea6<br \/>\n *\/<br \/>\nvoid W25Q128_ReadData_HardwareSPI(uint32_t address, uint8_t *data, uint16_t length) {<br \/>\n    \/\/ \u5b9a\u4e49\u53d1\u9001\u6570\u636e\u7684\u6570\u7ec4&#xff0c;\u5305\u542b\u8bfb\u53d6\u6570\u636e\u547d\u4ee4\u548c\u8bfb\u53d6\u5730\u5740\u4fe1\u606f<br \/>\n    uint8_t tx_data[4] &#061; {0x03, (uint8_t)(address &gt;&gt; 16), (uint8_t)(address &gt;&gt; 8), (uint8_t)address};<br \/>\n    \/\/ \u62c9\u4f4e\u7247\u9009\u4fe1\u53f7&#xff0c;\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u53d1\u9001tx_data\u6570\u7ec4\u4e2d\u76844\u4e2a\u5b57\u8282\u6570\u636e<br \/>\n    HAL_SPI_Transmit(&amp;hspi1, tx_data, 4, HAL_MAX_DELAY);<br \/>\n    \/\/ \u901a\u8fc7\u786c\u4ef6SPI\u63a5\u6536\u6307\u5b9a\u957f\u5ea6\u7684\u6570\u636e\u5230data\u6570\u7ec4\u4e2d<br \/>\n    HAL_SPI_Receive(&amp;hspi1, data, length, HAL_MAX_DELAY);<br \/>\n    \/\/ \u62c9\u9ad8\u7247\u9009\u4fe1\u53f7&#xff0c;\u53d6\u6d88\u9009\u4e2dW25Q128\u82af\u7247<br \/>\n    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);<br \/>\n} <\/p>\n<h4>5. \u4e3b\u51fd\u6570\u6d4b\u8bd5<\/h4>\n<p>\u5728\u4e3b\u51fd\u6570\u4e2d\u8c03\u7528\u4e0a\u8ff0\u51fd\u6570\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n<p>\/**<br \/>\n * &#064;brief \u4e3b\u51fd\u6570&#xff0c;\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9<br \/>\n *<br \/>\n * \u6b64\u51fd\u6570\u5b8c\u6210\u7cfb\u7edf\u521d\u59cb\u5316&#xff0c;\u5305\u62ecHAL\u5e93\u3001\u7cfb\u7edf\u65f6\u949f\u3001SPI\u76f8\u5173\u7684GPIO\u548cSPI\u5916\u8bbe&#xff0c;<br \/>\n * \u7136\u540e\u901a\u8fc7\u8f6f\u4ef6\u6a21\u62dfSPI\u548c\u786c\u4ef6SPI\u4e24\u79cd\u65b9\u5f0f\u8bfb\u53d6W25Q128\u7684\u8bbe\u5907ID&#xff0c;<br \/>\n * \u63a5\u7740\u5206\u522b\u4f7f\u7528\u8f6f\u4ef6\u6a21\u62dfSPI\u548c\u786c\u4ef6SPI\u5bf9W25Q128\u8fdb\u884c\u6247\u533a\u64e6\u9664\u3001\u6570\u636e\u5199\u5165\u548c\u8bfb\u53d6\u64cd\u4f5c&#xff0c;<br \/>\n * \u6700\u540e\u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u4fdd\u6301\u7a0b\u5e8f\u6301\u7eed\u8fd0\u884c\u3002<br \/>\n *\/<br \/>\nint main(void) {<br \/>\n    \/\/ \u521d\u59cb\u5316HAL\u5e93&#xff0c;\u8fd9\u662fSTM32 HAL\u5e93\u7684\u57fa\u7840\u521d\u59cb\u5316\u6b65\u9aa4&#xff0c;<br \/>\n    \/\/ \u5b83\u4f1a\u8fdb\u884c\u4e00\u4e9b\u5e95\u5c42\u7684\u786c\u4ef6\u521d\u59cb\u5316\u548c\u914d\u7f6e&#xff0c;\u4e3a\u540e\u7eed\u4f7f\u7528HAL\u5e93\u51fd\u6570\u505a\u51c6\u5907<br \/>\n    HAL_Init();<\/p>\n<p>    \/\/ \u914d\u7f6e\u7cfb\u7edf\u65f6\u949f&#xff0c;\u8bbe\u7f6e\u5408\u9002\u7684\u65f6\u949f\u9891\u7387&#xff0c;\u786e\u4fdd\u7cfb\u7edf\u5404\u4e2a\u6a21\u5757\u80fd\u6b63\u5e38\u5de5\u4f5c<br \/>\n    \/\/ \u8be5\u51fd\u6570\u53ef\u80fd\u5305\u542b\u4e86\u5bf9\u6676\u632f\u3001PLL\u7b49\u65f6\u949f\u6e90\u548c\u65f6\u949f\u5206\u9891\u5668\u7684\u914d\u7f6e<br \/>\n    SystemClock_Config();<\/p>\n<p>    \/\/ \u521d\u59cb\u5316SPI\u901a\u4fe1\u6240\u9700\u7684GPIO\u5f15\u811a&#xff0c;\u5305\u62ecSCK\u3001MISO\u3001MOSI\u548cNSS\u5f15\u811a&#xff0c;<br \/>\n    \/\/ \u4e3a\u8f6f\u4ef6\u6a21\u62dfSPI\u901a\u4fe1\u505a\u51c6\u5907<br \/>\n    SPI_GPIO_Init();<\/p>\n<p>    \/\/ \u521d\u59cb\u5316SPI1\u5916\u8bbe&#xff0c;\u914d\u7f6eSPI1\u7684\u5de5\u4f5c\u6a21\u5f0f\u3001\u6570\u636e\u65b9\u5411\u3001\u65f6\u949f\u6781\u6027\u7b49\u53c2\u6570&#xff0c;<br \/>\n    \/\/ \u4e3a\u786c\u4ef6SPI\u901a\u4fe1\u505a\u51c6\u5907<br \/>\n    SPI1_Init();<\/p>\n<p>    \/\/ \u4f7f\u7528\u8f6f\u4ef6\u6a21\u62dfSPI\u7684\u65b9\u5f0f\u8bfb\u53d6W25Q128\u7684\u8bbe\u5907ID&#xff0c;<br \/>\n    \/\/ \u5c06\u8bfb\u53d6\u5230\u7684\u8bbe\u5907ID\u5b58\u50a8\u5728device_id_soft\u53d8\u91cf\u4e2d<br \/>\n    uint16_t device_id_soft &#061; W25Q128_ReadID();<\/p>\n<p>    \/\/ \u4f7f\u7528\u786c\u4ef6SPI\u7684\u65b9\u5f0f\u8bfb\u53d6W25Q128\u7684\u8bbe\u5907ID&#xff0c;<br \/>\n    \/\/ \u5c06\u8bfb\u53d6\u5230\u7684\u8bbe\u5907ID\u5b58\u50a8\u5728device_id_hard\u53d8\u91cf\u4e2d<br \/>\n    uint16_t device_id_hard &#061; W25Q128_ReadID_HardwareSPI();<\/p>\n<p>    \/\/ \u5b9a\u4e49\u8981\u5199\u5165W25Q128\u7684\u6570\u636e\u6570\u7ec4&#xff0c;\u5305\u542b4\u4e2a\u5b57\u8282\u7684\u6570\u636e<br \/>\n    uint8_t write_data[] &#061; {0x01, 0x02, 0x03, 0x04};<\/p>\n<p>    \/\/ \u5b9a\u4e49\u4e00\u4e2a\u6570\u7ec4\u7528\u4e8e\u5b58\u50a8\u4eceW25Q128\u8bfb\u53d6\u7684\u6570\u636e&#xff0c;\u5927\u5c0f\u4e3a4\u4e2a\u5b57\u8282<br \/>\n    uint8_t read_data[4];<\/p>\n<p>    \/\/ \u4f7f\u7528\u8f6f\u4ef6\u6a21\u62dfSPI\u7684\u65b9\u5f0f\u64e6\u9664W25Q128\u76840x000000\u6247\u533a&#xff0c;<br \/>\n    \/\/ \u64e6\u9664\u64cd\u4f5c\u4f1a\u5c06\u8be5\u6247\u533a\u7684\u6570\u636e\u5168\u90e8\u7f6e\u4e3a0xFF<br \/>\n    W25Q128_SectorErase(0x000000);<\/p>\n<p>    \/\/ \u4f7f\u7528\u8f6f\u4ef6\u6a21\u62dfSPI\u7684\u65b9\u5f0f\u5c06write_data\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u5199\u5165W25Q128\u76840x000000\u5730\u5740&#xff0c;<br \/>\n    \/\/ \u5199\u5165\u6570\u636e\u957f\u5ea6\u4e3a4\u4e2a\u5b57\u8282<br \/>\n    W25Q128_PageProgram(0x000000, write_data, 4);<\/p>\n<p>    \/\/ \u4f7f\u7528\u8f6f\u4ef6\u6a21\u62dfSPI\u7684\u65b9\u5f0f\u4eceW25Q128\u76840x000000\u5730\u5740\u8bfb\u53d64\u4e2a\u5b57\u8282\u7684\u6570\u636e\u5230read_data\u6570\u7ec4\u4e2d<br \/>\n    W25Q128_ReadData(0x000000, read_data, 4);<\/p>\n<p>    \/\/ \u4f7f\u7528\u786c\u4ef6SPI\u7684\u65b9\u5f0f\u64e6\u9664W25Q128\u76840x000000\u6247\u533a<br \/>\n    W25Q128_SectorErase_HardwareSPI(0x000000);<\/p>\n<p>    \/\/ \u4f7f\u7528\u786c\u4ef6SPI\u7684\u65b9\u5f0f\u5c06write_data\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u5199\u5165W25Q128\u76840x000000\u5730\u5740&#xff0c;<br \/>\n    \/\/ \u5199\u5165\u6570\u636e\u957f\u5ea6\u4e3a4\u4e2a\u5b57\u8282<br \/>\n    W25Q128_PageProgram_HardwareSPI(0x000000, write_data, 4);<\/p>\n<p>    \/\/ \u4f7f\u7528\u786c\u4ef6SPI\u7684\u65b9\u5f0f\u4eceW25Q128\u76840x000000\u5730\u5740\u8bfb\u53d64\u4e2a\u5b57\u8282\u7684\u6570\u636e\u5230read_data\u6570\u7ec4\u4e2d<br \/>\n    W25Q128_ReadData_HardwareSPI(0x000000, read_data, 4);<\/p>\n<p>    \/\/ \u8fdb\u5165\u4e00\u4e2a\u65e0\u9650\u5faa\u73af&#xff0c;\u7a0b\u5e8f\u4f1a\u4e00\u76f4\u505c\u7559\u5728\u8fd9\u4e2a\u5faa\u73af\u4e2d&#xff0c;<br \/>\n    \/\/ \u53ef\u4ee5\u5728\u6b64\u5904\u6dfb\u52a0\u5176\u4ed6\u9700\u8981\u6301\u7eed\u8fd0\u884c\u7684\u4ee3\u7801\u903b\u8f91<br \/>\n    while (1) {<br \/>\n    }<br \/>\n} <\/p>\n<h4>6. \u603b\u7ed3<\/h4>\n<p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u57fa\u4e8e STM32F407 HAL \u5e93\u5b9e\u73b0\u8f6f\u4ef6\u6a21\u62df SPI \u548c\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u65b9\u6cd5\u3002\u8f6f\u4ef6\u6a21\u62df SPI \u5177\u6709\u7075\u6d3b\u6027\u9ad8\u3001\u65e0\u9700\u7279\u5b9a\u786c\u4ef6\u652f\u6301\u7684\u4f18\u70b9&#xff0c;\u4f46\u901a\u4fe1\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162&#xff1b;\u786c\u4ef6 SPI \u5219\u5177\u6709\u901a\u4fe1\u901f\u5ea6\u5feb\u3001\u7a33\u5b9a\u6027\u9ad8\u7684\u7279\u70b9&#xff0c;\u4f46\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d&#xff0c;\u53ef\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684 SPI \u901a\u4fe1\u65b9\u5f0f\u3002<\/p>\n<h4>7. \u6ce8\u610f\u4e8b\u9879<\/h4>\n<ul>\n<li>\u5728\u4f7f\u7528\u8f6f\u4ef6\u6a21\u62df SPI \u65f6&#xff0c;\u8981\u6ce8\u610f\u65f6\u949f\u4fe1\u53f7\u7684\u4ea7\u751f\u548c\u6570\u636e\u7684\u53d1\u9001\u4e0e\u63a5\u6536\u987a\u5e8f&#xff0c;\u786e\u4fdd\u901a\u4fe1\u7684\u6b63\u786e\u6027\u3002<\/li>\n<li>\u5728\u4f7f\u7528\u786c\u4ef6 SPI \u65f6&#xff0c;\u8981\u6b63\u786e\u914d\u7f6e SPI \u5916\u8bbe\u7684\u53c2\u6570&#xff0c;\u5982\u65f6\u949f\u6781\u6027\u3001\u65f6\u949f\u76f8\u4f4d\u3001\u6570\u636e\u4f4d\u5bbd\u7b49\u3002<\/li>\n<li>\u5728\u5bf9 W25Q128 \u8fdb\u884c\u5199\u64cd\u4f5c\u4e4b\u524d&#xff0c;\u9700\u8981\u5148\u8fdb\u884c\u5199\u4f7f\u80fd\u64cd\u4f5c&#xff0c;\u5e76\u4e14\u5728\u64e6\u9664\u6247\u533a\u548c\u5199\u5165\u6570\u636e\u65f6\u8981\u6ce8\u610f\u5730\u5740\u7684\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5185\u5bb9\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u57fa\u4e8e STM32F407 HAL \u5e93\u8f6f\u4ef6\u6a21\u62df SPI \u8bfb\u5199 W25Q128 \u4e0e\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u5b9e\u73b0\u65b9\u6cd5&#xff0c;\u4f60\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8c03\u6574\u548c\u6269\u5c55&#xff0c;\u6709\u4ec0\u4e48\u4e0d\u61c2\u7684\u53ef\u4ee5\u7559\u8a00\u79c1\u4fe1\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf9\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u57fa\u4e8e STM32F407 HAL \u5e93\u5b9e\u73b0\u8f6f\u4ef6\u6a21\u62df SPI \u548c\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u65b9\u6cd5\u3002\u8f6f\u4ef6\u6a21\u62df SPI \u5177\u6709\u7075\u6d3b\u6027\u9ad8\u3001\u65e0\u9700\u7279\u5b9a\u786c\u4ef6\u652f\u6301\u7684\u4f18\u70b9\uff0c\u4f46\u901a\u4fe1\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162\uff1b\u786c\u4ef6 SPI \u5219\u5177\u6709\u901a\u4fe1\u901f\u5ea6\u5feb\u3001\u7a33\u5b9a\u6027\u9ad8\u7684\u7279\u70b9\uff0c\u4f46\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684 SPI \u901a\u4fe1\u65b9\u5f0f\u3002_stm32 hal spi<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[217,2890,270,269],"topic":[],"class_list":["post-43176","post","type-post","status-publish","format-standard","hentry","category-server","tag-stm32","tag-stm32-hal","tag-270","tag-269"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/43176.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf9\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u57fa\u4e8e STM32F407 HAL \u5e93\u5b9e\u73b0\u8f6f\u4ef6\u6a21\u62df SPI \u548c\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u65b9\u6cd5\u3002\u8f6f\u4ef6\u6a21\u62df SPI \u5177\u6709\u7075\u6d3b\u6027\u9ad8\u3001\u65e0\u9700\u7279\u5b9a\u786c\u4ef6\u652f\u6301\u7684\u4f18\u70b9\uff0c\u4f46\u901a\u4fe1\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162\uff1b\u786c\u4ef6 SPI \u5219\u5177\u6709\u901a\u4fe1\u901f\u5ea6\u5feb\u3001\u7a33\u5b9a\u6027\u9ad8\u7684\u7279\u70b9\uff0c\u4f46\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684 SPI \u901a\u4fe1\u65b9\u5f0f\u3002_stm32 hal spi\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/43176.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-11T00:56:02+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43176.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/43176.html\",\"name\":\"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-11T00:56:02+00:00\",\"dateModified\":\"2025-06-11T00:56:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/43176.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/43176.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/43176.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/43176.html","og_locale":"zh_CN","og_type":"article","og_title":"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.9k\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf9\u6b21\u3002\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u57fa\u4e8e STM32F407 HAL \u5e93\u5b9e\u73b0\u8f6f\u4ef6\u6a21\u62df SPI \u548c\u786c\u4ef6 SPI \u8bfb\u5199 W25Q128 \u7684\u65b9\u6cd5\u3002\u8f6f\u4ef6\u6a21\u62df SPI \u5177\u6709\u7075\u6d3b\u6027\u9ad8\u3001\u65e0\u9700\u7279\u5b9a\u786c\u4ef6\u652f\u6301\u7684\u4f18\u70b9\uff0c\u4f46\u901a\u4fe1\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162\uff1b\u786c\u4ef6 SPI \u5219\u5177\u6709\u901a\u4fe1\u901f\u5ea6\u5feb\u3001\u7a33\u5b9a\u6027\u9ad8\u7684\u7279\u70b9\uff0c\u4f46\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u786c\u4ef6\u8d44\u6e90\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684 SPI \u901a\u4fe1\u65b9\u5f0f\u3002_stm32 hal spi","og_url":"https:\/\/www.wsisp.com\/helps\/43176.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-11T00:56:02+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/43176.html","url":"https:\/\/www.wsisp.com\/helps\/43176.html","name":"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-11T00:56:02+00:00","dateModified":"2025-06-11T00:56:02+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/43176.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/43176.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/43176.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"STM32 HAL\u5e93SPI\u8bfb\u5199W25Q128\uff08\u8f6f\u4ef6\u6a21\u62df+\u786c\u4ef6spi)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=43176"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/43176\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=43176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=43176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=43176"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=43176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}