Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stm32/f1/other: add ds18x20 library and demo app from Martin Thomas #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/stm32/f1/other/ds18x20/Makefile
@@ -0,0 +1,29 @@
##
## This file is part of the libopencm3-examples project.
##
## Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
##
## This library is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This library 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 Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with this library. If not, see <http://www.gnu.org/licenses/>.
##

BINARY = ds18x20demo

LDSCRIPT = ../../stm32vl-discovery/stm32vl-discovery.ld

OBJS = ds18x20.o onewire.o crc8.o

LDFLAGS = --specs=nano.specs --specs=rdimon.specs -lrdimon

include ../../Makefile.include

113 changes: 113 additions & 0 deletions examples/stm32/f1/other/ds18x20/README
@@ -0,0 +1,113 @@
This is a port of Martin Thomas's DS18x20 library and demo application
from http://siwawi.bauing.uni-kl.de/avr_projects/tempsensor/index.html

The port is fairly straightforward and was tested on an
stm32vldiscovery board with one DS18B20 sensor attached in parasite
power mode and an additional 15m line with seven DS18B20 sensors in
both parasite and externally powered modes. The DQ pins were connected
to PC6 shorted with PC7.

This is also an example of using direct register access to handle GPIO
to show off the fastest possible way to handle it, loosing
cross-target portability of course.

The output is automatically redirected to the machine running debugger
via ARM semihosting (works out of the box with BMP; OpenOCD needs "arm
semihosting enable" command to receive the output), no extra UART
connections needed.

Differences from the original code:

1. Multi-bus support removed
2. Output beautifications

Example output:

DS18x20 1-Wire demo by Martin Thomas
------------------------------------

Scanning bus for DS18x20
8 DS18x20 sensor(s) available:
#1 - FC: 28 (18B) SN: 97 44 D5 04 00 00 CRC: C2 (OK)
#1 is a DS18B20 which is externally powered
#2 - FC: 28 (18B) SN: 07 28 D6 04 00 00 CRC: CB (OK)
#2 is a DS18B20 which is externally powered
#3 - FC: 28 (18B) SN: AB 75 D6 04 00 00 CRC: 93 (OK)
#3 is a DS18B20 which is externally powered
#4 - FC: 28 (18B) SN: 09 72 D6 04 00 00 CRC: F0 (OK)
#4 is a DS18B20 which is externally powered
#5 - FC: 28 (18B) SN: B1 57 D5 04 00 00 CRC: F4 (OK)
#5 is a DS18B20 which is externally powered
#6 - FC: 28 (18B) SN: A1 56 D5 04 00 00 CRC: 62 (OK)
#6 is a DS18B20 which is externally powered
#7 - FC: 28 (18B) SN: 01 EF DD 03 00 00 CRC: E4 (OK)
#7 is a DS18B20 which is parasite powered
#8 - FC: 28 (18B) SN: DE F2 D5 04 00 00 CRC: F2 (OK)
#8 is a DS18B20 which is externally powered

Convert_T and Read sensor by sensor (reverse order):
Sensor #8 = 221 deci°C, +22.1 °C
Sensor #7 = 205 deci°C, +20.5 °C
Sensor #6 = 225 deci°C, +22.5 °C
Sensor #5 = 221 deci°C, +22.1 °C
Sensor #4 = 223 deci°C, +22.3 °C
Sensor #3 = 219 deci°C, +21.9 °C
Sensor #2 = 222 deci°C, +22.2 °C
Sensor #1 = 219 deci°C, +21.9 °C

Convert_T for all sensors and Read sensor by sensor:
Sensor #1 = 219 deci°C, +21.9 °C
Sensor #2 = 222 deci°C, +22.2 °C
Sensor #3 = 220 deci°C, +22.0 °C
Sensor #4 = 223 deci°C, +22.3 °C
Sensor #5 = 221 deci°C, +22.1 °C
Sensor #6 = 226 deci°C, +22.6 °C
Sensor #7 = 206 deci°C, +20.6 °C
Sensor #8 = 221 deci°C, +22.1 °C

Maximum resolution data:
Sensor #1 = 218750 °Ce-4, +21.8750 °C
Sensor #2 = 221875 °Ce-4, +22.1875 °C
Sensor #3 = 220000 °Ce-4, +22.0000 °C
Sensor #4 = 222500 °Ce-4, +22.2500 °C
Sensor #5 = 221250 °Ce-4, +22.1250 °C
Sensor #6 = 225625 °Ce-4, +22.5625 °C
Sensor #7 = 205625 °Ce-4, +20.5625 °C
Sensor #8 = 220625 °Ce-4, +22.0625 °C

Verbose output:
FC: 28 (18B) SN: 97 44 D5 04 00 00 CRC: C2 (OK)
SP: 5E 01 4B 46 7F FF 02 10 CRC: 8D (OK)
T_raw = 0x015E (B20/12) +21.8750°C conv: 219 deci°C fmt: +21.9 °C

FC: 28 (18B) SN: 07 28 D6 04 00 00 CRC: CB (OK)
SP: 64 01 4B 46 7F FF 0C 10 CRC: 01 (OK)
T_raw = 0x0164 (B20/12) +22.2500°C conv: 223 deci°C fmt: +22.3 °C

FC: 28 (18B) SN: AB 75 D6 04 00 00 CRC: 93 (OK)
SP: 60 01 4B 46 7F FF 10 10 CRC: B5 (OK)
T_raw = 0x0160 (B20/12) +22.0000°C conv: 220 deci°C fmt: +22.0 °C

FC: 28 (18B) SN: 09 72 D6 04 00 00 CRC: F0 (OK)
SP: 64 01 4B 46 7F FF 0C 10 CRC: 01 (OK)
T_raw = 0x0164 (B20/12) +22.2500°C conv: 223 deci°C fmt: +22.3 °C

FC: 28 (18B) SN: B1 57 D5 04 00 00 CRC: F4 (OK)
SP: 62 01 4B 46 7F FF 0E 10 CRC: 03 (OK)
T_raw = 0x0162 (B20/12) +22.1250°C conv: 221 deci°C fmt: +22.1 °C

FC: 28 (18B) SN: A1 56 D5 04 00 00 CRC: 62 (OK)
SP: 68 01 4B 46 7F FF 08 10 CRC: 05 (OK)
T_raw = 0x0168 (B20/12) +22.5000°C conv: 225 deci°C fmt: +22.5 °C

FC: 28 (18B) SN: 01 EF DD 03 00 00 CRC: E4 (OK)
SP: 49 01 4B 46 7F FF 07 10 CRC: F6 (OK)
T_raw = 0x0149 (B20/12) +20.5625°C conv: 206 deci°C fmt: +20.6 °C

FC: 28 (18B) SN: DE F2 D5 04 00 00 CRC: F2 (OK)
SP: 61 01 4B 46 7F FF 0F 10 CRC: 02 (OK)
T_raw = 0x0161 (B20/12) +22.0625°C conv: 221 deci°C fmt: +22.1 °C


Happy hacking,
Paul Fertser
63 changes: 63 additions & 0 deletions examples/stm32/f1/other/ds18x20/crc8.c
@@ -0,0 +1,63 @@
/* please read copyright-notice at EOF */

#include <stdint.h>

#define CRC8INIT 0x00
#define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0

uint8_t crc8( uint8_t *data, uint16_t number_of_bytes_in_data )
{
uint8_t crc;
uint16_t loop_count;
uint8_t bit_counter;
uint8_t b;
uint8_t feedback_bit;

crc = CRC8INIT;

for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++)
{
b = data[loop_count];

bit_counter = 8;
do {
feedback_bit = (crc ^ b) & 0x01;

if ( feedback_bit == 0x01 ) {
crc = crc ^ CRC8POLY;
}
crc = (crc >> 1) & 0x7F;
if ( feedback_bit == 0x01 ) {
crc = crc | 0x80;
}

b = b >> 1;
bit_counter--;

} while (bit_counter > 0);
}

return crc;
}

/*
This code is from Colin O'Flynn - Copyright (c) 2002
only minor changes by M.Thomas 9/2004

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
39 changes: 39 additions & 0 deletions examples/stm32/f1/other/ds18x20/crc8.h
@@ -0,0 +1,39 @@
#ifndef CRC8_H_
#define CRC8_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

uint8_t crc8( uint8_t* data, uint16_t number_of_bytes_in_data );

#ifdef __cplusplus
}
#endif

#endif

/*
This is based on code from :

Copyright (c) 2002 Colin O'Flynn

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
9 changes: 9 additions & 0 deletions examples/stm32/f1/other/ds18x20/delay.h
@@ -0,0 +1,9 @@
#ifndef _DELAY_H
#define _DELAY_H

#include <stdlib.h>

void delay_us(size_t n);
#define delay_ms(x) delay_us((x)*1000)

#endif /* _DELAY_H */