Skip to content

Commit de3e527

Browse files
committed
📦️ one shot mode
1 parent 8043bca commit de3e527

File tree

5 files changed

+191
-6
lines changed

5 files changed

+191
-6
lines changed

dma/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# USART communication using DMA in Circular mode
1+
# USART communication using DMA in one shot mode
22

33
Communication between PC and STM32 using USART and DMA peripherals.
44

5-
Transmit data using USART1 and DMA in continuous mode without interruption of CPU. This project does not require any IDE like CubeIde, any text editor will work including notepad, vim. For better debugging experience, VSCode is preferred.
5+
Transmit data using USART1 and DMA in one shot mode without interruption of CPU. The DMA is restarted adter every 2 seconds. This project does not require any IDE like CubeIde, any text editor will work including notepad, vim. For better debugging experience, VSCode is preferred.
66

77
![Build Passing](https://img.shields.io/badge/build-passing-brightgreen) [![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
88

@@ -127,8 +127,8 @@ Connect the board with host through USB to TTL converter (FTDI board in our case
127127

128128
## Output
129129

130-
"Hello world" messages are visible on the terminal arriving continuously dues to circular mode of operation as seen below.
131-
![Serial prompt at 115200 baudrate](docs/out_115200_circ.png "Output on terminal")
130+
"Hello world" messages are visible on the terminal arriving every 2 seconds as seen below.
131+
![Serial prompt at 115200 baudrate](docs/out_one_shot.png "Output on terminal")
132132

133133
## Debug
134134

dma/circular.md

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# USART communication using DMA in Circular mode
2+
3+
Communication between PC and STM32 using USART and DMA peripherals.
4+
5+
Transmit data using USART1 and DMA in continuous mode without interruption of CPU. This project does not require any IDE like CubeIde, any text editor will work including notepad, vim. For better debugging experience, VSCode is preferred.
6+
7+
![Build Passing](https://img.shields.io/badge/build-passing-brightgreen) [![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/)
8+
9+
## Dependencies
10+
11+
* make
12+
Make utility is required for configuring and building this project. You can install make on linux by running command:
13+
14+
```bash
15+
# for debian based linux distros
16+
sudo apt install build-essential
17+
18+
# for macos
19+
xcode-select --install
20+
21+
# for macos using brew formulae
22+
brew install make
23+
```
24+
25+
* gcc-arm-none-eabi toolchain
26+
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:
27+
28+
```bash
29+
# for debian based linux distros
30+
sudo apt install gcc-arm-none-eabi
31+
32+
# for macos
33+
brew install --cask gcc-arm-embedded
34+
```
35+
36+
* openocd
37+
It is an Open On Circuit Debugging tool used to flash and debug arm micro controllers. You can install openocd on linux by running command:
38+
39+
```bash
40+
# for debian based linux distros
41+
sudo apt install openocd -y
42+
43+
# for macos
44+
brew install openocd
45+
```
46+
47+
* stlink-tools
48+
This program is required for uploading binaries to the STM32 boards. You can install stlink tools by running the command:
49+
50+
```bash
51+
# for debian based linux distros
52+
sudo apt install stlink-tools
53+
54+
# for macos
55+
brew install stlink
56+
```
57+
58+
* Cortex Debug extension
59+
This extension for VSCode is helpful for debugging the application on Blue Pill. The contents of registers as well as memory are visible in the context menu. Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
60+
61+
```bash
62+
ext install marus25.cortex-debug
63+
```
64+
65+
## Project Structure
66+
67+
* `src` directory contains all source files for the project
68+
* `include` directory contains all header files for the project
69+
70+
### Source file description
71+
72+
* `stm32f1.ld` - linker script for stm32f103
73+
* `src/main.c` - main application code
74+
* `src/startup_stm32f1.c` - boot sequence for arm cortex-m3 processors
75+
* `src/system_stm32f1xx.c` - clock configuration and system initialization functions
76+
77+
## Run Locally
78+
79+
Running the project is super easy. Just clone, build, and flash. Clone this project using **Code** button above.
80+
81+
### Configuration
82+
83+
All the configuration required for building this project is given below.
84+
85+
1. Build output directory
86+
In `Makefile`, output directory can be configured using variable `BUILD_DIR`.
87+
88+
2. Build type
89+
In `Makefile`, build type can be configured using variable`BUILD_TYPE`. Possible values are `Debug` and `Release`.
90+
91+
3. Binary name
92+
In `Makefile`, output binary name can be configured using `TARGET` variable.
93+
** update the target name in the `executable` property `.vscode/launch.json` for the debugger to work.
94+
95+
### Build
96+
97+
Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to **Build Output Directory** as configured.
98+
99+
```bash
100+
make
101+
```
102+
103+
## Flash
104+
105+
1. Connect Stlink to PC and blue pill board using swd headers.
106+
2. Put blue pill board in programming mode *(optional)*.
107+
The *Boot0* jumper is set to *0*, set it to *1* and reset the device.
108+
3. Run following to flash board with binary.
109+
110+
```bash
111+
make flash
112+
```
113+
114+
4. Done.
115+
116+
## Hardware Setup
117+
118+
Connect the board with host through USB to TTL converter (FTDI board in our case). The connections are described as follows.
119+
120+
| Pin on Blue Pill | Pin on FTDI |
121+
|------------------ |------------- |
122+
| PA9 | Rx |
123+
| PA10 | Tx |
124+
| Gnd | Gnd |
125+
126+
![Connection diagram for USART1](../docs/label.png "Pin connection diagram for usart1")
127+
128+
## Output
129+
130+
"Hello world" messages are visible on the terminal arriving continuously dues to circular mode of operation as seen below.
131+
![Serial prompt at 115200 baudrate](docs/out_115200_circ.png "Output on terminal")
132+
133+
## Debug
134+
135+
Click in `Run and Debug` option in VsCode sidebar. Then launch `Cortex Debug` target.
136+

dma/docs/out_one_shot.png

429 KB
Loading

dma/include/main.h

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ void dma_usart_rx_init(void);
2424
*/
2525
void dma_usart_tx_enable(void);
2626

27+
/**
28+
* @brief Disable DMA to accept request for Channel 4
29+
*
30+
*/
31+
void dma_usart_tx_disable(void);
32+
2733
/**
2834
* @brief Enable DMA to accept request for channel 5
2935
*/
@@ -40,4 +46,11 @@ void usart1_init(void);
4046
*/
4147
void usart1_enable(void);
4248

49+
50+
/**
51+
* @brief Interrupt handler for DMA channel 4
52+
*
53+
*/
54+
void DMA1_Channel4_IRQHandler(void);
55+
4356
#endif

dma/src/main.c

+38-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <timer.h>
1919
#include <main.h>
2020

21-
const char * msg = "Hello world\r\n\0";
21+
const char *msg = "Hello world\r\n\0";
2222

2323
void dma1_clock_enable(void)
2424
{
@@ -41,17 +41,35 @@ void dma_usart_tx_init(void)
4141
DMA1_Channel4->CCR |= DMA_CCR_MINC;
4242

4343
// enable circular mode
44-
DMA1_Channel4->CCR |= DMA_CCR_CIRC;
44+
// DMA1_Channel4->CCR |= DMA_CCR_CIRC;
4545

4646
// set data transfer direction - memory -> peripheral
4747
DMA1_Channel4->CCR |= DMA_CCR_DIR;
48+
49+
// enable transmission complete interrupt
50+
DMA1_Channel4->CCR |= DMA_CCR_TCIE;
4851
}
4952

5053
void dma_usart_tx_enable(void)
5154
{
5255
DMA1_Channel4->CCR |= DMA_CCR_EN;
5356
}
5457

58+
void dma_usart_tx_disable(void)
59+
{
60+
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
61+
}
62+
63+
void DMA1_Channel4_IRQHandler(void)
64+
{
65+
if (DMA1->ISR & DMA_ISR_TCIF4)
66+
{
67+
// clear interrupt flasg
68+
DMA1->IFCR |= DMA_IFCR_CGIF4;
69+
dma_usart_tx_disable();
70+
}
71+
}
72+
5573
void usart1_init(void)
5674
{
5775
// enable clock for GPIOA and USART1
@@ -84,9 +102,27 @@ void usart1_enable(void)
84102

85103
int main(void)
86104
{
105+
SysTick_Config(SystemCoreClock/1000);
106+
87107
dma1_clock_enable();
88108
usart1_init();
89109
dma_usart_tx_init();
90110
dma_usart_tx_enable();
91111
usart1_enable();
112+
113+
// enable interrupt for channel 4
114+
NVIC_EnableIRQ(DMA1_Channel4_IRQn);
115+
116+
dma_usart_tx_enable();
117+
118+
usart1_enable();
119+
120+
while (1)
121+
{
122+
delay(2000);
123+
124+
// reload no. of transactions
125+
DMA1_Channel4->CNDTR = 13;
126+
dma_usart_tx_enable();
127+
}
92128
}

0 commit comments

Comments
 (0)