新聞中心

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

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

作者: 時(shí)間:2016-11-24 來源:網(wǎng)絡(luò) 收藏
  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. 函數(shù)名稱 : SPI_FLASH_Writebyte
  70. 函數(shù)功能 : 寫一個(gè)字節(jié)數(shù)據(jù)
  71. 入口參數(shù) : address:地址 buffer:數(shù)據(jù)
  72. 出口參數(shù) : 無
  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. 函數(shù)名稱 : SPI_FLASH_Writenbyte
  91. 函數(shù)功能 : 寫一個(gè)字節(jié)數(shù)據(jù)
  92. 入口參數(shù) : address:地址 buffer:數(shù)據(jù) num :數(shù)量
  93. 出口參數(shù) : 無
  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. 函數(shù)名稱 : SPI_FLASH_Readnbyte
  105. 函數(shù)功能 : 寫一個(gè)字節(jié)數(shù)據(jù)
  106. 入口參數(shù) : address:地址 buffer:數(shù)據(jù) num :數(shù)量
  107. 出口參數(shù) : 無
  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. }


關(guān)鍵詞: stm32f407spisst25vfflas

評(píng)論


技術(shù)專區(qū)

關(guān)閉