2 ******************************************************************************
4 * @author MCD Application Team
6 * @date 19-September-2011
7 * @brief Main program body
8 ******************************************************************************
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.
18 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
22 /* Includes ------------------------------------------------------------------*/
24 #include "usbd_hid_core.h"
26 #include "usbd_desc.h"
28 //Library config for this project!!!!!!!!!!!
29 #include "stm32f4xx_conf.h"
31 /** @addtogroup STM32F4-Discovery_Demo
35 /* Private typedef -----------------------------------------------------------*/
36 /* Private define ------------------------------------------------------------*/
38 #define TESTRESULT_ADDRESS 0x080FFFFC
39 #define ALLTEST_PASS 0x00000000
40 #define ALLTEST_FAIL 0x55555555
42 /* Private macro -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 #ifdef USB_OTG_HS_INTERNAL_DMA_ENABLED
45 #if defined ( __ICCARM__ ) /*!< IAR Compiler */
46 #pragma data_alignment = 4
48 #endif /* USB_OTG_HS_INTERNAL_DMA_ENABLED */
49 __ALIGN_BEGIN USB_OTG_CORE_HANDLE USB_OTG_dev __ALIGN_END;
51 uint16_t PrescalerValue = 0;
53 __IO uint32_t TimingDelay;
54 __IO uint8_t DemoEnterCondition = 0x00;
55 __IO uint8_t UserButtonPressed = 0x00;
56 LIS302DL_InitTypeDef LIS302DL_InitStruct;
57 LIS302DL_FilterConfigTypeDef LIS302DL_FilterStruct;
58 __IO int8_t X_Offset, Y_Offset, Z_Offset = 0x00;
61 /* Private function prototypes -----------------------------------------------*/
62 static uint32_t Demo_USBConfig(void);
63 static void TIM4_Config(void);
64 static void Demo_Exec(void);
66 /* Private functions ---------------------------------------------------------*/
69 * @brief Main program.
75 RCC_ClocksTypeDef RCC_Clocks;
77 /* Initialize LEDs and User_Button on STM32F4-Discovery --------------------*/
78 STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
80 STM_EVAL_LEDInit(LED4);
81 STM_EVAL_LEDInit(LED3);
82 STM_EVAL_LEDInit(LED5);
83 STM_EVAL_LEDInit(LED6);
85 /* SysTick end of count event each 10ms */
86 RCC_GetClocksFreq(&RCC_Clocks);
87 SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
89 if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
91 /* Turn on LEDs available on STM32F4-Discovery ---------------------------*/
97 if ((*(__IO uint32_t*) TESTRESULT_ADDRESS) == ALLTEST_PASS)
100 /* Waiting User Button is pressed or Test Program condition verified */
101 while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00))
106 /* Waiting User Button is Released or TimeOut*/
108 while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00))
110 if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
115 if (TimingDelay == 0x00)
117 /* Turn off LEDs available on STM32F4-Discovery ------------------------*/
118 STM_EVAL_LEDOff(LED4);
119 STM_EVAL_LEDOff(LED3);
120 STM_EVAL_LEDOff(LED5);
121 STM_EVAL_LEDOff(LED6);
123 /* Waiting User Button is released */
124 while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
127 /* Unlocks the FLASH control register access */
130 /* Move discovery kit to detect negative and positive acceleration values
131 on X, Y and Z axis */
132 Accelerometer_MEMS_Test();
134 /* USB Hardware connection */
137 /* Audio Hardware connection */
140 /* Microphone MEMS Hardware connection */
141 Microphone_MEMS_Test();
143 /* Write PASS code at last word in the flash memory */
144 FLASH_ProgramWord(TESTRESULT_ADDRESS, ALLTEST_PASS);
148 /* Toggle Green LED: signaling the End of the Test program */
149 STM_EVAL_LEDToggle(LED4);
165 * @brief Execute the demo application.
169 static void Demo_Exec(void)
171 RCC_ClocksTypeDef RCC_Clocks;
172 uint8_t togglecounter = 0x00;
176 DemoEnterCondition = 0x00;
178 /* Reset UserButton_Pressed variable */
179 UserButtonPressed = 0x00;
181 /* Initialize LEDs to be managed by GPIO */
182 STM_EVAL_LEDInit(LED4);
183 STM_EVAL_LEDInit(LED3);
184 STM_EVAL_LEDInit(LED5);
185 STM_EVAL_LEDInit(LED6);
187 /* SysTick end of count event each 10ms */
188 RCC_GetClocksFreq(&RCC_Clocks);
189 SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
191 /* Turn OFF all LEDs */
192 STM_EVAL_LEDOff(LED4);
193 STM_EVAL_LEDOff(LED3);
194 STM_EVAL_LEDOff(LED5);
195 STM_EVAL_LEDOff(LED6);
197 /* Waiting User Button is pressed */
198 while (UserButtonPressed == 0x00)
201 STM_EVAL_LEDToggle(LED4);
204 STM_EVAL_LEDToggle(LED3);
207 STM_EVAL_LEDToggle(LED5);
210 STM_EVAL_LEDToggle(LED6);
213 if (togglecounter == 0x10)
215 togglecounter = 0x00;
216 while (togglecounter < 0x10)
218 STM_EVAL_LEDToggle(LED4);
219 STM_EVAL_LEDToggle(LED3);
220 STM_EVAL_LEDToggle(LED5);
221 STM_EVAL_LEDToggle(LED6);
225 togglecounter = 0x00;
229 /* Waiting User Button is Released */
230 while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
232 UserButtonPressed = 0x00;
234 /* TIM4 channels configuration */
237 /* Disable all Timer4 channels */
238 TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE);
239 TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE);
240 TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE);
241 TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE);
243 /* MEMS configuration */
244 LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
245 LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
246 LIS302DL_InitStruct.Axes_Enable = LIS302DL_XYZ_ENABLE;
247 LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
248 LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
249 LIS302DL_Init(&LIS302DL_InitStruct);
251 /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate
255 DemoEnterCondition = 0x01;
256 /* MEMS High Pass Filter configuration */
257 LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
258 LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
259 LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
260 LIS302DL_FilterConfig(&LIS302DL_FilterStruct);
262 LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 6);
263 X_Offset = Buffer[0];
264 Y_Offset = Buffer[2];
265 Z_Offset = Buffer[4];
267 /* USB configuration */
270 /* Waiting User Button is pressed */
271 while (UserButtonPressed == 0x00)
274 /* Waiting User Button is Released */
275 while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
278 /* Disable SPI1 used to drive the MEMS accelerometre */
279 SPI_Cmd(LIS302DL_SPI, DISABLE);
281 /* Disconnect the USB device */
282 DCD_DevDisconnect(&USB_OTG_dev);
283 USB_OTG_StopDevice(&USB_OTG_dev);
288 * @brief Initializes the USB for the demonstration application.
292 static uint32_t Demo_USBConfig(void)
294 USBD_Init(&USB_OTG_dev,
304 * @brief Configures the TIM Peripheral.
308 static void TIM4_Config(void)
310 GPIO_InitTypeDef GPIO_InitStructure;
311 TIM_OCInitTypeDef TIM_OCInitStructure;
312 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
314 /* --------------------------- System Clocks Configuration -----------------*/
315 /* TIM4 clock enable */
316 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
318 /* GPIOD clock enable */
319 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
321 /*-------------------------- GPIO Configuration ----------------------------*/
322 /* GPIOD Configuration: Pins 12, 13, 14 and 15 in output push-pull */
323 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
324 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
325 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
326 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
327 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
328 GPIO_Init(GPIOD, &GPIO_InitStructure);
330 /* Connect TIM4 pins to AF2 */
331 GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
332 GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
333 GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
334 GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
336 /* -----------------------------------------------------------------------
337 TIM4 Configuration: Output Compare Timing Mode:
339 In this example TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1),
340 since APB1 prescaler is different from 1 (APB1 Prescaler = 4, see system_stm32f4xx.c file).
343 => TIM4CLK = 2*(HCLK / 4) = HCLK/2 = SystemCoreClock/2
345 To get TIM4 counter clock at 2 KHz, the prescaler is computed as follows:
346 Prescaler = (TIM4CLK / TIM1 counter clock) - 1
347 Prescaler = (168 MHz/(2 * 2 KHz)) - 1 = 41999
349 To get TIM4 output clock at 1 Hz, the period (ARR)) is computed as follows:
350 ARR = (TIM4 counter clock / TIM4 output clock) - 1
353 TIM4 Channel1 duty cycle = (TIM4_CCR1/ TIM4_ARR)* 100 = 50%
354 TIM4 Channel2 duty cycle = (TIM4_CCR2/ TIM4_ARR)* 100 = 50%
355 TIM4 Channel3 duty cycle = (TIM4_CCR3/ TIM4_ARR)* 100 = 50%
356 TIM4 Channel4 duty cycle = (TIM4_CCR4/ TIM4_ARR)* 100 = 50%
358 ==> TIM4_CCRx = TIM4_ARR/2 = 1000 (where x = 1, 2, 3 and 4).
361 SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
362 Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
363 function to update SystemCoreClock variable value. Otherwise, any configuration
364 based on this variable will be incorrect.
365 ----------------------------------------------------------------------- */
368 /* Compute the prescaler value */
369 PrescalerValue = (uint16_t) ((SystemCoreClock /2) / 2000) - 1;
371 /* Time base configuration */
372 TIM_TimeBaseStructure.TIM_Period = TIM_ARR;
373 TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
374 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
375 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
376 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
378 /* Enable TIM4 Preload register on ARR */
379 TIM_ARRPreloadConfig(TIM4, ENABLE);
381 /* TIM PWM1 Mode configuration: Channel */
382 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
383 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
384 TIM_OCInitStructure.TIM_Pulse = TIM_CCR;
385 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
387 /* Output Compare PWM1 Mode configuration: Channel1 */
388 TIM_OC1Init(TIM4, &TIM_OCInitStructure);
389 TIM_CCxCmd(TIM4, TIM_Channel_1, DISABLE);
391 TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
393 /* Output Compare PWM1 Mode configuration: Channel2 */
394 TIM_OC2Init(TIM4, &TIM_OCInitStructure);
395 TIM_CCxCmd(TIM4, TIM_Channel_2, DISABLE);
397 TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
399 /* Output Compare PWM1 Mode configuration: Channel3 */
400 TIM_OC3Init(TIM4, &TIM_OCInitStructure);
401 TIM_CCxCmd(TIM4, TIM_Channel_3, DISABLE);
403 TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
405 /* Output Compare PWM1 Mode configuration: Channel4 */
406 TIM_OC4Init(TIM4, &TIM_OCInitStructure);
407 TIM_CCxCmd(TIM4, TIM_Channel_4, DISABLE);
409 TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
411 /* TIM4 enable counter */
412 TIM_Cmd(TIM4, ENABLE);
416 * @brief Inserts a delay time.
417 * @param nTime: specifies the delay time length, in 10 ms.
420 void Delay(__IO uint32_t nTime)
424 while(TimingDelay != 0);
428 * @brief Decrements the TimingDelay variable.
432 void TimingDelay_Decrement(void)
434 if (TimingDelay != 0x00)
441 * @brief This function handles the test program fail.
445 void Fail_Handler(void)
447 /* Erase last sector */
448 FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
449 /* Write FAIL code at last word in the flash memory */
450 FLASH_ProgramWord(TESTRESULT_ADDRESS, ALLTEST_FAIL);
455 STM_EVAL_LEDToggle(LED5);
461 * @brief MEMS accelerometre management of the timeout situation.
465 uint32_t LIS302DL_TIMEOUT_UserCallback(void)
467 /* MEMS Accelerometer Timeout error occured during Test program execution */
468 if (DemoEnterCondition == 0x00)
470 /* Timeout error occured for SPI TXE/RXNE flags waiting loops.*/
473 /* MEMS Accelerometer Timeout error occured during Demo execution */
483 #ifdef USE_FULL_ASSERT
486 * @brief Reports the name of the source file and the source line number
487 * where the assert_param error has occurred.
488 * @param file: pointer to the source file name
489 * @param line: assert_param error line source number
492 void assert_failed(uint8_t* file, uint32_t line)
494 /* User can add his own implementation to report the file name and line number,
495 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
509 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/