|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2020 Jeff Epler for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdint.h> |
| 28 | +#include <string.h> |
| 29 | + |
| 30 | +#include "mpconfigport.h" |
| 31 | + |
| 32 | +#include "bindings/espidf/__init__.h" |
| 33 | + |
| 34 | +// Some boards don't implement I2SOut, so suppress any routines from here. |
| 35 | +#if CIRCUITPY_AUDIOBUSIO_I2SOUT |
| 36 | + |
| 37 | +#include "extmod/vfs_fat.h" |
| 38 | +#include "py/gc.h" |
| 39 | +#include "py/mperrno.h" |
| 40 | +#include "py/runtime.h" |
| 41 | +#include "common-hal/audiobusio/I2SOut.h" |
| 42 | +#include "shared-bindings/audiobusio/I2SOut.h" |
| 43 | +#include "shared-bindings/audiocore/RawSample.h" |
| 44 | +#include "shared-bindings/microcontroller/Pin.h" |
| 45 | +#include "supervisor/shared/translate.h" |
| 46 | + |
| 47 | +#include "driver/i2s.h" |
| 48 | + |
| 49 | +// Caller validates that pins are free. |
| 50 | +void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self, |
| 51 | + const mcu_pin_obj_t* bit_clock, const mcu_pin_obj_t* word_select, |
| 52 | + const mcu_pin_obj_t* data, bool left_justified) { |
| 53 | + |
| 54 | + port_i2s_allocate_init(&self->peripheral, left_justified); |
| 55 | + |
| 56 | + i2s_pin_config_t i2s_pin_config = { |
| 57 | + .bck_io_num = bit_clock->number, |
| 58 | + .ws_io_num = word_select->number, |
| 59 | + .data_out_num = data->number, |
| 60 | + .data_in_num = I2S_PIN_NO_CHANGE, |
| 61 | + }; |
| 62 | + ESP_CALL_RAISE(i2s_set_pin(self->peripheral.instance, &i2s_pin_config)); |
| 63 | + self->bit_clock = bit_clock; |
| 64 | + self->word_select = word_select; |
| 65 | + self->data = data; |
| 66 | +} |
| 67 | + |
| 68 | +bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t* self) { |
| 69 | + return self->peripheral.instance == -1; |
| 70 | +} |
| 71 | + |
| 72 | +void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) { |
| 73 | + if (common_hal_audiobusio_i2sout_deinited(self)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + if (self->bit_clock) { |
| 78 | + reset_pin_number(self->bit_clock->number); |
| 79 | + } |
| 80 | + self->bit_clock = NULL; |
| 81 | + |
| 82 | + if (self->word_select) { |
| 83 | + reset_pin_number(self->word_select->number); |
| 84 | + } |
| 85 | + self->word_select = NULL; |
| 86 | + |
| 87 | + if (self->data) { |
| 88 | + reset_pin_number(self->data->number); |
| 89 | + } |
| 90 | + self->data = NULL; |
| 91 | + |
| 92 | + if (self->peripheral.instance >= 0) { |
| 93 | + port_i2s_reset_instance(self->peripheral.instance); |
| 94 | + } |
| 95 | + self->peripheral.instance = -1; |
| 96 | +} |
| 97 | + |
| 98 | +void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self, |
| 99 | + mp_obj_t sample, bool loop) { |
| 100 | + if (common_hal_audiobusio_i2sout_get_playing(self)) { |
| 101 | + common_hal_audiobusio_i2sout_stop(self); |
| 102 | + } |
| 103 | + port_i2s_play(&self->peripheral, sample, loop); |
| 104 | +} |
| 105 | + |
| 106 | +void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t* self) { |
| 107 | + port_i2s_pause(&self->peripheral); |
| 108 | +} |
| 109 | + |
| 110 | +void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t* self) { |
| 111 | + port_i2s_resume(&self->peripheral); |
| 112 | +} |
| 113 | + |
| 114 | +bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t* self) { |
| 115 | + return port_i2s_paused(&self->peripheral); |
| 116 | +} |
| 117 | + |
| 118 | +void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t* self) { |
| 119 | + port_i2s_stop(&self->peripheral); |
| 120 | +} |
| 121 | + |
| 122 | +bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t* self) { |
| 123 | + return port_i2s_playing(&self->peripheral); |
| 124 | +} |
| 125 | + |
| 126 | +#endif // CIRCUITPY_AUDIOBUSIO_I2SOUT |
0 commit comments