|
| 1 | +/* |
| 2 | +* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: BSD-3-Clause |
| 5 | +*/ |
| 6 | + |
| 7 | +/*******************************************************************************************************************//** |
| 8 | + * @ingroup RENESAS_CONNECTIVITY_INTERFACES |
| 9 | + * @defgroup I2S_API I2S Interface |
| 10 | + * @brief Interface for I2S audio communication. |
| 11 | + * |
| 12 | + * @section I2S_API_SUMMARY Summary |
| 13 | + * @brief The I2S (Inter-IC Sound) interface provides APIs and definitions for I2S audio communication. |
| 14 | + * |
| 15 | + * @{ |
| 16 | + **********************************************************************************************************************/ |
| 17 | + |
| 18 | +#ifndef R_I2S_API_H |
| 19 | +#define R_I2S_API_H |
| 20 | + |
| 21 | +/*********************************************************************************************************************** |
| 22 | + * Includes |
| 23 | + **********************************************************************************************************************/ |
| 24 | + |
| 25 | +/* Register definitions, common services and error codes. */ |
| 26 | +#include "bsp_api.h" |
| 27 | +#ifndef BSP_OVERRIDE_I2S_INCLUDE |
| 28 | + #include "r_timer_api.h" |
| 29 | +#endif |
| 30 | +#include "r_transfer_api.h" |
| 31 | + |
| 32 | +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ |
| 33 | +FSP_HEADER |
| 34 | + |
| 35 | +/********************************************************************************************************************** |
| 36 | + * Macro definitions |
| 37 | + **********************************************************************************************************************/ |
| 38 | + |
| 39 | +/********************************************************************************************************************** |
| 40 | + * Typedef definitions |
| 41 | + **********************************************************************************************************************/ |
| 42 | +#ifndef BSP_OVERRIDE_I2S_PCM_WIDTH_T |
| 43 | + |
| 44 | +/** Audio PCM width */ |
| 45 | +typedef enum e_i2s_pcm_width |
| 46 | +{ |
| 47 | + I2S_PCM_WIDTH_8_BITS = 0, ///< Using 8-bit PCM |
| 48 | + I2S_PCM_WIDTH_16_BITS = 1, ///< Using 16-bit PCM |
| 49 | + I2S_PCM_WIDTH_18_BITS = 2, ///< Using 18-bit PCM |
| 50 | + I2S_PCM_WIDTH_20_BITS = 3, ///< Using 20-bit PCM |
| 51 | + I2S_PCM_WIDTH_22_BITS = 4, ///< Using 22-bit PCM |
| 52 | + I2S_PCM_WIDTH_24_BITS = 5, ///< Using 24-bit PCM |
| 53 | + I2S_PCM_WIDTH_32_BITS = 6, ///< Using 32-bit PCM |
| 54 | +} i2s_pcm_width_t; |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifndef BSP_OVERRIDE_I2S_WORD_LENGTH_T |
| 58 | + |
| 59 | +/** Audio system word length. */ |
| 60 | +typedef enum e_i2s_word_length |
| 61 | +{ |
| 62 | + I2S_WORD_LENGTH_8_BITS = 0, ///< Using 8-bit system word length |
| 63 | + I2S_WORD_LENGTH_16_BITS = 1, ///< Using 16-bit system word length |
| 64 | + I2S_WORD_LENGTH_24_BITS = 2, ///< Using 24-bit system word length |
| 65 | + I2S_WORD_LENGTH_32_BITS = 3, ///< Using 32-bit system word length |
| 66 | + I2S_WORD_LENGTH_48_BITS = 4, ///< Using 48-bit system word length |
| 67 | + I2S_WORD_LENGTH_64_BITS = 5, ///< Using 64-bit system word length |
| 68 | + I2S_WORD_LENGTH_128_BITS = 6, ///< Using 128-bit system word length |
| 69 | + I2S_WORD_LENGTH_256_BITS = 7, ///< Using 256-bit system word length |
| 70 | +} i2s_word_length_t; |
| 71 | +#endif |
| 72 | + |
| 73 | +/** Events that can trigger a callback function */ |
| 74 | +typedef enum e_i2s_event |
| 75 | +{ |
| 76 | + I2S_EVENT_IDLE, ///< Communication is idle |
| 77 | + I2S_EVENT_TX_EMPTY, ///< Transmit buffer is below FIFO trigger level |
| 78 | + I2S_EVENT_RX_FULL, ///< Receive buffer is above FIFO trigger level |
| 79 | +} i2s_event_t; |
| 80 | + |
| 81 | +#ifndef BSP_OVERRIDE_I2S_MODE_T |
| 82 | + |
| 83 | +/** I2S communication mode */ |
| 84 | +typedef enum e_i2s_mode |
| 85 | +{ |
| 86 | + I2S_MODE_SLAVE = 0, ///< Slave mode |
| 87 | + I2S_MODE_MASTER = 1, ///< Master mode |
| 88 | +} i2s_mode_t; |
| 89 | +#endif |
| 90 | + |
| 91 | +/** Mute audio samples. */ |
| 92 | +typedef enum e_i2s_mute |
| 93 | +{ |
| 94 | + I2S_MUTE_OFF = 0, ///< Disable mute |
| 95 | + I2S_MUTE_ON = 1, ///< Enable mute |
| 96 | +} i2s_mute_t; |
| 97 | + |
| 98 | +/** Whether to continue WS (word select line) transmission during idle state. */ |
| 99 | +typedef enum e_i2s_ws_continue |
| 100 | +{ |
| 101 | + I2S_WS_CONTINUE_ON = 0, ///< Enable WS continue mode |
| 102 | + I2S_WS_CONTINUE_OFF = 1, ///< Disable WS continue mode |
| 103 | +} i2s_ws_continue_t; |
| 104 | + |
| 105 | +/** Possible status values returned by @ref i2s_api_t::statusGet. */ |
| 106 | +typedef enum e_i2s_state |
| 107 | +{ |
| 108 | + I2S_STATE_IN_USE, ///< I2S is in use |
| 109 | + I2S_STATE_STOPPED ///< I2S is stopped |
| 110 | +} i2s_state_t; |
| 111 | + |
| 112 | +/** Callback function parameter data */ |
| 113 | +typedef struct st_i2s_callback_args |
| 114 | +{ |
| 115 | + /** Placeholder for user data. Set in @ref i2s_api_t::open function in @ref i2s_cfg_t. */ |
| 116 | + void const * p_context; |
| 117 | + i2s_event_t event; ///< The event can be used to identify what caused the callback (overflow or error). |
| 118 | +} i2s_callback_args_t; |
| 119 | + |
| 120 | +/** I2S control block. Allocate an instance specific control block to pass into the I2S API calls. |
| 121 | + */ |
| 122 | +typedef void i2s_ctrl_t; |
| 123 | + |
| 124 | +/** I2S status. */ |
| 125 | +typedef struct st_i2s_status |
| 126 | +{ |
| 127 | + i2s_state_t state; ///< Current I2S state |
| 128 | +} i2s_status_t; |
| 129 | + |
| 130 | +/** User configuration structure, used in open function */ |
| 131 | +typedef struct st_i2s_cfg |
| 132 | +{ |
| 133 | + /** Select a channel corresponding to the channel number of the hardware. */ |
| 134 | + uint32_t channel; |
| 135 | + i2s_pcm_width_t pcm_width; ///< Audio PCM data width |
| 136 | + i2s_word_length_t word_length; ///< Audio word length, bits must be >= i2s_cfg_t::pcm_width bits |
| 137 | + i2s_ws_continue_t ws_continue; ///< Whether to continue WS transmission during idle state. |
| 138 | + i2s_mode_t operating_mode; ///< Master or slave mode |
| 139 | + |
| 140 | + /** To use DMA for transmitting link a Transfer instance here. Set to NULL if unused. */ |
| 141 | + transfer_instance_t const * p_transfer_tx; |
| 142 | + |
| 143 | + /** To use DMA for receiving link a Transfer instance here. Set to NULL if unused. */ |
| 144 | + transfer_instance_t const * p_transfer_rx; |
| 145 | + |
| 146 | + /** Callback provided when an I2S ISR occurs. Set to NULL for no CPU interrupt. */ |
| 147 | + void (* p_callback)(i2s_callback_args_t * p_args); |
| 148 | + |
| 149 | + /** Placeholder for user data. Passed to the user callback in @ref i2s_callback_args_t. */ |
| 150 | + void const * p_context; |
| 151 | + void const * p_extend; ///< Extension parameter for hardware specific settings. |
| 152 | + uint8_t rxi_ipl; ///< Receive interrupt priority |
| 153 | + uint8_t txi_ipl; ///< Transmit interrupt priority |
| 154 | + uint8_t idle_err_ipl; ///< Idle/Error interrupt priority |
| 155 | + IRQn_Type txi_irq; ///< Transmit IRQ number |
| 156 | + IRQn_Type rxi_irq; ///< Receive IRQ number |
| 157 | + IRQn_Type int_irq; ///< Idle/Error IRQ number |
| 158 | +} i2s_cfg_t; |
| 159 | + |
| 160 | +/** I2S functions implemented at the HAL layer will follow this API. */ |
| 161 | +typedef struct st_i2s_api |
| 162 | +{ |
| 163 | + /** Initial configuration. |
| 164 | + * |
| 165 | + * @pre Peripheral clocks and any required output pins should be configured prior to calling this function. |
| 166 | + * @note To reconfigure after calling this function, call @ref i2s_api_t::close first. |
| 167 | + * @param[in] p_ctrl Pointer to control block. Must be declared by user. Elements set here. |
| 168 | + * @param[in] p_cfg Pointer to configuration structure. All elements of this structure must be set by user. |
| 169 | + */ |
| 170 | + fsp_err_t (* open)(i2s_ctrl_t * const p_ctrl, i2s_cfg_t const * const p_cfg); |
| 171 | + |
| 172 | + /** Stop communication. Communication is stopped when callback is called with I2S_EVENT_IDLE. |
| 173 | + * |
| 174 | + * |
| 175 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 176 | + */ |
| 177 | + fsp_err_t (* stop)(i2s_ctrl_t * const p_ctrl); |
| 178 | + |
| 179 | + /** Enable or disable mute. |
| 180 | + * |
| 181 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 182 | + * @param[in] mute_enable Whether to enable or disable mute. |
| 183 | + */ |
| 184 | + fsp_err_t (* mute)(i2s_ctrl_t * const p_ctrl, i2s_mute_t const mute_enable); |
| 185 | + |
| 186 | + /** Write I2S data. All transmit data is queued when callback is called with I2S_EVENT_TX_EMPTY. |
| 187 | + * Transmission is complete when callback is called with I2S_EVENT_IDLE. |
| 188 | + * |
| 189 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 190 | + * @param[in] p_src Buffer of PCM samples. Must be 4 byte aligned. |
| 191 | + * @param[in] bytes Number of bytes in the buffer. Recommended requesting a multiple of 8 bytes. If not |
| 192 | + * a multiple of 8, padding 0s will be added to transmission to make it a multiple of 8. |
| 193 | + */ |
| 194 | + fsp_err_t (* write)(i2s_ctrl_t * const p_ctrl, void const * const p_src, uint32_t const bytes); |
| 195 | + |
| 196 | + /** Read I2S data. Reception is complete when callback is called with I2S_EVENT_RX_EMPTY. |
| 197 | + * |
| 198 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 199 | + * @param[in] p_dest Buffer to store PCM samples. Must be 4 byte aligned. |
| 200 | + * @param[in] bytes Number of bytes in the buffer. Recommended requesting a multiple of 8 bytes. If not |
| 201 | + * a multiple of 8, receive will stop at the multiple of 8 below requested bytes. |
| 202 | + */ |
| 203 | + fsp_err_t (* read)(i2s_ctrl_t * const p_ctrl, void * const p_dest, uint32_t const bytes); |
| 204 | + |
| 205 | + /** Simultaneously write and read I2S data. Transmission and reception are complete when |
| 206 | + * callback is called with I2S_EVENT_IDLE. |
| 207 | + * |
| 208 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 209 | + * @param[in] p_src Buffer of PCM samples. Must be 4 byte aligned. |
| 210 | + * @param[in] p_dest Buffer to store PCM samples. Must be 4 byte aligned. |
| 211 | + * @param[in] bytes Number of bytes in the buffers. Recommended requesting a multiple of 8 bytes. If not |
| 212 | + * a multiple of 8, padding 0s will be added to transmission to make it a multiple of 8, |
| 213 | + * and receive will stop at the multiple of 8 below requested bytes. |
| 214 | + */ |
| 215 | + fsp_err_t (* writeRead)(i2s_ctrl_t * const p_ctrl, void const * const p_src, void * const p_dest, |
| 216 | + uint32_t const bytes); |
| 217 | + |
| 218 | + /** Get current status and store it in provided pointer p_status. |
| 219 | + * |
| 220 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 221 | + * @param[out] p_status Current status of the driver. |
| 222 | + */ |
| 223 | + fsp_err_t (* statusGet)(i2s_ctrl_t * const p_ctrl, i2s_status_t * const p_status); |
| 224 | + |
| 225 | + /** Allows driver to be reconfigured and may reduce power consumption. |
| 226 | + * |
| 227 | + * @param[in] p_ctrl Control block set in @ref i2s_api_t::open call for this instance. |
| 228 | + */ |
| 229 | + fsp_err_t (* close)(i2s_ctrl_t * const p_ctrl); |
| 230 | + |
| 231 | + /** |
| 232 | + * Specify callback function and optional context pointer and working memory pointer. |
| 233 | + * |
| 234 | + * @param[in] p_ctrl Pointer to the I2S control block. |
| 235 | + * @param[in] p_callback Callback function |
| 236 | + * @param[in] p_context Pointer to send to callback function |
| 237 | + * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated. |
| 238 | + * Callback arguments allocated here are only valid during the callback. |
| 239 | + */ |
| 240 | + fsp_err_t (* callbackSet)(i2s_ctrl_t * const p_ctrl, void (* p_callback)(i2s_callback_args_t *), |
| 241 | + void const * const p_context, i2s_callback_args_t * const p_callback_memory); |
| 242 | +} i2s_api_t; |
| 243 | + |
| 244 | +/** This structure encompasses everything that is needed to use an instance of this interface. */ |
| 245 | +typedef struct st_i2s_instance |
| 246 | +{ |
| 247 | + i2s_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance |
| 248 | + i2s_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance |
| 249 | + i2s_api_t const * p_api; ///< Pointer to the API structure for this instance |
| 250 | +} i2s_instance_t; |
| 251 | + |
| 252 | +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ |
| 253 | +FSP_FOOTER |
| 254 | + |
| 255 | +#endif |
| 256 | + |
| 257 | +/*******************************************************************************************************************//** |
| 258 | + * @} (end defgroup I2S_API) |
| 259 | + **********************************************************************************************************************/ |
0 commit comments