Skip to content

Conversation

Rathan25
Copy link

@Rathan25 Rathan25 commented Sep 18, 2025

PR Description.

  1. Added PDM.begin(), Which dose initialization of PDM, enables channels, configures sample rate.
  2. Added PDM.end(), which disables PDM and clear the FIFOs.
  3. In example added Basic test on begin() and end().

Done unit test with channels and passing sample rate. For single channel it configures with left channel . Going forward need to check with application

Captured logs:
image

Copy link
Member

@ederjc ederjc left a comment

Choose a reason for hiding this comment

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

Please complete the work or open against a feature branch.
Also, make sure the checks are passing.

@Rathan25 Rathan25 changed the base branch from main to pdm_integration September 22, 2025 11:39
Copy link
Member

@jaenrig-ifx jaenrig-ifx left a comment

Choose a reason for hiding this comment

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

Good start 👍
We have to also start thinking of the tests in arduino-core-test
In micropython we have faked a pcm signal with gpio toggling and then read the pattern from the receiver:
https://github.com/Infineon/micropython/blob/ports-psoc6-main/tests/ports/psoc6/board_ext_hw/multi/pdm_pcm_rx.py
https://github.com/Infineon/micropython/blob/ports-psoc6-main/tests/ports/psoc6/board_ext_hw/multi/pdm_pcm_tx.py

# Instances (KEYWORD2)
#######################################

PDM KEYWORD2
Copy link
Member

Choose a reason for hiding this comment

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

As we are going to be able to create multiple instances, this should be a data type -> KEYWORD1

#include "Arduino.h"
#include "cyhal_pdmpcm.h"

#define PDM_DATA P10_5
Copy link
Member

Choose a reason for hiding this comment

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

return; \
}

bool PdmClass::begin(int channels, int sample_rate) {
Copy link
Member

Choose a reason for hiding this comment

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

As discussed, there are few more things that need to be taken care in the begin:

#define PDM_DATA P10_5
#define PDM_CLK P10_4

#define SAMPLE_RATE_HZ 8000u
Copy link
Member

Choose a reason for hiding this comment

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

These defines can be private, in the .cpp?

#define SAMPLE_RATE_HZ 8000u
#define DECIMATION_RATE 64u

class PdmClass {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Author

@Rathan25 Rathan25 Sep 24, 2025

Choose a reason for hiding this comment

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

As per your comments, done changes. ISR is working. Please review once.

commit for fix
082bf15

Copy link
Member

@jaenrig-ifx jaenrig-ifx left a comment

Choose a reason for hiding this comment

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

A few comments to work on.
In parallel we should start with the tests.

PDMClass(pin_size_t sda, pin_size_t scl);
PDMClass(); // Constructor

int32_t active_rx_buffer[BUFFER_SIZE];
Copy link
Member

Choose a reason for hiding this comment

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

Are these variables required to be public?

void end();
uint32_t available();
uint32_t read(uint16_t *out_buffer, uint32_t samples_to_read);
static void pdm_pcm_isr_handler(void *arg, cyhal_pdm_pcm_event_t event);
Copy link
Member

Choose a reason for hiding this comment

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

Is the isr handled required to be public?

#include "cyhal_pdmpcm.h"
#include "cyhal.h"

#define SAMPLE_RATE_HZ 8000u
Copy link
Member

Choose a reason for hiding this comment

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

These defines could be constexpr and private, as they are only relevant to the internal of the class, or?
https://medium.com/@newcreation2kal/const-constexpr-and-macros-in-c-db806ece0b87

Serial.println("Begin function called");

if (channels != 1 && channels != 2) {
Serial.println("Invalid channel. Desired channel should be 1 0r 2");
Copy link
Member

Choose a reason for hiding this comment

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

We usually do not add Serial.print() calls in the library.
Reasons: they are computationally expensive, can interfere with the application performance, the Serial might not be required or even available by the application...
They can be convenient for debugging as you are using them now (as long as they don´t interfere with the functionality).
So keep them if you still need them, and we can remove them before merging to main.
We could implement them as macros that can be enabled/disabled conditionally. But we have not done that for any other built-in lib.

return false;
}

cyhal_pdm_pcm_register_callback(&pdm_pcm, pdm_pcm_isr_handler, this);
Copy link
Member

Choose a reason for hiding this comment

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

A bit of encapsulation of each of these calls would help with readability and maintainability.

Copy link
Member

Choose a reason for hiding this comment

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

PDMClass::clock_init();
// ringbuf_init -->to be implemented

cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_ON); // pcm signal with gpio toggling
Copy link
Member

Choose a reason for hiding this comment

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

The gpio toggling is a temporary utility or? We know... but good if we would add a comment for such things

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

Successfully merging this pull request may close these issues.

3 participants