Skip to content

Conversation

ZhaoxiangJin
Copy link
Contributor

  • 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.

@ZhaoxiangJin
Copy link
Contributor Author

ZhaoxiangJin commented Oct 3, 2025

Hello @TomasBarakNXP, @VitekST, please help review. Thanks.

0xc3, 0xc8, 0xc8, 0xc8, 0xc8, 0x38, 0xcf, 0x38, 0xcf, 0x1c, 0xd7, 0x1c, 0xd7, 0x37, 0xe0,
0x37, 0xe0, 0x45, 0xea, 0x45, 0xea, 0xf8, 0xf4, 0xf8, 0xf4};
const unsigned int __16kHz16bit_stereo_sine_pcm_len = sizeof(__16kHz16bit_stereo_sine_pcm);
static const unsigned int __16kHz16bit_stereo_sine_pcm_len = sizeof(__16kHz16bit_stereo_sine_pcm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ARRAY_SIZE be better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

int "Extra block size"
default 32
help
Count of DMIC channels to capture and process.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the count of DMIC channels?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

@ZhaoxiangJin
Copy link
Contributor Author

Update:
fixed ci issue

hakehuang
hakehuang previously approved these changes Oct 7, 2025
0xc3, 0xc8, 0xc8, 0xc8, 0xc8, 0x38, 0xcf, 0x38, 0xcf, 0x1c, 0xd7, 0x1c, 0xd7, 0x37, 0xe0,
0x37, 0xe0, 0x45, 0xea, 0x45, 0xea, 0xf8, 0xf4, 0xf8, 0xf4};
const unsigned int __16kHz16bit_stereo_sine_pcm_len = sizeof(__16kHz16bit_stereo_sine_pcm);
static const unsigned int __16kHz16bit_stereo_sine_pcm_len =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const is probably not necessary here. See the reported issue: when cast to (void *) in main.c, const is lost.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @TomasBarakNXP, currently, both __16kHz16bit_stereo_sine_pcm and __16kHz16bit_stereo_sine_pcm_len are modified with const. I'm not sure what the "reported issue" you're referring to is. However, I understand that it's worth keeping the const declarations in both places.

Why is it worth keeping the const declaration for __16kHz16bit_stereo_sine_pcm?

  1. Using const for storing the PCM table in flash can significantly save RAM (also stated this motivation in the comments at the top of sine.h).
  2. __16kHz16bit_stereo_sine_pcm is used here:
mem_block = (void *)&__16kHz16bit_stereo_sine_pcm;
ret = i2s_buf_write(i2s_dev_codec, mem_block, block_size);

The prototype of i2s_buf_write is: int i2s_buf_write(const struct device *dev, void *buf, size_t size), and the implementation only memcpy data from buf to the driver-allocated TX slab and then transmits it via DMA, buf is not modified (see drivers/i2s/i2s_common.c: memcpy(mem_block, (void *)buf, size)).
The code mem_block = (void)&__16kHz16bit_stereo_sine_pcm; simply casts the parameter to "lose const" to accommodate the void parameter of i2s_buf_write. Since the function only reads it, this is safe; the only drawback is that it's not perfectly const-correct. In other words, the lack of const API parameters is due to historical reasons/interface design issues, the actual source buffer is read-only.

Why is it worth keeping the const for __16kHz16bit_stereo_sine_pcm_len?

  1. To prevent accidental modification, it explicitly states that "this is read-only data." Any attempt to write to it will result in a compilation failure or an error when read-only segment protection is enabled, preventing potential risks such as length corruption and out-of-bounds access.
  2. Storing it in a read-only segment conserves RAM. Although the savings are not much.

them and the codec will be expected to consume them.

config EXTRA_BLOCKS
int "Extra block size"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int "Extra block size"
int "Number of extra blocks"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

- 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 <Zhaoxiang.Jin_1@nxp.com>
Copy link

sonarqubecloud bot commented Oct 7, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants