luat_audio_tm8211.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "luat_base.h"
  2. #include "luat_gpio.h"
  3. #include "luat_audio.h"
  4. #include "luat_rtos.h"
  5. #define LUAT_LOG_TAG "tm8211"
  6. #include "luat_log.h"
  7. static int tm8211_codec_init(luat_audio_codec_conf_t* conf,uint8_t mode){
  8. if (conf->power_pin != LUAT_CODEC_PA_NONE){
  9. luat_gpio_mode(conf->power_pin, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, !conf->power_on_level);
  10. luat_gpio_set(conf->power_pin, conf->power_on_level);
  11. }
  12. if (conf->power_on_delay_ms){
  13. luat_rtos_task_sleep(conf->power_on_delay_ms);
  14. }
  15. return 0;
  16. }
  17. static int tm8211_codec_deinit(luat_audio_codec_conf_t* conf){
  18. if (conf->pa_pin != LUAT_CODEC_PA_NONE){
  19. luat_gpio_set(conf->pa_pin, !conf->pa_on_level);
  20. luat_gpio_close(conf->pa_pin);
  21. }
  22. if (conf->power_pin != LUAT_CODEC_PA_NONE){
  23. luat_gpio_set(conf->power_pin, !conf->power_on_level);
  24. luat_gpio_close(conf->power_pin);
  25. }
  26. return 0;
  27. }
  28. static void tm8211_codec_pa(luat_audio_codec_conf_t* conf,uint8_t on){
  29. if (conf->pa_pin == LUAT_CODEC_PA_NONE) return;
  30. if (on){
  31. luat_gpio_set(conf->pa_pin, conf->pa_on_level);
  32. }else{
  33. luat_gpio_set(conf->pa_pin, !conf->pa_on_level);
  34. }
  35. }
  36. static int tm8211_mode_pwrdown(luat_audio_codec_conf_t* conf){
  37. if (conf->power_pin != LUAT_CODEC_PA_NONE){
  38. luat_gpio_set(conf->power_pin, !conf->power_on_level);
  39. }
  40. }
  41. static int tm8211_codec_control(luat_audio_codec_conf_t* conf,luat_audio_codec_ctl_t cmd,uint32_t data){
  42. switch (cmd){
  43. case LUAT_CODEC_SET_PA:
  44. tm8211_codec_pa(conf,(uint8_t)data);
  45. break;
  46. case LUAT_CODEC_MODE_PWRDOWN:
  47. tm8211_mode_pwrdown(conf);
  48. break;
  49. default:
  50. break;
  51. }
  52. return 0;
  53. }
  54. static int tm8211_codec_start(luat_audio_codec_conf_t* conf){
  55. tm8211_codec_pa(conf,1);
  56. if (conf->power_pin != LUAT_CODEC_PA_NONE){
  57. luat_gpio_set(conf->power_pin, conf->power_on_level);
  58. }
  59. return 0;
  60. }
  61. static int tm8211_codec_stop(luat_audio_codec_conf_t* conf){
  62. tm8211_codec_pa(conf,0);
  63. if (conf->power_pin != LUAT_CODEC_PA_NONE){
  64. luat_gpio_set(conf->power_pin, !conf->power_on_level);
  65. }
  66. return 0;
  67. }
  68. const luat_audio_codec_opts_t codec_opts_tm8211 = {
  69. .name = "tm8211",
  70. .init = tm8211_codec_init,
  71. .deinit = tm8211_codec_deinit,
  72. .control = tm8211_codec_control,
  73. .start = tm8211_codec_start,
  74. .stop = tm8211_codec_stop,
  75. .no_control = 1,
  76. };