Skip to content

Commit dd50879

Browse files
committed
samples: i2s_codec: Refine sample for samll RAM platforms
- Configure BLOCK count by using kconfig option 'EXTRA_BLOCKS' instad of fixed value. This allows user to customize the RAM usage based on their platform. - Keep the PCM data in flash to avoid large RAM usage. - Refine the sample to get the SAMPLE_BIT_WIDTH and BYTES_PER_SAMPLE from Kconfig options. Signed-off-by: Zhaoxiang Jin <[email protected]>
1 parent 16f4d6c commit dd50879

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

samples/drivers/i2s/i2s_codec/Kconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,24 @@ config USE_CODEC_CLOCK
2525
to generate those. If not selected, the I2S peripheral will generate
2626
them and the codec will be expected to consume them.
2727

28+
config EXTRA_BLOCKS
29+
int "Extra block size"
30+
default 32
31+
help
32+
Extra blocks for storing PCM sample.
33+
34+
config SAMPLE_WIDTH
35+
int "Sample bit width"
36+
default 16
37+
help
38+
PCM sample bit width.
39+
40+
config BYTES_PER_SAMPLE
41+
int "Bytes per sample"
42+
default 2
43+
help
44+
Number of bytes per PCM sample.
45+
2846
config USE_DMIC
2947
bool "Use DMIC as an audio input"
3048

samples/drivers/i2s/i2s_codec/src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#define I2S_CODEC_TX DT_ALIAS(i2s_codec_tx)
2020

2121
#define SAMPLE_FREQUENCY CONFIG_SAMPLE_FREQ
22-
#define SAMPLE_BIT_WIDTH (16U)
23-
#define BYTES_PER_SAMPLE sizeof(int16_t)
22+
#define SAMPLE_BIT_WIDTH CONFIG_SAMPLE_WIDTH
23+
#define BYTES_PER_SAMPLE CONFIG_BYTES_PER_SAMPLE
2424
#if CONFIG_USE_DMIC
2525
#define NUMBER_OF_CHANNELS CONFIG_DMIC_CHANNELS
2626
#else
@@ -32,7 +32,7 @@
3232
#define TIMEOUT (2000U)
3333

3434
#define BLOCK_SIZE (BYTES_PER_SAMPLE * SAMPLES_PER_BLOCK)
35-
#define BLOCK_COUNT (INITIAL_BLOCKS + 32)
35+
#define BLOCK_COUNT (INITIAL_BLOCKS + CONFIG_EXTRA_BLOCKS)
3636

3737
K_MEM_SLAB_DEFINE_IN_SECT_STATIC(mem_slab, __nocache, BLOCK_SIZE, BLOCK_COUNT, 4);
3838

samples/drivers/i2s/i2s_codec/src/sine.h

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#ifndef SINE_H_
8-
#define SINE_H_
7+
#ifndef SINE_H_
8+
#define SINE_H_
99

10-
#if CONFIG_NOCACHE_MEMORY
11-
#define __NOCACHE __attribute__((__section__(".nocache")))
12-
#elif defined(CONFIG_DT_DEFINED_NOCACHE)
13-
#define __NOCACHE __attribute__((__section__(CONFIG_DT_DEFINED_NOCACHE_NAME)))
14-
#else /* CONFIG_NOCACHE_MEMORY */
15-
#define __NOCACHE
16-
#endif /* CONFIG_NOCACHE_MEMORY */
17-
18-
unsigned char __16kHz16bit_stereo_sine_pcm[] __NOCACHE;
19-
unsigned char __16kHz16bit_stereo_sine_pcm[] = {
10+
/*
11+
* Keep the PCM data in flash to avoid large RAM usage.
12+
* If a platform requires DMA-readable RAM buffers, copy
13+
* chunks of this table into a small nocache TX buffer
14+
* at runtime instead of keeping the entire table in RAM.
15+
*/
16+
static const unsigned char __16kHz16bit_stereo_sine_pcm[] = {
2017
0x00, 0x00, 0x00, 0x00, 0x08, 0x0b, 0x08, 0x0b, 0xbb, 0x15, 0xbb, 0x15, 0xc9, 0x1f, 0xc9,
2118
0x1f, 0xe4, 0x28, 0xe4, 0x28, 0xc8, 0x30, 0xc8, 0x30, 0x38, 0x37, 0x38, 0x37, 0x03, 0x3c,
2219
0x03, 0x3c, 0x04, 0x3f, 0x04, 0x3f, 0x25, 0x40, 0x25, 0x40, 0x5d, 0x3f, 0x5d, 0x3f, 0xb1,
@@ -444,6 +441,7 @@ unsigned char __16kHz16bit_stereo_sine_pcm[] = {
444441
0xa3, 0xc0, 0xa3, 0xc0, 0xdb, 0xbf, 0xdb, 0xbf, 0xfc, 0xc0, 0xfc, 0xc0, 0xfd, 0xc3, 0xfd,
445442
0xc3, 0xc8, 0xc8, 0xc8, 0xc8, 0x38, 0xcf, 0x38, 0xcf, 0x1c, 0xd7, 0x1c, 0xd7, 0x37, 0xe0,
446443
0x37, 0xe0, 0x45, 0xea, 0x45, 0xea, 0xf8, 0xf4, 0xf8, 0xf4};
447-
const unsigned int __16kHz16bit_stereo_sine_pcm_len = sizeof(__16kHz16bit_stereo_sine_pcm);
444+
static const unsigned int __16kHz16bit_stereo_sine_pcm_len =
445+
ARRAY_SIZE(__16kHz16bit_stereo_sine_pcm);
448446

449447
#endif /* SINE_H_ */

0 commit comments

Comments
 (0)