您的位置 首页 系统

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

voidSPI_FLASH_Init(void){GPIO_InitTypeDefGPIO_InitStructure;SPI_InitTypeDefSPI_InitStructure;NVIC_In

  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. /*——————————————————————————
  90. 函数称号 : SPI_FLASH_Writenbyte
  91. 函数功用 : 写一个字节数据
  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. 函数功用 : 写一个字节数据
  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. }

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/xitong/260441.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部