Changeset 137

Show
Ignore:
Timestamp:
02/08/08 22:38:57 (11 months ago)
Author:
mlalondesvn
Message:

MODIF - More comments

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • HDQ/hdq.cpp

    r136 r137  
    11extern "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" 
    112    #include "WConstants.h" 
    12     #include <HardwareSerial.h> 
    13      
    143    #include "pins_arduino.h" 
    154} 
     
    209#define _HDQ_readPin() (*inputReg & bitmask)>>pin /* Change me to inline!*/ 
    2110 
     11/** 
     12 * Constructor 
     13 * @param pin: pin number to attach to 
     14**/ 
    2215HDQ::HDQ(uint8_t pinArg) 
    2316{ 
     
    3023} 
    3124 
     25/** 
     26 * sendBreak: writes a break to the HDQ line 
     27**/ 
    3228void HDQ::doBreak(void) 
    3329{ 
    34     sbi(*modeReg, pin); // Set pin as output 
     30    sbi(*modeReg, pin);     // Set pin as output 
     31     
     32    // Singal a break on the line 
    3533    cbi(*outputReg, pin);   // Bring pin low 
    36      
    37     // Wait the minimum break time 
    3834    delayMicroseconds(HDQ_DELAY_TB); 
    3935     
    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 
    4338    delayMicroseconds(HDQ_DELAY_TBR); 
    4439} 
    4540 
     41/** 
     42 * writeByte: write a raw byte of data to the bus 
     43 * @param payload: the byte to send 
     44 *  
     45**/ 
    4646void HDQ::writeByte(uint8_t payload) 
    4747{ 
     48    sbi(*modeReg, pin);     // Set pin as output 
    4849     
    4950    for (uint8_t ii = 0; ii < 8; ii++) 
    5051    { 
    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 
    5454        delayMicroseconds(HDQ_DELAY_BIT_START); 
    55      
     55         
    5656        // Toggle the pin for this bit, LSB first 
    5757        if (payload>>ii & 0x01) { 
    58             sbi(*outputReg, pin); 
     58            sbi(*outputReg, pin); // High 
    5959        } 
    6060        else { 
    61             cbi(*outputReg, pin); 
     61            cbi(*outputReg, pin); // Low 
    6262        } 
    63      
     63         
     64        // Bit time 
    6465        delayMicroseconds(HDQ_DELAY_BIT_WRITE); 
    65      
    66         sbi(*outputReg, pin); 
    67  
     66         
     67        // Stop bit 
     68        sbi(*outputReg, pin);   // Bring the pin high 
    6869        delayMicroseconds(HDQ_DELAY_BIT_END); 
    6970    } 
     
    7273} 
    7374 
     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**/ 
     83boolean 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**/ 
    74108boolean HDQ::write(uint8_t reg, uint8_t payload, boolean verif) 
    75109{ 
     110    // Write the payload 
    76111    HDQ::write(reg, payload); 
    77      
     112 
     113    // Verify the write 
    78114    if (payload == HDQ::read(reg)) return true; 
    79      
     115 
    80116    return false; 
    81117} 
    82118 
    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 } 
    95119 
     120/** 
     121 * read: read from the device 
     122 * @param register: address of the register to read 
     123 * @return a uint8_t integer 
     124**/ 
    96125uint8_t HDQ::read(uint8_t reg) 
    97126{ 
     
    100129    uint8_t ii; 
    101130     
     131    // Singal a break 
    102132    HDQ::doBreak(); 
    103133     
     134    // Write the register to read 
    104135    HDQ::writeByte((reg |= HDQ_ADDR_MASK_READ)); 
    105136     
    106137    for (ii = 0; ii < 8; ii++) 
    107138    { 
    108         // Wait for the slave to toggle a low 
     139        // Wait for the slave to toggle a low, or fail 
    109140        maxTries = HDQ_DELAY_FAIL_TRIES; 
    110141        while (_HDQ_readPin() != 0 && maxTries-- > 0) 
    111142            if (maxTries == 1) return 0xFF; 
    112143         
    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); 
    114146         
     147        // Read the bit 
    115148        result |= _HDQ_readPin()<<ii; 
    116149         
    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); 
    118152         
    119         // Wait for the slave to toggle a high again 
     153        // Wait for the slave to toggle a high, or fail 
    120154        maxTries = HDQ_DELAY_FAIL_TRIES; 
    121155        while (_HDQ_readPin() != 1 && maxTries-- > 0) 
    122156            if (maxTries == 1) return 0xFF; 
    123          
    124157    } 
    125158