Skip to content

Instantly share code, notes, and snippets.

@embedded-creations
Last active February 27, 2018 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embedded-creations/74441240f72acafb9a7bc1d80defd402 to your computer and use it in GitHub Desktop.
Save embedded-creations/74441240f72acafb9a7bc1d80defd402 to your computer and use it in GitHub Desktop.
ESP32 - Trying to connect GPIO output to peripheral input though GPIO Matrix with single pin
/* MCPWM basic config example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
/*
* This example will show you how to use each submodule of MCPWM unit.
* The example can't be used without modifying the code first.
* Edit the macros at the top of mcpwm_example_basic_config.c to enable/disable the submodules which are used in the example.
*/
#include <stdio.h>
#include "string.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_attr.h"
#include "soc/rtc.h"
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#define GPIO_PWM0A_OUT 19 //Set GPIO 19 as PWM0A
#define SYNC_PIN_SOURCE_EXTERNAL 0
#define SYNC_PIN_SOURCE_INTERNAL 1
#define SYNC_ON_GPIO_OUTPUT SYNC_PIN_SOURCE_INTERNAL
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_EXTERNAL)
#define GPIO_SYNC0_IN 2 //Set GPIO 02 as SYNC0 (driven by gpio_test_signal - connected externally to GPIO12)
#endif
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_INTERNAL)
#define GPIO_SYNC0_IN 12 //Set GPIO 12 (driven by gpio_test_signal) as SYNC0
#endif
static void mcpwm_example_gpio_initialize()
{
printf("initializing mcpwm gpio...\n");
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_PWM0A_OUT);
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_EXTERNAL)
// below are important pieces of: mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_SYNC_0, GPIO_SYNC0_IN);
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[GPIO_SYNC0_IN], PIN_FUNC_GPIO);
gpio_set_direction(GPIO_SYNC0_IN, GPIO_MODE_INPUT);
gpio_matrix_in(GPIO_SYNC0_IN, PWM0_SYNC0_IN_IDX, false);
#endif
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_INTERNAL)
// redundant: PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[GPIO_SYNC0_IN], PIN_FUNC_GPIO);
// redundant and broken, GPIO_MODE_INPUT_OUTPUT is set in gpio_test_signal instead: gpio_set_direction(GPIO_SYNC0_IN, GPIO_MODE_INPUT_OUTPUT);
gpio_matrix_in(GPIO_SYNC0_IN, PWM0_SYNC0_IN_IDX, false);
#endif
}
/**
* @brief Set gpio 12 as our test signal that generates high-low waveform continuously, connect this gpio to capture pin.
*/
static void gpio_test_signal(void *arg)
{
printf("intializing test signal...\n");
gpio_config_t gp;
gp.intr_type = GPIO_INTR_DISABLE;
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_EXTERNAL)
gp.mode = GPIO_MODE_OUTPUT;
#endif
#if (SYNC_ON_GPIO_OUTPUT == SYNC_PIN_SOURCE_INTERNAL)
gp.mode = GPIO_MODE_INPUT_OUTPUT;
#endif
gp.pin_bit_mask = GPIO_SEL_12;
gpio_config(&gp);
while (1) {
//here the period of test signal is 20ms
gpio_set_level(GPIO_NUM_12, 1); //Set high
vTaskDelay(1); //delay of 10ms
gpio_set_level(GPIO_NUM_12, 0); //Set low
vTaskDelay(1); //delay of 10ms
}
}
/**
* @brief Configure whole MCPWM module
*/
static void mcpwm_example_config(void *arg)
{
//1. mcpwm gpio initialization
mcpwm_example_gpio_initialize();
//2. initialize mcpwm configuration
printf("Configuring Initial Parameters of mcpwm...\n");
mcpwm_config_t pwm_config;
pwm_config.frequency = 60; //frequency = 60Hz
pwm_config.cmpr_a = 5.0; //duty cycle of PWMxA = 60.0%
pwm_config.counter_mode = MCPWM_UP_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); //Configure PWM0A & PWM0B with above settings
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_SYNC0, 000); //Load counter value with 20% of period counter of mcpwm timer 1 when sync 0 occurs
vTaskDelete(NULL);
}
void app_main()
{
printf("Testing MCPWM...\n");
xTaskCreate(gpio_test_signal, "gpio_test_signal", 4096, NULL, 5, NULL); //comment if you don't want to use capture module
xTaskCreate(mcpwm_example_config, "mcpwm_example_config", 4096, NULL, 5, NULL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment