2016-11-21 05:17:07 -05:00
|
|
|
/* Pulse counter module - Example
|
|
|
|
|
|
|
|
For other examples please check:
|
|
|
|
https://github.com/espressif/esp-idf/tree/master/examples
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
2016-11-23 23:01:21 -05:00
|
|
|
#include "freertos/portmacro.h"
|
2016-11-21 05:17:07 -05:00
|
|
|
#include "freertos/task.h"
|
2016-11-23 23:01:21 -05:00
|
|
|
#include "freertos/queue.h"
|
2016-11-21 05:17:07 -05:00
|
|
|
#include "driver/periph_ctrl.h"
|
|
|
|
#include "driver/ledc.h"
|
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/pcnt.h"
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "soc/gpio_sig_map.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TEST CODE BRIEF
|
2017-09-22 12:42:17 -04:00
|
|
|
*
|
2016-11-21 05:17:07 -05:00
|
|
|
* Use PCNT module to count rising edges generated by LEDC module.
|
|
|
|
*
|
2017-09-22 12:42:17 -04:00
|
|
|
* Functionality of GPIOs used in this example:
|
|
|
|
* - GPIO18 - output pin of a sample 1 Hz pulse generator,
|
|
|
|
* - GPIO4 - pulse input pin,
|
|
|
|
* - GPIO5 - control input pin.
|
|
|
|
*
|
|
|
|
* Load example, open a serial port to view the message printed on your screen.
|
2016-11-21 05:17:07 -05:00
|
|
|
*
|
2017-09-22 12:42:17 -04:00
|
|
|
* To do this test, you should connect GPIO18 with GPIO4.
|
|
|
|
* GPIO5 is the control signal, you can leave it floating with internal pull up,
|
|
|
|
* or connect it to ground. If left floating, the count value will be increasing.
|
|
|
|
* If you connect GPIO5 to GND, the count value will be decreasing.
|
2016-11-21 05:17:07 -05:00
|
|
|
*
|
2017-09-22 12:42:17 -04:00
|
|
|
* An interrupt will be triggered when the counter value:
|
|
|
|
* - reaches 'thresh1' or 'thresh0' value,
|
|
|
|
* - reaches 'l_lim' value or 'h_lim' value,
|
|
|
|
* - will be reset to zero.
|
2016-11-21 05:17:07 -05:00
|
|
|
*/
|
2017-09-22 12:42:17 -04:00
|
|
|
#define PCNT_TEST_UNIT PCNT_UNIT_0
|
|
|
|
#define PCNT_H_LIM_VAL 10
|
2016-12-21 20:18:15 -05:00
|
|
|
#define PCNT_L_LIM_VAL -10
|
2017-09-22 12:42:17 -04:00
|
|
|
#define PCNT_THRESH1_VAL 5
|
2016-12-21 20:18:15 -05:00
|
|
|
#define PCNT_THRESH0_VAL -5
|
2017-09-22 12:42:17 -04:00
|
|
|
#define PCNT_INPUT_SIG_IO 4 // Pulse Input GPIO
|
|
|
|
#define PCNT_INPUT_CTRL_IO 5 // Control GPIO HIGH=count up, LOW=count down
|
|
|
|
#define LEDC_OUTPUT_IO 18 // Output GPIO of a sample 1 Hz pulse generator
|
2016-11-23 23:01:21 -05:00
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
xQueueHandle pcnt_evt_queue; // A queue to handle pulse counter events
|
2018-05-25 03:37:20 -04:00
|
|
|
pcnt_isr_handle_t user_isr_handle = NULL; //user's ISR service handle
|
2016-11-23 23:01:21 -05:00
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* A sample structure to pass events from the PCNT
|
|
|
|
* interrupt handler to the main program.
|
|
|
|
*/
|
2016-11-23 23:01:21 -05:00
|
|
|
typedef struct {
|
2017-09-22 12:42:17 -04:00
|
|
|
int unit; // the PCNT unit that originated an interrupt
|
|
|
|
uint32_t status; // information on the event type that caused the interrupt
|
2016-11-23 23:01:21 -05:00
|
|
|
} pcnt_evt_t;
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Decode what PCNT's unit originated an interrupt
|
|
|
|
* and pass this information together with the event type
|
|
|
|
* the main program using a queue.
|
|
|
|
*/
|
|
|
|
static void IRAM_ATTR pcnt_example_intr_handler(void *arg)
|
2016-11-21 05:17:07 -05:00
|
|
|
{
|
|
|
|
uint32_t intr_status = PCNT.int_st.val;
|
|
|
|
int i;
|
2016-11-23 23:01:21 -05:00
|
|
|
pcnt_evt_t evt;
|
|
|
|
portBASE_TYPE HPTaskAwoken = pdFALSE;
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
for (i = 0; i < PCNT_UNIT_MAX; i++) {
|
|
|
|
if (intr_status & (BIT(i))) {
|
2016-11-23 23:01:21 -05:00
|
|
|
evt.unit = i;
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Save the PCNT event type that caused an interrupt
|
|
|
|
to pass it to the main program */
|
2016-11-23 23:01:21 -05:00
|
|
|
evt.status = PCNT.status_unit[i].val;
|
|
|
|
PCNT.int_clr.val = BIT(i);
|
|
|
|
xQueueSendFromISR(pcnt_evt_queue, &evt, &HPTaskAwoken);
|
2017-09-22 12:42:17 -04:00
|
|
|
if (HPTaskAwoken == pdTRUE) {
|
2016-11-23 23:01:21 -05:00
|
|
|
portYIELD_FROM_ISR();
|
2016-11-21 05:17:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Configure LED PWM Controller
|
|
|
|
* to output sample pulses at 1 Hz with duty of about 10%
|
|
|
|
*/
|
2016-11-21 05:17:07 -05:00
|
|
|
static void ledc_init(void)
|
|
|
|
{
|
2017-09-22 12:42:17 -04:00
|
|
|
// Prepare and then apply the LEDC PWM timer configuration
|
2016-11-21 05:17:07 -05:00
|
|
|
ledc_timer_config_t ledc_timer;
|
2017-10-22 23:32:47 -04:00
|
|
|
ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
|
|
|
|
ledc_timer.timer_num = LEDC_TIMER_1;
|
|
|
|
ledc_timer.duty_resolution = LEDC_TIMER_10_BIT;
|
|
|
|
ledc_timer.freq_hz = 1; // set output frequency at 1 Hz
|
2016-11-21 05:17:07 -05:00
|
|
|
ledc_timer_config(&ledc_timer);
|
2017-09-22 12:42:17 -04:00
|
|
|
|
|
|
|
// Prepare and then apply the LEDC PWM channel configuration
|
|
|
|
ledc_channel_config_t ledc_channel;
|
|
|
|
ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
|
|
|
|
ledc_channel.channel = LEDC_CHANNEL_1;
|
|
|
|
ledc_channel.timer_sel = LEDC_TIMER_1;
|
|
|
|
ledc_channel.intr_type = LEDC_INTR_DISABLE;
|
|
|
|
ledc_channel.gpio_num = LEDC_OUTPUT_IO;
|
|
|
|
ledc_channel.duty = 100; // set duty at about 10%
|
|
|
|
ledc_channel_config(&ledc_channel);
|
2016-11-21 05:17:07 -05:00
|
|
|
}
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Initialize PCNT functions:
|
|
|
|
* - configure and initialize PCNT
|
|
|
|
* - set up the input filter
|
|
|
|
* - set up the counter events to watch
|
|
|
|
*/
|
2017-03-22 00:36:11 -04:00
|
|
|
static void pcnt_example_init(void)
|
2016-11-21 05:17:07 -05:00
|
|
|
{
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Prepare configuration for the PCNT unit */
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_config_t pcnt_config = {
|
2017-09-22 12:42:17 -04:00
|
|
|
// Set PCNT input signal and control GPIOs
|
2016-11-21 05:17:07 -05:00
|
|
|
.pulse_gpio_num = PCNT_INPUT_SIG_IO,
|
|
|
|
.ctrl_gpio_num = PCNT_INPUT_CTRL_IO,
|
|
|
|
.channel = PCNT_CHANNEL_0,
|
|
|
|
.unit = PCNT_TEST_UNIT,
|
2017-09-22 12:42:17 -04:00
|
|
|
// What to do on the positive / negative edge of pulse input?
|
|
|
|
.pos_mode = PCNT_COUNT_INC, // Count up on the positive edge
|
|
|
|
.neg_mode = PCNT_COUNT_DIS, // Keep the counter value on the negative edge
|
|
|
|
// What to do when control input is low or high?
|
|
|
|
.lctrl_mode = PCNT_MODE_REVERSE, // Reverse counting direction if low
|
|
|
|
.hctrl_mode = PCNT_MODE_KEEP, // Keep the primary counter mode if high
|
|
|
|
// Set the maximum and minimum limit values to watch
|
2016-11-21 05:17:07 -05:00
|
|
|
.counter_h_lim = PCNT_H_LIM_VAL,
|
|
|
|
.counter_l_lim = PCNT_L_LIM_VAL,
|
|
|
|
};
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Initialize PCNT unit */
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_unit_config(&pcnt_config);
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Configure and enable the input filter */
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_set_filter_value(PCNT_TEST_UNIT, 100);
|
|
|
|
pcnt_filter_enable(PCNT_TEST_UNIT);
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Set threshold 0 and 1 values and enable events to watch */
|
2016-11-23 05:15:54 -05:00
|
|
|
pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_1, PCNT_THRESH1_VAL);
|
|
|
|
pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_1);
|
|
|
|
pcnt_set_event_value(PCNT_TEST_UNIT, PCNT_EVT_THRES_0, PCNT_THRESH0_VAL);
|
|
|
|
pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_THRES_0);
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Enable events on zero, maximum and minimum limit values */
|
|
|
|
pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_ZERO);
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_H_LIM);
|
|
|
|
pcnt_event_enable(PCNT_TEST_UNIT, PCNT_EVT_L_LIM);
|
|
|
|
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Initialize PCNT's counter */
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_counter_pause(PCNT_TEST_UNIT);
|
|
|
|
pcnt_counter_clear(PCNT_TEST_UNIT);
|
2017-09-22 12:42:17 -04:00
|
|
|
|
|
|
|
/* Register ISR handler and enable interrupts for PCNT unit */
|
2018-05-25 03:37:20 -04:00
|
|
|
pcnt_isr_register(pcnt_example_intr_handler, NULL, 0, &user_isr_handle);
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_intr_enable(PCNT_TEST_UNIT);
|
2017-09-22 12:42:17 -04:00
|
|
|
|
|
|
|
/* Everything is set up, now go to counting */
|
2016-11-21 05:17:07 -05:00
|
|
|
pcnt_counter_resume(PCNT_TEST_UNIT);
|
|
|
|
}
|
|
|
|
|
2016-11-23 05:15:54 -05:00
|
|
|
void app_main()
|
2016-11-21 05:17:07 -05:00
|
|
|
{
|
2017-09-22 12:42:17 -04:00
|
|
|
/* Initialize LEDC to generate sample pulse signal */
|
2016-11-23 05:15:54 -05:00
|
|
|
ledc_init();
|
2017-09-22 12:42:17 -04:00
|
|
|
|
|
|
|
/* Initialize PCNT event queue and PCNT functions */
|
2016-11-23 23:01:21 -05:00
|
|
|
pcnt_evt_queue = xQueueCreate(10, sizeof(pcnt_evt_t));
|
2017-03-22 00:36:11 -04:00
|
|
|
pcnt_example_init();
|
2016-11-23 05:15:54 -05:00
|
|
|
|
2016-11-21 05:17:07 -05:00
|
|
|
int16_t count = 0;
|
2016-11-23 23:01:21 -05:00
|
|
|
pcnt_evt_t evt;
|
|
|
|
portBASE_TYPE res;
|
2017-09-22 12:42:17 -04:00
|
|
|
while (1) {
|
|
|
|
/* Wait for the event information passed from PCNT's interrupt handler.
|
|
|
|
* Once received, decode the event type and print it on the serial monitor.
|
|
|
|
*/
|
2016-12-21 20:42:21 -05:00
|
|
|
res = xQueueReceive(pcnt_evt_queue, &evt, 1000 / portTICK_PERIOD_MS);
|
2017-09-22 12:42:17 -04:00
|
|
|
if (res == pdTRUE) {
|
2016-11-23 23:01:21 -05:00
|
|
|
pcnt_get_counter_value(PCNT_TEST_UNIT, &count);
|
|
|
|
printf("Event PCNT unit[%d]; cnt: %d\n", evt.unit, count);
|
2017-09-22 12:42:17 -04:00
|
|
|
if (evt.status & PCNT_STATUS_THRES1_M) {
|
2016-11-23 23:01:21 -05:00
|
|
|
printf("THRES1 EVT\n");
|
|
|
|
}
|
2017-09-22 12:42:17 -04:00
|
|
|
if (evt.status & PCNT_STATUS_THRES0_M) {
|
2016-11-23 23:01:21 -05:00
|
|
|
printf("THRES0 EVT\n");
|
|
|
|
}
|
2017-09-22 12:42:17 -04:00
|
|
|
if (evt.status & PCNT_STATUS_L_LIM_M) {
|
2016-11-23 23:01:21 -05:00
|
|
|
printf("L_LIM EVT\n");
|
|
|
|
}
|
2017-09-22 12:42:17 -04:00
|
|
|
if (evt.status & PCNT_STATUS_H_LIM_M) {
|
2016-11-23 23:01:21 -05:00
|
|
|
printf("H_LIM EVT\n");
|
|
|
|
}
|
2017-09-22 12:42:17 -04:00
|
|
|
if (evt.status & PCNT_STATUS_ZERO_M) {
|
2016-11-23 23:01:21 -05:00
|
|
|
printf("ZERO EVT\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pcnt_get_counter_value(PCNT_TEST_UNIT, &count);
|
|
|
|
printf("Current counter value :%d\n", count);
|
|
|
|
}
|
2016-11-21 05:17:07 -05:00
|
|
|
}
|
2018-05-25 03:37:20 -04:00
|
|
|
if(user_isr_handle) {
|
|
|
|
//Free the ISR service handle.
|
|
|
|
esp_intr_free(user_isr_handle);
|
|
|
|
user_isr_handle = NULL;
|
|
|
|
}
|
2016-11-21 05:17:07 -05:00
|
|
|
}
|