MCPWM register programming...

FX2032
Posts: 4
Joined: Sun Nov 17, 2024 8:57 pm

MCPWM register programming...

Postby FX2032 » Sun Nov 17, 2024 9:04 pm

I am trying to set up the MCPWM on a esp32-3s wroom-1 module.

I want to write to the registers directly to set the PWM up, but none of the values are changing after I write to them for example:

WRITE_PERI_REG(MCPWM_CLK_REG(0) , MCPWM_CLK_EN);
WRITE_PERI_REG(MCPWM_TIMER0_CFG0_REG(0), ((0x0<<MCPWM_TIMER0_PERIOD_UPMETHOD_S) | (914<<MCPWM_TIMER0_PERIOD_S))); // See page 1344 this sets the when to update and the period
WRITE_PERI_REG(MCPWM_GEN0_TSTMP_A_REG(0), (457)); // Set the timer compare value

None of these writes make any change to the default values if I read the values back after a pause!
(I have checked writing to things like the GPIO register etc and there seems no issue!)
HELP!

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: MCPWM register programming...

Postby ESP_Sprite » Mon Nov 18, 2024 2:19 am

Did you enable and un-clockgate the MCPWM peripheral? (periph_ll_enable_clk_clear_rst)

FX2032
Posts: 4
Joined: Sun Nov 17, 2024 8:57 pm

Re: MCPWM register programming...

Postby FX2032 » Wed Nov 20, 2024 7:49 pm

ESP_Sprite wrote:
Mon Nov 18, 2024 2:19 am
Did you enable and un-clockgate the MCPWM peripheral? (periph_ll_enable_clk_clear_rst)
Hi ESP_Sprite Many thanks! I checked and saw I made the foolish mistake of SETTING the clock reset, and not clearing it!

In case someone one day wants to do similar here is the code:

#include <stdio.h>
#include "esp_log.h"
#include "soc/mcpwm_reg.h" // For MCPWM register definitions
#include "soc/gpio_struct.h" // GPIO register definitions
#include "esp32/rom/gpio.h" // For gpio_matrix_out function
#include "driver/gpio.h" // needed for GPIO pins _num_41
#include "soc/gpio_sig_map.h" // needed for PWM0_OUT1A_IDX
#include "soc/system_reg.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/periph_ctrl.h" // For periph_module_enable() function


void app_main() {

SET_PERI_REG_MASK(SYSTEM_PERIP_CLK_EN0_REG, SYSTEM_RMT_CLK_EN); // Enable RMT clock (check which register is needed 0 or 1)
CLEAR_PERI_REG_MASK(SYSTEM_PERIP_RST_EN0_REG, SYSTEM_RMT_RST); // Unclear Periferal
WRITE_PERI_REG(RMT_CH0CONF0_REG, (40 << RMT_DIV_CNT_CH0_S) | (0x8 << RMT_MEM_SIZE_CH0_S)| (0x1 << RMT_APB_MEM_RST_CH0_S)| (0x1 << RMT_CONF_UPDATE_CH0_S)); // Initialise the TX register 0 !! Ymust have the update bit or you get a carrier wave and wrong timing. Note Memsize=>No. of 48x32 blocks-max8 =8x48 pulses
WRITE_PERI_REG(RMT_SYS_CONF_REG, RMT_SCLK_ACTIVE | RMT_CLK_EN | (0x1 <<RMT_SCLK_SEL_S) | (1 << RMT_SCLK_DIV_NUM_S));

gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT);
gpio_matrix_out(GPIO_NUM_4, RMT_SIG_OUT0_IDX, 0, 0); // Map the pin to the function , signal inversion, open drain

WRITE_PERI_REG(RMT_CH0DATA_REG, (((0b0 << 31) | (PulsePeriod << 16) | (0b1<< 15) | PulsePeriod )));
WRITE_PERI_REG(RMT_CH0DATA_REG, (0x0));
SET_PERI_REG_MASK(RMT_CH0CONF0_REG, RMT_TX_START_CH0); // Send the data

Who is online

Users browsing this forum: MicroController and 96 guests