Add clang-format
Signed-off-by: Matthias Blankertz <matthias@blankertz.org>
This commit is contained in:
14
.gitea/workflows/check-format.yaml
Normal file
14
.gitea/workflows/check-format.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
name: Check code formatting
|
||||
on:
|
||||
push:
|
||||
|
||||
jobs:
|
||||
Check-C-Format:
|
||||
runs-on: ubuntu-22.04-full
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v4
|
||||
- name: Run clang format
|
||||
run: |
|
||||
cmake software -B build
|
||||
cmake --build build -- check-format
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ hardware/tonberry-pico/tonberry-pico-backups/
|
||||
*~
|
||||
*.kicad_sch-bak
|
||||
*.kicad_sch.lck
|
||||
software/build
|
||||
|
||||
13
software/CMakeLists.txt
Normal file
13
software/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
project(tonberry-pico)
|
||||
|
||||
add_custom_target(check-format
|
||||
find . -iname '*.[ch]' -exec clang-format -Werror --dry-run {} +
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
)
|
||||
|
||||
add_custom_target(clang-format
|
||||
find . -iname '*.[ch]' -exec clang-format -i {} +
|
||||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
)
|
||||
6
software/src/.clang-format
Normal file
6
software/src/.clang-format
Normal file
@@ -0,0 +1,6 @@
|
||||
# -*- mode: yaml -*-
|
||||
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
ColumnLimit: 120
|
||||
BreakBeforeBraces: WebKit # Only break at start of functions
|
||||
@@ -6,17 +6,18 @@
|
||||
|
||||
#include "py/mperrno.h"
|
||||
|
||||
void core1_main(void) {
|
||||
void core1_main(void)
|
||||
{
|
||||
if (!i2s_init(shared_context.out_pin, shared_context.sideset_base, shared_context.samplerate))
|
||||
multicore_fifo_push_blocking(MP_EIO);
|
||||
multicore_fifo_push_blocking(MP_EIO);
|
||||
|
||||
multicore_fifo_push_blocking(0);
|
||||
uint32_t cmd;
|
||||
while ((cmd = multicore_fifo_pop_blocking()) != AUDIOCORE_CMD_SHUTDOWN) {
|
||||
switch (cmd) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (cmd) {
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i2s_deinit();
|
||||
|
||||
@@ -8,20 +8,23 @@
|
||||
#include <string.h>
|
||||
|
||||
/* Access rules
|
||||
* audiocore processing runs on core 1 and, unless stated otherwise, all variables may only be accessed from core 1.
|
||||
* Take care of interrupt safety where needed.
|
||||
* The micropython interface lives in module.c and is invoked from the micropython runtime on core 0.
|
||||
* Micropython objects may only be handled in that context
|
||||
* The audiocore_shared_context struct defined below is used for communication between the cores.
|
||||
* audiocore processing runs on core 1 and, unless stated otherwise, all
|
||||
* variables may only be accessed from core 1. Take care of interrupt safety
|
||||
* where needed. The micropython interface lives in module.c and is invoked from
|
||||
* the micropython runtime on core 0. Micropython objects may only be handled in
|
||||
* that context The audiocore_shared_context struct defined below is used for
|
||||
* communication between the cores.
|
||||
*/
|
||||
|
||||
#define AUDIO_BUFFER_SIZE 2048
|
||||
|
||||
// Context shared between the micropython runtime on core0 and the audio task on core1
|
||||
// All access must hold "lock" unless otherwise noted
|
||||
// Context shared between the micropython runtime on core0 and the audio task on
|
||||
// core1 All access must hold "lock" unless otherwise noted
|
||||
struct audiocore_shared_context {
|
||||
spin_lock_t *lock;
|
||||
int out_pin, sideset_base, samplerate; // Set by module.c before core1 is launched and then never changed, can be read without lock
|
||||
int out_pin, sideset_base,
|
||||
samplerate; // Set by module.c before core1 is launched and then never
|
||||
// changed, can be read without lock
|
||||
uint32_t audio_buffer[AUDIO_BUFFER_SIZE];
|
||||
int audio_buffer_write, audio_buffer_read;
|
||||
int underruns;
|
||||
@@ -32,28 +35,29 @@ extern struct audiocore_shared_context shared_context;
|
||||
static inline unsigned audiocore_get_audio_buffer_space(void)
|
||||
{
|
||||
if (shared_context.audio_buffer_write >= shared_context.audio_buffer_read)
|
||||
return AUDIO_BUFFER_SIZE - 1 - (shared_context.audio_buffer_write - shared_context.audio_buffer_read);
|
||||
return AUDIO_BUFFER_SIZE - 1 - (shared_context.audio_buffer_write - shared_context.audio_buffer_read);
|
||||
else
|
||||
return shared_context.audio_buffer_read - shared_context.audio_buffer_write - 1;
|
||||
return shared_context.audio_buffer_read - shared_context.audio_buffer_write - 1;
|
||||
}
|
||||
|
||||
static inline unsigned audiocore_get_audio_buffer_avail(void)
|
||||
{
|
||||
if (shared_context.audio_buffer_write >= shared_context.audio_buffer_read)
|
||||
return shared_context.audio_buffer_write - shared_context.audio_buffer_read;
|
||||
return shared_context.audio_buffer_write - shared_context.audio_buffer_read;
|
||||
else
|
||||
return AUDIO_BUFFER_SIZE - (shared_context.audio_buffer_read - shared_context.audio_buffer_write);
|
||||
return AUDIO_BUFFER_SIZE - (shared_context.audio_buffer_read - shared_context.audio_buffer_write);
|
||||
}
|
||||
|
||||
static inline void audiocore_audio_buffer_put(const uint32_t *restrict src, const size_t len)
|
||||
{
|
||||
const unsigned end_samples = AUDIO_BUFFER_SIZE - shared_context.audio_buffer_write;
|
||||
memcpy(shared_context.audio_buffer + shared_context.audio_buffer_write, src, 4 * ((end_samples >= len) ? len : end_samples));
|
||||
memcpy(shared_context.audio_buffer + shared_context.audio_buffer_write, src,
|
||||
4 * ((end_samples >= len) ? len : end_samples));
|
||||
if (end_samples < len) {
|
||||
memcpy(shared_context.audio_buffer, src + end_samples, 4 * (len - end_samples));
|
||||
shared_context.audio_buffer_write = len - end_samples;
|
||||
memcpy(shared_context.audio_buffer, src + end_samples, 4 * (len - end_samples));
|
||||
shared_context.audio_buffer_write = len - end_samples;
|
||||
} else {
|
||||
shared_context.audio_buffer_write += len;
|
||||
shared_context.audio_buffer_write += len;
|
||||
}
|
||||
shared_context.audio_buffer_write %= AUDIO_BUFFER_SIZE;
|
||||
}
|
||||
@@ -62,12 +66,12 @@ static inline void audiocore_audio_buffer_get(uint32_t *restrict dst, const size
|
||||
{
|
||||
const unsigned end_samples = AUDIO_BUFFER_SIZE - shared_context.audio_buffer_read;
|
||||
memcpy(dst, shared_context.audio_buffer + shared_context.audio_buffer_read,
|
||||
4 * ((end_samples >= len) ? len : end_samples));
|
||||
4 * ((end_samples >= len) ? len : end_samples));
|
||||
if (end_samples < len) {
|
||||
memcpy(dst + end_samples, shared_context.audio_buffer, 4 * (len - end_samples));
|
||||
shared_context.audio_buffer_read = len - end_samples;
|
||||
memcpy(dst + end_samples, shared_context.audio_buffer, 4 * (len - end_samples));
|
||||
shared_context.audio_buffer_read = len - end_samples;
|
||||
} else {
|
||||
shared_context.audio_buffer_read += len;
|
||||
shared_context.audio_buffer_read += len;
|
||||
}
|
||||
shared_context.audio_buffer_read %= AUDIO_BUFFER_SIZE;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2024 Matthias Blankertz <matthias@blankertz.org>
|
||||
|
||||
#include "i2s_max98357.pio.h"
|
||||
#include "audiocore.h"
|
||||
#include "i2s_max98357.pio.h"
|
||||
|
||||
#include <hardware/dma.h>
|
||||
#include <hardware/sync.h>
|
||||
@@ -23,20 +23,19 @@ struct i2s_context {
|
||||
|
||||
static struct i2s_context i2s_context;
|
||||
|
||||
|
||||
static void dma_isr(void)
|
||||
{
|
||||
if (!dma_channel_get_irq1_status(i2s_context.dma_ch))
|
||||
return;
|
||||
return;
|
||||
dma_channel_acknowledge_irq1(i2s_context.dma_ch);
|
||||
const uint32_t flags = spin_lock_blocking(shared_context.lock);
|
||||
if (audiocore_get_audio_buffer_avail() >= I2S_DMA_BUF_SIZE) {
|
||||
audiocore_audio_buffer_get(i2s_context.dma_buf, I2S_DMA_BUF_SIZE);
|
||||
spin_unlock(shared_context.lock, flags);
|
||||
audiocore_audio_buffer_get(i2s_context.dma_buf, I2S_DMA_BUF_SIZE);
|
||||
spin_unlock(shared_context.lock, flags);
|
||||
} else {
|
||||
++shared_context.underruns;
|
||||
spin_unlock(shared_context.lock, flags);
|
||||
memset(i2s_context.dma_buf, 0, sizeof(uint32_t) * I2S_DMA_BUF_SIZE);
|
||||
++shared_context.underruns;
|
||||
spin_unlock(shared_context.lock, flags);
|
||||
memset(i2s_context.dma_buf, 0, sizeof(uint32_t) * I2S_DMA_BUF_SIZE);
|
||||
}
|
||||
dma_channel_transfer_from_buffer_now(i2s_context.dma_ch, i2s_context.dma_buf, I2S_DMA_BUF_SIZE);
|
||||
}
|
||||
@@ -51,27 +50,28 @@ bool i2s_init(int out_pin, int sideset_base, int samplerate)
|
||||
{
|
||||
memset(i2s_context.dma_buf, 0, sizeof(i2s_context.dma_buf[0]) * I2S_DMA_BUF_SIZE);
|
||||
if (!pio_can_add_program(audiocore_pio, &i2s_max98357_program))
|
||||
return false;
|
||||
return false;
|
||||
i2s_context.pio_sm = pio_claim_unused_sm(audiocore_pio, false);
|
||||
if (i2s_context.pio_sm == -1)
|
||||
return false;
|
||||
return false;
|
||||
i2s_context.pio_program_offset = pio_add_program(audiocore_pio, &i2s_max98357_program);
|
||||
i2s_max98357_program_init(audiocore_pio, i2s_context.pio_sm, i2s_context.pio_program_offset, out_pin, sideset_base, samplerate);
|
||||
i2s_max98357_program_init(audiocore_pio, i2s_context.pio_sm, i2s_context.pio_program_offset, out_pin, sideset_base,
|
||||
samplerate);
|
||||
|
||||
i2s_context.dma_ch = dma_claim_unused_channel(false);
|
||||
if (i2s_context.dma_ch == -1)
|
||||
goto out_dma_claim;
|
||||
goto out_dma_claim;
|
||||
setup_dma_config();
|
||||
irq_set_exclusive_handler(DMA_IRQ_1, &dma_isr);
|
||||
dma_channel_set_irq1_enabled (i2s_context.dma_ch, true);
|
||||
dma_channel_set_irq1_enabled(i2s_context.dma_ch, true);
|
||||
irq_set_enabled(DMA_IRQ_1, true);
|
||||
dma_channel_configure(i2s_context.dma_ch, &i2s_context.dma_config, &audiocore_pio->txf[i2s_context.pio_sm],
|
||||
i2s_context.dma_buf, I2S_DMA_BUF_SIZE, true);
|
||||
i2s_context.dma_buf, I2S_DMA_BUF_SIZE, true);
|
||||
pio_sm_set_enabled(audiocore_pio, i2s_context.pio_sm, true);
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
out_dma_claim:
|
||||
out_dma_claim:
|
||||
pio_remove_program(audiocore_pio, &i2s_max98357_program, i2s_context.pio_program_offset);
|
||||
pio_sm_unclaim(audiocore_pio, i2s_context.pio_sm);
|
||||
return false;
|
||||
@@ -80,7 +80,7 @@ bool i2s_init(int out_pin, int sideset_base, int samplerate)
|
||||
void i2s_deinit(void)
|
||||
{
|
||||
pio_sm_set_enabled(audiocore_pio, i2s_context.pio_sm, false);
|
||||
dma_channel_set_irq1_enabled (i2s_context.dma_ch, false);
|
||||
dma_channel_set_irq1_enabled(i2s_context.dma_ch, false);
|
||||
dma_channel_unclaim(i2s_context.dma_ch);
|
||||
pio_remove_program(audiocore_pio, &i2s_max98357_program, i2s_context.pio_program_offset);
|
||||
pio_sm_unclaim(audiocore_pio, i2s_context.pio_sm);
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
#include "audiocore.h"
|
||||
|
||||
// Include MicroPython API.
|
||||
#include "py/runtime.h"
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
// This module is RP2 specific
|
||||
#include "mphalport.h"
|
||||
@@ -19,7 +19,8 @@ struct audiocore_Context_obj {
|
||||
mp_obj_base_t base;
|
||||
};
|
||||
|
||||
static mp_obj_t audiocore_Context_deinit(mp_obj_t self_in) {
|
||||
static mp_obj_t audiocore_Context_deinit(mp_obj_t self_in)
|
||||
{
|
||||
struct audiocore_Context_obj *self = MP_OBJ_TO_PTR(self_in);
|
||||
multicore_fifo_push_blocking(AUDIOCORE_CMD_SHUTDOWN);
|
||||
multicore_fifo_pop_blocking();
|
||||
@@ -30,70 +31,69 @@ static mp_obj_t audiocore_Context_deinit(mp_obj_t self_in) {
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_1(audiocore_Context_deinit_obj, audiocore_Context_deinit);
|
||||
|
||||
static mp_obj_t audiocore_Context_put(mp_obj_t self_in, mp_obj_t buffer) {
|
||||
static mp_obj_t audiocore_Context_put(mp_obj_t self_in, mp_obj_t buffer)
|
||||
{
|
||||
struct audiocore_Context_obj *self = MP_OBJ_TO_PTR(self_in);
|
||||
(void)self;
|
||||
mp_buffer_info_t bufinfo;
|
||||
if (!mp_get_buffer(buffer, &bufinfo, MP_BUFFER_READ))
|
||||
mp_raise_ValueError("not a read buffer");
|
||||
mp_raise_ValueError("not a read buffer");
|
||||
if (bufinfo.typecode != 'I')
|
||||
mp_raise_ValueError("unsupported buffer type");
|
||||
mp_raise_ValueError("unsupported buffer type");
|
||||
unsigned to_copy = bufinfo.len / 4;
|
||||
|
||||
const uint32_t flags = spin_lock_blocking(shared_context.lock);
|
||||
const unsigned buf_space = audiocore_get_audio_buffer_space();
|
||||
if (to_copy > buf_space)
|
||||
to_copy = buf_space;
|
||||
to_copy = buf_space;
|
||||
if (to_copy > 0) {
|
||||
audiocore_audio_buffer_put(bufinfo.buf, to_copy);
|
||||
audiocore_audio_buffer_put(bufinfo.buf, to_copy);
|
||||
}
|
||||
const unsigned underruns = shared_context.underruns;
|
||||
spin_unlock(shared_context.lock, flags);
|
||||
|
||||
mp_obj_t items[] = {mp_obj_new_int(to_copy),
|
||||
mp_obj_new_int(buf_space),
|
||||
mp_obj_new_int(underruns),};
|
||||
mp_obj_t items[] = {
|
||||
mp_obj_new_int(to_copy),
|
||||
mp_obj_new_int(buf_space),
|
||||
mp_obj_new_int(underruns),
|
||||
};
|
||||
return mp_obj_new_tuple(3, items);
|
||||
//return mp_obj_new_int(to_copy);
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_2(audiocore_Context_put_obj, audiocore_Context_put);
|
||||
|
||||
static const mp_rom_map_elem_t audiocore_Context_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiocore_Context_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiocore_Context_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&audiocore_Context_put_obj) },
|
||||
{MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiocore_Context_deinit_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiocore_Context_deinit_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_put), MP_ROM_PTR(&audiocore_Context_put_obj)},
|
||||
};
|
||||
static MP_DEFINE_CONST_DICT(audiocore_Context_locals_dict, audiocore_Context_locals_dict_table);
|
||||
const mp_obj_type_t audiocore_Context_type;
|
||||
|
||||
MP_DEFINE_CONST_OBJ_TYPE(
|
||||
audiocore_Context_type,
|
||||
MP_QSTR_Context,
|
||||
MP_TYPE_FLAG_NONE,
|
||||
locals_dict, &audiocore_Context_locals_dict
|
||||
);
|
||||
MP_DEFINE_CONST_OBJ_TYPE(audiocore_Context_type, MP_QSTR_Context, MP_TYPE_FLAG_NONE, locals_dict,
|
||||
&audiocore_Context_locals_dict);
|
||||
|
||||
static mp_obj_t audiocore_init(mp_obj_t pin_obj, mp_obj_t sideset_obj, mp_obj_t samplerate_obj) {
|
||||
static mp_obj_t audiocore_init(mp_obj_t pin_obj, mp_obj_t sideset_obj, mp_obj_t samplerate_obj)
|
||||
{
|
||||
if (initialized)
|
||||
mp_raise_OSError(MP_EBUSY);
|
||||
mp_raise_OSError(MP_EBUSY);
|
||||
if (!shared_context.lock) {
|
||||
// initialize shared context lock on first init
|
||||
int lock = spin_lock_claim_unused(false);
|
||||
if (lock == -1)
|
||||
mp_raise_OSError(MP_ENOMEM);
|
||||
shared_context.lock = spin_lock_init(lock);
|
||||
// initialize shared context lock on first init
|
||||
int lock = spin_lock_claim_unused(false);
|
||||
if (lock == -1)
|
||||
mp_raise_OSError(MP_ENOMEM);
|
||||
shared_context.lock = spin_lock_init(lock);
|
||||
}
|
||||
shared_context.audio_buffer_write = shared_context.audio_buffer_read = shared_context.underruns = 0;
|
||||
memset(shared_context.audio_buffer, 0, AUDIO_BUFFER_SIZE*4);
|
||||
memset(shared_context.audio_buffer, 0, AUDIO_BUFFER_SIZE * 4);
|
||||
multicore_reset_core1();
|
||||
struct audiocore_Context_obj *context = m_malloc_with_finaliser(sizeof(struct audiocore_Context_obj));
|
||||
context->base.type = &audiocore_Context_type;
|
||||
mp_hal_pin_obj_t pin = pin_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(pin_obj);
|
||||
if (pin == -1)
|
||||
mp_raise_ValueError("Invalid out pin");
|
||||
mp_raise_ValueError("Invalid out pin");
|
||||
mp_hal_pin_obj_t sideset_pin = sideset_obj == MP_OBJ_NULL ? -1 : mp_hal_get_pin_obj(sideset_obj);
|
||||
if (sideset_pin == -1)
|
||||
mp_raise_ValueError("Invalid sideset base pin");
|
||||
mp_raise_ValueError("Invalid sideset base pin");
|
||||
int samplerate = mp_obj_get_int(samplerate_obj);
|
||||
shared_context.out_pin = pin;
|
||||
shared_context.sideset_base = sideset_pin;
|
||||
@@ -102,24 +102,24 @@ static mp_obj_t audiocore_init(mp_obj_t pin_obj, mp_obj_t sideset_obj, mp_obj_t
|
||||
multicore_launch_core1(&core1_main);
|
||||
uint32_t result = multicore_fifo_pop_blocking();
|
||||
if (result != 0) {
|
||||
multicore_reset_core1();
|
||||
initialized = false;
|
||||
mp_raise_OSError(result);
|
||||
multicore_reset_core1();
|
||||
initialized = false;
|
||||
mp_raise_OSError(result);
|
||||
}
|
||||
|
||||
|
||||
return MP_OBJ_FROM_PTR(context);
|
||||
}
|
||||
static MP_DEFINE_CONST_FUN_OBJ_3(audiocore_init_obj, audiocore_init);
|
||||
|
||||
static const mp_rom_map_elem_t audiocore_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audiocore) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&audiocore_init_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_Context), MP_ROM_PTR(&audiocore_Context_type) },
|
||||
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_audiocore)},
|
||||
{MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&audiocore_init_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_Context), MP_ROM_PTR(&audiocore_Context_type)},
|
||||
};
|
||||
static MP_DEFINE_CONST_DICT(audiocore_module_globals, audiocore_module_globals_table);
|
||||
|
||||
const mp_obj_module_t audiocore_cmodule = {
|
||||
.base = { &mp_type_module },
|
||||
.base = {&mp_type_module},
|
||||
.globals = (mp_obj_dict_t *)&audiocore_module_globals,
|
||||
};
|
||||
MP_REGISTER_MODULE(MP_QSTR_audiocore, audiocore_cmodule);
|
||||
|
||||
Reference in New Issue
Block a user