spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. ******************************************************************************
  3. * File Name : SPI.c
  4. * Description : This file provides code for the configuration
  5. * of the SPI instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "spi.h"
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. SPI_HandleTypeDef hspi4;
  54. /* SPI4 init function */
  55. void MX_SPI4_Init(void)
  56. {
  57. hspi4.Instance = SPI4;
  58. hspi4.Init.Mode = SPI_MODE_MASTER;//主模式
  59. hspi4.Init.Direction = SPI_DIRECTION_2LINES;//全双工
  60. hspi4.Init.DataSize = SPI_DATASIZE_8BIT;//数据位为8位
  61. hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;//CPOL=0,low
  62. hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;//CPHA为数据线的第一个变化沿
  63. hspi4.Init.NSS = SPI_NSS_SOFT;//软件控制NSS
  64. hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;//256分频,54M/256=Hz
  65. hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;//最高位先发送
  66. hspi4.Init.TIMode = SPI_TIMODE_DISABLE;//TIMODE模式关闭
  67. hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;//CRC关闭
  68. hspi4.Init.CRCPolynomial = 7;//CRC值计算的多项式
  69. hspi4.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  70. hspi4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  71. if (HAL_SPI_Init(&hspi4) != HAL_OK)
  72. {
  73. Error_Handler();
  74. }
  75. }
  76. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  77. {
  78. GPIO_InitTypeDef GPIO_InitStruct = {0};
  79. if(spiHandle->Instance==SPI2)
  80. {
  81. /* USER CODE BEGIN SPI2_MspInit 0 */
  82. __HAL_RCC_SPI2_CLK_ENABLE();//使能SPI4时钟
  83. #if 0
  84. extern void HAL_SPI_MspInit_flash(void);
  85. HAL_SPI_MspInit_flash();
  86. #elif 1
  87. extern void HAL_SPI_MspInit_w25qxx(SPI_HandleTypeDef *hspi);
  88. HAL_SPI_MspInit_w25qxx(spiHandle);
  89. #else
  90. extern void HAL_SPI_MspInit_SDCARD(SPI_HandleTypeDef *hspi);
  91. HAL_SPI_MspInit_SDCARD(spiHandle);
  92. #endif
  93. /* USER CODE BEGIN SPI2_MspInit 1 */
  94. /* USER CODE END SPI2_MspInit 1 */
  95. }
  96. if(spiHandle->Instance==SPI4)
  97. {
  98. /* USER CODE BEGIN SPI4_MspInit 0 */
  99. /* USER CODE END SPI4_MspInit 0 */
  100. /* SPI4 clock enable */
  101. __HAL_RCC_SPI4_CLK_ENABLE();//使能SPI4时钟
  102. __HAL_RCC_GPIOE_CLK_ENABLE();
  103. /**SPI4 GPIO Configuration
  104. PE12 ------> SPI4_SCK
  105. PE13 ------> SPI4_MISO
  106. PE14 ------> SPI4_MOSI
  107. */
  108. GPIO_InitStruct.Pin = SPI4_FM25_SCK_Pin|SPI4_FM25_MISO_Pin|SPI4_FM25_MOSI_Pin;
  109. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用推挽输出
  110. GPIO_InitStruct.Pull = GPIO_PULLUP;//上拉
  111. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;//快速
  112. GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
  113. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);//配置SPI的数据线和时钟线
  114. /* USER CODE BEGIN SPI4_MspInit 1 */
  115. /* USER CODE END SPI4_MspInit 1 */
  116. }
  117. }
  118. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
  119. {
  120. if(spiHandle->Instance==SPI2)
  121. {
  122. /* USER CODE BEGIN SPI2_MspDeInit 0 */
  123. /* USER CODE END SPI2_MspDeInit 0 */
  124. /* Peripheral clock disable */
  125. __HAL_RCC_SPI2_CLK_DISABLE();
  126. /**SPI2 GPIO Configuration
  127. PB12 ------> SPI2_NSS
  128. PB13 ------> SPI2_SCK
  129. PB14 ------> SPI2_MISO
  130. PB15 ------> SPI2_MOSI
  131. */
  132. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
  133. /* SPI2 interrupt DeInit */
  134. HAL_NVIC_DisableIRQ(SPI2_IRQn);
  135. /* USER CODE BEGIN SPI2_MspDeInit 1 */
  136. /* USER CODE END SPI2_MspDeInit 1 */
  137. }
  138. if(spiHandle->Instance==SPI4)
  139. {
  140. /* USER CODE BEGIN SPI4_MspDeInit 0 */
  141. /* USER CODE END SPI4_MspDeInit 0 */
  142. /* Peripheral clock disable */
  143. __HAL_RCC_SPI4_CLK_DISABLE();
  144. /**SPI4 GPIO Configuration
  145. PE12 ------> SPI4_SCK
  146. PE13 ------> SPI4_MISO
  147. PE14 ------> SPI4_MOSI
  148. */
  149. HAL_GPIO_DeInit(GPIOE, SPI4_FM25_SCK_Pin|SPI4_FM25_MISO_Pin|SPI4_FM25_MOSI_Pin);
  150. /* USER CODE BEGIN SPI4_MspDeInit 1 */
  151. /* USER CODE END SPI4_MspDeInit 1 */
  152. }
  153. }
  154. /* USER CODE BEGIN 1 */
  155. //SPI4 读写一个字节
  156. //TxData:要写入的字节
  157. //返回值:读取到的字节
  158. uint8_t SPI4_ReadWriteByte(uint8_t TxData)
  159. {
  160. uint8_t Rxdata;
  161. HAL_SPI_TransmitReceive(&hspi4,&TxData,&Rxdata,1, 1000);
  162. return Rxdata; //返回收到的数据
  163. }
  164. void FM25L16B_Init(void)
  165. {
  166. FM_CS_1;
  167. MX_SPI4_Init();
  168. }
  169. /****************************************************
  170. ****函数名称:void FM25L16B_WriteByte(uint16_t addr,uint8_t data)
  171. ****函数作用:向指定地址写入一个字节数据
  172. ****输入参数:addr,指定地址 data 要写入的数据
  173. ****输出参数:
  174. ****************************************************/
  175. void FM25L16B_WriteByte(uint16_t addr,uint8_t data)
  176. {
  177. uint8_t addr_h,addr_l;
  178. addr_h=addr>>8;
  179. addr_l=addr&0xff;
  180. FM_CS_0;
  181. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  182. FM_CS_1;
  183. FM_CS_0;
  184. SPI4_ReadWriteByte(FM_WRITE); //写数据命令
  185. SPI4_ReadWriteByte(addr_h); //起始地址高8位
  186. SPI4_ReadWriteByte(addr_l); //起始地址低8位
  187. SPI4_ReadWriteByte(data);
  188. FM_CS_1;
  189. }
  190. /****************************************************
  191. ****函数名称:uint8_t FM25L16B_ReadByte(uint16_t addr)
  192. ****函数作用:读取指定地址的一个字节数据
  193. ****输入参数:addr 要读取的地址
  194. ****输出返回:当前地址数据值
  195. ****添加说明:没写入的地址数据默认是0x00.
  196. ****************************************************/
  197. uint8_t FM25L16B_ReadByte(uint16_t addr)
  198. {
  199. uint8_t addr_h,addr_l;
  200. uint8_t data=0;
  201. addr_h=addr>>8;
  202. addr_l=addr&0xff;
  203. FM_CS_0;
  204. SPI4_ReadWriteByte(FM_READ); //读数据命令
  205. SPI4_ReadWriteByte(addr_h); //起始地址高8位
  206. SPI4_ReadWriteByte(addr_l); //起始地址低8位
  207. data=SPI4_ReadWriteByte(0xff);
  208. FM_CS_1;
  209. return data;
  210. }
  211. /****************************************************
  212. ****函数名称:uint8_t FM25L16B_ReadStatus(void)
  213. ****函数作用:读取状态寄存器
  214. ****输入参数:
  215. ****输出参数:状态寄存器值
  216. ****************************************************/
  217. uint8_t FM25L16B_ReadStatus(void)
  218. {
  219. uint8_t data=0;
  220. FM_CS_0;
  221. SPI4_ReadWriteByte(FM_RDSR); //读状态寄存器命令
  222. data=SPI4_ReadWriteByte(0xff);
  223. FM_CS_1;
  224. return data;
  225. }
  226. /****************************************************
  227. ****函数名称:void FM25L16B_WriteStatus(uint8_t data)
  228. ****函数作用:写状态寄存器
  229. ****输入参数:要写入的数据
  230. ****输出参数:
  231. ****************************************************/
  232. void FM25L16B_WriteStatus(uint8_t data)
  233. {
  234. FM_CS_0;
  235. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  236. FM_CS_1;
  237. FM_CS_0;
  238. SPI4_ReadWriteByte(FM_WRSR); //写状态寄存器命令
  239. SPI4_ReadWriteByte(data); //起始地址高8位
  240. FM_CS_1;
  241. }
  242. /****************************************************
  243. ****函数名称:void FM25L16B_Write_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  244. ****函数作用:写数据到指定地址
  245. ****输入参数:addr 起始地址;data 要写入的数据;num 数据长度
  246. ****输出参数:
  247. ****************************************************/
  248. void FM25L16B_Write_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  249. {
  250. uint16_t i=0;
  251. FM_CS_0;
  252. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  253. FM_CS_1;
  254. FM_CS_0;
  255. SPI4_ReadWriteByte(FM_WRITE); //写数据命令
  256. SPI4_ReadWriteByte(addr>>8); //起始地址高8位
  257. SPI4_ReadWriteByte(addr&0xff); //起始地址低8位
  258. for(i=0;i<num;i++)
  259. {
  260. SPI4_ReadWriteByte(data[i]);
  261. }
  262. FM_CS_1;
  263. }
  264. /****************************************************
  265. ****函数名称:void FM25L16B_Read_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  266. ****函数作用:从指定地址读取数据
  267. ****输入参数:addr 起始地址;data,数据缓存区; num 数据长度
  268. ****输出参数:
  269. ****************************************************/
  270. void FM25L16B_Read_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  271. {
  272. uint16_t i=0;
  273. FM_CS_0;
  274. SPI4_ReadWriteByte(FM_READ); //读数据命令
  275. SPI4_ReadWriteByte(addr>>8); //起始地址高8位
  276. SPI4_ReadWriteByte(addr&0xff); //起始地址低8位
  277. for(i=0;i<num;i++)
  278. {
  279. data[i]=SPI4_ReadWriteByte(0xff);
  280. }
  281. FM_CS_1;
  282. }
  283. /* USER CODE END 1 */
  284. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/