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