1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - demos.TWL - spi - mic-3
3   File:     main.c
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2009-01-19#$
14   $Rev: 9884 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 /*---------------------------------------------------------------------------*
19     A sample that controls microphone sampling.
20 
21     USAGE:
22         UP, DOWN: Control sampling span.
23         LEFT, RIGHT: Control sampling bit range. ( 8bit or 12 bit etc. )
24         SEL , STA    : Control loop sampling enable or disable.
25 
26     HOWTO:
27         1. Initialize MIC library and SNDEX library.
28         2. Start automatic sampling of microphone by default configuration.
29         3. When you switch parameters, first, stop sampling. Then, if necessary,
30            switch I2S frequency. After that, edit parameters and start automatic
31            sampling again.
32  *---------------------------------------------------------------------------*/
33 
34 #include    <twl.h>
35 
36 /*---------------------------------------------------------------------------*
37     Constant Definitions
38  *---------------------------------------------------------------------------*/
39 #define     TEST_BUFFER_SIZE    (1024 * 1024)   // 1 Mbyte
40 #define     RETRY_MAX_COUNT     8
41 
42 /*---------------------------------------------------------------------------*
43     Internal Function Definitions
44  *---------------------------------------------------------------------------*/
45 static void     StartSampling(const MICAutoParam* param);
46 static void     StopSampling(void);
47 static void     AdjustSampling(u32 rate);
48 static SNDEXFrequency   GetI2SFrequency(void);
49 static void     SetI2SFrequency(SNDEXFrequency freq);
50 static void     SwitchI2SFrequency(void);
51 
52 static void     StepUpSamplingRate(void);
53 static void     StepDownSamplingRate(void);
54 static void     SetDrawData(void *address, MICSamplingType type);
55 static void     PrintfVariableData(void);
56 
57 static void     VBlankIntr(void);
58 static void     Init3D(void);
59 static void     Draw3D(void);
60 static void     DrawLine(s16 sx, s16 sy, s16 ex, s16 ey);
61 
62 /*---------------------------------------------------------------------------*
63     Internal Variable Definitions
64  *---------------------------------------------------------------------------*/
65 static MICAutoParam gMicAutoParam;
66 static u8           gDrawData[192];
67 static u8           gMicData[TEST_BUFFER_SIZE] ATTRIBUTE_ALIGN(HW_CACHE_LINE_SIZE);
68 
69 /*---------------------------------------------------------------------------*
70   Name:         TwlMain
71 
72   Description:  Initialization and main loop.
73 
74   Arguments:    None.
75 
76   Returns:      None.
77  *---------------------------------------------------------------------------*/
TwlMain(void)78 void TwlMain(void)
79 {
80     /* Various types of initialization */
81     OS_Init();
82     FX_Init();
83     GX_Init();
84     GX_DispOff();
85     GXS_DispOff();
86 
87     /* Initializes display settings */
88     GX_SetBankForLCDC(GX_VRAM_LCDC_ALL);
89     MI_CpuClearFast((void *)HW_LCDC_VRAM, HW_LCDC_VRAM_SIZE);
90     (void)GX_DisableBankForLCDC();
91     MI_CpuFillFast((void *)HW_OAM, 192, HW_OAM_SIZE);
92     MI_CpuClearFast((void *)HW_PLTT, HW_PLTT_SIZE);
93     MI_CpuFillFast((void *)HW_DB_OAM, 192, HW_DB_OAM_SIZE);
94     MI_CpuClearFast((void *)HW_DB_PLTT, HW_DB_PLTT_SIZE);
95 
96     /* 3D-related initialization */
97     Init3D();
98 
99     /* Interrupt settings */
100     OS_SetIrqFunction(OS_IE_V_BLANK, VBlankIntr);
101     (void)OS_EnableIrqMask(OS_IE_V_BLANK);
102     (void)OS_EnableIrq();
103     (void)OS_EnableInterrupts();
104     (void)GX_VBlankIntr(TRUE);
105 
106     /* Initialize related libraries*/
107     {
108         (void)PM_SetAmp(PM_AMP_ON);
109         SNDEX_Init();
110         MIC_Init();
111 
112         gMicAutoParam.type      =   MIC_SAMPLING_TYPE_8BIT;
113         gMicAutoParam.buffer    =   (void *)gMicData;
114         gMicAutoParam.size      =   TEST_BUFFER_SIZE;
115         if ((OS_IsRunOnTwl() == TRUE) && (GetI2SFrequency() == SNDEX_FREQUENCY_47610))
116         {
117             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_23810;
118         }
119         else
120         {
121             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_16360;
122         }
123         gMicAutoParam.loop_enable   =   TRUE;
124         gMicAutoParam.full_callback =   NULL;
125         StartSampling((const MICAutoParam*)&gMicAutoParam);
126     }
127 
128     /* Initialize internal variables */
129     MI_CpuFill8(gDrawData, 0x80, sizeof(gDrawData));
130 
131     /* LCD display start */
132     GX_DispOn();
133     GXS_DispOn();
134 
135     /* Debug string output */
136     OS_Printf("mic-3 demo started.\n");
137     OS_Printf("   up/down      -> change sampling rate\n");
138     OS_Printf("   left/right   -> change bit range\n");
139     OS_Printf("   start/select -> change loop setting\n");
140     OS_Printf("\n");
141     PrintfVariableData();
142 
143     /* Empty call for getting key input data (strategy for pressing A Button in the IPL) */
144     (void)PAD_Read();
145 
146     {
147         u16     keyOld  =   0;
148         u16     keyTrg;
149         u16     keyNow;
150 
151         /* Main loop */
152         while (TRUE)
153         {
154             /* Get key input data */
155             keyNow  =   PAD_Read();
156             keyTrg  =   (u16)((keyOld ^ keyNow) & keyNow);
157             keyOld  =   keyNow;
158 
159             /* Change sampling type (bit width) */
160             if (keyTrg & PAD_KEY_LEFT)
161             {
162                 StopSampling();
163                 gMicAutoParam.type
164                     =   (MICSamplingType)((gMicAutoParam.type + MIC_SAMPLING_TYPE_MAX - 1) % MIC_SAMPLING_TYPE_MAX);
165                 StartSampling((const MICAutoParam*)&gMicAutoParam);
166                 PrintfVariableData();
167             }
168             if (keyTrg & PAD_KEY_RIGHT)
169             {
170                 StopSampling();
171                 gMicAutoParam.type
172                         =   (MICSamplingType)((gMicAutoParam.type + 1) % MIC_SAMPLING_TYPE_MAX);
173                 StartSampling((const MICAutoParam*)&gMicAutoParam);
174                 PrintfVariableData();
175             }
176 
177             /* Change sampling rate */
178             if (keyTrg & PAD_KEY_UP)
179             {
180                 StepUpSamplingRate();
181                 PrintfVariableData();
182             }
183             if (keyTrg & PAD_KEY_DOWN)
184             {
185                 StepDownSamplingRate();
186                 PrintfVariableData();
187             }
188 
189             /* Change loop availability when buffer is full*/
190             if (keyTrg & (PAD_BUTTON_SELECT | PAD_BUTTON_START))
191             {
192                 StopSampling();
193                 gMicAutoParam.loop_enable = (gMicAutoParam.loop_enable + 1) % 2;
194                 StartSampling((const MICAutoParam*)&gMicAutoParam);
195                 PrintfVariableData();
196             }
197 
198             /* Render waveform */
199             SetDrawData(MIC_GetLastSamplingAddress(), gMicAutoParam.type);
200             Draw3D();
201 
202             /* Waiting for the V-Blank */
203             OS_WaitVBlankIntr();
204         }
205     }
206 }
207 
208 /*---------------------------------------------------------------------------*
209   Name:         StartSampling
210 
211   Description:  Calls the MIC_StartLimitedSampling function and performs error processing.
212 
213   Arguments:    param: Parameter passed to the microphone functions
214 
215   Returns:      None.
216  *---------------------------------------------------------------------------*/
217 static void
StartSampling(const MICAutoParam * param)218 StartSampling(const MICAutoParam* param)
219 {
220     MICResult   result;
221     s32         retry   =   0;
222 
223     while (TRUE)
224     {
225         result  =   MIC_StartLimitedSampling(param);
226         switch (result)
227         {
228         case MIC_RESULT_SUCCESS:            // Success
229             return;
230         case MIC_RESULT_BUSY:               // Microphone in use by other thread
231         case MIC_RESULT_SEND_ERROR:         // PXI queue is full
232             if (++ retry <= RETRY_MAX_COUNT)
233             {
234                 OS_Sleep(1);
235                 continue;
236             }
237             OS_TWarning("Retry count overflow.\n");
238             return;
239         case MIC_RESULT_ILLEGAL_STATUS:     // Not in an auto-sampling stopped state.
240             OS_TWarning("Already started sampling.\n");
241             return;
242         case MIC_RESULT_ILLEGAL_PARAMETER:  // The specified parameter is not supported
243             OS_TWarning("Illegal parameter to start automatic sampling.\n");
244             return;
245         default:                            // Other fatal error
246             OS_Panic("Fatal error (%d).\n", result);
247             /* Never return */
248         }
249     }
250 }
251 
252 /*---------------------------------------------------------------------------*
253   Name:         StopSampling
254 
255   Description:  Calls the MIC_StopLimitedSampling function and performs error processing.
256 
257   Arguments:    None.
258 
259   Returns:      None.
260  *---------------------------------------------------------------------------*/
261 static void
StopSampling(void)262 StopSampling(void)
263 {
264     MICResult   result;
265     s32         retry   =   0;
266 
267     while (TRUE)
268     {
269         result  =   MIC_StopLimitedSampling();
270         switch (result)
271         {
272         case MIC_RESULT_SUCCESS:            // Success
273         case MIC_RESULT_ILLEGAL_STATUS:     // Not in an auto-sampling state.
274             return;
275         case MIC_RESULT_BUSY:               // Microphone in use by other thread
276         case MIC_RESULT_SEND_ERROR:         // PXI queue is full
277             if (++ retry <= RETRY_MAX_COUNT)
278             {
279                 OS_Sleep(1);
280                 continue;
281             }
282             OS_TWarning("Retry count overflow.\n");
283             return;
284         default:                            // Other fatal error
285             OS_Panic("Fatal error (%d).\n", result);
286             /* Never return */
287         }
288     }
289 }
290 
291 /*---------------------------------------------------------------------------*
292   Name:         AdjustSampling
293 
294   Description:  Calls the MIC_AdjustLimitedSampling function and performs error processing.
295 
296   Arguments:    rate: Sampling period
297 
298   Returns:      None.
299  *---------------------------------------------------------------------------*/
300 static void
AdjustSampling(u32 rate)301 AdjustSampling(u32 rate)
302 {
303     MICResult   result;
304     s32         retry   =   0;
305 
306     while (TRUE)
307     {
308         result  =   MIC_AdjustLimitedSampling(rate);
309         switch (result)
310         {
311         case MIC_RESULT_SUCCESS:            // Success
312         case MIC_RESULT_ILLEGAL_STATUS:     // Not in an autosampling state
313             return;
314         case MIC_RESULT_BUSY:               // Microphone in use by other thread
315         case MIC_RESULT_SEND_ERROR:         // PXI queue is full
316             if (++ retry <= RETRY_MAX_COUNT)
317             {
318                 OS_Sleep(1);
319                 continue;
320             }
321             OS_TWarning("Retry count overflow.\n");
322             return;
323         case MIC_RESULT_ILLEGAL_PARAMETER:  // The specified parameter is not supported
324             OS_TWarning("Illegal parameter to adjust automatic sampling rate.\n");
325             return;
326         default:                            // Other fatal error
327             OS_Panic("Fatal error (%d).\n", result);
328             /* Never return */
329         }
330     }
331 }
332 
333 #include <twl/ltdmain_begin.h>
334 /*---------------------------------------------------------------------------*
335   Name:         GetI2SFrequency
336 
337   Description:  Gets the I2S operating frequency.
338 
339   Arguments:    None.
340 
341   Returns:      SDNEXFrequency: Returns the I2S operating frequency.
342  *---------------------------------------------------------------------------*/
343 static SNDEXFrequency
GetI2SFrequency(void)344 GetI2SFrequency(void)
345 {
346     SNDEXResult     result;
347     SNDEXFrequency  freq;
348     s32             retry   =   0;
349 
350     while (TRUE)
351     {
352         result  =   SNDEX_GetI2SFrequency(&freq);
353         switch (result)
354         {
355         case SNDEX_RESULT_SUCCESS:          // Success
356             return freq;
357         case SNDEX_RESULT_EXCLUSIVE:        // Exclusion error
358         case SNDEX_RESULT_PXI_SEND_ERROR:   // PXI queue is full
359             if (++ retry <= RETRY_MAX_COUNT)
360             {
361                 OS_Sleep(1);
362                 continue;
363             }
364             OS_TWarning("Retry count overflow.\n");
365             break;
366         case SNDEX_RESULT_BEFORE_INIT:      // Pre-initialization
367         case SNDEX_RESULT_ILLEGAL_STATE:    // Abnormal state
368             OS_TWarning("Illegal state to get I2S frequency.\n");
369             break;
370         case SNDEX_RESULT_FATAL_ERROR:      // Fatal error
371         default:
372             OS_Panic("Fatal error (%d).\n", result);
373             /* Never return */
374         }
375     }
376     return SNDEX_FREQUENCY_32730;
377 }
378 
379 /*---------------------------------------------------------------------------*
380   Name:         SetI2SFrequency
381 
382   Description:  Changes the I2S operating frequency.
383 
384   Arguments:    freq: I2S operating frequency
385 
386   Returns:      None.
387  *---------------------------------------------------------------------------*/
388 static void
SetI2SFrequency(SNDEXFrequency freq)389 SetI2SFrequency(SNDEXFrequency freq)
390 {
391     SNDEXResult     result;
392     s32             retry   =   0;
393 
394     while (TRUE)
395     {
396         result  =   SNDEX_SetI2SFrequency(freq);
397         switch (result)
398         {
399         case SNDEX_RESULT_SUCCESS:          // Success
400             return;
401         case SNDEX_RESULT_EXCLUSIVE:        // Exclusion error
402         case SNDEX_RESULT_PXI_SEND_ERROR:   // PXI queue is full
403             if (++ retry <= RETRY_MAX_COUNT)
404             {
405                 OS_Sleep(1);
406                 continue;
407             }
408             OS_TWarning("Retry count overflow.\n");
409             return;
410         case SNDEX_RESULT_BEFORE_INIT:      // Pre-initialization
411         case SNDEX_RESULT_ILLEGAL_STATE:    // Abnormal state
412             OS_TWarning("Illegal state to set I2S frequency.\n");
413             return;
414         case SNDEX_RESULT_INVALID_PARAM:    // Abnormal argument
415             OS_TWarning("Could not set I2S frequency into %d.\n", freq);
416             return;
417         case SNDEX_RESULT_FATAL_ERROR:      // Fatal error
418         default:
419             OS_Panic("Fatal error (%d)\n", result);
420             /* Never return */
421         }
422     }
423 }
424 
425 /*---------------------------------------------------------------------------*
426   Name:         SwitchI2SFrequency
427 
428   Description:  Changes the I2S operating frequency in conjunction with the microphone sampling frequency.
429 
430   Arguments:    None.
431 
432   Returns:      None.
433  *---------------------------------------------------------------------------*/
434 static void
SwitchI2SFrequency(void)435 SwitchI2SFrequency(void)
436 {
437     SNDEXFrequency  freq;
438 
439     freq    =   GetI2SFrequency();
440     switch (gMicAutoParam.rate)
441     {
442     case MIC_SAMPLING_RATE_8180:
443     case MIC_SAMPLING_RATE_10910:
444     case MIC_SAMPLING_RATE_16360:
445     case MIC_SAMPLING_RATE_32730:
446         if (freq != SNDEX_FREQUENCY_32730)
447         {
448             SetI2SFrequency(SNDEX_FREQUENCY_32730);
449         }
450         break;
451     case MIC_SAMPLING_RATE_11900:
452     case MIC_SAMPLING_RATE_15870:
453     case MIC_SAMPLING_RATE_23810:
454     case MIC_SAMPLING_RATE_47610:
455         if (freq != SNDEX_FREQUENCY_47610)
456         {
457             SetI2SFrequency(SNDEX_FREQUENCY_47610);
458         }
459         break;
460     }
461 }
462 
463 #include <twl/ltdmain_end.h>
464 
465 /*---------------------------------------------------------------------------*
466   Name:         StepUpSamplingRate
467 
468   Description:  Increases the microphone sampling frequency by one level.
469 
470   Arguments:    None.
471 
472   Returns:      None.
473  *---------------------------------------------------------------------------*/
474 static void
StepUpSamplingRate(void)475 StepUpSamplingRate(void)
476 {
477     if (OS_IsRunOnTwl() == TRUE)
478     {
479         switch (gMicAutoParam.rate)
480         {
481         case MIC_SAMPLING_RATE_8180:    // 32.73k / 4
482             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_10910;
483             break;
484         case MIC_SAMPLING_RATE_10910:   // 32.73k / 3
485             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_11900;
486             break;
487         case MIC_SAMPLING_RATE_11900:   // 47.61k / 4
488             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_15870;
489             break;
490         case MIC_SAMPLING_RATE_15870:   // 47.61k / 3
491             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_16360;
492             break;
493         case MIC_SAMPLING_RATE_16360:   // 32.73k / 2
494             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_23810;
495             break;
496         case MIC_SAMPLING_RATE_23810:   // 47.61k / 2
497             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_32730;
498             break;
499         case MIC_SAMPLING_RATE_32730:   // 32.73k / 1
500             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_47610;
501             break;
502         case MIC_SAMPLING_RATE_47610:   // 47.61k / 1
503         default:
504             /* Do nothing */
505             return;
506         }
507         StopSampling();
508         SwitchI2SFrequency();
509         StartSampling((const MICAutoParam*)&gMicAutoParam);
510     }
511     else
512     {
513         switch (gMicAutoParam.rate)
514         {
515         case MIC_SAMPLING_RATE_8180:    // 32.73k / 4
516             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_10910;
517             break;
518         case MIC_SAMPLING_RATE_10910:   // 32.73k / 3
519             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_11900;
520             break;
521         case MIC_SAMPLING_RATE_11900:   // 47.61k / 4
522             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_15870;
523             break;
524         case MIC_SAMPLING_RATE_15870:   // 47.61k / 3
525             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_16360;
526             break;
527         case MIC_SAMPLING_RATE_16360:   // 32.73k / 2
528             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_23810;
529             break;
530         case MIC_SAMPLING_RATE_23810:   // 47.61k / 2
531         default:
532             /* Do nothing */
533             return;
534         }
535         AdjustSampling(gMicAutoParam.rate);
536     }
537 }
538 
539 /*---------------------------------------------------------------------------*
540   Name:         StepUpSamplingRate
541 
542   Description:  Decreases the microphone sampling frequency by one level.
543 
544   Arguments:    None.
545 
546   Returns:      None.
547  *---------------------------------------------------------------------------*/
548 static void
StepDownSamplingRate(void)549 StepDownSamplingRate(void)
550 {
551     if (OS_IsRunOnTwl() == TRUE)
552     {
553         switch (gMicAutoParam.rate)
554         {
555         case MIC_SAMPLING_RATE_47610:   // 47.61k / 1
556             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_32730;
557             break;
558         case MIC_SAMPLING_RATE_32730:   // 32.73k / 1
559             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_23810;
560             break;
561         case MIC_SAMPLING_RATE_23810:   // 47.61k / 2
562             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_16360;
563             break;
564         case MIC_SAMPLING_RATE_16360:   // 32.73k / 2
565             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_15870;
566             break;
567         case MIC_SAMPLING_RATE_15870:   // 47.61k / 3
568             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_11900;
569             break;
570         case MIC_SAMPLING_RATE_11900:   // 47.61k / 4
571             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_10910;
572             break;
573         case MIC_SAMPLING_RATE_10910:   // 32.73k / 3
574             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_8180;
575             break;
576         case MIC_SAMPLING_RATE_8180:    // 32.73k / 4
577         default:
578             /* Do nothing */
579             return;
580         }
581         StopSampling();
582         SwitchI2SFrequency();
583         StartSampling((const MICAutoParam*)&gMicAutoParam);
584     }
585     else
586     {
587         switch (gMicAutoParam.rate)
588         {
589         case MIC_SAMPLING_RATE_23810:   // 47.61k / 2
590             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_16360;
591             break;
592         case MIC_SAMPLING_RATE_16360:   // 32.73k / 2
593             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_15870;
594             break;
595         case MIC_SAMPLING_RATE_15870:   // 47.61k / 3
596             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_11900;
597             break;
598         case MIC_SAMPLING_RATE_11900:   // 47.61k / 4
599             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_10910;
600             break;
601         case MIC_SAMPLING_RATE_10910:   // 32.73k / 3
602             gMicAutoParam.rate  =   MIC_SAMPLING_RATE_8180;
603             break;
604         case MIC_SAMPLING_RATE_8180:    // 32.73k / 4
605         default:
606             /* Do nothing */
607             return;
608         }
609         AdjustSampling(gMicAutoParam.rate);
610     }
611 }
612 
613 /*---------------------------------------------------------------------------*
614   Name:         SetDrawData
615 
616   Description:  Stores the current newest sampled data in the buffer that puts it on the display.
617 
618 
619   Arguments:    address: Location in main memory where the most recent sampling data was stored by the component.
620 
621                 type: Sampling type (bit width)
622 
623   Returns:      None.
624  *---------------------------------------------------------------------------*/
SetDrawData(void * address,MICSamplingType type)625 static void SetDrawData(void *address, MICSamplingType type)
626 {
627     s32     i;
628 
629     /* If sampling has never been performed, do nothing and stop.
630        (Because it would delete memory cache(s) unrelated to the microphone) */
631     if ((address < gMicData) || (address >= (gMicData + TEST_BUFFER_SIZE)))
632     {
633         return;
634     }
635 
636     switch (type)
637     {
638     case MIC_SAMPLING_TYPE_8BIT:
639     case MIC_SAMPLING_TYPE_SIGNED_8BIT:
640         /* In the case of 8-bit sampling */
641         {
642             u8     *p;
643 
644             p = (u8 *)((u32)address - 191);
645             if (p < gMicData)
646             {
647                 p = (u8 *)((u32)p + TEST_BUFFER_SIZE);
648             }
649             DC_InvalidateRange((void *)((u32)p & ~(HW_CACHE_LINE_SIZE - 1)), HW_CACHE_LINE_SIZE);
650             for (i = 0; i < 192; i++)
651             {
652                 gDrawData[i] = *p;
653                 p++;
654                 if ((u32)p >= (u32)(gMicData + TEST_BUFFER_SIZE))
655                 {
656                     p -= TEST_BUFFER_SIZE;
657                 }
658                 if (((u32)p % HW_CACHE_LINE_SIZE) == 0)
659                 {
660                     DC_InvalidateRange(p, HW_CACHE_LINE_SIZE);
661                 }
662             }
663         }
664         break;
665     case MIC_SAMPLING_TYPE_12BIT:
666     case MIC_SAMPLING_TYPE_SIGNED_12BIT:
667     case MIC_SAMPLING_TYPE_12BIT_FILTER_OFF:
668     case MIC_SAMPLING_TYPE_SIGNED_12BIT_FILTER_OFF:
669         /* For 12-bit sampling*/
670         {
671             u16    *p;
672 
673             p = (u16 *)((u32)address - 382);
674             if ((u32)p < (u32)gMicData)
675             {
676                 p = (u16 *)((u32)p + TEST_BUFFER_SIZE);
677             }
678             DC_InvalidateRange((void *)((u32)p & ~(HW_CACHE_LINE_SIZE - 1)), HW_CACHE_LINE_SIZE);
679             for (i = 0; i < 192; i++)
680             {
681                 gDrawData[i] = (u8)((*p >> 8) & 0x00ff);
682                 p++;
683                 if ((u32)p >= (u32)(gMicData + TEST_BUFFER_SIZE))
684                 {
685                     p = (u16 *)((u32)p - TEST_BUFFER_SIZE);
686                 }
687                 if (((u32)p % HW_CACHE_LINE_SIZE) == 0)
688                 {
689                     DC_InvalidateRange(p, HW_CACHE_LINE_SIZE);
690                 }
691             }
692         }
693         break;
694     }
695 }
696 
697 /*---------------------------------------------------------------------------*
698   Name:         PrintfVariableData
699 
700   Description:  Print-output the variable sampling settings.
701 
702   Arguments:    None.
703 
704   Returns:      None.
705  *---------------------------------------------------------------------------*/
706 static const char* bitRangeString[MIC_SAMPLING_TYPE_MAX] =
707 {
708     "unsigned 8",
709     "unsigned 12",
710     "signed 8",
711     "signed 12",
712     "unsigned 12 (filter off)",
713     "signed 12 (filter off)"
714 };
715 
716 /*---------------------------------------------------------------------------*/
PrintfVariableData(void)717 static void PrintfVariableData(void)
718 {
719     s32     range = 0;
720 
721     OS_Printf(" sampling-rate: ");
722     switch (gMicAutoParam.rate)
723     {
724     case MIC_SAMPLING_RATE_32730:   OS_Printf("32.73kHz");  break;
725     case MIC_SAMPLING_RATE_16360:   OS_Printf("16.36kHz");  break;
726     case MIC_SAMPLING_RATE_10910:   OS_Printf("10.91kHz");  break;
727     case MIC_SAMPLING_RATE_8180:    OS_Printf(" 8.18kHz");  break;
728     case MIC_SAMPLING_RATE_47610:   OS_Printf("47.61kHz");  break;
729     case MIC_SAMPLING_RATE_23810:   OS_Printf("23.81kHz");  break;
730     case MIC_SAMPLING_RATE_15870:   OS_Printf("15.87kHz");  break;
731     case MIC_SAMPLING_RATE_11900:   OS_Printf("11.90kHz");  break;
732     }
733 
734     OS_Printf(" , loop: %s", (gMicAutoParam.loop_enable ? " on" : "off"));
735 
736     OS_Printf(" , bit-range: %s\n",
737             bitRangeString[gMicAutoParam.type % MIC_SAMPLING_TYPE_MAX]);
738 }
739 
740 /*---------------------------------------------------------------------------*
741   Name:         VBlankIntr
742 
743   Description:  V-Blank interrupt vector.
744 
745   Arguments:    None.
746 
747   Returns:      None.
748  *---------------------------------------------------------------------------*/
VBlankIntr(void)749 static void VBlankIntr(void)
750 {
751     /* Sets the IRQ check flag */
752     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
753 }
754 
755 /*---------------------------------------------------------------------------*
756   Name:         Init3D
757 
758   Description:  Initialization for 3D display.
759 
760   Arguments:    None.
761 
762   Returns:      None.
763  *---------------------------------------------------------------------------*/
Init3D(void)764 static void Init3D(void)
765 {
766     G3X_Init();
767     G3X_InitMtxStack();
768     GX_SetGraphicsMode(GX_DISPMODE_GRAPHICS, GX_BGMODE_0, GX_BG0_AS_3D);
769     GX_SetVisiblePlane(GX_PLANEMASK_BG0);
770     G2_SetBG0Priority(0);
771     G3X_AlphaTest(FALSE, 0);
772     G3X_AntiAlias(TRUE);
773     G3X_EdgeMarking(FALSE);
774     G3X_SetFog(FALSE, (GXFogBlend)0, (GXFogSlope)0, 0);
775     G3X_SetClearColor(0, 0, 0x7fff, 63, FALSE);
776     G3_ViewPort(0, 0, 255, 191);
777     G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
778 }
779 
780 /*---------------------------------------------------------------------------*
781   Name:         Draw3D
782 
783   Description:  Displays waveforms in 3D.
784 
785   Arguments:    None.
786 
787   Returns:      None.
788  *---------------------------------------------------------------------------*/
Draw3D(void)789 static void Draw3D(void)
790 {
791     G3X_Reset();
792     G3_Identity();
793     G3_PolygonAttr(GX_LIGHTMASK_NONE, GX_POLYGONMODE_MODULATE, GX_CULL_NONE, 0, 31, 0);
794 
795     {
796         s32     i;
797 
798         if ((gMicAutoParam.type == MIC_SAMPLING_TYPE_SIGNED_8BIT) ||
799             (gMicAutoParam.type == MIC_SAMPLING_TYPE_SIGNED_12BIT) ||
800             (gMicAutoParam.type == MIC_SAMPLING_TYPE_SIGNED_12BIT_FILTER_OFF))
801         {
802             for (i = 0; i < 191; i++)
803             {
804                 DrawLine((s16)((s8)gDrawData[i]),
805                          (s16)i, (s16)((s8)gDrawData[i + 1]), (s16)(i + 1));
806             }
807         }
808         else
809         {
810             for (i = 0; i < 191; i++)
811             {
812                 DrawLine((s16)(gDrawData[i]), (s16)i, (s16)(gDrawData[i + 1]), (s16)(i + 1));
813             }
814         }
815     }
816 
817     G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
818 }
819 
820 /*---------------------------------------------------------------------------*
821   Name:         DrawLine
822 
823   Description:  Renders lines with triangular polygons.
824 
825   Arguments:    sx:   X-coordinate of line's starting point
826                 sy:   Y-coordinate of line's starting point
827                 ex:   X-coordinate of line's ending point
828                 ey:   Y-coordinate of line's ending point
829 
830   Returns:      None.
831  *---------------------------------------------------------------------------*/
DrawLine(s16 sx,s16 sy,s16 ex,s16 ey)832 static void DrawLine(s16 sx, s16 sy, s16 ex, s16 ey)
833 {
834     fx16    fsx = (fx16)(((sx - 128) * 0x1000) / 128);
835     fx16    fsy = (fx16)(((96 - sy) * 0x1000) / 96);
836     fx16    fex = (fx16)(((ex - 128) * 0x1000) / 128);
837     fx16    fey = (fx16)(((96 - ey) * 0x1000) / 96);
838 
839     G3_Begin(GX_BEGIN_TRIANGLES);
840     {
841         G3_Color(GX_RGB(31, 31, 31));
842         G3_Vtx(fsx, fsy, 0);
843         G3_Color(GX_RGB(31, 31, 31));
844         G3_Vtx(fex, fey, 0);
845         G3_Color(GX_RGB(31, 31, 31));
846         G3_Vtx(fsx, fsy, 1);
847     }
848     G3_End();
849 }
850 
851 /*---------------------------------------------------------------------------*
852   End of file
853  *---------------------------------------------------------------------------*/
854