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_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 "ssd1306_hal/io.h"
31 
32 #if defined(CONFIG_USI_SPI_AVAILABLE) && defined(CONFIG_USI_SPI_ENABLE)
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_spiSendBytes_Usi(const uint8_t *buffer, uint16_t size)
80 {
81  while (size--)
82  {
83  USIDR = *buffer;
84  USISR = (1<<USIOIF);
85  ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
86  {
87  while ( (USISR & (1<<USIOIF)) == 0 )
88  {
89  USICR |= (1<<USITC);
90  }
91  }
92  buffer++;
93  };
94 }
95 
96 static void ssd1306_spiStop_Usi()
97 {
98  if (s_ssd1306_cs >= 0)
99  {
100  digitalWrite(s_ssd1306_cs, HIGH);
101  }
103  {
104  digitalWrite(s_ssd1306_dc, LOW);
105  ssd1306_spiSendByte_Usi( 0x00 ); // Send NOP command to allow last data byte to pass (bug in PCD8544?)
106  // ssd1306 E3h is NOP command
107  }
108 // USICR &= ~((1<<USIWM1) | (1<<USIWM0));
109 }
110 
111 void ssd1306_spiInit_Usi(int8_t cesPin, int8_t dcPin)
112 {
113  if (cesPin >=0)
114  {
115  pinMode(cesPin, OUTPUT);
116  digitalWrite(cesPin, HIGH);
117  }
118  if (dcPin >= 0) pinMode(dcPin, OUTPUT);
119  if ((cesPin >= 0) || (dcPin >= 0))
120  {
121  /* Even if CS pin is not used we need still to set it to value passed to the function */
122  s_ssd1306_cs = cesPin;
123  s_ssd1306_dc = dcPin;
124  }
125  ssd1306_spiConfigure_Usi();
126  ssd1306_intf.spi = 1;
127  ssd1306_intf.start = ssd1306_spiStart_Usi;
128  ssd1306_intf.stop = ssd1306_spiStop_Usi;
129  ssd1306_intf.send = ssd1306_spiSendByte_Usi;
130  ssd1306_intf.send_buffer = ssd1306_spiSendBytes_Usi;
131  ssd1306_intf.close = ssd1306_spiClose_Usi;
132 }
133 
134 #endif
135 
136 
int8_t s_ssd1306_dc
Definition: ssd1306_spi.c:34
void(* send)(uint8_t data)
void(* close)(void)
deinitializes internal resources, allocated for interface.
ssd1306_lcd_t ssd1306_lcd
Definition: lcd_common.c:33
void ssd1306_spiInit_Usi(int8_t cesPin, int8_t dcPin)
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