1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - library - dsp
3   File:     dsp_g711.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:: 2008-11-21#$
14   $Rev: 9387 $
15   $Author: kitase_hirotake $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include <twl/dsp.h>
20 
21 #include <twl/dsp/common/pipe.h>
22 #include <twl/dsp/common/g711.h>
23 
24 #include "dsp_process.h"
25 
26 
27 /*---------------------------------------------------------------------------*/
28 /* Variables */
29 
30 static BOOL DSPiG711Available = FALSE;
31 static u32  DSPiG711CommandSend = 0;
32 static u32  DSPiG711CommandRecv = 0;
33 static DSPProcessContext DSPiProcessG711[1];
34 
35 
36 /*---------------------------------------------------------------------------*/
37 /* Functions */
38 
39 /*---------------------------------------------------------------------------*
40   Name:         DSPi_OpenStaticComponentG711Core
41 
42   Description:  Open the memory file for the G.711 component.
43                 It is no longer necessary to prepare a file system in advance. Because it is linked as static memory, the program size increases.
44 
45 
46   Arguments:    file: FSFile structure that opens the memory file
47 
48   Returns:      None.
49  *---------------------------------------------------------------------------*/
DSPi_OpenStaticComponentG711Core(FSFile * file)50 void DSPi_OpenStaticComponentG711Core(FSFile *file)
51 {
52     extern const u8 DSPiFirmware_audio[];
53     (void)DSPi_CreateMemoryFile(file, DSPiFirmware_audio);
54 }
55 
56 /*---------------------------------------------------------------------------*
57   Name:         DSPi_SendCodecG711
58 
59   Description:  Issues command to the the G.711 codec.
60 
61   Arguments:    dst: Conversion destination buffer. (Pack stream for A-law or u-law)
62                 src: Conversion source buffer. (PCM 8kHz * 8bits)
63                 len: Conversion sample count
64                 flags: Bit set for codec and conversion direction
65 
66   Returns:      None.
67  *---------------------------------------------------------------------------*/
DSPi_SendCodecG711(void * dst,const void * src,u32 len,DSPAudioCodecMode flags)68 static void DSPi_SendCodecG711(void *dst, const void *src, u32 len, DSPAudioCodecMode flags)
69 {
70     DSPAudioCodecCommand    command;
71     command.ctrl = DSP_32BIT_TO_DSP(flags);
72     command.src = DSP_32BIT_TO_DSP((DSPAddrInARM)src);
73     command.dst = DSP_32BIT_TO_DSP((DSPAddrInARM)dst);
74     command.len = DSP_32BIT_TO_DSP(len);
75     DSP_WriteProcessPipe(DSPiProcessG711, DSP_PIPE_BINARY, &command, sizeof(command));
76 
77     ++DSPiG711CommandSend;
78 }
79 
80 /*---------------------------------------------------------------------------*
81   Name:         DSPi_LoadG711Core
82 
83   Description:  Load the component that supports the G.711 encoding format to DSP
84 
85   Arguments:    file: G.711 component file
86                 slotB: WRAM-B allowed for use for code memory
87                 slotC: WRAM-C allowed for use for data memory
88 
89   Returns:      TRUE if the G.711 component is correctly loaded to DSP.
90  *---------------------------------------------------------------------------*/
DSPi_LoadG711Core(FSFile * file,int slotB,int slotC)91 BOOL DSPi_LoadG711Core(FSFile *file, int slotB, int slotC)
92 {
93     if (!DSPiG711Available)
94     {
95         DSP_InitProcessContext(DSPiProcessG711, "g711");
96         if (DSP_ExecuteProcess(DSPiProcessG711, file, slotB, slotC))
97         {
98             DSPiG711Available = TRUE;
99         }
100     }
101     return DSPiG711Available;
102 }
103 
104 /*---------------------------------------------------------------------------*
105   Name:         DSPi_UnloadG711Core
106 
107   Description:  Unloads the component that supports the G.711 encoding format from DSP.
108 
109   Arguments:    None.
110 
111   Returns:      None.
112  *---------------------------------------------------------------------------*/
DSPi_UnloadG711Core(void)113 void DSPi_UnloadG711Core(void)
114 {
115     if (DSPiG711Available)
116     {
117         DSP_QuitProcess(DSPiProcessG711);
118         DSPiG711Available = FALSE;
119     }
120 }
121 
122 /*---------------------------------------------------------------------------*
123   Name:         DSPi_EncodeG711Core
124 
125   Description:  G.711 encoding.
126 
127   Arguments:    dst: Conversion destination buffer. (Pack stream for A-law or u-law)
128                 src: Conversion source buffer. (PCM 8kHz * 8bits)
129                 len: Conversion sample count
130                 mode: Codec type
131 
132   Returns:      None.
133  *---------------------------------------------------------------------------*/
DSPi_EncodeG711Core(void * dst,const void * src,u32 len,DSPAudioCodecMode mode)134 void DSPi_EncodeG711Core(void *dst, const void *src, u32 len, DSPAudioCodecMode mode)
135 {
136     DSPi_SendCodecG711(dst, src, len,
137                        (DSPAudioCodecMode)(DSP_AUDIO_CODEC_TYPE_ENCODE |
138                                            (mode & DSP_AUDIO_CODEC_MODE_MASK)));
139 }
140 
141 /*---------------------------------------------------------------------------*
142   Name:         DSPi_DecodeG711Core
143 
144   Description:  G.711 decoding.
145 
146   Arguments:    dst: Conversion destination buffer. (PCM 8kHz * 8bits)
147                 src: Conversion source buffer. (Pack stream for A-law or u-law)
148                 len: Conversion sample count
149                 mode: Codec type
150 
151   Returns:      None.
152  *---------------------------------------------------------------------------*/
DSPi_DecodeG711Core(void * dst,const void * src,u32 len,DSPAudioCodecMode mode)153 void DSPi_DecodeG711Core(void *dst, const void *src, u32 len, DSPAudioCodecMode mode)
154 {
155     DSPi_SendCodecG711(dst, src, len,
156                        (DSPAudioCodecMode)(DSP_AUDIO_CODEC_TYPE_DECODE |
157                                            (mode & DSP_AUDIO_CODEC_MODE_MASK)));
158 }
159 
160 /*---------------------------------------------------------------------------*
161   Name:         DSPi_TryWaitForG711Core
162 
163   Description:  Determines whether the command that was first issued is completed.
164 
165   Arguments:    None.
166 
167   Returns:      TRUE if the command that was first issued is completed.
168  *---------------------------------------------------------------------------*/
DSPi_TryWaitForG711Core(void)169 BOOL DSPi_TryWaitForG711Core(void)
170 {
171     BOOL    retval = (DSPiG711CommandSend == DSPiG711CommandRecv);
172     if (!retval)
173     {
174         DSPByte32   ack;
175         DSPPipe     binin[1];
176         (void)DSP_LoadPipe(binin, DSP_PIPE_BINARY, DSP_PIPE_INPUT);
177         if (DSP_GetPipeReadableSize(binin) >= sizeof(ack))
178         {
179             DSP_ReadPipe(binin, &ack, sizeof(ack));
180             ++DSPiG711CommandRecv;
181             retval = (DSPiG711CommandSend == DSPiG711CommandRecv);
182         }
183     }
184     return retval;
185 }
186 
187 /*---------------------------------------------------------------------------*
188   Name:         DSPi_WaitForG711Core
189 
190   Description:  Waits for the command that was first issued to be completed.
191 
192   Arguments:    None.
193 
194   Returns:      None.
195  *---------------------------------------------------------------------------*/
DSPi_WaitForG711Core(void)196 void DSPi_WaitForG711Core(void)
197 {
198     if (DSPiG711CommandSend != DSPiG711CommandRecv)
199     {
200         DSPByte32   ack;
201         DSPPipe     binin[1];
202         (void)DSP_LoadPipe(binin, DSP_PIPE_BINARY, DSP_PIPE_INPUT);
203         while (DSPiG711CommandSend != DSPiG711CommandRecv)
204         {
205             DSP_ReadPipe(binin, &ack, sizeof(ack));
206             ++DSPiG711CommandRecv;
207         }
208     }
209 }
210