Skip to content

Commit 275c635

Browse files
committed
[driver] Add SSD1306 SPI driver
1 parent 77ba35a commit 275c635

File tree

5 files changed

+227
-7
lines changed

5 files changed

+227
-7
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ your specific needs.
729729
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cat24aa">CAT24AA</a></td>
730730
<td align="center"><a href="https://modm.io/reference/module/modm-driver-cycle_counter">CYCLE-COUNTER</a></td>
731731
</tr><tr>
732-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-drv832x_spi">DRV832X</a></td>
732+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-drv832x_spi">DRV832X-SPI</a></td>
733733
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1302">DS1302</a></td>
734734
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds1631">DS1631</a></td>
735735
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ds18b20">DS18B20</a></td>
@@ -797,22 +797,23 @@ your specific needs.
797797
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
798798
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
799799
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
800-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
800+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306-i2c">SSD1306-I2C</a></td>
801801
</tr><tr>
802+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306-spi">SSD1306-SPI</a></td>
802803
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
803804
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
804805
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
805806
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
806807
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
807-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
808808
</tr><tr>
809+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx128x">SX128X</a></td>
809810
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
810811
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
811812
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
812813
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
813814
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
814-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
815815
</tr><tr>
816+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
816817
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
817818
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
818819
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2014, 2016-2017, Sascha Schade
3+
* Copyright (c) 2014-2016, 2018, Niklas Hauser
4+
* Copyright (c) 2021, Thomas Sommer
5+
* Copyright (c) 2023, Raphael Lehmann
6+
*
7+
* This file is part of the modm project.
8+
*
9+
* This Source Code Form is subject to the terms of the Mozilla Public
10+
* License, v. 2.0. If a copy of the MPL was not distributed with this
11+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
12+
*/
13+
// ----------------------------------------------------------------------------
14+
15+
#ifndef MODM_SSD1306_SPI_HPP
16+
#define MODM_SSD1306_SPI_HPP
17+
18+
#include <modm/architecture/interface/spi_device.hpp>
19+
#include <modm/architecture/utils.hpp>
20+
#include <modm/processing/timer.hpp>
21+
#include <modm/ui/display/monochrome_graphic_display_vertical.hpp>
22+
#include "ssd1306_common.hpp"
23+
#include <utility>
24+
25+
namespace modm
26+
{
27+
28+
/**
29+
* Driver for SSD1306 based OLED-displays using SPI 4-wire mode.
30+
*
31+
* @author Raphael Lehmann
32+
* @ingroup modm_driver_ssd1306
33+
*/
34+
template<class SpiMaster, class Cs, class Dc, uint8_t Height = 64>
35+
class Ssd1306Spi : public Ssd1306Common<Height>,
36+
public SpiDevice<SpiMaster>,
37+
protected modm::NestedResumable<2>
38+
{
39+
public:
40+
enum class
41+
InitSequences : uint8_t
42+
{
43+
Hp12832_02, /// HP12832-02 display module, 0.91inch, 128x32px, 4-wire
44+
/// SPI, blue, external 7.5V supply
45+
};
46+
47+
modm::ResumableResult<bool>
48+
initialize(InitSequences initSeq = InitSequences::Hp12832_02);
49+
50+
/// Update the display with the content of the RAM buffer
51+
modm::ResumableResult<bool>
52+
writeDisplay();
53+
54+
/// Update the display with the content of the RAM buffer
55+
/// @warning This function is blocking as long as the transfer of the
56+
/// whole display buffer via SPI takes.
57+
// Better use @see writeDisplay() instead.
58+
void
59+
update()
60+
{
61+
RF_CALL_BLOCKING(writeDisplay());
62+
}
63+
64+
protected:
65+
modm::ResumableResult<bool>
66+
writeCommands(std::size_t length) override;
67+
};
68+
69+
} // namespace modm
70+
71+
#include "ssd1306_spi_impl.hpp"
72+
73+
#endif // MODM_SSD1306_SPI_HPP
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2023, Raphael Lehmann
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
14+
def init(module):
15+
module.name = ":driver:ssd1306.spi"
16+
module.description = "SSD1306 Display in SPI 4-wire mode"
17+
18+
def prepare(module, options):
19+
module.depends(
20+
":architecture:spi.device",
21+
":driver:ssd1306.common",
22+
":processing:timer",
23+
":ui:display")
24+
return True
25+
26+
def build(env):
27+
env.outbasepath = "modm/src/modm/driver/display"
28+
env.copy("ssd1306_spi.hpp")
29+
env.copy("ssd1306_spi_impl.hpp")
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2014-2015, Niklas Hauser
3+
* Copyright (c) 2023, Raphael Lehmann
4+
*
5+
* This file is part of the modm project.
6+
*
7+
* This Source Code Form is subject to the terms of the Mozilla Public
8+
* License, v. 2.0. If a copy of the MPL was not distributed with this
9+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
*/
11+
// ----------------------------------------------------------------------------
12+
13+
#ifndef MODM_SSD1306_SPI_HPP
14+
#error "Don't include this file directly, use 'ssd1306_spi.hpp' instead!"
15+
#endif
16+
17+
namespace modm
18+
{
19+
20+
template<class SpiMaster, class Cs, class Dc, uint8_t Height>
21+
ResumableResult<bool>
22+
Ssd1306Spi<SpiMaster, Cs, Dc, Height>::initialize(InitSequences initSeq)
23+
{
24+
RF_BEGIN();
25+
26+
this->attachConfigurationHandler([]() {
27+
SpiMaster::setDataMode(SpiMaster::DataMode::Mode0);
28+
SpiMaster::setDataOrder(SpiMaster::DataOrder::MsbFirst);
29+
});
30+
Cs::setOutput(modm::Gpio::High);
31+
Dc::setOutput();
32+
33+
if (initSeq == InitSequences::Hp12832_02)
34+
{
35+
// Initialize sequence from HP12832-02-TSBG12P091-A-VER1.0.pdf page 14
36+
37+
this->commandBuffer[0] = std::to_underlying(ssd1306::FundamentalCommands::DisplayOff);
38+
this->commandBuffer[1] = std::to_underlying(ssd1306::TimingAndDrivingCommands::DisplayClockDivideRatio);
39+
this->commandBuffer[2] = 0x80; // 0x90 or 0x80 (page 14)?
40+
this->commandBuffer[3] = std::to_underlying(ssd1306::HardwareConfigCommands::MultiplexRatio);
41+
this->commandBuffer[4] = 0x1F; // 0x3F or 0x1F (page 14)?
42+
this->commandBuffer[5] = std::to_underlying(ssd1306::HardwareConfigCommands::DisplayOffset);
43+
this->commandBuffer[6] = 0;
44+
this->commandBuffer[7] = std::to_underlying(ssd1306::AdressingCommands::MemoryMode);
45+
this->commandBuffer[8] = std::to_underlying(ssd1306::MemoryMode::HORIZONTAL);
46+
this->commandBuffer[9] = std::to_underlying(ssd1306::AdressingCommands::ColumnAddress);
47+
this->commandBuffer[10] = 0;
48+
this->commandBuffer[11] = 127;
49+
this->commandBuffer[12] = std::to_underlying(ssd1306::AdressingCommands::PageAddress);
50+
this->commandBuffer[13] = 0;
51+
this->commandBuffer[14] = (Height == 64) ? 7 : 3;
52+
this->commandBuffer[15] = std::to_underlying(ssd1306::HardwareConfigCommands::DisplayStartLine) | 0;
53+
this->commandBuffer[16] = std::to_underlying(ssd1306::HardwareConfigCommands::SegmentRemap0);
54+
this->commandBuffer[17] = std::to_underlying(ssd1306::HardwareConfigCommands::ComOutputScanDirectionIncrement);
55+
this->commandBuffer[18] = std::to_underlying(ssd1306::HardwareConfigCommands::ComPinsOrder);
56+
this->commandBuffer[19] = 0x02;
57+
this->commandBuffer[20] = std::to_underlying(ssd1306::FundamentalCommands::ContrastControl);
58+
this->commandBuffer[21] = 0x8F;
59+
this->commandBuffer[22] = std::to_underlying(ssd1306::TimingAndDrivingCommands::PreChargePeriod);
60+
this->commandBuffer[23] = 0x1F;
61+
this->commandBuffer[24] = std::to_underlying(ssd1306::TimingAndDrivingCommands::V_DeselectLevel);
62+
this->commandBuffer[25] = 0x30;
63+
this->commandBuffer[26] = std::to_underlying(ssd1306::FundamentalCommands::EntireDisplayResumeToRam);
64+
this->commandBuffer[27] = std::to_underlying(ssd1306::FundamentalCommands::NormalDisplay);
65+
this->commandBuffer[28] = std::to_underlying(ssd1306::TimingAndDrivingCommands::ChargePump);
66+
this->commandBuffer[29] = std::to_underlying(ssd1306::ChargePump::DISABLE);
67+
this->commandBuffer[30] = std::to_underlying(ssd1306::FundamentalCommands::DisplayOn);
68+
69+
RF_RETURN_CALL(writeCommands(31));
70+
}
71+
else {
72+
RF_RETURN(false);
73+
}
74+
75+
RF_END();
76+
}
77+
78+
template<class SpiMaster, class Cs, class Dc, uint8_t Height>
79+
ResumableResult<bool>
80+
Ssd1306Spi<SpiMaster, Cs, Dc, Height>::writeDisplay()
81+
{
82+
RF_BEGIN();
83+
84+
RF_WAIT_UNTIL(this->acquireMaster());
85+
Cs::reset();
86+
87+
Dc::set();
88+
89+
RF_CALL(SpiMaster::transfer((uint8_t*)(&this->buffer), nullptr, sizeof(this->buffer)));
90+
91+
if (this->releaseMaster()) {
92+
Cs::set();
93+
}
94+
95+
RF_END_RETURN(true);
96+
}
97+
98+
template<class SpiMaster, class Cs, class Dc, uint8_t Height>
99+
ResumableResult<bool>
100+
Ssd1306Spi<SpiMaster, Cs, Dc, Height>::writeCommands(std::size_t length)
101+
{
102+
RF_BEGIN();
103+
104+
RF_WAIT_UNTIL(this->acquireMaster());
105+
Cs::reset();
106+
107+
Dc::reset();
108+
109+
RF_CALL(SpiMaster::transfer(this->commandBuffer.data(), nullptr, length));
110+
111+
if (this->releaseMaster()) {
112+
Cs::set();
113+
}
114+
115+
RF_END_RETURN(true);
116+
}
117+
118+
} // namespace modm

tools/scripts/synchronize_docs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ def name(raw_name):
7474
.replace("-BITBANG", " BitBang")\
7575
.replace("GPIO-SAMPLER", "Gpio Sampler")\
7676
.replace("BLOCK-", "")\
77-
.replace("SPI-FLASH", "SPI Flash")\
78-
.replace("-SPI", "")
77+
.replace("SPI-FLASH", "SPI Flash")
7978
if result in ["DEVICE", "LIS3-TRANSPORT", "MEMORY-BUS", "TERMINAL", "ALLOCATOR",
80-
"MIRROR", "ADC-SAMPLER", "FAT", "HEAP", "--PYCACHE--", "FILE", "SPI-STACK-FLASH"]:
79+
"MIRROR", "ADC-SAMPLER", "FAT", "HEAP", "--PYCACHE--", "FILE", "SPI-STACK-FLASH", "SSD1306-COMMON"]:
8180
return None
8281
return result
8382

0 commit comments

Comments
 (0)