PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/taxi_v1.2/Project/Examples/I2C/Interrupt/main.c

https://github.com/jiesse/time-meter
C | 258 lines | 107 code | 40 blank | 111 comment | 3 complexity | 5da29a01076abcc7a4daf64b79d3b3c0 MD5 | raw file
  1. /**
  2. ******************************************************************************
  3. * @file I2C/Interrupt/main.c
  4. * @author MCD Application Team
  5. * @version V3.0.0
  6. * @date 04/06/2009
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @copy
  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>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x.h"
  22. #include "main.h"
  23. /** @addtogroup StdPeriph_Examples
  24. * @{
  25. */
  26. /** @addtogroup I2C_Interrupt
  27. * @{
  28. */
  29. /* Private typedef -----------------------------------------------------------*/
  30. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  31. /* Private define ------------------------------------------------------------*/
  32. #define I2C1_SLAVE_ADDRESS7 0x30
  33. #define ClockSpeed 200000
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. I2C_InitTypeDef I2C_InitStructure;
  37. NVIC_InitTypeDef NVIC_InitStructure;
  38. uint8_t I2C1_Buffer_Tx[Tx1BufferSize] = {1,2,3,4}, I2C2_Buffer_Tx[Tx2BufferSize] = {5,6,7,8};
  39. uint8_t I2C1_Buffer_Rx[Tx2BufferSize], I2C2_Buffer_Rx[Tx1BufferSize+1];
  40. __IO uint8_t Tx1_Idx = 0, Rx1_Idx = 0, Tx2_Idx = 0, Rx2_Idx = 0;
  41. __IO uint8_t Direction = Transmitter;
  42. volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
  43. /* Private functions ---------------------------------------------------------*/
  44. void RCC_Configuration(void);
  45. void GPIO_Configuration(void);
  46. void NVIC_Configuration(void);
  47. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  48. /**
  49. * @brief Main program
  50. * @param None
  51. * @retval : None
  52. */
  53. int main(void)
  54. {
  55. /* System clocks configuration ---------------------------------------------*/
  56. RCC_Configuration();
  57. /* NVIC configuration ------------------------------------------------------*/
  58. NVIC_Configuration();
  59. /* GPIO configuration ------------------------------------------------------*/
  60. GPIO_Configuration();
  61. /* Enable I2C1 and I2C2 ----------------------------------------------------*/
  62. I2C_Cmd(I2C1, ENABLE);
  63. I2C_Cmd(I2C2, ENABLE);
  64. /* I2C1 configuration ------------------------------------------------------*/
  65. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  66. I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  67. I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
  68. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  69. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  70. I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
  71. I2C_Init(I2C1, &I2C_InitStructure);
  72. /* I2C2 configuration ------------------------------------------------------*/
  73. I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
  74. I2C_Init(I2C2, &I2C_InitStructure);
  75. I2C_CalculatePEC(I2C1, ENABLE);
  76. I2C_CalculatePEC(I2C2, ENABLE);
  77. /* Enable I2C1 event and buffer interrupts */
  78. I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
  79. /* Enable I2C1 event and buffer interrupts */
  80. I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
  81. /*----- Transmission Phase -------------------------------------------------*/
  82. /* Set data direction to transmitter */
  83. Direction = Transmitter;
  84. /* Send I2C1 START condition */
  85. I2C_GenerateSTART(I2C1, ENABLE);
  86. /* Wait until all data and the PEC value are received */
  87. /* I2C2_Buffer_Rx buffer will contain the data plus the PEC value */
  88. while(Rx2_Idx < Tx1BufferSize)
  89. {
  90. }
  91. /* Check the corectness of the I2C1 transmitted data */
  92. TransferStatus1 = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, Tx1BufferSize);
  93. /* TransferStatus1 = PASSED, if the transmitted and received data
  94. are equal */
  95. /* TransferStatus1 = FAILED, if the transmitted and received data
  96. are different */
  97. /*----- Reception Phase --------------------------------------------------*/
  98. /* Re-configure and enable I2C1 event interrupt to have the higher priority */
  99. NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
  100. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  101. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  102. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  103. NVIC_Init(&NVIC_InitStructure);
  104. /* Wait until end of Slave transmission */
  105. while(Rx1_Idx < Tx2BufferSize)
  106. {
  107. }
  108. /* Check the corectness of the I2C1 received data */
  109. TransferStatus2 = Buffercmp(I2C2_Buffer_Tx, I2C1_Buffer_Rx, Tx2BufferSize);
  110. /* TransferStatus2 = PASSED, if the transmitted and received data
  111. are equal */
  112. /* TransferStatus2 = FAILED, if the transmitted and received data
  113. are different */
  114. while(1)
  115. {
  116. }
  117. }
  118. /**
  119. * @brief Configures the different system clocks.
  120. * @param None
  121. * @retval : None
  122. */
  123. void RCC_Configuration(void)
  124. {
  125. /* Setup the microcontroller system. Initialize the Embedded Flash Interface,
  126. initialize the PLL and update the SystemFrequency variable. */
  127. SystemInit();
  128. /* Enable peripheral clocks --------------------------------------------------*/
  129. /* Enable I2C1 and I2C2 clock */
  130. RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2, ENABLE);
  131. /* Enable GPIOB clock */
  132. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  133. }
  134. /**
  135. * @brief Configures the different GPIO ports.
  136. * @param None
  137. * @retval : None
  138. */
  139. void GPIO_Configuration(void)
  140. {
  141. GPIO_InitTypeDef GPIO_InitStructure;
  142. /* Configure I2C1 pins: SCL and SDA ----------------------------------------*/
  143. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  144. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  145. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  146. GPIO_Init(GPIOB, &GPIO_InitStructure);
  147. /* Configure I2C2 pins: SCL and SDA ----------------------------------------*/
  148. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  149. GPIO_Init(GPIOB, &GPIO_InitStructure);
  150. }
  151. /**
  152. * @brief Configures NVIC and Vector Table base location.
  153. * @param None
  154. * @retval : None
  155. */
  156. void NVIC_Configuration(void)
  157. {
  158. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  159. /* Configure and enable I2C1 event interrupt -------------------------------*/
  160. NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
  161. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  162. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  163. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  164. NVIC_Init(&NVIC_InitStructure);
  165. /* Configure and enable I2C2 event interrupt -------------------------------*/
  166. NVIC_InitStructure.NVIC_IRQChannel = I2C2_EV_IRQn;
  167. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  168. NVIC_Init(&NVIC_InitStructure);
  169. /* Configure and enable I2C2 error interrupt -------------------------------*/
  170. NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn;
  171. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  172. NVIC_Init(&NVIC_InitStructure);
  173. }
  174. /**
  175. * @brief Compares two buffers.
  176. * @param pBuffer1, pBuffer2: buffers to be compared.
  177. * @param BufferLength: buffer's length
  178. * @retval : PASSED: pBuffer1 identical to pBuffer2
  179. * FAILED: pBuffer1 differs from pBuffer2
  180. */
  181. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  182. {
  183. while(BufferLength--)
  184. {
  185. if(*pBuffer1 != *pBuffer2)
  186. {
  187. return FAILED;
  188. }
  189. pBuffer1++;
  190. pBuffer2++;
  191. }
  192. return PASSED;
  193. }
  194. #ifdef USE_FULL_ASSERT
  195. /**
  196. * @brief Reports the name of the source file and the source line number
  197. * where the assert_param error has occurred.
  198. * @param file: pointer to the source file name
  199. * @param line: assert_param error line source number
  200. * @retval : None
  201. */
  202. void assert_failed(uint8_t* file, uint32_t line)
  203. {
  204. /* User can add his own implementation to report the file name and line number,
  205. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  206. /* Infinite loop */
  207. while (1)
  208. {
  209. }
  210. }
  211. #endif
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/