|
| 1 | +#include <freertos/FreeRTOS.h> |
| 2 | +#include <freertos/task.h> |
| 3 | +#include <esp_log.h> |
| 4 | +#include <driver/rmt.h> |
| 5 | +#include "sdkconfig.h" |
| 6 | + |
| 7 | +// Clock divisor (base clock is 80MHz) |
| 8 | +#define CLK_DIV 100 |
| 9 | + |
| 10 | +// Number of clock ticks that represent 10us. 10 us = 1/100th msec. |
| 11 | +#define TICK_10_US (80000000 / CLK_DIV / 100000) |
| 12 | + |
| 13 | +static char tag[] = "rmt_receiver"; |
| 14 | +static RingbufHandle_t ringBuf; |
| 15 | + |
| 16 | +static void dumpStatus(rmt_channel_t channel) { |
| 17 | + bool loop_en; |
| 18 | + uint8_t div_cnt; |
| 19 | + uint8_t memNum; |
| 20 | + bool lowPowerMode; |
| 21 | + rmt_mem_owner_t owner; |
| 22 | + uint16_t idleThreshold; |
| 23 | + uint32_t status; |
| 24 | + rmt_source_clk_t srcClk; |
| 25 | + |
| 26 | + rmt_get_tx_loop_mode(channel, &loop_en); |
| 27 | + rmt_get_clk_div(channel, &div_cnt); |
| 28 | + rmt_get_mem_block_num(channel, &memNum); |
| 29 | + rmt_get_mem_pd(channel, &lowPowerMode); |
| 30 | + rmt_get_memory_owner(channel, &owner); |
| 31 | + rmt_get_rx_idle_thresh(channel, &idleThreshold); |
| 32 | + rmt_get_status(channel, &status); |
| 33 | + rmt_get_source_clk(channel, &srcClk); |
| 34 | + |
| 35 | + ESP_LOGD(tag, "Status for RMT channel %d", channel); |
| 36 | + ESP_LOGD(tag, "- Loop enabled: %d", loop_en); |
| 37 | + ESP_LOGD(tag, "- Clock divisor: %d", div_cnt); |
| 38 | + ESP_LOGD(tag, "- Number of memory blocks: %d", memNum); |
| 39 | + ESP_LOGD(tag, "- Low power mode: %d", lowPowerMode); |
| 40 | + ESP_LOGD(tag, "- Memory owner: %s", owner==RMT_MEM_OWNER_TX?"TX":"RX"); |
| 41 | + ESP_LOGD(tag, "- Idle threshold: %d", idleThreshold); |
| 42 | + ESP_LOGD(tag, "- Status: %d", status); |
| 43 | + ESP_LOGD(tag, "- Source clock: %s", srcClk==RMT_BASECLK_APB?"APB (80MHz)":"1MHz"); |
| 44 | +} |
| 45 | + |
| 46 | +bool isInRange(rmt_item32_t item, int lowDuration, int highDuration, int tolerance) { |
| 47 | + uint32_t lowValue = item.duration0 * 10 / TICK_10_US; |
| 48 | + uint32_t highValue = item.duration1 * 10 / TICK_10_US; |
| 49 | + /* |
| 50 | + ESP_LOGD(tag, "lowValue=%d, highValue=%d, lowDuration=%d, highDuration=%d", |
| 51 | + lowValue, highValue, lowDuration, highDuration); |
| 52 | + */ |
| 53 | + if (lowValue < (lowDuration - tolerance) || lowValue > (lowDuration + tolerance) || |
| 54 | + (highValue != 0 && |
| 55 | + (highValue < (highDuration - tolerance) || highValue > (highDuration + tolerance)))) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +bool NEC_is0(rmt_item32_t item) { |
| 62 | + return isInRange(item, 560, 560, 100); |
| 63 | +} |
| 64 | + |
| 65 | +bool NEC_is1(rmt_item32_t item) { |
| 66 | + return isInRange(item, 560, 1690, 100); |
| 67 | +} |
| 68 | + |
| 69 | +void decodeNEC(rmt_item32_t *data, int numItems) { |
| 70 | + if (!isInRange(data[0], 9000, 4500, 200)) { |
| 71 | + ESP_LOGD(tag, "Not an NEC"); |
| 72 | + return; |
| 73 | + } |
| 74 | + int i; |
| 75 | + uint8_t address = 0, notAddress = 0, command = 0, notCommand = 0; |
| 76 | + int accumCounter = 0; |
| 77 | + uint8_t accumValue = 0; |
| 78 | + for (i=1; i<numItems; i++) { |
| 79 | + if (NEC_is0(data[i])) { |
| 80 | + ESP_LOGD(tag, "%d: 0", i); |
| 81 | + accumValue = accumValue >> 1; |
| 82 | + } else if (NEC_is1(data[i])) { |
| 83 | + ESP_LOGD(tag, "%d: 1", i); |
| 84 | + accumValue = (accumValue >> 1) | 0x80; |
| 85 | + } else { |
| 86 | + ESP_LOGD(tag, "Unknown"); |
| 87 | + } |
| 88 | + if (accumCounter == 7) { |
| 89 | + accumCounter = 0; |
| 90 | + ESP_LOGD(tag, "Byte: 0x%.2x", accumValue); |
| 91 | + if (i==8) { |
| 92 | + address = accumValue; |
| 93 | + } else if (i==16) { |
| 94 | + notAddress = accumValue; |
| 95 | + } else if (i==24) { |
| 96 | + command = accumValue; |
| 97 | + } else if (i==32) { |
| 98 | + notCommand = accumValue; |
| 99 | + } |
| 100 | + accumValue = 0; |
| 101 | + } else { |
| 102 | + accumCounter++; |
| 103 | + } |
| 104 | + } |
| 105 | + ESP_LOGD(tag, "Address: 0x%.2x, NotAddress: 0x%.2x", address, notAddress ^ 0xff); |
| 106 | + if (address != (notAddress ^ 0xff) || command != (notCommand ^ 0xff)) { |
| 107 | + ESP_LOGD(tag, "Data mis match"); |
| 108 | + return; |
| 109 | + } |
| 110 | + ESP_LOGD(tag, "Address: 0x%.2x, Command: 0x%.2x", address, command); |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +static void task_watchRingbuf(void *ignore) { |
| 115 | + size_t itemSize; |
| 116 | + ESP_LOGD(tag, "Watching ringbuf: %d", TICK_10_US); |
| 117 | + while(1) { |
| 118 | + void *data = xRingbufferReceive(ringBuf, &itemSize, portMAX_DELAY); |
| 119 | + ESP_LOGD(tag, "Got an ringbuf item! Size=%d", itemSize); |
| 120 | + int numItems = itemSize / sizeof(rmt_item32_t); |
| 121 | + int i; |
| 122 | + rmt_item32_t *p = (rmt_item32_t *)data; |
| 123 | + for (i=0; i<numItems; i++) { |
| 124 | + ESP_LOGD(tag, "[0]: %d-%d us, [1]: %d-%d us", |
| 125 | + p->level0, p->duration0 * 10 / TICK_10_US, p->level1, p->duration1 * 10 / TICK_10_US); |
| 126 | + p++; |
| 127 | + } |
| 128 | + decodeNEC((rmt_item32_t *)data, numItems); |
| 129 | + vRingbufferReturnItem(ringBuf, data); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void runRmtTest() { |
| 134 | + ESP_LOGD(tag, ">> runRmtTest"); |
| 135 | + |
| 136 | + rmt_config_t config; |
| 137 | + config.rmt_mode = RMT_MODE_RX; |
| 138 | + config.channel = RMT_CHANNEL_0; |
| 139 | + config.gpio_num = 21; |
| 140 | + config.mem_block_num = 2; |
| 141 | + config.rx_config.filter_en = 1; |
| 142 | + config.rx_config.filter_ticks_thresh = 100; // 80000000/100 -> 800000 / 100 = 8000 = 125us |
| 143 | + config.rx_config.idle_threshold = TICK_10_US * 100 * 20; |
| 144 | + config.clk_div = CLK_DIV; |
| 145 | + |
| 146 | + ESP_ERROR_CHECK(rmt_config(&config)); |
| 147 | + ESP_ERROR_CHECK(rmt_driver_install(config.channel, 5000, 0)); |
| 148 | + rmt_get_ringbuf_handler(RMT_CHANNEL_0, &ringBuf); |
| 149 | + dumpStatus(config.channel); |
| 150 | + xTaskCreatePinnedToCore(&task_watchRingbuf, "task_watchRingbuf", 2048, NULL, 5, NULL, 0); |
| 151 | + rmt_rx_start(RMT_CHANNEL_0, 1); |
| 152 | + ESP_LOGD(tag, "<< runRmtTest"); |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | +void task_rmt_receiver(void *ignore) { |
| 158 | + runRmtTest(); |
| 159 | + vTaskDelete(NULL); |
| 160 | +} |
0 commit comments