BlueI2S.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. I2SIn and I2SOut for Raspberry Pi Pico
  3. Implements one or more I2S interfaces using DMA
  4. Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #include <Arduino.h>
  18. #include "BlueI2S.h"
  19. #include "blue_pio_i2s.pio.h"
  20. #include <pico/stdlib.h>
  21. I2S::I2S() {
  22. _running = false;
  23. _div_int = 48;
  24. _div_frac = 0;
  25. _bps = 16;
  26. _pio = pio0_hw;
  27. _sm = 1;
  28. _pinBCLK = 26;
  29. _pinDOUT = 28;
  30. }
  31. I2S::~I2S() {
  32. end();
  33. }
  34. bool I2S::setBCLK(pin_size_t pin) {
  35. if (_running || (pin > 28)) {
  36. return false;
  37. }
  38. _pinBCLK = pin;
  39. return true;
  40. }
  41. bool I2S::setDATA(pin_size_t pin) {
  42. if (_running || (pin > 29)) {
  43. return false;
  44. }
  45. _pinDOUT = pin;
  46. return true;
  47. }
  48. bool I2S::setBitsPerSample(int bps) {
  49. if (_running || ((bps != 8) && (bps != 16) && (bps != 24) && (bps != 32))) {
  50. return false;
  51. }
  52. _bps = bps;
  53. return true;
  54. }
  55. volatile void *I2S::getPioFIFOAddr()
  56. {
  57. return (volatile void *)&_pio->txf[_sm];
  58. }
  59. bool I2S::setDivider(uint16_t div_int, uint8_t div_frac) {
  60. _div_int = div_int;
  61. _div_frac = div_frac;
  62. return true;
  63. }
  64. uint I2S::getPioDreq() {
  65. return pio_get_dreq(_pio, _sm, true);
  66. }
  67. bool I2S::begin(PIO pio, uint sm) {
  68. if (_running)
  69. return true;
  70. _pio = pio;
  71. _sm = sm;
  72. _running = true;
  73. int off = 0;
  74. pio_sm_claim(_pio, _sm);
  75. off = pio_add_program(_pio, &pio_i2s_out_program);
  76. pio_i2s_out_program_init(_pio, _sm, off, _pinDOUT, _pinBCLK, _bps);
  77. pio_sm_set_clkdiv_int_frac(_pio, _sm, _div_int, _div_frac);
  78. pio_sm_set_enabled(_pio, _sm, true);
  79. return true;
  80. }
  81. void I2S::end() {
  82. if (_running) {
  83. pio_sm_set_enabled(_pio, _sm, false);
  84. _running = false;
  85. }
  86. }