UniGraphic-Fork for ST7920-LCD-controller and SH1106. Tested with 128x64 LCD with SPI and 128x64-OLED with IIC

Dependents:   UniGraphic-St7920-Test AfficheurUTILECO

Fork of UniGraphic by GraphicsDisplay

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2C_bus.cpp Source File

I2C_bus.cpp

00001 /* mbed UniGraphic library - I2C protocol class
00002  * Copyright (c) 2017 Peter Drescher
00003  * Released under the MIT License: http://mbed.org/license/mit
00004  *
00005  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00006  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00007  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00008  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00009  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00010  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00011  * THE SOFTWARE.
00012  */
00013  
00014 #include "I2C_bus.h"
00015 
00016 I2C_bus::I2C_bus(int Hz, int address, PinName sda, PinName scl, PinName reset)
00017     : _i2c(sda,scl), _reset(reset)
00018 {
00019     _i2c.frequency(Hz);
00020     _address = address;
00021     _reset = 1;
00022     hw_reset();    
00023 }
00024 
00025 void I2C_bus::wr_cmd8(unsigned char cmd)
00026 {     
00027     char tmp[2];
00028     tmp[0] = 0x00;  //command 
00029     tmp[1] = cmd;
00030     _i2c.write(_address,tmp,2);
00031 }
00032 void I2C_bus::wr_data8(unsigned char data)
00033 {
00034     char tmp[2];
00035     tmp[0] = 0x40;  //data mode
00036     tmp[1] = data;
00037     _i2c.write(_address,tmp,2);    // write 
00038 }
00039 void I2C_bus::wr_cmd16(unsigned short cmd)
00040 {     
00041     char tmp[3];
00042     tmp[0] = 00; //command
00043     tmp[1] = cmd>>8;
00044     tmp[2] = cmd&0xFF;
00045     
00046     _i2c.write(_address,tmp,3);
00047 }
00048 void I2C_bus::wr_data16(unsigned short data)
00049 {
00050     _i2c.write(data>>8);    // write 8bit
00051     _i2c.write(data&0xFF);    // write 8bit
00052 }
00053 void I2C_bus::wr_gram(unsigned short data)
00054 {
00055     _i2c.write(data>>8);    // write 8bit
00056     _i2c.write(data&0xFF);    // write 8bit
00057 }
00058 void I2C_bus::wr_gram(unsigned short data, unsigned int count)
00059 {
00060     _i2c.start();
00061     _i2c.write(_address);
00062     _i2c.write(0x40);          // data continue
00063     if((data>>8)==(data&0xFF))
00064     {
00065         count<<=1;
00066         while(count)
00067         {
00068             _i2c.write(data);    // write 8bit
00069             count--;
00070         }
00071     }
00072     else
00073     {
00074         while(count)
00075         {
00076             _i2c.write(data>>8);    // write 8bit
00077             _i2c.write(data&0xFF);    // write 8bit
00078             count--;
00079         }
00080     }
00081    _i2c.stop();
00082 }
00083 void I2C_bus::wr_grambuf(unsigned short* data, unsigned int lenght)
00084 {
00085     _i2c.start();
00086     _i2c.write(_address);
00087     _i2c.write(0x40);          // data continue
00088     while(lenght)
00089     {
00090         //_i2c.write((*data)>>8);      // write high 8bit
00091         _i2c.write((*data)&0xFF);    // write low 8bit
00092         _i2c.write((*data)>>8);      // write high 8bit
00093         data++;
00094         lenght--;
00095     }
00096     _i2c.stop();
00097 }
00098 
00099 void I2C_bus::hw_reset()
00100 {
00101     wait_ms(15);
00102     _reset = 0;                        // display reset
00103     wait_ms(2);
00104     _reset = 1;                       // end reset
00105     wait_ms(100);
00106 }
00107 void I2C_bus::BusEnable(bool enable)
00108 {
00109 }
00110 
00111 void I2C_bus::reg_select(unsigned char reg, bool forread)
00112 {    
00113 }
00114 
00115 unsigned int I2C_bus::rd_reg_data32(unsigned char reg)
00116 {
00117      return 0;
00118 }
00119 
00120 unsigned int I2C_bus::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
00121 {
00122     return 0;
00123 }
00124 
00125 void I2C_bus::dummyread()
00126 {
00127 }
00128 
00129 unsigned short I2C_bus::rd_gram(bool convert)
00130 {
00131     return (0);    
00132 }
00133 
00134 unsigned short I2C_bus::reg_read(unsigned char reg)
00135 {
00136     return (0);
00137 }
00138 
00139 void I2C_bus::reg_write(unsigned char reg, unsigned short data)
00140 {
00141 }