Using gpio's for pwm through MALOS layer

I got matrix creator recently,i tried python to control gpio’s.Can i use gpio’s as pwm pins through MALOS layer ?How?

Yes, here are examples

Let me know how it goes.

I have tried those examples,it’s working.I have to send raw ir code to my circuit through that possible with the MALOS layer?I want set certain frequency by using pwm

Are you trying to use these PWM outputs for IR communication?

yes,we are.because we need more externel ir leds

You could definitely use MALOS to control PWM output in those gpio pins. But not sure if it would work for IR communication. Bear in mind that this was not designed for IR. Do you already have an idea on how to do it, what software to use for IR running in the Pi?

An option could be to have some external GPIO pins (the ones in the MATRIX Creator) bypassed via the FPGA so you can work with them as they were Rasp Pi GPIOs.

I am converting ir codes into raw timings,then i want to send through pwm pin.
Could you tell me how to set frequency on pwm by using python script?
I didn’t got any api’s to do that

Have you check LIRC ?

Yeah,we checked LIRC it was working .Are BCM12,BCM13 pins bypassed to gpio pins in matrix?

id did this with python using wrapped matrix cpp hal drivers. the pwm frequency was 500Hz and duty cycles from 30% to 100%.

the normal cpp hal drivers don’t use frequencies as value instead they are using a prescalor with period in seconds and a duty counter.

the malos doesn’t seem to have raw pwm protocols, only gpio (in/out) and a common servo interface (pwm).

my plan is migrating the python hal drivers from spi to a cpp wrapper interface, this works local on my device, but is not really releasable right now. but i’m working on it.

Can you send me that how to set that through cpp layer?
i used that servo example by changing prescalar value ,its not worked

i think you don’t need to set the prescalar, you need to change the peroid, can you share some details of frequency and so on.

I want to set 38KHz with duty cycle 50%,how?
then i want to control the gpio pin high and low with certain timings

you can try this:

# e.g. on bank 0
# 38kHz period with 0 prescaler
SetPrescaler = 0x0
SetPeriod = 2631 # int((1 / 38000) * FPGA_CLOCK / ((1 << PRESCALER) * 2))
e.g. 50% Duty cycle
SetDuty = int(2631 / 100 * 50)

there is also some implementation of timer functions in the hal cpp driver, but i did’t play with it. maybe someone else could explain what they are for.

thanks ,i will try this

Hi @loom ,i have tried but i am confused with period counter and duty counter
How to set duty cycle to 50% ?
const int kFPGAClock = 200000000;
const uint16_t kGPIOPrescaler = 0x0;
const float kPWMPeriod = 0.02; // Seconds
uint16_t period_counter =
(kPWMPeriod * kFPGAClock) /
((1 << kGPIOPrescaler) * 2); /* Set Period counter for 20ms */
gpio.Bank(0).SetDuty(0, dutycounter);

period means how long is your period in second
1Hz = 1 / 1 = 1s
50Hz = 1 / 50 = 0.02s
38kHz = 1 / 38000 = 0,000026316

so do this

const float kPWMPeriod = 1 / 38000
# with that a period counter is calculated
uint16_t period_counter = (kPWMPeriod * kFPGAClock) / ((1 << kGPIOPrescaler) * 2);
# we set the period counter in the fpga
# the fpga is no counting from 0 to period_counter, again and again, with a frequenzy of 38khz starting over
int16_t duty_counter;
duty_counter = (int16_t)(period_counter / 2);
gpio.Bank(0).SetDuty(0, duty_counter);
# setting the duty means in the range while he is counting to put the pin high -> our duty counter is half the period counter, so while he counts from 0 to period_counter he puts the pin high half the time

Hi @loom ,
I have tried that configuration but it’s not working properly
what is actual FPGA clock frequency?In example it’s mentioned as 200MHz but in cpp drivers it’s mentioned as 125MHz.I have tried on those calculations also but it’s not working.
When i put duty counter value 0 ,normally it have to be turn off but it’s just reducing brightness.

yeah you are right 200MHz seems to be wrong 125MHz is correct, thank you for telling me this. I am not a matrix developer, but with 125 MHz I get the following values. (I only have a cheap oscilloscope and I used it for the first time today)

prescaler 0x5 | period = 0.01 (10ms = 100Hz) | duty 50%

prescaler 0x0 | period = 1/38000 (26.31µs = 38kHz) | duty 50%

prescaler 0x0 | period = 1/38000 (26.31µs = 38kHz) | duty 0%

it seems all ok with(125MHz) on my device … what is your wiring?