Skip to content

Commit 5dcdf1d

Browse files
committed
[exmaples] Add SPI DMA exmaples for Nucleo-F439ZI and -F767ZI
1 parent 32f426a commit 5dcdf1d

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2021, Raphael Lehmann
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+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/processing.hpp>
14+
15+
using Mosi = GpioOutputB5;
16+
using Miso = GpioInputB4;
17+
using Sck = GpioOutputB3;
18+
using DmaRx = Dma2::Channel0;
19+
using DmaTx = Dma2::Channel3;
20+
using Spi = SpiMaster1_Dma<DmaRx, DmaTx>;
21+
22+
class SpiThread : public modm::pt::Protothread
23+
{
24+
public:
25+
26+
bool
27+
update()
28+
{
29+
PT_BEGIN();
30+
31+
// Enable DMA controller
32+
Dma2::enable();
33+
34+
// Enable and initialize SPI
35+
Spi::connect<Mosi::Mosi, Miso::Miso, Sck::Sck>();
36+
Spi::initialize<Board::SystemClock, 328_kHz>();
37+
38+
while (true)
39+
{
40+
MODM_LOG_INFO << "sendBuffer adress: " << modm::hex << sendBuffer << modm::endl;
41+
MODM_LOG_INFO << "receiveBuffer adress: " << modm::hex << receiveBuffer << modm::endl;
42+
MODM_LOG_INFO << "Info: 0x20000000 is start of SRAM1" << modm::endl;
43+
44+
MODM_LOG_INFO << "Before first transfer" << modm::endl;
45+
46+
// send out 12 bytes, don't care about response
47+
PT_CALL(Spi::transfer(sendBuffer, nullptr, 12));
48+
49+
MODM_LOG_INFO << "After first transfer" << modm::endl;
50+
51+
// send out 12 bytes, read in 12 bytes
52+
PT_CALL(Spi::transfer(sendBuffer, receiveBuffer, 12));
53+
54+
MODM_LOG_INFO << "After second transfer" << modm::endl << modm::endl;
55+
56+
timeout.restart(500ms);
57+
PT_WAIT_UNTIL(timeout.isExpired());
58+
}
59+
60+
PT_END();
61+
}
62+
63+
private:
64+
uint8_t sendBuffer[13] { "data to send" };
65+
uint8_t receiveBuffer[13];
66+
modm::ShortTimeout timeout;
67+
};
68+
69+
SpiThread spiThread;
70+
71+
int main()
72+
{
73+
Board::initialize();
74+
75+
MODM_LOG_INFO << "Hello from SPI-DMA example on Nucleo-F439ZI!" << modm::endl;
76+
77+
while (true)
78+
{
79+
spiThread.update();
80+
}
81+
82+
return 0;
83+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:nucleo-f439zi</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_f439zi/spi_dma</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:dma</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:processing:protothread</module>
11+
<module>modm:processing:timer</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2021, Raphael Lehmann
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+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/board.hpp>
13+
#include <modm/processing.hpp>
14+
15+
using Mosi = GpioOutputB5;
16+
using Miso = GpioInputB4;
17+
using Sck = GpioOutputB3;
18+
using DmaRx = Dma2::Channel0;
19+
using DmaTx = Dma2::Channel3;
20+
using Spi = SpiMaster1_Dma<DmaRx, DmaTx>;
21+
22+
class SpiThread : public modm::pt::Protothread
23+
{
24+
public:
25+
26+
bool
27+
update()
28+
{
29+
PT_BEGIN();
30+
31+
// Enable DMA controller
32+
Dma2::enable();
33+
34+
// Enable and initialize SPI
35+
Spi::connect<Mosi::Mosi, Miso::Miso, Sck::Sck>();
36+
Spi::initialize<Board::SystemClock, 422_kHz>();
37+
38+
while (true)
39+
{
40+
MODM_LOG_INFO << "sendBuffer adress: " << modm::hex << sendBuffer << modm::endl;
41+
MODM_LOG_INFO << "receiveBuffer adress: " << modm::hex << receiveBuffer << modm::endl;
42+
MODM_LOG_INFO << "Info: 0x20000000 is start of SRAM1" << modm::endl;
43+
44+
MODM_LOG_INFO << "Before first transfer" << modm::endl;
45+
46+
// send out 12 bytes, don't care about response
47+
PT_CALL(Spi::transfer(sendBuffer, nullptr, 12));
48+
49+
MODM_LOG_INFO << "After first transfer" << modm::endl;
50+
51+
// send out 12 bytes, read in 12 bytes
52+
PT_CALL(Spi::transfer(sendBuffer, receiveBuffer, 12));
53+
54+
MODM_LOG_INFO << "After second transfer" << modm::endl << modm::endl;
55+
56+
timeout.restart(500ms);
57+
PT_WAIT_UNTIL(timeout.isExpired());
58+
}
59+
60+
PT_END();
61+
}
62+
63+
private:
64+
uint8_t sendBuffer[13] { "data to send" };
65+
uint8_t receiveBuffer[13];
66+
modm::ShortTimeout timeout;
67+
};
68+
69+
SpiThread spiThread;
70+
71+
int main()
72+
{
73+
Board::initialize();
74+
75+
MODM_LOG_INFO << "Hello from SPI-DMA example on Nucleo-F767ZI!" << modm::endl;
76+
77+
while (true)
78+
{
79+
spiThread.update();
80+
}
81+
82+
return 0;
83+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:nucleo-f767zi</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_f767zi/spi_dma</option>
5+
</options>
6+
<modules>
7+
<module>modm:platform:gpio</module>
8+
<module>modm:platform:dma</module>
9+
<module>modm:platform:spi:1</module>
10+
<module>modm:processing:protothread</module>
11+
<module>modm:processing:timer</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>

0 commit comments

Comments
 (0)