You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.8KB

  1. // Copyright 2014 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. // See http://creativecommons.org/licenses/MIT/ for more information.
  24. //
  25. // -----------------------------------------------------------------------------
  26. //
  27. // WM8371 Codec support.
  28. #ifndef ELEMENTS_DRIVERS_CODEC_H_
  29. #define ELEMENTS_DRIVERS_CODEC_H_
  30. #include <stm32f4xx_conf.h>
  31. #include "stmlib/stmlib.h"
  32. #include "stmlib/utils/ring_buffer.h"
  33. namespace elements {
  34. enum CodecProtocol {
  35. CODEC_PROTOCOL_PHILIPS = I2S_Standard_Phillips,
  36. CODEC_PROTOCOL_MSB_FIRST = I2S_Standard_MSB,
  37. CODEC_PROTOCOL_LSB_FIRST = I2S_Standard_LSB
  38. };
  39. enum CodecFormat {
  40. CODEC_FORMAT_16_BIT = I2S_DataFormat_16b,
  41. CODEC_FORMAT_24_BIT = I2S_DataFormat_24b,
  42. CODEC_FORMAT_32_BIT = I2S_DataFormat_32b
  43. };
  44. enum CodecSettings {
  45. CODEC_INPUT_0_DB = 0x17,
  46. CODEC_HEADPHONES_MUTE = 0x00,
  47. CODEC_MIC_BOOST = 0x1,
  48. CODEC_MIC_MUTE = 0x2,
  49. CODEC_ADC_MIC = 0x4,
  50. CODEC_ADC_LINE = 0x0,
  51. CODEC_OUTPUT_DAC_ENABLE = 0x10,
  52. CODEC_OUTPUT_MONITOR = 0x20,
  53. CODEC_DEEMPHASIS_NONE = 0x00,
  54. CODEC_DEEMPHASIS_32K = 0x01,
  55. CODEC_DEEMPHASIS_44K = 0x02,
  56. CODEC_DEEMPHASIS_48K = 0x03,
  57. CODEC_SOFT_MUTE = 0x01,
  58. CODEC_ADC_HPF = 0x00,
  59. CODEC_POWER_DOWN_LINE_IN = 0x01,
  60. CODEC_POWER_DOWN_MIC = 0x02,
  61. CODEC_POWER_DOWN_ADC = 0x04,
  62. CODEC_POWER_DOWN_DAC = 0x08,
  63. CODEC_POWER_DOWN_LINE_OUT = 0x10,
  64. CODEC_POWER_DOWN_OSCILLATOR = 0x20,
  65. CODEC_POWER_DOWN_CLOCK_OUTPUT = 0x40,
  66. CODEC_POWER_DOWN_EVERYTHING = 0x80,
  67. CODEC_PROTOCOL_MASK_MSB_FIRST = 0x00,
  68. CODEC_PROTOCOL_MASK_LSB_FIRST = 0x01,
  69. CODEC_PROTOCOL_MASK_PHILIPS = 0x02,
  70. CODEC_PROTOCOL_MASK_DSP = 0x03,
  71. CODEC_FORMAT_MASK_16_BIT = 0x00 << 2,
  72. CODEC_FORMAT_MASK_20_BIT = 0x01 << 2,
  73. CODEC_FORMAT_MASK_24_BIT = 0x02 << 2,
  74. CODEC_FORMAT_MASK_32_BIT = 0x03 << 2,
  75. CODEC_FORMAT_LR_SWAP = 0x20,
  76. CODEC_FORMAT_MASTER = 0x40,
  77. CODEC_FORMAT_SLAVE = 0x00,
  78. CODEC_FORMAT_INVERT_CLOCK = 0x80,
  79. CODEC_RATE_48K_48K = 0x00 << 2,
  80. CODEC_RATE_96K_96K = 0x07 << 2,
  81. CODEC_RATE_32K_32K = 0x06 << 2,
  82. CODEC_RATE_44K_44K = 0x18 << 2,
  83. };
  84. // Size of an audio chunk in frames (stereo samples).
  85. // The DMA buffer is 2 chunks long.
  86. const size_t kAudioChunkSize = 16;
  87. // Size of the additional FIFOs in chunks. This gives an additional layer of
  88. // Buffering to make things more robust to jitter (for example if there are
  89. // occasional expensive calls in the UI).
  90. // Use 0 for using naive double-buffering.
  91. const size_t kNumFIFOChunks = 0;
  92. class Codec {
  93. public:
  94. Codec() { }
  95. ~Codec() { }
  96. typedef struct { short l; short r; } Frame;
  97. typedef void (*FillBufferCallback)(Frame* rx, Frame* tx, size_t size);
  98. bool Init(uint32_t sample_rate, CodecProtocol protocol, CodecFormat format);
  99. bool Start() {
  100. // No callback - the caller is supposed to poll with available()
  101. return Start(NULL);
  102. }
  103. bool Start(FillBufferCallback callback);
  104. void Stop();
  105. void Fill(size_t offset);
  106. static Codec* GetInstance() { return instance_; }
  107. // When naive double-buffering is used:
  108. // 1. Call available() to know if a half-buffer is available.
  109. // 2. Call Grab(&input_ptr, &output_ptr) to retrieve pointer on the
  110. // available rx/tx half-buffers, along with their size.
  111. inline bool available() const {
  112. return transmitted_ != processed_;
  113. }
  114. inline size_t Grab(Frame** input, Frame** output) {
  115. ++processed_;
  116. *input = client_rx_;
  117. *output = client_tx_;
  118. return kAudioChunkSize;
  119. }
  120. // When the extra FIFOs are used:
  121. // 1. Call writable() and readable() to know how much data can be read
  122. // and written from/to the FIFO.
  123. // 2. Call ImmedidateRead() and Overwrite() to read/write from/to the FIFO.
  124. size_t writable() const { return tx_buffer_.writable(); }
  125. size_t readable() const { return rx_buffer_.readable(); }
  126. void ImmediateRead(Frame* destination, size_t size) {
  127. rx_buffer_.ImmediateRead(destination, size);
  128. }
  129. void Overwrite(Frame* source, size_t size) {
  130. tx_buffer_.Overwrite(source, size);
  131. }
  132. private:
  133. bool InitializeGPIO();
  134. bool InitializeControlInterface();
  135. bool InitializeAudioInterface(uint32_t, CodecProtocol, CodecFormat);
  136. bool WriteControlRegister(uint8_t address, uint16_t data);
  137. bool InitializeCodec(uint32_t, CodecProtocol, CodecFormat);
  138. bool InitializeDMA();
  139. static Codec* instance_;
  140. bool use_buffering_;
  141. size_t transmitted_;
  142. size_t processed_;
  143. Frame* client_tx_;
  144. Frame* client_rx_;
  145. FillBufferCallback callback_;
  146. DMA_InitTypeDef dma_init_tx_;
  147. DMA_InitTypeDef dma_init_rx_;
  148. Frame tx_dma_buffer_[kAudioChunkSize * 2];
  149. Frame rx_dma_buffer_[kAudioChunkSize * 2];
  150. stmlib::RingBuffer<Frame, kAudioChunkSize * kNumFIFOChunks> rx_buffer_;
  151. stmlib::RingBuffer<Frame, kAudioChunkSize * kNumFIFOChunks> tx_buffer_;
  152. DISALLOW_COPY_AND_ASSIGN(Codec);
  153. };
  154. } // namespace elements
  155. #endif // ELEMENTS_DRIVERS_CODEC_H_