libctru  v2.4.1
dsp.h
Go to the documentation of this file.
1 /**
2  * @file dsp.h
3  * @brief DSP Service to access the DSP processor commands (sound)
4  *
5  * The DSP has access to the Linear memory region, and to the DSP memory region if allowed in the exheader.
6  */
7 #pragma once
8 #include <3ds/types.h>
9 
10 /// DSP interrupt types.
11 typedef enum
12 {
13  DSP_INTERRUPT_PIPE = 2 ///< Pipe interrupt.
15 
16 /// DSP hook types.
17 typedef enum
18 {
19  DSPHOOK_ONSLEEP = 0, ///< DSP is going to sleep.
20  DSPHOOK_ONWAKEUP = 1, ///< DSP is waking up.
21  DSPHOOK_ONCANCEL = 2, ///< DSP was sleeping and the app was cancelled.
22 } DSP_HookType;
23 
24 /// DSP hook function.
25 typedef void (* dspHookFn)(DSP_HookType hook);
26 
27 /// DSP hook cookie.
28 typedef struct tag_dspHookCookie
29 {
30  struct tag_dspHookCookie* next; ///< Next cookie.
31  dspHookFn callback; ///< Hook callback.
33 
34 /**
35  * @brief Initializes the dsp service.
36  *
37  * Call this before calling any DSP_* function.
38  * @note This will also unload any previously loaded DSP binary.
39  * It is done this way since you have to provide your binary when the 3DS leaves sleep mode anyway.
40  */
42 
43 /**
44  * @brief Closes the dsp service.
45  * @note This will also unload the DSP binary.
46  */
47 void dspExit(void);
48 
49 /// Returns true if a component is loaded, false otherwise.
51 
52 /**
53  * @brief Sets up a DSP status hook.
54  * @param cookie Hook cookie to use.
55  * @param callback Function to call when DSP's status changes.
56  */
57 void dspHook(dspHookCookie* cookie, dspHookFn callback);
58 
59 /**
60  * @brief Removes a DSP status hook.
61  * @param cookie Hook cookie to remove.
62  */
63 void dspUnhook(dspHookCookie* cookie);
64 
65 /**
66  * @brief Checks if a headphone is inserted.
67  * @param is_inserted Pointer to output the insertion status to.
68  */
69 Result DSP_GetHeadphoneStatus(bool* is_inserted);
70 
71 /**
72  * @brief Flushes the cache
73  * @param address Beginning of the memory range to flush, inside the Linear or DSP memory regions
74  * @param size Size of the memory range to flush
75  *
76  * Flushes the cache for the specified memory range and invalidates the cache
77  */
78 Result DSP_FlushDataCache(const void* address, u32 size);
79 
80 /**
81  * @brief Invalidates the cache
82  * @param address Beginning of the memory range to invalidate, inside the Linear or DSP memory regions
83  * @param size Size of the memory range to flush
84  *
85  * Invalidates the cache for the specified memory range
86  */
87 Result DSP_InvalidateDataCache(const void* address, u32 size);
88 
89 /**
90  * @brief Retrieves the handle of the DSP semaphore.
91  * @param semaphore Pointer to output the semaphore to.
92  */
94 
95 /**
96  * @brief Sets the DSP hardware semaphore value.
97  * @param value Value to set.
98  */
100 
101 /**
102  * @brief Masks the DSP hardware semaphore value.
103  * @param mask Mask to apply.
104  */
106 
107 /**
108  * @brief Loads a DSP binary and starts the DSP
109  * @param component The program file address in memory
110  * @param size The size of the program
111  * @param prog_mask DSP memory block related ? Default is 0xff.
112  * @param data_mask DSP memory block related ? Default is 0xff.
113  * @param is_loaded Indicates if the DSP was succesfully loaded.
114  *
115  * @note The binary must be signed (http://3dbrew.org/wiki/DSP_Binary)
116  * @note Seems to be called when the 3ds leaves the Sleep mode
117  */
118 Result DSP_LoadComponent(const void* component, u32 size, u16 prog_mask, u16 data_mask, bool* is_loaded);
119 
120 ///Stops the DSP by unloading the binary.
122 
123 /**
124  * @brief Registers an event handle with the DSP through IPC
125  * @param handle Event handle to register.
126  * @param interrupt The type of interrupt that will trigger the event. Usual value is DSP_INTERRUPT_PIPE.
127  * @param channel The pipe channel. Usual value is 2
128  *
129  * @note It is possible that interrupt are inverted
130  */
131 Result DSP_RegisterInterruptEvents(Handle handle, u32 interrupt, u32 channel);
132 
133 /**
134  * @brief Reads a pipe if possible.
135  * @param channel unknown. Usually 2
136  * @param peer unknown. Usually 0
137  * @param buffer The buffer that will store the values read from the pipe
138  * @param length Length of the buffer
139  * @param length_read Number of bytes read by the command
140  */
141 Result DSP_ReadPipeIfPossible(u32 channel, u32 peer, void* buffer, u16 length, u16* length_read);
142 
143 /**
144  * @brief Writes to a pipe.
145  * @param channel unknown. Usually 2
146  * @param buffer The message to send to the DSP process
147  * @param length Length of the message
148  */
149 Result DSP_WriteProcessPipe(u32 channel, const void* buffer, u32 length);
150 
151 /**
152  * @brief Converts a DSP memory address to a virtual address usable by the process.
153  * @param dsp_address Address to convert.
154  * @param arm_address Pointer to output the converted address to.
155  */
157 
158 /**
159  * @brief Reads a DSP register
160  * @param regNo Offset of the hardware register, base address is 0x1EC40000
161  * @param value Pointer to read the register value to.
162  */
163 Result DSP_RecvData(u16 regNo, u16* value);
164 
165 /**
166  * @brief Checks if you can read a DSP register
167  * @param regNo Offset of the hardware register, base address is 0x1EC40000
168  * @param is_ready Pointer to write the ready status to.
169  *
170  * @warning This call might hang if the data is not ready. See @ref DSP_SendDataIsEmpty.
171  */
172 Result DSP_RecvDataIsReady(u16 regNo, bool* is_ready);
173 
174 /**
175  * @brief Writes to a DSP register
176  * @param regNo Offset of the hardware register, base address is 0x1EC40000
177  * @param value Value to write.
178  *
179  * @warning This call might hang if the SendData is not empty. See @ref DSP_SendDataIsEmpty.
180  */
181 Result DSP_SendData(u16 regNo, u16 value);
182 
183 /**
184  * @brief Checks if you can write to a DSP register ?
185  * @param regNo Offset of the hardware register, base address is 0x1EC40000
186  * @param is_empty Pointer to write the empty status to.
187  */
188 Result DSP_SendDataIsEmpty(u16 regNo, bool* is_empty);
void(* dspHookFn)(DSP_HookType hook)
DSP hook function.
Definition: dsp.h:25
Result DSP_FlushDataCache(const void *address, u32 size)
Flushes the cache.
Result DSP_RegisterInterruptEvents(Handle handle, u32 interrupt, u32 channel)
Registers an event handle with the DSP through IPC.
Result DSP_WriteProcessPipe(u32 channel, const void *buffer, u32 length)
Writes to a pipe.
Result dspInit(void)
Initializes the dsp service.
Result DSP_SendData(u16 regNo, u16 value)
Writes to a DSP register.
Result DSP_SetSemaphoreMask(u16 mask)
Masks the DSP hardware semaphore value.
void dspExit(void)
Closes the dsp service.
Result DSP_LoadComponent(const void *component, u32 size, u16 prog_mask, u16 data_mask, bool *is_loaded)
Loads a DSP binary and starts the DSP.
Result DSP_RecvDataIsReady(u16 regNo, bool *is_ready)
Checks if you can read a DSP register.
Result DSP_ConvertProcessAddressFromDspDram(u32 dsp_address, u32 *arm_address)
Converts a DSP memory address to a virtual address usable by the process.
void dspUnhook(dspHookCookie *cookie)
Removes a DSP status hook.
Result DSP_ReadPipeIfPossible(u32 channel, u32 peer, void *buffer, u16 length, u16 *length_read)
Reads a pipe if possible.
void dspHook(dspHookCookie *cookie, dspHookFn callback)
Sets up a DSP status hook.
Result DSP_SetSemaphore(u16 value)
Sets the DSP hardware semaphore value.
Result DSP_RecvData(u16 regNo, u16 *value)
Reads a DSP register.
DSP_InterruptType
DSP interrupt types.
Definition: dsp.h:12
@ DSP_INTERRUPT_PIPE
Pipe interrupt.
Definition: dsp.h:13
Result DSP_SendDataIsEmpty(u16 regNo, bool *is_empty)
Checks if you can write to a DSP register ?
Result DSP_InvalidateDataCache(const void *address, u32 size)
Invalidates the cache.
Result DSP_UnloadComponent(void)
Stops the DSP by unloading the binary.
DSP_HookType
DSP hook types.
Definition: dsp.h:18
@ DSPHOOK_ONSLEEP
DSP is going to sleep.
Definition: dsp.h:19
@ DSPHOOK_ONCANCEL
DSP was sleeping and the app was cancelled.
Definition: dsp.h:21
@ DSPHOOK_ONWAKEUP
DSP is waking up.
Definition: dsp.h:20
Result DSP_GetHeadphoneStatus(bool *is_inserted)
Checks if a headphone is inserted.
Result DSP_GetSemaphoreHandle(Handle *semaphore)
Retrieves the handle of the DSP semaphore.
bool dspIsComponentLoaded(void)
Returns true if a component is loaded, false otherwise.
DSP hook cookie.
Definition: dsp.h:29
struct tag_dspHookCookie * next
Next cookie.
Definition: dsp.h:30
dspHookFn callback
Hook callback.
Definition: dsp.h:31
Various system types.
u32 Handle
Resource handle.
Definition: types.h:41
s32 Result
Function result.
Definition: types.h:42
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23