Libraries and Example of mbed parallel bus using I2C port expanders

Dependencies:   HDSP253X mbed PCF8574_Bus

Committer:
wim
Date:
Sat Aug 20 12:49:44 2011 +0000
Revision:
2:1dab1089c332
Child:
6:aaefa04f06be
First commit, testloop

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 2:1dab1089c332 1 /* MBED_ControlBus - Use MBED Port Pins for controlling the external Bus
wim 2:1dab1089c332 2 * Copyright (c) 2011 Wim Huiskamp
wim 2:1dab1089c332 3 *
wim 2:1dab1089c332 4 * Released under the MIT License: http://mbed.org/license/mit
wim 2:1dab1089c332 5 *
wim 2:1dab1089c332 6 * version 0.2 Initial Release
wim 2:1dab1089c332 7 */
wim 2:1dab1089c332 8 #include "mbed.h"
wim 2:1dab1089c332 9 #include "MBED_ControlBus.h"
wim 2:1dab1089c332 10
wim 2:1dab1089c332 11 //Debounce and Edge detecting stuff
wim 2:1dab1089c332 12 #include "PinDetect.h"
wim 2:1dab1089c332 13 //#define __DIGITAL_IN
wim 2:1dab1089c332 14 //#define __INTERRUPT_IN
wim 2:1dab1089c332 15 #define __PINDETECT_IN
wim 2:1dab1089c332 16
wim 2:1dab1089c332 17
wim 2:1dab1089c332 18
wim 2:1dab1089c332 19
wim 2:1dab1089c332 20 /** Create an MBED_ControlBus object connected to the specified Pins
wim 2:1dab1089c332 21 *
wim 2:1dab1089c332 22 * @param PinName WR the Write pin
wim 2:1dab1089c332 23 * @param PinName RD the Read pin
wim 2:1dab1089c332 24 * @param PinName DTR the databuffer Transmit/Receive direction pin
wim 2:1dab1089c332 25 * @param PinName CDBUF the databuffer enable pin
wim 2:1dab1089c332 26 * @param PinName CDINT the Keyboard interrupt pin
wim 2:1dab1089c332 27 * @param PinName FIRE the Manual Fire Unit pin
wim 2:1dab1089c332 28 */
wim 2:1dab1089c332 29 MBED_ControlBus::MBED_ControlBus(PinName WR, PinName RD, PinName DTR, PinName CDBUF, PinName CDINT, PinName FIRE) :
wim 2:1dab1089c332 30 _WR(WR), // WR pin
wim 2:1dab1089c332 31 _RD(RD), // RD pin
wim 2:1dab1089c332 32 _DTR(DTR), // DTR pin
wim 2:1dab1089c332 33 _CDBUF(CDBUF), // CDBUF pin
wim 2:1dab1089c332 34 _CDINT(CDINT), // CDINT pin
wim 2:1dab1089c332 35 _FIRE(FIRE) { // FIRE pin
wim 2:1dab1089c332 36 _init();
wim 2:1dab1089c332 37 }
wim 2:1dab1089c332 38
wim 2:1dab1089c332 39
wim 2:1dab1089c332 40 /** Set or Clear the WR pin on Control Bus
wim 2:1dab1089c332 41 *
wim 2:1dab1089c332 42 * @param Bit_Level wr_level
wim 2:1dab1089c332 43 */
wim 2:1dab1089c332 44 void MBED_ControlBus::WR (Bit_Level wr_level) {
wim 2:1dab1089c332 45
wim 2:1dab1089c332 46 if (wr_level == LOW) {
wim 2:1dab1089c332 47 // Make sure that databus buffer is enabled for Write
wim 2:1dab1089c332 48 //busctrl(ENABLE, WRITE);
wim 2:1dab1089c332 49
wim 2:1dab1089c332 50 _RD = 1; // RD Pin High, make sure there is no collision
wim 2:1dab1089c332 51 _WR = 0; // WR Pin Low
wim 2:1dab1089c332 52 }
wim 2:1dab1089c332 53 else {
wim 2:1dab1089c332 54 _WR = 1; // WR Pin High
wim 2:1dab1089c332 55 }
wim 2:1dab1089c332 56 }
wim 2:1dab1089c332 57
wim 2:1dab1089c332 58
wim 2:1dab1089c332 59 /** Set or Clear the RD pin on Control Bus
wim 2:1dab1089c332 60 *
wim 2:1dab1089c332 61 * @param Bit_Level rd_level
wim 2:1dab1089c332 62 */
wim 2:1dab1089c332 63 void MBED_ControlBus::RD (Bit_Level rd_level) {
wim 2:1dab1089c332 64
wim 2:1dab1089c332 65 if (rd_level == LOW) {
wim 2:1dab1089c332 66 // Make sure that databus buffer is enabled for Read
wim 2:1dab1089c332 67 //busctrl(ENABLE, READ);
wim 2:1dab1089c332 68
wim 2:1dab1089c332 69 _WR = 1; // WR Pin High, make sure there is no collision
wim 2:1dab1089c332 70 _RD = 0; // RD Pin Low
wim 2:1dab1089c332 71 }
wim 2:1dab1089c332 72 else {
wim 2:1dab1089c332 73 _RD = 1; // RD Pin High
wim 2:1dab1089c332 74 }
wim 2:1dab1089c332 75 }
wim 2:1dab1089c332 76
wim 2:1dab1089c332 77
wim 2:1dab1089c332 78 /** Enable databus buffer for Write or Read
wim 2:1dab1089c332 79 *
wim 2:1dab1089c332 80 * @param Bus_Dir buf_dir
wim 2:1dab1089c332 81 */
wim 2:1dab1089c332 82 void MBED_ControlBus::busdir (Bus_Dir buf_dir) {
wim 2:1dab1089c332 83
wim 2:1dab1089c332 84 if (buf_dir == READ) {
wim 2:1dab1089c332 85 // Make sure that databus buffer is enabled for READ
wim 2:1dab1089c332 86 _DTR = 0; // DTR Pin Low, Read Direction
wim 2:1dab1089c332 87 _CDBUF = 0; // CDBUF Pin Low, Enable Buffer
wim 2:1dab1089c332 88 }
wim 2:1dab1089c332 89 else {
wim 2:1dab1089c332 90 // Make sure that databus buffer is enabled for Write
wim 2:1dab1089c332 91 _DTR = 1; // DTR Pin High, Write Direction
wim 2:1dab1089c332 92 _CDBUF = 0; // CDBUF Pin Low, Enable Buffer
wim 2:1dab1089c332 93 }
wim 2:1dab1089c332 94 }
wim 2:1dab1089c332 95
wim 2:1dab1089c332 96
wim 2:1dab1089c332 97 /** Enable/Disable databus buffer and control Write or Read Direction
wim 2:1dab1089c332 98 *
wim 2:1dab1089c332 99 * @param Bus_Ena buf_ena
wim 2:1dab1089c332 100 * @param Bus_Dir buf_dir
wim 2:1dab1089c332 101 */
wim 2:1dab1089c332 102 void MBED_ControlBus::busctrl (Bus_Ena buf_ena, Bus_Dir buf_dir) {
wim 2:1dab1089c332 103
wim 2:1dab1089c332 104 if (buf_ena == ENABLE) {
wim 2:1dab1089c332 105 _CDBUF = 0; // CDBUF Pin Low, Enable Buffer
wim 2:1dab1089c332 106 }
wim 2:1dab1089c332 107 else {
wim 2:1dab1089c332 108 _CDBUF = 1; // CDBUF Pin High, Disable Buffer
wim 2:1dab1089c332 109 }
wim 2:1dab1089c332 110
wim 2:1dab1089c332 111 if (buf_dir == READ) {
wim 2:1dab1089c332 112 _DTR = 0; // DTR Pin Low, Read Direction
wim 2:1dab1089c332 113 }
wim 2:1dab1089c332 114 else {
wim 2:1dab1089c332 115 _DTR = 1; // DTR Pin High, Write Direction
wim 2:1dab1089c332 116 }
wim 2:1dab1089c332 117 }
wim 2:1dab1089c332 118
wim 2:1dab1089c332 119
wim 2:1dab1089c332 120 #ifdef __DIGITAL_IN
wim 2:1dab1089c332 121 /** Get the CDINT pin value from Control Bus
wim 2:1dab1089c332 122 *
wim 2:1dab1089c332 123 * @returns Bit_Level CDINT_level
wim 2:1dab1089c332 124 */
wim 2:1dab1089c332 125 Bit_Level MBED_ControlBus::CDINT () {
wim 2:1dab1089c332 126
wim 2:1dab1089c332 127 // CDINT Pin value, used as regular Pin rather than as interrupt
wim 2:1dab1089c332 128 if (_CDINT == 0)
wim 2:1dab1089c332 129 return LOW;
wim 2:1dab1089c332 130 else
wim 2:1dab1089c332 131 return HIGH;
wim 2:1dab1089c332 132 }
wim 2:1dab1089c332 133
wim 2:1dab1089c332 134 /** Get the FIRE pin value from Control Bus
wim 2:1dab1089c332 135 *
wim 2:1dab1089c332 136 * @returns Bit_Level FIRE_level
wim 2:1dab1089c332 137 */
wim 2:1dab1089c332 138 Bit_Level MBED_ControlBus::FIRE() {
wim 2:1dab1089c332 139
wim 2:1dab1089c332 140 // FIRE Pin value, used as regular Pin rather than as interrupt
wim 2:1dab1089c332 141 if (_FIRE == 0)
wim 2:1dab1089c332 142 return LOW;
wim 2:1dab1089c332 143 else
wim 2:1dab1089c332 144 return HIGH;
wim 2:1dab1089c332 145 }
wim 2:1dab1089c332 146
wim 2:1dab1089c332 147 /** Setup debouncing and edge detection on the CDINT pin
wim 2:1dab1089c332 148 *
wim 2:1dab1089c332 149 * @returns
wim 2:1dab1089c332 150 */
wim 2:1dab1089c332 151 void MBED_ControlBus::_CDINT_init() {
wim 2:1dab1089c332 152 }
wim 2:1dab1089c332 153
wim 2:1dab1089c332 154 /** Setup debouncing and edge detection on the FIRE pin
wim 2:1dab1089c332 155 *
wim 2:1dab1089c332 156 * @returns
wim 2:1dab1089c332 157 */
wim 2:1dab1089c332 158 void MBED_ControlBus::_FIRE_init() {
wim 2:1dab1089c332 159 }
wim 2:1dab1089c332 160 #endif //__DIGITAL_IN
wim 2:1dab1089c332 161
wim 2:1dab1089c332 162
wim 2:1dab1089c332 163
wim 2:1dab1089c332 164 #ifdef __INTERRUPT_IN
wim 2:1dab1089c332 165 /** Get the CDINT pin value from Control Bus
wim 2:1dab1089c332 166 *
wim 2:1dab1089c332 167 * @returns Bit_Level CDINT_detected
wim 2:1dab1089c332 168 */
wim 2:1dab1089c332 169 Bit_Level MBED_ControlBus::CDINT () {
wim 2:1dab1089c332 170
wim 2:1dab1089c332 171 // CDINT pin has been Debounced and Edge detected
wim 2:1dab1089c332 172 // Respond only once for every keypress
wim 2:1dab1089c332 173 if (_CDINT_detected) {
wim 2:1dab1089c332 174 _CDINT_detected = false;
wim 2:1dab1089c332 175 return HIGH;
wim 2:1dab1089c332 176 }
wim 2:1dab1089c332 177 else {
wim 2:1dab1089c332 178 return LOW;
wim 2:1dab1089c332 179 }
wim 2:1dab1089c332 180 }
wim 2:1dab1089c332 181
wim 2:1dab1089c332 182 /** Get the FIRE pin value from Control Bus
wim 2:1dab1089c332 183 *
wim 2:1dab1089c332 184 * @returns Bit_Level FIRE_detected
wim 2:1dab1089c332 185 */
wim 2:1dab1089c332 186 Bit_Level MBED_ControlBus::FIRE() {
wim 2:1dab1089c332 187
wim 2:1dab1089c332 188 // FIRE pin has been Debounced and Edge detected
wim 2:1dab1089c332 189 // Respond only once for every keypress
wim 2:1dab1089c332 190 if (_FIRE_detected) {
wim 2:1dab1089c332 191 _FIRE_detected = false;
wim 2:1dab1089c332 192 return HIGH;
wim 2:1dab1089c332 193 }
wim 2:1dab1089c332 194 else {
wim 2:1dab1089c332 195 return LOW;
wim 2:1dab1089c332 196 }
wim 2:1dab1089c332 197 }
wim 2:1dab1089c332 198
wim 2:1dab1089c332 199
wim 2:1dab1089c332 200 /** Called on detection of a rising edge at the CDINT pin
wim 2:1dab1089c332 201 *
wim 2:1dab1089c332 202 * @returns
wim 2:1dab1089c332 203 */
wim 2:1dab1089c332 204 void MBED_ControlBus::_CDINT_activated(void) {
wim 2:1dab1089c332 205 _CDINT_detected = true;
wim 2:1dab1089c332 206 }
wim 2:1dab1089c332 207
wim 2:1dab1089c332 208
wim 2:1dab1089c332 209 /** Called on detection of a rising edge at the FIRE pin
wim 2:1dab1089c332 210 *
wim 2:1dab1089c332 211 * @returns
wim 2:1dab1089c332 212 */
wim 2:1dab1089c332 213 void MBED_ControlBus::_FIRE_activated(void) {
wim 2:1dab1089c332 214 _FIRE_detected = true;
wim 2:1dab1089c332 215 }
wim 2:1dab1089c332 216
wim 2:1dab1089c332 217
wim 2:1dab1089c332 218 /** Setup simple edge detection on the CDINT pin
wim 2:1dab1089c332 219 *
wim 2:1dab1089c332 220 * @returns
wim 2:1dab1089c332 221 */
wim 2:1dab1089c332 222 void MBED_ControlBus::_CDINT_init() {
wim 2:1dab1089c332 223 // Setup the callback function
wim 2:1dab1089c332 224 _CDINT.mode(PullDown);
wim 2:1dab1089c332 225 _CDINT.rise(this, &MBED_ControlBus::_CDINT_activated);
wim 2:1dab1089c332 226
wim 2:1dab1089c332 227 // Init the edge detection boolean
wim 2:1dab1089c332 228 _CDINT_detected = false;
wim 2:1dab1089c332 229 }
wim 2:1dab1089c332 230
wim 2:1dab1089c332 231 /** Setup simple edge detection on the FIRE pin
wim 2:1dab1089c332 232 *
wim 2:1dab1089c332 233 * @returns
wim 2:1dab1089c332 234 */
wim 2:1dab1089c332 235 void MBED_ControlBus::_FIRE_init() {
wim 2:1dab1089c332 236 // Setup the callback function
wim 2:1dab1089c332 237 _FIRE.mode(PullDown);
wim 2:1dab1089c332 238 _FIRE.rise(this, &MBED_ControlBus::_FIRE_activated);
wim 2:1dab1089c332 239
wim 2:1dab1089c332 240 // Init the edge detection boolean
wim 2:1dab1089c332 241 _FIRE_detected = false;
wim 2:1dab1089c332 242 }
wim 2:1dab1089c332 243
wim 2:1dab1089c332 244 /** Setup debouncing and edge detection on the CDINT pin
wim 2:1dab1089c332 245 *
wim 2:1dab1089c332 246 * @returns
wim 2:1dab1089c332 247 */
wim 2:1dab1089c332 248 void MBED_ControlBus::_CDINT_init() {
wim 2:1dab1089c332 249 }
wim 2:1dab1089c332 250
wim 2:1dab1089c332 251 /** Setup debouncing and edge detection on the FIRE pin
wim 2:1dab1089c332 252 *
wim 2:1dab1089c332 253 * @returns
wim 2:1dab1089c332 254 */
wim 2:1dab1089c332 255 void MBED_ControlBus::_FIRE_init() {
wim 2:1dab1089c332 256 }
wim 2:1dab1089c332 257 #endif //__INTERRUPT_IN
wim 2:1dab1089c332 258
wim 2:1dab1089c332 259
wim 2:1dab1089c332 260
wim 2:1dab1089c332 261 #ifdef __PINDETECT_IN
wim 2:1dab1089c332 262 /** Get the CDINT pin value from Control Bus
wim 2:1dab1089c332 263 *
wim 2:1dab1089c332 264 * @returns Bit_Level CDINT_detected
wim 2:1dab1089c332 265 */
wim 2:1dab1089c332 266 Bit_Level MBED_ControlBus::CDINT () {
wim 2:1dab1089c332 267
wim 2:1dab1089c332 268 // CDINT pin has been Debounced and Edge detected
wim 2:1dab1089c332 269 // Respond only once for every keypress
wim 2:1dab1089c332 270 if (_CDINT_detected) {
wim 2:1dab1089c332 271 _CDINT_detected = false;
wim 2:1dab1089c332 272 return HIGH;
wim 2:1dab1089c332 273 }
wim 2:1dab1089c332 274 else {
wim 2:1dab1089c332 275 return LOW;
wim 2:1dab1089c332 276 }
wim 2:1dab1089c332 277 }
wim 2:1dab1089c332 278
wim 2:1dab1089c332 279 /** Get the FIRE pin value from Control Bus
wim 2:1dab1089c332 280 *
wim 2:1dab1089c332 281 * @returns Bit_Level FIRE_detected
wim 2:1dab1089c332 282 */
wim 2:1dab1089c332 283 Bit_Level MBED_ControlBus::FIRE() {
wim 2:1dab1089c332 284
wim 2:1dab1089c332 285 // FIRE pin has been Debounced and Edge detected
wim 2:1dab1089c332 286 // Respond only once for every keypress
wim 2:1dab1089c332 287 if (_FIRE_detected) {
wim 2:1dab1089c332 288 _FIRE_detected = false;
wim 2:1dab1089c332 289 return HIGH;
wim 2:1dab1089c332 290 }
wim 2:1dab1089c332 291 else {
wim 2:1dab1089c332 292 return LOW;
wim 2:1dab1089c332 293 }
wim 2:1dab1089c332 294 }
wim 2:1dab1089c332 295
wim 2:1dab1089c332 296 /** Called on detection of a rising edge at the CDINT pin
wim 2:1dab1089c332 297 *
wim 2:1dab1089c332 298 * @returns
wim 2:1dab1089c332 299 */
wim 2:1dab1089c332 300 void MBED_ControlBus::_CDINT_activated(void) {
wim 2:1dab1089c332 301 _CDINT_detected = true;
wim 2:1dab1089c332 302 }
wim 2:1dab1089c332 303
wim 2:1dab1089c332 304 /** Called on detection of a falling edge at the CDINT pin
wim 2:1dab1089c332 305 *
wim 2:1dab1089c332 306 * @returns
wim 2:1dab1089c332 307 */
wim 2:1dab1089c332 308 void MBED_ControlBus::_CDINT_deactivated(void) {
wim 2:1dab1089c332 309 _CDINT_detected = false;
wim 2:1dab1089c332 310 }
wim 2:1dab1089c332 311
wim 2:1dab1089c332 312 /** Called on detection of a rising edge at the FIRE pin
wim 2:1dab1089c332 313 *
wim 2:1dab1089c332 314 * @returns
wim 2:1dab1089c332 315 */
wim 2:1dab1089c332 316 void MBED_ControlBus::_FIRE_activated(void) {
wim 2:1dab1089c332 317 _FIRE_detected = true;
wim 2:1dab1089c332 318 }
wim 2:1dab1089c332 319
wim 2:1dab1089c332 320 /** Called on detection of a falling edge at the FIRE pin
wim 2:1dab1089c332 321 *
wim 2:1dab1089c332 322 * @returns
wim 2:1dab1089c332 323 */
wim 2:1dab1089c332 324 void MBED_ControlBus::_FIRE_deactivated(void) {
wim 2:1dab1089c332 325 _FIRE_detected = false;
wim 2:1dab1089c332 326 }
wim 2:1dab1089c332 327
wim 2:1dab1089c332 328 /** Setup debouncing and edge detection on the CDINT pin
wim 2:1dab1089c332 329 *
wim 2:1dab1089c332 330 * @returns
wim 2:1dab1089c332 331 */
wim 2:1dab1089c332 332 void MBED_ControlBus::_CDINT_init() {
wim 2:1dab1089c332 333
wim 2:1dab1089c332 334 // Setup the callback functions
wim 2:1dab1089c332 335 _CDINT.mode(PullDown);
wim 2:1dab1089c332 336 _CDINT.attach_asserted(this, &MBED_ControlBus::_CDINT_activated);
wim 2:1dab1089c332 337 _CDINT.attach_deasserted(this, &MBED_ControlBus::_CDINT_deactivated);
wim 2:1dab1089c332 338 // _CDINT.attach_asserted_held( &keyPressedHeld );
wim 2:1dab1089c332 339 // _CDINT.attach_deasserted_held( &keyReleasedHeld );
wim 2:1dab1089c332 340
wim 2:1dab1089c332 341 // Init the edge detection boolean
wim 2:1dab1089c332 342 _CDINT_detected = false;
wim 2:1dab1089c332 343
wim 2:1dab1089c332 344 // Sampling does not begin until you set a frequency.
wim 2:1dab1089c332 345 // The default is 20ms. If you want a different frequency
wim 2:1dab1089c332 346 // then pass the period in microseconds for example, for 10ms :-
wim 2:1dab1089c332 347 // _CDINT.setSampleFrequency(10000);
wim 2:1dab1089c332 348 //
wim 2:1dab1089c332 349 _CDINT.setSampleFrequency(); // Defaults to 20ms.
wim 2:1dab1089c332 350 }
wim 2:1dab1089c332 351
wim 2:1dab1089c332 352 /** Setup debouncing and edge detection on the FIRE pin
wim 2:1dab1089c332 353 *
wim 2:1dab1089c332 354 * @returns
wim 2:1dab1089c332 355 */
wim 2:1dab1089c332 356 void MBED_ControlBus::_FIRE_init() {
wim 2:1dab1089c332 357
wim 2:1dab1089c332 358 // Setup the callback functions
wim 2:1dab1089c332 359 _FIRE.mode(PullDown);
wim 2:1dab1089c332 360 _FIRE.attach_asserted(this, &MBED_ControlBus::_FIRE_activated);
wim 2:1dab1089c332 361 _FIRE.attach_deasserted(this, &MBED_ControlBus::_FIRE_deactivated);
wim 2:1dab1089c332 362 // _FIRE.attach_asserted_held( &keyPressedHeld );
wim 2:1dab1089c332 363 // _FIRE.attach_deasserted_held( &keyReleasedHeld );
wim 2:1dab1089c332 364
wim 2:1dab1089c332 365 // Init the edge detection boolean
wim 2:1dab1089c332 366 _FIRE_detected = false;
wim 2:1dab1089c332 367
wim 2:1dab1089c332 368 // Sampling does not begin until you set a frequency.
wim 2:1dab1089c332 369 // The default is 20ms. If you want a different frequency
wim 2:1dab1089c332 370 // then pass the period in microseconds for example, for 10ms :-
wim 2:1dab1089c332 371 // _FIRE.setSampleFrequency(10000);
wim 2:1dab1089c332 372 //
wim 2:1dab1089c332 373 _FIRE.setSampleFrequency(); // Defaults to 20ms.
wim 2:1dab1089c332 374 }
wim 2:1dab1089c332 375 #endif // __PINDETECT_IN
wim 2:1dab1089c332 376
wim 2:1dab1089c332 377
wim 2:1dab1089c332 378 /** Init MBED_ControlBus
wim 2:1dab1089c332 379 * @param
wim 2:1dab1089c332 380 * @returns
wim 2:1dab1089c332 381 */
wim 2:1dab1089c332 382 void MBED_ControlBus::_init() {
wim 2:1dab1089c332 383 // Make sure that all Control pins are disabled
wim 2:1dab1089c332 384 _RD = 1; // RD Pin High
wim 2:1dab1089c332 385 _WR = 1; // WR Pin High
wim 2:1dab1089c332 386
wim 2:1dab1089c332 387 // Make sure that databus buffer is enabled for Write
wim 2:1dab1089c332 388 busctrl(ENABLE, WRITE);
wim 2:1dab1089c332 389
wim 2:1dab1089c332 390 // Setup debouncing and edge detection for CDINT
wim 2:1dab1089c332 391 _CDINT_init();
wim 2:1dab1089c332 392
wim 2:1dab1089c332 393 // Setup debouncing and edge detection for FIRE
wim 2:1dab1089c332 394 _FIRE_init();
wim 2:1dab1089c332 395 }