gd32f303c_audio_codec.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*!
  2. \file gd32F303c_audio_codec.h
  3. \brief header file of the low layer driver for audio codec
  4. \version 2023-06-30, V2.1.6, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2023, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #ifndef GD32F303C_AUDIO_CODEC_H
  30. #define GD32F303C_AUDIO_CODEC_H
  31. #include "gd32f30x.h"
  32. /* Select the interrupt preemption priority and sub-priority for the DMA interrupt */
  33. #define AD_IRQ_PREPRIO 0
  34. #define AD_IRQ_SUBRIO 0
  35. /* uncomment the defines below to select if the master clock mode should be enabled or not */
  36. #define IIS_MCLK_ENABLED
  37. /* #define IIS_MCLK_DISABLED */
  38. /* hardware configuration defines parameters */
  39. /* I2S peripheral configuration defines (data and control interface of the audio codec) */
  40. #define AD_I2S SPI1
  41. #define AD_I2S_CLK RCU_SPI1
  42. #define AD_I2S_ADDRESS (SPI1+0x0C)
  43. #define AD_I2S_IRQ SPI1_IRQn
  44. #define AD_I2S_WS_PIN GPIO_PIN_12
  45. #define AD_I2S_SCK_PIN GPIO_PIN_13
  46. #define AD_I2S_SD_PIN GPIO_PIN_15
  47. #define AD_I2S_MCK_PIN GPIO_PIN_6
  48. #define AD_I2S_MCK_GPIO GPIOC
  49. #define AD_I2S_GPIO GPIOB
  50. #define AD_I2S_GPIO_CLK RCU_GPIOB
  51. #define AD_I2S_MCK_CLK RCU_GPIOC
  52. /* I2S DMA stream definitions */
  53. #define AD_DMA_CLOCK RCU_DMA0
  54. #define AD_DMA_CHANNEL DMA_CH4
  55. #define AD_DMA_IRQ DMA0_Channel4_IRQn
  56. #define AD_DMA_FLAG_TC DMA_FLAG_FTF
  57. #define AD_DMA_FLAG_HT DMA_FLAG_HTF
  58. #define AD_DMA_FLAG_TE DMA_FLAG_ERR
  59. #define AD_DMA_FLAG_GL DMA_INT_FLAG_G
  60. #define AD_DMA_FLAG_ALL DMA_INT_FLAG_G
  61. #define AD_DMA DMA0
  62. #define Audio_DMA_IRQHandler DMA0_Channel4_IRQHandler
  63. /* mute commands */
  64. #define AD_MUTE 0x01U
  65. #define AD_UNMUTE 0x00U
  66. /* functions return value */
  67. #define AD_OK 0x00U
  68. #define AD_FAIL 0xFFU
  69. #define AD_PAUSE 0U
  70. #define AD_RESUME 1U
  71. /* audio machine states */
  72. #define AD_STATE_INACTIVE 0x00U
  73. #define AD_STATE_ACTIVE 0x01U
  74. #define AD_STATE_PLAYING 0x02U
  75. #define AD_STATE_PAUSED 0x03U
  76. #define AD_STATE_STOPPED 0x04U
  77. #define AD_STATE_ERROR 0x05U
  78. /* mask for the bit en of the i2s cfgr register */
  79. #define I2S_ENABLE_MASK (0x0400)
  80. /* audio commands enumeration */
  81. typedef enum
  82. {
  83. AD_CMD_PLAY = 1U,
  84. AD_CMD_PAUSE,
  85. AD_CMD_STOP,
  86. }audio_cmd_enum;
  87. /* function declarations */
  88. /* initializes the audio codec audio interface (i2s) */
  89. void codec_audio_interface_init(uint32_t audio_freq);
  90. /* deinitialize the audio codec audio interface */
  91. void codec_audio_interface_deinit(void);
  92. /* initializes IOs used by the audio codec */
  93. void codec_gpio_init(void);
  94. /* deinitialize IOs used by the audio codec interface */
  95. void codec_gpio_deinit(void);
  96. /* initializes dma to prepare for audio data transfer */
  97. void codec_i2s_dma_init(void);
  98. /* restore default state of the used media */
  99. void codec_i2s_dma_deinit(void);
  100. /* starts playing audio stream from the audio media */
  101. void audio_play(uint32_t addr, uint32_t size);
  102. /* pauses or resumes the audio stream playing from the media */
  103. void audio_pause_resume(uint32_t cmd, uint32_t addr, uint32_t size);
  104. /* stops audio stream playing on the used media */
  105. void audio_stop(void);
  106. #endif /* GD32F303C_AUDIO_CODEC_H */