Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / Projects / discovery_demo / stm32f4xx_it.c
1 /**
2   ******************************************************************************
3   * @file    stm32f4xx_it.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    19-September-2011
7   * @brief   Main Interrupt Service Routines.
8   *          This file provides all exceptions handler and peripherals interrupt
9   *          service routine.
10   ******************************************************************************
11   * @attention
12   *
13   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
14   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
15   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
16   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
17   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
18   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
19   *
20   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
21   ******************************************************************************
22   */ 
23
24 /* Includes ------------------------------------------------------------------*/
25 #include "stm32f4xx_it.h"
26 #include "main.h"
27 #include "usb_core.h"
28 #include "usbd_core.h"
29 #include "stm32f4_discovery.h"
30 #include "usbd_hid_core.h"
31
32 //Library config for this project!!!!!!!!!!!
33 #include "stm32f4xx_conf.h"
34
35
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 #define CURSOR_STEP     7
39
40 extern uint8_t Buffer[6];
41 /* Private macro -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 extern __IO uint8_t DemoEnterCondition;
44 uint8_t Counter  = 0x00;
45 extern int8_t X_Offset;
46 extern int8_t Y_Offset;
47 extern __IO uint8_t UserButtonPressed;
48 __IO uint8_t TempAcceleration = 0;
49 /* Private function prototypes -----------------------------------------------*/
50 extern USB_OTG_CORE_HANDLE           USB_OTG_dev;
51 static uint8_t *USBD_HID_GetPos (void);
52 extern uint32_t USBD_OTG_ISR_Handler (USB_OTG_CORE_HANDLE *pdev);
53
54 /******************************************************************************/
55 /*            Cortex-M3 Processor Exceptions Handlers                         */
56 /******************************************************************************/
57
58 /**
59   * @brief   This function handles NMI exception.
60   * @param  None
61   * @retval None
62   */
63 void NMI_Handler(void)
64 {
65 }
66
67 /**
68   * @brief  This function handles Hard Fault exception.
69   * @param  None
70   * @retval None
71   */
72 void HardFault_Handler(void)
73 {
74   /* Go to infinite loop when Hard Fault exception occurs */
75   while (1)
76   {
77   }
78 }
79
80 /**
81   * @brief  This function handles Memory Manage exception.
82   * @param  None
83   * @retval None
84   */
85 void MemManage_Handler(void)
86 {
87   /* Go to infinite loop when Memory Manage exception occurs */
88   while (1)
89   {
90   }
91 }
92
93 /**
94   * @brief  This function handles Bus Fault exception.
95   * @param  None
96   * @retval None
97   */
98 void BusFault_Handler(void)
99 {
100   /* Go to infinite loop when Bus Fault exception occurs */
101   while (1)
102   {
103   }
104 }
105
106 /**
107   * @brief  This function handles Usage Fault exception.
108   * @param  None
109   * @retval None
110   */
111 void UsageFault_Handler(void)
112 {
113   /* Go to infinite loop when Usage Fault exception occurs */
114   while (1)
115   {
116   }
117 }
118
119 /**
120   * @brief  This function handles SVCall exception.
121   * @param  None
122   * @retval None
123   */
124 void SVC_Handler(void)
125 {
126 }
127
128 /**
129   * @brief  This function handles Debug Monitor exception.
130   * @param  None
131   * @retval None
132   */
133 void DebugMon_Handler(void)
134 {
135 }
136
137 /**
138   * @brief  This function handles PendSVC exception.
139   * @param  None
140   * @retval None
141   */
142 void PendSV_Handler(void)
143 {
144 }
145
146 /**
147   * @brief  This function handles SysTick Handler.
148   * @param  None
149   * @retval None
150   */
151 void SysTick_Handler(void)
152 {
153   uint8_t *buf;
154   uint8_t temp1, temp2 = 0x00;
155   
156   if (DemoEnterCondition == 0x00)
157   {
158     TimingDelay_Decrement();
159   }
160   else
161   {
162     buf = USBD_HID_GetPos();
163     if((buf[1] != 0) ||(buf[2] != 0))
164     {
165       USBD_HID_SendReport (&USB_OTG_dev, 
166                            buf,
167                            4);
168     } 
169     Counter ++;
170     if (Counter == 10)
171     {
172       Buffer[0] = 0;
173       Buffer[2] = 0;
174       /* Disable All TIM4 Capture Compare Channels */
175       TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE);
176       TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE);
177       TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE);
178       TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE);
179       
180       LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
181       /* Remove the offsets values from data */
182       Buffer[0] -= X_Offset;
183       Buffer[2] -= Y_Offset;
184       /* Update autoreload and capture compare registers value*/
185       temp1 = ABS((int8_t)(Buffer[0]));
186       temp2 = ABS((int8_t)(Buffer[2]));       
187       TempAcceleration = MAX(temp1, temp2);
188
189       if(TempAcceleration != 0)
190       { 
191         if ((int8_t)Buffer[0] < -2)
192         {
193           /* Enable TIM4 Capture Compare Channel 4 */
194           TIM_CCxCmd(TIM4, TIM_Channel_4, ENABLE);
195           /* Sets the TIM4 Capture Compare4 Register value */
196           TIM_SetCompare4(TIM4, TIM_CCR/TempAcceleration);
197         }
198         if ((int8_t)Buffer[0] > 2)
199         {
200           /* Enable TIM4 Capture Compare Channel 2 */
201           TIM_CCxCmd(TIM4, TIM_Channel_2, ENABLE);
202           /* Sets the TIM4 Capture Compare2 Register value */
203           TIM_SetCompare2(TIM4, TIM_CCR/TempAcceleration);
204         }
205         if ((int8_t)Buffer[2] > 2)
206         { 
207           /* Enable TIM4 Capture Compare Channel 1 */
208           TIM_CCxCmd(TIM4, TIM_Channel_1, ENABLE);
209           /* Sets the TIM4 Capture Compare1 Register value */
210           TIM_SetCompare1(TIM4, TIM_CCR/TempAcceleration);
211         }      
212         if ((int8_t)Buffer[2] < -2)
213         { 
214           /* Enable TIM4 Capture Compare Channel 3 */
215           TIM_CCxCmd(TIM4, TIM_Channel_3, ENABLE);
216           /* Sets the TIM4 Capture Compare3 Register value */
217           TIM_SetCompare3(TIM4, TIM_CCR/TempAcceleration);
218         }
219         /* Time base configuration */
220         TIM_SetAutoreload(TIM4,  TIM_ARR/TempAcceleration);
221       }
222       Counter = 0x00;
223     }  
224   }
225   
226 }
227
228 /******************************************************************************/
229 /*                 STM32Fxxx Peripherals Interrupt Handlers                   */
230 /*  Add here the Interrupt Handler for the used peripheral(s) (PPP), for the  */
231 /*  available peripheral interrupt handler's name please refer to the startup */
232 /*  file (startup_stm32fxxx.s).                                               */
233 /******************************************************************************/
234
235 /**
236   * @brief  This function handles PPP interrupt request.
237   * @param  None
238   * @retval None
239   */
240 /*void PPP_IRQHandler(void)
241 {
242 }*/
243
244 /**
245   * @brief  This function handles EXTI0_IRQ Handler.
246   * @param  None
247   * @retval None
248   */
249 void EXTI0_IRQHandler(void)
250 {
251   UserButtonPressed = 0x01;
252   
253   /* Clear the EXTI line pending bit */
254   EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);
255 }
256
257 /**
258   * @brief  This function handles EXTI15_10_IRQ Handler.
259   * @param  None
260   * @retval None
261   */
262 void OTG_FS_WKUP_IRQHandler(void)
263 {
264   if(USB_OTG_dev.cfg.low_power)
265   {
266         /* Reset SLEEPDEEP and SLEEPONEXIT bits */
267         SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
268
269         /* After wake-up from sleep mode, reconfigure the system clock */
270         SystemInit();
271     USB_OTG_UngateClock(&USB_OTG_dev);
272   }
273   EXTI_ClearITPendingBit(EXTI_Line18);
274 }
275
276 /**
277   * @brief  This function handles OTG_HS Handler.
278   * @param  None
279   * @retval None
280   */
281 void OTG_FS_IRQHandler(void)
282 {
283   USBD_OTG_ISR_Handler (&USB_OTG_dev);
284 }
285
286 /**
287 * @brief  USBD_HID_GetPos
288 * @param  None
289 * @retval Pointer to report
290 */
291 static uint8_t *USBD_HID_GetPos (void)
292 {
293   static uint8_t HID_Buffer[4] = {0};
294   
295   HID_Buffer[1] = 0;
296   HID_Buffer[2] = 0;
297   /* LEFT Direction */
298   if(((int8_t)Buffer[2]) < -2)
299   {
300     HID_Buffer[1] += CURSOR_STEP;
301   }
302   /* RIGHT Direction */ 
303   if(((int8_t)Buffer[2]) > 2)
304   {
305    HID_Buffer[1] -= CURSOR_STEP;
306   } 
307   /* UP Direction */
308   if(((int8_t)Buffer[0]) < -2)
309   {
310     HID_Buffer[2] += CURSOR_STEP;
311   }
312   /* DOWN Direction */ 
313   if(((int8_t)Buffer[0]) > 2)
314   {
315     HID_Buffer[2] -= CURSOR_STEP;
316   } 
317   
318   return HID_Buffer;
319 }
320 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/