SSD1306 OLED display driver 1.8.2 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
33 #include <util/delay_basic.h>
34
41 static uint8_t s_scl = (1<<SSD1306_SCL);
42 static uint8_t s_sda = (1<<SSD1306_SDA);
43 static uint8_t s_sa = SSD1306_SA;
44
45 #if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
46 // at 8Mhz each command takes ~ 0.125us
47 #define DDR_REG DDRB
48 #define PORT_REG PORTB
49 #elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
50 // For AttinyX4 controllers
51 // at 8Mhz each command takes ~ 0.125us
52 #define DDR_REG DDRA
53 #define PORT_REG PORTA
54 #else // For Atmega
55 // at 16Mhz each command takes ~ 0.0625us
56 #define DDR_REG DDRC
57 #define PORT_REG PORTC
58 #endif
59
60
61 #ifndef F_CPU
62 #warning "F_CPU is not defined, there can be I2C issues"
63 #define F_CPU 8000000
64 #endif
65 #define CPU_CYCLE_NS (1000000000/F_CPU)
66
67 #define DELAY_LOOP_CYCLES 4
68 #define ssd1306_delay(x) _delay_loop_2(x)
69
73 #define SSD1306_I2C_START_STOP_DELAY 600
74 #define SSD1306_I2C_RISE_TIME 300
75 #define SSD1306_I2C_FALL_TIME 300
76 #define SSD1306_I2C_DATA_HOLD_TIME 300
77 #define SSD1306_I2C_IDLE_TIME 1300
78 #define SSD1306_I2C_CLOCK 2500
79
80
81 #define I2C_START_STOP_DELAY ((SSD1306_I2C_START_STOP_DELAY/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
82
83 #define I2C_RISE_TIME ((SSD1306_I2C_RISE_TIME/CPU_CYCLE_NS)/DELAY_LOOP_CYCLES)
84
85 #define I2C_DATA_HOLD_TIME ((SSD1306_I2C_DATA_HOLD_TIME/CPU_CYCLE_NS + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
86
87 #define I2C_IDLE_TIME (((SSD1306_I2C_IDLE_TIME/CPU_CYCLE_NS) + DELAY_LOOP_CYCLES/2)/DELAY_LOOP_CYCLES)
88
89 #define I2C_HALF_CLOCK (((SSD1306_I2C_CLOCK - SSD1306_I2C_FALL_TIME - SSD1306_I2C_RISE_TIME - SSD1306_I2C_FALL_TIME)/CPU_CYCLE_NS/2 \
90 )/DELAY_LOOP_CYCLES)
91
92
93 /* I2C HIGH = PORT as INPUT(0) and PULL-UP ENABLE (1) */
94 //#define DIGITAL_WRITE_HIGH(DREG, PREG, BIT) { DREG &= ~(1 << BIT); PREG |= (1 << BIT); }
95 #define DIGITAL_WRITE_HIGH(DREG, PREG, BIT) { DREG &= ~BIT; PREG |= BIT; }
96
97 /* I2C LOW = PORT as OUTPUT(1) and OUTPUT LOW (0) */
98 //#define DIGITAL_WRITE_LOW(DREG, PREG, BIT) { DREG |= (1 << BIT); PREG &= ~(1 << BIT); }
99 #define DIGITAL_WRITE_LOW(DREG, PREG, BIT) { DREG |= BIT; PREG &= ~BIT; }
100
101 static uint8_t oldSREG;
102 static uint8_t interruptsOff = 0;
103
108 static void ssd1306_i2cSendByte_Embedded(uint8_t data)
109 {
110 uint8_t i;
111 for(i=8; i>0; i--)
112 {
113 if(data & 0x80)
114 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda)
115 else
116 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda);
117 data<<=1;
118 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
119
120 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
121 ssd1306_delay(I2C_HALF_CLOCK);
122
123 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
124 ssd1306_delay(I2C_HALF_CLOCK);
125 }
126 // generating confirmation impulse
127 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda);
128 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
129 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl);
130 ssd1306_delay(I2C_HALF_CLOCK);
131 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl);
132 ssd1306_delay(I2C_HALF_CLOCK);
133 }
134
135 static void ssd1306_i2cSendBytes_Embedded(const uint8_t *buffer, uint16_t size)
136 {
137 while (size--)
138 {
139 ssd1306_i2cSendByte_Embedded(*buffer);
140 buffer++;
141 }
142 }
143
147 static void ssd1306_i2cStart_Embedded(void)
148 {
149 oldSREG = SREG;
150 cli();
151 interruptsOff = 1;
152 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda); // Set to LOW
153 ssd1306_delay(I2C_START_STOP_DELAY);
154 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_scl); // Set to LOW
155 ssd1306_delay(I2C_HALF_CLOCK);
156 ssd1306_i2cSendByte_Embedded((s_sa << 1) | 0);
157 }
158
159 static void ssd1306_i2cStop_Embedded(void)
160 {
161 DIGITAL_WRITE_LOW(DDR_REG, PORT_REG, s_sda); // Set to LOW
162 ssd1306_delay(I2C_RISE_TIME); // Fall time is the same as rise time
163 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_scl); // Set to HIGH
164 ssd1306_delay(I2C_START_STOP_DELAY);
165 DIGITAL_WRITE_HIGH(DDR_REG, PORT_REG, s_sda); // Set to HIGH
166 ssd1306_delay(I2C_IDLE_TIME);
167 if (interruptsOff)
168 {
169 SREG = oldSREG;
170 interruptsOff = 0;
171 }
172 }
173
174 static void ssd1306_i2cClose_Embedded()
175 {
176 }
177
178 void ssd1306_i2cInit_Embedded(int8_t scl, int8_t sda, uint8_t sa)
179 {
180 if (scl>=0) s_scl = (1<<scl);
181 if (sda>=0) s_sda = (1<<sda);
182 if (sa) s_sa = sa;
183 ssd1306_intf.spi = 0;
184 ssd1306_intf.start = ssd1306_i2cStart_Embedded;
185 ssd1306_intf.stop = ssd1306_i2cStop_Embedded;
186 ssd1306_intf.send = ssd1306_i2cSendByte_Embedded;
187 ssd1306_intf.send_buffer = ssd1306_i2cSendBytes_Embedded;
188 ssd1306_intf.close = ssd1306_i2cClose_Embedded;
189 }
190
191 #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:31
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:178
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