-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoneWire.h
120 lines (91 loc) · 3.42 KB
/
oneWire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef __ONE_WIRE_H__
#define __ONE_WIRE_H__
#include "ch.h"
#include "hal.h"
/*
This is a library for OneWire Master bus http://en.wikipedia.org/wiki/1-Wire
based on advices found on dallas documentation :
http://www.maximintegrated.com/app-notes/index.mvp/id/214
this library use an usart to avoid to consume cpu cycle in bit banging
the library is synchronous, but when waiting it yield the processor to chibios.
Alexandre Bustico
TODO : envoyer les 0xff depuis la flash pour economiser de la stack
port discovery
temperature en asynchrone sur plusieurs capteurs
*/
struct OneWireDriver;
typedef struct OneWireDriver OneWireDriver;
typedef struct {
uint8_t addr[8];
} OneWireRomAddress;
typedef struct {
ioportid_t dqPort; // the port for the data IO
uint8_t dqPad; // pad for the data IO, shloud be the tx of an usart
uint8_t dqAlternate; // alternate function, see stm32 ref manuel to find it
UARTDriver *uartd; // Chibios Uart Driver used as oneWire interface
} OneWireConfig;
/*
create OneWireDriver object, initialise UART
*/
void oneWireInit (OneWireDriver* drv, const OneWireConfig *cfg);
/*
send a reset condition on the OneWire bus to reinitialise all
slave on this bus
*/
bool_t oneWireReset (OneWireDriver* drv);
/*
basic io : send/receive bit, byte, buffers
since every OneWire Bit involve uart byte,
oneWireSend need to reserve len*8 bytes on the stack
oneWireReceive need to reserve len*8*2 bytes on the stack
So Stack size should be calculated according to this if you send receive
big bunch of bytes.
*/
bool_t oneWireReceiveBit (OneWireDriver* drv);
void oneWireSendBit (OneWireDriver* drv, bool_t bit);
void oneWireSendByte (OneWireDriver* drv, uint8_t command);
uint8_t oneWireReceiveByte (OneWireDriver* drv);
bool_t oneWireSend (OneWireDriver* drv, const uint8_t *command, uint8_t len);
bool_t oneWireReceive (OneWireDriver* drv, uint8_t *buffer, uint8_t len);
/*
get rom address of the slave when there is just ONE slave connected
otherwise (multiple slaves connected), use oneWireSearchRom
*/
bool_t oneWireReadRom (OneWireDriver* drv, OneWireRomAddress *addr);
/*
get all rom address when multiple slave are connected
depending on conditional value, get All the
slave or just the slave which are in alarm state (cf conditional search)
it's your responsability to give a pointer on an sufficently large
array for all the slave, otherwise you will miss some slave.
*/
uint8_t oneWireSearchRom (OneWireDriver* drv, bool_t conditional,
OneWireRomAddress *addrs, uint8_t len);
/*
crc helper function implementing crc8 used bu oneWire slave, to ensure that
the data received are valid
*/
//uint8_t oneWireCrc8( uint8_t *addr, uint8_t len);
uint8_t owCrc8( uint8_t *addr, uint8_t len);
/*
If more than one thread would communicate with different slaves,
call to oneWire function should be pritected by these functions
*/
void oneWireAcquireBus (OneWireDriver* drv);
void oneWireRealeaseBus (void);
/*
This is private data, for internal library state only
*/
// Private Data Daclaration
typedef enum {ModeGpioIn, ModeSerialReset, ModeSerialTransfert} SerialMode;
struct OneWireDriver {
OneWireConfig config;
SerialMode currentSm;
Mutex acquireLock;
BinarySemaphore semSent;
BinarySemaphore semReceive;
uint8_t lastDiscrep ;
bool_t doneFlag ;
};
#endif //__ONE_WIRE_H__