| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- /*
- * Copyright (C) 2012 Uwe Bonnes. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the copyright holders nor the names of
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
- * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
- * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENce OR OTHERWISE) ARISING IN ANY WAY OUT OF
- * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * For additional information see http://www.ethernut.de/
- */
- /*!
- * \file app/owibus/owibus.c
- * \brief Example access to a DS18B20.
- *
- * The Onewire device needs to be connected to a port pin with pull-up
- * or the uart with TX low driving RX low and TX high not touching RX
- *
- * STM32 UART can do that by itself, with RX connected internal to TX
- * and TX driven Tristate
- *
- * \verbatim
- */
- #include <string.h>
- #include <stdio.h>
- #include <io.h>
- #include <cfg/arch.h>
- #include <stdint.h>
- #include <sys/timer.h>
- #include <dev/board.h>
- #include <dev/gpio.h>
- #if !defined(__GNUC__) || !defined(DEF_OWIBUS)
- int main(void)
- {
- uint32_t baud = 115200;
- NutRegisterDevice(&DEV_CONSOLE, 0, 0);
- freopen(DEV_CONSOLE.dev_name, "w", stdout);
- _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
- #if !defined(__GNUC__)
- puts("This program requires a compiler that supports 64-bit integers.");
- #else
- puts("This program requires a configured OWIBUS.");
- #endif
- for (;;);
- return 0;
- }
- #else
- #include <dev/owibus.h>
- static char *banner = "\nNut/OS OWI Bus on " BOARDNAME
- " "__DATE__ " " __TIME__"\n";
- /*
- * UART sample.
- *
- */
- int main(void)
- {
- uint32_t baud = 115200;
- FILE *uart;
- int res, i = 0;
- uint64_t hid = 0;
- int32_t xcelsius;
- int run =0;
- uint8_t raw[2];
- uint8_t diff;
- uint8_t family;
- NutRegisterDevice(&DEV_CONSOLE, 0, 0);
- uart = fopen(DEV_CONSOLE.dev_name, "r+");
- _ioctl(_fileno(uart), UART_SETSPEED, &baud);
- freopen(DEV_CONSOLE.dev_name, "w", stdout);
- fprintf(stdout, banner);
- res = OwiInit(&DEF_OWIBUS);
- if(res)
- {
- printf("Owi Error %d\n", res);
- while(1)
- NutSleep(10);
- }
- diff = OWI_SEARCH_FIRST;
- res = OwiRomSearch(&DEF_OWIBUS, &diff, &hid);
- if(res)
- {
- printf("OwiRomSearch failed\n");
- while(1)
- NutSleep(10);
- }
- fprintf(stdout, "Hardware ID of first device %08lx%08lx\n",
- (uint32_t)(hid>>32),
- (uint32_t)(hid &0xffffffff));
- family = hid & 0xff;
- if ((family != W1_THERM_DS18B20) && (family != W1_THERM_DS18S20))
- {
- fprintf(stdout, "One-wire device found, but family not handled"
- "in this example\n");
- while(1) NutSleep(10);
- }
- while(1)
- {
- res = OwiCommand(&DEF_OWIBUS, OWI_CONVERT_T, NULL);
- if (res)
- printf("OwiCommand convert_t error %d\n", res);
- NutSleep(750);
- while (!(res = OwiReadBlock(&DEF_OWIBUS, &diff, 1)) && !diff && i < 100)
- {
- NutSleep(10);
- i++;
- }
- if (i)
- {
- printf(
- "Conversion took additional %d poll cycles\n",
- i);
- i = 0;
- }
- res = OwiCommand(&DEF_OWIBUS, OWI_READ, NULL);
- if (res)
- printf("OwiCommand read error %d\n", res);
- res = OwiReadBlock(&DEF_OWIBUS, raw, 16);
- if (res)
- printf("OwiReadBlock error %d\n", res);
- if (family == W1_THERM_DS18B20 )
- xcelsius = ((raw[1]<<8) | raw[0]) * (int32_t)625;
- else
- xcelsius = ((raw[1]<< 8) | raw[0]) * (int32_t)5000;
- fprintf(stdout, "Run %3d: Temp %ld.%04ld\r",
- run++, xcelsius/10000, xcelsius%10000);
- }
- return 0;
- }
- #endif
|