SSD1306 OLED display driver  1.8.2
This library is developed to control SSD1306/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
ssd1306_spi_avr.c
1 /*
2  MIT License
3 
4  Copyright (c) 2018-2019, 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_spi_avr.h"
26 #include "ssd1306_spi.h"
27 
28 #include "intf/ssd1306_interface.h"
29 #include "lcd/lcd_common.h"
30 #include "ssd1306_hal/io.h"
31 
32 #if defined(CONFIG_AVR_SPI_AVAILABLE) && defined(CONFIG_AVR_SPI_ENABLE)
33 
34 #include <stdlib.h>
35 
36 #define PORT_SPI PORTB
37 #define DDR_SPI DDRB
38 
39 #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
40  #define DD_MISO DDB3
41  #define DD_MOSI DDB2
42  #define DD_SCK DDB1
43  #define DD_SS DDB0
44 #else
45  #define DD_MISO DDB4
46  #define DD_MOSI DDB3
47  #define DD_SCK DDB5
48  #define DD_SS DDB2
49 #endif
50 
51 #define SPI_CLOCK_MASK 0x03
52 #define SPI_2XCLOCK_MASK 0x01
53 
54 extern uint32_t s_ssd1306_spi_clock;
55 
56 static void ssd1306_spiConfigure_avr()
57 {
58  uint8_t clockDiv;
59  uint32_t clockSetting = F_CPU / 2;
60  clockDiv = 0;
61  while ((clockDiv < 6) && (s_ssd1306_spi_clock < clockSetting))
62  {
63  clockSetting /= 2;
64  clockDiv++;
65  }
66  if (clockDiv == 6)
67  {
68  clockDiv = 7;
69  }
70  // Invert the SPI2X bit
71  clockDiv ^= 0x1;
72 
73  // SS pin must be HIGH, when enabling MASTER SPI mode
74  // Otherwise, SPI will drop automatically to SLAVE mode
75  DDR_SPI &= ~((1<<DD_SCK)|(1<<DD_SS)|(1<<DD_MOSI));
76  PORT_SPI |= (1<<DD_SS);
77  /* Define the following pins as output */
78  DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SCK)|(1<<DD_SS));
79  PORT_SPI |= (1<<DD_SS);
80 
81  SPCR = (1<<SPE)|(1<<MSTR)|(0<<CPOL)|(0<<CPHA)|
82  ((clockDiv >> 1) & SPI_CLOCK_MASK);
83  SPSR = clockDiv & SPI_2XCLOCK_MASK; // Double Clock Rate
84  // Wait for some time to give SPI HW module to initialize
85  delay(10);
86 }
87 
88 static void ssd1306_spiClose_avr()
89 {
90 }
91 
92 static void ssd1306_spiStart_avr()
93 {
94  if (s_ssd1306_cs >= 0)
95  {
96  digitalWrite(s_ssd1306_cs,LOW);
97  }
98 }
99 
100 static void ssd1306_spiSendByte_avr(uint8_t data)
101 {
102  SPDR = data;
103  asm volatile("nop");
104  while((SPSR & (1<<SPIF))==0);
105 }
106 
107 static void ssd1306_spiSendBytes_avr(const uint8_t * buffer, uint16_t size)
108 {
109  while (size--)
110  {
111  SPDR = *buffer;
112  asm volatile("nop"); // to improve speed
113  while((SPSR & (1<<SPIF))==0);
114  SPDR; // read SPI input
115  buffer++;
116  }
117 }
118 
119 static void ssd1306_spiStop_avr()
120 {
122  {
123  digitalWrite(s_ssd1306_dc, LOW);
124  ssd1306_spiSendByte_avr( 0x00 ); // Send NOP command to allow last data byte to pass (bug in PCD8544?)
125  // ssd1306 E3h is NOP command
126  }
127  if (s_ssd1306_cs >= 0)
128  {
129  digitalWrite(s_ssd1306_cs, HIGH);
130  }
131 }
132 
133 void ssd1306_spiInit_avr(int8_t cesPin, int8_t dcPin)
134 {
135  if (cesPin >=0) pinMode(cesPin, OUTPUT);
136  if (dcPin >= 0) pinMode(dcPin, OUTPUT);
137  if (cesPin) s_ssd1306_cs = cesPin;
138  if (dcPin) s_ssd1306_dc = dcPin;
139  ssd1306_intf.spi = 1;
140  ssd1306_spiConfigure_avr();
141  ssd1306_intf.start = ssd1306_spiStart_avr;
142  ssd1306_intf.stop = ssd1306_spiStop_avr;
143  ssd1306_intf.send = ssd1306_spiSendByte_avr;
144  ssd1306_intf.send_buffer = ssd1306_spiSendBytes_avr;
145  ssd1306_intf.close = ssd1306_spiClose_avr;
146 }
147 
148 #endif
149 
150 
int8_t s_ssd1306_dc
Definition: ssd1306_spi.c:34
void(* send)(uint8_t data)
uint32_t s_ssd1306_spi_clock
Definition: ssd1306_spi.c:35
void ssd1306_spiInit_avr(int8_t cesPin, int8_t dcPin)
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:33
ssd1306_interface_t ssd1306_intf
void(* send_buffer)(const uint8_t *buffer, uint16_t size)
Sends bytes to SSD1306 device.
lcd_type_t type
Definition: lcd_common.h:91
int8_t s_ssd1306_cs
Definition: ssd1306_spi.c:33