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_twi.c
1 /*
2 MIT License
3
4 Copyright (c) 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_twi.h"
26 #include "intf/ssd1306_interface.h"
27 #include "ssd1306_i2c.h"
28
29 #if defined(CONFIG_TWI_I2C_AVAILABLE) && defined(CONFIG_TWI_I2C_ENABLE)
30
31 #include <avr/io.h>
32 #include <util/twi.h>
33
34 /* Max i2c frequency, supported by OLED controllers */
35 #define SSD1306_TWI_FREQ 400000
36 #define MAX_RETRIES 64
37
38 static uint8_t s_sa = SSD1306_SA;
39
40 static uint8_t ssd1306_twi_start(void)
41 {
42 uint8_t twst;
43 uint8_t iters = MAX_RETRIES;
44 do
45 {
46 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
47 while ( (TWCR & (1<<TWINT)) == 0 );
48 twst = TWSR & 0xF8;
49 if (!--iters)
50 {
51 break;
52 }
53 } while (twst == TW_MT_ARB_LOST);
54 if ((twst != TW_START) && (twst != TW_REP_START))
55 {
56 return twst;
57 }
58 return 0;
59 }
60
61 static uint8_t ssd1306_twi_send(uint8_t data)
62 {
63 uint8_t twsr;
64 uint8_t iters = MAX_RETRIES;
65 do
66 {
67 TWDR = data;
68 TWCR = (1<<TWINT) | (1<<TWEN);
69 while ( (TWCR & (1<<TWINT)) == 0 );
70 twsr = TWSR & 0xF8;
71 if ((twsr == TW_MT_SLA_ACK) || (twsr == TW_MT_DATA_ACK))
72 {
73 return 0;
74 }
75 if (twsr == TW_MT_ARB_LOST)
76 {
77 return twsr;
78 }
79 iters++;
80 if (!--iters)
81 {
82 break;
83 }
84 } while (twsr != TW_MT_ARB_LOST);
85 return twsr;
86 }
87
88
89 static void ssd1306_twi_stop(void)
90 {
91 TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
92 }
93
94 static void ssd1306_i2cStart_Twi(void)
95 {
96 do
97 {
98 if (ssd1306_twi_start() != 0)
99 {
100 /* Some serious error happened, but we don't care. Our API functions have void type */
101 return;
102 }
103 } while (ssd1306_twi_send(s_sa << 1) == TW_MT_ARB_LOST);
104 }
105
106 static void ssd1306_i2cStop_Twi(void)
107 {
108 ssd1306_twi_stop();
109 }
110
111 void ssd1306_i2cConfigure_Twi(uint8_t arg)
112 {
113 #if defined(__AVR_ATmega328P__)
114 /* Enable internal pull-ups */
115 DDRC &= ~(1<<PINC4); PORTC |= (1<<PINC4);
116 DDRC &= ~(1<<PINC5); PORTC |= (1<<PINC5);
117 #endif
118 #if defined(TWPS0)
119 TWSR = 0;
120 #endif
121 TWBR = ((F_CPU / SSD1306_TWI_FREQ) - 16) / 2 / (1); // Always use prescaler 1 (TWSR 0x00)
122 TWCR = (1 << TWEN) | (1 << TWEA);
123 }
124
125 static void ssd1306_i2cSendByte_Twi(uint8_t data)
126 {
127 for(;;)
128 {
129 if (ssd1306_twi_send(data) != TW_MT_ARB_LOST)
130 {
131 break;
132 }
133 if (ssd1306_twi_start() != 0)
134 {
135 /* Some serious error happened, but we don't care. Our API functions have void type */
136 break;
137 }
138 if (ssd1306_twi_send(s_sa << 1) != TW_MT_ARB_LOST)
139 {
140 /* Some serious error happened, but we don't care. Our API functions have void type */
141 break;
142 }
143 }
144 }
145
146 static void ssd1306_i2cSendBytes_Twi(const uint8_t *buffer, uint16_t size)
147 {
148 while (size--)
149 {
150 ssd1306_i2cSendByte_Twi(*buffer);
151 buffer++;
152 }
153 }
154
155
156 static void ssd1306_i2cClose_Twi()
157 {
158 }
159
160 void ssd1306_i2cInit_Twi(uint8_t sa)
161 {
162 if (sa) s_sa = sa;
163 ssd1306_intf.spi = 0;
164 ssd1306_intf.start = ssd1306_i2cStart_Twi;
165 ssd1306_intf.stop = ssd1306_i2cStop_Twi;
166 ssd1306_intf.send = ssd1306_i2cSendByte_Twi;
167 ssd1306_intf.send_buffer = ssd1306_i2cSendBytes_Twi;
168 ssd1306_intf.close = ssd1306_i2cClose_Twi;
169 }
170
171 #endif
172
173
ssd1306_i2cInit_Twi
void ssd1306_i2cInit_Twi(uint8_t sa)
Definition: ssd1306_i2c_twi.c:160
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
ssd1306_i2c.h
ssd1306_i2cConfigure_Twi
void ssd1306_i2cConfigure_Twi(uint8_t arg)
Definition: ssd1306_i2c_twi.c:111
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_i2c_twi.h
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