Changeset 137
- Timestamp:
- 02/08/08 22:38:57 (11 months ago)
- Files:
-
- 1 modified
-
HDQ/hdq.cpp (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
HDQ/hdq.cpp
r136 r137 1 1 extern "C" { 2 #include <avr/pgmspace.h>3 #include <avr/interrupt.h>4 #include <stdio.h>5 #include <string.h>6 #include <inttypes.h>7 #include <stdlib.h>8 9 #include "wiring_private.h"10 #include "wiring.h"11 2 #include "WConstants.h" 12 #include <HardwareSerial.h>13 14 3 #include "pins_arduino.h" 15 4 } … … 20 9 #define _HDQ_readPin() (*inputReg & bitmask)>>pin /* Change me to inline!*/ 21 10 11 /** 12 * Constructor 13 * @param pin: pin number to attach to 14 **/ 22 15 HDQ::HDQ(uint8_t pinArg) 23 16 { … … 30 23 } 31 24 25 /** 26 * sendBreak: writes a break to the HDQ line 27 **/ 32 28 void HDQ::doBreak(void) 33 29 { 34 sbi(*modeReg, pin); // Set pin as output 30 sbi(*modeReg, pin); // Set pin as output 31 32 // Singal a break on the line 35 33 cbi(*outputReg, pin); // Bring pin low 36 37 // Wait the minimum break time38 34 delayMicroseconds(HDQ_DELAY_TB); 39 35 40 sbi(*outputReg, pin); // Bring the pin high again 41 42 // Wait the minimum break time 36 // Make sure we leave enough time for the slave to recover 37 cbi(*modeReg, pin); // Release pin 43 38 delayMicroseconds(HDQ_DELAY_TBR); 44 39 } 45 40 41 /** 42 * writeByte: write a raw byte of data to the bus 43 * @param payload: the byte to send 44 * 45 **/ 46 46 void HDQ::writeByte(uint8_t payload) 47 47 { 48 sbi(*modeReg, pin); // Set pin as output 48 49 49 50 for (uint8_t ii = 0; ii < 8; ii++) 50 51 { 51 sbi(*modeReg, pin); // Set pin as output 52 cbi(*outputReg, pin); // Bring pin low 53 // Wait the minimum break time 52 // Start bit 53 cbi(*outputReg, pin); // Bring pin low 54 54 delayMicroseconds(HDQ_DELAY_BIT_START); 55 55 56 56 // Toggle the pin for this bit, LSB first 57 57 if (payload>>ii & 0x01) { 58 sbi(*outputReg, pin); 58 sbi(*outputReg, pin); // High 59 59 } 60 60 else { 61 cbi(*outputReg, pin); 61 cbi(*outputReg, pin); // Low 62 62 } 63 63 64 // Bit time 64 65 delayMicroseconds(HDQ_DELAY_BIT_WRITE); 65 66 sbi(*outputReg, pin);67 66 67 // Stop bit 68 sbi(*outputReg, pin); // Bring the pin high 68 69 delayMicroseconds(HDQ_DELAY_BIT_END); 69 70 } … … 72 73 } 73 74 75 /** 76 * write: send a payload to the device 77 * @param reg: the address of the register to write to 78 * @param payload: data to be sent 79 * @return: false, unless if verif is set, then 80 * it will read back the register and 81 * return true if it matches the payload 82 **/ 83 boolean HDQ::write(uint8_t reg, uint8_t payload) 84 { 85 // Singal a break 86 HDQ::doBreak(); 87 88 // Write the register to write 89 HDQ::writeByte((reg |= HDQ_ADDR_MASK_WRITE)); 90 91 // Wait for the slave to finish reading the register 92 delayMicroseconds((HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_BIT_TOTAL) / 2); 93 94 // Write the payload 95 HDQ::writeByte(payload); 96 97 // Wait for the slave to finish writing the payload 98 delayMicroseconds((HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_BIT_TOTAL) / 2); 99 100 cbi(*modeReg, pin); // Release pin 101 102 return true; 103 } 104 105 /** 106 * Write with verification 107 **/ 74 108 boolean HDQ::write(uint8_t reg, uint8_t payload, boolean verif) 75 109 { 110 // Write the payload 76 111 HDQ::write(reg, payload); 77 112 113 // Verify the write 78 114 if (payload == HDQ::read(reg)) return true; 79 115 80 116 return false; 81 117 } 82 118 83 boolean HDQ::write(uint8_t reg, uint8_t payload)84 {85 HDQ::doBreak();86 87 HDQ::writeByte((reg |= HDQ_ADDR_MASK_WRITE));88 89 delayMicroseconds((HDQ_DELAY_TRSPS_MAX - HDQ_DELAY_BIT_TOTAL) / 2);90 91 HDQ::writeByte(payload);92 93 return false;94 }95 119 120 /** 121 * read: read from the device 122 * @param register: address of the register to read 123 * @return a uint8_t integer 124 **/ 96 125 uint8_t HDQ::read(uint8_t reg) 97 126 { … … 100 129 uint8_t ii; 101 130 131 // Singal a break 102 132 HDQ::doBreak(); 103 133 134 // Write the register to read 104 135 HDQ::writeByte((reg |= HDQ_ADDR_MASK_READ)); 105 136 106 137 for (ii = 0; ii < 8; ii++) 107 138 { 108 // Wait for the slave to toggle a low 139 // Wait for the slave to toggle a low, or fail 109 140 maxTries = HDQ_DELAY_FAIL_TRIES; 110 141 while (_HDQ_readPin() != 0 && maxTries-- > 0) 111 142 if (maxTries == 1) return 0xFF; 112 143 113 delayMicroseconds(HDQ_DELAY_BIT_WRITE / 2 + HDQ_DELAY_BIT_START); // 100 / 2 + 30 144 // Wait until Tdsub and half or one bit has passed 145 delayMicroseconds(HDQ_DELAY_BIT_WRITE / 2 + HDQ_DELAY_BIT_START); 114 146 147 // Read the bit 115 148 result |= _HDQ_readPin()<<ii; 116 149 117 delayMicroseconds(HDQ_DELAY_TSSUB - (HDQ_DELAY_BIT_WRITE / 2 + HDQ_DELAY_BIT_START) + 10); // pass Tssub 150 // Wait until Tssub has passed 151 delayMicroseconds(HDQ_DELAY_TSSUB - (HDQ_DELAY_BIT_WRITE / 2 + HDQ_DELAY_BIT_START) + 10); 118 152 119 // Wait for the slave to toggle a high again153 // Wait for the slave to toggle a high, or fail 120 154 maxTries = HDQ_DELAY_FAIL_TRIES; 121 155 while (_HDQ_readPin() != 1 && maxTries-- > 0) 122 156 if (maxTries == 1) return 0xFF; 123 124 157 } 125 158