Skip to content
This repository was archived by the owner on Sep 7, 2022. It is now read-only.

Commit 78a0f9e

Browse files
committed
Initial, but working versions
1 parent 874b115 commit 78a0f9e

File tree

2 files changed

+265
-0
lines changed

2 files changed

+265
-0
lines changed

dht.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#include "esp_common.h"
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <gpio.h>
5+
/*
6+
GPIO0: PERIPHS_IO_MUX_GPIO0_U
7+
GPIO1: PERIPHS_IO_MUX_U0TXD_U
8+
GPIO2: PERIPHS_IO_MUX_GPIO2_U
9+
GPIO3: PERIPHS_IO_MUX_U0RXD_U
10+
GPIO4: PERIPHS_IO_MUX_GPIO4_U
11+
GPIO5: PERIPHS_IO_MUX_GPIO5_U
12+
GPIO6: PERIPHS_IO_MUX_SD_CLK_U
13+
GPIO7: PERIPHS_IO_MUX_SD_DATA0_U
14+
GPIO8: PERIPHS_IO_MUX_SD_DATA1_U
15+
GPIO9: PERIPHS_IO_MUX_SD_DATA2_U
16+
GPIO10: PERIPHS_IO_MUX_SD_DATA3_U
17+
GPIO11: PERIPHS_IO_MUX_SD_CMD_U
18+
GPIO12: PERIPHS_IO_MUX_MTDI_U
19+
GPIO13: PERIPHS_IO_MUX_MTCK_U
20+
GPIO14: PERIPHS_IO_MUX_MTMS_U
21+
GPIO15: PERIPHS_IO_MUX_MTDO_U
22+
*/
23+
24+
/* ESP12 PIN4 and PIN5 swapped :@ */
25+
#define OW_PIN_NUM 4
26+
#define OW_GET_IN() ( GPIO_INPUT_GET(GPIO_ID_PIN(OW_PIN_NUM)) )
27+
#define OW_OUT_LOW() ( GPIO_OUTPUT_SET(GPIO_ID_PIN(OW_PIN_NUM), 0) )
28+
#define OW_OUT_HIGH() ( GPIO_OUTPUT_SET(GPIO_ID_PIN(OW_PIN_NUM), 1) )
29+
#define OW_DIR_IN() ( GPIO_DIS_OUTPUT(GPIO_ID_PIN(OW_PIN_NUM)) )
30+
31+
void delay_ms(uint32_t ms)
32+
{
33+
uint32_t i;
34+
for (i = 0; i < ms; i++)
35+
os_delay_us(1000);
36+
}
37+
38+
#define MAXWAIT 1000000
39+
int expectpulse(uint level) {
40+
int waittime = MAXWAIT-1;
41+
while (waittime--) {
42+
if (OW_GET_IN() == level) {
43+
break;
44+
}
45+
//os_delay_us(1);
46+
}
47+
if (waittime)
48+
return(MAXWAIT-waittime);
49+
else
50+
return(0);
51+
}
52+
53+
int dht_read(int *temp, int *hum) {
54+
uint8_t data[5];
55+
int i;
56+
57+
memset(data, 0x0, 5);
58+
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
59+
PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO4_U);
60+
61+
OW_OUT_HIGH();
62+
delay_ms(250);
63+
OW_OUT_LOW();
64+
delay_ms(5); // Tbe (Host starting signal low time)
65+
66+
portENTER_CRITICAL();
67+
uint32_t cycles[80];
68+
{
69+
int i;
70+
OW_DIR_IN();
71+
72+
i = expectpulse(1);
73+
if (!i)
74+
goto bad;
75+
76+
i = expectpulse(0);
77+
if (!i)
78+
goto bad;
79+
80+
i = expectpulse(1);
81+
if (!i)
82+
goto bad;
83+
84+
i = expectpulse(0);
85+
if (!i)
86+
goto bad;
87+
88+
for (i=0; i<80; i+=2) {
89+
cycles[i] = expectpulse(1);
90+
cycles[i+1] = expectpulse(0);
91+
}
92+
}
93+
portEXIT_CRITICAL();
94+
for (i=0; i<40; ++i) {
95+
uint32_t lowCycles = cycles[2*i];
96+
uint32_t highCycles = cycles[2*i+1];
97+
if ((lowCycles == 0) || (highCycles == 0)) {
98+
return(0);
99+
}
100+
data[i/8] <<= 1;
101+
if (highCycles > lowCycles) {
102+
data[i/8] |= 1;
103+
}
104+
}
105+
106+
*hum = ((data[0] << 8) + data[1]);
107+
if ( *hum > 1000 )
108+
*hum = data[0];
109+
110+
*temp = (((data[2] & 0x7F) << 8) + data[3]);
111+
if ( *temp > 1250 )
112+
*temp = data[2];
113+
if ( data[2] & 0x80 )
114+
*temp = -*temp;
115+
116+
return(1);
117+
bad:
118+
portEXIT_CRITICAL();
119+
return(0);
120+
}

ds18b20.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#include "esp_common.h"
2+
#include <fcntl.h>
3+
#include <stdio.h>
4+
#include <gpio.h>
5+
/*
6+
GPIO0: PERIPHS_IO_MUX_GPIO0_U
7+
GPIO1: PERIPHS_IO_MUX_U0TXD_U
8+
GPIO2: PERIPHS_IO_MUX_GPIO2_U
9+
GPIO3: PERIPHS_IO_MUX_U0RXD_U
10+
GPIO4: PERIPHS_IO_MUX_GPIO4_U
11+
GPIO5: PERIPHS_IO_MUX_GPIO5_U
12+
GPIO6: PERIPHS_IO_MUX_SD_CLK_U
13+
GPIO7: PERIPHS_IO_MUX_SD_DATA0_U
14+
GPIO8: PERIPHS_IO_MUX_SD_DATA1_U
15+
GPIO9: PERIPHS_IO_MUX_SD_DATA2_U
16+
GPIO10: PERIPHS_IO_MUX_SD_DATA3_U
17+
GPIO11: PERIPHS_IO_MUX_SD_CMD_U
18+
GPIO12: PERIPHS_IO_MUX_MTDI_U
19+
GPIO13: PERIPHS_IO_MUX_MTCK_U
20+
GPIO14: PERIPHS_IO_MUX_MTMS_U
21+
GPIO15: PERIPHS_IO_MUX_MTDO_U
22+
*/
23+
24+
/* ESP12 PIN4 and PIN5 swapped :@ */
25+
#define OW_PIN_NUM 4
26+
#define OW_GET_IN() ( GPIO_INPUT_GET(GPIO_ID_PIN(OW_PIN_NUM)) )
27+
#define OW_OUT_LOW() ( GPIO_OUTPUT_SET(GPIO_ID_PIN(OW_PIN_NUM), 0) )
28+
#define OW_OUT_HIGH() ( GPIO_OUTPUT_SET(GPIO_ID_PIN(OW_PIN_NUM), 1) )
29+
#define OW_DIR_IN() ( GPIO_DIS_OUTPUT(GPIO_ID_PIN(OW_PIN_NUM)) )
30+
31+
// OK if just using a single permanently connected device
32+
int onewire_reset() {
33+
int r;
34+
PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO4_U, FUNC_GPIO4);
35+
PIN_PULLUP_DIS(PERIPHS_IO_MUX_GPIO4_U);
36+
portENTER_CRITICAL();
37+
OW_OUT_LOW();
38+
os_delay_us(480);
39+
OW_DIR_IN();
40+
os_delay_us(70);
41+
r = OW_GET_IN(); // Is OW device present it will pull low
42+
portEXIT_CRITICAL();
43+
os_delay_us(410); // TODO: Why?
44+
// if r - 1 - bad, means device didnt pulled low
45+
return(r);
46+
}
47+
48+
#define OW_RECOVERY_TIME 10 // Depends on wire length
49+
50+
/*********************** onewire_write() ********************************/
51+
/*This function writes a byte to the sensor.*/
52+
/* */
53+
/*Parameters: byte - the byte to be written to the 1-wire */
54+
/*Returns: */
55+
/*********************************************************************/
56+
57+
void onewire_write(int data) {
58+
int count, temp;
59+
60+
for (count=0; count<8; ++count) {
61+
portENTER_CRITICAL();
62+
temp = data>>count;
63+
temp &= 0x1;
64+
if (temp) {
65+
OW_OUT_LOW();
66+
os_delay_us(10);
67+
OW_OUT_HIGH();
68+
os_delay_us(55);
69+
} else {
70+
OW_OUT_LOW();
71+
os_delay_us(65);
72+
OW_OUT_HIGH();
73+
os_delay_us(5);
74+
}
75+
portEXIT_CRITICAL();
76+
}
77+
}
78+
79+
int onewire_read() {
80+
int count, data = 0;
81+
for (count=0; count<8; ++count) {
82+
portENTER_CRITICAL();
83+
OW_OUT_LOW();
84+
os_delay_us(3);
85+
OW_DIR_IN();
86+
os_delay_us(10);
87+
if (OW_GET_IN())
88+
data |= (1<<count);
89+
os_delay_us(53);
90+
portEXIT_CRITICAL();
91+
}
92+
return( data );
93+
}
94+
95+
int ds1820_read() {
96+
uint8_t i=0, data[9], type=0;
97+
int16_t raw;
98+
99+
if (onewire_reset())
100+
return(0); // Device not found
101+
102+
onewire_reset();
103+
104+
// Read ROM (and important - type)
105+
onewire_write(0x33);
106+
for (i=0; i<8; i++)
107+
data[i] = onewire_read();
108+
109+
type = data[0];
110+
111+
onewire_reset();
112+
onewire_write(0xCC);
113+
onewire_write(0x44);
114+
115+
while (i == 0)
116+
i = onewire_read();
117+
// Conversion delay
118+
vTaskDelay(1000 / portTICK_RATE_MS);
119+
120+
onewire_reset();
121+
onewire_write(0xCC);
122+
onewire_write(0xBE);
123+
for (i=0; i<9; i++)
124+
data[i] = onewire_read();
125+
126+
raw = (data[1] << 8) | data[0]; // glue to 16bit value
127+
// old DS
128+
if (type == 0x10) {
129+
raw = raw << 3; // 9 bit resolution default
130+
// remaining - to archieve full resolution (12bit)
131+
if (data[7] == 0x10)
132+
raw = (raw & 0xFFF0) + 12 - data[6];
133+
} else {
134+
uint8_t cfg = (data[4] & 0x60);
135+
if (cfg == 0x00)
136+
raw = raw & ~7; // 9 bit resolution, 93.75 ms
137+
else if (cfg == 0x20)
138+
raw = raw & ~3; // 10 bit res, 187.5 ms
139+
else if (cfg == 0x40)
140+
raw = raw & ~1; // 11 bit res, 375 ms
141+
}
142+
// default is 12 bit resolution, 750 ms conversion time
143+
// we get YXXX - where it is Y.XXX * 1000 to avoid float
144+
return(raw * 1000 / 16);
145+
}

0 commit comments

Comments
 (0)