spi.c 9.0 KB

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