SSD1306 OLED display driver  1.5.0
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
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 
43 static void ssd1306_spiConfigure_avr()
44 {
45  DDR_SPI &= ~((1<<DD_MOSI)|(1<<DD_MISO)|(1<<DD_SS)|(1<<DD_SCK));
46  /* Define the following pins as output */
47  DDR_SPI |= ((1<<DD_MOSI)|(1<<DD_SS)|(1<<DD_SCK));
48  SPCR = ((1<<SPE)| // SPI Enable
49  (0<<SPIE)| // SPI Interupt Enable
50  (0<<DORD)| // Data Order (0:MSB first / 1:LSB first)
51  (1<<MSTR)| // Master/Slave select
52  (0<<SPR1)|(1<<SPR0)| // SPI Clock Rate
53  (0<<CPOL)| // Clock Polarity (0:SCK low / 1:SCK hi when idle)
54  (0<<CPHA)); // Clock Phase (0:leading / 1:trailing edge sampling)
55  SPSR = (1<<SPI2X); // Double Clock Rate
56 }
57 
58 static void ssd1306_spiClose_avr()
59 {
60 }
61 
62 static void ssd1306_spiStart_avr()
63 {
64  if (s_ssd1306_cs >= 0)
65  {
66  digitalWrite(s_ssd1306_cs,LOW);
67  }
68 }
69 
70 static void ssd1306_spiSendByte_avr(uint8_t data)
71 {
72  SPDR = data;
73  while((SPSR & (1<<SPIF))==0);
74 }
75 
76 static void ssd1306_spiStop_avr()
77 {
78  if (s_ssd1306_cs >= 0)
79  {
80  digitalWrite(s_ssd1306_cs, HIGH);
81  }
83  {
84  digitalWrite(s_ssd1306_dc, LOW);
85  ssd1306_spiSendByte_avr( 0x00 ); // Send NOP command to allow last data byte to pass (bug in PCD8544?)
86  // ssd1306 E3h is NOP command
87  }
88 }
89 
90 void ssd1306_spiInit_avr(int8_t cesPin, int8_t dcPin)
91 {
92  if (cesPin >=0) pinMode(cesPin, OUTPUT);
93  if (dcPin >= 0) pinMode(dcPin, OUTPUT);
94  if (cesPin) s_ssd1306_cs = cesPin;
95  if (dcPin) s_ssd1306_dc = dcPin;
97  ssd1306_spiConfigure_avr();
98  ssd1306_startTransmission = ssd1306_spiStart_avr;
99  ssd1306_endTransmission = ssd1306_spiStop_avr;
100  ssd1306_sendByte = ssd1306_spiSendByte_avr;
101  ssd1306_closeInterface = ssd1306_spiClose_avr;
104 }
105 
106 #endif
107 
108 
uint8_t ssd1306_dcQuickSwitch
int8_t s_ssd1306_dc
Definition: ssd1306_spi.c:36
void(* ssd1306_dataStart)(void)
void ssd1306_spiCommandStart()
Definition: ssd1306_spi.c:53
void(* ssd1306_closeInterface)(void)
deinitializes internal resources, allocated for interface.
void(* ssd1306_endTransmission)(void)
void(* ssd1306_startTransmission)(void)
uint8_t g_lcd_type
void ssd1306_spiDataStart()
Definition: ssd1306_spi.c:59
void(* ssd1306_sendByte)(uint8_t data)
void(* ssd1306_commandStart)(void)
int8_t s_ssd1306_cs
Definition: ssd1306_spi.c:35