Libraries and Example of mbed parallel bus using I2C port expanders
Dependencies: HDSP253X mbed PCF8574_Bus
Revision 0:2467aed99127, committed 2011-08-31
- Comitter:
- wim
- Date:
- Wed Aug 31 19:45:31 2011 +0000
- Child:
- 1:e180256ba6fb
- Commit message:
- First Version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BusDefines.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,38 @@ +/* BusDefines - Use the MBED Port pins and PCF8574s for controlling the Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _BUS_DEFINES_H +#define _BUS_DEFINES_H + + +//Pin Defines for MBED Control Bus (inputs) +#define D_CDINT p20 +//Pin Defines for MBED Control Bus (outputs) +#define D_DTR p21 +#define D_CDBUF p22 +#define D_RD p23 +#define D_WR p24 + +#define D_CTRL_MSK 0xFF + +//Pin Defines for I2C Bus +//#define D_SDA p9 +//#define D_SCL p10 +#define D_SDA p28 +#define D_SCL p27 + +//I2C Bus Address Defines for PCF8574 slaves +#define D_I2C_DATA_BUS 0x40 +#define D_I2C_ADDR_BUS 0x42 +#define D_I2C_ENA_BUS 0x44 + + +//Host PC Baudrate (Virtual Com Port on USB) +#define D_BAUDRATE 9600 +//#define D_BAUDRATE 57600 + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BusEnums.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,17 @@ +/* BusEnums - Use the MBED Port pins and PCF8574s for controlling the Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _BUS_ENUMS_H +#define _BUS_ENUMS_H + +//Enums for Control Bus and Enable Bus +enum Bit_Level { LOW, HIGH }; +enum Bus_Dir { READ, WRITE }; +enum Bus_Ena { ENABLE, DISABLE }; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Dbg.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,62 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#define __DEBUG +#include "Dbg.h" +#include "mbed.h" +#include <cstdarg> + +void DebugStream::debug(const char* format, ...) +{ + va_list argp; + + va_start(argp, format); + vprintf(format, argp); + va_end(argp); +} + +void DebugStream::release() +{ + +} + +void DebugStream::breakPoint(const char* file, int line) +{ + printf("\r\nBREAK in %s at line %d\r\n", file, line); + fflush(stdout); + getchar(); + fflush(stdin); +} + +/* +int snprintf(char *str, int size, const char *format, ...) +{ + va_list argp; + + va_start(argp, format); + vsprintf(str, format, argp); + va_end(argp); + + return strlen(str); +} +*/
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Dbg.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,78 @@ +/** \file +Debugging helpers header file +*/ + +/** + Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef DBG_H +#define DBG_H + +/*! + Define to enable debugging in one file + #define __DEBUG +*/ + +#ifdef __DEBUG + +#ifndef __DEBUGSTREAM +#define __DEBUGSTREAM + +class DebugStream { + public: + static void debug(const char* format, ...); + static void release(); + static void breakPoint(const char* file, int line); + private: + +}; + +#undef DBG +#undef DBGL +#undef DBG_END +#undef BREAK + +//Debug output (if enabled), same syntax as printf, with heading info +//#define DBG(...) do{ DebugStream::debug("[%s:%s@%d] ", __FILE__, __FUNCTION__, __LINE__); DebugStream::debug(__VA_ARGS__); } while(0); +#define DBG(...) do{ DebugStream::debug("[%s @%d] ", __FUNCTION__, __LINE__); DebugStream::debug(__VA_ARGS__); } while(0); + +//Debug output (if enabled), same syntax as printf, no heading info +#define DBGL(...) do{ DebugStream::debug(__VA_ARGS__); } while(0); +#define DBG_END DebugStream::release + +//Breakpoint using serial debug interface (if debug enabled) +#define BREAK() DebugStream::breakPoint(__FILE__, __LINE__) +#endif + +#else +#undef DBG +#undef DBGL +#undef DBG_END +#undef BREAK + +#define DBG(...) +#define DBGL(...) +#define DBG_END() +#define BREAK() +#endif + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MBED_ControlBus.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,136 @@ +/* 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" + + +/** 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 +*/ +MBED_ControlBus::MBED_ControlBus(PinName WR, PinName RD, PinName DTR, PinName CDBUF, PinName CDINT) : + _WR(WR), // WR pin + _RD(RD), // RD pin + _DTR(DTR), // DTR pin + _CDBUF(CDBUF), // CDBUF pin + _CDINT(CDINT){ // CDINT 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 + } +} + + +/** 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; +} + + +/** 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); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MBED_ControlBus.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,51 @@ +/* MBED_ControlBus - Use the MBED Port pins for controlling the Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _MBED_CONTROLBUS_H +#define _MBED_CONTROLBUS_H + +//Enums for Control Bus +#include "BusEnums.h" + +/** 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 +*/ +class MBED_ControlBus { +public: + MBED_ControlBus(PinName WR, PinName RD, PinName DTR, PinName CDBUF, PinName CDINT); + void WR (Bit_Level wr_level); + void RD (Bit_Level rd_level); + void busdir (Bus_Dir buf_dir); + void busctrl (Bus_Ena buf_ena, Bus_Dir buf_dir); + Bit_Level CDINT (); + +protected: + DigitalOut _WR; // Write pin + DigitalOut _RD; // Read pin + DigitalOut _DTR; // Databuffer Transmit/Receive direction pin + DigitalOut _CDBUF; // Databuffer enable pin + +//Plain digital input pin + DigitalIn _CDINT; // Keyboard interrupt pin + +//Plain digital interrupt pin +// InterruptIn _CDINT; // Keyboard interrupt pin + +//Debounced and Edge detected input pin (Andy Kirkmans's Lib) +// PinDetect _CDINT; // Keyboard interrupt pin + + bool _CDINT_detected; + + void _init(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_AddressBus.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,40 @@ +/* PCF8574_AddressBus - Use the PCF8574 I2C Port Extender for controlling the Address 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 "PCF8574_AddressBus.h" + +/** Create an PCF8574_AddressBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +PCF8574_AddressBus::PCF8574_AddressBus(I2C &i2c, char deviceAddress) : _i2c(i2c) { + _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write + _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read + _init(); +} + +/** Optimised AddressBus write operation. + * @param address the addressvalue to output on the bus +*/ +void PCF8574_AddressBus::write(char address) { + char data[1]; + + data[0] = address; + _i2c.write(_writeOpcode, data, 1); // Write addressvalue to bus +} + + +/** Init PCF8574_AddressBus + * @param + * @returns + */ +void PCF8574_AddressBus::_init() { + + write(0x00); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_AddressBus.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,41 @@ +/* PCF8574_AddressBus - Use the PCF8574 I2C Port Extender for controlling the Address Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _PCF8574_ADDRESSBUS_H +#define _PCF8574_ADDRESSBUS_H + + +//Pin Defines for PCF8574 Address Bus +#define D_A0 0x01 +#define D_A1 0x02 +#define D_A2 0x04 +#define D_A3 0x08 +#define D_A4 0x10 +#define D_A5 0x20 +#define D_A6 0x40 +#define D_A7 0x80 + +#define D_ADDR_MSK 0x3F + + +/** Create an PCF8574_AddressBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +class PCF8574_AddressBus { +public: + PCF8574_AddressBus(I2C &i2c, char deviceAddress); + void write(char address); +protected: + I2C &_i2c; + char _readOpcode; + char _writeOpcode; + void _init(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_DataBus.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,75 @@ +/* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data 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 "PCF8574_DataBus.h" + +/** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +PCF8574_DataBus::PCF8574_DataBus(I2C &i2c, char deviceAddress) : _i2c(i2c) { + _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write + _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read + _init(); +} + +/** Optimised DataBus write operation. + * @param byte the datavalue to output on the bus +*/ +void PCF8574_DataBus::write(char byte) { + char data[1]; + + data[0] = byte; + _i2c.write(_writeOpcode, data, 1); // Write datavalue to bus +} + +/** Optimised DataBus read operation. + * + * @returns current data from Databus +*/ +char PCF8574_DataBus::read() { + char data[1]; + +//Make sure that databus is enabled for Read +// data[0] = 0xFF; // Init Port for datainput by Writing 0xFF +// _i2c.write(_writeOpcode, data, 1); // Write to bus + + _i2c.read(_readOpcode, data, 1); // Read data from bus + + return data[0]; +} + + +/** Enable databus for Write or Read + * + * @param Bus_Dir bus_dir +*/ +void PCF8574_DataBus::busdir (Bus_Dir bus_dir) { + + if (bus_dir == READ) { + // Make sure that databus is enabled for READ + write(0xFF); // Init Port as input by Writing 0xFF + + } + else { + // Make sure that databus is enabled for WRITE + write(0xFF); // Not really needed, just Init Port to safe settings + } +} + + + +/** Init PCF8574_DataBus + * @param + * @returns + */ +void PCF8574_DataBus::_init() { + + busdir(WRITE); // Init Port as output +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_DataBus.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,45 @@ +/* PCF8574_DataBus - Use the PCF8574 I2C Port Extender for controlling the Data Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _PCF8574_DATABUS_H +#define _PCF8574_DATABUS_H + +//Pin Defines for PCF8574 Data Bus +#define D_D0 0x01 +#define D_D1 0x02 +#define D_D2 0x04 +#define D_D3 0x08 +#define D_D4 0x10 +#define D_D5 0x20 +#define D_D6 0x40 +#define D_D7 0x80 + +#define D_DATA_MSK 0xFF + +//Enums for Data Bus +#include "BusEnums.h" + + +/** Create an PCF8574_DataBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +class PCF8574_DataBus { +public: + PCF8574_DataBus(I2C &i2c, char deviceAddress); + char read(); + void write(char byte); + void busdir (Bus_Dir bus_dir); +protected: + I2C &_i2c; + char _readOpcode; + char _writeOpcode; + void _init(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_EnableBus.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,130 @@ +/* PCF8574_EnableBus - Use the PCF8574 I2C Port Extender for controlling the Chip Enable 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 "PCF8574_EnableBus.h" + + +/** Create an PCF8574_EnableBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +PCF8574_EnableBus::PCF8574_EnableBus(I2C &i2c, char deviceAddress) : _i2c(i2c) { + _writeOpcode = deviceAddress & 0xFE; // low order bit = 0 for write + _readOpcode = deviceAddress | 0x01; // low order bit = 1 for read + _init(); +} + + +/** Set or Reset Chip Select pins on Enable Bus + * + * @param CS_Pin cs_pin + * @param Bit_Level cs_level +*/ +void PCF8574_EnableBus::chipselect (CS_Pin cs_pin, Bit_Level cs_level) { + int result = 1; + + switch (cs_pin) { + case CS_SWITCH : if (cs_level == LOW) + _enable_bus = ~D_CS_SWITCH; // CS Pin Low, make sure that only one CS is active + else + _enable_bus |= D_CS_SWITCH; // CS Pin High + break; + case LATCHEN_1 : if (cs_level == LOW) + _enable_bus = ~D_LATCHEN_1; // CS Pin Low, make sure that only one CS is active + else + _enable_bus |= D_LATCHEN_1; // CS Pin High + break; + case LATCHEN_2 : if (cs_level == LOW) + _enable_bus = ~D_LATCHEN_2; // CS Pin Low, make sure that only one CS is active + else + _enable_bus |= D_LATCHEN_2; // CS Pin High + break; + case CS_BRIGHT : if (cs_level == LOW) + _enable_bus = ~D_CS_BRIGHT; // CS Pin Low, make sure that only one CS is active + else + _enable_bus |= D_CS_BRIGHT; // CS Pin High + break; + case CS_DISP : if (cs_level == LOW) + _enable_bus = ~D_CS_DISP; // CS Pin Low, make sure that only one CS is active + else + _enable_bus |= D_CS_DISP; // CS Pin High + break; + + default: // Oops, we should never end up here.... + result = -1; + } + + _write(); // Write chip enable bits to bus +} + +/** Set or Clear the Reset pin on Enable Bus + * + * @param Bit_Level rst_level +*/ +void PCF8574_EnableBus::reset (Bit_Level rst_level) { + + if (rst_level == LOW) { + _reset_pin = 0x00; // Reset Pin Low + } + else { + _reset_pin = D_RESET; // Reset Pin High + } + + _write(); // Write RST bit to bus +} + + +/** Set or Clear the NoGo pin on Enable Bus + * + * @param Bit_Level nogo_level +*/ +void PCF8574_EnableBus::nogo (Bit_Level nogo_level) { + + if (nogo_level == LOW) { + _nogo_pin = 0x00; // NOGO Pin Low + } + else { + _nogo_pin = D_NOGO; // NOGO Pin High + } + + _write(); // Write NoGo bit to bus +} + + +/** Optimised EnableBus write operation. + * @param byte the value to output on the bus +*/ +void PCF8574_EnableBus::_write(char byte) { + char data[1]; + + data[0] = byte; + _i2c.write(_writeOpcode, data, 1); // Write value to bus +} + +/** Optimised EnableBus write operation. + * @param +*/ +void PCF8574_EnableBus::_write() { + char data[1]; + + data[0] = (_enable_bus & D_ENABLE_MSK) | _reset_pin | _nogo_pin; // Combine enable bits and control bits + _i2c.write(_writeOpcode, data, 1); // Write value to bus +} + + +/** Init PCF8574_EnableBus + * @param + * @returns + */ +void PCF8574_EnableBus::_init() { + _enable_bus = 0xFF; // Make sure that all CS pins are disabled + _reset_pin = D_RESET; // Make sure that Reset pin is disabled + _nogo_pin = D_NOGO; // Make sure that NoGo pin is disabled + _write(); // Write value to bus +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PCF8574_EnableBus.h Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,53 @@ +/* PCF8574_EnableBus - Use the PCF8574 I2C Port Extender for controlling the Chip Enable Bus + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#ifndef _PCF8574_ENABLEBUS_H +#define _PCF8574_ENABLEBUS_H + +//Pin Defines for PCF8574 Enable Bus +//Note: 'Reset' causes all devices on the 'Control & Display Unit' to be reset! +#define D_CS_SWITCH 0x01 +#define D_LATCHEN_1 0x02 +#define D_LATCHEN_2 0x04 +#define D_CS_BRIGHT 0x08 +#define D_CS_DISP 0x10 +#define D_CS_SPARE 0x20 +#define D_RESET 0x40 +#define D_NOGO 0x80 + +#define D_ENABLE_MSK 0x3F + +//Enums for Enable Bus +#include "BusEnums.h" +enum CS_Pin { CS_SWITCH, LATCHEN_1, LATCHEN_2, CS_BRIGHT, CS_DISP, CS_SPARE }; + + +/** Create an PCF8574_EnableBus object connected to the specified I2C object and using the specified deviceAddress + * + * @param I2C &i2c the I2C port to connect to + * @param char deviceAddress the address of the PCF8574 +*/ +class PCF8574_EnableBus { +public: + PCF8574_EnableBus(I2C &i2c, char deviceAddress); + void chipselect (CS_Pin cs_pin, Bit_Level cs_level); + void reset (Bit_Level rst_level); + void nogo (Bit_Level nogo_level); +protected: + I2C &_i2c; + char _readOpcode; + char _writeOpcode; + char _enable_bus; + char _reset_pin; + char _nogo_pin; +// char _read(); + void _write(); + void _write(char byte); + void _init(); +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,278 @@ +/* mbed bus - Main + * Copyright (c) 2011 Wim Huiskamp + * + * Released under the MIT License: http://mbed.org/license/mit + * + * version 0.2 Initial Release +*/ +#include "mbed.h" +#include "BusDefines.h" +#include "PCF8574_DataBus.h" +#include "PCF8574_AddressBus.h" +#include "PCF8574_EnableBus.h" +#include "MBED_ControlBus.h" + +// Debug stuff +#define __DEBUG +#include "Dbg.h" + +// mbed Interface Hardware definitions +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut heartbeatLED(LED4); + +// Host PC Communication channels +Serial pc(USBTX, USBRX); + +//I2C Bus +I2C i2c(D_SDA, D_SCL); + +// Bus Interface Hardware definitions +PCF8574_DataBus databus = PCF8574_DataBus(i2c, D_I2C_DATA_BUS); //Copy constructors.. +PCF8574_AddressBus addressbus = PCF8574_AddressBus(i2c, D_I2C_ADDR_BUS); +PCF8574_EnableBus enablebus = PCF8574_EnableBus(i2c, D_I2C_ENA_BUS); +MBED_ControlBus controlbus = MBED_ControlBus(D_WR, D_RD, D_DTR, D_CDBUF, D_CDINT); + +// Dummy delay +#define DEVICE_WAIT_MS 0 + + +// Variables for Heartbeat and Status monitoring +Ticker heartbeat; +bool heartbeatflag=false; + +// Cycle Timer +Timer cycletimer; +int cyclecount = 0; +const int maxcount = 10; + +// Local functions +void clear_screen() { +//ANSI Terminal Commands + pc.printf("\x1B[2J"); + pc.printf("\x1B[H"); +} + + +void init_interfaces() { +// Init Host PC communication, default is 9600 + pc.baud(D_BAUDRATE); + +// Init I/F hardware + i2c.frequency(100000); + + //Done, Tell me about it + myled1 = 1; + DBG("Init Interfaces Done\r"); +} + + +// Heartbeat monitor +void pulse() { + heartbeatLED = !heartbeatLED; +} + +void heartbeat_start() { + heartbeat.attach(&pulse, 0.5); + heartbeatflag = true; +} + +void heartbeat_stop() { + heartbeat.detach(); + heartbeatflag = false; +} + +void show_LEDS () { + static int state = 0; + + switch (state) { + case 0: + myled1 = 1; + myled2 = 0; + myled3 = 0; + state = 1; + break; + case 1: + myled1 = 0; + myled2 = 1; + myled3 = 0; + state = 2; + break; + case 2: + myled1 = 0; + myled2 = 0; + myled3 = 1; + state = 0; + break; + } +} + + +// The next two functions are examples of low-level reading and writing to a device that is connected on the mbed bus. +// In your own application you can develop a Class for each specific slave device and include modified versions of the +// functions below as 'private' functions. This allows you to hardcode the device CS_pin signals, define specific delays +// when needed, change the sequence of CS, WR etc or mask out certain address or databits when they are not used in a certain case. +// + +/*---------------------------------------------------------------------------*\ + | + | Function: write + | + | Description: Low level data write routine for device. Takes in data + | and address and CS pin to identify the device and writes + | data to the display. For simplicity, entire address byte + | is written, even though top two bits are unused inputs. + | After performing the operation, address lines are set + | all high, in order to eliminate current drain through + | pullup resistors (0.5mA per pin with 10K pullups) + | + | Parameters: address - full address in bits 0-5 + | device - enum CS_Pin for Chip Select pin + | data - data byte to write out + | + | Returns: Nothing. + | +\*---------------------------------------------------------------------------*/ + +void write(uint8_t address, CS_Pin device, uint8_t data) +{ +// // Switch databus buffer to outputs (note: this is the default state) +// controlbus.busdir(WRITE); +// // Switch databus to outputs +// databus.busdir(WRITE); + + + // Write out the address on to the addressbus and wait + addressbus.write(address); + wait_ms(DEVICE_WAIT_MS); + + // Set CE low and wait + enablebus.chipselect(device, LOW); + wait_ms(DEVICE_WAIT_MS); + + // Write data to the databus + databus.write(data); + wait_ms(DEVICE_WAIT_MS); + + // Set WR low, wait, then set high and wait + controlbus.WR(LOW); + wait_ms(DEVICE_WAIT_MS); + controlbus.WR(HIGH); + wait_ms(DEVICE_WAIT_MS); + + // Set CE high and wait + enablebus.chipselect(device, HIGH); + wait_ms(DEVICE_WAIT_MS); + +// // Switch databus back to inputs +// databus.busdir(READ); +// // Switch databus buffer back to inputs +// controlbus.busdir(READ); + +// // Set address lines all high to minimise power through pullups +// addressbus.write(0xFF); +} + +/*---------------------------------------------------------------------------*\ + | + | Function: read + | + | Description: Low level data read routine for a Device. Takes in + | address and CS pin to identify the device and then + | reads data from the device. + | After performing the operation, address lines are set + | all high, in order to eliminate current drain through + | pullup resistors (0.5mA per pin with 10K pullups) + | + | Parameters: address - 8 bit address + | device - enum CS_Pin for Chip Select pin + | Returns: data - data byte read + | +\*---------------------------------------------------------------------------*/ + +uint8_t read(uint8_t address, CS_Pin device) +{ + uint8_t data = 0; + + // Switch databus to inputs (default state is output) + databus.busdir(READ); + // Switch databus buffer to inputs + controlbus.busdir(READ); + + // Write out the address on to the addressbus and wait + addressbus.write(address); + wait_ms(DEVICE_WAIT_MS); + + // Set CE low and wait + enablebus.chipselect(device, LOW); + wait_ms(DEVICE_WAIT_MS); + + // Set RD low and wait + controlbus.RD(LOW); + wait_ms(DEVICE_WAIT_MS); + + // Read the data byte from databus + data = databus.read(); + + // set RD high and wait + controlbus.RD(HIGH); + wait_ms(DEVICE_WAIT_MS); + + // Set CE high and wait + enablebus.chipselect(device, HIGH); + wait_ms(DEVICE_WAIT_MS); + +// // Set address lines all high to minimise power through pullups +// addressbus.write(0xFF); + + // Switch databus buffer back to outputs + controlbus.busdir(WRITE); + // Switch databus to outputs + databus.busdir(WRITE); + + // Return read data to caller + return data; +} + +int main() { + int address, result; + uint8_t data, dummy; + + init_interfaces(); + + heartbeat_start(); + + clear_screen(); + + DBG("Start Main Loop\r"); + +//Testing stuff + + //Test cycletime + cycletimer.start(); + cycletimer.reset(); + + while (1) { + for (address=0; address<256; address++) { + //data = read(address, CS_SWITCH); + + dummy = ~address; + write(address, LATCHEN_1, dummy); +// wait(0.05); + } + + // Just for Info, lets see how fast this cycle is... + cyclecount++; + if (cyclecount == maxcount) { + pc.printf("Freq = %d Hz\r", (cyclecount * 256 * 1000) / cycletimer.read_ms()); + cyclecount = 0; + cycletimer.reset(); + } + + show_LEDS (); + } + + + DBG("I'll be back...\r\r"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Aug 31 19:45:31 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912