mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
Merge pull request #1889 from openmv/refactor_common_header
misc: Rename common header.
This commit is contained in:
commit
d85935e787
@ -16,7 +16,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_gpio.h"
|
#include "omv_gpio.h"
|
||||||
#include "omv_spi.h"
|
#include "omv_spi.h"
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include "winc.h"
|
#include "winc.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
// WINC's includes
|
// WINC's includes
|
||||||
#include "driver/include/nmasic.h"
|
#include "driver/include/nmasic.h"
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
#include "dma_alloc.h"
|
#include "dma_alloc.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
typedef union block block_t;
|
typedef union block block_t;
|
||||||
union block {
|
union block {
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
#include "extmod/vfs.h"
|
#include "extmod/vfs.h"
|
||||||
#include "extmod/vfs_fat.h"
|
#include "extmod/vfs_fat.h"
|
||||||
|
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "fb_alloc.h"
|
#include "fb_alloc.h"
|
||||||
#include "ff_wrapper.h"
|
#include "ff_wrapper.h"
|
||||||
#define FF_MIN(x, y) (((x) < (y))?(x):(y))
|
#define FF_MIN(x, y) (((x) < (y))?(x):(y))
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#include "vospi.h"
|
#include "vospi.h"
|
||||||
#include "crc16.h"
|
#include "crc16.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_spi.h"
|
#include "omv_spi.h"
|
||||||
|
|
||||||
#define VOSPI_LINE_PIXELS (80)
|
#define VOSPI_LINE_PIXELS (80)
|
||||||
|
|||||||
@ -11,7 +11,7 @@
|
|||||||
#include <arm_math.h>
|
#include <arm_math.h>
|
||||||
#include "fb_alloc.h"
|
#include "fb_alloc.h"
|
||||||
#include "ff_wrapper.h"
|
#include "ff_wrapper.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "fft.h"
|
#include "fft.h"
|
||||||
// http://processors.wiki.ti.com/index.php/Efficient_FFT_Computation_of_Real_Input
|
// http://processors.wiki.ti.com/index.php/Efficient_FFT_Computation_of_Real_Input
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
* Fast approximate math functions.
|
* Fast approximate math functions.
|
||||||
*/
|
*/
|
||||||
#include "fmath.h"
|
#include "fmath.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
#define M_PI 3.14159265f
|
#define M_PI 3.14159265f
|
||||||
#define M_PI_2 1.57079632f
|
#define M_PI_2 1.57079632f
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "imlib.h"
|
#include "imlib.h"
|
||||||
#include "mutex.h"
|
#include "mutex.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
// DMA Buffers need to be aligned by cache lines or 16 bytes.
|
// DMA Buffers need to be aligned by cache lines or 16 bytes.
|
||||||
#ifndef __DCACHE_PRESENT
|
#ifndef __DCACHE_PRESENT
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
#include "array.h"
|
#include "array.h"
|
||||||
#include "ff_wrapper.h"
|
#include "ff_wrapper.h"
|
||||||
#include "imlib.h"
|
#include "imlib.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
|
|
||||||
void imlib_init_all() {
|
void imlib_init_all() {
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include "mimxrt_hal.h"
|
#include "mimxrt_hal.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "omv_gpio.h"
|
#include "omv_gpio.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
#define IOMUXC_SET_REG(base, reg, val) *((volatile uint32_t *) ((((uint32_t) base) << 16) | reg)) = (val)
|
#define IOMUXC_SET_REG(base, reg, val) *((volatile uint32_t *) ((((uint32_t) base) << 16) | reg)) = (val)
|
||||||
|
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
#include CLOCK_CONFIG_H
|
#include CLOCK_CONFIG_H
|
||||||
|
|
||||||
#include "mimxrt_hal.h"
|
#include "mimxrt_hal.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_spi.h"
|
#include "omv_spi.h"
|
||||||
|
|
||||||
typedef struct dma_descr {
|
typedef struct dma_descr {
|
||||||
|
|||||||
@ -21,21 +21,21 @@
|
|||||||
#include "py_helper.h"
|
#include "py_helper.h"
|
||||||
#include "fb_alloc.h"
|
#include "fb_alloc.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "nrfx_pdm.h"
|
#include "nrfx_pdm.h"
|
||||||
#include "hal/nrf_gpio.h"
|
#include "hal/nrf_gpio.h"
|
||||||
|
|
||||||
#if MICROPY_PY_AUDIO
|
#if MICROPY_PY_AUDIO
|
||||||
#define PDM_DEFAULT_GAIN (20)
|
#define PDM_DEFAULT_GAIN (20)
|
||||||
#define PDM_IRQ_PRIORITY (7)
|
#define PDM_IRQ_PRIORITY (7)
|
||||||
#define PDM_BUFFER_SIZE (256)
|
#define PDM_BUFFER_SIZE (256)
|
||||||
|
|
||||||
#define NRF_PDM_FREQ_1280K (nrf_pdm_freq_t)(0x0A000000UL) // PDM_CLK= 1.280 MHz (32 MHz / 25) => Fs= 20000 Hz
|
#define NRF_PDM_FREQ_1280K (nrf_pdm_freq_t) (0x0A000000UL) // PDM_CLK= 1.280 MHz (32 MHz / 25) => Fs= 20000 Hz
|
||||||
#define NRF_PDM_FREQ_2000K (nrf_pdm_freq_t)(0x10000000UL) // PDM_CLK= 2.000 MHz (32 MHz / 16) => Fs= 31250 Hz
|
#define NRF_PDM_FREQ_2000K (nrf_pdm_freq_t) (0x10000000UL) // PDM_CLK= 2.000 MHz (32 MHz / 16) => Fs= 31250 Hz
|
||||||
#define NRF_PDM_FREQ_2667K (nrf_pdm_freq_t)(0x15000000UL) // PDM_CLK= 2.667 MHz (32 MHz / 12) => Fs= 41667 Hz
|
#define NRF_PDM_FREQ_2667K (nrf_pdm_freq_t) (0x15000000UL) // PDM_CLK= 2.667 MHz (32 MHz / 12) => Fs= 41667 Hz
|
||||||
#define NRF_PDM_FREQ_3200K (nrf_pdm_freq_t)(0x19000000UL) // PDM_CLK= 3.200 MHz (32 MHz / 10) => Fs= 50000 Hz
|
#define NRF_PDM_FREQ_3200K (nrf_pdm_freq_t) (0x19000000UL) // PDM_CLK= 3.200 MHz (32 MHz / 10) => Fs= 50000 Hz
|
||||||
#define NRF_PDM_FREQ_4000K (nrf_pdm_freq_t)(0x20000000UL) // PDM_CLK= 4.000 MHz (32 MHz / 8) => Fs= 62500 Hz
|
#define NRF_PDM_FREQ_4000K (nrf_pdm_freq_t) (0x20000000UL) // PDM_CLK= 4.000 MHz (32 MHz / 8) => Fs= 62500 Hz
|
||||||
#define RAISE_OS_EXCEPTION(msg) mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT(msg))
|
#define RAISE_OS_EXCEPTION(msg) mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT(msg))
|
||||||
|
|
||||||
static mp_obj_array_t *g_pcmbuf = NULL;
|
static mp_obj_array_t *g_pcmbuf = NULL;
|
||||||
static mp_obj_t g_audio_callback = mp_const_none;
|
static mp_obj_t g_audio_callback = mp_const_none;
|
||||||
@ -45,13 +45,13 @@ static int16_t PDM_BUFFERS[2][PDM_BUFFER_SIZE];
|
|||||||
static mp_sched_node_t audio_task_sched_node;
|
static mp_sched_node_t audio_task_sched_node;
|
||||||
static void audio_task_callback(mp_sched_node_t *node);
|
static void audio_task_callback(mp_sched_node_t *node);
|
||||||
|
|
||||||
static void nrfx_pdm_event_handler(nrfx_pdm_evt_t const *evt)
|
static void nrfx_pdm_event_handler(nrfx_pdm_evt_t const *evt) {
|
||||||
{
|
|
||||||
if (evt->error == NRFX_PDM_NO_ERROR) {
|
if (evt->error == NRFX_PDM_NO_ERROR) {
|
||||||
if (evt->buffer_requested) {
|
if (evt->buffer_requested) {
|
||||||
if (evt->buffer_released == PDM_BUFFERS[0]) {
|
if (evt->buffer_released == PDM_BUFFERS[0]) {
|
||||||
nrfx_pdm_buffer_set(PDM_BUFFERS[1], PDM_BUFFER_SIZE);
|
nrfx_pdm_buffer_set(PDM_BUFFERS[1], PDM_BUFFER_SIZE);
|
||||||
} else { // NULL (first buffer request) or second buffer is full.
|
} else {
|
||||||
|
// NULL (first buffer request) or second buffer is full.
|
||||||
nrfx_pdm_buffer_set(PDM_BUFFERS[0], PDM_BUFFER_SIZE);
|
nrfx_pdm_buffer_set(PDM_BUFFERS[0], PDM_BUFFER_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,24 +64,23 @@ static void nrfx_pdm_event_handler(nrfx_pdm_evt_t const *evt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_args)
|
static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||||
{
|
|
||||||
// Read Args.
|
// Read Args.
|
||||||
g_channels = py_helper_keyword_int(n_args, args, 0, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_channels), 2);
|
g_channels = py_helper_keyword_int(n_args, args, 0, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_channels), 2);
|
||||||
uint32_t frequency = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_frequency), 16000);
|
uint32_t frequency = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_frequency), 16000);
|
||||||
int gain_db = py_helper_keyword_int(n_args, args, 2, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_gain_db), PDM_DEFAULT_GAIN);
|
int gain_db = py_helper_keyword_int(n_args, args, 2, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_gain_db), PDM_DEFAULT_GAIN);
|
||||||
|
|
||||||
nrfx_pdm_config_t nrfx_pdm_config = {
|
nrfx_pdm_config_t nrfx_pdm_config = {
|
||||||
.pin_clk = PDM_CLK_PIN,
|
.pin_clk = PDM_CLK_PIN,
|
||||||
.pin_din = PDM_DIN_PIN,
|
.pin_din = PDM_DIN_PIN,
|
||||||
.gain_l = gain_db,
|
.gain_l = gain_db,
|
||||||
.gain_r = gain_db,
|
.gain_r = gain_db,
|
||||||
.interrupt_priority = PDM_IRQ_PRIORITY,
|
.interrupt_priority = PDM_IRQ_PRIORITY,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Enable high frequency oscillator if not already enabled
|
// Enable high frequency oscillator if not already enabled
|
||||||
if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
|
if (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
|
||||||
NRF_CLOCK->TASKS_HFCLKSTART = 1;
|
NRF_CLOCK->TASKS_HFCLKSTART = 1;
|
||||||
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
|
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,8 +124,7 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_ar
|
|||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
||||||
void py_audio_deinit()
|
void py_audio_deinit() {
|
||||||
{
|
|
||||||
// Disable PDM and IRQ
|
// Disable PDM and IRQ
|
||||||
nrfx_pdm_uninit();
|
nrfx_pdm_uninit();
|
||||||
|
|
||||||
@ -141,15 +139,13 @@ void py_audio_deinit()
|
|||||||
g_audio_callback = mp_const_none;
|
g_audio_callback = mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void audio_task_callback(mp_sched_node_t *node)
|
static void audio_task_callback(mp_sched_node_t *node) {
|
||||||
{
|
|
||||||
memcpy(g_pcmbuf->items, PDM_BUFFERS[g_buf_index], PDM_BUFFER_SIZE * sizeof(int16_t));
|
memcpy(g_pcmbuf->items, PDM_BUFFERS[g_buf_index], PDM_BUFFER_SIZE * sizeof(int16_t));
|
||||||
// Call user callback
|
// Call user callback
|
||||||
mp_call_function_1(g_audio_callback, MP_OBJ_FROM_PTR(g_pcmbuf));
|
mp_call_function_1(g_audio_callback, MP_OBJ_FROM_PTR(g_pcmbuf));
|
||||||
}
|
}
|
||||||
|
|
||||||
static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj)
|
static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj) {
|
||||||
{
|
|
||||||
g_audio_callback = callback_obj;
|
g_audio_callback = callback_obj;
|
||||||
|
|
||||||
if (!mp_obj_is_callable(g_audio_callback)) {
|
if (!mp_obj_is_callable(g_audio_callback)) {
|
||||||
@ -166,8 +162,7 @@ static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj)
|
|||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
|
||||||
static mp_obj_t py_audio_stop_streaming()
|
static mp_obj_t py_audio_stop_streaming() {
|
||||||
{
|
|
||||||
// Stop PDM.
|
// Stop PDM.
|
||||||
nrfx_pdm_stop();
|
nrfx_pdm_stop();
|
||||||
g_audio_callback = mp_const_none;
|
g_audio_callback = mp_const_none;
|
||||||
@ -189,7 +184,7 @@ STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table);
|
|||||||
|
|
||||||
const mp_obj_module_t audio_module = {
|
const mp_obj_module_t audio_module = {
|
||||||
.base = { &mp_type_module },
|
.base = { &mp_type_module },
|
||||||
.globals = (mp_obj_t)&globals_dict,
|
.globals = (mp_obj_t) &globals_dict,
|
||||||
};
|
};
|
||||||
|
|
||||||
MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
|
MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "omv_i2c.h"
|
#include "omv_i2c.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
#define I2C_TIMEOUT (1000)
|
#define I2C_TIMEOUT (1000)
|
||||||
#define I2C_SCAN_TIMEOUT (100)
|
#define I2C_SCAN_TIMEOUT (100)
|
||||||
|
|||||||
@ -31,12 +31,12 @@
|
|||||||
#endif
|
#endif
|
||||||
#include "py_audio.h"
|
#include "py_audio.h"
|
||||||
|
|
||||||
#define PDM_DEFAULT_GAIN (8)
|
#define PDM_DEFAULT_GAIN (8)
|
||||||
#define PDM_DEFAULT_FREQ (16000)
|
#define PDM_DEFAULT_FREQ (16000)
|
||||||
#define PDM_DEFAULT_BUFFERS (128) // Number of PCM samples buffers.
|
#define PDM_DEFAULT_BUFFERS (128) // Number of PCM samples buffers.
|
||||||
#define PDM_BUFFER_SIZE (512)
|
#define PDM_BUFFER_SIZE (512)
|
||||||
#define PDM_TIME_CONV (0) // Enable to print average conversion time.
|
#define PDM_TIME_CONV (0) // Enable to print average conversion time.
|
||||||
#define RAISE_OS_EXCEPTION(msg) mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT(msg))
|
#define RAISE_OS_EXCEPTION(msg) mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT(msg))
|
||||||
|
|
||||||
typedef struct _audio_data_t {
|
typedef struct _audio_data_t {
|
||||||
volatile uint32_t head;
|
volatile uint32_t head;
|
||||||
@ -58,21 +58,20 @@ typedef struct _audio_data_t {
|
|||||||
uint8_t dma_buf_idx;
|
uint8_t dma_buf_idx;
|
||||||
mp_obj_t user_callback;
|
mp_obj_t user_callback;
|
||||||
TPDMFilter_InitStruct pdm_filter; // OpenPDM filter used to convert PDM into PCM
|
TPDMFilter_InitStruct pdm_filter; // OpenPDM filter used to convert PDM into PCM
|
||||||
void (*pdm_filter_func) (uint8_t*, int16_t*, uint16_t, TPDMFilter_InitStruct*);
|
void (*pdm_filter_func) (uint8_t *, int16_t *, uint16_t, TPDMFilter_InitStruct *);
|
||||||
} audio_data_t;
|
} audio_data_t;
|
||||||
|
|
||||||
MP_REGISTER_ROOT_POINTER(struct _audio_data_t *audio_data);
|
MP_REGISTER_ROOT_POINTER(struct _audio_data_t *audio_data);
|
||||||
|
|
||||||
#define audio_data MP_STATE_PORT(audio_data)
|
#define audio_data MP_STATE_PORT(audio_data)
|
||||||
#define NEXT_BUFFER(x) (((x) + 1) % (audio_data->n_buffers))
|
#define NEXT_BUFFER(x) (((x) + 1) % (audio_data->n_buffers))
|
||||||
|
|
||||||
static bool audio_initialized = false;
|
static bool audio_initialized = false;
|
||||||
static mp_sched_node_t audio_task_sched_node;
|
static mp_sched_node_t audio_task_sched_node;
|
||||||
static volatile bool audio_task_scheduled = false;
|
static volatile bool audio_task_scheduled = false;
|
||||||
static bool irq_handler_installed = false;
|
static bool irq_handler_installed = false;
|
||||||
|
|
||||||
static void audio_task_callback(mp_sched_node_t *node)
|
static void audio_task_callback(mp_sched_node_t *node) {
|
||||||
{
|
|
||||||
if (audio_data->streaming == false) {
|
if (audio_data->streaming == false) {
|
||||||
audio_task_scheduled = false;
|
audio_task_scheduled = false;
|
||||||
return;
|
return;
|
||||||
@ -100,8 +99,7 @@ static void audio_task_callback(mp_sched_node_t *node)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dma_irq_handler()
|
static void dma_irq_handler() {
|
||||||
{
|
|
||||||
if (dma_irqn_get_channel_status(PDM_DMA, audio_data->dma_channel)) {
|
if (dma_irqn_get_channel_status(PDM_DMA, audio_data->dma_channel)) {
|
||||||
// Clear the interrupt request.
|
// Clear the interrupt request.
|
||||||
dma_irqn_acknowledge_channel(PDM_DMA, audio_data->dma_channel);
|
dma_irqn_acknowledge_channel(PDM_DMA, audio_data->dma_channel);
|
||||||
@ -109,7 +107,7 @@ static void dma_irq_handler()
|
|||||||
// Set the next PDM buffer and retrigger the DMA channel
|
// Set the next PDM buffer and retrigger the DMA channel
|
||||||
// immediatley while PDM samples are converted to PCM samples.
|
// immediatley while PDM samples are converted to PCM samples.
|
||||||
dma_channel_set_write_addr(audio_data->dma_channel,
|
dma_channel_set_write_addr(audio_data->dma_channel,
|
||||||
&audio_data->pdm_buffer[(audio_data->dma_buf_idx ^ 1) * PDM_BUFFER_SIZE], true);
|
&audio_data->pdm_buffer[(audio_data->dma_buf_idx ^ 1) * PDM_BUFFER_SIZE], true);
|
||||||
|
|
||||||
#if PDM_TIME_CONV
|
#if PDM_TIME_CONV
|
||||||
mp_uint_t start = mp_hal_ticks_us();
|
mp_uint_t start = mp_hal_ticks_us();
|
||||||
@ -117,9 +115,9 @@ static void dma_irq_handler()
|
|||||||
|
|
||||||
// Convert PDM to PCM samples.
|
// Convert PDM to PCM samples.
|
||||||
audio_data->pdm_filter_func(
|
audio_data->pdm_filter_func(
|
||||||
&audio_data->pdm_buffer[audio_data->dma_buf_idx * PDM_BUFFER_SIZE],
|
&audio_data->pdm_buffer[audio_data->dma_buf_idx * PDM_BUFFER_SIZE],
|
||||||
&audio_data->pcm_buffer[audio_data->tail * audio_data->n_samples],
|
&audio_data->pcm_buffer[audio_data->tail * audio_data->n_samples],
|
||||||
1, &audio_data->pdm_filter);
|
1, &audio_data->pdm_filter);
|
||||||
|
|
||||||
#if PDM_TIME_CONV
|
#if PDM_TIME_CONV
|
||||||
audio_data->conv_total += (mp_hal_ticks_us() - start);
|
audio_data->conv_total += (mp_hal_ticks_us() - start);
|
||||||
@ -147,9 +145,10 @@ static void dma_irq_handler()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
{
|
enum {
|
||||||
enum { ARG_channels, ARG_frequency, ARG_gain_db, ARG_buffers, ARG_overflow, ARG_clkdiv };
|
ARG_channels, ARG_frequency, ARG_gain_db, ARG_buffers, ARG_overflow, ARG_clkdiv
|
||||||
|
};
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_channels, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
|
{ MP_QSTR_channels, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
|
||||||
{ MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PDM_DEFAULT_FREQ} },
|
{ MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PDM_DEFAULT_FREQ} },
|
||||||
@ -169,8 +168,8 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(args[ARG_frequency].u_int == 16000
|
if (!(args[ARG_frequency].u_int == 16000
|
||||||
|| args[ARG_frequency].u_int == 32000
|
|| args[ARG_frequency].u_int == 32000
|
||||||
|| args[ARG_frequency].u_int == 48000)) {
|
|| args[ARG_frequency].u_int == 48000)) {
|
||||||
RAISE_OS_EXCEPTION("Invalid frequency. Expected 16KHz, 32KHz or 48KHz.");
|
RAISE_OS_EXCEPTION("Invalid frequency. Expected 16KHz, 32KHz or 48KHz.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,7 +180,7 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||||||
// 32 KHz * 64 -> 2048 KHz
|
// 32 KHz * 64 -> 2048 KHz
|
||||||
// 48 KHz * 64 -> 3072 KHz
|
// 48 KHz * 64 -> 3072 KHz
|
||||||
uint32_t decimation = (args[ARG_frequency].u_int == 16000) ? 128 : 64;
|
uint32_t decimation = (args[ARG_frequency].u_int == 16000) ? 128 : 64;
|
||||||
uint32_t n_samples = (PDM_BUFFER_SIZE * 8) / decimation;
|
uint32_t n_samples = (PDM_BUFFER_SIZE * 8) / decimation;
|
||||||
|
|
||||||
audio_data = m_new_obj(audio_data_t);
|
audio_data = m_new_obj(audio_data_t);
|
||||||
audio_data->head = 0;
|
audio_data->head = 0;
|
||||||
@ -211,23 +210,23 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||||||
// Instead of returning a new heap object for every buffer, which will result in heap
|
// Instead of returning a new heap object for every buffer, which will result in heap
|
||||||
// fragmentation, reuse the same object and point to PCM buffers returned to the user.
|
// fragmentation, reuse the same object and point to PCM buffers returned to the user.
|
||||||
audio_data->pcm_buffer_user = mp_obj_new_bytearray_by_ref(
|
audio_data->pcm_buffer_user = mp_obj_new_bytearray_by_ref(
|
||||||
audio_data->n_samples * sizeof(int16_t),
|
audio_data->n_samples * sizeof(int16_t),
|
||||||
&audio_data->pcm_buffer[audio_data->n_samples * 0]);
|
&audio_data->pcm_buffer[audio_data->n_samples * 0]);
|
||||||
|
|
||||||
if (audio_data->pcm_buffer == NULL || audio_data->pdm_buffer == NULL) {
|
if (audio_data->pcm_buffer == NULL || audio_data->pdm_buffer == NULL) {
|
||||||
RAISE_OS_EXCEPTION("Failed to allocate memory for PDM/PCM buffer.");
|
RAISE_OS_EXCEPTION("Failed to allocate memory for PDM/PCM buffer.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize OpenPDM filter.
|
// Initialize OpenPDM filter.
|
||||||
audio_data->pdm_filter.Fs = args[ARG_frequency].u_int;
|
audio_data->pdm_filter.Fs = args[ARG_frequency].u_int;
|
||||||
audio_data->pdm_filter.MaxVolume = 1;
|
audio_data->pdm_filter.MaxVolume = 1;
|
||||||
audio_data->pdm_filter.nSamples = n_samples;
|
audio_data->pdm_filter.nSamples = n_samples;
|
||||||
audio_data->pdm_filter.LP_HZ = args[ARG_frequency].u_int / 2;
|
audio_data->pdm_filter.LP_HZ = args[ARG_frequency].u_int / 2;
|
||||||
audio_data->pdm_filter.HP_HZ = 10;
|
audio_data->pdm_filter.HP_HZ = 10;
|
||||||
audio_data->pdm_filter.In_MicChannels = args[ARG_channels].u_int;
|
audio_data->pdm_filter.In_MicChannels = args[ARG_channels].u_int;
|
||||||
audio_data->pdm_filter.Out_MicChannels = args[ARG_channels].u_int;
|
audio_data->pdm_filter.Out_MicChannels = args[ARG_channels].u_int;
|
||||||
audio_data->pdm_filter.Decimation = decimation;
|
audio_data->pdm_filter.Decimation = decimation;
|
||||||
audio_data->pdm_filter.filterGain = args[ARG_gain_db].u_int;
|
audio_data->pdm_filter.filterGain = args[ARG_gain_db].u_int;
|
||||||
Open_PDM_Filter_Init(&audio_data->pdm_filter);
|
Open_PDM_Filter_Init(&audio_data->pdm_filter);
|
||||||
|
|
||||||
// Configure PIO state machine
|
// Configure PIO state machine
|
||||||
@ -259,11 +258,11 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||||||
|
|
||||||
// Configure DMA channel without starting.
|
// Configure DMA channel without starting.
|
||||||
dma_channel_configure(audio_data->dma_channel, &c,
|
dma_channel_configure(audio_data->dma_channel, &c,
|
||||||
audio_data->pdm_buffer, // Destinatinon pointer, first buffer.
|
audio_data->pdm_buffer, // Destinatinon pointer, first buffer.
|
||||||
&PDM_PIO->rxf[PDM_SM], // Source pointer.
|
&PDM_PIO->rxf[PDM_SM], // Source pointer.
|
||||||
PDM_BUFFER_SIZE, // Number of transfers.
|
PDM_BUFFER_SIZE, // Number of transfers.
|
||||||
false // Don't start immediately.
|
false // Don't start immediately.
|
||||||
);
|
);
|
||||||
|
|
||||||
// Setup DMA IRQ handler.
|
// Setup DMA IRQ handler.
|
||||||
// Disable IRQs.
|
// Disable IRQs.
|
||||||
@ -287,20 +286,17 @@ static mp_obj_t py_audio_init(uint n_args, const mp_obj_t *pos_args, mp_map_t *k
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_audio_init_obj, 0, py_audio_init);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_audio_init_obj, 0, py_audio_init);
|
||||||
|
|
||||||
static mp_obj_t py_audio_samples()
|
static mp_obj_t py_audio_samples() {
|
||||||
{
|
|
||||||
return mp_obj_new_int(audio_data->t_samples);
|
return mp_obj_new_int(audio_data->t_samples);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_samples_obj, py_audio_samples);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_samples_obj, py_audio_samples);
|
||||||
|
|
||||||
static mp_obj_t py_audio_overflow()
|
static mp_obj_t py_audio_overflow() {
|
||||||
{
|
|
||||||
return mp_obj_new_bool(audio_data->overflow);
|
return mp_obj_new_bool(audio_data->overflow);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_overflow_obj, py_audio_overflow);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_overflow_obj, py_audio_overflow);
|
||||||
|
|
||||||
static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj)
|
static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj) {
|
||||||
{
|
|
||||||
audio_data->head = 0;
|
audio_data->head = 0;
|
||||||
audio_data->tail = 0;
|
audio_data->tail = 0;
|
||||||
#if PDM_TIME_CONV
|
#if PDM_TIME_CONV
|
||||||
@ -327,8 +323,7 @@ static mp_obj_t py_audio_start_streaming(mp_obj_t callback_obj)
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_audio_start_streaming_obj, py_audio_start_streaming);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_audio_start_streaming_obj, py_audio_start_streaming);
|
||||||
|
|
||||||
static mp_obj_t py_audio_stop_streaming()
|
static mp_obj_t py_audio_stop_streaming() {
|
||||||
{
|
|
||||||
if (audio_data->streaming) {
|
if (audio_data->streaming) {
|
||||||
// Disable PDM and IRQ
|
// Disable PDM and IRQ
|
||||||
dma_channel_abort(audio_data->dma_channel);
|
dma_channel_abort(audio_data->dma_channel);
|
||||||
@ -346,12 +341,14 @@ static mp_obj_t py_audio_stop_streaming()
|
|||||||
|
|
||||||
audio_data->streaming = false;
|
audio_data->streaming = false;
|
||||||
for (mp_uint_t start = mp_hal_ticks_ms();
|
for (mp_uint_t start = mp_hal_ticks_ms();
|
||||||
audio_task_scheduled && (mp_hal_ticks_ms() - start) < 1500;
|
audio_task_scheduled && (mp_hal_ticks_ms() - start) < 1500;
|
||||||
mp_hal_delay_ms(10));
|
mp_hal_delay_ms(10)) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
#if PDM_TIME_CONV
|
#if PDM_TIME_CONV
|
||||||
mp_printf(&mp_plat_print, "Average conversion time:%ld us\n",
|
mp_printf(&mp_plat_print, "Average conversion time:%ld us\n",
|
||||||
(audio_data->conv_total / audio_data->conv_times));
|
(audio_data->conv_total / audio_data->conv_times));
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -359,9 +356,10 @@ static mp_obj_t py_audio_stop_streaming()
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_stop_streaming_obj, py_audio_stop_streaming);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(py_audio_stop_streaming_obj, py_audio_stop_streaming);
|
||||||
|
|
||||||
static mp_obj_t py_audio_get_buffer(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args)
|
static mp_obj_t py_audio_get_buffer(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||||
{
|
enum {
|
||||||
enum { ARG_timeout };
|
ARG_timeout
|
||||||
|
};
|
||||||
static const mp_arg_t allowed_args[] = {
|
static const mp_arg_t allowed_args[] = {
|
||||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||||
};
|
};
|
||||||
@ -399,8 +397,7 @@ static mp_obj_t py_audio_get_buffer(uint n_args, const mp_obj_t *pos_args, mp_ma
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_audio_get_buffer_obj, 0, py_audio_get_buffer);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_audio_get_buffer_obj, 0, py_audio_get_buffer);
|
||||||
|
|
||||||
void py_audio_deinit()
|
void py_audio_deinit() {
|
||||||
{
|
|
||||||
if (audio_initialized) {
|
if (audio_initialized) {
|
||||||
py_audio_stop_streaming();
|
py_audio_stop_streaming();
|
||||||
audio_data->head = 0;
|
audio_data->head = 0;
|
||||||
@ -439,7 +436,7 @@ STATIC MP_DEFINE_CONST_DICT(globals_dict, globals_dict_table);
|
|||||||
|
|
||||||
const mp_obj_module_t audio_module = {
|
const mp_obj_module_t audio_module = {
|
||||||
.base = { &mp_type_module },
|
.base = { &mp_type_module },
|
||||||
.globals = (mp_obj_t)&globals_dict,
|
.globals = (mp_obj_t) &globals_dict,
|
||||||
};
|
};
|
||||||
|
|
||||||
MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
|
MP_REGISTER_MODULE(MP_QSTR_audio, audio_module);
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
|
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "dma_utils.h"
|
#include "dma_utils.h"
|
||||||
|
|
||||||
// Defined in micropython/ports/stm32/dma.c
|
// Defined in micropython/ports/stm32/dma.c
|
||||||
|
|||||||
@ -23,7 +23,7 @@
|
|||||||
#include "pdm2pcm_glo.h"
|
#include "pdm2pcm_glo.h"
|
||||||
#include "fb_alloc.h"
|
#include "fb_alloc.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "dma_utils.h"
|
#include "dma_utils.h"
|
||||||
|
|
||||||
#if MICROPY_PY_AUDIO
|
#if MICROPY_PY_AUDIO
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "libtf.h"
|
#include "libtf.h"
|
||||||
#include "py_tf.h"
|
#include "py_tf.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
|
|
||||||
#if MICROPY_PY_MICRO_SPEECH
|
#if MICROPY_PY_MICRO_SPEECH
|
||||||
#define kMaxAudioSampleSize (512)
|
#define kMaxAudioSampleSize (512)
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
#include "extmod/modnetwork.h"
|
#include "extmod/modnetwork.h"
|
||||||
#include "pin.h"
|
#include "pin.h"
|
||||||
#include "genhdr/pins.h"
|
#include "genhdr/pins.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "py_helper.h"
|
#include "py_helper.h"
|
||||||
#include "ff_wrapper.h"
|
#include "ff_wrapper.h"
|
||||||
|
|
||||||
|
|||||||
@ -14,7 +14,7 @@
|
|||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
|
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_gpio.h"
|
#include "omv_gpio.h"
|
||||||
#include "omv_i2c.h"
|
#include "omv_i2c.h"
|
||||||
|
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "irq.h"
|
#include "irq.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "dma_utils.h"
|
#include "dma_utils.h"
|
||||||
#include "omv_spi.h"
|
#include "omv_spi.h"
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@
|
|||||||
#include "imlib.h"
|
#include "imlib.h"
|
||||||
#include "irq.h"
|
#include "irq.h"
|
||||||
#include "omv_boardconfig.h"
|
#include "omv_boardconfig.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
// Define pin objects in this file.
|
// Define pin objects in this file.
|
||||||
#define OMV_GPIO_DEFINE_PINS (1)
|
#define OMV_GPIO_DEFINE_PINS (1)
|
||||||
#include "omv_gpio.h"
|
#include "omv_gpio.h"
|
||||||
|
|||||||
@ -15,7 +15,7 @@
|
|||||||
#include "sensor.h"
|
#include "sensor.h"
|
||||||
#include "vospi.h"
|
#include "vospi.h"
|
||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
#include "common.h"
|
#include "omv_common.h"
|
||||||
#include "omv_gpio.h"
|
#include "omv_gpio.h"
|
||||||
#include "omv_i2c.h"
|
#include "omv_i2c.h"
|
||||||
#include "framebuffer.h"
|
#include "framebuffer.h"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user