|
| 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 |
0 commit comments