Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

MBED_ControlBus.cpp

Committer:
wim
Date:
2011-08-20
Revision:
2:1dab1089c332
Child:
6:aaefa04f06be

File content as of revision 2:1dab1089c332:

/* MBED_ControlBus - Use MBED Port Pins for controlling the external Bus
 * Copyright (c) 2011 Wim Huiskamp
 *
 * Released under the MIT License: http://mbed.org/license/mit
 *
 * version 0.2 Initial Release
*/
#include "mbed.h"
#include "MBED_ControlBus.h"

//Debounce and Edge detecting stuff
#include "PinDetect.h"
//#define __DIGITAL_IN
//#define __INTERRUPT_IN
#define __PINDETECT_IN




/** Create an MBED_ControlBus object connected to the specified Pins
 *
 * @param PinName WR the Write pin 
 * @param PinName RD the Read pin 
 * @param PinName DTR the databuffer Transmit/Receive direction pin 
 * @param PinName CDBUF the databuffer enable pin
 * @param PinName CDINT the Keyboard interrupt pin  
 * @param PinName FIRE the Manual Fire Unit pin   
*/
MBED_ControlBus::MBED_ControlBus(PinName WR, PinName RD, PinName DTR, PinName CDBUF, PinName CDINT, PinName FIRE) :
   _WR(WR),        // WR pin
   _RD(RD),        // RD pin
   _DTR(DTR),      // DTR pin
   _CDBUF(CDBUF),  // CDBUF pin   
   _CDINT(CDINT),  // CDINT pin      
   _FIRE(FIRE) {   // FIRE pin         
   _init();
}


/** Set or Clear the WR pin on Control Bus
 *
 * @param Bit_Level wr_level
*/
void MBED_ControlBus::WR (Bit_Level wr_level) {

    if (wr_level == LOW) {
        // Make sure that databus buffer is enabled for Write
        //busctrl(ENABLE, WRITE);        
             
        _RD = 1;                    // RD Pin High, make sure there is no collision    
        _WR = 0;                    // WR Pin Low
      }               
    else {
        _WR = 1;                    // WR Pin High
    }      
}
 

/** Set or Clear the RD pin on Control Bus
 *
 * @param Bit_Level rd_level
*/
void MBED_ControlBus::RD (Bit_Level rd_level) {

    if (rd_level == LOW) {
        // Make sure that databus buffer is enabled for Read
        //busctrl(ENABLE, READ);

        _WR = 1;                    // WR Pin High, make sure there is no collision    
        _RD = 0;                    // RD Pin Low
      }               
    else {
        _RD = 1;                    // RD Pin High
    }      
}
               

/** Enable databus buffer for Write or Read
 *
 * @param Bus_Dir buf_dir
*/
void MBED_ControlBus::busdir (Bus_Dir buf_dir) {

    if (buf_dir == READ) {
        // Make sure that databus buffer is enabled for READ
        _DTR   = 0;                 // DTR Pin Low, Read Direction            
        _CDBUF = 0;                 // CDBUF Pin Low, Enable Buffer                
      }                
    else {
        // Make sure that databus buffer is enabled for Write   
        _DTR   = 1;                 // DTR Pin High, Write Direction            
        _CDBUF = 0;                 // CDBUF Pin Low, Enable Buffer
    }      
}


/** Enable/Disable databus buffer and control Write or Read Direction
 *
 * @param Bus_Ena buf_ena
 * @param Bus_Dir buf_dir
*/
void MBED_ControlBus::busctrl (Bus_Ena buf_ena, Bus_Dir buf_dir) {

    if (buf_ena == ENABLE) {
        _CDBUF = 0;                 // CDBUF Pin Low, Enable Buffer
      }                
    else {
        _CDBUF = 1;                 // CDBUF Pin High, Disable Buffer                
    }      

    if (buf_dir == READ) {
        _DTR   = 0;                 // DTR Pin Low, Read Direction            
      }                
    else {
        _DTR   = 1;                 // DTR Pin High, Write Direction            
    }      
}


#ifdef __DIGITAL_IN
/** Get the CDINT pin value from Control Bus
 *
 * @returns Bit_Level CDINT_level
*/
Bit_Level MBED_ControlBus::CDINT () {
    
    // CDINT Pin value, used as regular Pin rather than as interrupt
    if (_CDINT == 0)
      return LOW;
    else 
      return HIGH;      
}

/** Get the FIRE pin value from Control Bus
 *
 * @returns Bit_Level FIRE_level
*/
Bit_Level MBED_ControlBus::FIRE() {
    
    // FIRE Pin value, used as regular Pin rather than as interrupt
    if (_FIRE == 0)
      return LOW;
    else 
      return HIGH;      
}

/** Setup debouncing and edge detection on the CDINT pin 
 *
 * @returns
*/
void MBED_ControlBus::_CDINT_init() {
}

/** Setup debouncing and edge detection on the FIRE pin 
 *
 * @returns
*/
void MBED_ControlBus::_FIRE_init() {
}
#endif   //__DIGITAL_IN



#ifdef __INTERRUPT_IN
/** Get the CDINT pin value from Control Bus
 *
 * @returns Bit_Level CDINT_detected
*/
Bit_Level MBED_ControlBus::CDINT () {
       
    // CDINT pin has been Debounced and Edge detected
    // Respond only once for every keypress
    if (_CDINT_detected) {
      _CDINT_detected = false;
      return HIGH;
    }
    else {
      return LOW;      
    }
}
 
/** Get the FIRE pin value from Control Bus
 *
 * @returns Bit_Level FIRE_detected
*/
Bit_Level MBED_ControlBus::FIRE() {
    
    // FIRE pin has been Debounced and Edge detected
    // Respond only once for every keypress
    if (_FIRE_detected) {
      _FIRE_detected = false;
      return HIGH;
    }
    else {
      return LOW;      
    }
}


/** Called on detection of a rising edge at the CDINT pin 
 *
 * @returns
*/
 void MBED_ControlBus::_CDINT_activated(void) {
    _CDINT_detected = true;
}


/** Called on detection of a rising edge at the FIRE pin 
 *
 * @returns
*/
 void MBED_ControlBus::_FIRE_activated(void) {
    _FIRE_detected = true;
}


/** Setup simple edge detection on the CDINT pin 
 *
 * @returns
*/
void MBED_ControlBus::_CDINT_init() {
    // Setup the callback function
   _CDINT.mode(PullDown);
   _CDINT.rise(this, &MBED_ControlBus::_CDINT_activated);

   // Init the edge detection boolean
   _CDINT_detected = false;
}

/** Setup simple edge detection on the FIRE pin 
 *
 * @returns
*/
void MBED_ControlBus::_FIRE_init() {
    // Setup the callback function
   _FIRE.mode(PullDown);
   _FIRE.rise(this, &MBED_ControlBus::_FIRE_activated);

   // Init the edge detection boolean
   _FIRE_detected = false;
}

/** Setup debouncing and edge detection on the CDINT pin 
 *
 * @returns
*/
void MBED_ControlBus::_CDINT_init() {
}

/** Setup debouncing and edge detection on the FIRE pin 
 *
 * @returns
*/
void MBED_ControlBus::_FIRE_init() {
}
#endif //__INTERRUPT_IN



#ifdef __PINDETECT_IN
/** Get the CDINT pin value from Control Bus
 *
 * @returns Bit_Level CDINT_detected
*/
Bit_Level MBED_ControlBus::CDINT () {
       
    // CDINT pin has been Debounced and Edge detected
    // Respond only once for every keypress
    if (_CDINT_detected) {
      _CDINT_detected = false;
      return HIGH;
    }
    else {
      return LOW;      
    }
}
 
 /** Get the FIRE pin value from Control Bus
 *
 * @returns Bit_Level FIRE_detected
*/
Bit_Level MBED_ControlBus::FIRE() {
    
    // FIRE pin has been Debounced and Edge detected
    // Respond only once for every keypress
    if (_FIRE_detected) {
      _FIRE_detected = false;
      return HIGH;
    }
    else {
      return LOW;      
    }
}

/** Called on detection of a rising edge at the CDINT pin 
 *
 * @returns
*/
 void MBED_ControlBus::_CDINT_activated(void) {
    _CDINT_detected = true;
}

/** Called on detection of a falling edge at the CDINT pin 
 *
 * @returns
*/
void MBED_ControlBus::_CDINT_deactivated(void) {
    _CDINT_detected = false;
}

/** Called on detection of a rising edge at the FIRE pin 
 *
 * @returns
*/
 void MBED_ControlBus::_FIRE_activated(void) {
    _FIRE_detected = true;
}

/** Called on detection of a falling edge at the FIRE pin 
 *
 * @returns
*/
void MBED_ControlBus::_FIRE_deactivated(void) {
    _FIRE_detected = false;
}

/** Setup debouncing and edge detection on the CDINT pin 
 *
 * @returns
*/
void MBED_ControlBus::_CDINT_init() {

    // Setup the callback functions
   _CDINT.mode(PullDown);
   _CDINT.attach_asserted(this, &MBED_ControlBus::_CDINT_activated);
   _CDINT.attach_deasserted(this, &MBED_ControlBus::_CDINT_deactivated);
//   _CDINT.attach_asserted_held( &keyPressedHeld );
//   _CDINT.attach_deasserted_held( &keyReleasedHeld );
 
   // Init the edge detection boolean
   _CDINT_detected = false;

   // Sampling does not begin until you set a frequency.
   // The default is 20ms. If you want a different frequency
   // then pass the period in microseconds for example, for 10ms :-
   //     _CDINT.setSampleFrequency(10000);
   //
   _CDINT.setSampleFrequency(); // Defaults to 20ms.
}

/** Setup debouncing and edge detection on the FIRE pin 
 *
 * @returns
*/
void MBED_ControlBus::_FIRE_init() {

    // Setup the callback functions
   _FIRE.mode(PullDown);
   _FIRE.attach_asserted(this, &MBED_ControlBus::_FIRE_activated);
   _FIRE.attach_deasserted(this, &MBED_ControlBus::_FIRE_deactivated);
//   _FIRE.attach_asserted_held( &keyPressedHeld );
//   _FIRE.attach_deasserted_held( &keyReleasedHeld );
 
   // Init the edge detection boolean
   _FIRE_detected = false;

   // Sampling does not begin until you set a frequency.
   // The default is 20ms. If you want a different frequency
   // then pass the period in microseconds for example, for 10ms :-
   //     _FIRE.setSampleFrequency(10000);
   //
   _FIRE.setSampleFrequency(); // Defaults to 20ms.
}
#endif // __PINDETECT_IN


/** Init MBED_ControlBus
 * @param
 * @returns 
 */
void MBED_ControlBus::_init() {
    // Make sure that all Control pins are disabled
    _RD    = 1;                        // RD Pin High    
    _WR    = 1;                        // WR Pin High        
    
    // Make sure that databus buffer is enabled for Write
    busctrl(ENABLE, WRITE);
    
    // Setup debouncing and edge detection for CDINT    
    _CDINT_init();    
    
    // Setup debouncing and edge detection for FIRE    
    _FIRE_init();        
}