Scott Roy / OneWire
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers OneWire.h Source File

OneWire.h

00001 #ifndef ONE_WIRE
00002 #define ONE_WIRE
00003 
00004 #include "mbed.h"
00005 #include "rtos.h"
00006 
00007 class OneWire;
00008 
00009 typedef void (* OneWire_Instruction_Func)(OneWire *);
00010 
00011 typedef struct OneWire_InstCall{
00012     unsigned char code;
00013     OneWire_Instruction_Func inst;
00014     void * args;
00015 } OneWire_InstCall;
00016 
00017 typedef struct OneWire_Instruction{
00018     OneWire_InstCall network;
00019     OneWire_InstCall transport;
00020 } OneWire_Instruction;
00021 
00022 typedef void (* OneWire_ReadHandler)(OneWire * which, char bit);
00023 
00024 typedef enum OneWire_Error {SUCCESS,
00025                             NO_PRESENCE,
00026                              ABORTED,
00027                              CRC_ERROR} OneWire_Error;
00028 
00029 #define ONEWIRE_TIMESLOT 60 // 60 to 120
00030 #define ONEWIRE_RECOVER 1 // 1 to whatever
00031 #define ONEWIRE_PULSEHIGH 30 // 15 to 30
00032 #define ONEWIRE_PULSELOW 120 // 60 to 240
00033 #define ONEWIRE_WRITE1LOW 1 // 1 to 15
00034 #define ONEWIRE_WRITE0LOW 59 // 1 to ONEWIRE_TIMESLOT-1
00035 #define ONEWIRE_READLOW 1 // 1 to 14
00036 #define ONEWIRE_READDURATION 15 // 15
00037 #define ONEWIRE_READRELEASE 15 // 0 to 44
00038 
00039 extern const unsigned short DEFAULT_TIMES[];
00040 
00041 typedef void(OneWire::*OneWire_MicroInstruction)(void);
00042 
00043 // type for detection handler
00044 typedef void (* OneWire_Detection_Handler)(OneWire * which);
00045 
00046 class OneWire {
00047 public:
00048     OneWire(PinName pin);
00049     int send(OneWire_Instruction *, unsigned char);
00050     void endInstruction();
00051     void repeatInstruction();
00052     
00053     // stops all communication, resuming blocked thread with error.
00054     void abort(OneWire_Error err);
00055     
00056     // one wire wakeup function
00057     
00058     // public fields
00059     OneWire_Error error;
00060     char registers[16];
00061     
00062     // configuration
00063     OneWire_Detection_Handler detecthandle;
00064     const unsigned short * timeing;
00065     OneWire_ReadHandler readhandle;
00066     OneWire_InstCall execute;
00067     
00068     // high level IO ops
00069     void op_send1();
00070     void op_send0();
00071     void op_read();
00072     void op_reset();
00073     
00074 private:
00075     // system resources
00076     DigitalInOut _pin;
00077     InterruptIn _detect;
00078     Timeout _timer;
00079     osThreadId _caller;
00080     
00081     // instruction system
00082     OneWire_Instruction * _instruction;
00083     unsigned char _instn;
00084     unsigned char _inststate;
00085     static const OneWire_MicroInstruction _OneWire_MicroProgram[];
00086     unsigned char _microinstc;
00087     unsigned char _microinstn;
00088     
00089     void _nextmicroinst();
00090     void _resumeinst();
00091     void _nextinst();
00092     
00093     // reset presence detect handler
00094     static void _presencedetect(OneWire * which, char bit);
00095     
00096     // external interrupt handler
00097     void _ei_detect();
00098     
00099     // low level IO ops
00100     void _io_high();
00101     void _io_low();
00102     void _io_read();
00103 };
00104 
00105 #endif