modm API documentation
ili9341_spi.hpp
1 /*
2  * Copyright (c) 2019, Mike Wolfram
3  *
4  * This file is part of the modm project.
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9  */
10 #ifndef MODM_ILI9341_SPI_HPP
11 #define MODM_ILI9341_SPI_HPP
12 
13 #include "ili9341.hpp"
14 #include <modm/architecture/interface/spi_device.hpp>
15 #include <modm/platform/spi/spi_base.hpp>
16 
17 namespace modm
18 {
19 
20 /// @ingroup modm_driver_ili9341
21 template<class SPI, class Cs, class Dc>
22 class Ili9341SPIInterface: public ili9341, public modm::SpiDevice<SPI>
23 {
24 public:
26  {
27  this->attachConfigurationHandler([]() {
28  SPI::setDataMode(SPI::DataMode::Mode0);
29  SPI::setDataOrder(SPI::DataOrder::MsbFirst);
30  });
31  Cs::setOutput(modm::Gpio::High);
32  Dc::setOutput();
33  }
34 
35  __attribute__((noinline)) void
36  writeCommand(Command command)
37  {
38  Dc::reset(); // enable command
39  SPI::transferBlocking(i(command));
40  Dc::set(); // reset to data
41  }
42  __attribute__((noinline)) void
43  writeCommand(Command command, uint8_t const *args, std::size_t length)
44  {
45  Dc::reset(); // enable command
46  SPI::transferBlocking(i(command));
47  Dc::set(); // reset to data
48  if (length != 0)
49  {
50  SPI::transferBlocking(const_cast<unsigned char *>(args), nullptr, length);
51  }
52  }
53  void
54  writeData(uint8_t const *data, std::size_t length)
55  {
56  SPI::transferBlocking(const_cast<unsigned char *>(data), nullptr, length);
57  }
58  void
59  writeCommandValue8(Command command, uint8_t value)
60  {
61  writeCommand(command, &value, 1);
62  }
63 
64  void
65  readData(Command command, uint8_t *buffer, std::size_t length)
66  {
68  uint8_t b[4];
69 
70  Dc::reset(); // enable command
71  // SPI::Hal::setDataSize(SpiBase::DataSize::Bit9);
72  SPI::transferBlocking(i(command) << 1);
73  SPI::Hal::setDataSize(SpiBase::DataSize::Bit8);
74  Dc::set(); // reset to data
75  SPI::transferBlocking(b /*nullptr*/, buffer, length);
76  }
77  uint8_t
78  readData(Command command)
79  {
80  writeCommand(command);
81  return SPI::transferBlocking(0x00);
82  }
83 
84 public:
85  struct BatchHandle
86  {
89  : i(iface)
90  {
91  i.acquireMaster();
92  Cs::reset();
93  }
94  ~BatchHandle()
95  {
96  if (i.releaseMaster())
97  Cs::set();
98  }
99  };
100 };
101 
102 /// @ingroup modm_driver_ili9341
103 template <class SPI, class Cs, class Dc, class Reset, class Backlight, std::size_t BufferSize = 320>
104 using Ili9341Spi = Ili9341<
106  Reset, Backlight, BufferSize>;
107 
108 } // namespace modm
109 
110 #endif // MODM_ILI9341_SPI_HPP
Definition: ili9341_spi.hpp:85
Definition: spi_base.hpp:36
Definition: sab/interface_test.cpp:73
Definition: fault_storage.cpp:16
Definition: modm/src/modm/architecture/interface/spi_device.hpp:31
Definition: ili9341.hpp:24
Definition: ili9341_spi.hpp:22