fsl_wm8904.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. * Change Logs:
  35. * Date Author Notes
  36. * 2019-11-09 LiWeiHao Porting to RT-Thread
  37. */
  38. #ifndef _FSL_WM8904_H_
  39. #define _FSL_WM8904_H_
  40. #include "fsl_common.h"
  41. #include "rtdevice.h"
  42. #include "rtthread.h"
  43. /*!
  44. * @addtogroup wm8904
  45. * @{
  46. */
  47. /*******************************************************************************
  48. * Definitions
  49. ******************************************************************************/
  50. /*! @brief WM8904 I2C address. */
  51. #define WM8904_I2C_ADDRESS (0x1A)
  52. /*! @brief WM8904 I2C bit rate. */
  53. #define WM8904_I2C_BITRATE (400000U)
  54. /*! @brief WM8904 status return codes. */
  55. enum _wm8904_status
  56. {
  57. kStatus_WM8904_Success = 0x0, /*!< Success */
  58. kStatus_WM8904_Fail = 0x1 /*!< Failure */
  59. };
  60. /*! @brief The audio data transfer protocol. */
  61. typedef enum _wm8904_protocol
  62. {
  63. kWM8904_ProtocolRightJustified = 0x0, /*!< Right justified mode */
  64. kWM8904_ProtocolLeftJustified = 0x1, /*!< Left justified mode */
  65. kWM8904_ProtocolI2S = 0x2, /*!< I2S mode */
  66. kWM8904_ProtocolDSP = 0x3 /*!< DSP mode */
  67. } wm8904_protocol_t;
  68. /*! @brief The SYSCLK / fs ratio. */
  69. typedef enum _wm8904_fs_ratio
  70. {
  71. kWM8904_FsRatio64X = 0x0, /*!< SYSCLK is 64 * sample rate * frame width */
  72. kWM8904_FsRatio128X = 0x1, /*!< SYSCLK is 128 * sample rate * frame width */
  73. kWM8904_FsRatio192X = 0x2, /*!< SYSCLK is 192 * sample rate * frame width */
  74. kWM8904_FsRatio256X = 0x3, /*!< SYSCLK is 256 * sample rate * frame width */
  75. kWM8904_FsRatio384X = 0x4, /*!< SYSCLK is 384 * sample rate * frame width */
  76. kWM8904_FsRatio512X = 0x5, /*!< SYSCLK is 512 * sample rate * frame width */
  77. kWM8904_FsRatio768X = 0x6, /*!< SYSCLK is 768 * sample rate * frame width */
  78. kWM8904_FsRatio1024X = 0x7, /*!< SYSCLK is 1024 * sample rate * frame width */
  79. kWM8904_FsRatio1408X = 0x8, /*!< SYSCLK is 1408 * sample rate * frame width */
  80. kWM8904_FsRatio1536X = 0x9 /*!< SYSCLK is 1536 * sample rate * frame width */
  81. } wm8904_fs_ratio_t;
  82. /*! @brief Sample rate. */
  83. typedef enum _wm8904_sample_rate
  84. {
  85. kWM8904_SampleRate8kHz = 0x0, /*!< 8 kHz */
  86. kWM8904_SampleRate12kHz = 0x1, /*!< 11.025kHz, 12kHz */
  87. kWM8904_SampleRate16kHz = 0x2, /*!< 16kHz */
  88. kWM8904_SampleRate24kHz = 0x3, /*!< 22.05kHz, 24kHz */
  89. kWM8904_SampleRate32kHz = 0x4, /*!< 32kHz */
  90. kWM8904_SampleRate48kHz = 0x5 /*!< 44.1kHz, 48kHz */
  91. } wm8904_sample_rate_t;
  92. /*! @brief Bit width. */
  93. typedef enum _wm8904_bit_width
  94. {
  95. kWM8904_BitWidth16 = 0x0, /*!< 16 bits */
  96. kWM8904_BitWidth20 = 0x1, /*!< 20 bits */
  97. kWM8904_BitWidth24 = 0x2, /*!< 24 bits */
  98. kWM8904_BitWidth32 = 0x3 /*!< 32 bits */
  99. } wm8904_bit_width_t;
  100. /*! @brief Audio format configuration. */
  101. typedef struct _wm8904_audio_format
  102. {
  103. wm8904_fs_ratio_t fsRatio; /*!< SYSCLK / fs ratio */
  104. wm8904_sample_rate_t sampleRate; /*!< Sample rate */
  105. wm8904_bit_width_t bitWidth; /*!< Bit width */
  106. } wm8904_audio_format_t;
  107. /*! @brief WM8904 data. */
  108. typedef struct _wm8904_handle
  109. {
  110. struct rt_i2c_bus_device *i2c; /*!< Configured I2C instance */
  111. } wm8904_handle_t;
  112. /*! @brief Configuration structure of WM8904. */
  113. typedef struct _wm8904_config
  114. {
  115. bool master; /*!< Master or slave */
  116. wm8904_protocol_t protocol; /*!< Audio transfer protocol */
  117. wm8904_audio_format_t format; /*!< Audio format */
  118. } wm8904_config_t;
  119. /*******************************************************************************
  120. * API
  121. ******************************************************************************/
  122. #if defined(__cplusplus)
  123. extern "C"
  124. {
  125. #endif
  126. /*!
  127. * @brief Initializes WM8904.
  128. *
  129. * @param handle WM8904 handle structure.
  130. * @param codec_config WM8904 configuration structure.
  131. */
  132. status_t WM8904_Init(wm8904_handle_t *handle, wm8904_config_t *config);
  133. /*!
  134. * @brief Deinitializes the WM8904 codec.
  135. *
  136. * This function resets WM8904.
  137. *
  138. * @param handle WM8904 handle structure.
  139. *
  140. * @return kStatus_WM8904_Success if successful, different code otherwise.
  141. */
  142. status_t WM8904_Deinit(wm8904_handle_t *handle);
  143. /*!
  144. * @brief Fills the configuration structure with default values.
  145. *
  146. * The default values are:
  147. *
  148. * master = false;
  149. * protocol = kWM8904_ProtocolI2S;
  150. * format.fsRatio = kWM8904_FsRatio64X;
  151. * format.sampleRate = kWM8904_SampleRate48kHz;
  152. * format.bitWidth = kWM8904_BitWidth16;
  153. *
  154. * @param handle WM8904 handle structure to be filled with default values.
  155. */
  156. void WM8904_GetDefaultConfig(wm8904_config_t *config);
  157. /*!
  158. * @brief Sets WM8904 as master or slave.
  159. *
  160. * @param handle WM8904 handle structure.
  161. * @param master true for master, false for slave.
  162. *
  163. * @return kStatus_WM8904_Success if successful, different code otherwise.
  164. */
  165. status_t WM8904_SetMasterSlave(wm8904_handle_t *handle, bool master);
  166. /*!
  167. * @brief Sets the audio data transfer protocol.
  168. *
  169. * @param handle WM8904 handle structure.
  170. * @param protocol Audio transfer protocol.
  171. *
  172. * @return kStatus_WM8904_Success if successful, different code otherwise.
  173. */
  174. status_t WM8904_SetProtocol(wm8904_handle_t *handle, wm8904_protocol_t protocol);
  175. /*!
  176. * @brief Sets the audio data format.
  177. *
  178. * @param handle WM8904 handle structure.
  179. * @param format Audio format parameters.
  180. *
  181. * @return kStatus_WM8904_Success if successful, different code otherwise.
  182. */
  183. status_t WM8904_SetAudioFormat(wm8904_handle_t *handle, wm8904_audio_format_t *format);
  184. /*!
  185. * @brief Sets the headphone output volume.
  186. *
  187. * The parameter should be from 0 to 63.
  188. * The resulting volume will be (parameter - 57 dB).
  189. * 0 for -57 dB, 57 for 0 dB, 63 for +6 dB etc.
  190. *
  191. * @param handle WM8904 handle structure.
  192. * @param volumeLeft Volume of the left channel.
  193. * @param volumeRight Volume of the right channel.
  194. *
  195. * @return kStatus_WM8904_Success if successful, different code otherwise.
  196. */
  197. status_t WM8904_SetVolume(wm8904_handle_t *handle, uint16_t volumeLeft, uint16_t volumeRight);
  198. /*!
  199. * @brief Sets the headphone output mute.
  200. *
  201. * @param handle WM8904 handle structure.
  202. * @param muteLeft true to mute left channel, false to unmute.
  203. * @param muteRight true to mute right channel, false to unmute.
  204. *
  205. * @return kStatus_WM8904_Success if successful, different code otherwise.
  206. */
  207. status_t WM8904_SetMute(wm8904_handle_t *handle, bool muteLeft, bool muteRight);
  208. /*!
  209. * @brief Reads content of all WM8904 registers and prints it to debug console.
  210. *
  211. * @param handle WM8904 handle structure.
  212. *
  213. * @return kStatus_WM8904_Success if successful, different code otherwise.
  214. */
  215. status_t WM8904_PrintRegisters(wm8904_handle_t *handle);
  216. #if defined(__cplusplus)
  217. }
  218. #endif
  219. /*! @} */
  220. #endif /* _FSL_WM8904_H_ */