Merge pull request #93 from zyp/master
[fw/stlink] / example / stm32f4 / Projects / discovery_demo / selftest.c
1 /**
2   ******************************************************************************
3   * @file    selftest.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    19-September-2011
7   * @brief   This file provides the hardware tests
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>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19   */ 
20
21 /* Includes ------------------------------------------------------------------*/
22 #include "selftest.h"
23
24 //Library config for this project!!!!!!!!!!!
25 #include "stm32f4xx_conf.h"
26
27
28 /* Private typedef -----------------------------------------------------------*/
29 /* Private define ------------------------------------------------------------*/
30 #define MEMS_PASSCONDITION              15
31 /* Private macro -------------------------------------------------------------*/
32 /* Private variables ---------------------------------------------------------*/
33 /* Init Structure definition */
34 RCC_ClocksTypeDef      RCC_Clocks;
35 ADC_InitTypeDef        ADC_InitStructure;
36 ADC_CommonInitTypeDef  ADC_CommonInitStructure;
37
38 __IO uint16_t ConvData1, ConvData2;
39 __IO uint16_t counter0 = 0, counter1 = 0, Idx = 0;
40 uint8_t ADC_Channel[2] = {ADC_Channel_2, ADC_Channel_3};
41 uint8_t DACTest = 0;
42 uint8_t GPIO_Pin [2] = {GPIO_Pin_2, GPIO_Pin_3};
43
44 uint16_t count = 0, count1 = 24, Left_Right = 0;
45 const int16_t sinebuf[48] = {0, 4276, 8480, 12539, 16383, 19947, 23169, 25995,
46                              28377, 30272, 31650, 32486, 32767, 32486, 31650, 30272,
47                              28377, 25995, 23169, 19947, 16383, 12539, 8480, 4276,
48                              0, -4276, -8480, -12539, -16383, -19947, -23169, -25995,
49                              -28377, -30272, -31650, -32486, -32767, -32486, -31650, -30272,
50                              -28377, -25995, -23169, -19947, -16383, -12539, -8480, -4276
51                              };
52 extern __IO uint32_t TimingDelay;
53
54 extern LIS302DL_InitTypeDef  LIS302DL_InitStruct;
55 extern LIS302DL_FilterConfigTypeDef LIS302DL_FilterStruct;  
56
57 extern __IO int8_t X_Offset, Y_Offset, Z_Offset;
58 extern uint8_t Buffer[6];
59 /* Private function prototypes -----------------------------------------------*/
60 /* Private functions ---------------------------------------------------------*/
61 /**
62   * @brief Test MEMS Hardware.
63   *   The main objectif of this test is to check the hardware connection of the 
64   *   MEMS peripheral.
65   * @param None
66   * @retval None
67   */
68 void Accelerometer_MEMS_Test(void)
69 {
70   uint8_t temp, memsteststatus = 0x00;
71   uint8_t xdata, ydata = 0;
72   
73   /* MEMS configuration ------------------------------------------------------*/
74   /* Set configuration of LIS302DL*/
75   LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE;
76   LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100;
77   LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE;
78   LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3;
79   LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL;
80   LIS302DL_Init(&LIS302DL_InitStruct);
81   
82   /* Set configuration of Internal High Pass Filter of LIS302DL*/
83   LIS302DL_FilterStruct.HighPassFilter_Data_Selection = LIS302DL_FILTEREDDATASELECTION_OUTPUTREGISTER;
84   LIS302DL_FilterStruct.HighPassFilter_CutOff_Frequency = LIS302DL_HIGHPASSFILTER_LEVEL_1;
85   LIS302DL_FilterStruct.HighPassFilter_Interrupt = LIS302DL_HIGHPASSFILTERINTERRUPT_1_2;
86   LIS302DL_FilterConfig(&LIS302DL_FilterStruct);
87   
88   /* Required delay for the MEMS Accelerometre: Turn-on time = 3/Output data Rate 
89                                                              = 3/100 = 30ms */
90   Delay(30);
91   
92   /* Read WHO_AM_I register */
93   LIS302DL_Read(&temp, LIS302DL_WHO_AM_I_ADDR, 1);
94   
95   /* Check device identification register, this register should contains 
96   the device identifier that for LIS302DL is set to 0x3B */
97   if (temp != 0x3B)
98   {
99     Fail_Handler();
100   }
101
102   TimingDelay = 500;
103   /* Wait until detecting all MEMS direction or timeout */
104   while((memsteststatus == 0x00)&&(TimingDelay != 0x00))
105   {
106     LIS302DL_Read(Buffer, LIS302DL_OUT_X_ADDR, 4);
107     xdata = ABS((int8_t)(Buffer[0]));
108     ydata = ABS((int8_t)(Buffer[2]));
109     /* Check test PASS condition */   
110     if ((xdata > MEMS_PASSCONDITION) || (ydata > MEMS_PASSCONDITION)) 
111     {
112       /* MEMS Test PASS */
113       memsteststatus = 0x01;
114     }
115   }
116   
117   /* MEMS test status: PASS */ 
118   if(memsteststatus != 0x00)
119   {
120     /* Turn Green LED ON: signaling MEMS Test PASS */
121     STM_EVAL_LEDOn(LED4);
122     
123     /* Waiting User Button is pressed */
124     while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
125     {}
126     
127     /* Waiting User Button is Released */
128     while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)
129     {}
130     
131     /* Turn Green LED OFF: signaling the end of MEMS Test and switching to 
132        the next Sub Test */
133     STM_EVAL_LEDOff(LED4);
134   }
135   /* MEMS test status: Timeout occurs */
136   else
137   {
138     Fail_Handler();
139   }
140 }
141
142 /**
143   * @brief Test USB Hardware.
144   *   The main objectif of this test is to check the hardware connection of the 
145   *   Audio and USB peripheral.
146   * @param None
147   * @retval None
148   */
149 void USB_Test(void)
150 {
151   GPIO_InitTypeDef GPIO_InitStructure;
152
153   /******************************** USB Test **********************************/
154   
155   /*----------------- Part1: without cables connected ------------------------*/ 
156   
157   /* GPIOA, GPIOC and GPIOD clock enable */
158   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC | \
159                          RCC_AHB1Periph_GPIOD, ENABLE);
160   
161   /* GPIOD Configuration: Pins 5 in output push-pull */
162   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
163   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
164   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
165   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
166   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
167   GPIO_Init(GPIOD, &GPIO_InitStructure);
168   
169   /* Turn LED8 ON using PD5 */
170   GPIO_ResetBits(GPIOD, GPIO_Pin_5);
171   
172   /* GPIOC Configuration: Pin 0 in output push-pull */
173   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
174   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
175   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
176   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
177   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
178   GPIO_Init(GPIOC, &GPIO_InitStructure);
179   
180   /* GPIOA Configuration: Pin 9 in input pull-up */
181   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
182   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
183   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
184   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
185   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
186   GPIO_Init(GPIOA, &GPIO_InitStructure);
187   
188   /* Turn LED7 ON using PC0 (5v) */
189   GPIO_ResetBits(GPIOC, GPIO_Pin_0); 
190   
191   /* Waiting delay 10ms */
192   Delay(1);
193   
194   if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == Bit_RESET)
195   {
196     Fail_Handler();
197   }
198   
199   /* GPIOA Configuration: Pins 10 in output push-pull */
200   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
201   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
202   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
203   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
204   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
205   GPIO_Init(GPIOA, &GPIO_InitStructure);
206   
207   /* Waiting delay 10ms */
208   Delay(1);
209   
210   /* Check the ID level without cable connected */
211   if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10) == Bit_RESET)
212   {
213     Fail_Handler();
214   }
215   
216   /* Turn LED7 OFF using PC0 */
217   GPIO_SetBits(GPIOC, GPIO_Pin_0);  
218   
219   /* GPIOA Configuration: Pins 11, 12 in input pull-up */
220   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
221   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
222   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
223   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
224   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
225   GPIO_Init(GPIOA, &GPIO_InitStructure);
226   
227   /* GPIOA Configuration: Pin 9 in output push-pull */
228   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
229   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
230   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
231   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
232   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
233   GPIO_Init(GPIOA, &GPIO_InitStructure);
234   GPIO_ResetBits(GPIOA, GPIO_Pin_9);
235   
236   /* Waiting delay 10ms */
237   Delay(1);
238   
239   /* Check PA11 and PA12 level without cable connected */
240   if ((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_RESET) || \
241       (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == Bit_RESET))
242   {
243     Fail_Handler();
244   }
245   
246   /* GPIOA Configuration: Pins 12 in input pull-up */
247   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
248   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
249   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
250   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
251   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
252   GPIO_Init(GPIOA, &GPIO_InitStructure);
253   
254   /* GPIOA Configuration: Pin 11 in output push-pull */
255   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
256   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
257   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
258   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
259   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
260   GPIO_Init(GPIOA, &GPIO_InitStructure);
261   GPIO_ResetBits(GPIOA, GPIO_Pin_11);
262   
263   /* Waiting delay 10ms */
264   Delay(1);
265   
266   /* Check PA12 level without cable connected */
267   if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == Bit_RESET)
268   {
269     Fail_Handler();
270   }
271   
272   /* GPIOA Configuration: Pins 11 in input pull-up */
273   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
274   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
275   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
276   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
277   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
278   GPIO_Init(GPIOA, &GPIO_InitStructure);
279   
280   /* GPIOA Configuration: Pin 12 in output push-pull */
281   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
282   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
283   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
284   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
285   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
286   GPIO_Init(GPIOA, &GPIO_InitStructure);
287   GPIO_ResetBits(GPIOA, GPIO_Pin_12);
288   
289   /* Waiting delay 10ms */
290   Delay(1);
291   
292   /* Check PA12 level without cable connected */
293   if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_RESET)
294   {
295     Fail_Handler();
296   }
297   
298   /* GPIOA Configuration: Pins 9 in output push-pull */
299   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
300   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
301   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
302   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
303   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
304   GPIO_Init(GPIOA, &GPIO_InitStructure);
305   
306   /* Turn LED7 ON using PA9 */
307   GPIO_SetBits(GPIOA, GPIO_Pin_9);
308   
309   /* Turn Green LED ON: signaling Audio USB Test part1 PASS */
310   STM_EVAL_LEDOn(LED4);
311   
312   /* Waiting User Button is pressed */
313   while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
314   {}
315   
316   /* Waiting User Button is Released */
317   while (STM_EVAL_PBGetState(BUTTON_USER) != Bit_RESET)
318   {}
319   
320   /* Turn Green LED OFF: signaling the end of Audio USB Test part1 and switching to 
321   the part2 */
322   STM_EVAL_LEDOff(LED4);
323   
324   /* Turn LED7 OFF using PA9 */
325   GPIO_ResetBits(GPIOA, GPIO_Pin_9);
326   
327   /* Turn LED8 OFF using PD5 */
328   GPIO_SetBits(GPIOD, GPIO_Pin_5);
329   
330   /*--------------- Part2: with Audio USB cables connected  ------------------*/ 
331   
332   /*********************************** USB Test *******************************/
333   /* Check the ID level with cable connected */
334   if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_10) != Bit_RESET)
335   {
336     Fail_Handler();
337   }
338   
339   /* GPIOA Configuration: Pins 11, 12 in input pull-down */
340   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
341   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
342   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
343   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
344   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
345   GPIO_Init(GPIOA, &GPIO_InitStructure);
346   
347   /* GPIOA Configuration: Pin 9 in output push-pull */
348   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
349   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
350   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
351   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
352   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
353   GPIO_Init(GPIOA, &GPIO_InitStructure);
354   GPIO_SetBits(GPIOA, GPIO_Pin_9);
355   
356   /* Waiting delay 10ms */
357   Delay(1);
358   
359   /* Check PA11 and PA12 level with cable connected */
360   if ((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_RESET) || \
361       (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == Bit_RESET))
362   {
363     Fail_Handler();
364   }
365   
366   /* GPIOA Configuration: Pins 9, 12 in input pull-down */
367   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_12;
368   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
369   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
370   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
371   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
372   GPIO_Init(GPIOA, &GPIO_InitStructure);
373   
374   /* GPIOA Configuration: Pin 11 in output push-pull */
375   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
376   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
377   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
378   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
379   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
380   GPIO_Init(GPIOA, &GPIO_InitStructure);
381   GPIO_SetBits(GPIOA, GPIO_Pin_11);
382   
383   /* Waiting delay 10ms */
384   Delay(1);
385   
386   /* Check PA9 and PA12 level with cable connected */
387   if ((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == Bit_RESET)|| \
388       (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12) == Bit_RESET))
389   {
390     Fail_Handler();
391   }
392   
393   /* GPIOA Configuration: Pins 9, 11 in input pull-down */
394   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11;
395   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
396   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
397   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
398   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
399   GPIO_Init(GPIOA, &GPIO_InitStructure);
400   
401   /* GPIOA Configuration: Pin 12 in output push-pull */
402   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
403   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
404   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
405   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
406   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
407   GPIO_Init(GPIOA, &GPIO_InitStructure);
408   GPIO_SetBits(GPIOA, GPIO_Pin_12);
409   
410   /* Waiting delay 10ms */
411   Delay(1);
412   
413   /* Check PA9 and PA12 level with cable connected */
414   if ((GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_9) == Bit_RESET)|| \
415       (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_11) == Bit_RESET))
416   {
417     Fail_Handler();
418   }
419
420   /* GPIOA Configuration: Pins 11, 12 in input pull-down */
421   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
422   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
423   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
424   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
425   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
426   GPIO_Init(GPIOA, &GPIO_InitStructure);
427   
428   /* GPIOA Configuration: Pin 9 in output push-pull */
429   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
430   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
431   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
432   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
433   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
434   GPIO_Init(GPIOA, &GPIO_InitStructure);
435   
436   /* Turn LED7 OFF using PA9 */
437   GPIO_ResetBits(GPIOA, GPIO_Pin_9);
438 }
439
440 /**
441   * @brief Test Audio Hardware.
442   *   The main objectif of this test is to check the hardware connection of the 
443   *   Audio peripheral.
444   * @param  None
445   * @retval None
446   */
447 void Audio_Test(void)
448
449   GPIO_InitTypeDef  GPIO_InitStructure;  
450   uint8_t audioteststatus = 0x00;
451
452   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
453   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
454   
455   /* Set the current audio interface: I2S or DAC */
456   EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_I2S);
457   
458   /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOs...) */  
459   if (EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, 87, I2S_AudioFreq_48k) !=0)
460   {
461     Fail_Handler();
462   }
463   /* I2S code to be exectued under the I2S interrupt */
464   DACTest = 0;
465   
466   /* ADC Common Init */
467   ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
468   ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
469   ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
470   ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; 
471   ADC_CommonInit(&ADC_CommonInitStructure);
472   
473   /* ADC peripherals Init */
474   ADC_StructInit(&ADC_InitStructure);
475   ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
476   ADC_InitStructure.ADC_ScanConvMode = DISABLE;
477   ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
478   ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
479   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
480   ADC_InitStructure.ADC_NbrOfConversion = 1;
481   ADC_Init(ADC1, &ADC_InitStructure);
482
483   ADC_Init(ADC2, &ADC_InitStructure);
484   
485   /* Configure ADC Channels pin as analog input */
486   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
487   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
488   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
489   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
490   GPIO_Init(GPIOA, &GPIO_InitStructure);
491   
492   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ;
493   GPIO_Init(GPIOA, &GPIO_InitStructure);
494   
495   counter1 = 0;
496   counter0 = 0;
497   audioteststatus = 0;
498   /* ADCperipheral[PerIdx] Regular Channel Config */  
499   ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_56Cycles);
500   /* ADCperipheral[PerIdx] Regular Channel Config */  
501   ADC_RegularChannelConfig(ADC2, ADC_Channel_3, 1, ADC_SampleTime_56Cycles);
502   /* Enable ADC1 */
503   ADC_Cmd(ADC1, ENABLE);
504   ADC_Cmd(ADC2, ENABLE);
505  
506   TimingDelay = 500;
507   /* Wait until detecting 500 data*/
508   while((audioteststatus == 0)&&(TimingDelay != 0))
509   {
510     ADC_SoftwareStartConv(ADC1);
511     while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
512     ConvData1 = ADC_GetConversionValue(ADC1); 
513
514     ADC_SoftwareStartConv(ADC2);
515     while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
516     ConvData2 = ADC_GetConversionValue(ADC2);
517     
518     /* 1.75V equals to 150 */
519     if ((ConvData1 > 150) && (ConvData2 < 10) && (counter1 != 500))
520     {
521       counter1 ++;
522     }
523     if ((ConvData1 < 10) && (ConvData2 > 150) && (counter0 != 500))
524     {
525       counter0 ++;
526     }
527     if((counter1 == 500) && (counter0 == 500))
528     {
529       audioteststatus = 1; 
530     }
531   }
532   
533   /* Disable ADC Peripherals */ 
534   ADC_Cmd(ADC1, DISABLE);
535   ADC_Cmd(ADC2, DISABLE);
536   
537   /* Audio test status: FAIL */ 
538   if(audioteststatus == 0)
539   {
540     Fail_Handler();
541   }
542   
543   EVAL_AUDIO_DeInit();
544   EVAL_AUDIO_SetAudioInterface(AUDIO_INTERFACE_DAC);
545   /* Initialize the Audio codec and all related peripherals (I2S, I2C, IOs...) */  
546   if (EVAL_AUDIO_Init(OUTPUT_DEVICE_HEADPHONE, 100, I2S_AudioFreq_48k) !=0)
547   {
548     Fail_Handler();
549   }
550   
551   /* DAC code to be exectued under the I2S interrupt */
552   DACTest = 1;
553   counter1 = 0;
554   counter0 = 0;
555   audioteststatus = 0;
556   
557   /* Enable ADC1 */
558   ADC_Cmd(ADC1, ENABLE);
559   ADC_Cmd(ADC2, ENABLE);
560   
561   TimingDelay = 500;
562   /* Wait until detecting 50 data*/
563   while((audioteststatus == 0)&&(TimingDelay != 0))
564   {
565     
566     ADC_SoftwareStartConv(ADC1);
567     while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
568     
569     ConvData1 = ADC_GetConversionValue(ADC1); 
570     
571     ADC_SoftwareStartConv(ADC2);
572     while(ADC_GetFlagStatus(ADC2, ADC_FLAG_EOC) == RESET);
573     
574     ConvData2 = ADC_GetConversionValue(ADC2);
575     
576     /* 2.0V equals to 170 */
577     if ((ConvData1 > 170) && (ConvData2 > 170) &&(counter1 != 500))
578     {
579       counter1 ++;
580     }
581     if ((ConvData1 < 10) && (ConvData2 < 10) && (counter0 != 500))
582     {
583       counter0 ++;
584     }
585     if((counter1 == 500) && (counter0 == 500))
586     {
587       audioteststatus = 1; 
588     }
589   }
590   
591   /* Audio test status: FAIL */ 
592   if(audioteststatus == 0x00)
593   {
594     Fail_Handler();
595   }
596   
597   /* Turn Green LED ON: signaling Audio USB Test part2 PASS */
598   STM_EVAL_LEDOn(LED4);
599   
600   /* Waiting User_Button pressed */
601   while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET)
602   {}
603   
604   /* Turn Green LED OFF: signaling the end of Audio USB Test part2 */
605   STM_EVAL_LEDOff(LED4);
606 }
607
608 /**
609   * @brief Test Micophone MEMS Hardware.
610   *   The main objectif of this test is to check the hardware connection of the 
611   *   Microphone MEMS peripheral.
612   * @param None
613   * @retval None
614   */
615 void Microphone_MEMS_Test(void)
616 {
617   uint16_t data = 0x00;
618   uint8_t index = 0x00;
619   I2S_InitTypeDef  I2S_InitStructure;
620   GPIO_InitTypeDef GPIO_InitStructure;
621
622   /* Enable the SPI clock */
623   RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
624
625   /* Enable GPIO clocks */
626   RCC_AHB1PeriphClockCmd(SPI_SCK_GPIO_CLK | SPI_MOSI_GPIO_CLK, ENABLE);
627
628   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
629   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
630   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
631   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
632
633   /* SPI SCK pin configuration */
634   GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN;
635   GPIO_Init(SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
636   
637   /* Connect SPI pins to AF5 */  
638   GPIO_PinAFConfig(SPI_SCK_GPIO_PORT, SPI_SCK_SOURCE, SPI_SCK_AF);
639   
640   /* SPI MOSI pin configuration */
641   GPIO_InitStructure.GPIO_Pin =  SPI_MOSI_PIN;
642   GPIO_Init(SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
643   
644   GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF);
645   
646   /* I2S configuration -------------------------------------------------------*/
647   SPI_I2S_DeInit(SPI2);
648   I2S_InitStructure.I2S_AudioFreq = 64000;
649   I2S_InitStructure.I2S_Standard = I2S_Standard_MSB;
650   I2S_InitStructure.I2S_DataFormat = I2S_DataFormat_16b;
651   I2S_InitStructure.I2S_CPOL = I2S_CPOL_Low;
652   I2S_InitStructure.I2S_Mode = I2S_Mode_MasterRx;
653   I2S_InitStructure.I2S_MCLKOutput = I2S_MCLKOutput_Disable;
654   /* Initialize the I2S peripheral with the structure above */
655   I2S_Init(SPI2, &I2S_InitStructure);
656   
657   /* Enable the I2S peripheral */
658   I2S_Cmd(SPI2, ENABLE);
659   
660   /* Waiting until MEMS microphone ready : Wake-up Time */
661   Delay(10);
662   
663   TimingDelay = 500;
664   /* Wait until detect the click on the MEMS microphone or TimeOut delay*/
665   while((index < 30) && (TimingDelay != 0x00))
666   { 
667     /* Waiting RXNE Flag or TimeOut delay */
668     while((SPI_I2S_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET)&& (TimingDelay != 0x00))
669     {}
670     data = SPI_I2S_ReceiveData(SPI2);
671     if (data == 0xFFFF)
672     {
673       index++;  
674     }
675   }
676   
677   /* MEMS microphone test status: Timeout occurs */
678   if(index != 30)
679   {
680     Fail_Handler();
681   }
682 }
683
684 /*--------------------------------
685        Callbacks implementation:
686            the callbacks prototypes are defined in the stm324xg_eval_audio_codec.h file
687            and their implementation should be done in the user code if they are needed.
688            Below some examples of callback implementations.
689                                      --------------------------------------------------------*/
690 /**
691   * @brief  Calculates the remaining file size and new position of the pointer.
692   * @param  None
693   * @retval None
694   */
695 void EVAL_AUDIO_TransferComplete_CallBack(uint32_t pBuffer, uint32_t Size)
696 {
697   /* Calculate the remaining audio data in the file and the new size 
698      for the DMA transfer. If the Audio files size is less than the DMA max 
699      data transfer size, so there is no calculation to be done, just restart 
700      from the beginning of the file ... */
701   /* Check if the end of file has been reached */
702
703 }
704
705 /**
706   * @brief  Manages the DMA Half Transfer complete interrupt.
707   * @param  None
708   * @retval None
709   */
710 void EVAL_AUDIO_HalfTransfer_CallBack(uint32_t pBuffer, uint32_t Size)
711 {  
712 #ifdef AUDIO_MAL_MODE_CIRCULAR
713   
714   /* Display message on the LCD screen */
715   LCD_DisplayStringLine(Line8, " 1/2 Buffer Reached "); 
716   
717 #endif /* AUDIO_MAL_MODE_CIRCULAR */
718   
719   /* Generally this interrupt routine is used to load the buffer when 
720   a streaming scheme is used: When first Half buffer is already transferred load 
721   the new data to the first half of buffer while DMA is transferring data from 
722   the second half. And when Transfer complete occurs, load the second half of 
723   the buffer while the DMA is transferring from the first half ... */
724   /* 
725     ...........
726                    */
727 }
728 /**
729   * @brief  Get next data sample callback
730   * @param  None
731   * @retval Next data sample to be sent
732   */
733 uint16_t EVAL_AUDIO_GetSampleCallBack(void)
734 {
735   uint16_t data = 0;
736   
737   if (DACTest == 0)
738   {
739     if (Left_Right==0)
740     {
741       /* Get the next sample to be sent */
742       data = sinebuf[count++];
743       
744       if (count == 48)
745       {
746         count = 0x00;
747       }
748       Left_Right = 1;
749     }
750     else
751     {
752       /* Get the next sample to be sent */
753       data = sinebuf[count1++];
754       
755       if (count1 == 48)
756       {
757         count1 = 0x00;
758       }
759       Left_Right = 0;
760     }
761   }
762   else
763   {
764     /* Get the next sample to be sent */
765     data = 32768 + sinebuf[count++];
766
767     if (count == 48)
768     {
769       count = 0x00;
770     }
771   }
772   return data;
773 }
774
775
776 /**
777   * @brief  Manages the DMA FIFO error interrupt.
778   * @param  None
779   * @retval None
780   */
781 void EVAL_AUDIO_Error_CallBack(void* pData)
782 {
783   /* Stop the program with an infinite loop */
784   while (1)
785   {}
786   
787   /* could also generate a system reset to recover from the error */
788   /* .... */
789 }
790
791 #ifndef USE_DEFAULT_TIMEOUT_CALLBACK
792 /**
793   * @brief  Basic management of the timeout situation.
794   * @param  None.
795   * @retval None.
796   */
797 uint32_t Codec_TIMEOUT_UserCallback(void)
798 {   
799   /* Block communication and all processes */
800   while (1)
801   {   
802   }
803 }
804 #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
805 /*----------------------------------------------------------------------------*/
806
807
808 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/