SSD1306 OLED display driver 1.7.8 This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays

ssd1306_i2c_embedded.c
1 /*
2 MIT License
3
4 Copyright (c) 2016-2018, Alexey Dynda
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "ssd1306_i2c_embedded.h"
26 #include "intf/ssd1306_interface.h"
27 #include "ssd1306_i2c.h"
28
29 #include "ssd1306_hal/io.h"
30
31 #if defined(CONFIG_SOFTWARE_I2C_AVAILABLE) && defined(CONFIG_SOFTWARE_I2C_ENABLE)
32
39 static uint8_t s_scl = (1<<SSD1306_SCL);
40 static uint8_t s_sda = (1<<SSD1306_SDA);
41 static uint8_t s_sa = SSD1306_SA;
42
43 #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
44 // at 8Mhz each command takes ~ 0.125us
45 #define DDR_REG DDRB
46 #define PORT_REG PORTB
47 #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
48 // For AttinyX4 controllers
49 // at 8Mhz each command takes ~ 0.125us
50 #define DDR_REG DDRA
51 #define PORT_REG PORTA
52 #else // For Atmega
53 // at 16Mhz each command takes ~ 0.0625us
54 #define DDR_REG DDRC
55 #define PORT_REG PORTC
56 #endif
57
58
59 #ifndef F_CPU
60 #warning "F_CPU is not defined, there can be I2C issues"
61 #define F_CPU 8000000
62 #endif
63 #define CPU_CYCLE_NS (1000000000/F_CPU)
64
65 // each delay loop takes 4 cycles: nop(1), dec(1), jnz(2)
66 #define DELAY_LOOP_CYCLES 4
67 #define ssd1306_delay(x) for(uint8_t i2=x; i2>0; i2--){__asm__("nop\n\t");}
68
72 #define SSD1306_I2C_START_STOP_DELAY 600
73 #define SSD1306_I2C_RISE_TIME 300
74 #define SSD1306_I2C_FALL_TIME 300
75 #define SSD1306_I2C_DATA_HOLD_TIME 300
76 #define SSD1306_I2C_IDLE_TIME 1300
77 #define SSD1306_I2C_CLOCK 2500
78
79
80 #define I2C_START_STOP_DELAY ((SSD1306_I2C_START_STOP_DELAY/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
81
82 #define I2C_RISE_TIME ((SSD1306_I2C_RISE_TIME/CPU_CYCLE_NS)/DELAY_LOOP_CYCLES)
83
84 #define I2C_DATA_HOLD_TIME ((SSD1306_I2C_DATA_HOLD_TIME/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
85
86 #define I2C_IDLE_TIME (((SSD1306_I2C_IDLE_TIME/CPU_CYCLE_NS) + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
87
88 #define I2C_HALF_CLOCK (((SSD1306_I2C_CLOCK - SSD1306_I2C_FALL_TIME - SSD1306_I2C_RISE_TIME - SSD1306_I2C_FALL_TIME)/CPU_CYCLE_NS/2 \
89 )/DELAY_LOOP_CYCLES)
90
91
92 /* I2C HIGH = PORT as INPUT(0) and PULL-UP ENABLE (1) */
93 //#define DIGITAL_WRITE_HIGH(DREG, PREG, BIT) { DREG &= ~(1 << BIT); PREG |= (1 << BIT); }
94 #define DIGITAL_WRITE_HIGH(DREG, PREG, BIT) { DREG &= ~BIT; PREG |= BIT; }
95
96 /* I2C LOW = PORT as OUTPUT(1) and OUTPUT LOW (0) */
97 //#define DIGITAL_WRITE_LOW(DREG, PREG, BIT) { DREG |= (1 << BIT); PREG &= ~(1 << BIT); }
98 #define DIGITAL_WRITE_LOW(DREG, PREG, BIT) { DREG |= BIT; PREG &= ~BIT; }
99
100 static uint8_t oldSREG;
101 static uint8_t interruptsOff = 0;
102
107 static void ssd1306_i2cSendByte_Embedded(uint8_t data)
108 {
109 uint8_t i;
110 for(i=8; i>0; i--)
111 {
112 if(data & 0x80)
113 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda)
114 else
115 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda);
116 data<<=1;
117 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
118
119 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
120 ssd1306_delay(I2C_HALF_CLOCK);
121
122 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
123 ssd1306_delay(I2C_HALF_CLOCK);
124 }
125 // generating confirmation impulse
126 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda);
127 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
128 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
129 ssd1306_delay(I2C_HALF_CLOCK);
130 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
131 ssd1306_delay(I2C_HALF_CLOCK);
132 }
133
134 static void ssd1306_i2cSendBytes_Embedded(const uint8_t *buffer, uint16_t size)
135 {
136 while (size--)
137 {
138 ssd1306_i2cSendByte_Embedded(*buffer);
139 buffer++;
140 }
141 }
142
146 static void ssd1306_i2cStart_Embedded(void)
147 {
148 oldSREG = SREG;
149 cli();
150 interruptsOff = 1;
151 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda); // Set to LOW
152 ssd1306_delay(I2C_START_STOP_DELAY);
153 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl); // Set to LOW
154 ssd1306_delay(I2C_HALF_CLOCK);
155 ssd1306_i2cSendByte_Embedded((s_sa << 1) | 0);
156 }
157
158 static void ssd1306_i2cStop_Embedded(void)
159 {
160 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda); // Set to LOW
161 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
162 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl); // Set to HIGH
163 ssd1306_delay(I2C_START_STOP_DELAY);
164 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda); // Set to HIGH
165 ssd1306_delay(I2C_IDLE_TIME);
166 if (interruptsOff)
167 {
168 SREG = oldSREG;
169 interruptsOff = 0;
170 }
171 }
172
173 static void ssd1306_i2cClose_Embedded()
174 {
175 }
176
177 void ssd1306_i2cInit_Embedded(int8_t scl, int8_t sda, uint8_t sa)
178 {
179 if (scl>=0) s_scl = (1<<scl);
180 if (sda>=0) s_sda = (1<<sda);
181 if (sa) s_sa = sa;
182 ssd1306_intf.spi = 0;
183 ssd1306_intf.start = ssd1306_i2cStart_Embedded;
184 ssd1306_intf.stop = ssd1306_i2cStop_Embedded;
185 ssd1306_intf.send = ssd1306_i2cSendByte_Embedded;
186 ssd1306_intf.send_buffer = ssd1306_i2cSendBytes_Embedded;
187 ssd1306_intf.close = ssd1306_i2cClose_Embedded;
188 }
189
190 #endif
ssd1306_interface_t::send
void(* send)(uint8_t data)
Definition: ssd1306_interface.h:70
ssd1306_interface_t::stop
void(* stop)(void)
Definition: ssd1306_interface.h:65
io.h
ssd1306_i2c.h
SSD1306_SDA
#define SSD1306_SDA
SDA, Pin 4 on SSD1306 Board.
Definition: ssd1306_i2c_conf.h:55
ssd1306_i2c_embedded.h
ssd1306_interface_t::start
void(* start)(void)
Definition: ssd1306_interface.h:61
ssd1306_interface_t::close
void(* close)(void)
deinitializes internal resources, allocated for interface.
Definition: ssd1306_interface.h:88
ssd1306_intf
ssd1306_interface_t ssd1306_intf
Definition: ssd1306_interface.c:29
SSD1306_SA
#define SSD1306_SA
Definition: ssd1306_i2c_conf.h:47
SSD1306_SCL
#define SSD1306_SCL
SCL, Pin 3 on SSD1306 Board.
Definition: ssd1306_i2c_conf.h:52
ssd1306_i2cInit_Embedded
void ssd1306_i2cInit_Embedded(int8_t scl, int8_t sda, uint8_t sa)
Definition: ssd1306_i2c_embedded.c:177
ssd1306_interface.h
ssd1306_interface_t::spi
uint8_t spi
Definition: ssd1306_interface.h:57
ssd1306_interface_t::send_buffer
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.
Definition: ssd1306_interface.h:80