2019-11-02 11:52:26 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2019 Artur Pacholec
|
2020-05-27 11:45:15 -04:00
|
|
|
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
|
2019-11-02 11:52:26 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
mimxrt10xx: implement i2sout
tested on metro m7 (green prototype version) with max98357a i2s amplifier and the following test code:
```py
import board
import time
import digitalio
from audiobusio import I2SOut
from audiocore import RawSample
from microcontroller import pin
from ulab import numpy as np
n = np.array(np.sin(np.linspace(0, np.pi*2, 218, endpoint=False)) * 200, dtype=np.int16)
print(n)
r = RawSample(n, sample_rate=8000, channel_count=2)
def main():
with digitalio.DigitalInOut(board.LED) as l:
l.switch_to_output(True)
value = False
while True:
with I2SOut(pin.GPIO_06, pin.GPIO_07, pin.GPIO_04) as i:
time.sleep(.01)
l.value = value = not value
i.play(r, loop=True)
print(i.playing)
time.sleep(.5)
i.stop()
print("STOPPED")
print(i.playing)
time.sleep(.5)
i.play(r, loop=True)
print(i.playing)
print("PLAY AGAIN")
time.sleep(.5)
time.sleep(1)
```
Only stereo, 16-bit, raw samples were tested; the sample rate is actually fixed
at 48kHz in the core right now. There is more to do, but the basics work.
# Conflicts:
# ports/mimxrt10xx/Makefile
# ports/mimxrt10xx/mpconfigport.mk
2023-03-22 13:14:00 -04:00
|
|
|
#include "py/runtime.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
mimxrt10xx: implement i2sout
tested on metro m7 (green prototype version) with max98357a i2s amplifier and the following test code:
```py
import board
import time
import digitalio
from audiobusio import I2SOut
from audiocore import RawSample
from microcontroller import pin
from ulab import numpy as np
n = np.array(np.sin(np.linspace(0, np.pi*2, 218, endpoint=False)) * 200, dtype=np.int16)
print(n)
r = RawSample(n, sample_rate=8000, channel_count=2)
def main():
with digitalio.DigitalInOut(board.LED) as l:
l.switch_to_output(True)
value = False
while True:
with I2SOut(pin.GPIO_06, pin.GPIO_07, pin.GPIO_04) as i:
time.sleep(.01)
l.value = value = not value
i.play(r, loop=True)
print(i.playing)
time.sleep(.5)
i.stop()
print("STOPPED")
print(i.playing)
time.sleep(.5)
i.play(r, loop=True)
print(i.playing)
print("PLAY AGAIN")
time.sleep(.5)
time.sleep(1)
```
Only stereo, 16-bit, raw samples were tested; the sample rate is actually fixed
at 48kHz in the core right now. There is more to do, but the basics work.
# Conflicts:
# ports/mimxrt10xx/Makefile
# ports/mimxrt10xx/mpconfigport.mk
2023-03-22 13:14:00 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2023-03-07 11:37:08 -05:00
|
|
|
#include "py/gc.h"
|
|
|
|
|
2023-05-11 18:02:56 -04:00
|
|
|
STATIC bool claimed_pins[PAD_COUNT];
|
|
|
|
STATIC bool never_reset_pins[PAD_COUNT];
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2022-12-15 13:17:28 -05:00
|
|
|
// Default is that no pins are forbidden to reset.
|
|
|
|
MP_WEAK const mcu_pin_obj_t *mimxrt10xx_reset_forbidden_pins[] = {
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC bool _reset_forbidden(const mcu_pin_obj_t *pin) {
|
|
|
|
const mcu_pin_obj_t **forbidden_pin = &mimxrt10xx_reset_forbidden_pins[0];
|
|
|
|
while (*forbidden_pin) {
|
|
|
|
if (pin == *forbidden_pin) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
forbidden_pin++;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-11 11:15:02 -04:00
|
|
|
// There are two numbering systems used here:
|
2020-05-30 05:44:13 -04:00
|
|
|
// IOMUXC index, used for iterating through pins and accessing reset information,
|
|
|
|
// and GPIO port and number, used to store claimed and reset tagging. The two number
|
2020-05-11 11:15:02 -04:00
|
|
|
// systems are not related and one cannot determine the other without a pin object
|
2019-11-02 11:52:26 -04:00
|
|
|
void reset_all_pins(void) {
|
2023-05-11 18:02:56 -04:00
|
|
|
for (uint8_t i = 0; i < PAD_COUNT; i++) {
|
2020-05-11 11:15:02 -04:00
|
|
|
claimed_pins[i] = never_reset_pins[i];
|
|
|
|
}
|
2023-05-11 18:02:56 -04:00
|
|
|
for (uint8_t i = 0; i < PAD_COUNT; i++) {
|
2022-02-18 20:57:54 -05:00
|
|
|
mcu_pin_obj_t *pin = mcu_pin_globals.map.table[i].value;
|
|
|
|
if (never_reset_pins[pin->mux_idx]) {
|
|
|
|
continue;
|
2020-05-11 11:15:02 -04:00
|
|
|
}
|
2022-12-14 19:34:26 -05:00
|
|
|
common_hal_reset_pin(pin);
|
2020-05-11 11:15:02 -04:00
|
|
|
}
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
2022-12-14 19:34:26 -05:00
|
|
|
MP_WEAK bool mimxrt10xx_board_reset_pin_number(const mcu_pin_obj_t *pin) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:44:13 -04:00
|
|
|
// Since i.MX pins need extra register and reset information to reset properly,
|
|
|
|
// resetting pins by number alone has been removed.
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_reset_pin(const mcu_pin_obj_t *pin) {
|
2020-12-28 09:34:00 -05:00
|
|
|
if (pin == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-14 19:34:26 -05:00
|
|
|
|
2022-12-15 13:17:28 -05:00
|
|
|
if (_reset_forbidden(pin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:34:26 -05:00
|
|
|
// Give the board a chance to reset the pin in a particular way, or not reset it at all.
|
|
|
|
if (mimxrt10xx_board_reset_pin_number(pin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-06 17:04:39 -05:00
|
|
|
disable_pin_change_interrupt(pin);
|
2020-05-19 14:20:07 -04:00
|
|
|
never_reset_pins[pin->mux_idx] = false;
|
|
|
|
claimed_pins[pin->mux_idx] = false;
|
2023-05-11 18:02:56 -04:00
|
|
|
|
|
|
|
// This should never be true, but protect against it anyway.
|
|
|
|
if (pin->mux_reg == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
*(uint32_t *)pin->mux_reg = pin->mux_reset;
|
|
|
|
*(uint32_t *)pin->cfg_reg = pin->pad_reset;
|
2020-05-11 11:15:02 -04:00
|
|
|
}
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
|
2022-02-18 20:57:54 -05:00
|
|
|
if (pin == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-19 14:20:07 -04:00
|
|
|
never_reset_pins[pin->mux_idx] = true;
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {
|
2020-05-19 14:20:07 -04:00
|
|
|
return !claimed_pins[pin->mux_idx];
|
2020-05-11 11:15:02 -04:00
|
|
|
}
|
2019-11-02 11:52:26 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
uint8_t common_hal_mcu_pin_number(const mcu_pin_obj_t *pin) {
|
2020-05-11 11:15:02 -04:00
|
|
|
return pin->mux_idx; // returns IOMUXC to align with pin table
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
|
2020-05-19 14:20:07 -04:00
|
|
|
claimed_pins[pin->mux_idx] = true;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void claim_pin(const mcu_pin_obj_t *pin) {
|
2020-05-19 14:20:07 -04:00
|
|
|
common_hal_mcu_pin_claim(pin);
|
2019-11-02 11:52:26 -04:00
|
|
|
}
|
2020-02-19 16:23:32 -05:00
|
|
|
|
2020-05-11 11:15:02 -04:00
|
|
|
void common_hal_mcu_pin_reset_number(uint8_t pin_no) {
|
2021-03-15 09:57:36 -04:00
|
|
|
common_hal_reset_pin((mcu_pin_obj_t *)(mcu_pin_globals.map.table[pin_no].value));
|
2020-02-19 16:23:32 -05:00
|
|
|
}
|
mimxrt10xx: implement i2sout
tested on metro m7 (green prototype version) with max98357a i2s amplifier and the following test code:
```py
import board
import time
import digitalio
from audiobusio import I2SOut
from audiocore import RawSample
from microcontroller import pin
from ulab import numpy as np
n = np.array(np.sin(np.linspace(0, np.pi*2, 218, endpoint=False)) * 200, dtype=np.int16)
print(n)
r = RawSample(n, sample_rate=8000, channel_count=2)
def main():
with digitalio.DigitalInOut(board.LED) as l:
l.switch_to_output(True)
value = False
while True:
with I2SOut(pin.GPIO_06, pin.GPIO_07, pin.GPIO_04) as i:
time.sleep(.01)
l.value = value = not value
i.play(r, loop=True)
print(i.playing)
time.sleep(.5)
i.stop()
print("STOPPED")
print(i.playing)
time.sleep(.5)
i.play(r, loop=True)
print(i.playing)
print("PLAY AGAIN")
time.sleep(.5)
time.sleep(1)
```
Only stereo, 16-bit, raw samples were tested; the sample rate is actually fixed
at 48kHz in the core right now. There is more to do, but the basics work.
# Conflicts:
# ports/mimxrt10xx/Makefile
# ports/mimxrt10xx/mpconfigport.mk
2023-03-22 13:14:00 -04:00
|
|
|
|
|
|
|
const mcu_periph_obj_t *find_pin_function_sz(const mcu_periph_obj_t *list, size_t sz, const mcu_pin_obj_t *pin, int *instance, uint16_t name) {
|
|
|
|
if (!pin) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < sz; i++) {
|
|
|
|
if (*instance != -1 && *instance != list[i].bank_idx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (pin == list[i].pin) {
|
|
|
|
*instance = list[i].bank_idx;
|
|
|
|
return &list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_raise_ValueError_varg(translate("Invalid %q pin"), name);
|
|
|
|
}
|