libpruw1  0.4
One wire (W1) driver for Beaglebone hardware based on libpruio
dallas.c
Go to the documentation of this file.
1 
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "libpruio/pruio.h"
23 #include "libpruio/pruio_pins.h" // libruio pin numbering
24 #include "../c_include/pruw1.h" // library header (local)
25 //#include "libpruw1/pruw1.h" // library header when installed
26 
28 int main(int argc, char **argv)
29 {
30  int i, n;
31  UInt8 crc, typ, *res;
32  float val;
33  char *txt;
34  UInt64 id;
35  // Create new libpruio instance.
36  pruIo *io = pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0);
37 
38  do {
39  if (io->Errr) {
40  printf("libpruio CTOR failed (%s)\n", io->Errr); break;}
41  // uncomment the following lines in order to use function PruW1::getIn()
42  //if (io->config()) {
43  //printf("libpruio config failed (%s)\n", io->Errr); break;}
44 
45  // Create new libpruw1 instance.
46  pruw1 *w1 = pruw1_new(io, P9_15, PRUW1_PARPOW + PRUW1_PULLUP); // 2 wire (VDD=GND))
47  //pruw1 *w1 = pruw1_new(io, P9_15, PRUW1_PULLUP); // external VDD and pullup
48  if (w1->Errr) {
49  printf("libpruw1 CTOR failed (%s)\n", w1->Errr); break;}
50  // Scan the bus for parasite powered devices
51  printf("%s parasite powered device.",
52  (pruw1_checkPara(w1) ? "At least one" : "No"));
53  // Scan the bus for device IDs
54  printf("\ntrying to scan devices ...");
55  if (pruw1_scanBus(w1)) {
56  printf("scanBus failed (%s)\n", w1->Errr); break;}
57  for (i = 0; i <= pruw1_getSlotMax(w1); i++)
58  {
59  printf("\nfound device %d, ID: %llX", i, pruw1_getId(w1, i));
60  }
61  res = (UInt8 *) w1->DRam + 16;
62  printf("\n");
63  // Perform some measurements
64  for (n = 0; n <= 10; n++)
65  {
66  // Start measurement, send the presense pulse (0 = OK).
67  if (pruw1_resetBus(w1)) { printf("no devices"); break;}
68  pruw1_sendByte(w1, 0xCC); // SKIP_ROM command -> broadcast message
69  pruw1_sendByte(w1, 0x44); //convert T command -> all sensors triggered
70  usleep(750000);
71 
72  // Fetch the data from sensor scratch pads
73  for (i = 0; i <= pruw1_getSlotMax(w1); i++) {
74  id = pruw1_getId(w1, i);
75 
76  typ = id & 0xFF;
77  if (typ != 0x10 && typ != 0x20 && typ != 0x22 && typ != 0x28 && typ != 0x3B && typ != 0x42) continue;
78  //printf("\nTyp: %X", crc);
79 
80  // Start reading, send the presense pulse (0 = OK).
81  if (pruw1_resetBus(w1)) { printf("no devices"); break;}
82  pruw1_sendByte(w1, 0x55); // ROM_MATCH command -> adress single sensor
83  pruw1_sendRom(w1, id); // send sensor ID -> select sensor
84  pruw1_sendByte(w1, 0xBE); //READ_SCRATCH command -> sensor sends scratchpad
85  pruw1_recvBlock(w1, 9); // read data block (64 bit scratchpad and CRC byte)
86 
87  // Output result
88  crc = pruw1_calcCrc(w1, 9);
89  if (crc) {
90  txt = "error";
91  val = -99;
92  } else {
93  txt = "OK:";
94  val = (float)(typ == 0x10 ? T_FAM10(res) : T_FAM20(res)) / 256;
95  }
96  printf("\nsensor %llX --> %s %3.4f °C", id, txt, val);
97  }
98  printf("\n");
99  }
100  pruw1_destroy(w1);
101  } while (0);
102 
103 /* we're done */
104 
105  pruio_destroy(io); /* destroy driver structure */
106  return 0;
107 }
VAR res
pointer to measurement data
Definition: dallas.bas:51
VAR io
Pointer to libpruio instance.
Definition: dallas.bas:25
VAR crc
The checksum (0 = OK).
Definition: dallas.bas:74
VAR w1
Create new libpruw1 instance.
Definition: dallas.bas:34
int main(int argc, char **argv)
The main function.
Definition: dallas.c:28
@ PRUW1_PARPOW
Set line output high during idle.
Definition: pruw1.bi:29
@ PRUW1_PULLUP
Activate internal pull up resistor (default)
Definition: pruw1.bi:30
Int16 T_FAM10(UInt8 *Rom)
Compute the temperature for a series 10 sensor (old format).
unsigned char UInt8
8 bit unsigned integer data type.
Definition: pruw1.h:22
Int16 T_FAM20(UInt8 *Rom)
Compute the temperature for a series 20 sensor (new format).
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_recvBlock(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 N)
Wrapper function for PruW1::recvBlock().
SUB_CDECL_ALIAS pruw1_sendRom(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt64 V)
Wrapper function for PruW1::sendRom().
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_checkPara(BYVAL_AS_PruW1_PTR W1)
Wrapper function for PruW1::checkPara().
FUNCTION_CDECL_ALIAS_AS_UInt64 pruw1_getId(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt32 N)
Function to get ID from the array PruW1::Slots from C.
SUB_CDECL_ALIAS pruw1_destroy(BYVAL_AS_PruW1_PTR W1)
Wrapper function for destructor PruW1::~PruW1.
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_resetBus(BYVAL_AS_PruW1_PTR W1)
Wrapper function for PruW1::resetBus().
FUNCTION_CDECL_ALIAS_AS_UInt8 pruw1_calcCrc(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 N)
Wrapper function for PruW1::calcCrc().
SUB_CDECL_ALIAS pruw1_sendByte(BYVAL_AS_PruW1_PTR W1, BYVAL_AS_UInt8 V)
Wrapper function for PruW1::sendByte().
FUNCTION_CDECL_ALIAS_AS_Int32 pruw1_getSlotMax(BYVAL_AS_PruW1_PTR W1)
Property to get size of array PruW1::Slots from C.
FUNCTION_CDECL_ALIAS_AS_PruW1_PTR pruw1_new(BYVAL_AS_PruIo_PTR P, BYVAL_AS_Uint8 B, BYVAL_AS_Uint8 M)
Wrapper function for constructor PruW1::PruW1().
The PruW1 C wrapper structure.
Definition: pruw1.h:85