当前位置:   article > 正文

STM32双机SPI中断通信机制_stm32f4spi中断如何

stm32f4spi中断如何

研究STM32F4的SPI有一阵子了,进步很缓慢,前几天终于鼓起勇气,用手抄了一遍官方的代码,然后自己一个字字的敲进去,然后再调试。历时大约一个星期,终于把SPI的中断发送和中断接收给整清楚了。在没有正确运行出结果之前,一切都是辣么痛苦。此文鼓励我继续往前进步。

先说主机端,首先当然是写SPI驱动文件,也就是SPI的配置,具体文件如下:

SPI.c

  1. #include <stm32f4xx.h>
  2. #include "usart.h"
  3. #include "spi.h"
  4. u8 Master_Temp =0;
  5. extern SPI_InitTypeDef SPI_InitStructure;
  6. u8 Slave_Temp=0;
  7. static __IO uint32_t TimingDelay;
  8. void SPI_Config(void)
  9. {
  10. GPIO_InitTypeDef GPIO_InitStructure;
  11. NVIC_InitTypeDef NVIC_InitStructure;
  12. /* Peripheral Clock Enable -------------------------------------------------*/
  13. /* Enable the SPI clock */
  14. SPIx_CLK_INIT(SPIx_CLK, ENABLE);
  15. /* Enable GPIO clocks */
  16. RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
  17. /* SPI GPIO Configuration --------------------------------------------------*/
  18. /* Connect SPI pins to AF5 */
  19. GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
  20. GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
  21. GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
  22. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  23. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  24. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  25. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  26. /* SPI SCK pin configuration */
  27. GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
  28. GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
  29. /* SPI MOSI pin configuration */
  30. GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
  31. GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
  32. /* SPI MISO pin configuration */
  33. GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
  34. GPIO_Init(SPIx_MISO_GPIO_PORT,&GPIO_InitStructure);
  35. /* SPI configuration -------------------------------------------------------*/
  36. SPI_I2S_DeInit(SPIx);
  37. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  38. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  39. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  40. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  41. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  42. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  43. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  44. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  45. SPI_InitStructure.SPI_CRCPolynomial = 7;
  46. /* Configure the Priority Group to 1 bit */
  47. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  48. /* Configure the SPI interrupt priority */
  49. NVIC_InitStructure.NVIC_IRQChannel = SPIx_IRQn;
  50. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  51. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  52. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  53. NVIC_Init(&NVIC_InitStructure);
  54. }
	SPI.H
  1. /**
  2. ******************************************************************************
  3. * @file SPI/SPI_TwoBoards/DataExchangeInterrupt/main.h
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Define to prevent recursive inclusion -------------------------------------*/
  22. #ifndef __SPI_H_
  23. #define __SPI_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm32f4xx.h"
  29. #include "stm324xg_eval.h"
  30. #include "stm324xg_eval_ioe.h"
  31. /* Exported typedef -----------------------------------------------------------*/
  32. #define countof(a) (sizeof(a) / sizeof(*(a)))
  33. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  34. /* Exported define ------------------------------------------------------------*/
  35. /* Uncomment the line below if you will use the SPI peripheral as a Master */
  36. #define SPI_MASTER
  37. /* Uncomment the line below if you will use the SPI peripheral as a Slave */
  38. //#define SPI_SLAVE
  39. /* USER_TIMEOUT value for waiting loops. This timeout is just guarantee that the
  40. application will not remain stuck if the USART communication is corrupted.
  41. You may modify this timeout value depending on CPU frequency and application
  42. conditions (interrupts routines, number of data to transfer, baudrate, CPU
  43. frequency...). */
  44. #define USER_TIMEOUT ((uint32_t)0x64) /* Waiting 1s */
  45. /* SPIx Communication boards Interface */
  46. #define SPIx SPI2
  47. #define SPIx_CLK RCC_APB1Periph_SPI2
  48. #define SPIx_CLK_INIT RCC_APB1PeriphClockCmd
  49. #define SPIx_IRQn SPI2_IRQn
  50. #define SPIx_IRQHANDLER SPI2_IRQHandler
  51. #define SPIx_SCK_PIN GPIO_Pin_1
  52. #define SPIx_SCK_GPIO_PORT GPIOI
  53. #define SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOI
  54. #define SPIx_SCK_SOURCE GPIO_PinSource1
  55. #define SPIx_SCK_AF GPIO_AF_SPI2
  56. #define SPIx_MISO_PIN GPIO_Pin_2
  57. #define SPIx_MISO_GPIO_PORT GPIOI
  58. #define SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOI
  59. #define SPIx_MISO_SOURCE GPIO_PinSource2
  60. #define SPIx_MISO_AF GPIO_AF_SPI2
  61. #define SPIx_MOSI_PIN GPIO_Pin_3
  62. #define SPIx_MOSI_GPIO_PORT GPIOI
  63. #define SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOI
  64. #define SPIx_MOSI_SOURCE GPIO_PinSource3
  65. #define SPIx_MOSI_AF GPIO_AF_SPI2
  66. #define TXBUFFERSIZE (countof(TxBuffer) - 1)
  67. #define RXBUFFERSIZE TXBUFFERSIZE
  68. /* Define numbers of bytes to transmit from TxBuffer */
  69. #define CMD_RIGHT_SIZE 0x01
  70. #define CMD_LEFT_SIZE 0x05
  71. #define CMD_UP_SIZE 0x14
  72. #define CMD_DOWN_SIZE 0x1E
  73. #define CMD_SEL_SIZE TXBUFFERSIZE
  74. /* Exported types ------------------------------------------------------------*/
  75. /* Exported constants --------------------------------------------------------*/
  76. /* Exported macro ------------------------------------------------------------*/
  77. /* Exported functions ------------------------------------------------------- */
  78. #ifdef SPI_MASTER
  79. uint8_t GetVar_NbrOfData(void);
  80. #endif
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. void SPI_Config( void);
  85. void SysTickConfig(void);
  86. void Fill_Buffer(__IO uint8_t *pBuffer, uint16_t BufferLength);
  87. #endif /* __MAIN_H */
  88. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

  stm32f4xx_it.c

  1. /**
  2. ******************************************************************************
  3. * @file stm32fxxx_it.c
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 19-March-2012
  7. * @brief This file includes the interrupt handlers for the application
  8. ******************************************************************************
  9. /* Includes ------------------------------------------------------------------*/
  10. #include "usb_bsp.h"
  11. #include "usb_hcd_int.h"
  12. #include "usbh_core.h"
  13. #include <stdio.h>
  14. #include "stm32fxxx_it.h"
  15. #include "spi.h"
  16. #include "main.h"
  17. extern __IO uint8_t CmdTransmitted;
  18. extern __IO uint8_t Tx_Idx;
  19. extern __IO uint8_t CmdStatus;
  20. /* Private typedef -----------------------------------------------------------*/
  21. /* Private define ------------------------------------------------------------*/
  22. /* Private macro -------------------------------------------------------------*/
  23. /* Private variables ---------------------------------------------------------*/
  24. extern USB_OTG_CORE_HANDLE USB_OTG_Core;
  25. extern USBH_HOST USB_Host;
  26. extern uint8_t TxBuffer[];
  27. extern __IO uint32_t TimeOut;
  28. extern __IO uint8_t Counter;
  29. /* Private function prototypes -----------------------------------------------*/
  30. extern void USB_OTG_BSP_TimerIRQ (void);
  31. void SysTick_Handler(void)
  32. {
  33. /* Decrement the timeout value */
  34. if (TimeOut != 0x0)
  35. {
  36. TimeOut--;
  37. }
  38. if (Counter < 10)
  39. {
  40. Counter++;
  41. }
  42. else
  43. {
  44. Counter = 0x00;
  45. STM_EVAL_LEDToggle(LED1);
  46. }
  47. }
  48. void SPIx_IRQHANDLER(void)
  49. {
  50. #ifdef SPI_MASTER
  51. if(SPI_I2S_GetITStatus(SPIx,SPI_I2S_IT_TXE) == SET)
  52. {
  53. if(CmdStatus == 0x00)
  54. {
  55. SPI_I2S_SendData(SPIx,CmdTransmitted);
  56. CmdStatus = 0x01;
  57. }
  58. else
  59. {
  60. if(Tx_Idx < GetVar_NbrOfData())
  61. {
  62. // printf("send data%c\n",TxBuffer[Tx_Idx]);
  63. SPI_I2S_SendData(SPIx,TxBuffer[Tx_Idx++]);
  64. // printf("%d\n",Tx_Idx);
  65. // Tx_Idx++;
  66. }
  67. else
  68. { printf(" DISABLE IR\n"); SPI_I2S_ITConfig(SPIx,SPI_I2S_IT_TXE,DISABLE); }
  69. }
  70. }
  71. #endif
  72. }
  73. /**
  74. * @brief DebugMon_Handler
  75. * This function handles Debug Monitor exception.
  76. * @param None
  77. * @retval None
  78. */
  79. void DebugMon_Handler(void)
  80. {
  81. }
  82. /**
  83. * @brief PendSV_Handler
  84. * This function handles PendSVC exception.
  85. * @param None
  86. * @retval None
  87. */
  88. void PendSV_Handler(void)
  89. {
  90. }
  91. /**
  92. * @brief EXTI1_IRQHandler
  93. * This function handles External line 1 interrupt request.
  94. * @param None
  95. * @retval None
  96. */
  97. void EXTI1_IRQHandler(void)
  98. {
  99. if(EXTI_GetITStatus(EXTI_Line1) != RESET)
  100. {
  101. USB_Host.usr_cb->OverCurrentDetected();
  102. EXTI_ClearITPendingBit(EXTI_Line1);
  103. }
  104. }
  105. /**
  106. * @brief TIM2_IRQHandler
  107. * This function handles Timer2 Handler.
  108. * @param None
  109. * @retval None
  110. */
  111. void TIM2_IRQHandler(void)
  112. {
  113. USB_OTG_BSP_TimerIRQ();
  114. }
  115. /**
  116. * @brief OTG_FS_IRQHandler
  117. * This function handles USB-On-The-Go FS global interrupt request.
  118. * requests.
  119. * @param None
  120. * @retval None
  121. */
  122. #ifdef USE_USB_OTG_FS
  123. void OTG_FS_IRQHandler(void)
  124. #else
  125. void OTG_HS_IRQHandler(void)
  126. #endif
  127. {
  128. USBH_OTG_ISR_Handler(&USB_OTG_Core);
  129. }
  130. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/


  stm32f4xx_it.h

  1. /**
  2. ******************************************************************************
  3. * @file stm32fxxx_it.h
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 19-March-2012
  7. * @brief This file contains the headers of the interrupt handlers.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __STM32Fxxx_IT_H
  29. #define __STM32Fxxx_IT_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "usb_conf.h"
  35. /* Exported types ------------------------------------------------------------*/
  36. /* Exported constants --------------------------------------------------------*/
  37. /* Exported macro ------------------------------------------------------------*/
  38. /* Exported functions ------------------------------------------------------- */
  39. void NMI_Handler(void);
  40. void HardFault_Handler(void);
  41. void MemManage_Handler(void);
  42. void BusFault_Handler(void);
  43. void UsageFault_Handler(void);
  44. void SVC_Handler(void);
  45. void DebugMon_Handler(void);
  46. void PendSV_Handler(void);
  47. void SysTick_Handler(void);
  48. void SPIx_IRQHANDLER(void);
  49. #ifdef __cplusplus
  50. }
  51. #endif
  52. #endif /* __STM32Fxxx_IT_H */
  53. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

   main.c

  1. /**
  2. ******************************************************************************
  3. * @file main.c
  4. * @author MCD Application Team
  5. * @version V2.1.0
  6. * @date 19-March-2012
  7. * @brief USB host MSC class demo main file
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. /* Includes ------------------------------------------------------------------*/
  30. #include "main.h"
  31. #include "usbh_core.h"
  32. #include "usbh_usr.h"
  33. #include "usbh_msc_core.h"
  34. #include "nvic.h"
  35. #include "usart.h"
  36. #include "spi.h"
  37. #include "dma.h"
  38. #include "KBD.h"
  39. #include "terminal.h"
  40. //DMA_InitTypeDef DMA_InitStructure;
  41. SPI_InitTypeDef SPI_InitStructure;
  42. uint8_t TxBuffer[] ="SPI Interrupt Example: Communication between two SPI using Interrupts";
  43. //__IO uint32_t TimeOut = 0x0;
  44. //#ifdef SPI_SLAVE
  45. // __IO uint8_t uint8_t RxBuffer[RXBUFFERSIZE];
  46. // __IO uint8_t Rx_Idx = 0;
  47. //#endif
  48. #ifdef SPI_MASTER
  49. __IO uint8_t Tx_Idx =0;
  50. __IO uint8_t CmdTransmitted = CMD_NONE;
  51. __IO uint8_t CmdStatus = 0x00;
  52. __IO uint8_t NumberOfByte = 0;
  53. __IO uint32_t TimeOut;
  54. __IO uint8_t Counter;
  55. #endif
  56. //static void SPI_Config(void);
  57. void SysTickConfig(void);
  58. #ifdef SPI_MASTER
  59. static void TimeOut_UserCallback(void);
  60. #endif
  61. //#ifdef SPI_SLAVE
  62. // static TestStatus Buffercmp(uint8_t *pBuffer1,__IO uint8_t *pBuffer2,uint16_t BufferLength);
  63. // static void Fill_Buffer(__IO uint8_t *pBuffe,uint16_t BufferLength);
  64. //#endif
  65. #define Buff_SIZE 32
  66. const uint8_t Buff[Buff_SIZE]= {
  67. 0xA0,0x00,0x00,0xAA,
  68. 0x55,0x55,0x55,0x55,
  69. 0x55,0x00,0x00,0x00,
  70. 0x00,0x00,0x00,0x00,
  71. 0xAA,0xAA,0xAA,0xAA,
  72. 0xAA,0xAA,0xAA,0xAA,
  73. 0xAA,0xAA,0xAA,0xAA,
  74. 0x00,0x00,0xA0,0xAA };
  75. uint32_t DST_Buffer[BUFFER_SIZE];
  76. /** @addtogroup USBH_USER
  77. * @{
  78. */
  79. /** @defgroup USBH_USR_MAIN
  80. * @brief This file is the MSC demo main file
  81. * @{
  82. */
  83. /** @defgroup USBH_USR_MAIN_Private_TypesDefinitions
  84. * @{
  85. */
  86. /**
  87. * @}
  88. */
  89. /** @defgroup USBH_USR_MAIN_Private_Defines
  90. * @{
  91. */
  92. /**
  93. * @}
  94. */
  95. /** @defgroup USBH_USR_MAIN_Private_Macros
  96. * @{
  97. */
  98. /**
  99. * @}
  100. */
  101. /** @defgroup USBH_USR_MAIN_Private_Variables
  102. * @{
  103. */
  104. /* Private function prototypes -----------------------------------------------*/
  105. //#ifdef SPI_MASTER
  106. // static void TimeOut_UserCallback(void);
  107. //#endif
  108. #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
  109. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  110. #pragma data_alignment=4
  111. #endif
  112. #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
  113. __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_Core __ALIGN_END;
  114. #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
  115. #if defined ( __ICCARM__ ) /*!< IAR Compiler */
  116. #pragma data_alignment=4
  117. #endif
  118. #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
  119. __ALIGN_BEGIN USBH_HOST USB_Host __ALIGN_END;
  120. /**
  121. * @}
  122. */
  123. /** @defgroup USBH_USR_MAIN_Private_FunctionPrototypes
  124. * @{
  125. */
  126. /**
  127. * @}
  128. */
  129. /** @defgroup USBH_USR_MAIN_Private_Functions
  130. * @{
  131. */
  132. /**
  133. * @brief Main routine for MSC class application
  134. * @param None
  135. * @retval int
  136. */
  137. int main(void)
  138. {
  139. uint32_t KEY;
  140. /* SPI configuration -------------------------------------------------------*/
  141. KBD_Init();
  142. SPI_Config();
  143. SysTickConfig();
  144. // STM_EVAL_LEDInit(LED1);
  145. // SPI1_DMA_Configuration();
  146. usart3.initialize(9600);
  147. terminal.initialize();
  148. /* Init Host Library */
  149. USBH_Init(&USB_OTG_Core,
  150. #ifdef USE_USB_OTG_FS
  151. USB_OTG_FS_CORE_ID,
  152. #else
  153. USB_OTG_HS_CORE_ID,
  154. #endif
  155. &USB_Host,
  156. &USBH_MSC_cb,
  157. &USR_cb);
  158. #ifdef SPI_MASTER
  159. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  160. SPI_Init(SPIx,&SPI_InitStructure);
  161. SPI_Cmd(SPIx,ENABLE);
  162. while(1)
  163. {
  164. /* Host Task handler */
  165. USBH_Process(&USB_OTG_Core, &USB_Host);
  166. // SPI2_ReadWriteByte(0xAA);
  167. CmdTransmitted = CMD_NONE;
  168. KEY = KBD_GetKeys();
  169. switch(KEY)
  170. {
  171. case WAKEUP:
  172. CmdTransmitted = CMD_UP;
  173. NumberOfByte = CMD_UP_SIZE;
  174. printf("\r\n WAKEUP");
  175. break;
  176. case TAMPER:
  177. CmdTransmitted = CMD_SEL;
  178. NumberOfByte = CMD_SEL_SIZE;
  179. printf("\r\n TAMPER");
  180. break;
  181. default:
  182. // CmdTransmitted = CMD_LEFT;
  183. // NumberofByte = CMD_LEFT_SIZE;
  184. break;
  185. }
  186. CmdStatus = 0x00;
  187. Tx_Idx = 0;
  188. if(CmdTransmitted != CMD_NONE)
  189. {
  190. SPI_I2S_ITConfig(SPIx,SPI_I2S_IT_TXE,ENABLE);
  191. while(Tx_Idx<GetVar_NbrOfData())
  192. {}
  193. }
  194. }
  195. #endif
  196. }
  197. void SysTickConfig(void)
  198. {
  199. /* Set SysTick Timer for 10ms interrupts */
  200. if (SysTick_Config(SystemCoreClock / 100))
  201. {
  202. /* Capture error */
  203. while (1);
  204. }
  205. /* Configure the SysTick handler priority */
  206. NVIC_SetPriority(SysTick_IRQn, 0x0);
  207. }
  208. #ifdef USE_FULL_ASSERT
  209. /**
  210. * @brief assert_failed
  211. * Reports the name of the source file and the source line number
  212. * where the assert_param error has occurred.
  213. * @param File: pointer to the source file name
  214. * @param Line: assert_param error line source number
  215. * @retval None
  216. */
  217. void assert_failed(uint8_t* file, uint32_t line)
  218. {
  219. /* User can add his own implementation to report the file name and line number,
  220. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  221. /* Infinite loop */
  222. while (1)
  223. {}
  224. }
  225. #endif
  226. uint8_t GetVar_NbrOfData(void)
  227. {
  228. return NumberOfByte;
  229. }
  230. void TimeOut_UserCallback(void)
  231. {
  232. /* User can add his own implementation to manage TimeOut Communication failure */
  233. /* Block communication and all processes */
  234. while (1)
  235. {
  236. }
  237. }
  238. /**
  239. * @}
  240. */
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
main.h

  1. #ifndef __MAIN_H
  2. #define __MAIN_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #define SPI_MASTER
  7. #define TXBUFFERSIZE (countof(TxBuffer) - 1)
  8. #define RXBUFFERSIZE TXBUFFERSIZE
  9. #define USER_TIMEOUT ((uint32_t)0x64) /* Waiting 1s */
  10. /* Joystick Commands */
  11. #define CMD_RIGHT 0x55
  12. #define CMD_LEFT 0xAA
  13. #define CMD_UP 0x33
  14. #define CMD_DOWN 0xCC
  15. #define CMD_SEL 0xFF
  16. #define CMD_NONE 0x00
  17. #define CMD_ACK 0x66
  18. /* Define numbers of bytes to transmit from TxBuffer */
  19. #define CMD_RIGHT_SIZE 0x01
  20. #define CMD_LEFT_SIZE 0x05
  21. #define CMD_UP_SIZE 0x14
  22. #define CMD_DOWN_SIZE 0x1E
  23. #define CMD_SEL_SIZE TXBUFFERSIZE
  24. #ifdef __cplusplus
  25. }
  26. #endif
  27. #endif /* __MAIN_H */


   从机的SPI的驱动跟主机的是一样的,修改一下主从机就行了。

stm32fxx_it.c的关键代码如下:

  1. void SPI1_IRQHandler(void)
  2. {
  3. #ifdef SPI_SLAVE
  4. if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_RXNE) == SET)
  5. {
  6. // printf("enter RECEIVE IR \r\n");
  7. RxBuffer[Rx_Idx++] = SPI_I2S_ReceiveData(SPIx);
  8. // printf("%x\r\n",SPI2_ReadWriteByte(0x55));
  9. // Delay(10);
  10. }
  11. // SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, DISABLE);
  12. #endif
  13. }

   main.c的关键代码如下:

  1. /********************************************************************
  2. *¹¤³ÌÃû³Æ£ºARM Cortex-M4 GPIO²âÊÔ
  3. *×÷ ÕߣºÌìϵÄÈË
  4. *ÈÕ ÆÚ£º2012Äê3ÔÂ23ÈÕ
  5. *********************************************************************/
  6. #include "main.h"
  7. #include "stdio.h"
  8. #include "MagicBlueSTM32BSP.h"
  9. #include "stm32f4_spi.h"
  10. #include "stm32f4x7_eth_bsp.h"
  11. #include "stm32f4x7_eth.h"
  12. /********************************************************************/
  13. SPI_InitTypeDef SPI_InitStructure;
  14. uint8_t TxBuffer[] = "SPI Interrupt Example: Communication between two SPI using Interrupts";
  15. __IO uint32_t TimeOut = 0;
  16. #ifdef SPI_SLAVE
  17. __IO uint8_t RxBuffer[RXBUFFERSIZE];
  18. __IO uint8_t Rx_Idx = 0 ;
  19. #endif
  20. //DMA_InitTypeDef DMA_InitStructure;
  21. uint8_t Buff[BuffSize] = { 0xAA,0x00,0x00,0xAA,
  22. 0x55,0x55,0x55,0x55,
  23. 0x55,0x00,0x00,0x00,
  24. 0x00,0x00,0x00,0x00,
  25. 0xAA,0xAA,0xAA,0xAA,
  26. 0xAA,0xAA,0xAA,0xAA,
  27. 0xAA,0xAA,0xAA,0xAA,
  28. 0x00,0x00,0x00,0xAA};
  29. uint8_t Buffer[64];
  30. uint16_t *Data=0;
  31. /********************************************************************/
  32. void Delay(__IO uint32_t nCount);
  33. void SPI1Init(void);
  34. void SysTickConfig(void);
  35. #ifdef SPI_SLAVE
  36. static TestStatus Buffercmp(uint8_t *pBuffer1,__IO uint8_t *pBuffer2,uint16_t BufferLength);
  37. static void Fill_Buffer(__IO uint8_t *pBuffer,uint16_t BufferLength);
  38. #endif
  39. /********************************************************************/
  40. int main()
  41. {
  42. // uint8_t i=0;
  43. ETH_BSP_Config();
  44. ETH_WritePHYRegister(0x01,0x00,(0x0001<<11));
  45. SPI1Init();
  46. SysTickConfig();
  47. #ifdef SPI_SLAVE
  48. SPI_I2S_DeInit(SPIx);
  49. SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  50. SPI_Init(SPIx,&SPI_InitStructure);
  51. SPI_I2S_ITConfig(SPIx,SPI_I2S_IT_RXNE,ENABLE);
  52. SPI_Cmd(SPIx,ENABLE);
  53. UART1_Init(115200);
  54. UART6_Init(115200);
  55. // SPI1_DMA_Congiguration();
  56. KeyInit();
  57. printf("init success!\n");
  58. LED_Init();
  59. LED_ON(1);
  60. LED_OFF(2);
  61. LED_ON(3);
  62. Rx_Idx = 0;
  63. Fill_Buffer(RxBuffer,TXBUFFERSIZE);
  64. while(1)
  65. {
  66. // DMA_DeInit(SPIx_DMA_STREAM);
  67. // DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buff;
  68. // DMA_InitStructure.DMA_BufferSize = BuffSize;
  69. // DMA_Init(SPIx_DMA_STREAM, &DMA_InitStructure);
  70. // SPI_I2S_DMACmd(SPIx,SPI_DMAReq_Rx,ENABLE);
  71. // DMA_Cmd(SPIx_DMA_STREAM,ENABLE);
  72. while(RxBuffer[0] == CMD_NONE){}
  73. TimeOut = 1;
  74. while (TimeOut != 0x00)
  75. {
  76. }
  77. //printf("%d",RxBuffer[0]);
  78. // printf("%s\r\n",RxBuffer);
  79. // printf("%d",RxBuffer[1]);
  80. switch(RxBuffer[0])
  81. {
  82. case CMD_UP:
  83. if(Buffercmp(TxBuffer,&RxBuffer[1],CMD_UP_SIZE) != FAILED)
  84. {
  85. LED_ON(1);
  86. LED_ON(2);
  87. LED_OFF(3);
  88. printf("CMD_UP recieve!");
  89. Rx_Idx = 0;
  90. Fill_Buffer(RxBuffer,TXBUFFERSIZE);
  91. }
  92. break;
  93. case CMD_SEL:
  94. if(Buffercmp(TxBuffer,&RxBuffer[1],CMD_SEL_SIZE)!= FAILED)
  95. {
  96. LED_OFF(1);
  97. LED_OFF(2);
  98. LED_OFF(3);
  99. printf("CMD_SEL recieve!");
  100. Rx_Idx = 0;
  101. Fill_Buffer(RxBuffer,TXBUFFERSIZE);
  102. }
  103. else
  104. {
  105. printf("CMD_SEL_SIZE== FAILED\r\nARxBuffer[1]=%s\r\nRxBuffer[2]=%s\r\n",&RxBuffer[1],TxBuffer);
  106. }
  107. break;
  108. default:
  109. printf("\r\n enter default");
  110. Rx_Idx = 0;
  111. Fill_Buffer(RxBuffer,TXBUFFERSIZE);
  112. break;
  113. }
  114. }
  115. #endif
  116. }
  117. void SysTickConfig(void)
  118. {
  119. /* Setup SysTick Timer for 10ms interrupts */
  120. if (SysTick_Config(SystemCoreClock / 100))
  121. {
  122. /* Capture error */
  123. while (1);
  124. }
  125. /* Configure the SysTick handler priority */
  126. NVIC_SetPriority(SysTick_IRQn, 0x0);
  127. }
  128. /********************************************************************/
  129. /**
  130. * @brief Fills buffer.
  131. * @param pBuffer: pointer on the Buffer to fill
  132. * @param BufferLength: size of the buffer to fill
  133. * @retval None
  134. */
  135. static void Fill_Buffer(__IO uint8_t *pBuffer, uint16_t BufferLength)
  136. {
  137. uint32_t index = 0;
  138. /* Put in global buffer same values */
  139. for (index = 0; index < BufferLength; index++ )
  140. {
  141. pBuffer[index] = 0x00;
  142. }
  143. }
  144. static TestStatus Buffercmp(uint8_t* pBuffer1, __IO uint8_t* pBuffer2, uint16_t BufferLength)
  145. {
  146. while (BufferLength--)
  147. {
  148. if (*pBuffer1 != *pBuffer2)
  149. {
  150. return FAILED;
  151. }
  152. pBuffer1++;
  153. pBuffer2++;
  154. }
  155. return PASSED;
  156. }
  157. /********************************************************************/
  158. #ifdef USE_FULL_ASSERT
  159. /**
  160. * @brief Reports the name of the source file and the source line number
  161. * where the assert_param error has occurred.
  162. * @param file: pointer to the source file name
  163. * @param line: assert_param error line source number
  164. * @retval None
  165. */
  166. void assert_failed(uint8_t* file, uint32_t line)
  167. {
  168. /* User can add his own implementation to report the file name and line number,
  169. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  170. /* Infinite loop */
  171. while (1)
  172. {
  173. }
  174. }
  175. #endif
  176. //------------------- The end ---------------------------------
    mian.h

  1. ///**************************************************************
  2. //*
  3. //*
  4. //*
  5. //***************************************************************/
  6. //#include "stm32f4xx.h"
  7. //#include "stm32f4xx_conf.h"
  8. //#include <stdio.h>
  9. #include"core_cm4.h";
  10. //#include "STM32F4_UART.h"
  11. //#include "SysTick.h"
  12. //#include "MagicBlueSTM32BSP.h"
  13. /* Define to prevent recursive inclusion -------------------------------------*/
  14. #ifndef __MAIN_H
  15. #define __MAIN_H
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f4xx.h"
  21. #include "stm32f4xx_conf.h"
  22. #include <stdio.h>
  23. //#include"core_cm4.h";
  24. #include "STM32F4_UART.h"
  25. #include "SysTick.h"
  26. #include "MagicBlueSTM32BSP.h"
  27. //#include "stm324xg_eval.h"
  28. //#include "stm324xg_eval_ioe.h"
  29. #define SPI_SLAVE
  30. /* Exported typedef -----------------------------------------------------------*/
  31. #define countof(a) (sizeof(a) / sizeof(*(a)))
  32. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  33. void SPI1_IRQHandler(void);
  34. #define TXBUFFERSIZE (countof(TxBuffer) - 1)
  35. #define RXBUFFERSIZE TXBUFFERSIZE
  36. /* Joystick Commands */
  37. #define CMD_RIGHT 0x55
  38. #define CMD_LEFT 0xAA
  39. #define CMD_UP 0x33
  40. #define CMD_DOWN 0xCC
  41. #define CMD_SEL 0xFF
  42. #define CMD_NONE 0x00
  43. #define CMD_ACK 0x66
  44. /* Define numbers of bytes to transmit from TxBuffer */
  45. #define CMD_RIGHT_SIZE 0x01
  46. #define CMD_LEFT_SIZE 0x05
  47. #define CMD_UP_SIZE 0x14
  48. #define CMD_DOWN_SIZE 0x1E
  49. #define CMD_SEL_SIZE TXBUFFERSIZE
  50. #ifdef __cplusplus
  51. }
  52. #endif
  53. #endif /* __MAIN_H */
工作的原理大致就是,配置完SPI后就打开SPI中断,首先判断是不是命令,否则判断字节长度是不是小于设定的字节数,然后SPI就发送一个字节,直至达到要发送的字节数,关闭发送中断。接收会触发一个接收中断,然后把接收到数据和TxBuffer进行比较,如果成功就亮起灯或者熄灭灯。注意:接收到的第一个字节是命令。从第二个字节后开始比较。

整个流程就是这样。谢谢您的注意。


本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/50865
推荐阅读
相关标签
  

闽ICP备14008679号