微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32f407硬件spi读写sst25vf闪存flash代码

stm32f407硬件spi读写sst25vf闪存flash代码

时间: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. 函数功能 : 写一个字节数据
  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. /*--------------------

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top