pcm.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef _PCM_H
  2. #define _PCM_H
  3. #include "task/task.h"
  4. #include "platform.h"
  5. //#define DEBUG_PIN 2
  6. #ifdef DEBUG_PIN
  7. # define dbg_platform_gpio_write(level) platform_gpio_write( DEBUG_PIN, level )
  8. # define dbg_platform_gpio_mode(mode, pull) platform_gpio_mode( DEBUG_PIN, mode, pull)
  9. #else
  10. # define dbg_platform_gpio_write(level)
  11. # define dbg_platform_gpio_mode(mode, pull)
  12. #endif
  13. #define BASE_RATE 1000000
  14. enum pcm_driver_index {
  15. PCM_DRIVER_SD = 0,
  16. PCM_DRIVER_END = 1
  17. };
  18. enum pcm_rate_index {
  19. PCM_RATE_1K = 0,
  20. PCM_RATE_2K = 1,
  21. PCM_RATE_4K = 2,
  22. PCM_RATE_5K = 3,
  23. PCM_RATE_8K = 4,
  24. PCM_RATE_10K = 5,
  25. PCM_RATE_12K = 6,
  26. PCM_RATE_16K = 7,
  27. };
  28. static const uint16_t pcm_rate_def[] = {BASE_RATE / 1000, BASE_RATE / 2000, BASE_RATE / 4000,
  29. BASE_RATE / 5000, BASE_RATE / 8000, BASE_RATE / 10000,
  30. BASE_RATE / 12000, BASE_RATE / 16000};
  31. typedef struct {
  32. // available bytes in buffer
  33. size_t len;
  34. // next read position
  35. size_t rpos;
  36. // empty flag
  37. uint8_t empty;
  38. // allocated size
  39. size_t buf_size;
  40. uint8_t *data;
  41. } pcm_buf_t;
  42. typedef struct {
  43. // selected sample rate
  44. int rate;
  45. // ISR throttle control
  46. // -1 = ISR and data task throttled
  47. // 1 = ISR throttled
  48. // 0 = all running
  49. sint8_t isr_throttled;
  50. // buffer selectors
  51. uint8_t rbuf_idx; // read by ISR
  52. uint8_t fbuf_idx; // fill by data task
  53. // callback fn refs
  54. int self_ref;
  55. int cb_data_ref, cb_drained_ref, cb_paused_ref, cb_stopped_ref, cb_vu_ref;
  56. // data buffers
  57. pcm_buf_t bufs[2];
  58. // vu measuring
  59. uint8_t vu_freq;
  60. uint16_t vu_req_samples, vu_samples_tmp;
  61. uint16_t vu_peak_tmp, vu_peak;
  62. // sigma-delta: output pin
  63. int pin;
  64. } cfg_t;
  65. typedef uint8_t (*drv_fn_t)(cfg_t *);
  66. typedef struct {
  67. drv_fn_t init;
  68. drv_fn_t close;
  69. drv_fn_t play;
  70. drv_fn_t record;
  71. drv_fn_t stop;
  72. } drv_t;
  73. typedef struct {
  74. cfg_t cfg;
  75. const drv_t *drv;
  76. } pud_t;
  77. void pcm_data_vu( task_param_t param, uint8 prio );
  78. void pcm_data_play( task_param_t param, uint8 prio );
  79. // task handles
  80. extern task_handle_t pcm_data_vu_task, pcm_data_play_task, pcm_start_play_task;
  81. #endif /* _PCM_H */