<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > stm32f407硬件spi讀寫sst25vf閃存flash代碼

          stm32f407硬件spi讀寫sst25vf閃存flash代碼

          作者: 時間:2016-11-24 來源:網絡 收藏
          1. void SPI_FLASH_Init(void)
          2. {
          3. GPIO_InitTypeDef GPIO_InitStructure;
          4. SPI_InitTypeDef SPI_InitStructure;
          5. NVIC_InitTypeDef NVIC_InitStructure;
          6. /* Enable the SPI periph */
          7. RCC_APB2PeriphClockCmd(FLASH_SPI_CLK, ENABLE);
          8. /* Enable SCK, MOSI and MISO GPIO clocks */
          9. RCC_AHB1PeriphClockCmd(FLASH_SPI_SCK_GPIO_CLK | FLASH_SPI_MOSI_GPIO_CLK | FLASH_SPI_MISO_GPIO_CLK, ENABLE);//FLASH_SPI_MISO_GPIO_CLK
          10. /* Enable CS GPIO clock */
          11. RCC_AHB1PeriphClockCmd(FLASH_SPI_CS_GPIO_CLK, ENABLE);
          12. /* Enable 157 GPIO clock */
          13. RCC_AHB1PeriphClockCmd(FLASH_SPI_157_GPIO_CLK, ENABLE);
          14. GPIO_PinAFConfig(FLASH_SPI_SCK_GPIO_PORT, FLASH_SPI_SCK_SOURCE, FLASH_SPI_SCK_AF);
          15. GPIO_PinAFConfig(FLASH_SPI_MISO_GPIO_PORT, FLASH_SPI_MISO_SOURCE, FLASH_SPI_MISO_AF);
          16. GPIO_PinAFConfig(FLASH_SPI_MOSI_GPIO_PORT, FLASH_SPI_MOSI_SOURCE, FLASH_SPI_MOSI_AF);
          17. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
          18. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          19. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
          20. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          21. /* SPI SCK pin configuration */
          22. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
          23. GPIO_Init(FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
          24. /* SPI MOSI pin configuration */
          25. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
          26. GPIO_Init(FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
          27. /* SPI MISO pin configuration */
          28. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
          29. GPIO_Init(FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
          30. /* Configure GPIO PIN for Lis Chip select */
          31. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
          32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
          33. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          34. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          35. GPIO_Init(FLASH_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
          36. SPI_FLASH_CS_HIGH();
          37. /* Configure GPIO PIN for 157 */
          38. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_157_PIN;
          39. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
          40. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
          41. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          42. GPIO_Init(FLASH_SPI_157_GPIO_PORT, &GPIO_InitStructure);
          43. /* SPI1 configuration */
          44. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
          45. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
          46. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
          47. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
          48. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
          49. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
          50. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
          51. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
          52. SPI_InitStructure.SPI_CRCPolynomial = 7;
          53. SPI_Init(FLASH_SPI, &SPI_InitStructure);
          54. /* Enable SPI1 */
          55. SPI_Cmd(FLASH_SPI, ENABLE);
          56. SPI_FLASH_157_DIS();
          57. //SPI_DMAInit();
          58. /* I2S DMA IRQ Channel configuration */
          59. /* NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_RX_IRQn;
          60. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
          61. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
          62. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
          63. NVIC_Init(&NVIC_InitStructure);
          64. NVIC_InitStructure.NVIC_IRQChannel = SPI1_DMA_TX_IRQn;
          65. NVIC_Init(&NVIC_InitStructure);
          66. Delay_Init(168);*/
          67. }
          68. /*------------------------------------------------------------------------------
          69. 函數名稱 : SPI_FLASH_Writebyte
          70. 函數功能 : 寫一個字節(jié)數據
          71. 入口參數 : address:地址 buffer:數據
          72. 出口參數 : 無
          73. ------------------------------------------------------------------------------*/
          74. void SPI_FLASH_Writebyte(unsigned int Addr, unsigned char *buf)
          75. {
          76. unsigned short i = 0, j = 0;
          77. SPI_FLASH_WriteEnable();
          78. SPI_FLASH_CS_LOW();
          79. SPI_FLASH_SendByte(0x02);
          80. SPI_FLASH_SendByte((Addr)>>16);
          81. SPI_FLASH_SendByte((Addr)>>8);
          82. SPI_FLASH_SendByte((Addr));
          83. SPI_FLASH_SendByte(*buf);
          84. SPI_FLASH_CS_HIGH();
          85. SPI_FLASH_WaitForWriteEnd();
          86. SPI_FLASH_WriteDisable();
          87. delay_nus(20);
          88. }
          89. /*------------------------------------------------------------------------------
          90. 函數名稱 : SPI_FLASH_Writenbyte
          91. 函數功能 : 寫一個字節(jié)數據
          92. 入口參數 : address:地址 buffer:數據 num :數量
          93. 出口參數 : 無
          94. ------------------------------------------------------------------------------*/
          95. void SPI_FLASH_Writenbyte(unsigned int Addr, unsigned char *buf,u16 num)
          96. {
          97. u8 i;
          98. for(i=0;i
          99. {
          100. SPI_FLASH_Writebyte(Addr++,&buf[i]);
          101. }
          102. }
          103. /*------------------------------------------------------------------------------
          104. 函數名稱 : SPI_FLASH_Readnbyte
          105. 函數功能 : 寫一個字節(jié)數據
          106. 入口參數 : address:地址 buffer:數據 num :數量
          107. 出口參數 : 無
          108. ------------------------------------------------------------------------------*/
          109. void SPI_FLASH_Readnbyte(unsigned int Addr,unsigned char *buf,u16 num)
          110. {
          111. unsigned short i = 0;
          112. SPI_FLASH_CS_LOW();
          113. SPI_FLASH_SendByte(0x0b);
          114. SPI_FLASH_SendByte(Addr>>16);
          115. SPI_FLASH_SendByte(Addr>>8);
          116. SPI_FLASH_SendByte(Addr);
          117. //dummy
          118. SPI_FLASH_SendByte(Dummy_Byte);
          119. for(i=0;i
          120. {
          121. buf[i]= SPI_FLASH_SendByte(Dummy_Byte);
          122. }
          123. SPI_FLASH_CS_HIGH();
          124. }
          125. main()
          126. {
          127. u8 bufda[25]="123456789abcdefghijkl";
          128. SPI_FLASH_Init();
          129. // Write_74HC595(HC595_OUT,3,&davalu);
          130. // Write_595_ENABLE();
          131. SPI_FLASH_157_EN();//kang 2014.07.10
          132. SPI_FLASH_ChipErase();
          133. SPI_FLASH_157_DIS();
          134. SPI_FLASH_157_EN(); //kang 2014.07.10
          135. i= SPI_FLASH_ReadID();
          136. SPI_FLASH_157_DIS();
          137. SPI_FLASH_157_EN();
          138. SPI_FLASH_Writenbyte(5,bufda,20);
          139. SPI_FLASH_157_DIS();
          140. SPI_FLASH_157_EN();
          141. SPI_FLASH_Readnbyte(5,readbuf,20);
          142. SPI_FLASH_157_DIS();
          143. }


          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();