Skip to content

Commit a05b622

Browse files
author
kolban
committed
Wed Jan 25 19:19:21 CST 2017
1 parent be067a8 commit a05b622

File tree

3 files changed

+220
-5
lines changed

3 files changed

+220
-5
lines changed
+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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+
}

i2c/scanner/i2cscanner.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <driver/i2c.h>
2+
#include <esp_log.h>
3+
#include <freertos/FreeRTOS.h>
4+
#include <freertos/task.h>
5+
#include <stdio.h>
6+
#include "sdkconfig.h"
7+
8+
#define SDA_PIN 18
9+
#define SCL_PIN 19
10+
11+
static char tag[] = "i2cscanner";
12+
13+
void task_i2cscanner(void *ignore) {
14+
ESP_LOGD(tag, ">> i2cScanner");
15+
i2c_config_t conf;
16+
conf.mode = I2C_MODE_MASTER;
17+
conf.sda_io_num = SDA_PIN;
18+
conf.scl_io_num = SCL_PIN;
19+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
20+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
21+
conf.master.clk_speed = 100000;
22+
i2c_param_config(I2C_NUM_0, &conf);
23+
24+
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
25+
26+
int i;
27+
esp_err_t espRc;
28+
printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n");
29+
printf("00: ");
30+
for (i=3; i< 0x78; i++) {
31+
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
32+
i2c_master_start(cmd);
33+
i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
34+
i2c_master_stop(cmd);
35+
36+
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
37+
if (i%16 == 0) {
38+
printf("\n%.2x:", i);
39+
}
40+
if (espRc == 0) {
41+
printf(" %.2x", i);
42+
} else {
43+
printf(" --");
44+
}
45+
//ESP_LOGD(tag, "i=%d, rc=%d (0x%x)", i, espRc, espRc);
46+
i2c_cmd_link_delete(cmd);
47+
}
48+
printf("\n");
49+
vTaskDelete(NULL);
50+
}

networking/mqtt/paho_mqtt_embedded_c/MQTTClient-C/src/MQTTClient.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -243,19 +243,24 @@ int cycle(MQTTClient* c, Timer* timer)
243243
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->readbuf, c->readbuf_size) != 1)
244244
goto exit;
245245
msg.qos = (enum QoS)intQoS;
246+
printf("X1\n");
246247
deliverMessage(c, &topicName, &msg);
248+
printf("Publish ... qos = %d\n", msg.qos);
247249
if (msg.qos != QOS0)
248250
{
249-
if (msg.qos == QOS1)
251+
252+
if (msg.qos == QOS1) {
250253
len = MQTTSerialize_ack(c->buf, c->buf_size, PUBACK, 0, msg.id);
251-
else if (msg.qos == QOS2)
254+
} else if (msg.qos == QOS2) {
252255
len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREC, 0, msg.id);
253-
if (len <= 0)
256+
} if (len <= 0) {
254257
rc = FAILURE;
255-
else
258+
} else {
256259
rc = sendPacket(c, len, timer);
257-
if (rc == FAILURE)
260+
}
261+
if (rc == FAILURE) {
258262
goto exit; // there was a problem
263+
}
259264
}
260265
break;
261266
}

0 commit comments

Comments
 (0)