-
Notifications
You must be signed in to change notification settings - Fork 3
libraries/PDM/src/PDM.cpp: Add PDM begin and end. #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: pdm_integration
Are you sure you want to change the base?
Conversation
Signed-off-by: Rathan25 <[email protected]>
There was a problem hiding this 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.
There was a problem hiding this 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
libraries/PDM/keywords.txt
Outdated
# Instances (KEYWORD2) | ||
####################################### | ||
|
||
PDM KEYWORD2 |
There was a problem hiding this comment.
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
libraries/PDM/src/PDM.h
Outdated
#include "Arduino.h" | ||
#include "cyhal_pdmpcm.h" | ||
|
||
#define PDM_DATA P10_5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a constructor which accepts the pins, like ->
https://github.com/Infineon/arduino-core-psoc6/blob/main/libraries/Wire/src/Wire.h#L17*
Then we can have a pre-instantiated object like this:
https://github.com/Infineon/arduino-core-psoc6/blob/main/libraries/Wire/src/Wire.cpp#L236-L241
And those defines are part of the pins_arduino.h -> https://github.com/Infineon/arduino-core-psoc6/blob/main/libraries/Wire/src/Wire.cpp#L236-L241
libraries/PDM/src/PDM.cpp
Outdated
return; \ | ||
} | ||
|
||
bool PdmClass::begin(int channels, int sample_rate) { |
There was a problem hiding this comment.
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:
- irq setup
- dma
- ringbuffer usage (there is a coreAPI ringbuffer that maybe can be used -> https://github.com/arduino/ArduinoCore-API/blob/master/api/RingBuffer.h
- The clock also needs to be setup depending on the sampling rate.
https://github.com/Infineon/micropython/blob/ports-psoc6-main/ports/psoc6/machine_pdm_pcm.c#L435-L499
It is fine, you can gradually add that in subsequent PR.
libraries/PDM/src/PDM.h
Outdated
#define PDM_DATA P10_5 | ||
#define PDM_CLK P10_4 | ||
|
||
#define SAMPLE_RATE_HZ 8000u |
There was a problem hiding this comment.
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?
libraries/PDM/src/PDM.h
Outdated
#define SAMPLE_RATE_HZ 8000u | ||
#define DECIMATION_RATE 64u | ||
|
||
class PdmClass { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would name it PDMClass
, as here -> https://github.com/arduino/ArduinoCore-mbed/blob/main/libraries/PDM/src/PDM.h
There was a problem hiding this comment.
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
Signed-off-by: Rathan25 <[email protected]>
Signed-off-by: Rathan25 <[email protected]>
There was a problem hiding this 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]; |
There was a problem hiding this comment.
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?
libraries/PDM/src/PDM.h
Outdated
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); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
Signed-off-by: Rathan25 <[email protected]>
PR Description.
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:
