SSD1306 OLED display driver  1.4.10
This library is developed to control SSD1306/SSD1331 RGB i2c/spi OLED displays and spi PCD8544 LED display
ssd1306_spi_usi.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_usi.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_USI_SPI_SUPPORTED
33 
34 #include <stdlib.h>
35 #include <util/atomic.h>
36 
37 #define PORT_SPI PORTB
38 #define DDR_SPI DDRB
39 #define DD_DI DDB0
40 #define DD_DO DDB1
41 #define DD_SCK DDB2
42 
43 static void ssd1306_spiConfigure_Usi()
44 {
45  DDR_SPI |= (1<<DD_DO); // as output (DO) - data out
46  DDR_SPI |= (1<<DD_SCK); // as output (USISCK) - clock
47  /* DI pin is still used by USI, although ssd1306 library doesn't need it */
48 // DDR_SPI &= ~(1<<DD_DI); // as input (DI) - data in
49 // PORT_SPI|= (1<<DD_DI); // pullup on (DI)
50 }
51 
52 static void ssd1306_spiClose_Usi()
53 {
54 }
55 
56 static void ssd1306_spiStart_Usi()
57 {
58  if (s_ssd1306_cs >= 0)
59  {
60  digitalWrite(s_ssd1306_cs,LOW);
61  }
62  USICR = (0<<USIWM1) | (1<<USIWM0) |
63  (1<<USICS1) | (0<<USICS0) | (1<<USICLK);
64 }
65 
66 static void ssd1306_spiSendByte_Usi(uint8_t data)
67 {
68  USIDR = data;
69  USISR = (1<<USIOIF);
70  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
71  {
72  while ( (USISR & (1<<USIOIF)) == 0 )
73  {
74  USICR |= (1<<USITC);
75  }
76  }
77 }
78 
79 static void ssd1306_spiStop_Usi()
80 {
81  if (s_ssd1306_cs >= 0)
82  {
83  digitalWrite(s_ssd1306_cs, HIGH);
84  }
86  {
87  digitalWrite(s_ssd1306_dc, LOW);
88  ssd1306_spiSendByte_Usi( 0x00 ); // Send NOP command to allow last data byte to pass (bug in PCD8544?)
89  // ssd1306 E3h is NOP command
90  }
91 // USICR &= ~((1<<USIWM1) | (1<<USIWM0));
92 }
93 
94 void ssd1306_spiInit_Usi(int8_t cesPin, int8_t dcPin)
95 {
96  if (cesPin >=0)
97  {
98  pinMode(cesPin, OUTPUT);
99  digitalWrite(cesPin, HIGH);
100  }
101  if (dcPin >= 0) pinMode(dcPin, OUTPUT);
102  if ((cesPin >= 0) || (dcPin >= 0))
103  {
104  /* Even if CS pin is not used we need still to set it to value passed to the function */
105  s_ssd1306_cs = cesPin;
106  s_ssd1306_dc = dcPin;
107  }
108  ssd1306_spiConfigure_Usi();
109  ssd1306_startTransmission = ssd1306_spiStart_Usi;
110  ssd1306_endTransmission = ssd1306_spiStop_Usi;
111  ssd1306_sendByte = ssd1306_spiSendByte_Usi;
112  ssd1306_closeInterface = ssd1306_spiClose_Usi;
115 }
116 
117 #endif
118 
119 
void(* ssd1306_closeInterface)(void)
deinitializes internal resources, allocated for interface.
void(* ssd1306_sendByte)(uint8_t data)
void ssd1306_spiCommandStart()
Definition: ssd1306_spi.c:52
void(* ssd1306_dataStart)(void)
void ssd1306_spiDataStart()
Definition: ssd1306_spi.c:58
void(* ssd1306_startTransmission)(void)
void(* ssd1306_endTransmission)(void)
int8_t s_ssd1306_cs
Definition: ssd1306_spi.c:35
uint8_t g_lcd_type
Definition: ssd1306.c:40
void(* ssd1306_commandStart)(void)
int8_t s_ssd1306_dc
Definition: ssd1306_spi.c:36