SSD1306 OLED display driver  1.4.0
This library is developed to control SSD1306 i2c/spi OLED display
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_conf.h"
28 #include "ssd1306_i2c.h"
29 
30 #ifdef SSD1306_TWI_SUPPORTED
31 
32 #include <avr/io.h>
33 #include <util/twi.h>
34 
35 /* Max i2c frequency, supported by OLED controllers */
36 #define SSD1306_TWI_FREQ 400000
37 #define MAX_RETRIES 64
38 
39 static uint8_t s_sa = SSD1306_SA;
40 
41 static uint8_t ssd1306_twi_start(void)
42 {
43  uint8_t twst;
44  uint8_t iters = MAX_RETRIES;
45  do
46  {
47  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
48  while ( (TWCR & (1<<TWINT)) == 0 );
49  twst = TWSR & 0xF8;
50  if (!--iters);
51  {
52  break;
53  }
54  } while (twst == TW_MT_ARB_LOST);
55  if ((twst != TW_START) && (twst != TW_REP_START))
56  {
57  return twst;
58  }
59  return 0;
60 }
61 
62 static uint8_t ssd1306_twi_send(uint8_t data)
63 {
64  uint8_t twsr;
65  uint8_t iters = MAX_RETRIES;
66  do
67  {
68  TWDR = data;
69  TWCR = (1<<TWINT) | (1<<TWEN);
70  while ( (TWCR & (1<<TWINT)) == 0 );
71  twsr = TWSR & 0xF8;
72  if ((twsr == TW_MT_SLA_ACK) || (twsr == TW_MT_DATA_ACK))
73  {
74  return 0;
75  }
76  if (twsr == TW_MT_ARB_LOST)
77  {
78  return twsr;
79  }
80  iters++;
81  if (!--iters)
82  {
83  break;
84  }
85  } while (twsr != TW_MT_ARB_LOST);
86  return twsr;
87 }
88 
89 
90 static void ssd1306_twi_stop(void)
91 {
92  TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
93 }
94 
95 void ssd1306_i2cStart_Twi(void)
96 {
97  do
98  {
99  if (ssd1306_twi_start() != 0)
100  {
101  /* Some serious error happened, but we don't care. Our API functions have void type */
102  return;
103  }
104  } while (ssd1306_twi_send(s_sa << 1) == TW_MT_ARB_LOST);
105 }
106 
107 void ssd1306_i2cStop_Twi(void)
108 {
109  ssd1306_twi_stop();
110 }
111 
112 void ssd1306_i2cConfigure_Twi(uint8_t arg)
113 {
114 #if defined(__AVR_ATmega328P__)
115  /* Enable internal pull-ups */
116  DDRC &= ~(1<<PINC4); PORTC |= (1<<PINC4);
117  DDRC &= ~(1<<PINC5); PORTC |= (1<<PINC5);
118 #endif
119 #if defined(TWPS0)
120  TWSR = 0;
121 #endif
122  TWBR = ((F_CPU / SSD1306_TWI_FREQ) - 16) / 2 / (1); // Always use prescaler 1 (TWSR 0x00)
123  TWCR = (1 << TWEN) | (1 << TWEA);
124 }
125 
126 void ssd1306_i2cSendByte_Twi(uint8_t data)
127 {
128  for(;;)
129  {
130  if (ssd1306_twi_send(data) != TW_MT_ARB_LOST)
131  {
132  break;
133  }
134  if (ssd1306_twi_start() != 0)
135  {
136  /* Some serious error happened, but we don't care. Our API functions have void type */
137  break;
138  }
139  if (ssd1306_twi_send(s_sa << 1) != TW_MT_ARB_LOST)
140  {
141  /* Some serious error happened, but we don't care. Our API functions have void type */
142  break;
143  }
144  }
145 }
146 
147 void ssd1306_i2cInit_Twi(uint8_t sa)
148 {
149  if (sa) s_sa = sa;
150  ssd1306_startTransmission = ssd1306_i2cStart_Twi;
151  ssd1306_endTransmission = ssd1306_i2cStop_Twi;
152  ssd1306_sendByte = ssd1306_i2cSendByte_Twi;
155 }
156 
157 #endif
158 
159 
void(* ssd1306_sendByte)(uint8_t data)
void(* ssd1306_dataStart)()
void(* ssd1306_endTransmission)()
void ssd1306_i2cCommandStart()
void ssd1306_i2cDataStart()
void(* ssd1306_startTransmission)()
void(* ssd1306_commandStart)()
#define SSD1306_SA