summaryrefslogtreecommitdiff
path: root/usb-1wire/usb-1wire.c
blob: 6fed4f541a0fcfaa302853f6ca9237068874b172 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *  spreadspace avr utils - usb-1wire example
 *
 *
 *  Copyright (C) 2013 Bernhard Tittelbach <xro@realraum.at>
 *  basically this is refactored and enhanced code from:
 *   http://www.pjrc.com/teensy/td_libs_OneWire.html
 *
 *  This file is part of spreadspace avr utils.
 *
 *  spreadspace avr utils is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  spreadspace avr utils is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
 */


#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>

#include "util.h"
#include "led.h"
#include "usbio.h"

#include "onewire.h"

#define MAX_OWI_DEVICES 3

uint8_t owi_addr_[MAX_OWI_DEVICES][8];
uint8_t num_owi_dev_found_;

void discoverOWIBus(void)
{
    uint8_t d=0;
    led_on();

    printf("Searching OW Bus ");
    num_owi_dev_found_ = 0;
    if (owi_reset())
        printf(" !something is there! ");
    owi_reset_search();
    _delay_ms(250);
    
    //Search only for DS1820 temp sensors
    owi_target_search(DS1820_FAMILY_ID);
    
    while ( owi_search(owi_addr_[ d ]))
    {
        d++;
        printf("%d. found, ", d);
        led_toggle();
        if ( d >= MAX_OWI_DEVICES)
            break;
    }
    num_owi_dev_found_ = d;
    printf(" done \r\n");
    printf("%d devices found\r\n", d);
    
    for (d=0; d<num_owi_dev_found_; d++)
    {
        printf("OW Dev #%d: addr = ", d);
        for (uint8_t a=0;  a<8; a++)
            printf("%02x ", owi_addr_[d][a]);
        if (owi_crc8(owi_addr_[d], 7) == owi_addr_[d][7])
            printf("  CRC OK");
        else
            printf("  CRC ERROR");        
        printf("\r\n");
    }
    led_off();
}

void tempToUSB(uint8_t resolution) 
{
    uint8_t data[9];
    int16_t celsius = 0;
    int16_t raw = 0;
    uint8_t type_s, cfg;
    uint8_t d=0;
    uint8_t cfg_res=12;
    uint8_t crc_result = 0;
    
    led_on();

    owi_reset();
    //we assume we do NOT use parasitic power, so we can issue
    //commands while other sensors are already busy converting
    for (d=0; d<num_owi_dev_found_; d++)
    {
        owi_select(owi_addr_[d]);
        
        owi_write(DS1820_WRITE_SCRATCHPAD, 0);
        owi_write(0xFF, 0);
        owi_write(0x7F, 0);
        owi_write(resolution | 0x1F, 0);
        owi_reset();
        owi_select(owi_addr_[d]);
        owi_write(DS1820_START_CONVERSION, 0);
    }

    switch (resolution) 
    {
            case DS1820_RESOLUTION_9BITS: _delay_ms(DS1820_TCONV_MS_9BITS); break;
            case DS1820_RESOLUTION_10BITS: _delay_ms(DS1820_TCONV_MS_10BITS); break;
            case DS1820_RESOLUTION_11BITS: _delay_ms(DS1820_TCONV_MS_11BITS); break;
            default: _delay_ms(DS1820_TCONV_MS_12BITS); break;
    }
    
    if (owi_reset()) //reset is needed
        printf("Sensor present\r\n");
    else
        printf("Sensor NOT present\r\n");    
    
    for (d=0; d<num_owi_dev_found_; d++)
    {
        owi_select(owi_addr_[d]);
        owi_write(DS1820_READ_SCRATCHPAD, 0);         // Read Scratchpad
        printf("Data: ");
        for (uint8_t i = 0; i < 9; i++) {          // we need 9 bytes
            data[i] = owi_read();
            printf("%02x " , data[i]);
        }
        crc_result = owi_crc8(data,8);
        printf(" crc: %x is %s\r\n" , crc_result, (crc_result == data[8])? "OK": "ERRORENOUS");

        // the first ROM byte indicates which chip
        switch (owi_addr_[d][0]) 
        {
            case 0x10:
                //~ Serial.println("  Chip = DS18S20");  // or old DS1820
                type_s = 1;
                break;
            case 0x28:
                //~ Serial.println("  Chip = DS18B20");
                type_s = 0;
                break;
            case 0x22:
                //~ Serial.println("  Chip = DS1822");
                type_s = 0;
                break;
            default:
                continue;
        } 
        
        // Convert the data to actual temperature
        // because the result is a 16 bit signed integer, it should
        // be stored to an "int16_t" type, which is always 16 bits
        // even when compiled on a 32 bit processor.
        raw = (data[1] << 8) | data[0];
        if (type_s)
        {
            raw = raw << 3; // 9 bit resolution default
            if (data[7] == 0x10) {
                // "count remain" gives full 12 bit resolution
                raw = (raw & 0xFFF0) + 12 - data[6];
            }
        } else {
            cfg = (data[4] & 0x60);
            // at lower res, the low bits are undefined, so let's zero them
            if (cfg == 0x00)
            {
                raw = raw & ~7;  // 9 bit resolution, 93.75 ms
                cfg_res = 9;
            }
            else if (cfg == 0x20)
            {
                raw = raw & ~3; // 10 bit res, 187.5 ms
                cfg_res = 10;
            }
            else if (cfg == 0x40)
            {
                raw = raw & ~1; // 11 bit res, 375 ms
                cfg_res = 11;
            }
        //// default is 12 bit resolution, 750 ms conversion time
        }
        celsius = raw / 16;
      
        printf("Temperature: raw %d, celsius: %d, resolution: %d bits\r\n", raw, celsius, cfg_res);
    }
    led_off();
}


void handle_cmd(uint8_t cmd)
{
    switch(cmd) {
        case '0': discoverOWIBus(); break;
        case '1': tempToUSB(DS1820_RESOLUTION_9BITS); break;
        case '2': tempToUSB(DS1820_RESOLUTION_10BITS); break;
        case '3': tempToUSB(DS1820_RESOLUTION_11BITS); break;
        case '4': tempToUSB(DS1820_RESOLUTION_12BITS); break;
        case 'r': reset2bootloader(); break;
        default: printf("error\r\n"); return;
    }
    printf("ok\r\n");
}

int main(void)
{
    MCUSR &= ~(1 << WDRF);
    wdt_disable();

    cpu_init();
    led_init();
    usbio_init();
    sei();
    DDRC &= ~7;
    owi_init(7, &PINC);

    for(;;) 
    {
        int16_t BytesReceived = usbio_bytes_received();
        while(BytesReceived > 0) 
        {
            int ReceivedByte = fgetc(stdin);
            if(ReceivedByte != EOF) {
                handle_cmd(ReceivedByte);
            }
            BytesReceived--;
        }

    usbio_task();
    }
}