2 * Interface to DS18B20 Temperature Sensors
4 * (c) 2019 Martin Mareš <mj@ucw.cz>
11 #include <libopencm3/cm3/cortex.h>
12 #include <libopencm3/stm32/dma.h>
13 #include <libopencm3/stm32/gpio.h>
14 #include <libopencm3/stm32/rcc.h>
17 /*** Configuration ***/
19 // You should set the following parameters in config.h
21 // #define DS_TIMER TIM3
22 // #define DS_GPIO GPIOA
23 // #define DS_PIN GPIO7
24 // #define DS_DMA DMA1
25 // #define DS_DMA_CH 6
30 // Maximum number of supported sensors
31 // #define DS_NUM_SENSORS 8
34 #define DEBUG debug_printf
36 #define DEBUG(xxx, ...) do { } while (0)
40 #define DEBUG2 debug_printf
42 #define DEBUG2(xxx, ...) do { } while (0)
45 static volatile u32 ds_dma_buffer;
47 static bool ds_reset(void)
49 DEBUG2("DS18B20: Reset\n");
50 timer_disable_counter(DS_TIMER);
51 timer_one_shot_mode(DS_TIMER);
53 // DMA for reading pin state
54 ds_dma_buffer = 0xdeadbeef;
55 dma_set_memory_address(DS_DMA, DS_DMA_CH, (u32) &ds_dma_buffer);
56 dma_set_peripheral_address(DS_DMA, DS_DMA_CH, (u32) &GPIO_IDR(DS_GPIO));
57 dma_set_number_of_data(DS_DMA, DS_DMA_CH, 1);
58 dma_enable_channel(DS_DMA, DS_DMA_CH);
60 // CC1 is used to drive the DMA (read line state at specified time)
61 timer_disable_oc_output(DS_TIMER, TIM_OC1);
62 timer_set_oc_mode(DS_TIMER, TIM_OC1, TIM_OCM_FROZEN);
63 timer_set_oc_value(DS_TIMER, TIM_OC1, 560);
64 timer_set_dma_on_compare_event(DS_TIMER);
65 timer_enable_dma_cc1(DS_TIMER);
67 // CC2 is used to generate pulses (return line to idle state at specified time)
68 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_FORCE_HIGH);
69 timer_enable_oc_output(DS_TIMER, TIM_OC2);
70 timer_set_oc_value(DS_TIMER, TIM_OC2, 480);
71 timer_set_oc_polarity_low(DS_TIMER, TIM_OC2);
73 // Set timer period to the length of the whole transaction (1 ms)
74 timer_set_period(DS_TIMER, 999);
76 // Pull line down and start timer
77 cm_disable_interrupts();
78 timer_enable_counter(DS_TIMER);
79 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_INACTIVE);
80 cm_enable_interrupts();
82 // Wait until the timer expires
83 while (timer_is_counter_enabled(DS_TIMER))
85 // Counter is automatically disabled at the end of cycle
88 timer_disable_dma_cc1(DS_TIMER);
89 dma_disable_channel(DS_DMA, DS_DMA_CH);
91 DEBUG2("Init DMA: %08x [%u] (%u remains)\n",
93 !!(ds_dma_buffer & DS_PIN),
94 dma_get_number_of_data(DS_DMA, DS_DMA_CH));
96 // Did the device respond?
97 if (ds_dma_buffer & DS_PIN) {
98 DEBUG("DS18B20: Initialization failed\n");
104 static void ds_send_bit(bool bit)
106 timer_set_period(DS_TIMER, 99); // Each write slot takes 100 μs
107 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_FORCE_HIGH);
108 timer_set_oc_value(DS_TIMER, TIM_OC2, (bit ? 3 : 89)); // 1: 3μs pulse, 0: 89μs pulse
109 cm_disable_interrupts();
110 // XXX: On STM32F1, we must configure the OC channel _after_ we enable the counter,
111 // otherwise OC triggers immediately. Reasons?
112 timer_enable_counter(DS_TIMER);
113 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_INACTIVE);
114 cm_enable_interrupts();
115 while (timer_is_counter_enabled(DS_TIMER))
119 static void ds_send_byte(byte b)
121 DEBUG2("DS write: %02x\n", b);
122 for (uint m = 1; m < 0x100; m <<= 1)
126 static bool ds_recv_bit(void)
128 timer_set_period(DS_TIMER, 79); // Each read slot takes 80μs
129 timer_set_oc_value(DS_TIMER, TIM_OC2, 2); // Generate 2μs pulse to start read slot
130 timer_set_oc_value(DS_TIMER, TIM_OC1, 8); // Sample data 8μs after start of slot
131 timer_enable_dma_cc1(DS_TIMER);
133 ds_dma_buffer = 0xdeadbeef;
134 dma_set_number_of_data(DS_DMA, DS_DMA_CH, 1);
135 dma_enable_channel(DS_DMA, DS_DMA_CH);
136 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_FORCE_HIGH);
137 cm_disable_interrupts();
138 timer_enable_counter(DS_TIMER);
139 timer_set_oc_mode(DS_TIMER, TIM_OC2, TIM_OCM_INACTIVE);
140 cm_enable_interrupts();
141 while (timer_is_counter_enabled(DS_TIMER))
143 // DEBUG2("XXX %08x\n", ds_dma_buffer);
144 bool out = ds_dma_buffer & DS_PIN;
145 dma_disable_channel(DS_DMA, DS_DMA_CH);
147 timer_disable_dma_cc1(DS_TIMER);
152 static byte ds_recv_byte(void)
155 for (uint m = 1; m < 0x100; m <<= 1) {
160 DEBUG2("DS read: %02x\n", out);
164 static byte ds_buf[10];
166 static byte ds_crc_block(uint n)
168 /// XXX: This might be worth optimizing
171 for (uint i = 0; i < n; i++) {
173 for (uint j = 0; j < 8; j++) {
174 uint k = (b & 1) ^ (crc >> 7);
175 crc = (crc << 1) & 0xff;
185 static bool ds_recv_block(uint n)
187 for (uint i = 0; i < n; i++)
188 ds_buf[i] = ds_recv_byte();
190 byte crc = ds_crc_block(n);
192 DEBUG("DS18B20: Invalid CRC %02x\n", crc);
198 struct ds_sensor ds_sensors[DS_NUM_SENSORS];
200 #if DS_NUM_SENSORS == 1
202 static void ds_enumerate(void)
207 ds_send_byte(0x33); // READ_ROM
208 if (!ds_recv_block(8))
211 DEBUG("DS18B20: Found sensor ");
212 for (uint i = 0; i < 8; i++) {
213 DEBUG("%02x", ds_buf[i]);
214 ds_sensors[0].address[i] = ds_buf[i];
221 static void ds_enumerate(void)
224 * The enumeration algorithm roughly follows the one described in the
225 * Book of iButton Standards (Maxim Integrated Application Note 937).
227 * It simulates depth-first search on the trie of all device IDs.
228 * In each pass, it walks the trie from the root and recognizes branching nodes.
230 * The old_choice variable remembers the deepest left branch taken in the
231 * previous pass, new_choice is the same for the current pass.
234 DEBUG("DS18B20: Enumerate\n");
236 uint num_sensors = 0;
242 DEBUG("DS18B20: Enumeration found no sensor\n");
246 ds_send_byte(0xf0); // SEARCH_ROM
248 for (byte i=0; i<64; i++) {
249 bool have_one = ds_recv_bit();
250 bool have_zero = ds_recv_bit();
251 bool old_bit = addr[i/8] & (1U << (i%8));
253 switch (2*have_one + have_zero) {
255 // This should not happen
256 DEBUG("DS18B20: Enumeration failed\n");
270 else if (i > old_choice) {
280 addr[i/8] |= 1U << (i%8);
282 addr[i/8] &= ~(1U << (i%8));
283 ds_send_bit(new_bit);
286 if (num_sensors >= DS_NUM_SENSORS) {
287 DEBUG("DS18B20: Too many sensors\n");
291 DEBUG("DS18B20: Found sensor #%u: ", num_sensors);
292 for (byte i=0; i<8; i++)
293 DEBUG("%02x", addr[i]);
294 if (ds_crc_block(8)) {
295 DEBUG(" - invalid CRC!\n");
296 } else if (ds_buf[0] == 0x28) {
298 memcpy(ds_sensors[num_sensors].address, ds_buf, 8);
301 DEBUG(" - wrong type\n");
304 old_choice = new_choice;
314 DEBUG("DS18B20: Init\n");
316 for (uint i = 0; i < DS_NUM_SENSORS; i++) {
317 memset(ds_sensors[i].address, 0, 8);
318 ds_sensors[i].current_temp = DS_TEMP_UNKNOWN;
321 dma_set_read_from_peripheral(DS_DMA, DS_DMA_CH);
322 dma_set_priority(DS_DMA, DS_DMA_CH, DMA_CCR_PL_VERY_HIGH);
323 dma_disable_peripheral_increment_mode(DS_DMA, DS_DMA_CH);
324 dma_enable_memory_increment_mode(DS_DMA, DS_DMA_CH);
325 dma_set_peripheral_size(DS_DMA, DS_DMA_CH, DMA_CCR_PSIZE_16BIT);
326 dma_set_memory_size(DS_DMA, DS_DMA_CH, DMA_CCR_MSIZE_16BIT);
328 timer_set_prescaler(DS_TIMER, CPU_CLOCK_MHZ - 1); // 1 tick = 1 μs
329 timer_set_mode(DS_TIMER, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
330 timer_disable_preload(DS_TIMER);
332 gpio_set_mode(DS_GPIO, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, DS_PIN);
336 // FIXME: Configure precision?
339 #if DS_NUM_SENSORS == 1
340 #define ds_current_id 0
342 static byte ds_current_id;
345 static bool ds_activate(void)
348 DEBUG("DS18B20: Reset failed\n");
351 #if DS_NUM_SENSORS == 1
352 ds_send_byte(0xcc); // SKIP_ROM
354 ds_send_byte(0x55); // MATCH_ROM
355 for (uint i = 0; i < 8; i++)
356 ds_send_byte(ds_sensors[ds_current_id].address[i]);
363 static byte ds_running;
364 static byte ds_timeout;
368 #if DS_NUM_SENSORS != 1
369 uint maxn = DS_NUM_SENSORS;
374 if (ds_current_id >= DS_NUM_SENSORS) {
377 } while (!ds_sensors[ds_current_id].address[0]);
379 if (!ds_activate()) {
380 ds_sensors[ds_current_id].current_temp = DS_TEMP_UNKNOWN;
383 ds_send_byte(0x44); // CONVERT_T
388 if (!ds_recv_bit()) {
390 DEBUG("DS18B20 #%u: Timeout\n", ds_current_id);
391 ds_sensors[ds_current_id].current_temp = DS_TEMP_UNKNOWN;
401 ds_send_byte(0xbe); // READ_SCRATCHPAD
402 if (!ds_recv_block(9)) {
403 ds_sensors[ds_current_id].current_temp = DS_TEMP_UNKNOWN;
406 int t = (int16_t) (ds_buf[0] | (ds_buf[1] << 8));
409 DEBUG("DS18B20 #%u: %d.%03d degC\n", ds_current_id, t / 1000, t % 1000);
410 ds_sensors[ds_current_id].current_temp = t;