upm  1.0.2
Sensor/Actuator repository for libmraa (v1.1.1)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ds18b20.h
Go to the documentation of this file.
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 
30 #include <mraa/uart_ow.h>
31 
32 #include "upm.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 // The family code for these devices. We handle all of them that
39 // are found on the bus.
40 #define DS18B20_FAMILY_CODE 0x28
41 
50  // forward declaration
51  typedef struct _ds18b20_info_t ds18b20_info_t;
52 
56  typedef struct _ds18b20_context {
57  mraa_uart_ow_context ow;
58 
59  // number of devices found
60  int numDevices;
61 
62  // list of allocated ds18b20_info_t instances
63  ds18b20_info_t *devices;
64  } *ds18b20_context;
65 
66  // commands
67  typedef enum {
68  DS18B20_CMD_CONVERT = 0x44, // start a temp conversion
69  DS18B20_CMD_WRITE_SCRATCHPAD = 0x4e,
70  DS18B20_CMD_READ_SCRATCHPAD = 0xbe,
71  DS18B20_CMD_COPY_SCRATCHPAD = 0x48, // copy scratchpad to EEPROM
72  DS18B20_CMD_RECALL_EEPROM = 0xb8, // copy EEPROM to scratchpad
73  DS18B20_CMD_READ_POWER_SUPPLY = 0xb4 // parasitically powered?
74  } DS18B20_CMD_T;
75 
76  // config register (scratchpad[4])
77  typedef enum {
78  DS18B20_CFG_RESOLUTION_R0 = 0x20,
79  DS18B20_CFG_RESOLUTION_R1 = 0x40,
80  _DS18B20_CFG_RESOLUTION_MASK = 3,
81  _DS18B20_CFG_RESOLUTION_SHIFT = 5
82 
83  // all other bits reserved and non-writable
84  } DS18B20_CFG_BITS_T;
85 
86  typedef enum {
87  DS18B20_RESOLUTION_9BITS = 0, // 93.75ms (tconv/8)
88  DS18B20_RESOLUTION_10BITS = 1, // 187.5 (tconv/4)
89  DS18B20_RESOLUTION_11BITS = 2, // 375ms (tconv/2)
90  DS18B20_RESOLUTION_12BITS = 3 // 750ms (tconv)
91  } DS18B20_RESOLUTIONS_T;
92 
102  ds18b20_context ds18b20_init(unsigned int uart);
103 
107  void ds18b20_close(ds18b20_context dev);
108 
116  void ds18b20_update(const ds18b20_context dev, int index);
117 
126  unsigned int index);
127 
136  void ds18b20_set_resolution(const ds18b20_context dev, unsigned int index,
137  DS18B20_RESOLUTIONS_T res);
138 
145  void ds18b20_copy_scratchpad(const ds18b20_context dev, unsigned int index);
146 
155  void ds18b20_recall_eeprom(const ds18b20_context dev, unsigned int index);
156 
163  int ds18b20_devices_found(const ds18b20_context dev);
164 
174  const uint8_t *get_id(const ds18b20_context dev, unsigned int index);
175 
176 #ifdef __cplusplus
177 }
178 #endif
void ds18b20_recall_eeprom(const ds18b20_context dev, unsigned int index)
void ds18b20_close(ds18b20_context dev)
Definition: ds18b20.c:187
void ds18b20_set_resolution(const ds18b20_context dev, unsigned int index, DS18B20_RESOLUTIONS_T res)
Definition: ds18b20.c:317
struct _ds18b20_context * ds18b20_context
void ds18b20_copy_scratchpad(const ds18b20_context dev, unsigned int index)
int ds18b20_devices_found(const ds18b20_context dev)
Definition: ds18b20.c:386
float ds18b20_get_temperature(const ds18b20_context dev, unsigned int index)
Definition: ds18b20.c:304
void ds18b20_update(const ds18b20_context dev, int index)
Definition: ds18b20.c:199
Definition: ds18b20.h:56
Definition: ds18b20.c:40
const uint8_t * get_id(const ds18b20_context dev, unsigned int index)
ds18b20_context ds18b20_init(unsigned int uart)
Definition: ds18b20.c:50