An experimental timer interrupt driven software based 1-Wire master interface that was created by Robert David Brinzer for a University of Glasgow Level 4 Electronic and Software Engineering Final Year project. This implementation requires mbed RTOS and does not use NOP waits. Perfect for programs that require multi-tasking. Includes the Read and Search network commands and can be easily extended to support new network and transport commands.

Committer:
sroy
Date:
Mon Apr 15 07:02:49 2013 +0000
Revision:
0:7c6dd6bc20e4
An experimental 1-Wire Master interface driven by timer interrupts rather than NOP waits created as part of a University of Glasgow Level 4 Electronic and Software Engineering final year project.   Requires the use of mbed RTOS.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sroy 0:7c6dd6bc20e4 1 #ifndef ONE_WIRE
sroy 0:7c6dd6bc20e4 2 #define ONE_WIRE
sroy 0:7c6dd6bc20e4 3
sroy 0:7c6dd6bc20e4 4 #include "mbed.h"
sroy 0:7c6dd6bc20e4 5 #include "rtos.h"
sroy 0:7c6dd6bc20e4 6
sroy 0:7c6dd6bc20e4 7 class OneWire;
sroy 0:7c6dd6bc20e4 8
sroy 0:7c6dd6bc20e4 9 typedef void (* OneWire_Instruction_Func)(OneWire *);
sroy 0:7c6dd6bc20e4 10
sroy 0:7c6dd6bc20e4 11 typedef struct OneWire_InstCall{
sroy 0:7c6dd6bc20e4 12 unsigned char code;
sroy 0:7c6dd6bc20e4 13 OneWire_Instruction_Func inst;
sroy 0:7c6dd6bc20e4 14 void * args;
sroy 0:7c6dd6bc20e4 15 } OneWire_InstCall;
sroy 0:7c6dd6bc20e4 16
sroy 0:7c6dd6bc20e4 17 typedef struct OneWire_Instruction{
sroy 0:7c6dd6bc20e4 18 OneWire_InstCall network;
sroy 0:7c6dd6bc20e4 19 OneWire_InstCall transport;
sroy 0:7c6dd6bc20e4 20 } OneWire_Instruction;
sroy 0:7c6dd6bc20e4 21
sroy 0:7c6dd6bc20e4 22 typedef void (* OneWire_ReadHandler)(OneWire * which, char bit);
sroy 0:7c6dd6bc20e4 23
sroy 0:7c6dd6bc20e4 24 typedef enum OneWire_Error {SUCCESS,
sroy 0:7c6dd6bc20e4 25 NO_PRESENCE,
sroy 0:7c6dd6bc20e4 26 ABORTED,
sroy 0:7c6dd6bc20e4 27 CRC_ERROR} OneWire_Error;
sroy 0:7c6dd6bc20e4 28
sroy 0:7c6dd6bc20e4 29 #define ONEWIRE_TIMESLOT 60 // 60 to 120
sroy 0:7c6dd6bc20e4 30 #define ONEWIRE_RECOVER 1 // 1 to whatever
sroy 0:7c6dd6bc20e4 31 #define ONEWIRE_PULSEHIGH 30 // 15 to 30
sroy 0:7c6dd6bc20e4 32 #define ONEWIRE_PULSELOW 120 // 60 to 240
sroy 0:7c6dd6bc20e4 33 #define ONEWIRE_WRITE1LOW 1 // 1 to 15
sroy 0:7c6dd6bc20e4 34 #define ONEWIRE_WRITE0LOW 59 // 1 to ONEWIRE_TIMESLOT-1
sroy 0:7c6dd6bc20e4 35 #define ONEWIRE_READLOW 1 // 1 to 14
sroy 0:7c6dd6bc20e4 36 #define ONEWIRE_READDURATION 15 // 15
sroy 0:7c6dd6bc20e4 37 #define ONEWIRE_READRELEASE 15 // 0 to 44
sroy 0:7c6dd6bc20e4 38
sroy 0:7c6dd6bc20e4 39 extern const unsigned short DEFAULT_TIMES[];
sroy 0:7c6dd6bc20e4 40
sroy 0:7c6dd6bc20e4 41 typedef void(OneWire::*OneWire_MicroInstruction)(void);
sroy 0:7c6dd6bc20e4 42
sroy 0:7c6dd6bc20e4 43 // type for detection handler
sroy 0:7c6dd6bc20e4 44 typedef void (* OneWire_Detection_Handler)(OneWire * which);
sroy 0:7c6dd6bc20e4 45
sroy 0:7c6dd6bc20e4 46 class OneWire {
sroy 0:7c6dd6bc20e4 47 public:
sroy 0:7c6dd6bc20e4 48 OneWire(PinName pin);
sroy 0:7c6dd6bc20e4 49 int send(OneWire_Instruction *, unsigned char);
sroy 0:7c6dd6bc20e4 50 void endInstruction();
sroy 0:7c6dd6bc20e4 51 void repeatInstruction();
sroy 0:7c6dd6bc20e4 52
sroy 0:7c6dd6bc20e4 53 // stops all communication, resuming blocked thread with error.
sroy 0:7c6dd6bc20e4 54 void abort(OneWire_Error err);
sroy 0:7c6dd6bc20e4 55
sroy 0:7c6dd6bc20e4 56 // one wire wakeup function
sroy 0:7c6dd6bc20e4 57
sroy 0:7c6dd6bc20e4 58 // public fields
sroy 0:7c6dd6bc20e4 59 OneWire_Error error;
sroy 0:7c6dd6bc20e4 60 char registers[16];
sroy 0:7c6dd6bc20e4 61
sroy 0:7c6dd6bc20e4 62 // configuration
sroy 0:7c6dd6bc20e4 63 OneWire_Detection_Handler detecthandle;
sroy 0:7c6dd6bc20e4 64 const unsigned short * timeing;
sroy 0:7c6dd6bc20e4 65 OneWire_ReadHandler readhandle;
sroy 0:7c6dd6bc20e4 66 OneWire_InstCall execute;
sroy 0:7c6dd6bc20e4 67
sroy 0:7c6dd6bc20e4 68 // high level IO ops
sroy 0:7c6dd6bc20e4 69 void op_send1();
sroy 0:7c6dd6bc20e4 70 void op_send0();
sroy 0:7c6dd6bc20e4 71 void op_read();
sroy 0:7c6dd6bc20e4 72 void op_reset();
sroy 0:7c6dd6bc20e4 73
sroy 0:7c6dd6bc20e4 74 private:
sroy 0:7c6dd6bc20e4 75 // system resources
sroy 0:7c6dd6bc20e4 76 DigitalInOut _pin;
sroy 0:7c6dd6bc20e4 77 InterruptIn _detect;
sroy 0:7c6dd6bc20e4 78 Timeout _timer;
sroy 0:7c6dd6bc20e4 79 osThreadId _caller;
sroy 0:7c6dd6bc20e4 80
sroy 0:7c6dd6bc20e4 81 // instruction system
sroy 0:7c6dd6bc20e4 82 OneWire_Instruction * _instruction;
sroy 0:7c6dd6bc20e4 83 unsigned char _instn;
sroy 0:7c6dd6bc20e4 84 unsigned char _inststate;
sroy 0:7c6dd6bc20e4 85 static const OneWire_MicroInstruction _OneWire_MicroProgram[];
sroy 0:7c6dd6bc20e4 86 unsigned char _microinstc;
sroy 0:7c6dd6bc20e4 87 unsigned char _microinstn;
sroy 0:7c6dd6bc20e4 88
sroy 0:7c6dd6bc20e4 89 void _nextmicroinst();
sroy 0:7c6dd6bc20e4 90 void _resumeinst();
sroy 0:7c6dd6bc20e4 91 void _nextinst();
sroy 0:7c6dd6bc20e4 92
sroy 0:7c6dd6bc20e4 93 // reset presence detect handler
sroy 0:7c6dd6bc20e4 94 static void _presencedetect(OneWire * which, char bit);
sroy 0:7c6dd6bc20e4 95
sroy 0:7c6dd6bc20e4 96 // external interrupt handler
sroy 0:7c6dd6bc20e4 97 void _ei_detect();
sroy 0:7c6dd6bc20e4 98
sroy 0:7c6dd6bc20e4 99 // low level IO ops
sroy 0:7c6dd6bc20e4 100 void _io_high();
sroy 0:7c6dd6bc20e4 101 void _io_low();
sroy 0:7c6dd6bc20e4 102 void _io_read();
sroy 0:7c6dd6bc20e4 103 };
sroy 0:7c6dd6bc20e4 104
sroy 0:7c6dd6bc20e4 105 #endif