Skip to content

Commit

Permalink
📦️ one shot mode
Browse files Browse the repository at this point in the history
  • Loading branch information
csrohit committed Dec 7, 2022
1 parent 8043bca commit de3e527
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 6 deletions.
8 changes: 4 additions & 4 deletions dma/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# USART communication using DMA in Circular mode
# USART communication using DMA in one shot mode

Communication between PC and STM32 using USART and DMA peripherals.

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.
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.

![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/)

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

## Output

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

## Debug

Expand Down
136 changes: 136 additions & 0 deletions dma/circular.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# USART communication using DMA in Circular mode

Communication between PC and STM32 using USART and DMA peripherals.

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.

![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/)

## Dependencies

* make
Make utility is required for configuring and building this project. You can install make on linux by running command:

```bash
# for debian based linux distros
sudo apt install build-essential

# for macos
xcode-select --install

# for macos using brew formulae
brew install make
```

* gcc-arm-none-eabi toolchain
ARM cross-platform toolchain is required to build applications for arm mcus. Toolchain can be installed by running following command:

```bash
# for debian based linux distros
sudo apt install gcc-arm-none-eabi
# for macos
brew install --cask gcc-arm-embedded
```

* openocd
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:

```bash
# for debian based linux distros
sudo apt install openocd -y
# for macos
brew install openocd
```

* stlink-tools
This program is required for uploading binaries to the STM32 boards. You can install stlink tools by running the command:

```bash
# for debian based linux distros
sudo apt install stlink-tools
# for macos
brew install stlink
```

* Cortex Debug extension
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.

```bash
ext install marus25.cortex-debug
```

## Project Structure

* `src` directory contains all source files for the project
* `include` directory contains all header files for the project

### Source file description

* `stm32f1.ld` - linker script for stm32f103
* `src/main.c` - main application code
* `src/startup_stm32f1.c` - boot sequence for arm cortex-m3 processors
* `src/system_stm32f1xx.c` - clock configuration and system initialization functions

## Run Locally

Running the project is super easy. Just clone, build, and flash. Clone this project using **Code** button above.

### Configuration

All the configuration required for building this project is given below.

1. Build output directory
In `Makefile`, output directory can be configured using variable `BUILD_DIR`.

2. Build type
In `Makefile`, build type can be configured using variable`BUILD_TYPE`. Possible values are `Debug` and `Release`.

3. Binary name
In `Makefile`, output binary name can be configured using `TARGET` variable.
** update the target name in the `executable` property `.vscode/launch.json` for the debugger to work.

### Build

Run following command in terminal to generate flashable binaries for blue pill board. Build files will be written to **Build Output Directory** as configured.

```bash
make
```

## Flash

1. Connect Stlink to PC and blue pill board using swd headers.
2. Put blue pill board in programming mode *(optional)*.
The *Boot0* jumper is set to *0*, set it to *1* and reset the device.
3. Run following to flash board with binary.

```bash
make flash
```

4. Done.

## Hardware Setup

Connect the board with host through USB to TTL converter (FTDI board in our case). The connections are described as follows.

| Pin on Blue Pill | Pin on FTDI |
|------------------ |------------- |
| PA9 | Rx |
| PA10 | Tx |
| Gnd | Gnd |

![Connection diagram for USART1](../docs/label.png "Pin connection diagram for usart1")

## Output

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

## Debug

Click in `Run and Debug` option in VsCode sidebar. Then launch `Cortex Debug` target.

Binary file added dma/docs/out_one_shot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions dma/include/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ void dma_usart_rx_init(void);
*/
void dma_usart_tx_enable(void);

/**
* @brief Disable DMA to accept request for Channel 4
*
*/
void dma_usart_tx_disable(void);

/**
* @brief Enable DMA to accept request for channel 5
*/
Expand All @@ -40,4 +46,11 @@ void usart1_init(void);
*/
void usart1_enable(void);


/**
* @brief Interrupt handler for DMA channel 4
*
*/
void DMA1_Channel4_IRQHandler(void);

#endif
40 changes: 38 additions & 2 deletions dma/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <timer.h>
#include <main.h>

const char * msg = "Hello world\r\n\0";
const char *msg = "Hello world\r\n\0";

void dma1_clock_enable(void)
{
Expand All @@ -41,17 +41,35 @@ void dma_usart_tx_init(void)
DMA1_Channel4->CCR |= DMA_CCR_MINC;

// enable circular mode
DMA1_Channel4->CCR |= DMA_CCR_CIRC;
// DMA1_Channel4->CCR |= DMA_CCR_CIRC;

// set data transfer direction - memory -> peripheral
DMA1_Channel4->CCR |= DMA_CCR_DIR;

// enable transmission complete interrupt
DMA1_Channel4->CCR |= DMA_CCR_TCIE;
}

void dma_usart_tx_enable(void)
{
DMA1_Channel4->CCR |= DMA_CCR_EN;
}

void dma_usart_tx_disable(void)
{
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
}

void DMA1_Channel4_IRQHandler(void)
{
if (DMA1->ISR & DMA_ISR_TCIF4)
{
// clear interrupt flasg
DMA1->IFCR |= DMA_IFCR_CGIF4;
dma_usart_tx_disable();
}
}

void usart1_init(void)
{
// enable clock for GPIOA and USART1
Expand Down Expand Up @@ -84,9 +102,27 @@ void usart1_enable(void)

int main(void)
{
SysTick_Config(SystemCoreClock/1000);

dma1_clock_enable();
usart1_init();
dma_usart_tx_init();
dma_usart_tx_enable();
usart1_enable();

// enable interrupt for channel 4
NVIC_EnableIRQ(DMA1_Channel4_IRQn);

dma_usart_tx_enable();

usart1_enable();

while (1)
{
delay(2000);

// reload no. of transactions
DMA1_Channel4->CNDTR = 13;
dma_usart_tx_enable();
}
}

0 comments on commit de3e527

Please sign in to comment.