spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /* USER CODE END SPI2_MspInit 0 */
  83. /* Peripheral clock enable */
  84. __HAL_RCC_SPI2_CLK_ENABLE();
  85. __HAL_RCC_GPIOB_CLK_ENABLE();
  86. /**SPI2 GPIO Configuration
  87. PB12 ------> SPI2_NSS
  88. PB13 ------> SPI2_SCK
  89. PB14 ------> SPI2_MISO
  90. PB15 ------> SPI2_MOSI
  91. */
  92. GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  93. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  94. GPIO_InitStruct.Pull = GPIO_NOPULL;
  95. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  96. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  97. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  98. /* SPI1 interrupt Init */
  99. HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);
  100. HAL_NVIC_EnableIRQ(SPI2_IRQn);
  101. /* USER CODE BEGIN SPI2_MspInit 1 */
  102. /* USER CODE END SPI2_MspInit 1 */
  103. }
  104. if(spiHandle->Instance==SPI4)
  105. {
  106. /* USER CODE BEGIN SPI4_MspInit 0 */
  107. /* USER CODE END SPI4_MspInit 0 */
  108. /* SPI4 clock enable */
  109. __HAL_RCC_SPI4_CLK_ENABLE();//使能SPI4时钟
  110. __HAL_RCC_GPIOE_CLK_ENABLE();
  111. /**SPI4 GPIO Configuration
  112. PE12 ------> SPI4_SCK
  113. PE13 ------> SPI4_MISO
  114. PE14 ------> SPI4_MOSI
  115. */
  116. GPIO_InitStruct.Pin = SPI4_FM25_SCK_Pin|SPI4_FM25_MISO_Pin|SPI4_FM25_MOSI_Pin;
  117. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;//复用推挽输出
  118. GPIO_InitStruct.Pull = GPIO_PULLUP;//上拉
  119. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;//快速
  120. GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
  121. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);//配置SPI的数据线和时钟线
  122. /* USER CODE BEGIN SPI4_MspInit 1 */
  123. /* USER CODE END SPI4_MspInit 1 */
  124. }
  125. }
  126. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
  127. {
  128. if(spiHandle->Instance==SPI2)
  129. {
  130. /* USER CODE BEGIN SPI2_MspDeInit 0 */
  131. /* USER CODE END SPI2_MspDeInit 0 */
  132. /* Peripheral clock disable */
  133. __HAL_RCC_SPI2_CLK_DISABLE();
  134. /**SPI2 GPIO Configuration
  135. PB12 ------> SPI2_NSS
  136. PB13 ------> SPI2_SCK
  137. PB14 ------> SPI2_MISO
  138. PB15 ------> SPI2_MOSI
  139. */
  140. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
  141. /* SPI2 interrupt DeInit */
  142. HAL_NVIC_DisableIRQ(SPI2_IRQn);
  143. /* USER CODE BEGIN SPI2_MspDeInit 1 */
  144. /* USER CODE END SPI2_MspDeInit 1 */
  145. }
  146. if(spiHandle->Instance==SPI4)
  147. {
  148. /* USER CODE BEGIN SPI4_MspDeInit 0 */
  149. /* USER CODE END SPI4_MspDeInit 0 */
  150. /* Peripheral clock disable */
  151. __HAL_RCC_SPI4_CLK_DISABLE();
  152. /**SPI4 GPIO Configuration
  153. PE12 ------> SPI4_SCK
  154. PE13 ------> SPI4_MISO
  155. PE14 ------> SPI4_MOSI
  156. */
  157. HAL_GPIO_DeInit(GPIOE, SPI4_FM25_SCK_Pin|SPI4_FM25_MISO_Pin|SPI4_FM25_MOSI_Pin);
  158. /* USER CODE BEGIN SPI4_MspDeInit 1 */
  159. /* USER CODE END SPI4_MspDeInit 1 */
  160. }
  161. }
  162. /* USER CODE BEGIN 1 */
  163. //SPI4 读写一个字节
  164. //TxData:要写入的字节
  165. //返回值:读取到的字节
  166. uint8_t SPI4_ReadWriteByte(uint8_t TxData)
  167. {
  168. uint8_t Rxdata;
  169. HAL_SPI_TransmitReceive(&hspi4,&TxData,&Rxdata,1, 1000);
  170. return Rxdata; //返回收到的数据
  171. }
  172. void FM25L16B_Init(void)
  173. {
  174. FM_CS_1;
  175. MX_SPI4_Init();
  176. }
  177. /****************************************************
  178. ****函数名称:void FM25L16B_WriteByte(uint16_t addr,uint8_t data)
  179. ****函数作用:向指定地址写入一个字节数据
  180. ****输入参数:addr,指定地址 data 要写入的数据
  181. ****输出参数:
  182. ****************************************************/
  183. void FM25L16B_WriteByte(uint16_t addr,uint8_t data)
  184. {
  185. uint8_t addr_h,addr_l;
  186. addr_h=addr>>8;
  187. addr_l=addr&0xff;
  188. FM_CS_0;
  189. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  190. FM_CS_1;
  191. FM_CS_0;
  192. SPI4_ReadWriteByte(FM_WRITE); //写数据命令
  193. SPI4_ReadWriteByte(addr_h); //起始地址高8位
  194. SPI4_ReadWriteByte(addr_l); //起始地址低8位
  195. SPI4_ReadWriteByte(data);
  196. FM_CS_1;
  197. }
  198. /****************************************************
  199. ****函数名称:uint8_t FM25L16B_ReadByte(uint16_t addr)
  200. ****函数作用:读取指定地址的一个字节数据
  201. ****输入参数:addr 要读取的地址
  202. ****输出返回:当前地址数据值
  203. ****添加说明:没写入的地址数据默认是0x00.
  204. ****************************************************/
  205. uint8_t FM25L16B_ReadByte(uint16_t addr)
  206. {
  207. uint8_t addr_h,addr_l;
  208. uint8_t data=0;
  209. addr_h=addr>>8;
  210. addr_l=addr&0xff;
  211. FM_CS_0;
  212. SPI4_ReadWriteByte(FM_READ); //读数据命令
  213. SPI4_ReadWriteByte(addr_h); //起始地址高8位
  214. SPI4_ReadWriteByte(addr_l); //起始地址低8位
  215. data=SPI4_ReadWriteByte(0xff);
  216. FM_CS_1;
  217. return data;
  218. }
  219. /****************************************************
  220. ****函数名称:uint8_t FM25L16B_ReadStatus(void)
  221. ****函数作用:读取状态寄存器
  222. ****输入参数:
  223. ****输出参数:状态寄存器值
  224. ****************************************************/
  225. uint8_t FM25L16B_ReadStatus(void)
  226. {
  227. uint8_t data=0;
  228. FM_CS_0;
  229. SPI4_ReadWriteByte(FM_RDSR); //读状态寄存器命令
  230. data=SPI4_ReadWriteByte(0xff);
  231. FM_CS_1;
  232. return data;
  233. }
  234. /****************************************************
  235. ****函数名称:void FM25L16B_WriteStatus(uint8_t data)
  236. ****函数作用:写状态寄存器
  237. ****输入参数:要写入的数据
  238. ****输出参数:
  239. ****************************************************/
  240. void FM25L16B_WriteStatus(uint8_t data)
  241. {
  242. FM_CS_0;
  243. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  244. FM_CS_1;
  245. FM_CS_0;
  246. SPI4_ReadWriteByte(FM_WRSR); //写状态寄存器命令
  247. SPI4_ReadWriteByte(data); //起始地址高8位
  248. FM_CS_1;
  249. }
  250. /****************************************************
  251. ****函数名称:void FM25L16B_Write_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  252. ****函数作用:写数据到指定地址
  253. ****输入参数:addr 起始地址;data 要写入的数据;num 数据长度
  254. ****输出参数:
  255. ****************************************************/
  256. void FM25L16B_Write_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  257. {
  258. uint16_t i=0;
  259. FM_CS_0;
  260. SPI4_ReadWriteByte(FM_WREN); //写使能命令
  261. FM_CS_1;
  262. FM_CS_0;
  263. SPI4_ReadWriteByte(FM_WRITE); //写数据命令
  264. SPI4_ReadWriteByte(addr>>8); //起始地址高8位
  265. SPI4_ReadWriteByte(addr&0xff); //起始地址低8位
  266. for(i=0;i<num;i++)
  267. {
  268. SPI4_ReadWriteByte(data[i]);
  269. }
  270. FM_CS_1;
  271. }
  272. /****************************************************
  273. ****函数名称:void FM25L16B_Read_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  274. ****函数作用:从指定地址读取数据
  275. ****输入参数:addr 起始地址;data,数据缓存区; num 数据长度
  276. ****输出参数:
  277. ****************************************************/
  278. void FM25L16B_Read_N_Bytes(uint16_t addr,uint8_t *data,uint16_t num)
  279. {
  280. uint16_t i=0;
  281. FM_CS_0;
  282. SPI4_ReadWriteByte(FM_READ); //读数据命令
  283. SPI4_ReadWriteByte(addr>>8); //起始地址高8位
  284. SPI4_ReadWriteByte(addr&0xff); //起始地址低8位
  285. for(i=0;i<num;i++)
  286. {
  287. data[i]=SPI4_ReadWriteByte(0xff);
  288. }
  289. FM_CS_1;
  290. }
  291. /* USER CODE END 1 */
  292. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/