Skip to content

Commit

Permalink
Merge pull request #20 from niwciu/feature/readme_file_update
Browse files Browse the repository at this point in the history
Feature/readme file update
  • Loading branch information
niwciu authored Aug 4, 2024
2 parents 2fb5034 + 7116051 commit e7a6060
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_lizard_lib_check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Lizard Library Check
name: Run Code Complexity Check
on:
workflow_dispatch:
push:
Expand Down
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{
"cwd": "${workspaceFolder}",
"executable": "./examples/STM32G070RB_MODBUS_SLAVE/Debug/STM32G070RB_MODBUS_SLAVE.elf",
"name": "Debug with OpenOCD ModBus Slave",
"name": "Debug with OpenOCD ModBus Slave example",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
Expand Down
103 changes: 94 additions & 9 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# ModBus corss platform C library containing Master and Slave RTU implementation
# ModBus corss platform C library
![GitHub License](https://img.shields.io/github/license/niwciu/MODBUS)
![GitHub top language](https://img.shields.io/github/languages/top/niwciu/MODBUS)
![GitHub Release](https://img.shields.io/github/v/release/niwciu/MODBUS)
![GitHub branch check runs](https://img.shields.io/github/check-runs/niwciu/LCD_HD44780/main)
![example workflow](https://github.com/niwciu/MODBUS/actions/workflows/run_modbus_tests.yml/badge.svg)
![example workflow](https://github.com/niwciu/MODBUS/actions/workflows/run_cppcheck.yml/badge.svg)
![example workflow](https://github.com/niwciu/MODBUS/actions/workflows/clang-format_check.yml/badge.svg)
![example workflow](https://github.com/niwciu/MODBUS/actions/workflows/run_lizard_lib_check.yml/badge.svg)

## Features
- Contain ModBus RTU Master and Slave implementation
- Support the following functions:
Expand All @@ -24,11 +33,87 @@ Library contain an examples of three different configurations that can be implem
- ModBus RTU Master (ModBus RTU Client)
- ModBus RTU Slave&Master (ModBus RTU Sever&Client)

### How to run ModBus RTU Slave example
TBD
### How to run ModBus RTU Master example
TBD
### How to run ModBus RTU Master&Slave example
TBD
## How to use in project
TBD
### How to run STM32G071RB ModBus RTU Slave example
#### Requirements for compiling and running the example:
1. CMake installed
2. Make or Ninja installed
3. ARM GNU Toolchain (gcc-arm-none-eabi) installed
4. STM32_Programmer_CLI installed
5. ST-link (placed on Nucleo Board) installed

In order to test the slave node, any software that can simulate Modbus Master RTU is required. In my case I was using [qModMaster](https://sourceforge.net/projects/qmodmaster/)


#### Hardware requirements, configuration, and connections
1. STM32G071 Nucleo-64<br>
![drawing](https://raw.githubusercontent.com/niwciu/MODBUS/main/examples/doc/images/STM32G071RB_Nucleo.png){width=500;}

#### How to build and run the example
1. Open the location you want to clone the repository to in your termina
3. Clone the repository to your preferred localization
```bash
git clone https://github.com/niwciu/MODBUS.git
```
4. Enter to MODBUS/examples/STM32G071RB_MODBUS_SLAVE
```bash
cd ./MODBUS/examples/STM32G071RB_MODBUS_SLAVE
```
5. For Make type:
```bash
cmake -S ./ -B Debug -G"Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
make all
```
```bash
make flash
```
6. For Ninja type:
```bash
cmake -S ./ -B Debug -G"Ninja" -DCMAKE_BUILD_TYPE=Debug
```
```bash
cd Debug
```
```bash
ninja
```
```bash
ninja flash
```
7. In some cases Nucleo board requires plugging out and in a USB port to run the program.
### How to run ModBus STM32G071RB RTU Master example
#### Requirements for compiling and running the example:
1. CMake installed
2. Make or Ninja installed
3. ARM GNU Toolchain (gcc-arm-none-eabi) installed
4. STM32_Programmer_CLI installed
5. ST-link (placed on Nucleo Board) installed

In order to test the Master node, any software that can simulate Modbus Slave RTU is required. In my case I was using [xxx]()


#### Hardware requirements, configuration, and connections
1. STM32G071 Nucleo-64<br>
![drawing](https://raw.githubusercontent.com/niwciu/MODBUS/main/examples/doc/images/STM32G071RB_Nucleo.png){width=500;}
### How to run ModBus STM32G071RB RTU Master&Slave example
#### Requirements for compiling and running the example:
1. CMake installed
2. Make or Ninja installed
3. ARM GNU Toolchain (gcc-arm-none-eabi) installed
4. STM32_Programmer_CLI installed
5. ST-link (placed on Nucleo Board) installed

In order to test the Master node, any software that can simulate Modbus Slave RTU is required. In my case I was using [xxx]()

#### Hardware requirements, configuration, and connections
1. STM32G071 Nucleo-64<br>
![drawing](https://raw.githubusercontent.com/niwciu/MODBUS/main/examples/doc/images/STM32G071RB_Nucleo.png){width=500;}

## How to configure, use in project, colaborate and more
For information about the configuration and usage of the library, as well as collaboration in the project please refer to
[MODBUS wiki](https://github.com/niwciu/MODBUS/wiki)

11 changes: 6 additions & 5 deletions examples/STM32G070RB_MODBUS_SLAVE/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set(CUSTOM_TARGETS_FILE_DIR ./custom_targets.cmake)
# Allow assembler
enable_language(ASM)

project(STM32G070RB_MODBUS_SLAVE_HW_TEST)
project(STM32G070RB_MODBUS_SLAVE)

set(INCLUDE_DIRS
Core
Expand All @@ -37,8 +37,8 @@ Drivers
Drivers/CMSIS
Drivers/CMSIS/Device
Drivers/CMSIS/Include
Drivers/CMSIS/STM32G0_Drivers
Drivers/STM32G0_Drivers/core_init
# Drivers/CMSIS/STM32G0_Drivers
# Drivers/STM32G0_Drivers/core_init

../../src
../../src/common
Expand All @@ -51,8 +51,9 @@ set(C_SRCS
Core/Src/main.c
Core/Src/syscalls.c
Core/Src/sysmem.c
Drivers/STM32G0_Drivers/core_init/core_init.c
Drivers/STM32G0_Drivers/modbus_driver_interface.c
Core/Src/core_init.c
Core/Src/modbus_driver_interface.c
Core/Src/LED_driver.c

../../src/slave_internall/modbus_slave_PDU.c
../../src/slave_internall/modbus_slave_RTU.c
Expand Down
36 changes: 36 additions & 0 deletions examples/STM32G070RB_MODBUS_SLAVE/Core/Inc/LED_driver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file LED_driver.h
* @author niwciu (niwciu@gmail.com)
* @brief simple module for driving LED on nucleo board. Allowe to init turn on and off the LED
* @version 0.0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/

#ifndef _LED_DRIVER_H_
#define _LED_DRIVER_H_

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

#include "stm32g070xx.h"

// LED port and pin defines
#define LED_PORT (GPIOA)
#define LED_PORT_CLK_EN (RCC_IOPENR_GPIOAEN)
#define MODER_LED_Msk (GPIO_MODER_MODE5_Msk)
#define MODER_LED_0 (GPIO_MODER_MODE5_0)
#define LED_OUT_PIN (GPIO_ODR_OD5)

void LED_init(void);
void LED_on(void);
void LED_off(void);

#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _LED_DRIVER_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#define _CORE_INIT_H_

#ifdef __cplusplus
extern "C" {
extern "C"
{
#endif /* __cplusplus */


void core_init(void);
void core_init(void);

#ifdef __cplusplus
}
Expand Down
57 changes: 57 additions & 0 deletions examples/STM32G070RB_MODBUS_SLAVE/Core/Inc/modbus_driver_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file modbus_driver_config.h
* @author niwciu (niwciu@gmail.com)
* @brief Header file with defines that speed up modbus driver configuration specially when porting driver to different USARTs and Timers
* @version 0.0.1
* @date 2024-05-24
*
* @copyright Copyright (c) 2024
*
*/
#pragma once

#include "stm32g070xx.h"

#define RX_DATA_BUF_SIZE 256
#define LOW 0
#define HIGH 1

/** modbus USART configuration*/
#define MODBUS_USART (USART2)
#define MODBUS_USART_CLK APBENR1
#define MODBUS_USART_CLK_EN (RCC_APBENR1_USART2EN)
#define MODBUS_USART_CLK_FREQ 64000000UL
#define MODBUS_USART_IRQN USART2_IRQn
#define MODBUS_USART_IRQHandler USART2_IRQHandler
#define MODBUS_USART_IRQ_PRIORITY 10

#define MODBUS_USART_PORT (GPIOA)

#define MODBUS_USART_RX_MODE (GPIO_MODER_MODE3_1)
#define MODBUS_USART_TX_MODE (GPIO_MODER_MODE2_1)
#define MODBUS_USART_DE_MODE (GPIO_MODER_MODE1_1)

#define MODBUS_USART_RX_MODE_Msk (GPIO_MODER_MODE3)
#define MODBUS_USART_TX_MODE_Msk (GPIO_MODER_MODE2)
#define MODBUS_USART_DE_MODE_Msk (GPIO_MODER_MODE1)

#define MODBUS_USART_RX_AF_Msk (GPIO_AFRL_AFSEL3)
#define MODBUS_USART_TX_AF_Msk (GPIO_AFRL_AFSEL2)
#define MODBUS_USART_DE_AF_Msk (GPIO_AFRL_AFSEL1)

#define MODBUS_USART_RX_AF (GPIO_AFRL_AFSEL3_0)
#define MODBUS_USART_TX_AF (GPIO_AFRL_AFSEL2_0)
#define MODBUS_USART_DE_AF (GPIO_AFRL_AFSEL1_0)

#define MODBUS_USART_AF_REG (LOW)

#define MODBUS_USART_GPIO_CLK_EN (RCC_IOPENR_GPIOAEN)

/** modbus Timer configuration*/
#define MODBUS_TIMER (TIM7)
#define MODBUS_TIMER_CLK_EN (RCC_APBENR1_TIM7EN)
#define MODBUS_TIMER_CLK_FREQ 64000000UL
#define MODBUS_TIMER_IRQN TIM7_IRQn
#define MODBUS_TIMER_IRQHandler TIM7_LPTIM2_IRQHandler
#define MODBUS_TIMER_IRQ_PRIORITY 10
27 changes: 27 additions & 0 deletions examples/STM32G070RB_MODBUS_SLAVE/Core/Src/LED_driver.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file LED_driver.c
* @author niwciu (niwciu@gmail.com)
* @brief simple module for driving LED on nucleo board. Allowe to init turn on and off the LED
* @version 0.0.1
* @date 2024-08-04
*
* @copyright Copyright (c) 2024
*
*/
#include "LED_driver.h"

void LED_init(void)
{
RCC->IOPENR |= LED_PORT_CLK_EN;
LED_PORT->MODER &= (~MODER_LED_Msk);
LED_PORT->MODER |= MODER_LED_0;
}

void LED_on(void)
{
LED_PORT->ODR |= LED_OUT_PIN;
}
void LED_off(void)
{
LED_PORT->ODR &= ~LED_OUT_PIN;
}
71 changes: 52 additions & 19 deletions examples/STM32G070RB_MODBUS_SLAVE/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,68 @@
******************************************************************************
*/

#include <stdint.h>
#include "main.h"
#include "core_init.h"
#include "modbus_slave.h"
#include "LED_driver.h"

#include <stdio.h>
#include <stdint.h>

#if !defined(__SOFT_FP__) && defined(__ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif

modbus_coil_disin_t [20] = {1,0,1,0,0,1,0,1,1,1,0,0,0,1,1,1,1,0,0,1};
modbus_reg_t hreg[20] = {2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,3001,3002,3003,3004,3005,3006,3007,3008,3009,3010};
modbus_reg_t inreg[20] = {4201, 4202, 4203, 4204, 4205, 4206, 4207, 4208, 4209, 4210, 4301, 4302, 4303, 4304, 4305, 4306, 4307, 4308, 4309, 4310};
#define EXAMPLE_DATA_QTY 20


modbus_coil_disin_t coil_data[EXAMPLE_DATA_QTY] = {1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1};
modbus_coil_disin_t din_data[EXAMPLE_DATA_QTY] = {1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1};
modbus_reg_t hreg_data[EXAMPLE_DATA_QTY] = {2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 3001, 3002, 3003, 3004, 3005, 3006, 3007, 3008, 3009, 3010};
modbus_reg_t inreg_data[EXAMPLE_DATA_QTY] = {4201, 4202, 4203, 4204, 4205, 4206, 4207, 4208, 4209, 4210, 4301, 4302, 4303, 4304, 4305, 4306, 4307, 4308, 4309, 4310};
modbus_device_ID_t Slave_ID = 0x01;

modbus_coil_disin_t LED_status = 0;

static void register_modbus_data(void);
static void update_LED(void);


int main(void)
{
core_init();
modbus_slave_init(RTU,115200,NONE,Slave_ID);
for (int i=0;i<20;i++)
{
register_app_data_to_modbus_slave_coils_table(i,&coils[i]);
register_app_data_to_modbus_slave_hreg_table(i,&hreg[i]);
register_app_data_to_modbus_slave_inreg_table( i, &inreg[i]);
}

/* Loop forever */
while(1)
{
check_modbus_request();
}
core_init();
LED_init();
modbus_slave_init(RTU, 115200, NONE, Slave_ID);
register_modbus_data();

/* Loop forever */
while (1)
{
check_modbus_request();

update_LED();
}
}

static void register_modbus_data(void)
{
for (int i = 0; i < 20; i++)
{
register_app_data_to_modbus_slave_coils_table(i, &coil_data[i]);
register_app_data_to_modbus_slave_din_table(i, din_data + i);
register_app_data_to_modbus_slave_hreg_table(i, &hreg_data[i]);
register_app_data_to_modbus_slave_inreg_table(i, &inreg_data[i]);
}
register_app_data_to_modbus_slave_coils_table(21,&LED_status);
}
static void update_LED(void)
{
if (LED_status == 1)
{
LED_on();
}
else
{
LED_off();
}
}
Loading

0 comments on commit e7a6060

Please sign in to comment.