Skip to content

Commit de16c0f

Browse files
committed
added bit resolution to Spi
1 parent cfbec09 commit de16c0f

15 files changed

+419
-164
lines changed

src/modm/driver/display/ili9341.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class Ili9341 : public Interface,
100100
setOrientation(Orientation orientation);
101101

102102
modm::ResumableResult<void>
103-
setScrollArea(uint16_t topFixedRows, uint16_t bottomFixedRows, uint16_t firstRow);
103+
setScrollArea(uint16_t topFixedRows, uint16_t firstRow, uint16_t bottomFixedRows);
104104

105105
modm::ResumableResult<void>
106106
scrollTo(uint16_t row);

src/modm/driver/display/ili9341_impl.hpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,15 @@ modm::Ili9341<Interface, Reset, BC>::setOrientation(
174174

175175
template<class Interface, class Reset, size_t BC>
176176
modm::ResumableResult<void>
177-
modm::Ili9341<Interface, Reset, BC>::setScrollArea(uint16_t topFixedRows,
178-
uint16_t bottomFixedRows,
179-
uint16_t firstRow)
177+
modm::Ili9341<Interface, Reset, BC>::setScrollArea(uint16_t topFixedRows, uint16_t firstRow, uint16_t bottomFixedRows)
180178
{
181179
RF_BEGIN();
182180

183-
buff_cmd16[0] = modm::fromBigEndian(topFixedRows);
184-
buff_cmd16[1] = modm::fromBigEndian(firstRow);
185-
buff_cmd16[2] = modm::fromBigEndian(bottomFixedRows);
181+
buff_cmd16[0] = topFixedRows;
182+
buff_cmd16[1] = firstRow;
183+
buff_cmd16[2] = bottomFixedRows;
186184

187-
RF_CALL(this->writeCommand(Command::VerticalScrollDefinition, buff_cmd8, 6));
185+
RF_CALL(this->writeCommand(Command::VerticalScrollDefinition, buff_cmd16, 3));
188186
RF_END();
189187
}
190188

@@ -193,10 +191,7 @@ modm::ResumableResult<void>
193191
modm::Ili9341<Interface, Reset, BC>::scrollTo(uint16_t row)
194192
{
195193
RF_BEGIN();
196-
197-
buff_cmd16[0] = modm::fromBigEndian(row);
198-
199-
RF_CALL(this->writeCommand(Command::VerticalScrollStartAddr, buff_cmd8, 2));
194+
RF_CALL(this->writeCommand(Command::VerticalScrollStartAddr, &row, 1));
200195
RF_END();
201196
}
202197

@@ -208,13 +203,13 @@ modm::Ili9341<Interface, Reset, BC>::setClipping(Point point)
208203
{
209204
RF_BEGIN();
210205

211-
p.buff_cmd_clipping[0] = modm::fromBigEndian(point.x);
206+
p.buff_cmd_clipping[0] = point.x;
212207
p.buff_cmd_clipping[1] = p.buff_cmd_clipping[0];
213-
RF_CALL(this->writeCommand(Command::PageAddressSet, (uint8_t *)(p.buff_cmd_clipping), 4));
208+
RF_CALL(this->writeCommand(Command::PageAddressSet, p.buff_cmd_clipping, 4));
214209

215-
p.buff_cmd_clipping[0] = modm::fromBigEndian(point.y);
210+
p.buff_cmd_clipping[0] = point.y;
216211
p.buff_cmd_clipping[1] = p.buff_cmd_clipping[0];
217-
RF_CALL(this->writeCommand(Command::ColumnAddressSet, (uint8_t *)(p.buff_cmd_clipping), 4));
212+
RF_CALL(this->writeCommand(Command::ColumnAddressSet, p.buff_cmd_clipping, 4));
218213

219214
RF_CALL(this->writeCommand(Command::MemoryWrite));
220215

@@ -227,13 +222,13 @@ modm::Ili9341<Interface, Reset, BC>::updateClipping()
227222
{
228223
RF_BEGIN();
229224

230-
p.buff_cmd_clipping[0] = modm::fromBigEndian(this->clipping.topLeft.x);
231-
p.buff_cmd_clipping[1] = modm::fromBigEndian(int16_t(this->clipping.bottomRight.x - 1));
232-
RF_CALL(this->writeCommand(Command::PageAddressSet, (uint8_t *)(p.buff_cmd_clipping), 4));
225+
p.buff_cmd_clipping[0] = this->clipping.topLeft.x;
226+
p.buff_cmd_clipping[1] = this->clipping.bottomRight.x - 1;
227+
RF_CALL(this->writeCommand(Command::PageAddressSet, p.buff_cmd_clipping, 2));
233228

234-
p.buff_cmd_clipping[0] = modm::fromBigEndian(this->clipping.topLeft.y);
235-
p.buff_cmd_clipping[1] = modm::fromBigEndian(int16_t(this->clipping.bottomRight.y - 1));
236-
RF_CALL(this->writeCommand(Command::ColumnAddressSet, (uint8_t *)(p.buff_cmd_clipping), 4));
229+
p.buff_cmd_clipping[0] = this->clipping.topLeft.y;
230+
p.buff_cmd_clipping[1] = this->clipping.bottomRight.y - 1;
231+
RF_CALL(this->writeCommand(Command::ColumnAddressSet, p.buff_cmd_clipping, 2));
237232

238233
RF_CALL(this->writeCommand(Command::MemoryWrite));
239234
p.pixels = this->clipping.getPixels();

src/modm/driver/display/ili9341_interface_spi.hpp

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
4848
Cs::reset();
4949

5050
Dc::reset();
51-
RF_CALL(SpiMaster::transfer(uint8_t(command)));
51+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
5252
Dc::set();
5353

5454
if (this->releaseMaster())
@@ -66,10 +66,10 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
6666
Cs::reset();
6767

6868
Dc::reset();
69-
RF_CALL(SpiMaster::transfer(uint8_t(command)));
69+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
7070
Dc::set();
7171

72-
RF_CALL(SpiMaster::transfer(data));
72+
RF_CALL(SpiMaster::template transfer<8>(data));
7373

7474
if (this->releaseMaster())
7575
Cs::set();
@@ -78,18 +78,18 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
7878
}
7979

8080
modm::ResumableResult<void>
81-
writeCommand(Command command, const uint8_t *data, std::size_t length)
81+
writeCommand(Command command, const uint8_t *data, uint16_t length)
8282
{
8383
RF_BEGIN();
8484

8585
RF_WAIT_UNTIL(this->acquireMaster());
8686
Cs::reset();
8787

8888
Dc::reset();
89-
RF_CALL(SpiMaster::transfer(uint8_t(command)));
89+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
9090
Dc::set();
9191

92-
RF_CALL(SpiMaster::transfer(data, nullptr, length));
92+
RF_CALL(SpiMaster::template transfer<8>(data, nullptr, length));
9393

9494
if (this->releaseMaster())
9595
Cs::set();
@@ -98,14 +98,34 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
9898
}
9999

100100
modm::ResumableResult<void>
101-
writeData(const color::Rgb565 *pixels, size_t length)
101+
writeCommand(Command command, const uint16_t *data, uint16_t length)
102102
{
103103
RF_BEGIN();
104104

105105
RF_WAIT_UNTIL(this->acquireMaster());
106106
Cs::reset();
107107

108-
RF_CALL(SpiMaster::transfer((uint8_t*)(pixels), nullptr, 2 * length));
108+
Dc::reset();
109+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
110+
Dc::set();
111+
112+
RF_CALL(SpiMaster::template transfer<16>(data, nullptr, length));
113+
114+
if (this->releaseMaster())
115+
Cs::set();
116+
117+
RF_END();
118+
}
119+
120+
modm::ResumableResult<void>
121+
writeData(const color::Rgb565 *pixels, uint16_t length)
122+
{
123+
RF_BEGIN();
124+
125+
RF_WAIT_UNTIL(this->acquireMaster());
126+
Cs::reset();
127+
128+
RF_CALL(SpiMaster::template transfer<16>((uint16_t*)(pixels), nullptr, length));
109129

110130
if (this->releaseMaster())
111131
Cs::set();
@@ -121,7 +141,23 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
121141
RF_WAIT_UNTIL(this->acquireMaster());
122142
Cs::reset();
123143

124-
RF_CALL(SpiMaster::transfer((uint8_t*)(&pixel), nullptr, 2));
144+
RF_CALL(SpiMaster::template transfer<16>(pixel.getValue()));
145+
146+
if (this->releaseMaster())
147+
Cs::set();
148+
149+
RF_END();
150+
}
151+
152+
modm::ResumableResult<void>
153+
writeData(color::Rgb565 pixel, uint16_t repeat)
154+
{
155+
RF_BEGIN();
156+
157+
RF_WAIT_UNTIL(this->acquireMaster());
158+
Cs::reset();
159+
160+
RF_CALL(SpiMaster::template transfer<16>(pixel.getValue(), repeat));
125161

126162
if (this->releaseMaster())
127163
Cs::set();
@@ -138,10 +174,10 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
138174
Cs::reset();
139175

140176
Dc::reset();
141-
RF_CALL(SpiMaster::transfer(uint8_t(command)));
177+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
142178
Dc::set();
143179

144-
read = RF_CALL(SpiMaster::transfer(0x00)).getResult();
180+
read = RF_CALL(SpiMaster::template transfer<8>(0x00)).getResult();
145181

146182
if (this->releaseMaster())
147183
Cs::set();
@@ -150,18 +186,18 @@ class Ili9341InterfaceSpi : public ili9341_register, public modm::SpiDevice< Spi
150186
}
151187

152188
modm::ResumableResult<void>
153-
readData(Command command, uint8_t *buffer, size_t length)
189+
readData(Command command, uint8_t *buffer, uint16_t length)
154190
{
155191
RF_BEGIN();
156192

157193
RF_WAIT_UNTIL(this->acquireMaster());
158194
Cs::reset();
159195

160196
Dc::reset();
161-
RF_CALL(SpiMaster::transfer(uint8_t(command)));
197+
RF_CALL(SpiMaster::template transfer<8>(uint8_t(command)));
162198
Dc::set();
163199

164-
RF_CALL(SpiMaster::transfer(nullptr, buffer, length));
200+
RF_CALL(SpiMaster::template transfer<8>(nullptr, buffer, length));
165201

166202
if (this->releaseMaster())
167203
Cs::set();

src/modm/math/utils/integer_traits.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace modm
1616
(Bits <= 64), uint64_t>>>>;
1717
};
1818

19+
template<int Bits>
20+
using least_uint = typename modm::uint_t<Bits>::least;
21+
1922
template<typename T>
2023
constexpr int most_digits() {
2124
return std::numeric_limits<T>::digits;

src/modm/platform/dma/stm32/dma.hpp.in

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,34 @@ public:
184184
ChannelHal::setPeripheralAddress(address);
185185
}
186186

187+
static void
188+
setMemoryDataSize(MemoryDataSize memoryDataSize)
189+
{
190+
ChannelHal::setMemoryDataSize(memoryDataSize);
191+
}
192+
193+
static void
194+
setPeripheralDataSize(PeripheralDataSize peripheralDataSize)
195+
{
196+
ChannelHal::setPeripheralDataSize(peripheralDataSize);
197+
}
198+
199+
template<std::unsigned_integral T>
200+
static void
201+
configureDataSize() {
202+
if constexpr(std::is_same<T, uint8_t>::value) {
203+
ChannelHal::setMemoryDataSize(DmaBase::MemoryDataSize::Byte);
204+
ChannelHal::setPeripheralDataSize(DmaBase::PeripheralDataSize::Byte);
205+
}
206+
else if constexpr(std::is_same<T, uint16_t>::value) {
207+
ChannelHal::setMemoryDataSize(DmaBase::MemoryDataSize::HalfWord);
208+
ChannelHal::setPeripheralDataSize(DmaBase::PeripheralDataSize::HalfWord);
209+
} else if constexpr(std::is_same<T, uint32_t>::value) {
210+
ChannelHal::setMemoryDataSize(DmaBase::MemoryDataSize::Word);
211+
ChannelHal::setPeripheralDataSize(DmaBase::PeripheralDataSize::Word);
212+
}
213+
}
214+
187215
/**
188216
* Enable/disable memory increment
189217
*

src/modm/platform/dma/stm32/dma_base.hpp.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public:
152152
Bit16 = HalfWord,
153153
Word = {{ reg_prefix }}_MSIZE_1,
154154
Bit32 = Word,
155+
All = DMA_CCR_MSIZE_0 | DMA_CCR_MSIZE_1
155156
};
156157

157158
enum class
@@ -163,6 +164,7 @@ public:
163164
Bit16 = HalfWord,
164165
Word = {{ reg_prefix }}_PSIZE_1,
165166
Bit32 = Word,
167+
All = DMA_CCR_PSIZE_0 | DMA_CCR_PSIZE_1
166168
};
167169

168170
enum class

src/modm/platform/dma/stm32/dma_hal.hpp.in

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ public:
200200
Base->CPAR = address;
201201
}
202202

203+
static void
204+
setMemoryDataSize(MemoryDataSize memoryDataSize)
205+
{
206+
DMA_Channel_TypeDef *Base = (DMA_Channel_TypeDef *) CHANNEL_BASE;
207+
Base->CCR &= ~uint32_t(MemoryDataSize::All);
208+
Base->CCR |= uint32_t(memoryDataSize);
209+
}
210+
211+
static void
212+
setPeripheralDataSize(PeripheralDataSize peripheralDataSize)
213+
{
214+
DMA_Channel_TypeDef *Base = (DMA_Channel_TypeDef *) CHANNEL_BASE;
215+
Base->CCR &= ~uint32_t(PeripheralDataSize::All);
216+
Base->CCR |= uint32_t(peripheralDataSize);
217+
}
218+
203219
/**
204220
* Enable/disable memory increment
205221
*
@@ -240,9 +256,11 @@ public:
240256

241257
/**
242258
* Set length of data to transfer
259+
*
260+
* @param lenght range: 0-65535
243261
*/
244262
static void
245-
setDataLength(std::size_t length)
263+
setDataLength(uint16_t length)
246264
{
247265
DMA_Channel_TypeDef *Base = (DMA_Channel_TypeDef *) CHANNEL_BASE;
248266
Base->CNDTR = length;

src/modm/platform/spi/stm32/module.lb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ class Instance(Module):
2929
module.description = "Instance {}".format(self.instance)
3030

3131
def prepare(self, module, options):
32-
module.depends(":platform:spi")
32+
module.depends(
33+
":platform:spi",
34+
":math:utils"
35+
)
3336
return True
3437

3538
def build(self, env):
@@ -43,6 +46,7 @@ class Instance(Module):
4346
env.template("spi_hal_impl.hpp.in", "spi_hal_{}_impl.hpp".format(self.instance))
4447
env.template("spi_master.hpp.in", "spi_master_{}.hpp".format(self.instance))
4548
env.template("spi_master.cpp.in", "spi_master_{}.cpp".format(self.instance))
49+
env.template("spi_master_impl.hpp.in", "spi_master_{}_impl.hpp".format(self.instance))
4650
if env.has_module(":platform:dma"):
4751
env.template("spi_master_dma.hpp.in", "spi_master_{}_dma.hpp".format(self.instance))
4852
env.template("spi_master_dma_impl.hpp.in", "spi_master_{}_dma_impl.hpp".format(self.instance))

src/modm/platform/spi/stm32/spi_hal.hpp.in

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2013-2018, Niklas Hauser
44
* Copyright (c) 2014, Daniel Krebs
55
* Copyright (c) 2020, Mike Wolfram
6+
* Copyright (c) 2021, Thomas Sommer
67
*
78
* This file is part of the modm project.
89
*
@@ -65,6 +66,10 @@ public:
6566
static void
6667
setDataSize(DataSize dataSize);
6768

69+
template<int Bits = 8>
70+
static void
71+
setDataSize();
72+
6873
static void
6974
setMasterSelection(MasterSelection masterSelection);
7075

@@ -82,24 +87,24 @@ public:
8287
isTransmitRegisterEmpty();
8388

8489
/**
85-
* Write up to 16 Bit to the data register
90+
* Write up to 8 Bit to the data register
8691
*
8792
* @warning This method does NOT do any sanity checks!!
8893
* It is your responsibility to check if the register
8994
* is empty!
9095
*/
9196
static void
92-
write(uint16_t data);
97+
write(uint8_t data);
9398

9499
/**
95-
* Write 8 Bit to the data register
100+
* Write up to 16 Bit to the data register
96101
*
97102
* @warning This method does NOT do any sanity checks!!
98103
* It is your responsibility to check if the register
99104
* is empty!
100105
*/
101106
static void
102-
write(uint8_t data);
107+
write(uint16_t data);
103108

104109
/**
105110
* Returns the value of the data register

0 commit comments

Comments
 (0)