1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include - dsp - common
3   File:     pipe.h
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-10-21#$
14   $Rev: 9018 $
15   $Author: kitase_hirotake $
16  *---------------------------------------------------------------------------*/
17 #ifndef TWL_DSP_PIPE_H_
18 #define TWL_DSP_PIPE_H_
19 
20 
21 #include <twl/dsp/common/byteaccess.h>
22 
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 
29 /*---------------------------------------------------------------------------*/
30 /* Constants */
31 
32 // Constants for defining IN and OUT. (In principle, the flow is configured as follows. 0: DSP -> ARM, 1: ARM -> DSP)
33 #ifdef SDK_TWL
34 #define DSP_PIPE_INPUT      0
35 #define DSP_PIPE_OUTPUT     1
36 #else
37 #define DSP_PIPE_INPUT      1
38 #define DSP_PIPE_OUTPUT     0
39 #endif
40 #define DSP_PIPE_PEER_MAX   2
41 
42 // System resources
43 #define DSP_PIPE_PORT_MAX               8   // Maximum number of usable ports for pipes
44 #define DSP_PIPE_DEFAULT_BUFFER_LENGTH  256 // Default ring buffer size
45 
46 // Defined ports
47 #define DSP_PIPE_CONSOLE    0   // DSP -> ARM: Debugging console
48 #define DSP_PIPE_DMA        1   // DSP <-> ARM: Pseudo-DMA
49 #define DSP_PIPE_AUDIO      2   // DSP <-> ARM: General-purpose audio communications
50 #define DSP_PIPE_BINARY     3   // DSP <-> ARM: General-purpose binary
51 #define DSP_PIPE_EPHEMERAL  4   // Free region that can be allocated with DSP_CreatePipe()
52 
53 #define DSP_PIPE_FLAG_INPUT     0x0000  // Input side
54 #define DSP_PIPE_FLAG_OUTPUT    0x0001  // Output side
55 #define DSP_PIPE_FLAG_PORTMASK  0x00FF  // Negative field for port numbers
56 #define DSP_PIPE_FLAG_BOUND     0x0100  // Opened
57 #define DSP_PIPE_FLAG_EOF       0x0200  // EOF
58 
59 #define DSP_PIPE_FLAG_EXIT_OS   0x8000  // Exit processing for the DSP's AHB master
60 
61 #define DSP_PIPE_COMMAND_REGISTER   2
62 
63 // Command structures for DSP file I/O.
64 #define DSP_PIPE_IO_COMMAND_OPEN    0
65 #define DSP_PIPE_IO_COMMAND_CLOSE   1
66 #define DSP_PIPE_IO_COMMAND_SEEK    2
67 #define DSP_PIPE_IO_COMMAND_READ    3
68 #define DSP_PIPE_IO_COMMAND_WRITE   4
69 #define DSP_PIPE_IO_COMMAND_MEMMAP  5
70 
71 #define DSP_PIPE_IO_MODE_R          0x0001
72 #define DSP_PIPE_IO_MODE_W          0x0002
73 #define DSP_PIPE_IO_MODE_RW         0x0004
74 #define DSP_PIPE_IO_MODE_TRUNC      0x0008
75 #define DSP_PIPE_IO_MODE_CREATE     0x0010
76 
77 #define DSP_PIPE_IO_SEEK_SET        0
78 #define DSP_PIPE_IO_SEEK_CUR        1
79 #define DSP_PIPE_IO_SEEK_END        2
80 
81 
82 /*---------------------------------------------------------------------------*/
83 /* Declarations */
84 
85 // Pipe structures.
86 // There is no procedure for direct ARM access from the DSP, so the DSP buffer is normally controlled in the APBP-FIFO from an ARM processor.
87 // It will be treated as a ring buffer in the absence of a particular specification.
88 //
89 typedef struct DSPPipe
90 {
91     DSPAddr address;    // Starting address of the buffer
92     DSPByte length;     // Buffer size
93     DSPByte rpos;       // First unread region
94     DSPByte wpos;       // Last appended region
95     u16     flags;      // Attribute flags
96 }
97 DSPPipe;
98 
99 // Pipe information maintained by the DSP and accessed by an ARM processor.
100 typedef struct DSPPipeMonitor
101 {
102     DSPPipe pipe[DSP_PIPE_PORT_MAX][DSP_PIPE_PEER_MAX];
103 }
104 DSPPipeMonitor;
105 
106 // DSP pipe communication handler.
107 typedef void (*DSPPipeCallback)(void *userdata, int port, int peer);
108 
109 
110 /*---------------------------------------------------------------------------*/
111 /* Functions */
112 
113 /*---------------------------------------------------------------------------*
114   Name:         DSP_InitPipe
115 
116   Description:  Initializes DSP pipe communication.
117                 This takes possession of DSP command register 2.
118 
119   Arguments:    None.
120 
121   Returns:      None.
122  *---------------------------------------------------------------------------*/
123 void DSP_InitPipe(void);
124 
125 /*---------------------------------------------------------------------------*
126   Name:         DSP_SetPipeCallback
127 
128   Description:  Sets a callback for DSP pipe communications.
129 
130   Arguments:    port: The pipe's port number.
131                 callback: The callback to invoke on a Readable/Writable event.
132                 userdata: Arbitrary user-defined argument
133 
134   Returns:      None.
135  *---------------------------------------------------------------------------*/
136 void DSP_SetPipeCallback(int port, void (*callback)(void *, int, int), void *userdata);
137 
138 /*---------------------------------------------------------------------------*
139   Name:         DSP_LoadPipe
140 
141   Description:  Loads DSP pipe information.
142 
143   Arguments:    pipe: Location to store pipe information (this may be NULL on the DSP)
144                 port: The pipe's port number
145                 peer: DSP_PIPE_INPUT or DSP_PIPE_OUTPUT
146 
147   Returns:      A pointer to the loaded pipe information.
148  *---------------------------------------------------------------------------*/
149 DSPPipe* DSP_LoadPipe(DSPPipe *pipe, int port, int peer);
150 
151 /*---------------------------------------------------------------------------*
152   Name:         DSP_SyncPipe
153 
154   Description:  Updates DSP pipe information to the most recent content.
155 
156   Arguments:    pipe: Pipe information
157 
158   Returns:      None.
159  *---------------------------------------------------------------------------*/
160 void DSP_SyncPipe(DSPPipe *pipe);
161 
162 /*---------------------------------------------------------------------------*
163   Name:         DSP_FlushPipe
164 
165   Description:  Flushes a DSP pipe stream.
166 
167   Arguments:    pipe: Pipe information
168 
169   Returns:      None.
170  *---------------------------------------------------------------------------*/
171 void DSP_FlushPipe(DSPPipe *pipe);
172 
173 /*---------------------------------------------------------------------------*
174   Name:         DSP_GetPipeReadableSize
175 
176   Description:  Gets the maximum size that can currently be read from the specified DSP pipe.
177 
178   Arguments:    pipe: Pipe information
179 
180   Returns:      The maximum size that can currently be read.
181  *---------------------------------------------------------------------------*/
182 u16 DSP_GetPipeReadableSize(const DSPPipe *pipe);
183 
184 /*---------------------------------------------------------------------------*
185   Name:         DSP_GetPipeWritableSize
186 
187   Description:  Gets the maximum size that can currently be written to the specified DSP pipe.
188 
189   Arguments:    pipe: Pipe information
190 
191   Returns:      The maximum size that can currently be written.
192  *---------------------------------------------------------------------------*/
193 u16 DSP_GetPipeWritableSize(const DSPPipe *pipe);
194 
195 /*---------------------------------------------------------------------------*
196   Name:         DSP_ReadPipe
197 
198   Description:  Reads data from a DSP pipe's communications port.
199 
200   Arguments:    pipe: Pipe information
201                 buffer: Buffer to transfer data to
202                 length: Transfer size. (however, this is in units of the environment's word size)
203                          Note that 1-byte units are used on an ARM processor and 2-byte units are used on the DSP.
204 
205   Returns:      None.
206  *---------------------------------------------------------------------------*/
207 void DSP_ReadPipe(DSPPipe *pipe, void *buffer, u16 length);
208 
209 /*---------------------------------------------------------------------------*
210   Name:         DSP_WritePipe
211 
212   Description:  Writes data to a DSP pipe's communications port.
213 
214   Arguments:    pipe: Pipe information
215                 buffer: Buffer to transfer data from
216                 length: Transfer size. (however, this is in units of the environment's word size)
217                          Note that 1-byte units are used on an ARM processor and 2-byte units are used on the DSP.
218   Returns:      None.
219  *---------------------------------------------------------------------------*/
220 void DSP_WritePipe(DSPPipe *pipe, const void *buffer, u16 length);
221 
222 /*---------------------------------------------------------------------------*
223   Name:         DSP_HookPipeNotification
224 
225   Description:  Pipe notification hook that should be invoked in a DSP interrupt.
226 
227   Arguments:    None.
228 
229   Returns:      None.
230  *---------------------------------------------------------------------------*/
231 void DSP_HookPipeNotification(void);
232 
233 #ifdef SDK_TWL
234 
235 #else
236 
237 /*---------------------------------------------------------------------------*
238   Name:         DSP_Printf
239 
240   Description:  Outputs a string to the DSP pipe's console port.
241 
242   Arguments:    format: Formatted string
243 
244   Returns:      None.
245  *---------------------------------------------------------------------------*/
246 void DSP_Printf(const char *format, ...);
247 
248 int DSP_OpenFile(const char *path, int mode);
249 int DSP_OpenMemoryFile(DSPAddrInARM address, DSPWord32 length);
250 void DSP_CloseFile(int port);
251 s32 DSP_GetFilePosition(int port);
252 s32 DSP_GetFileLength(int port);
253 s32 DSP_SeekFile(int port, s32 offset, int whence);
254 s32 DSP_ReadFile(int port, void *buffer, DSPWord length);
255 s32 DSP_WriteFile(int port, const void *buffer, DSPWord length);
256 
257 // Sample replacement functions for the C Standard Library
258 #if 0
259 typedef void FILE;
260 #define fopen(path, mode)           (FILE*)DSP_OpenFile(path, mode)
261 #define fclose(f)                   DSP_CloseFile((int)f)
262 #define fseek(f, ofs, whence)       DSP_SeekFile((int)f, ofs, whence)
263 #define fread(buf, len, unit, f)    DSP_ReadFile((int)f, buf, (len) * (unit))
264 #define fwrite(buf, len, unit, f)   DSP_WriteFile((int)f, buf, (len) * (unit))
265 #define rewind(f)                   (void)DSP_SeekFile((int)f, 0, DSP_PIPE_IO_SEEK_SET)
266 #define ftell(f)                    (void)DSP_SeekFile((int)f, 0, DSP_PIPE_IO_SEEK_CUR)
267 #define fgetpos(f, ppos)            (((*(ppos) = ftell((int)f)) != -1) ? 0 : -1)
268 #define fsetpos(f, ppos)            fseek((int)f, *(ppos), DSP_PIPE_IO_SEEK_SET)
269 #endif
270 
271 #endif
272 
273 
274 /*===========================================================================*/
275 
276 #ifdef __cplusplus
277 } /* extern "C" */
278 #endif
279 
280 #endif /* TWL_DSP_PIPE_H_ */
281