增加Modbus
This commit is contained in:
parent
4fc6228ab9
commit
2057b27d55
33
.mxproject
Normal file
33
.mxproject
Normal file
File diff suppressed because one or more lines are too long
71
Core/Inc/main.h
Normal file
71
Core/Inc/main.h
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
/* USER CODE BEGIN Header */
|
||||||
|
/**
|
||||||
|
******************************************************************************
|
||||||
|
* @file : main.h
|
||||||
|
* @brief : Header for main.c file.
|
||||||
|
* This file contains the common defines of the application.
|
||||||
|
******************************************************************************
|
||||||
|
* @attention
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 STMicroelectronics.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is licensed under terms that can be found in the LICENSE file
|
||||||
|
* in the root directory of this software component.
|
||||||
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*/
|
||||||
|
/* USER CODE END Header */
|
||||||
|
|
||||||
|
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||||
|
#ifndef __MAIN_H
|
||||||
|
#define __MAIN_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Includes ------------------------------------------------------------------*/
|
||||||
|
#include "stm32f1xx_hal.h"
|
||||||
|
|
||||||
|
/* Private includes ----------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN Includes */
|
||||||
|
|
||||||
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
|
/* Exported types ------------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN ET */
|
||||||
|
|
||||||
|
/* USER CODE END ET */
|
||||||
|
|
||||||
|
/* Exported constants --------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN EC */
|
||||||
|
|
||||||
|
/* USER CODE END EC */
|
||||||
|
|
||||||
|
/* Exported macro ------------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN EM */
|
||||||
|
|
||||||
|
/* USER CODE END EM */
|
||||||
|
|
||||||
|
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
|
||||||
|
|
||||||
|
/* Exported functions prototypes ---------------------------------------------*/
|
||||||
|
void Error_Handler(void);
|
||||||
|
|
||||||
|
/* USER CODE BEGIN EFP */
|
||||||
|
|
||||||
|
/* USER CODE END EFP */
|
||||||
|
|
||||||
|
/* Private defines -----------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* USER CODE BEGIN Private defines */
|
||||||
|
|
||||||
|
/* USER CODE END Private defines */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __MAIN_H */
|
||||||
391
Core/Inc/stm32f1xx_hal_conf.h
Normal file
391
Core/Inc/stm32f1xx_hal_conf.h
Normal file
@ -0,0 +1,391 @@
|
|||||||
|
/* USER CODE BEGIN Header */
|
||||||
|
/**
|
||||||
|
******************************************************************************
|
||||||
|
* @file stm32f1xx_hal_conf.h
|
||||||
|
* @brief HAL configuration file.
|
||||||
|
******************************************************************************
|
||||||
|
* @attention
|
||||||
|
*
|
||||||
|
* Copyright (c) 2017 STMicroelectronics.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is licensed under terms that can be found in the LICENSE file
|
||||||
|
* in the root directory of this software component.
|
||||||
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*/
|
||||||
|
/* USER CODE END Header */
|
||||||
|
|
||||||
|
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||||
|
#ifndef __STM32F1xx_HAL_CONF_H
|
||||||
|
#define __STM32F1xx_HAL_CONF_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Exported types ------------------------------------------------------------*/
|
||||||
|
/* Exported constants --------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* ########################## Module Selection ############################## */
|
||||||
|
/**
|
||||||
|
* @brief This is the list of modules to be used in the HAL driver
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define HAL_MODULE_ENABLED
|
||||||
|
#define HAL_ADC_MODULE_ENABLED
|
||||||
|
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||||
|
/*#define HAL_CAN_MODULE_ENABLED */
|
||||||
|
/*#define HAL_CAN_LEGACY_MODULE_ENABLED */
|
||||||
|
/*#define HAL_CEC_MODULE_ENABLED */
|
||||||
|
/*#define HAL_CORTEX_MODULE_ENABLED */
|
||||||
|
/*#define HAL_CRC_MODULE_ENABLED */
|
||||||
|
/*#define HAL_DAC_MODULE_ENABLED */
|
||||||
|
#define HAL_DMA_MODULE_ENABLED
|
||||||
|
/*#define HAL_ETH_MODULE_ENABLED */
|
||||||
|
/*#define HAL_FLASH_MODULE_ENABLED */
|
||||||
|
#define HAL_GPIO_MODULE_ENABLED
|
||||||
|
/*#define HAL_I2C_MODULE_ENABLED */
|
||||||
|
/*#define HAL_I2S_MODULE_ENABLED */
|
||||||
|
/*#define HAL_IRDA_MODULE_ENABLED */
|
||||||
|
/*#define HAL_IWDG_MODULE_ENABLED */
|
||||||
|
/*#define HAL_NOR_MODULE_ENABLED */
|
||||||
|
/*#define HAL_NAND_MODULE_ENABLED */
|
||||||
|
/*#define HAL_PCCARD_MODULE_ENABLED */
|
||||||
|
/*#define HAL_PCD_MODULE_ENABLED */
|
||||||
|
/*#define HAL_HCD_MODULE_ENABLED */
|
||||||
|
/*#define HAL_PWR_MODULE_ENABLED */
|
||||||
|
/*#define HAL_RCC_MODULE_ENABLED */
|
||||||
|
/*#define HAL_RTC_MODULE_ENABLED */
|
||||||
|
/*#define HAL_SD_MODULE_ENABLED */
|
||||||
|
/*#define HAL_MMC_MODULE_ENABLED */
|
||||||
|
/*#define HAL_SDRAM_MODULE_ENABLED */
|
||||||
|
/*#define HAL_SMARTCARD_MODULE_ENABLED */
|
||||||
|
/*#define HAL_SPI_MODULE_ENABLED */
|
||||||
|
/*#define HAL_SRAM_MODULE_ENABLED */
|
||||||
|
#define HAL_TIM_MODULE_ENABLED
|
||||||
|
#define HAL_UART_MODULE_ENABLED
|
||||||
|
/*#define HAL_USART_MODULE_ENABLED */
|
||||||
|
/*#define HAL_WWDG_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#define HAL_CORTEX_MODULE_ENABLED
|
||||||
|
#define HAL_DMA_MODULE_ENABLED
|
||||||
|
#define HAL_FLASH_MODULE_ENABLED
|
||||||
|
#define HAL_EXTI_MODULE_ENABLED
|
||||||
|
#define HAL_GPIO_MODULE_ENABLED
|
||||||
|
#define HAL_PWR_MODULE_ENABLED
|
||||||
|
#define HAL_RCC_MODULE_ENABLED
|
||||||
|
|
||||||
|
/* ########################## Oscillator Values adaptation ####################*/
|
||||||
|
/**
|
||||||
|
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
|
||||||
|
* This value is used by the RCC HAL module to compute the system frequency
|
||||||
|
* (when HSE is used as system clock source, directly or through the PLL).
|
||||||
|
*/
|
||||||
|
#if !defined (HSE_VALUE)
|
||||||
|
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
|
||||||
|
#endif /* HSE_VALUE */
|
||||||
|
|
||||||
|
#if !defined (HSE_STARTUP_TIMEOUT)
|
||||||
|
#define HSE_STARTUP_TIMEOUT 100U /*!< Time out for HSE start up, in ms */
|
||||||
|
#endif /* HSE_STARTUP_TIMEOUT */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Internal High Speed oscillator (HSI) value.
|
||||||
|
* This value is used by the RCC HAL module to compute the system frequency
|
||||||
|
* (when HSI is used as system clock source, directly or through the PLL).
|
||||||
|
*/
|
||||||
|
#if !defined (HSI_VALUE)
|
||||||
|
#define HSI_VALUE 8000000U /*!< Value of the Internal oscillator in Hz*/
|
||||||
|
#endif /* HSI_VALUE */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Internal Low Speed oscillator (LSI) value.
|
||||||
|
*/
|
||||||
|
#if !defined (LSI_VALUE)
|
||||||
|
#define LSI_VALUE 40000U /*!< LSI Typical Value in Hz */
|
||||||
|
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
|
||||||
|
The real value may vary depending on the variations
|
||||||
|
in voltage and temperature. */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief External Low Speed oscillator (LSE) value.
|
||||||
|
* This value is used by the UART, RTC HAL module to compute the system frequency
|
||||||
|
*/
|
||||||
|
#if !defined (LSE_VALUE)
|
||||||
|
#define LSE_VALUE 32768U /*!< Value of the External oscillator in Hz*/
|
||||||
|
#endif /* LSE_VALUE */
|
||||||
|
|
||||||
|
#if !defined (LSE_STARTUP_TIMEOUT)
|
||||||
|
#define LSE_STARTUP_TIMEOUT 5000U /*!< Time out for LSE start up, in ms */
|
||||||
|
#endif /* LSE_STARTUP_TIMEOUT */
|
||||||
|
|
||||||
|
/* Tip: To avoid modifying this file each time you need to use different HSE,
|
||||||
|
=== you can define the HSE value in your toolchain compiler preprocessor. */
|
||||||
|
|
||||||
|
/* ########################### System Configuration ######################### */
|
||||||
|
/**
|
||||||
|
* @brief This is the HAL system configuration section
|
||||||
|
*/
|
||||||
|
#define VDD_VALUE 3300U /*!< Value of VDD in mv */
|
||||||
|
#define TICK_INT_PRIORITY 15U /*!< tick interrupt priority (lowest by default) */
|
||||||
|
#define USE_RTOS 0U
|
||||||
|
#define PREFETCH_ENABLE 1U
|
||||||
|
|
||||||
|
#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */
|
||||||
|
#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */
|
||||||
|
#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */
|
||||||
|
#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */
|
||||||
|
#define USE_HAL_ETH_REGISTER_CALLBACKS 0U /* ETH register callback disabled */
|
||||||
|
#define USE_HAL_HCD_REGISTER_CALLBACKS 0U /* HCD register callback disabled */
|
||||||
|
#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */
|
||||||
|
#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */
|
||||||
|
#define USE_HAL_MMC_REGISTER_CALLBACKS 0U /* MMC register callback disabled */
|
||||||
|
#define USE_HAL_NAND_REGISTER_CALLBACKS 0U /* NAND register callback disabled */
|
||||||
|
#define USE_HAL_NOR_REGISTER_CALLBACKS 0U /* NOR register callback disabled */
|
||||||
|
#define USE_HAL_PCCARD_REGISTER_CALLBACKS 0U /* PCCARD register callback disabled */
|
||||||
|
#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */
|
||||||
|
#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */
|
||||||
|
#define USE_HAL_SD_REGISTER_CALLBACKS 0U /* SD register callback disabled */
|
||||||
|
#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */
|
||||||
|
#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */
|
||||||
|
#define USE_HAL_SRAM_REGISTER_CALLBACKS 0U /* SRAM register callback disabled */
|
||||||
|
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */
|
||||||
|
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */
|
||||||
|
#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */
|
||||||
|
#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */
|
||||||
|
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */
|
||||||
|
|
||||||
|
/* ########################## Assert Selection ############################## */
|
||||||
|
/**
|
||||||
|
* @brief Uncomment the line below to expanse the "assert_param" macro in the
|
||||||
|
* HAL drivers code
|
||||||
|
*/
|
||||||
|
/* #define USE_FULL_ASSERT 1U */
|
||||||
|
|
||||||
|
/* ################## Ethernet peripheral configuration ##################### */
|
||||||
|
|
||||||
|
/* Section 1 : Ethernet peripheral configuration */
|
||||||
|
|
||||||
|
/* MAC ADDRESS: MAC_ADDR0:MAC_ADDR1:MAC_ADDR2:MAC_ADDR3:MAC_ADDR4:MAC_ADDR5 */
|
||||||
|
#define MAC_ADDR0 2U
|
||||||
|
#define MAC_ADDR1 0U
|
||||||
|
#define MAC_ADDR2 0U
|
||||||
|
#define MAC_ADDR3 0U
|
||||||
|
#define MAC_ADDR4 0U
|
||||||
|
#define MAC_ADDR5 0U
|
||||||
|
|
||||||
|
/* Definition of the Ethernet driver buffers size and count */
|
||||||
|
#define ETH_RX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for receive */
|
||||||
|
#define ETH_TX_BUF_SIZE ETH_MAX_PACKET_SIZE /* buffer size for transmit */
|
||||||
|
#define ETH_RXBUFNB 8U /* 4 Rx buffers of size ETH_RX_BUF_SIZE */
|
||||||
|
#define ETH_TXBUFNB 4U /* 4 Tx buffers of size ETH_TX_BUF_SIZE */
|
||||||
|
|
||||||
|
/* Section 2: PHY configuration section */
|
||||||
|
|
||||||
|
/* DP83848_PHY_ADDRESS Address*/
|
||||||
|
#define DP83848_PHY_ADDRESS 0x01U
|
||||||
|
/* PHY Reset delay these values are based on a 1 ms Systick interrupt*/
|
||||||
|
#define PHY_RESET_DELAY 0x000000FFU
|
||||||
|
/* PHY Configuration delay */
|
||||||
|
#define PHY_CONFIG_DELAY 0x00000FFFU
|
||||||
|
|
||||||
|
#define PHY_READ_TO 0x0000FFFFU
|
||||||
|
#define PHY_WRITE_TO 0x0000FFFFU
|
||||||
|
|
||||||
|
/* Section 3: Common PHY Registers */
|
||||||
|
|
||||||
|
#define PHY_BCR ((uint16_t)0x00) /*!< Transceiver Basic Control Register */
|
||||||
|
#define PHY_BSR ((uint16_t)0x01) /*!< Transceiver Basic Status Register */
|
||||||
|
|
||||||
|
#define PHY_RESET ((uint16_t)0x8000) /*!< PHY Reset */
|
||||||
|
#define PHY_LOOPBACK ((uint16_t)0x4000) /*!< Select loop-back mode */
|
||||||
|
#define PHY_FULLDUPLEX_100M ((uint16_t)0x2100) /*!< Set the full-duplex mode at 100 Mb/s */
|
||||||
|
#define PHY_HALFDUPLEX_100M ((uint16_t)0x2000) /*!< Set the half-duplex mode at 100 Mb/s */
|
||||||
|
#define PHY_FULLDUPLEX_10M ((uint16_t)0x0100) /*!< Set the full-duplex mode at 10 Mb/s */
|
||||||
|
#define PHY_HALFDUPLEX_10M ((uint16_t)0x0000) /*!< Set the half-duplex mode at 10 Mb/s */
|
||||||
|
#define PHY_AUTONEGOTIATION ((uint16_t)0x1000) /*!< Enable auto-negotiation function */
|
||||||
|
#define PHY_RESTART_AUTONEGOTIATION ((uint16_t)0x0200) /*!< Restart auto-negotiation function */
|
||||||
|
#define PHY_POWERDOWN ((uint16_t)0x0800) /*!< Select the power down mode */
|
||||||
|
#define PHY_ISOLATE ((uint16_t)0x0400) /*!< Isolate PHY from MII */
|
||||||
|
|
||||||
|
#define PHY_AUTONEGO_COMPLETE ((uint16_t)0x0020) /*!< Auto-Negotiation process completed */
|
||||||
|
#define PHY_LINKED_STATUS ((uint16_t)0x0004) /*!< Valid link established */
|
||||||
|
#define PHY_JABBER_DETECTION ((uint16_t)0x0002) /*!< Jabber condition detected */
|
||||||
|
|
||||||
|
/* Section 4: Extended PHY Registers */
|
||||||
|
#define PHY_SR ((uint16_t)0x10U) /*!< PHY status register Offset */
|
||||||
|
|
||||||
|
#define PHY_SPEED_STATUS ((uint16_t)0x0002U) /*!< PHY Speed mask */
|
||||||
|
#define PHY_DUPLEX_STATUS ((uint16_t)0x0004U) /*!< PHY Duplex mask */
|
||||||
|
|
||||||
|
/* ################## SPI peripheral configuration ########################## */
|
||||||
|
|
||||||
|
/* CRC FEATURE: Use to activate CRC feature inside HAL SPI Driver
|
||||||
|
* Activated: CRC code is present inside driver
|
||||||
|
* Deactivated: CRC code cleaned from driver
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define USE_SPI_CRC 0U
|
||||||
|
|
||||||
|
/* Includes ------------------------------------------------------------------*/
|
||||||
|
/**
|
||||||
|
* @brief Include module's header file
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef HAL_RCC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_rcc.h"
|
||||||
|
#endif /* HAL_RCC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_GPIO_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_gpio.h"
|
||||||
|
#endif /* HAL_GPIO_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_EXTI_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_exti.h"
|
||||||
|
#endif /* HAL_EXTI_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_DMA_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_dma.h"
|
||||||
|
#endif /* HAL_DMA_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_ETH_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_eth.h"
|
||||||
|
#endif /* HAL_ETH_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_CAN_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_can.h"
|
||||||
|
#endif /* HAL_CAN_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_CAN_LEGACY_MODULE_ENABLED
|
||||||
|
#include "Legacy/stm32f1xx_hal_can_legacy.h"
|
||||||
|
#endif /* HAL_CAN_LEGACY_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_CEC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_cec.h"
|
||||||
|
#endif /* HAL_CEC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_CORTEX_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_cortex.h"
|
||||||
|
#endif /* HAL_CORTEX_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_ADC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_adc.h"
|
||||||
|
#endif /* HAL_ADC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_CRC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_crc.h"
|
||||||
|
#endif /* HAL_CRC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_DAC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_dac.h"
|
||||||
|
#endif /* HAL_DAC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_FLASH_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_flash.h"
|
||||||
|
#endif /* HAL_FLASH_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_SRAM_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_sram.h"
|
||||||
|
#endif /* HAL_SRAM_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_NOR_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_nor.h"
|
||||||
|
#endif /* HAL_NOR_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_I2C_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_i2c.h"
|
||||||
|
#endif /* HAL_I2C_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_I2S_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_i2s.h"
|
||||||
|
#endif /* HAL_I2S_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_IWDG_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_iwdg.h"
|
||||||
|
#endif /* HAL_IWDG_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_PWR_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_pwr.h"
|
||||||
|
#endif /* HAL_PWR_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_RTC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_rtc.h"
|
||||||
|
#endif /* HAL_RTC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_PCCARD_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_pccard.h"
|
||||||
|
#endif /* HAL_PCCARD_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_SD_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_sd.h"
|
||||||
|
#endif /* HAL_SD_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_NAND_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_nand.h"
|
||||||
|
#endif /* HAL_NAND_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_SPI_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_spi.h"
|
||||||
|
#endif /* HAL_SPI_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_TIM_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_tim.h"
|
||||||
|
#endif /* HAL_TIM_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_UART_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_uart.h"
|
||||||
|
#endif /* HAL_UART_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_USART_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_usart.h"
|
||||||
|
#endif /* HAL_USART_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_IRDA_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_irda.h"
|
||||||
|
#endif /* HAL_IRDA_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_SMARTCARD_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_smartcard.h"
|
||||||
|
#endif /* HAL_SMARTCARD_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_WWDG_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_wwdg.h"
|
||||||
|
#endif /* HAL_WWDG_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_PCD_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_pcd.h"
|
||||||
|
#endif /* HAL_PCD_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_HCD_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_hcd.h"
|
||||||
|
#endif /* HAL_HCD_MODULE_ENABLED */
|
||||||
|
|
||||||
|
#ifdef HAL_MMC_MODULE_ENABLED
|
||||||
|
#include "stm32f1xx_hal_mmc.h"
|
||||||
|
#endif /* HAL_MMC_MODULE_ENABLED */
|
||||||
|
|
||||||
|
/* Exported macro ------------------------------------------------------------*/
|
||||||
|
#ifdef USE_FULL_ASSERT
|
||||||
|
/**
|
||||||
|
* @brief The assert_param macro is used for function's parameters check.
|
||||||
|
* @param expr If expr is false, it calls assert_failed function
|
||||||
|
* which reports the name of the source file and the source
|
||||||
|
* line number of the call that failed.
|
||||||
|
* If expr is true, it returns no value.
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
|
||||||
|
/* Exported functions ------------------------------------------------------- */
|
||||||
|
void assert_failed(uint8_t* file, uint32_t line);
|
||||||
|
#else
|
||||||
|
#define assert_param(expr) ((void)0U)
|
||||||
|
#endif /* USE_FULL_ASSERT */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __STM32F1xx_HAL_CONF_H */
|
||||||
|
|
||||||
69
Core/Inc/stm32f1xx_it.h
Normal file
69
Core/Inc/stm32f1xx_it.h
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
/* USER CODE BEGIN Header */
|
||||||
|
/**
|
||||||
|
******************************************************************************
|
||||||
|
* @file stm32f1xx_it.h
|
||||||
|
* @brief This file contains the headers of the interrupt handlers.
|
||||||
|
******************************************************************************
|
||||||
|
* @attention
|
||||||
|
*
|
||||||
|
* Copyright (c) 2025 STMicroelectronics.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This software is licensed under terms that can be found in the LICENSE file
|
||||||
|
* in the root directory of this software component.
|
||||||
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||||||
|
*
|
||||||
|
******************************************************************************
|
||||||
|
*/
|
||||||
|
/* USER CODE END Header */
|
||||||
|
|
||||||
|
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||||
|
#ifndef __STM32F1xx_IT_H
|
||||||
|
#define __STM32F1xx_IT_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Private includes ----------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN Includes */
|
||||||
|
|
||||||
|
/* USER CODE END Includes */
|
||||||
|
|
||||||
|
/* Exported types ------------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN ET */
|
||||||
|
|
||||||
|
/* USER CODE END ET */
|
||||||
|
|
||||||
|
/* Exported constants --------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN EC */
|
||||||
|
|
||||||
|
/* USER CODE END EC */
|
||||||
|
|
||||||
|
/* Exported macro ------------------------------------------------------------*/
|
||||||
|
/* USER CODE BEGIN EM */
|
||||||
|
|
||||||
|
/* USER CODE END EM */
|
||||||
|
|
||||||
|
/* Exported functions prototypes ---------------------------------------------*/
|
||||||
|
void NMI_Handler(void);
|
||||||
|
void HardFault_Handler(void);
|
||||||
|
void MemManage_Handler(void);
|
||||||
|
void BusFault_Handler(void);
|
||||||
|
void UsageFault_Handler(void);
|
||||||
|
void DebugMon_Handler(void);
|
||||||
|
void EXTI0_IRQHandler(void);
|
||||||
|
void DMA1_Channel1_IRQHandler(void);
|
||||||
|
void TIM1_UP_IRQHandler(void);
|
||||||
|
void TIM2_IRQHandler(void);
|
||||||
|
void TIM3_IRQHandler(void);
|
||||||
|
void TIM5_IRQHandler(void);
|
||||||
|
/* USER CODE BEGIN EFP */
|
||||||
|
|
||||||
|
/* USER CODE END EFP */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __STM32F1xx_IT_H */
|
||||||
83
Core/Src/Delay.c
Normal file
83
Core/Src/Delay.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/**************** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 *******************
|
||||||
|
* 文 件 名:
|
||||||
|
* 创 建 者: Kaiser
|
||||||
|
* 描 述 :
|
||||||
|
* 最后修改:
|
||||||
|
*********************************** 修订记录 ************************************
|
||||||
|
* 版 本:
|
||||||
|
* 修订人:
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
/******************************** 功能说明 *************************************
|
||||||
|
*
|
||||||
|
*******************************************************************************/
|
||||||
|
#include "stm32f10x.h"
|
||||||
|
void delay_us(uint32_t us)
|
||||||
|
{
|
||||||
|
while (us--) // 一个NOP一个指令周期 72Mhz下72个指令周期为1us 去掉us--的指令周期 去掉while判断和循环的指令周期 还需要约61个NOP
|
||||||
|
{
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
__NOP();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/******** (C) COPYRIGHT 2023 山东云唐智能科技有限公司 **** End Of File ********/
|
||||||
225
Core/Src/Modbus/MODBUS-Port-STM32.C
Normal file
225
Core/Src/Modbus/MODBUS-Port-STM32.C
Normal file
@ -0,0 +1,225 @@
|
|||||||
|
#include "mb.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
#include "mbutils.h"
|
||||||
|
#include "cmsis_os2.h"
|
||||||
|
#define MB_USART USART1
|
||||||
|
#define MB_TIMER TIM7
|
||||||
|
|
||||||
|
#include "portEvent.c"
|
||||||
|
#include "portSerial.c"
|
||||||
|
#include "portTimer.c"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define REG_INPUT_START 1
|
||||||
|
#define REG_INPUT_NREGS 80
|
||||||
|
USHORT usRegInputBuf[REG_INPUT_NREGS];
|
||||||
|
|
||||||
|
#define REG_HOLDING_START 1
|
||||||
|
#define REG_HOLDING_NREGS 80
|
||||||
|
USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
|
||||||
|
|
||||||
|
#define REG_DISC_START 1
|
||||||
|
#define REG_DISC_SIZE 80
|
||||||
|
UCHAR ucRegDiscBuf[(REG_DISC_SIZE+7U)/8U];
|
||||||
|
|
||||||
|
#define REG_COILS_START 1
|
||||||
|
#define REG_COILS_SIZE 80
|
||||||
|
UCHAR ucRegCoilsBuf[(REG_COILS_SIZE+7U)/8U];
|
||||||
|
|
||||||
|
#define REG_NVRAM_START 1001
|
||||||
|
#define REG_NVRAM_NREGS 1000
|
||||||
|
extern void delay_us(uint32_t us);
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
|
||||||
|
// 读数字寄存器 功能码0x04
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
int16_t iRegIndex;
|
||||||
|
delay_us( 50u );
|
||||||
|
if( ( usAddress >= REG_INPUT_START )
|
||||||
|
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
|
||||||
|
{
|
||||||
|
iRegIndex = (int16_t)( usAddress - REG_INPUT_START );
|
||||||
|
while( usNRegs > 0 )
|
||||||
|
{
|
||||||
|
*pucRegBuffer++ = (uint8_t)( usRegInputBuf[iRegIndex] >> 8 );
|
||||||
|
*pucRegBuffer++ = (uint8_t)( usRegInputBuf[iRegIndex] & 0xFF );
|
||||||
|
iRegIndex++;
|
||||||
|
usNRegs--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOREG;
|
||||||
|
}
|
||||||
|
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 寄存器的读写函数 支持的命令为读 0x03 和写0x06
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
int16_t iRegIndex;
|
||||||
|
delay_us( 50u );
|
||||||
|
if( ( usAddress >= REG_HOLDING_START ) && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
|
||||||
|
{ // 访问的地址空间是RAM映像区
|
||||||
|
iRegIndex = (int16_t)( usAddress - REG_HOLDING_START );
|
||||||
|
switch ( eMode )
|
||||||
|
{
|
||||||
|
case MB_REG_READ:
|
||||||
|
while( usNRegs > 0 )
|
||||||
|
{
|
||||||
|
*pucRegBuffer++ = (uint8_t)( usRegHoldingBuf[iRegIndex] >> 8 );
|
||||||
|
*pucRegBuffer++ = (uint8_t)( usRegHoldingBuf[iRegIndex] & 0xFF );
|
||||||
|
iRegIndex++;
|
||||||
|
usNRegs--;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MB_REG_WRITE:
|
||||||
|
while( usNRegs > 0 )
|
||||||
|
{
|
||||||
|
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
||||||
|
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
||||||
|
iRegIndex++;
|
||||||
|
usNRegs--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if( ( usAddress >= REG_NVRAM_START ) && ( usAddress + usNRegs <= REG_NVRAM_START + REG_NVRAM_NREGS ) )
|
||||||
|
{ // 访问的地址空间是 NVRAM 区
|
||||||
|
iRegIndex = ( int )( usAddress - REG_NVRAM_START );
|
||||||
|
switch ( eMode )
|
||||||
|
{
|
||||||
|
case MB_REG_READ:
|
||||||
|
// while( usNRegs > 0 )
|
||||||
|
// {
|
||||||
|
// *pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
|
||||||
|
// *pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF );
|
||||||
|
// iRegIndex++;
|
||||||
|
// usNRegs--;
|
||||||
|
// }
|
||||||
|
// NVRAM_Load( iRegIndex * 2u, pucRegBuffer, usNRegs * 2u );
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MB_REG_WRITE:
|
||||||
|
// NVRAM_Save( iRegIndex * 2u, pucRegBuffer, usNRegs * 2u );
|
||||||
|
// while( usNRegs > 0 )
|
||||||
|
// {
|
||||||
|
// usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
||||||
|
// usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
||||||
|
// iRegIndex++;
|
||||||
|
// usNRegs--;
|
||||||
|
// }
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOREG;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读/写开关寄存器 0x01 0x05
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
int16_t iNCoils = (int16_t)usNCoils;
|
||||||
|
uint16_t usBitOffset;
|
||||||
|
|
||||||
|
delay_us( 50u );
|
||||||
|
/* Check if we have registers mapped at this block. */
|
||||||
|
if( ( usAddress >= REG_COILS_START ) &&
|
||||||
|
( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
|
||||||
|
{
|
||||||
|
usBitOffset = (uint16_t)( usAddress - REG_COILS_START );
|
||||||
|
switch ( eMode )
|
||||||
|
{
|
||||||
|
/* Read current values and pass to protocol stack. */
|
||||||
|
case MB_REG_READ:
|
||||||
|
while( iNCoils > 0 )
|
||||||
|
{
|
||||||
|
*pucRegBuffer++ = xMBUtilGetBits( ucRegCoilsBuf, usBitOffset, (uint8_t)( iNCoils > 8 ? 8 : iNCoils ));
|
||||||
|
iNCoils -= 8;
|
||||||
|
usBitOffset += 8;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Update current register values. */
|
||||||
|
case MB_REG_WRITE:
|
||||||
|
while( iNCoils > 0 )
|
||||||
|
{
|
||||||
|
xMBUtilSetBits( ucRegCoilsBuf, usBitOffset, (uint8_t)( iNCoils > 8 ? 8 : iNCoils ), *pucRegBuffer++ );
|
||||||
|
iNCoils -= 8;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOREG;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读开关寄存器 0x02
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
int16_t iNDiscrete = ( int16_t )usNDiscrete;
|
||||||
|
uint16_t usBitOffset;
|
||||||
|
|
||||||
|
delay_us( 50u );
|
||||||
|
/* Check if we have registers mapped at this block. */
|
||||||
|
if( ( usAddress >= REG_DISC_START ) &&
|
||||||
|
( usAddress + usNDiscrete <= REG_DISC_START + REG_DISC_SIZE ) )
|
||||||
|
{
|
||||||
|
usBitOffset = ( uint16_t )( usAddress - REG_DISC_START );
|
||||||
|
while( iNDiscrete > 0 )
|
||||||
|
{
|
||||||
|
*pucRegBuffer++ = xMBUtilGetBits( ucRegDiscBuf, usBitOffset, (uint8_t)( iNDiscrete > 8 ? 8 : iNDiscrete ) );
|
||||||
|
iNDiscrete -= 8;
|
||||||
|
usBitOffset += 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOREG;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
osThreadId_t MBRunTaskHandle;
|
||||||
|
const osThreadAttr_t MBRunTask_attributes = {
|
||||||
|
.name = "MBRunTask",
|
||||||
|
.stack_size = 128 * 10,
|
||||||
|
.priority = (osPriority_t) osPriorityHigh,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 初始化MODBUS
|
||||||
|
void MODBUS_Init( uint8_t MBAddress )
|
||||||
|
{
|
||||||
|
// osThreadDef( ModbusPoll, osPriorityHigh, 1u, 0u );
|
||||||
|
// osThreadCreate( osThread( ModbusPoll ), NULL );
|
||||||
|
MBRunTaskHandle = osThreadNew( ModbusPoll, NULL, &MBRunTask_attributes);
|
||||||
|
|
||||||
|
eMBInit( MB_RTU, MBAddress, 1, 115200u, MB_PAR_NONE ); // 初始化 FreeModbus 为RTU模式 从机地址为1 Uart1 9600 无校验
|
||||||
|
eMBEnable(); // 启动 FreeModbus
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/******** (C) COPYRIGHT 2020 青岛顺昕电子科技有限公司 **** End Of File ********/
|
||||||
486
Core/Src/Modbus/ascii/mbascii.c
Normal file
486
Core/Src/Modbus/ascii/mbascii.c
Normal file
@ -0,0 +1,486 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbascii.c,v 1.17 2010/06/06 13:47:07 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
#include "mbascii.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
|
||||||
|
#include "mbcrc.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
|
||||||
|
#if MB_ASCII_ENABLED > 0
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_ASCII_DEFAULT_CR '\r' /*!< Default CR character for Modbus ASCII. */
|
||||||
|
#define MB_ASCII_DEFAULT_LF '\n' /*!< Default LF character for Modbus ASCII. */
|
||||||
|
#define MB_SER_PDU_SIZE_MIN 3 /*!< Minimum size of a Modbus ASCII frame. */
|
||||||
|
#define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus ASCII frame. */
|
||||||
|
#define MB_SER_PDU_SIZE_LRC 1 /*!< Size of LRC field in PDU. */
|
||||||
|
#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
|
||||||
|
#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
|
||||||
|
|
||||||
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STATE_RX_IDLE, /*!< Receiver is in idle state. */
|
||||||
|
STATE_RX_RCV, /*!< Frame is beeing received. */
|
||||||
|
STATE_RX_WAIT_EOF /*!< Wait for End of Frame. */
|
||||||
|
} eMBRcvState;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STATE_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||||
|
STATE_TX_START, /*!< Starting transmission (':' sent). */
|
||||||
|
STATE_TX_DATA, /*!< Sending of data (Address, Data, LRC). */
|
||||||
|
STATE_TX_END, /*!< End of transmission. */
|
||||||
|
STATE_TX_NOTIFY /*!< Notify sender that the frame has been sent. */
|
||||||
|
} eMBSndState;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
BYTE_HIGH_NIBBLE, /*!< Character for high nibble of byte. */
|
||||||
|
BYTE_LOW_NIBBLE /*!< Character for low nibble of byte. */
|
||||||
|
} eMBBytePos;
|
||||||
|
|
||||||
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
static UCHAR prvucMBCHAR2BIN( UCHAR ucCharacter );
|
||||||
|
|
||||||
|
static UCHAR prvucMBBIN2CHAR( UCHAR ucByte );
|
||||||
|
|
||||||
|
static UCHAR prvucMBLRC( UCHAR * pucFrame, USHORT usLen );
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
static volatile eMBSndState eSndState;
|
||||||
|
static volatile eMBRcvState eRcvState;
|
||||||
|
|
||||||
|
/* We reuse the Modbus RTU buffer because only one buffer is needed and the
|
||||||
|
* RTU buffer is bigger. */
|
||||||
|
extern volatile UCHAR ucRTUBuf[];
|
||||||
|
static volatile UCHAR *ucASCIIBuf = ucRTUBuf;
|
||||||
|
|
||||||
|
static volatile USHORT usRcvBufferPos;
|
||||||
|
static volatile eMBBytePos eBytePos;
|
||||||
|
|
||||||
|
static volatile UCHAR *pucSndBufferCur;
|
||||||
|
static volatile USHORT usSndBufferCount;
|
||||||
|
|
||||||
|
static volatile UCHAR ucLRC;
|
||||||
|
static volatile UCHAR ucMBLFCharacter;
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBErrorCode
|
||||||
|
eMBASCIIInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
( void )ucSlaveAddress;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
ucMBLFCharacter = MB_ASCII_DEFAULT_LF;
|
||||||
|
|
||||||
|
if( xMBPortSerialInit( ucPort, ulBaudRate, 7, eParity ) != TRUE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
else if( xMBPortTimersInit( MB_ASCII_TIMEOUT_SEC * 20000UL ) != TRUE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBASCIIStart( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
/* No special startup required for ASCII. */
|
||||||
|
( void )xMBPortEventPost( EV_READY );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBASCIIStop( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
vMBPortSerialEnable( FALSE, FALSE );
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
|
||||||
|
|
||||||
|
/* Length and CRC check */
|
||||||
|
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
||||||
|
&& ( prvucMBLRC( ( UCHAR * ) ucASCIIBuf, usRcvBufferPos ) == 0 ) )
|
||||||
|
{
|
||||||
|
/* Save the address field. All frames are passed to the upper layed
|
||||||
|
* and the decision if a frame is used is done there.
|
||||||
|
*/
|
||||||
|
*pucRcvAddress = ucASCIIBuf[MB_SER_PDU_ADDR_OFF];
|
||||||
|
|
||||||
|
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||||
|
* size of address field and CRC checksum.
|
||||||
|
*/
|
||||||
|
*pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_LRC );
|
||||||
|
|
||||||
|
/* Return the start of the Modbus PDU to the caller. */
|
||||||
|
*pucFrame = ( UCHAR * ) & ucASCIIBuf[MB_SER_PDU_PDU_OFF];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBASCIISend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
UCHAR usLRC;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
/* Check if the receiver is still in idle state. If not we where too
|
||||||
|
* slow with processing the received frame and the master sent another
|
||||||
|
* frame on the network. We have to abort sending the frame.
|
||||||
|
*/
|
||||||
|
if( eRcvState == STATE_RX_IDLE )
|
||||||
|
{
|
||||||
|
/* First byte before the Modbus-PDU is the slave address. */
|
||||||
|
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||||
|
usSndBufferCount = 1;
|
||||||
|
|
||||||
|
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||||
|
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||||
|
usSndBufferCount += usLength;
|
||||||
|
|
||||||
|
/* Calculate LRC checksum for Modbus-Serial-Line-PDU. */
|
||||||
|
usLRC = prvucMBLRC( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
||||||
|
ucASCIIBuf[usSndBufferCount++] = usLRC;
|
||||||
|
|
||||||
|
/* Activate the transmitter. */
|
||||||
|
eSndState = STATE_TX_START;
|
||||||
|
vMBPortSerialEnable( FALSE, TRUE );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL
|
||||||
|
xMBASCIIReceiveFSM( void )
|
||||||
|
{
|
||||||
|
BOOL xNeedPoll = FALSE;
|
||||||
|
UCHAR ucByte;
|
||||||
|
UCHAR ucResult;
|
||||||
|
|
||||||
|
assert( eSndState == STATE_TX_IDLE );
|
||||||
|
|
||||||
|
( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||||
|
switch ( eRcvState )
|
||||||
|
{
|
||||||
|
/* A new character is received. If the character is a ':' the input
|
||||||
|
* buffer is cleared. A CR-character signals the end of the data
|
||||||
|
* block. Other characters are part of the data block and their
|
||||||
|
* ASCII value is converted back to a binary representation.
|
||||||
|
*/
|
||||||
|
case STATE_RX_RCV:
|
||||||
|
/* Enable timer for character timeout. */
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
if( ucByte == ':' )
|
||||||
|
{
|
||||||
|
/* Empty receive buffer. */
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
usRcvBufferPos = 0;
|
||||||
|
}
|
||||||
|
else if( ucByte == MB_ASCII_DEFAULT_CR )
|
||||||
|
{
|
||||||
|
eRcvState = STATE_RX_WAIT_EOF;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucResult = prvucMBCHAR2BIN( ucByte );
|
||||||
|
switch ( eBytePos )
|
||||||
|
{
|
||||||
|
/* High nibble of the byte comes first. We check for
|
||||||
|
* a buffer overflow here. */
|
||||||
|
case BYTE_HIGH_NIBBLE:
|
||||||
|
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||||
|
{
|
||||||
|
ucASCIIBuf[usRcvBufferPos] = ( UCHAR )( ucResult << 4 );
|
||||||
|
eBytePos = BYTE_LOW_NIBBLE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* not handled in Modbus specification but seems
|
||||||
|
* a resonable implementation. */
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
/* Disable previously activated timer because of error state. */
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BYTE_LOW_NIBBLE:
|
||||||
|
ucASCIIBuf[usRcvBufferPos] |= ucResult;
|
||||||
|
usRcvBufferPos++;
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_RX_WAIT_EOF:
|
||||||
|
if( ucByte == ucMBLFCharacter )
|
||||||
|
{
|
||||||
|
/* Disable character timeout timer because all characters are
|
||||||
|
* received. */
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
/* Receiver is again in idle state. */
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
|
||||||
|
/* Notify the caller of eMBASCIIReceive that a new frame
|
||||||
|
* was received. */
|
||||||
|
xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
|
||||||
|
}
|
||||||
|
else if( ucByte == ':' )
|
||||||
|
{
|
||||||
|
/* Empty receive buffer and back to receive state. */
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
usRcvBufferPos = 0;
|
||||||
|
eRcvState = STATE_RX_RCV;
|
||||||
|
|
||||||
|
/* Enable timer for character timeout. */
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Frame is not okay. Delete entire frame. */
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_RX_IDLE:
|
||||||
|
if( ucByte == ':' )
|
||||||
|
{
|
||||||
|
/* Enable timer for character timeout. */
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
/* Reset the input buffers to store the frame. */
|
||||||
|
usRcvBufferPos = 0;;
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
eRcvState = STATE_RX_RCV;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return xNeedPoll;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL
|
||||||
|
xMBASCIITransmitFSM( void )
|
||||||
|
{
|
||||||
|
BOOL xNeedPoll = FALSE;
|
||||||
|
UCHAR ucByte;
|
||||||
|
|
||||||
|
assert( eRcvState == STATE_RX_IDLE );
|
||||||
|
switch ( eSndState )
|
||||||
|
{
|
||||||
|
/* Start of transmission. The start of a frame is defined by sending
|
||||||
|
* the character ':'. */
|
||||||
|
case STATE_TX_START:
|
||||||
|
ucByte = ':';
|
||||||
|
xMBPortSerialPutByte( ( CHAR )ucByte );
|
||||||
|
eSndState = STATE_TX_DATA;
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Send the data block. Each data byte is encoded as a character hex
|
||||||
|
* stream with the high nibble sent first and the low nibble sent
|
||||||
|
* last. If all data bytes are exhausted we send a '\r' character
|
||||||
|
* to end the transmission. */
|
||||||
|
case STATE_TX_DATA:
|
||||||
|
if( usSndBufferCount > 0 )
|
||||||
|
{
|
||||||
|
switch ( eBytePos )
|
||||||
|
{
|
||||||
|
case BYTE_HIGH_NIBBLE:
|
||||||
|
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur >> 4 ) );
|
||||||
|
xMBPortSerialPutByte( ( CHAR ) ucByte );
|
||||||
|
eBytePos = BYTE_LOW_NIBBLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case BYTE_LOW_NIBBLE:
|
||||||
|
ucByte = prvucMBBIN2CHAR( ( UCHAR )( *pucSndBufferCur & 0x0F ) );
|
||||||
|
xMBPortSerialPutByte( ( CHAR )ucByte );
|
||||||
|
pucSndBufferCur++;
|
||||||
|
eBytePos = BYTE_HIGH_NIBBLE;
|
||||||
|
usSndBufferCount--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xMBPortSerialPutByte( MB_ASCII_DEFAULT_CR );
|
||||||
|
eSndState = STATE_TX_END;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Finish the frame by sending a LF character. */
|
||||||
|
case STATE_TX_END:
|
||||||
|
xMBPortSerialPutByte( ( CHAR )ucMBLFCharacter );
|
||||||
|
/* We need another state to make sure that the CR character has
|
||||||
|
* been sent. */
|
||||||
|
eSndState = STATE_TX_NOTIFY;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Notify the task which called eMBASCIISend that the frame has
|
||||||
|
* been sent. */
|
||||||
|
case STATE_TX_NOTIFY:
|
||||||
|
eSndState = STATE_TX_IDLE;
|
||||||
|
xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
|
||||||
|
|
||||||
|
/* Disable transmitter. This prevents another transmit buffer
|
||||||
|
* empty interrupt. */
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
eSndState = STATE_TX_IDLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* We should not get a transmitter event if the transmitter is in
|
||||||
|
* idle state. */
|
||||||
|
case STATE_TX_IDLE:
|
||||||
|
/* enable receiver/disable transmitter. */
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return xNeedPoll;
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOL
|
||||||
|
xMBASCIITimerT1SExpired( void )
|
||||||
|
{
|
||||||
|
switch ( eRcvState )
|
||||||
|
{
|
||||||
|
/* If we have a timeout we go back to the idle state and wait for
|
||||||
|
* the next frame.
|
||||||
|
*/
|
||||||
|
case STATE_RX_RCV:
|
||||||
|
case STATE_RX_WAIT_EOF:
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
assert( ( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_WAIT_EOF ) );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
|
||||||
|
/* no context switch required. */
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static UCHAR
|
||||||
|
prvucMBCHAR2BIN( UCHAR ucCharacter )
|
||||||
|
{
|
||||||
|
if( ( ucCharacter >= '0' ) && ( ucCharacter <= '9' ) )
|
||||||
|
{
|
||||||
|
return ( UCHAR )( ucCharacter - '0' );
|
||||||
|
}
|
||||||
|
else if( ( ucCharacter >= 'A' ) && ( ucCharacter <= 'F' ) )
|
||||||
|
{
|
||||||
|
return ( UCHAR )( ucCharacter - 'A' + 0x0A );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return 0xFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static UCHAR
|
||||||
|
prvucMBBIN2CHAR( UCHAR ucByte )
|
||||||
|
{
|
||||||
|
if( ucByte <= 0x09 )
|
||||||
|
{
|
||||||
|
return ( UCHAR )( '0' + ucByte );
|
||||||
|
}
|
||||||
|
else if( ( ucByte >= 0x0A ) && ( ucByte <= 0x0F ) )
|
||||||
|
{
|
||||||
|
return ( UCHAR )( ucByte - 0x0A + 'A' );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Programming error. */
|
||||||
|
assert( 0 );
|
||||||
|
}
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static UCHAR
|
||||||
|
prvucMBLRC( UCHAR * pucFrame, USHORT usLen )
|
||||||
|
{
|
||||||
|
UCHAR ucLRC = 0; /* LRC char initialized */
|
||||||
|
|
||||||
|
while( usLen-- )
|
||||||
|
{
|
||||||
|
ucLRC += *pucFrame++; /* Add buffer byte without carry */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return twos complement */
|
||||||
|
ucLRC = ( UCHAR ) ( -( ( CHAR ) ucLRC ) );
|
||||||
|
return ucLRC;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
56
Core/Src/Modbus/ascii/mbascii.h
Normal file
56
Core/Src/Modbus/ascii/mbascii.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbascii.h,v 1.8 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_ASCII_H
|
||||||
|
#define _MB_ASCII_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_ASCII_ENABLED > 0
|
||||||
|
eMBErrorCode eMBASCIIInit( UCHAR slaveAddress, UCHAR ucPort,
|
||||||
|
ULONG ulBaudRate, eMBParity eParity );
|
||||||
|
void eMBASCIIStart( void );
|
||||||
|
void eMBASCIIStop( void );
|
||||||
|
|
||||||
|
eMBErrorCode eMBASCIIReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||||
|
USHORT * pusLength );
|
||||||
|
eMBErrorCode eMBASCIISend( UCHAR slaveAddress, const UCHAR * pucFrame,
|
||||||
|
USHORT usLength );
|
||||||
|
BOOL xMBASCIIReceiveFSM( void );
|
||||||
|
BOOL xMBASCIITransmitFSM( void );
|
||||||
|
BOOL xMBASCIITimerT1SExpired( void );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
270
Core/Src/Modbus/functions/mbfunccoils.c
Normal file
270
Core/Src/Modbus/functions/mbfunccoils.c
Normal file
@ -0,0 +1,270 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
#define MB_PDU_FUNC_READ_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||||
|
#define MB_PDU_FUNC_READ_COILCNT_MAX ( 0x07D0 )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ( 0x07B0 )
|
||||||
|
|
||||||
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usCoilCount;
|
||||||
|
UCHAR ucNBytes;
|
||||||
|
UCHAR *pucFrameCur;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
|
||||||
|
usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
|
||||||
|
|
||||||
|
/* Check if the number of registers to read is valid. If not
|
||||||
|
* return Modbus illegal data value exception.
|
||||||
|
*/
|
||||||
|
if( ( usCoilCount >= 1 ) &&
|
||||||
|
( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
|
||||||
|
{
|
||||||
|
/* Set the current PDU data pointer to the beginning. */
|
||||||
|
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||||
|
*usLen = MB_PDU_FUNC_OFF;
|
||||||
|
|
||||||
|
/* First byte contains the function code. */
|
||||||
|
*pucFrameCur++ = MB_FUNC_READ_COILS;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||||
|
* byte is only partially field with unused coils set to zero. */
|
||||||
|
if( ( usCoilCount & 0x0007 ) != 0 )
|
||||||
|
{
|
||||||
|
ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucNBytes = ( UCHAR )( usCoilCount / 8 );
|
||||||
|
}
|
||||||
|
*pucFrameCur++ = ucNBytes;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
|
||||||
|
MB_REG_READ );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The response contains the function code, the starting address
|
||||||
|
* and the quantity of registers. We reuse the old values in the
|
||||||
|
* buffer because they are still valid. */
|
||||||
|
*usLen += ucNBytes;;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid read coil register request because the length
|
||||||
|
* is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||||
|
eMBException
|
||||||
|
eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
UCHAR ucBuf[2];
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
|
||||||
|
( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
|
||||||
|
( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
|
||||||
|
{
|
||||||
|
ucBuf[1] = 0;
|
||||||
|
if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
|
||||||
|
{
|
||||||
|
ucBuf[0] = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucBuf[0] = 0;
|
||||||
|
}
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid write coil register request because the length
|
||||||
|
* is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||||
|
eMBException
|
||||||
|
eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usCoilCnt;
|
||||||
|
UCHAR ucByteCount;
|
||||||
|
UCHAR ucByteCountVerify;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
|
||||||
|
usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
|
||||||
|
|
||||||
|
ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||||
|
|
||||||
|
/* Compute the number of expected bytes in the request. */
|
||||||
|
if( ( usCoilCnt & 0x0007 ) != 0 )
|
||||||
|
{
|
||||||
|
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( ( usCoilCnt >= 1 ) &&
|
||||||
|
( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
|
||||||
|
( ucByteCountVerify == ucByteCount ) )
|
||||||
|
{
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||||
|
usRegAddress, usCoilCnt, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The response contains the function code, the starting address
|
||||||
|
* and the quantity of registers. We reuse the old values in the
|
||||||
|
* buffer because they are still valid. */
|
||||||
|
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid write coil register request because the length
|
||||||
|
* is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
29
Core/Src/Modbus/functions/mbfuncdiag.c
Normal file
29
Core/Src/Modbus/functions/mbfuncdiag.c
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfuncdiag.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
125
Core/Src/Modbus/functions/mbfuncdisc.c
Normal file
125
Core/Src/Modbus/functions/mbfuncdisc.c
Normal file
@ -0,0 +1,125 @@
|
|||||||
|
/*
|
||||||
|
* FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
#define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||||
|
#define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
|
||||||
|
|
||||||
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usDiscreteCnt;
|
||||||
|
UCHAR ucNBytes;
|
||||||
|
UCHAR *pucFrameCur;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
|
||||||
|
usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
|
||||||
|
|
||||||
|
/* Check if the number of registers to read is valid. If not
|
||||||
|
* return Modbus illegal data value exception.
|
||||||
|
*/
|
||||||
|
if( ( usDiscreteCnt >= 1 ) &&
|
||||||
|
( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
|
||||||
|
{
|
||||||
|
/* Set the current PDU data pointer to the beginning. */
|
||||||
|
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||||
|
*usLen = MB_PDU_FUNC_OFF;
|
||||||
|
|
||||||
|
/* First byte contains the function code. */
|
||||||
|
*pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Test if the quantity of coils is a multiple of 8. If not last
|
||||||
|
* byte is only partially field with unused coils set to zero. */
|
||||||
|
if( ( usDiscreteCnt & 0x0007 ) != 0 )
|
||||||
|
{
|
||||||
|
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
|
||||||
|
}
|
||||||
|
*pucFrameCur++ = ucNBytes;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The response contains the function code, the starting address
|
||||||
|
* and the quantity of registers. We reuse the old values in the
|
||||||
|
* buffer because they are still valid. */
|
||||||
|
*usLen += ucNBytes;;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid read coil register request because the length
|
||||||
|
* is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
308
Core/Src/Modbus/functions/mbfuncholding.c
Normal file
308
Core/Src/Modbus/functions/mbfuncholding.c
Normal file
@ -0,0 +1,308 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfuncholding.c,v 1.12 2007/02/18 23:48:22 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||||
|
#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||||
|
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 0)
|
||||||
|
#define MB_PDU_FUNC_WRITE_VALUE_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_SIZE ( 4 )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF ( MB_PDU_DATA_OFF + 5 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN ( 5 )
|
||||||
|
#define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ( 0x0078 )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF ( MB_PDU_DATA_OFF + 0 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF ( MB_PDU_DATA_OFF + 4 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF ( MB_PDU_DATA_OFF + 6 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_BYTECNT_OFF ( MB_PDU_DATA_OFF + 8 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF ( MB_PDU_DATA_OFF + 9 )
|
||||||
|
#define MB_PDU_FUNC_READWRITE_SIZE_MIN ( 9 )
|
||||||
|
|
||||||
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
/* Make callback to update the value. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
|
||||||
|
usRegAddress, 1, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||||
|
eMBException
|
||||||
|
eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usRegCount;
|
||||||
|
UCHAR ucRegByteCount;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
|
||||||
|
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
|
||||||
|
|
||||||
|
if( ( usRegCount >= 1 ) &&
|
||||||
|
( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
|
||||||
|
( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
|
||||||
|
{
|
||||||
|
/* Make callback to update the register values. */
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
|
||||||
|
usRegAddress, usRegCount, MB_REG_WRITE );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The response contains the function code, the starting
|
||||||
|
* address and the quantity of registers. We reuse the
|
||||||
|
* old values in the buffer because they are still valid.
|
||||||
|
*/
|
||||||
|
*usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usRegCount;
|
||||||
|
UCHAR *pucFrameCur;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||||
|
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
/* Check if the number of registers to read is valid. If not
|
||||||
|
* return Modbus illegal data value exception.
|
||||||
|
*/
|
||||||
|
if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
|
||||||
|
{
|
||||||
|
/* Set the current PDU data pointer to the beginning. */
|
||||||
|
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||||
|
*usLen = MB_PDU_FUNC_OFF;
|
||||||
|
|
||||||
|
/* First byte contains the function code. */
|
||||||
|
*pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Second byte in the response contain the number of bytes. */
|
||||||
|
*pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Make callback to fill the buffer. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*usLen += usRegCount * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid request because the length is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegReadAddress;
|
||||||
|
USHORT usRegReadCount;
|
||||||
|
USHORT usRegWriteAddress;
|
||||||
|
USHORT usRegWriteCount;
|
||||||
|
UCHAR ucRegWriteByteCount;
|
||||||
|
UCHAR *pucFrameCur;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
|
||||||
|
usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
|
||||||
|
usRegReadAddress++;
|
||||||
|
|
||||||
|
usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
|
||||||
|
usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
|
||||||
|
usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
|
||||||
|
usRegWriteAddress++;
|
||||||
|
|
||||||
|
usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
|
||||||
|
usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
|
||||||
|
|
||||||
|
if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
|
||||||
|
( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
|
||||||
|
( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
|
||||||
|
{
|
||||||
|
/* Make callback to update the register values. */
|
||||||
|
eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
|
||||||
|
usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
|
||||||
|
|
||||||
|
if( eRegStatus == MB_ENOERR )
|
||||||
|
{
|
||||||
|
/* Set the current PDU data pointer to the beginning. */
|
||||||
|
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||||
|
*usLen = MB_PDU_FUNC_OFF;
|
||||||
|
|
||||||
|
/* First byte contains the function code. */
|
||||||
|
*pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Second byte in the response contain the number of bytes. */
|
||||||
|
*pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Make the read callback. */
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
|
||||||
|
if( eRegStatus == MB_ENOERR )
|
||||||
|
{
|
||||||
|
*usLen += 2 * usRegReadCount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
122
Core/Src/Modbus/functions/mbfuncinput.c
Normal file
122
Core/Src/Modbus/functions/mbfuncinput.c
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfuncinput.c,v 1.10 2007/09/12 10:15:56 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
#define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
|
||||||
|
#define MB_PDU_FUNC_READ_SIZE ( 4 )
|
||||||
|
#define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
|
||||||
|
|
||||||
|
#define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
|
||||||
|
|
||||||
|
/* ----------------------- Static functions ---------------------------------*/
|
||||||
|
eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
USHORT usRegAddress;
|
||||||
|
USHORT usRegCount;
|
||||||
|
UCHAR *pucFrameCur;
|
||||||
|
|
||||||
|
eMBException eStatus = MB_EX_NONE;
|
||||||
|
eMBErrorCode eRegStatus;
|
||||||
|
|
||||||
|
if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
|
||||||
|
{
|
||||||
|
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
|
||||||
|
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
|
||||||
|
usRegAddress++;
|
||||||
|
|
||||||
|
usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
|
||||||
|
usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
|
||||||
|
|
||||||
|
/* Check if the number of registers to read is valid. If not
|
||||||
|
* return Modbus illegal data value exception.
|
||||||
|
*/
|
||||||
|
if( ( usRegCount >= 1 )
|
||||||
|
&& ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) )
|
||||||
|
{
|
||||||
|
/* Set the current PDU data pointer to the beginning. */
|
||||||
|
pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
|
||||||
|
*usLen = MB_PDU_FUNC_OFF;
|
||||||
|
|
||||||
|
/* First byte contains the function code. */
|
||||||
|
*pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
/* Second byte in the response contain the number of bytes. */
|
||||||
|
*pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
|
||||||
|
*usLen += 1;
|
||||||
|
|
||||||
|
eRegStatus =
|
||||||
|
eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );
|
||||||
|
|
||||||
|
/* If an error occured convert it into a Modbus exception. */
|
||||||
|
if( eRegStatus != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eStatus = prveMBError2Exception( eRegStatus );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
*usLen += usRegCount * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Can't be a valid read input register request because the length
|
||||||
|
* is incorrect. */
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_VALUE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
88
Core/Src/Modbus/functions/mbfuncother.c
Normal file
88
Core/Src/Modbus/functions/mbfuncother.c
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfuncother.c,v 1.8 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
|
||||||
|
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
static UCHAR ucMBSlaveID[MB_FUNC_OTHER_REP_SLAVEID_BUF];
|
||||||
|
static USHORT usMBSlaveIDLen;
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBSetSlaveID( UCHAR ucSlaveID, _Bool xIsRunning,
|
||||||
|
UCHAR const *pucAdditional, USHORT usAdditionalLen )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
/* the first byte and second byte in the buffer is reserved for
|
||||||
|
* the parameter ucSlaveID and the running flag. The rest of
|
||||||
|
* the buffer is available for additional data. */
|
||||||
|
if( usAdditionalLen + 2 < MB_FUNC_OTHER_REP_SLAVEID_BUF )
|
||||||
|
{
|
||||||
|
usMBSlaveIDLen = 0;
|
||||||
|
ucMBSlaveID[usMBSlaveIDLen++] = ucSlaveID;
|
||||||
|
ucMBSlaveID[usMBSlaveIDLen++] = ( UCHAR )( xIsRunning ? 0xFF : 0x00 );
|
||||||
|
if( usAdditionalLen > 0 )
|
||||||
|
{
|
||||||
|
memcpy( &ucMBSlaveID[usMBSlaveIDLen], pucAdditional,
|
||||||
|
( size_t )usAdditionalLen );
|
||||||
|
usMBSlaveIDLen += usAdditionalLen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_ENORES;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen )
|
||||||
|
{
|
||||||
|
memcpy( &pucFrame[MB_PDU_DATA_OFF], &ucMBSlaveID[0], ( size_t )usMBSlaveIDLen );
|
||||||
|
*usLen = ( USHORT )( MB_PDU_DATA_OFF + usMBSlaveIDLen );
|
||||||
|
return MB_EX_NONE;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
141
Core/Src/Modbus/functions/mbutils.c
Normal file
141
Core/Src/Modbus/functions/mbutils.c
Normal file
@ -0,0 +1,141 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbutils.c,v 1.6 2007/02/18 23:49:07 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define BITS_UCHAR 8U
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
void
|
||||||
|
xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,
|
||||||
|
UCHAR ucValue )
|
||||||
|
{
|
||||||
|
USHORT usWordBuf;
|
||||||
|
USHORT usMask;
|
||||||
|
USHORT usByteOffset;
|
||||||
|
USHORT usNPreBits;
|
||||||
|
USHORT usValue = ucValue;
|
||||||
|
|
||||||
|
assert( ucNBits <= 8 );
|
||||||
|
assert( ( size_t )BITS_UCHAR == sizeof( UCHAR ) * 8 );
|
||||||
|
|
||||||
|
/* Calculate byte offset for first byte containing the bit values starting
|
||||||
|
* at usBitOffset. */
|
||||||
|
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
||||||
|
|
||||||
|
/* How many bits precede our bits to set. */
|
||||||
|
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
||||||
|
|
||||||
|
/* Move bit field into position over bits to set */
|
||||||
|
usValue <<= usNPreBits;
|
||||||
|
|
||||||
|
/* Prepare a mask for setting the new bits. */
|
||||||
|
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
||||||
|
usMask <<= usBitOffset - usByteOffset * BITS_UCHAR;
|
||||||
|
|
||||||
|
/* copy bits into temporary storage. */
|
||||||
|
usWordBuf = ucByteBuf[usByteOffset];
|
||||||
|
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||||
|
|
||||||
|
/* Zero out bit field bits and then or value bits into them. */
|
||||||
|
usWordBuf = ( USHORT )( ( usWordBuf & ( ~usMask ) ) | usValue );
|
||||||
|
|
||||||
|
/* move bits back into storage */
|
||||||
|
ucByteBuf[usByteOffset] = ( UCHAR )( usWordBuf & 0xFF );
|
||||||
|
ucByteBuf[usByteOffset + 1] = ( UCHAR )( usWordBuf >> BITS_UCHAR );
|
||||||
|
}
|
||||||
|
|
||||||
|
UCHAR
|
||||||
|
xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits )
|
||||||
|
{
|
||||||
|
USHORT usWordBuf;
|
||||||
|
USHORT usMask;
|
||||||
|
USHORT usByteOffset;
|
||||||
|
USHORT usNPreBits;
|
||||||
|
|
||||||
|
/* Calculate byte offset for first byte containing the bit values starting
|
||||||
|
* at usBitOffset. */
|
||||||
|
usByteOffset = ( USHORT )( ( usBitOffset ) / BITS_UCHAR );
|
||||||
|
|
||||||
|
/* How many bits precede our bits to set. */
|
||||||
|
usNPreBits = ( USHORT )( usBitOffset - usByteOffset * BITS_UCHAR );
|
||||||
|
|
||||||
|
/* Prepare a mask for setting the new bits. */
|
||||||
|
usMask = ( USHORT )( ( 1 << ( USHORT ) ucNBits ) - 1 );
|
||||||
|
|
||||||
|
/* copy bits into temporary storage. */
|
||||||
|
usWordBuf = ucByteBuf[usByteOffset];
|
||||||
|
usWordBuf |= ucByteBuf[usByteOffset + 1] << BITS_UCHAR;
|
||||||
|
|
||||||
|
/* throw away unneeded bits. */
|
||||||
|
usWordBuf >>= usNPreBits;
|
||||||
|
|
||||||
|
/* mask away bits above the requested bitfield. */
|
||||||
|
usWordBuf &= usMask;
|
||||||
|
|
||||||
|
return ( UCHAR ) usWordBuf;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBException
|
||||||
|
prveMBError2Exception( eMBErrorCode eErrorCode )
|
||||||
|
{
|
||||||
|
eMBException eStatus;
|
||||||
|
|
||||||
|
switch ( eErrorCode )
|
||||||
|
{
|
||||||
|
case MB_ENOERR:
|
||||||
|
eStatus = MB_EX_NONE;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MB_ENOREG:
|
||||||
|
eStatus = MB_EX_ILLEGAL_DATA_ADDRESS;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MB_ETIMEDOUT:
|
||||||
|
eStatus = MB_EX_SLAVE_BUSY;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
eStatus = MB_EX_SLAVE_DEVICE_FAILURE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
417
Core/Src/Modbus/include/mb.h
Normal file
417
Core/Src/Modbus/include/mb.h
Normal file
@ -0,0 +1,417 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mb.h,v 1.17 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_H
|
||||||
|
#define _MB_H
|
||||||
|
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "mbport.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
|
||||||
|
/*! \defgroup modbus Modbus
|
||||||
|
* \code #include "mb.h" \endcode
|
||||||
|
*
|
||||||
|
* This module defines the interface for the application. It contains
|
||||||
|
* the basic functions and types required to use the Modbus protocol stack.
|
||||||
|
* A typical application will want to call eMBInit() first. If the device
|
||||||
|
* is ready to answer network requests it must then call eMBEnable() to activate
|
||||||
|
* the protocol stack. In the main loop the function eMBPoll() must be called
|
||||||
|
* periodically. The time interval between pooling depends on the configured
|
||||||
|
* Modbus timeout. If an RTOS is available a separate task should be created
|
||||||
|
* and the task should always call the function eMBPoll().
|
||||||
|
*
|
||||||
|
* \code
|
||||||
|
* // Initialize protocol stack in RTU mode for a slave with address 10 = 0x0A
|
||||||
|
* eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
|
||||||
|
* // Enable the Modbus Protocol Stack.
|
||||||
|
* eMBEnable( );
|
||||||
|
* for( ;; )
|
||||||
|
* {
|
||||||
|
* // Call the main polling loop of the Modbus protocol stack.
|
||||||
|
* eMBPoll( );
|
||||||
|
* ...
|
||||||
|
* }
|
||||||
|
* \endcode
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Use the default Modbus TCP port (502)
|
||||||
|
*/
|
||||||
|
#define MB_TCP_PORT_USE_DEFAULT 0
|
||||||
|
|
||||||
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Modbus serial transmission modes (RTU/ASCII).
|
||||||
|
*
|
||||||
|
* Modbus serial supports two transmission modes. Either ASCII or RTU. RTU
|
||||||
|
* is faster but has more hardware requirements and requires a network with
|
||||||
|
* a low jitter. ASCII is slower and more reliable on slower links (E.g. modems)
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MB_RTU, /*!< RTU transmission mode. */
|
||||||
|
MB_ASCII, /*!< ASCII transmission mode. */
|
||||||
|
MB_TCP /*!< TCP mode. */
|
||||||
|
} eMBMode;
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief If register should be written or read.
|
||||||
|
*
|
||||||
|
* This value is passed to the callback functions which support either
|
||||||
|
* reading or writing register values. Writing means that the application
|
||||||
|
* registers should be updated and reading means that the modbus protocol
|
||||||
|
* stack needs to know the current register values.
|
||||||
|
*
|
||||||
|
* \see eMBRegHoldingCB( ), eMBRegCoilsCB( ), eMBRegDiscreteCB( ) and
|
||||||
|
* eMBRegInputCB( ).
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MB_REG_READ, /*!< Read register values and pass to protocol stack. */
|
||||||
|
MB_REG_WRITE /*!< Update register values. */
|
||||||
|
} eMBRegisterMode;
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Errorcodes used by all function in the protocol stack.
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MB_ENOERR, /*!< no error. */
|
||||||
|
MB_ENOREG, /*!< illegal register address. */
|
||||||
|
MB_EINVAL, /*!< illegal argument. */
|
||||||
|
MB_EPORTERR, /*!< porting layer error. */
|
||||||
|
MB_ENORES, /*!< insufficient resources. */
|
||||||
|
MB_EIO, /*!< I/O error. */
|
||||||
|
MB_EILLSTATE, /*!< protocol stack in illegal state. */
|
||||||
|
MB_ETIMEDOUT /*!< timeout error occurred. */
|
||||||
|
} eMBErrorCode;
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Initialize the Modbus protocol stack.
|
||||||
|
*
|
||||||
|
* This functions initializes the ASCII or RTU module and calls the
|
||||||
|
* init functions of the porting layer to prepare the hardware. Please
|
||||||
|
* note that the receiver is still disabled and no Modbus frames are
|
||||||
|
* processed until eMBEnable( ) has been called.
|
||||||
|
*
|
||||||
|
* \param eMode If ASCII or RTU mode should be used.
|
||||||
|
* \param ucSlaveAddress The slave address. Only frames sent to this
|
||||||
|
* address or to the broadcast address are processed.
|
||||||
|
* \param ucPort The port to use. E.g. 1 for COM1 on windows. This value
|
||||||
|
* is platform dependent and some ports simply choose to ignore it.
|
||||||
|
* \param ulBaudRate The baudrate. E.g. 19200. Supported baudrates depend
|
||||||
|
* on the porting layer.
|
||||||
|
* \param eParity Parity used for serial transmission.
|
||||||
|
*
|
||||||
|
* \return If no error occurs the function returns eMBErrorCode::MB_ENOERR.
|
||||||
|
* The protocol is then in the disabled state and ready for activation
|
||||||
|
* by calling eMBEnable( ). Otherwise one of the following error codes
|
||||||
|
* is returned:
|
||||||
|
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||||
|
* slave addresses are in the range 1 - 247.
|
||||||
|
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBInit( eMBMode eMode, UCHAR ucSlaveAddress,
|
||||||
|
UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Initialize the Modbus protocol stack for Modbus TCP.
|
||||||
|
*
|
||||||
|
* This function initializes the Modbus TCP Module. Please note that
|
||||||
|
* frame processing is still disabled until eMBEnable( ) is called.
|
||||||
|
*
|
||||||
|
* \param usTCPPort The TCP port to listen on.
|
||||||
|
* \return If the protocol stack has been initialized correctly the function
|
||||||
|
* returns eMBErrorCode::MB_ENOERR. Otherwise one of the following error
|
||||||
|
* codes is returned:
|
||||||
|
* - eMBErrorCode::MB_EINVAL If the slave address was not valid. Valid
|
||||||
|
* slave addresses are in the range 1 - 247.
|
||||||
|
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBTCPInit( USHORT usTCPPort );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Release resources used by the protocol stack.
|
||||||
|
*
|
||||||
|
* This function disables the Modbus protocol stack and release all
|
||||||
|
* hardware resources. It must only be called when the protocol stack
|
||||||
|
* is disabled.
|
||||||
|
*
|
||||||
|
* \note Note all ports implement this function. A port which wants to
|
||||||
|
* get an callback must define the macro MB_PORT_HAS_CLOSE to 1.
|
||||||
|
*
|
||||||
|
* \return If the resources where released it return eMBErrorCode::MB_ENOERR.
|
||||||
|
* If the protocol stack is not in the disabled state it returns
|
||||||
|
* eMBErrorCode::MB_EILLSTATE.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBClose( void );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Enable the Modbus protocol stack.
|
||||||
|
*
|
||||||
|
* This function enables processing of Modbus frames. Enabling the protocol
|
||||||
|
* stack is only possible if it is in the disabled state.
|
||||||
|
*
|
||||||
|
* \return If the protocol stack is now in the state enabled it returns
|
||||||
|
* eMBErrorCode::MB_ENOERR. If it was not in the disabled state it
|
||||||
|
* return eMBErrorCode::MB_EILLSTATE.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBEnable( void );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Disable the Modbus protocol stack.
|
||||||
|
*
|
||||||
|
* This function disables processing of Modbus frames.
|
||||||
|
*
|
||||||
|
* \return If the protocol stack has been disabled it returns
|
||||||
|
* eMBErrorCode::MB_ENOERR. If it was not in the enabled state it returns
|
||||||
|
* eMBErrorCode::MB_EILLSTATE.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBDisable( void );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief The main pooling loop of the Modbus protocol stack.
|
||||||
|
*
|
||||||
|
* This function must be called periodically. The timer interval required
|
||||||
|
* is given by the application dependent Modbus slave timeout. Internally the
|
||||||
|
* function calls xMBPortEventGet() and waits for an event from the receiver or
|
||||||
|
* transmitter state machines.
|
||||||
|
*
|
||||||
|
* \return If the protocol stack is not in the enabled state the function
|
||||||
|
* returns eMBErrorCode::MB_EILLSTATE. Otherwise it returns
|
||||||
|
* eMBErrorCode::MB_ENOERR.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBPoll( void );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Configure the slave id of the device.
|
||||||
|
*
|
||||||
|
* This function should be called when the Modbus function <em>Report Slave ID</em>
|
||||||
|
* is enabled ( By defining MB_FUNC_OTHER_REP_SLAVEID_ENABLED in mbconfig.h ).
|
||||||
|
*
|
||||||
|
* \param ucSlaveID Values is returned in the <em>Slave ID</em> byte of the
|
||||||
|
* <em>Report Slave ID</em> response.
|
||||||
|
* \param xIsRunning If TRUE the <em>Run Indicator Status</em> byte is set to 0xFF.
|
||||||
|
* otherwise the <em>Run Indicator Status</em> is 0x00.
|
||||||
|
* \param pucAdditional Values which should be returned in the <em>Additional</em>
|
||||||
|
* bytes of the <em> Report Slave ID</em> response.
|
||||||
|
* \param usAdditionalLen Length of the buffer <code>pucAdditonal</code>.
|
||||||
|
*
|
||||||
|
* \return If the static buffer defined by MB_FUNC_OTHER_REP_SLAVEID_BUF in
|
||||||
|
* mbconfig.h is to small it returns eMBErrorCode::MB_ENORES. Otherwise
|
||||||
|
* it returns eMBErrorCode::MB_ENOERR.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBSetSlaveID( UCHAR ucSlaveID, _Bool xIsRunning,
|
||||||
|
UCHAR const *pucAdditional,
|
||||||
|
USHORT usAdditionalLen );
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Registers a callback handler for a given function code.
|
||||||
|
*
|
||||||
|
* This function registers a new callback handler for a given function code.
|
||||||
|
* The callback handler supplied is responsible for interpreting the Modbus PDU and
|
||||||
|
* the creation of an appropriate response. In case of an error it should return
|
||||||
|
* one of the possible Modbus exceptions which results in a Modbus exception frame
|
||||||
|
* sent by the protocol stack.
|
||||||
|
*
|
||||||
|
* \param ucFunctionCode The Modbus function code for which this handler should
|
||||||
|
* be registers. Valid function codes are in the range 1 to 127.
|
||||||
|
* \param pxHandler The function handler which should be called in case
|
||||||
|
* such a frame is received. If \c NULL a previously registered function handler
|
||||||
|
* for this function code is removed.
|
||||||
|
*
|
||||||
|
* \return eMBErrorCode::MB_ENOERR if the handler has been installed. If no
|
||||||
|
* more resources are available it returns eMBErrorCode::MB_ENORES. In this
|
||||||
|
* case the values in mbconfig.h should be adjusted. If the argument was not
|
||||||
|
* valid it returns eMBErrorCode::MB_EINVAL.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBRegisterCB( UCHAR ucFunctionCode,
|
||||||
|
pxMBFunctionHandler pxHandler );
|
||||||
|
|
||||||
|
/* ----------------------- Callback -----------------------------------------*/
|
||||||
|
|
||||||
|
/*! \defgroup modbus_registers Modbus Registers
|
||||||
|
* \code #include "mb.h" \endcode
|
||||||
|
* The protocol stack does not internally allocate any memory for the
|
||||||
|
* registers. This makes the protocol stack very small and also usable on
|
||||||
|
* low end targets. In addition the values don't have to be in the memory
|
||||||
|
* and could for example be stored in a flash.<br>
|
||||||
|
* Whenever the protocol stack requires a value it calls one of the callback
|
||||||
|
* function with the register address and the number of registers to read
|
||||||
|
* as an argument. The application should then read the actual register values
|
||||||
|
* (for example the ADC voltage) and should store the result in the supplied
|
||||||
|
* buffer.<br>
|
||||||
|
* If the protocol stack wants to update a register value because a write
|
||||||
|
* register function was received a buffer with the new register values is
|
||||||
|
* passed to the callback function. The function should then use these values
|
||||||
|
* to update the application register values.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*! \ingroup modbus_registers
|
||||||
|
* \brief Callback function used if the value of a <em>Input Register</em>
|
||||||
|
* is required by the protocol stack. The starting register address is given
|
||||||
|
* by \c usAddress and the last register is given by <tt>usAddress +
|
||||||
|
* usNRegs - 1</tt>.
|
||||||
|
*
|
||||||
|
* \param pucRegBuffer A buffer where the callback function should write
|
||||||
|
* the current value of the modbus registers to.
|
||||||
|
* \param usAddress The starting address of the register. Input registers
|
||||||
|
* are in the range 1 - 65535.
|
||||||
|
* \param usNRegs Number of registers the callback function must supply.
|
||||||
|
*
|
||||||
|
* \return The function must return one of the following error codes:
|
||||||
|
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||||
|
* Modbus response is sent.
|
||||||
|
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||||
|
* for registers within this range. In this case a
|
||||||
|
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||||
|
* currently not available and the application dependent response
|
||||||
|
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||||
|
* exception is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||||
|
USHORT usNRegs );
|
||||||
|
|
||||||
|
/*! \ingroup modbus_registers
|
||||||
|
* \brief Callback function used if a <em>Holding Register</em> value is
|
||||||
|
* read or written by the protocol stack. The starting register address
|
||||||
|
* is given by \c usAddress and the last register is given by
|
||||||
|
* <tt>usAddress + usNRegs - 1</tt>.
|
||||||
|
*
|
||||||
|
* \param pucRegBuffer If the application registers values should be updated the
|
||||||
|
* buffer points to the new registers values. If the protocol stack needs
|
||||||
|
* to now the current values the callback function should write them into
|
||||||
|
* this buffer.
|
||||||
|
* \param usAddress The starting address of the register.
|
||||||
|
* \param usNRegs Number of registers to read or write.
|
||||||
|
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application register
|
||||||
|
* values should be updated from the values in the buffer. For example
|
||||||
|
* this would be the case when the Modbus master has issued an
|
||||||
|
* <b>WRITE SINGLE REGISTER</b> command.
|
||||||
|
* If the value eMBRegisterMode::MB_REG_READ the application should copy
|
||||||
|
* the current values into the buffer \c pucRegBuffer.
|
||||||
|
*
|
||||||
|
* \return The function must return one of the following error codes:
|
||||||
|
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||||
|
* Modbus response is sent.
|
||||||
|
* - eMBErrorCode::MB_ENOREG If the application can not supply values
|
||||||
|
* for registers within this range. In this case a
|
||||||
|
* <b>ILLEGAL DATA ADDRESS</b> exception frame is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||||
|
* currently not available and the application dependent response
|
||||||
|
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||||
|
* exception is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||||
|
USHORT usNRegs, eMBRegisterMode eMode );
|
||||||
|
|
||||||
|
/*! \ingroup modbus_registers
|
||||||
|
* \brief Callback function used if a <em>Coil Register</em> value is
|
||||||
|
* read or written by the protocol stack. If you are going to use
|
||||||
|
* this function you might use the functions xMBUtilSetBits( ) and
|
||||||
|
* xMBUtilGetBits( ) for working with bitfields.
|
||||||
|
*
|
||||||
|
* \param pucRegBuffer The bits are packed in bytes where the first coil
|
||||||
|
* starting at address \c usAddress is stored in the LSB of the
|
||||||
|
* first byte in the buffer <code>pucRegBuffer</code>.
|
||||||
|
* If the buffer should be written by the callback function unused
|
||||||
|
* coil values (I.e. if not a multiple of eight coils is used) should be set
|
||||||
|
* to zero.
|
||||||
|
* \param usAddress The first coil number.
|
||||||
|
* \param usNCoils Number of coil values requested.
|
||||||
|
* \param eMode If eMBRegisterMode::MB_REG_WRITE the application values should
|
||||||
|
* be updated from the values supplied in the buffer \c pucRegBuffer.
|
||||||
|
* If eMBRegisterMode::MB_REG_READ the application should store the current
|
||||||
|
* values in the buffer \c pucRegBuffer.
|
||||||
|
*
|
||||||
|
* \return The function must return one of the following error codes:
|
||||||
|
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||||
|
* Modbus response is sent.
|
||||||
|
* - eMBErrorCode::MB_ENOREG If the application does not map an coils
|
||||||
|
* within the requested address range. In this case a
|
||||||
|
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||||
|
* currently not available and the application dependent response
|
||||||
|
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||||
|
* exception is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||||
|
USHORT usNCoils, eMBRegisterMode eMode );
|
||||||
|
|
||||||
|
/*! \ingroup modbus_registers
|
||||||
|
* \brief Callback function used if a <em>Input Discrete Register</em> value is
|
||||||
|
* read by the protocol stack.
|
||||||
|
*
|
||||||
|
* If you are going to use his function you might use the functions
|
||||||
|
* xMBUtilSetBits( ) and xMBUtilGetBits( ) for working with bitfields.
|
||||||
|
*
|
||||||
|
* \param pucRegBuffer The buffer should be updated with the current
|
||||||
|
* coil values. The first discrete input starting at \c usAddress must be
|
||||||
|
* stored at the LSB of the first byte in the buffer. If the requested number
|
||||||
|
* is not a multiple of eight the remaining bits should be set to zero.
|
||||||
|
* \param usAddress The starting address of the first discrete input.
|
||||||
|
* \param usNDiscrete Number of discrete input values.
|
||||||
|
* \return The function must return one of the following error codes:
|
||||||
|
* - eMBErrorCode::MB_ENOERR If no error occurred. In this case a normal
|
||||||
|
* Modbus response is sent.
|
||||||
|
* - eMBErrorCode::MB_ENOREG If no such discrete inputs exists.
|
||||||
|
* In this case a <b>ILLEGAL DATA ADDRESS</b> exception frame is sent
|
||||||
|
* as a response.
|
||||||
|
* - eMBErrorCode::MB_ETIMEDOUT If the requested register block is
|
||||||
|
* currently not available and the application dependent response
|
||||||
|
* timeout would be violated. In this case a <b>SLAVE DEVICE BUSY</b>
|
||||||
|
* exception is sent as a response.
|
||||||
|
* - eMBErrorCode::MB_EIO If an unrecoverable error occurred. In this case
|
||||||
|
* a <b>SLAVE DEVICE FAILURE</b> exception is sent as a response.
|
||||||
|
*/
|
||||||
|
eMBErrorCode eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
|
||||||
|
USHORT usNDiscrete );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
132
Core/Src/Modbus/include/mbconfig.h
Normal file
132
Core/Src/Modbus/include/mbconfig.h
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbconfig.h,v 1.15 2010/06/06 13:54:40 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_CONFIG_H
|
||||||
|
#define _MB_CONFIG_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
/*! \defgroup modbus_cfg Modbus Configuration
|
||||||
|
*
|
||||||
|
* Most modules in the protocol stack are completly optional and can be
|
||||||
|
* excluded. This is specially important if target resources are very small
|
||||||
|
* and program memory space should be saved.<br>
|
||||||
|
*
|
||||||
|
* All of these settings are available in the file <code>mbconfig.h</code>
|
||||||
|
*/
|
||||||
|
/*! \addtogroup modbus_cfg
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
/*! \brief If Modbus ASCII support is enabled. */
|
||||||
|
#define MB_ASCII_ENABLED ( 0 )
|
||||||
|
|
||||||
|
/*! \brief If Modbus RTU support is enabled. */
|
||||||
|
#define MB_RTU_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If Modbus TCP support is enabled. */
|
||||||
|
#define MB_TCP_ENABLED ( 0 )
|
||||||
|
|
||||||
|
/*! \brief The character timeout value for Modbus ASCII.
|
||||||
|
*
|
||||||
|
* The character timeout value is not fixed for Modbus ASCII and is therefore
|
||||||
|
* a configuration option. It should be set to the maximum expected delay
|
||||||
|
* time of the network.
|
||||||
|
*/
|
||||||
|
#define MB_ASCII_TIMEOUT_SEC ( 1 )
|
||||||
|
|
||||||
|
/*! \brief Timeout to wait in ASCII prior to enabling transmitter.
|
||||||
|
*
|
||||||
|
* If defined the function calls vMBPortSerialDelay with the argument
|
||||||
|
* MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS to allow for a delay before
|
||||||
|
* the serial transmitter is enabled. This is required because some
|
||||||
|
* targets are so fast that there is no time between receiving and
|
||||||
|
* transmitting the frame. If the master is to slow with enabling its
|
||||||
|
* receiver then he will not receive the response correctly.
|
||||||
|
*/
|
||||||
|
#ifndef MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS
|
||||||
|
#define MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS ( 0 )
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*! \brief Maximum number of Modbus functions codes the protocol stack
|
||||||
|
* should support.
|
||||||
|
*
|
||||||
|
* The maximum number of supported Modbus functions must be greater than
|
||||||
|
* the sum of all enabled functions in this file and custom function
|
||||||
|
* handlers. If set to small adding more functions will fail.
|
||||||
|
*/
|
||||||
|
#define MB_FUNC_HANDLERS_MAX ( 16 )
|
||||||
|
|
||||||
|
/*! \brief Number of bytes which should be allocated for the <em>Report Slave ID
|
||||||
|
* </em>command.
|
||||||
|
*
|
||||||
|
* This number limits the maximum size of the additional segment in the
|
||||||
|
* report slave id function. See eMBSetSlaveID( ) for more information on
|
||||||
|
* how to set this value. It is only used if MB_FUNC_OTHER_REP_SLAVEID_ENABLED
|
||||||
|
* is set to <code>1</code>.
|
||||||
|
*/
|
||||||
|
#define MB_FUNC_OTHER_REP_SLAVEID_BUF ( 32 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Report Slave ID</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_OTHER_REP_SLAVEID_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Read Input Registers</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_READ_INPUT_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Read Holding Registers</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_READ_HOLDING_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Write Single Register</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_WRITE_HOLDING_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Write Multiple registers</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Read Coils</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_READ_COILS_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Write Coils</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_WRITE_COIL_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Write Multiple Coils</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Read Discrete Inputs</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_READ_DISCRETE_INPUTS_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! \brief If the <em>Read/Write Multiple Registers</em> function should be enabled. */
|
||||||
|
#define MB_FUNC_READWRITE_HOLDING_ENABLED ( 1 )
|
||||||
|
|
||||||
|
/*! @} */
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
87
Core/Src/Modbus/include/mbframe.h
Normal file
87
Core/Src/Modbus/include/mbframe.h
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbframe.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_FRAME_H
|
||||||
|
#define _MB_FRAME_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* Constants which defines the format of a modbus frame. The example is
|
||||||
|
* shown for a Modbus RTU/ASCII frame. Note that the Modbus PDU is not
|
||||||
|
* dependent on the underlying transport.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* <------------------------ MODBUS SERIAL LINE PDU (1) ------------------->
|
||||||
|
* <----------- MODBUS PDU (1') ---------------->
|
||||||
|
* +-----------+---------------+----------------------------+-------------+
|
||||||
|
* | Address | Function Code | Data | CRC/LRC |
|
||||||
|
* +-----------+---------------+----------------------------+-------------+
|
||||||
|
* | | | |
|
||||||
|
* (2) (3/2') (3') (4)
|
||||||
|
*
|
||||||
|
* (1) ... MB_SER_PDU_SIZE_MAX = 256
|
||||||
|
* (2) ... MB_SER_PDU_ADDR_OFF = 0
|
||||||
|
* (3) ... MB_SER_PDU_PDU_OFF = 1
|
||||||
|
* (4) ... MB_SER_PDU_SIZE_CRC = 2
|
||||||
|
*
|
||||||
|
* (1') ... MB_PDU_SIZE_MAX = 253
|
||||||
|
* (2') ... MB_PDU_FUNC_OFF = 0
|
||||||
|
* (3') ... MB_PDU_DATA_OFF = 1
|
||||||
|
* </code>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_PDU_SIZE_MAX 253 /*!< Maximum size of a PDU. */
|
||||||
|
#define MB_PDU_SIZE_MIN 1 /*!< Function Code */
|
||||||
|
#define MB_PDU_FUNC_OFF 0 /*!< Offset of function code in PDU. */
|
||||||
|
#define MB_PDU_DATA_OFF 1 /*!< Offset for response data in PDU. */
|
||||||
|
|
||||||
|
/* ----------------------- Prototypes 0-------------------------------------*/
|
||||||
|
typedef void ( *pvMBFrameStart ) ( void );
|
||||||
|
|
||||||
|
typedef void ( *pvMBFrameStop ) ( void );
|
||||||
|
|
||||||
|
typedef eMBErrorCode( *peMBFrameReceive ) ( UCHAR * pucRcvAddress,
|
||||||
|
UCHAR ** pucFrame,
|
||||||
|
USHORT * pusLength );
|
||||||
|
|
||||||
|
typedef eMBErrorCode( *peMBFrameSend ) ( UCHAR slaveAddress,
|
||||||
|
const UCHAR * pucFrame,
|
||||||
|
USHORT usLength );
|
||||||
|
|
||||||
|
typedef void( *pvMBFrameClose ) ( void );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
80
Core/Src/Modbus/include/mbfunc.h
Normal file
80
Core/Src/Modbus/include/mbfunc.h
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbfunc.h,v 1.12 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_FUNC_H
|
||||||
|
#define _MB_FUNC_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_OTHER_REP_SLAVEID_BUF > 0
|
||||||
|
eMBException eMBFuncReportSlaveID( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||||
|
eMBException eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||||
|
eMBException eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||||
|
eMBException eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||||
|
eMBException eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||||
|
eMBException eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||||
|
eMBException eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||||
|
eMBException eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||||
|
eMBException eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||||
|
eMBException eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
129
Core/Src/Modbus/include/mbport.h
Normal file
129
Core/Src/Modbus/include/mbport.h
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbport.h,v 1.19 2010/06/06 13:54:40 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_PORT_H
|
||||||
|
#define _MB_PORT_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
EV_READY, /*!< Startup finished. */
|
||||||
|
EV_FRAME_RECEIVED, /*!< Frame received. */
|
||||||
|
EV_EXECUTE, /*!< Execute function. */
|
||||||
|
EV_FRAME_SENT /*!< Frame sent. */
|
||||||
|
} eMBEventType;
|
||||||
|
|
||||||
|
/*! \ingroup modbus
|
||||||
|
* \brief Parity used for characters in serial mode.
|
||||||
|
*
|
||||||
|
* The parity which should be applied to the characters sent over the serial
|
||||||
|
* link. Please note that this values are actually passed to the porting
|
||||||
|
* layer and therefore not all parity modes might be available.
|
||||||
|
*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MB_PAR_NONE, /*!< No parity. */
|
||||||
|
MB_PAR_ODD, /*!< Odd parity. */
|
||||||
|
MB_PAR_EVEN /*!< Even parity. */
|
||||||
|
} eMBParity;
|
||||||
|
|
||||||
|
/* ----------------------- Supporting functions -----------------------------*/
|
||||||
|
_Bool xMBPortEventInit( void );
|
||||||
|
|
||||||
|
_Bool xMBPortEventPost( eMBEventType eEvent );
|
||||||
|
|
||||||
|
_Bool xMBPortEventGet( /*@out@ */ eMBEventType * eEvent );
|
||||||
|
|
||||||
|
/* ----------------------- Serial port functions ----------------------------*/
|
||||||
|
|
||||||
|
_Bool xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate,
|
||||||
|
UCHAR ucDataBits, eMBParity eParity );
|
||||||
|
|
||||||
|
void vMBPortClose( void );
|
||||||
|
|
||||||
|
void xMBPortSerialClose( void );
|
||||||
|
|
||||||
|
void vMBPortSerialEnable( _Bool xRxEnable, _Bool xTxEnable );
|
||||||
|
|
||||||
|
_Bool xMBPortSerialGetByte( CHAR * pucByte );
|
||||||
|
|
||||||
|
_Bool xMBPortSerialPutByte( CHAR ucByte );
|
||||||
|
|
||||||
|
/* ----------------------- Timers functions ---------------------------------*/
|
||||||
|
_Bool xMBPortTimersInit( USHORT usTimeOut50us );
|
||||||
|
|
||||||
|
void xMBPortTimersClose( void );
|
||||||
|
|
||||||
|
void vMBPortTimersEnable( void );
|
||||||
|
|
||||||
|
void vMBPortTimersDisable( void );
|
||||||
|
|
||||||
|
void vMBPortTimersDelay( USHORT usTimeOutMS );
|
||||||
|
|
||||||
|
/* ----------------------- Callback for the protocol stack ------------------*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
* \brief Callback function for the porting layer when a new byte is
|
||||||
|
* available.
|
||||||
|
*
|
||||||
|
* Depending upon the mode this callback function is used by the RTU or
|
||||||
|
* ASCII transmission layers. In any case a call to xMBPortSerialGetByte()
|
||||||
|
* must immediately return a new character.
|
||||||
|
*
|
||||||
|
* \return <code>TRUE</code> if a event was posted to the queue because
|
||||||
|
* a new byte was received. The port implementation should wake up the
|
||||||
|
* tasks which are currently blocked on the eventqueue.
|
||||||
|
*/
|
||||||
|
extern _Bool( *pxMBFrameCBByteReceived ) ( void );
|
||||||
|
|
||||||
|
extern _Bool( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||||
|
|
||||||
|
extern _Bool( *pxMBPortCBTimerExpired ) ( void );
|
||||||
|
|
||||||
|
/* ----------------------- TCP port functions -------------------------------*/
|
||||||
|
_Bool xMBTCPPortInit( USHORT usTCPPort );
|
||||||
|
|
||||||
|
void vMBTCPPortClose( void );
|
||||||
|
|
||||||
|
void vMBTCPPortDisable( void );
|
||||||
|
|
||||||
|
_Bool xMBTCPPortGetRequest( UCHAR **ppucMBTCPFrame, USHORT * usTCPLength );
|
||||||
|
|
||||||
|
_Bool xMBTCPPortSendResponse( const UCHAR *pucMBTCPFrame, USHORT usTCPLength );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
83
Core/Src/Modbus/include/mbproto.h
Normal file
83
Core/Src/Modbus/include/mbproto.h
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbproto.h,v 1.14 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_PROTO_H
|
||||||
|
#define _MB_PROTO_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_ADDRESS_BROADCAST ( 0 ) /*! Modbus broadcast address. */
|
||||||
|
#define MB_ADDRESS_MIN ( 1 ) /*! Smallest possible slave address. */
|
||||||
|
#define MB_ADDRESS_MAX ( 247 ) /*! Biggest possible slave address. */
|
||||||
|
#define MB_FUNC_NONE ( 0 )
|
||||||
|
#define MB_FUNC_READ_COILS ( 1 )
|
||||||
|
#define MB_FUNC_READ_DISCRETE_INPUTS ( 2 )
|
||||||
|
#define MB_FUNC_WRITE_SINGLE_COIL ( 5 )
|
||||||
|
#define MB_FUNC_WRITE_MULTIPLE_COILS ( 15 )
|
||||||
|
#define MB_FUNC_READ_HOLDING_REGISTER ( 3 )
|
||||||
|
#define MB_FUNC_READ_INPUT_REGISTER ( 4 )
|
||||||
|
#define MB_FUNC_WRITE_REGISTER ( 6 )
|
||||||
|
#define MB_FUNC_WRITE_MULTIPLE_REGISTERS ( 16 )
|
||||||
|
#define MB_FUNC_READWRITE_MULTIPLE_REGISTERS ( 23 )
|
||||||
|
#define MB_FUNC_DIAG_READ_EXCEPTION ( 7 )
|
||||||
|
#define MB_FUNC_DIAG_DIAGNOSTIC ( 8 )
|
||||||
|
#define MB_FUNC_DIAG_GET_COM_EVENT_CNT ( 11 )
|
||||||
|
#define MB_FUNC_DIAG_GET_COM_EVENT_LOG ( 12 )
|
||||||
|
#define MB_FUNC_OTHER_REPORT_SLAVEID ( 17 )
|
||||||
|
#define MB_FUNC_ERROR ( 128 )
|
||||||
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
MB_EX_NONE = 0x00,
|
||||||
|
MB_EX_ILLEGAL_FUNCTION = 0x01,
|
||||||
|
MB_EX_ILLEGAL_DATA_ADDRESS = 0x02,
|
||||||
|
MB_EX_ILLEGAL_DATA_VALUE = 0x03,
|
||||||
|
MB_EX_SLAVE_DEVICE_FAILURE = 0x04,
|
||||||
|
MB_EX_ACKNOWLEDGE = 0x05,
|
||||||
|
MB_EX_SLAVE_BUSY = 0x06,
|
||||||
|
MB_EX_MEMORY_PARITY_ERROR = 0x08,
|
||||||
|
MB_EX_GATEWAY_PATH_FAILED = 0x0A,
|
||||||
|
MB_EX_GATEWAY_TGT_FAILED = 0x0B
|
||||||
|
} eMBException;
|
||||||
|
|
||||||
|
typedef eMBException( *pxMBFunctionHandler ) ( UCHAR * pucFrame, USHORT * pusLength );
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
UCHAR ucFunctionCode;
|
||||||
|
pxMBFunctionHandler pxHandler;
|
||||||
|
} xMBFunctionHandler;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
108
Core/Src/Modbus/include/mbutils.h
Normal file
108
Core/Src/Modbus/include/mbutils.h
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbutils.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_UTILS_H
|
||||||
|
#define _MB_UTILS_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
/*! \defgroup modbus_utils Utilities
|
||||||
|
*
|
||||||
|
* This module contains some utility functions which can be used by
|
||||||
|
* the application. It includes some special functions for working with
|
||||||
|
* bitfields backed by a character array buffer.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*! \addtogroup modbus_utils
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
/*! \brief Function to set bits in a byte buffer.
|
||||||
|
*
|
||||||
|
* This function allows the efficient use of an array to implement bitfields.
|
||||||
|
* The array used for storing the bits must always be a multiple of two
|
||||||
|
* bytes. Up to eight bits can be set or cleared in one operation.
|
||||||
|
*
|
||||||
|
* \param ucByteBuf A buffer where the bit values are stored. Must be a
|
||||||
|
* multiple of 2 bytes. No length checking is performed and if
|
||||||
|
* usBitOffset / 8 is greater than the size of the buffer memory contents
|
||||||
|
* is overwritten.
|
||||||
|
* \param usBitOffset The starting address of the bits to set. The first
|
||||||
|
* bit has the offset 0.
|
||||||
|
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||||
|
* than 8.
|
||||||
|
* \param ucValues Thew new values for the bits. The value for the first bit
|
||||||
|
* starting at <code>usBitOffset</code> is the LSB of the value
|
||||||
|
* <code>ucValues</code>
|
||||||
|
*
|
||||||
|
* \code
|
||||||
|
* ucBits[2] = {0, 0};
|
||||||
|
*
|
||||||
|
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
|
||||||
|
* xMBUtilSetBits( ucBits, 4, 1, 1 );
|
||||||
|
*
|
||||||
|
* // Set bit 7 to 1 and bit 8 to 0.
|
||||||
|
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
|
||||||
|
*
|
||||||
|
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
|
||||||
|
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
|
||||||
|
* \endcode
|
||||||
|
*/
|
||||||
|
void xMBUtilSetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||||
|
UCHAR ucNBits, UCHAR ucValues );
|
||||||
|
|
||||||
|
/*! \brief Function to read bits in a byte buffer.
|
||||||
|
*
|
||||||
|
* This function is used to extract up bit values from an array. Up to eight
|
||||||
|
* bit values can be extracted in one step.
|
||||||
|
*
|
||||||
|
* \param ucByteBuf A buffer where the bit values are stored.
|
||||||
|
* \param usBitOffset The starting address of the bits to set. The first
|
||||||
|
* bit has the offset 0.
|
||||||
|
* \param ucNBits Number of bits to modify. The value must always be smaller
|
||||||
|
* than 8.
|
||||||
|
*
|
||||||
|
* \code
|
||||||
|
* UCHAR ucBits[2] = {0, 0};
|
||||||
|
* UCHAR ucResult;
|
||||||
|
*
|
||||||
|
* // Extract the bits 3 - 10.
|
||||||
|
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
|
||||||
|
* \endcode
|
||||||
|
*/
|
||||||
|
UCHAR xMBUtilGetBits( UCHAR * ucByteBuf, USHORT usBitOffset,
|
||||||
|
UCHAR ucNBits );
|
||||||
|
|
||||||
|
/*! @} */
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
414
Core/Src/Modbus/mb.c
Normal file
414
Core/Src/Modbus/mb.c
Normal file
@ -0,0 +1,414 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mb.c,v 1.28 2010/06/06 13:54:40 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbproto.h"
|
||||||
|
#include "mbfunc.h"
|
||||||
|
|
||||||
|
#include "mbport.h"
|
||||||
|
#if MB_RTU_ENABLED == 1
|
||||||
|
#include "mbrtu.h"
|
||||||
|
#endif
|
||||||
|
#if MB_ASCII_ENABLED == 1
|
||||||
|
#include "mbascii.h"
|
||||||
|
#endif
|
||||||
|
#if MB_TCP_ENABLED == 1
|
||||||
|
#include "mbtcp.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MB_PORT_HAS_CLOSE
|
||||||
|
#define MB_PORT_HAS_CLOSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
|
||||||
|
static UCHAR ucMBAddress;
|
||||||
|
static eMBMode eMBCurrentMode;
|
||||||
|
|
||||||
|
static enum
|
||||||
|
{
|
||||||
|
STATE_ENABLED,
|
||||||
|
STATE_DISABLED,
|
||||||
|
STATE_NOT_INITIALIZED
|
||||||
|
} eMBState = STATE_NOT_INITIALIZED;
|
||||||
|
|
||||||
|
/* Functions pointer which are initialized in eMBInit( ). Depending on the
|
||||||
|
* mode (RTU or ASCII) the are set to the correct implementations.
|
||||||
|
*/
|
||||||
|
static peMBFrameSend peMBFrameSendCur;
|
||||||
|
static pvMBFrameStart pvMBFrameStartCur;
|
||||||
|
static pvMBFrameStop pvMBFrameStopCur;
|
||||||
|
static peMBFrameReceive peMBFrameReceiveCur;
|
||||||
|
static pvMBFrameClose pvMBFrameCloseCur;
|
||||||
|
|
||||||
|
/* Callback functions required by the porting layer. They are called when
|
||||||
|
* an external event has happend which includes a timeout or the reception
|
||||||
|
* or transmission of a character.
|
||||||
|
*/
|
||||||
|
_Bool( *pxMBFrameCBByteReceived ) ( void );
|
||||||
|
_Bool( *pxMBFrameCBTransmitterEmpty ) ( void );
|
||||||
|
_Bool( *pxMBPortCBTimerExpired ) ( void );
|
||||||
|
|
||||||
|
_Bool( *pxMBFrameCBReceiveFSMCur ) ( void );
|
||||||
|
_Bool( *pxMBFrameCBTransmitFSMCur ) ( void );
|
||||||
|
|
||||||
|
/* An array of Modbus functions handlers which associates Modbus function
|
||||||
|
* codes with implementing functions.
|
||||||
|
*/
|
||||||
|
static xMBFunctionHandler xFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
|
||||||
|
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
|
||||||
|
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_READ_INPUT_ENABLED > 0
|
||||||
|
{MB_FUNC_READ_INPUT_REGISTER, eMBFuncReadInputRegister},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_READ_HOLDING_ENABLED > 0
|
||||||
|
{MB_FUNC_READ_HOLDING_REGISTER, eMBFuncReadHoldingRegister},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
|
||||||
|
{MB_FUNC_WRITE_MULTIPLE_REGISTERS, eMBFuncWriteMultipleHoldingRegister},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_WRITE_HOLDING_ENABLED > 0
|
||||||
|
{MB_FUNC_WRITE_REGISTER, eMBFuncWriteHoldingRegister},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
|
||||||
|
{MB_FUNC_READWRITE_MULTIPLE_REGISTERS, eMBFuncReadWriteMultipleHoldingRegister},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_READ_COILS_ENABLED > 0
|
||||||
|
{MB_FUNC_READ_COILS, eMBFuncReadCoils},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_WRITE_COIL_ENABLED > 0
|
||||||
|
{MB_FUNC_WRITE_SINGLE_COIL, eMBFuncWriteCoil},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
|
||||||
|
{MB_FUNC_WRITE_MULTIPLE_COILS, eMBFuncWriteMultipleCoils},
|
||||||
|
#endif
|
||||||
|
#if MB_FUNC_READ_DISCRETE_INPUTS_ENABLED > 0
|
||||||
|
{MB_FUNC_READ_DISCRETE_INPUTS, eMBFuncReadDiscreteInputs},
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBErrorCode
|
||||||
|
eMBInit( eMBMode eMode, UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
/* check preconditions */
|
||||||
|
if( ( ucSlaveAddress == MB_ADDRESS_BROADCAST ) ||
|
||||||
|
( ucSlaveAddress < MB_ADDRESS_MIN ) || ( ucSlaveAddress > MB_ADDRESS_MAX ) )
|
||||||
|
{
|
||||||
|
eStatus = MB_EINVAL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ucMBAddress = ucSlaveAddress;
|
||||||
|
|
||||||
|
switch ( eMode )
|
||||||
|
{
|
||||||
|
#if MB_RTU_ENABLED > 0
|
||||||
|
case MB_RTU:
|
||||||
|
pvMBFrameStartCur = eMBRTUStart;
|
||||||
|
pvMBFrameStopCur = eMBRTUStop;
|
||||||
|
peMBFrameSendCur = eMBRTUSend;
|
||||||
|
peMBFrameReceiveCur = eMBRTUReceive;
|
||||||
|
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
|
||||||
|
pxMBFrameCBByteReceived = xMBRTUReceiveFSM;
|
||||||
|
pxMBFrameCBTransmitterEmpty = xMBRTUTransmitFSM;
|
||||||
|
pxMBPortCBTimerExpired = xMBRTUTimerT35Expired;
|
||||||
|
|
||||||
|
eStatus = eMBRTUInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
#if MB_ASCII_ENABLED > 0
|
||||||
|
case MB_ASCII:
|
||||||
|
pvMBFrameStartCur = eMBASCIIStart;
|
||||||
|
pvMBFrameStopCur = eMBASCIIStop;
|
||||||
|
peMBFrameSendCur = eMBASCIISend;
|
||||||
|
peMBFrameReceiveCur = eMBASCIIReceive;
|
||||||
|
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBPortClose : NULL;
|
||||||
|
pxMBFrameCBByteReceived = xMBASCIIReceiveFSM;
|
||||||
|
pxMBFrameCBTransmitterEmpty = xMBASCIITransmitFSM;
|
||||||
|
pxMBPortCBTimerExpired = xMBASCIITimerT1SExpired;
|
||||||
|
|
||||||
|
eStatus = eMBASCIIInit( ucMBAddress, ucPort, ulBaudRate, eParity );
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
eStatus = MB_EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( eStatus == MB_ENOERR )
|
||||||
|
{
|
||||||
|
if( !xMBPortEventInit( ) )
|
||||||
|
{
|
||||||
|
/* port dependent event module initalization failed. */
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eMBCurrentMode = eMode;
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MB_TCP_ENABLED > 0
|
||||||
|
eMBErrorCode
|
||||||
|
eMBTCPInit( USHORT ucTCPPort )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( ( eStatus = eMBTCPDoInit( ucTCPPort ) ) != MB_ENOERR )
|
||||||
|
{
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
}
|
||||||
|
else if( !xMBPortEventInit( ) )
|
||||||
|
{
|
||||||
|
/* Port dependent event module initalization failed. */
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pvMBFrameStartCur = eMBTCPStart;
|
||||||
|
pvMBFrameStopCur = eMBTCPStop;
|
||||||
|
peMBFrameReceiveCur = eMBTCPReceive;
|
||||||
|
peMBFrameSendCur = eMBTCPSend;
|
||||||
|
pvMBFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
|
||||||
|
ucMBAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||||
|
eMBCurrentMode = MB_TCP;
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRegisterCB( UCHAR ucFunctionCode, pxMBFunctionHandler pxHandler )
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
eMBErrorCode eStatus;
|
||||||
|
|
||||||
|
if( ( 0 < ucFunctionCode ) && ( ucFunctionCode <= 127 ) )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
if( pxHandler != NULL )
|
||||||
|
{
|
||||||
|
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||||
|
{
|
||||||
|
if( ( xFuncHandlers[i].pxHandler == NULL ) ||
|
||||||
|
( xFuncHandlers[i].pxHandler == pxHandler ) )
|
||||||
|
{
|
||||||
|
xFuncHandlers[i].ucFunctionCode = ucFunctionCode;
|
||||||
|
xFuncHandlers[i].pxHandler = pxHandler;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
eStatus = ( i != MB_FUNC_HANDLERS_MAX ) ? MB_ENOERR : MB_ENORES;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||||
|
{
|
||||||
|
if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
|
||||||
|
{
|
||||||
|
xFuncHandlers[i].ucFunctionCode = 0;
|
||||||
|
xFuncHandlers[i].pxHandler = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Remove can't fail. */
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EINVAL;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBClose( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
if( pvMBFrameCloseCur != NULL )
|
||||||
|
{
|
||||||
|
pvMBFrameCloseCur( );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBEnable( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
/* Activate the protocol stack. */
|
||||||
|
pvMBFrameStartCur( );
|
||||||
|
eMBState = STATE_ENABLED;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBDisable( void )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus;
|
||||||
|
|
||||||
|
if( eMBState == STATE_ENABLED )
|
||||||
|
{
|
||||||
|
pvMBFrameStopCur( );
|
||||||
|
eMBState = STATE_DISABLED;
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
}
|
||||||
|
else if( eMBState == STATE_DISABLED )
|
||||||
|
{
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
extern void delay_us ( uint32_t us );
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBPoll( void )
|
||||||
|
{
|
||||||
|
static UCHAR *ucMBFrame;
|
||||||
|
static UCHAR ucRcvAddress;
|
||||||
|
static UCHAR ucFunctionCode;
|
||||||
|
static USHORT usLength;
|
||||||
|
static eMBException eException;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
eMBEventType eEvent;
|
||||||
|
|
||||||
|
/* Check if the protocol stack is ready. */
|
||||||
|
if( eMBState != STATE_ENABLED )
|
||||||
|
{
|
||||||
|
return MB_EILLSTATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check if there is a event available. If not return control to caller.
|
||||||
|
* Otherwise we will handle the event. */
|
||||||
|
if( xMBPortEventGet( &eEvent ) == TRUE )
|
||||||
|
{
|
||||||
|
switch ( eEvent )
|
||||||
|
{
|
||||||
|
case EV_READY:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EV_FRAME_RECEIVED:
|
||||||
|
eStatus = peMBFrameReceiveCur( &ucRcvAddress, &ucMBFrame, &usLength );
|
||||||
|
if( eStatus == MB_ENOERR )
|
||||||
|
{
|
||||||
|
/* Check if the frame is for us. If not ignore the frame. */
|
||||||
|
if( ( ucRcvAddress == ucMBAddress ) || ( ucRcvAddress == MB_ADDRESS_BROADCAST ) )
|
||||||
|
{
|
||||||
|
( void )xMBPortEventPost( EV_EXECUTE );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EV_EXECUTE:
|
||||||
|
ucFunctionCode = ucMBFrame[MB_PDU_FUNC_OFF];
|
||||||
|
eException = MB_EX_ILLEGAL_FUNCTION;
|
||||||
|
for( i = 0; i < MB_FUNC_HANDLERS_MAX; i++ )
|
||||||
|
{
|
||||||
|
/* No more function handlers registered. Abort. */
|
||||||
|
if( xFuncHandlers[i].ucFunctionCode == 0 )
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if( xFuncHandlers[i].ucFunctionCode == ucFunctionCode )
|
||||||
|
{
|
||||||
|
eException = xFuncHandlers[i].pxHandler( ucMBFrame, &usLength );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* If the request was not sent to the broadcast address we
|
||||||
|
* return a reply. */
|
||||||
|
if( ucRcvAddress != MB_ADDRESS_BROADCAST )
|
||||||
|
{
|
||||||
|
if( eException != MB_EX_NONE )
|
||||||
|
{
|
||||||
|
/* An exception occured. Build an error frame. */
|
||||||
|
usLength = 0;
|
||||||
|
ucMBFrame[usLength++] = ( UCHAR )( ucFunctionCode | MB_FUNC_ERROR );
|
||||||
|
ucMBFrame[usLength++] = eException;
|
||||||
|
}
|
||||||
|
if( ( eMBCurrentMode == MB_ASCII ) && MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS )
|
||||||
|
{
|
||||||
|
// vMBPortTimersDelay( MB_ASCII_TIMEOUT_WAIT_BEFORE_SEND_MS );
|
||||||
|
}
|
||||||
|
|
||||||
|
eStatus = peMBFrameSendCur( ucMBAddress, ucMBFrame, usLength );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case EV_FRAME_SENT:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MB_ENOERR;
|
||||||
|
}
|
||||||
58
Core/Src/Modbus/port-stm32/port.h
Normal file
58
Core/Src/Modbus/port-stm32/port.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: BARE Port
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* File: $Id: port.h,v 1.1 2006/08/22 21:35:13 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _PORT_H
|
||||||
|
#define _PORT_H
|
||||||
|
|
||||||
|
// #include <assert.h>
|
||||||
|
// #include <inttypes.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
// #define INLINE __inline
|
||||||
|
#define PR_BEGIN_EXTERN_C extern "C" {
|
||||||
|
#define PR_END_EXTERN_C }
|
||||||
|
|
||||||
|
#define ENTER_CRITICAL_SECTION( )
|
||||||
|
// __disable_irq()
|
||||||
|
#define EXIT_CRITICAL_SECTION( )
|
||||||
|
// __enable_irq()
|
||||||
|
|
||||||
|
typedef unsigned char UCHAR;
|
||||||
|
typedef char CHAR;
|
||||||
|
|
||||||
|
typedef uint16_t USHORT;
|
||||||
|
typedef int16_t SHORT;
|
||||||
|
|
||||||
|
typedef uint32_t ULONG;
|
||||||
|
typedef int32_t LONG;
|
||||||
|
|
||||||
|
#ifndef TRUE
|
||||||
|
#define TRUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FALSE
|
||||||
|
#define FALSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
81
Core/Src/Modbus/port-stm32/portEvent.c
Normal file
81
Core/Src/Modbus/port-stm32/portEvent.c
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: BARE Port
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* File: $Id: portevent.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
|
||||||
|
/* ----------------------- Variables ----------------------------------------*/
|
||||||
|
static eMBEventType eQueuedEvent;
|
||||||
|
static _Bool xEventInQueue;
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
_Bool
|
||||||
|
xMBPortEventInit( void )
|
||||||
|
{
|
||||||
|
xEventInQueue = FALSE;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBPortEventPost( eMBEventType eEvent )
|
||||||
|
{
|
||||||
|
xEventInQueue = TRUE;
|
||||||
|
eQueuedEvent = eEvent;
|
||||||
|
|
||||||
|
// 悬起PendSV,使用PendSV解析协议栈
|
||||||
|
// SCB->ICSR = SCB_ICSR_PENDSVSET;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBPortEventGet( eMBEventType * eEvent )
|
||||||
|
{
|
||||||
|
_Bool xEventHappened = FALSE;
|
||||||
|
|
||||||
|
if( xEventInQueue )
|
||||||
|
{
|
||||||
|
*eEvent = eQueuedEvent;
|
||||||
|
xEventInQueue = FALSE;
|
||||||
|
xEventHappened = TRUE;
|
||||||
|
}
|
||||||
|
return xEventHappened;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// PendSV中断
|
||||||
|
//__irq
|
||||||
|
//void
|
||||||
|
//PendSV_Handler ( void )
|
||||||
|
//{
|
||||||
|
// eMBPoll();
|
||||||
|
//}
|
||||||
|
void ModbusPoll( void * parg )
|
||||||
|
{
|
||||||
|
for( ;; )
|
||||||
|
{
|
||||||
|
eMBPoll();
|
||||||
|
osDelay( 10u );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
224
Core/Src/Modbus/port-stm32/portSerial.c
Normal file
224
Core/Src/Modbus/port-stm32/portSerial.c
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: BARE Port
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* File: $Id: portserial.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
|
||||||
|
*/
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
#include "port.h"
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
#include "stm32f10x.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------- static functions ---------------------------------*/
|
||||||
|
extern void delay_us ( uint32_t us );
|
||||||
|
static void prvvUARTTxReadyISR( void );
|
||||||
|
static void prvvUARTRxISR( void );
|
||||||
|
int Errcount = 0;
|
||||||
|
// 控制485总线方向
|
||||||
|
void MB_485_Direct_Transmit( void )
|
||||||
|
{
|
||||||
|
// if( !READ_BIT( GPIOA->ODR, GPIO_ODR_ODR8) )
|
||||||
|
// {
|
||||||
|
SET_BIT( GPIOA->BSRR, GPIO_BSRR_BS8 );
|
||||||
|
// delay_us(100u);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
void MB_485_Direct_Receive( void )
|
||||||
|
{
|
||||||
|
if( READ_BIT( GPIOA->ODR, GPIO_ODR_ODR8) )
|
||||||
|
{
|
||||||
|
delay_us(400u);
|
||||||
|
SET_BIT( GPIOA->BSRR, GPIO_BSRR_BR8 );
|
||||||
|
// delay_us(10u);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
void
|
||||||
|
vMBPortSerialEnable( _Bool xRxEnable, _Bool xTxEnable )
|
||||||
|
{
|
||||||
|
/* If xRxEnable enable serial receive interrupts.
|
||||||
|
* If xTxENable enable transmitter empty interrupts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ( xRxEnable )
|
||||||
|
{
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_RXNEIE );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_RXNEIE );
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( xTxEnable )
|
||||||
|
{
|
||||||
|
// 允许发送,将总线切换到发送状态
|
||||||
|
|
||||||
|
MB_485_Direct_Transmit();
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_TXEIE );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_TXEIE );
|
||||||
|
// 禁止发送,通过发送结束中断切换总线方向
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_TCIE );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
|
||||||
|
{
|
||||||
|
( void )ucPORT;
|
||||||
|
|
||||||
|
SET_BIT( RCC->APB2ENR, RCC_APB2ENR_IOPAEN );
|
||||||
|
MODIFY_REG( GPIOA->CRH, 0x00000FFFu, 0x000004B3u );
|
||||||
|
|
||||||
|
|
||||||
|
SET_BIT( RCC->APB2ENR, RCC_APB2ENR_USART1EN );
|
||||||
|
|
||||||
|
MB_485_Direct_Receive();
|
||||||
|
MB_USART->CR1 = 0u;
|
||||||
|
MB_USART->CR2 = 0u;
|
||||||
|
MB_USART->CR3 = 0u;
|
||||||
|
MB_USART->CR1 = USART_CR1_RE | USART_CR1_TE;
|
||||||
|
MB_USART->BRR = ( SystemCoreClock / ulBaudRate );
|
||||||
|
|
||||||
|
switch ( ucDataBits )
|
||||||
|
{
|
||||||
|
case 8:
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_M );
|
||||||
|
break;
|
||||||
|
case 9:
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_M );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ( eParity )
|
||||||
|
{
|
||||||
|
case MB_PAR_NONE: /*!< No parity. */
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_PCE );
|
||||||
|
break;
|
||||||
|
case MB_PAR_ODD: /*!< Odd parity. */
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_PS );
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_PCE );
|
||||||
|
break;
|
||||||
|
case MB_PAR_EVEN: /*!< Even parity. */
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_PS );
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_PCE );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
SET_BIT( MB_USART->CR1, USART_CR1_UE ); // Enable MB_USART
|
||||||
|
|
||||||
|
NVIC_EnableIRQ( USART1_IRQn );
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBPortSerialPutByte( CHAR ucByte )
|
||||||
|
{
|
||||||
|
/* Put a byte in the UARTs transmit buffer. This function is called
|
||||||
|
* by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
|
||||||
|
* called. */
|
||||||
|
MB_485_Direct_Transmit();
|
||||||
|
MB_USART->DR = ucByte;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBPortSerialGetByte( CHAR * pucByte )
|
||||||
|
{
|
||||||
|
/* Return the byte in the UARTs receive buffer. This function is called
|
||||||
|
* by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
|
||||||
|
*/
|
||||||
|
MB_485_Direct_Receive();
|
||||||
|
* pucByte = MB_USART->DR;
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create an interrupt handler for the transmit buffer empty interrupt
|
||||||
|
* (or an equivalent) for your target processor. This function should then
|
||||||
|
* call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
|
||||||
|
* a new character can be sent. The protocol stack will then call
|
||||||
|
* xMBPortSerialPutByte( ) to send the character.
|
||||||
|
*/
|
||||||
|
static void prvvUARTTxReadyISR( void )
|
||||||
|
{
|
||||||
|
pxMBFrameCBTransmitterEmpty( );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create an interrupt handler for the receive interrupt for your target
|
||||||
|
* processor. This function should then call pxMBFrameCBByteReceived( ). The
|
||||||
|
* protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
|
||||||
|
* character.
|
||||||
|
*/
|
||||||
|
static void prvvUARTRxISR( void )
|
||||||
|
{
|
||||||
|
pxMBFrameCBByteReceived( );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 串口中断
|
||||||
|
USHORT ucByte;
|
||||||
|
void
|
||||||
|
USART1_IRQHandler( void )
|
||||||
|
{
|
||||||
|
if( USART1->SR & USART_SR_ORE == USART_SR_ORE )
|
||||||
|
{
|
||||||
|
ucByte = (unsigned char)(USART1->SR);
|
||||||
|
ucByte = (unsigned char)(USART1->DR);
|
||||||
|
}
|
||||||
|
if(ucByte != 0)
|
||||||
|
{
|
||||||
|
ucByte = 0;
|
||||||
|
Errcount ++;
|
||||||
|
}
|
||||||
|
if ( READ_BIT( MB_USART->CR1, USART_CR1_RXNEIE ))
|
||||||
|
{
|
||||||
|
if ( READ_BIT( MB_USART->SR, USART_SR_RXNE ))
|
||||||
|
{
|
||||||
|
// 接收数据处理
|
||||||
|
prvvUARTRxISR();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( READ_BIT( MB_USART->CR1, USART_CR1_TXEIE ))
|
||||||
|
{
|
||||||
|
if ( READ_BIT( MB_USART->SR, USART_SR_TXE ))
|
||||||
|
{
|
||||||
|
// 发送数据处理
|
||||||
|
prvvUARTTxReadyISR();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 发送完成,将总线切换到接收状态
|
||||||
|
if ( READ_BIT( MB_USART->CR1, USART_CR1_TCIE ) || READ_BIT( MB_USART->SR, USART_SR_TC ))
|
||||||
|
{
|
||||||
|
// if ( READ_BIT( MB_USART->SR, USART_SR_TC ))
|
||||||
|
{
|
||||||
|
CLEAR_BIT( MB_USART->CR1, USART_CR1_TCIE );
|
||||||
|
MB_485_Direct_Receive();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
93
Core/Src/Modbus/port-stm32/portTimer.c
Normal file
93
Core/Src/Modbus/port-stm32/portTimer.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: BARE Port
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* File: $Id: porttimer.c,v 1.1 2006/08/22 21:35:13 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
|
||||||
|
/* ----------------------- static functions ---------------------------------*/
|
||||||
|
static void prvvTIMERExpiredISR( void );
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
_Bool
|
||||||
|
xMBPortTimersInit( USHORT usTim1Timerout50us )
|
||||||
|
{
|
||||||
|
// 模块使能
|
||||||
|
SET_BIT( RCC->APB1ENR, RCC_APB1ENR_TIM7EN );
|
||||||
|
// // 时基初始化
|
||||||
|
MB_TIMER->CR1 = 0u; /* Counter Up, Division: 1 */
|
||||||
|
MB_TIMER->PSC = ( SystemCoreClock / 50000 ) - 1u; /* Set the Prescaler value */ /*50us*/
|
||||||
|
MB_TIMER->ARR = usTim1Timerout50us; /* Set the Autoreload value */
|
||||||
|
MB_TIMER->EGR = TIM_EGR_UG; // 生成更新事件,立即更新 PSC
|
||||||
|
MB_TIMER->DIER = TIM_DIER_UIE; // 使能中断
|
||||||
|
// SET_BIT( TIM1->BDTR, TIM_BDTR_MOE );
|
||||||
|
// 配置中断
|
||||||
|
// NVIC_SetPriority( TIM4_IRQn, 255u );
|
||||||
|
NVIC_EnableIRQ( TIM7_IRQn );
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
vMBPortTimersEnable( )
|
||||||
|
{
|
||||||
|
/* Enable the timer with the timeout passed to xMBPortTimersInit( ) */
|
||||||
|
CLEAR_BIT( MB_TIMER->SR, TIM_SR_UIF );
|
||||||
|
SET_BIT( MB_TIMER->DIER, TIM_DIER_UIE );
|
||||||
|
MB_TIMER->CNT = 0u;
|
||||||
|
SET_BIT( MB_TIMER->CR1, TIM_CR1_CEN );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
vMBPortTimersDisable( )
|
||||||
|
{
|
||||||
|
/* Disable any pending timers. */
|
||||||
|
CLEAR_BIT( MB_TIMER->CR1, TIM_CR1_CEN );
|
||||||
|
MB_TIMER->CNT = 0u;
|
||||||
|
CLEAR_BIT( MB_TIMER->DIER, TIM_DIER_UIE );
|
||||||
|
CLEAR_BIT( MB_TIMER->SR, TIM_SR_UIF );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Create an ISR which is called whenever the timer has expired. This function
|
||||||
|
* must then call pxMBPortCBTimerExpired( ) to notify the protocol stack that
|
||||||
|
* the timer has expired.
|
||||||
|
*/
|
||||||
|
static void prvvTIMERExpiredISR( void )
|
||||||
|
{
|
||||||
|
( void )pxMBPortCBTimerExpired( );
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定时器中断
|
||||||
|
|
||||||
|
void
|
||||||
|
TIM7_IRQHandler( void )
|
||||||
|
{
|
||||||
|
if ( READ_BIT( MB_TIMER->SR, TIM_SR_UIF ))
|
||||||
|
{
|
||||||
|
/* Clear TIM Capture Compare1 interrupt pending bit*/
|
||||||
|
CLEAR_BIT( MB_TIMER->SR, TIM_SR_UIF );
|
||||||
|
prvvTIMERExpiredISR( );
|
||||||
|
}
|
||||||
|
}
|
||||||
58
Core/Src/Modbus/port.h
Normal file
58
Core/Src/Modbus/port.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: BARE Port
|
||||||
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2.1 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*
|
||||||
|
* File: $Id: port.h,v 1.1 2006/08/22 21:35:13 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _PORT_H
|
||||||
|
#define _PORT_H
|
||||||
|
|
||||||
|
// #include <assert.h>
|
||||||
|
// #include <inttypes.h>
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
// #define INLINE __inline
|
||||||
|
#define PR_BEGIN_EXTERN_C extern "C" {
|
||||||
|
#define PR_END_EXTERN_C }
|
||||||
|
|
||||||
|
#define ENTER_CRITICAL_SECTION( )
|
||||||
|
// __disable_irq()
|
||||||
|
#define EXIT_CRITICAL_SECTION( )
|
||||||
|
// __enable_irq()
|
||||||
|
|
||||||
|
typedef unsigned char UCHAR;
|
||||||
|
typedef char CHAR;
|
||||||
|
|
||||||
|
typedef uint16_t USHORT;
|
||||||
|
typedef int16_t SHORT;
|
||||||
|
|
||||||
|
typedef uint32_t ULONG;
|
||||||
|
typedef int32_t LONG;
|
||||||
|
|
||||||
|
#ifndef TRUE
|
||||||
|
#define TRUE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef FALSE
|
||||||
|
#define FALSE 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
98
Core/Src/Modbus/rtu/mbcrc.c
Normal file
98
Core/Src/Modbus/rtu/mbcrc.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbcrc.c,v 1.7 2007/02/18 23:50:27 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
static const UCHAR aucCRCHi[] = {
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
|
||||||
|
0x00, 0xC1, 0x81, 0x40
|
||||||
|
};
|
||||||
|
|
||||||
|
static const UCHAR aucCRCLo[] = {
|
||||||
|
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7,
|
||||||
|
0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E,
|
||||||
|
0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9,
|
||||||
|
0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC,
|
||||||
|
0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
|
||||||
|
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32,
|
||||||
|
0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D,
|
||||||
|
0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38,
|
||||||
|
0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF,
|
||||||
|
0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
|
||||||
|
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1,
|
||||||
|
0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4,
|
||||||
|
0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB,
|
||||||
|
0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA,
|
||||||
|
0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
|
||||||
|
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0,
|
||||||
|
0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97,
|
||||||
|
0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E,
|
||||||
|
0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89,
|
||||||
|
0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
|
||||||
|
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83,
|
||||||
|
0x41, 0x81, 0x80, 0x40
|
||||||
|
};
|
||||||
|
|
||||||
|
USHORT
|
||||||
|
usMBCRC16( UCHAR * pucFrame, USHORT usLen )
|
||||||
|
{
|
||||||
|
UCHAR ucCRCHi = 0xFF;
|
||||||
|
UCHAR ucCRCLo = 0xFF;
|
||||||
|
int iIndex;
|
||||||
|
|
||||||
|
while( usLen-- )
|
||||||
|
{
|
||||||
|
iIndex = ucCRCLo ^ *( pucFrame++ );
|
||||||
|
ucCRCLo = ( UCHAR )( ucCRCHi ^ aucCRCHi[iIndex] );
|
||||||
|
ucCRCHi = aucCRCLo[iIndex];
|
||||||
|
}
|
||||||
|
return ( USHORT )( ucCRCHi << 8 | ucCRCLo );
|
||||||
|
}
|
||||||
36
Core/Src/Modbus/rtu/mbcrc.h
Normal file
36
Core/Src/Modbus/rtu/mbcrc.h
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbcrc.h,v 1.5 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_CRC_H
|
||||||
|
#define _MB_CRC_H
|
||||||
|
|
||||||
|
USHORT usMBCRC16( UCHAR * pucFrame, USHORT usLen );
|
||||||
|
|
||||||
|
#endif
|
||||||
357
Core/Src/Modbus/rtu/mbrtu.c
Normal file
357
Core/Src/Modbus/rtu/mbrtu.c
Normal file
@ -0,0 +1,357 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbrtu.c,v 1.18 2007/09/12 10:15:56 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbrtu.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
|
||||||
|
#include "mbcrc.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_SER_PDU_SIZE_MIN 4 /*!< Minimum size of a Modbus RTU frame. */
|
||||||
|
#define MB_SER_PDU_SIZE_MAX 256 /*!< Maximum size of a Modbus RTU frame. */
|
||||||
|
#define MB_SER_PDU_SIZE_CRC 2 /*!< Size of CRC field in PDU. */
|
||||||
|
#define MB_SER_PDU_ADDR_OFF 0 /*!< Offset of slave address in Ser-PDU. */
|
||||||
|
#define MB_SER_PDU_PDU_OFF 1 /*!< Offset of Modbus-PDU in Ser-PDU. */
|
||||||
|
|
||||||
|
/* ----------------------- Type definitions ---------------------------------*/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STATE_RX_INIT, /*!< Receiver is in initial state. */
|
||||||
|
STATE_RX_IDLE, /*!< Receiver is in idle state. */
|
||||||
|
STATE_RX_RCV, /*!< Frame is beeing received. */
|
||||||
|
STATE_RX_ERROR /*!< If the frame is invalid. */
|
||||||
|
} eMBRcvState;
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
STATE_TX_IDLE, /*!< Transmitter is in idle state. */
|
||||||
|
STATE_TX_XMIT /*!< Transmitter is in transfer state. */
|
||||||
|
} eMBSndState;
|
||||||
|
|
||||||
|
/* ----------------------- Static variables ---------------------------------*/
|
||||||
|
static volatile eMBSndState eSndState;
|
||||||
|
static volatile eMBRcvState eRcvState;
|
||||||
|
|
||||||
|
volatile UCHAR ucRTUBuf[MB_SER_PDU_SIZE_MAX];
|
||||||
|
|
||||||
|
static volatile UCHAR *pucSndBufferCur;
|
||||||
|
static volatile USHORT usSndBufferCount;
|
||||||
|
|
||||||
|
static volatile USHORT usRcvBufferPos;
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRTUInit( UCHAR ucSlaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
ULONG usTimerT35_50us;
|
||||||
|
|
||||||
|
( void )ucSlaveAddress;
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
/* Modbus RTU uses 8 Databits. */
|
||||||
|
if( xMBPortSerialInit( ucPort, ulBaudRate, 8, eParity ) != TRUE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* If baudrate > 19200 then we should use the fixed timer values
|
||||||
|
* t35 = 1750us. Otherwise t35 must be 3.5 times the character time.
|
||||||
|
*/
|
||||||
|
if( ulBaudRate > 19200 )
|
||||||
|
{
|
||||||
|
usTimerT35_50us = 35; /* 1800us. */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* The timer reload value for a character is given by:
|
||||||
|
*
|
||||||
|
* ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )
|
||||||
|
* = 11 * Ticks_per_1s / Baudrate
|
||||||
|
* = 220000 / Baudrate
|
||||||
|
* The reload for t3.5 is 1.5 times this value and similary
|
||||||
|
* for t3.5.
|
||||||
|
*/
|
||||||
|
usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );
|
||||||
|
}
|
||||||
|
if( xMBPortTimersInit( ( USHORT ) usTimerT35_50us ) != TRUE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBRTUStart( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
/* Initially the receiver is in the state STATE_RX_INIT. we start
|
||||||
|
* the timer and if no character is received within t3.5 we change
|
||||||
|
* to STATE_RX_IDLE. This makes sure that we delay startup of the
|
||||||
|
* modbus protocol stack until the bus is free.
|
||||||
|
*/
|
||||||
|
eRcvState = STATE_RX_INIT;
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBRTUStop( void )
|
||||||
|
{
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
vMBPortSerialEnable( FALSE, FALSE );
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength )
|
||||||
|
{
|
||||||
|
// _Bool xFrameReceived = FALSE;
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
assert( usRcvBufferPos < MB_SER_PDU_SIZE_MAX );
|
||||||
|
|
||||||
|
/* Length and CRC check */
|
||||||
|
if( ( usRcvBufferPos >= MB_SER_PDU_SIZE_MIN )
|
||||||
|
&& ( usMBCRC16( ( UCHAR * ) ucRTUBuf, usRcvBufferPos ) == 0 ) )
|
||||||
|
{
|
||||||
|
/* Save the address field. All frames are passed to the upper layed
|
||||||
|
* and the decision if a frame is used is done there.
|
||||||
|
*/
|
||||||
|
*pucRcvAddress = ucRTUBuf[MB_SER_PDU_ADDR_OFF];
|
||||||
|
|
||||||
|
/* Total length of Modbus-PDU is Modbus-Serial-Line-PDU minus
|
||||||
|
* size of address field and CRC checksum.
|
||||||
|
*/
|
||||||
|
*pusLength = ( USHORT )( usRcvBufferPos - MB_SER_PDU_PDU_OFF - MB_SER_PDU_SIZE_CRC );
|
||||||
|
|
||||||
|
/* Return the start of the Modbus PDU to the caller. */
|
||||||
|
*pucFrame = ( UCHAR * ) & ucRTUBuf[MB_SER_PDU_PDU_OFF];
|
||||||
|
// xFrameReceived = TRUE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
extern void delay_us ( uint32_t us );
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBRTUSend( UCHAR ucSlaveAddress, const UCHAR * pucFrame, USHORT usLength )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
USHORT usCRC16;
|
||||||
|
|
||||||
|
ENTER_CRITICAL_SECTION( );
|
||||||
|
|
||||||
|
/* Check if the receiver is still in idle state. If not we where to
|
||||||
|
* slow with processing the received frame and the master sent another
|
||||||
|
* frame on the network. We have to abort sending the frame.
|
||||||
|
*/
|
||||||
|
if( eRcvState == STATE_RX_IDLE )
|
||||||
|
{
|
||||||
|
/* First byte before the Modbus-PDU is the slave address. */
|
||||||
|
pucSndBufferCur = ( UCHAR * ) pucFrame - 1;
|
||||||
|
usSndBufferCount = 1;
|
||||||
|
|
||||||
|
/* Now copy the Modbus-PDU into the Modbus-Serial-Line-PDU. */
|
||||||
|
pucSndBufferCur[MB_SER_PDU_ADDR_OFF] = ucSlaveAddress;
|
||||||
|
usSndBufferCount += usLength;
|
||||||
|
|
||||||
|
/* Calculate CRC16 checksum for Modbus-Serial-Line-PDU. */
|
||||||
|
usCRC16 = usMBCRC16( ( UCHAR * ) pucSndBufferCur, usSndBufferCount );
|
||||||
|
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 & 0xFF );
|
||||||
|
ucRTUBuf[usSndBufferCount++] = ( UCHAR )( usCRC16 >> 8 );
|
||||||
|
/* Activate the transmitter. */
|
||||||
|
osDelay(30);
|
||||||
|
eSndState = STATE_TX_XMIT;
|
||||||
|
vMBPortSerialEnable( FALSE, TRUE );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
EXIT_CRITICAL_SECTION( );
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBRTUReceiveFSM( void )
|
||||||
|
{
|
||||||
|
_Bool xTaskNeedSwitch = FALSE;
|
||||||
|
UCHAR ucByte;
|
||||||
|
|
||||||
|
assert( eSndState == STATE_TX_IDLE );
|
||||||
|
|
||||||
|
/* Always read the character. */
|
||||||
|
( void )xMBPortSerialGetByte( ( CHAR * ) & ucByte );
|
||||||
|
|
||||||
|
switch ( eRcvState )
|
||||||
|
{
|
||||||
|
/* If we have received a character in the init state we have to
|
||||||
|
* wait until the frame is finished.
|
||||||
|
*/
|
||||||
|
case STATE_RX_INIT:
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* In the error state we wait until all characters in the
|
||||||
|
* damaged frame are transmitted.
|
||||||
|
*/
|
||||||
|
case STATE_RX_ERROR:
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* In the idle state we wait for a new character. If a character
|
||||||
|
* is received the t1.5 and t3.5 timers are started and the
|
||||||
|
* receiver is in the state STATE_RX_RECEIVCE.
|
||||||
|
*/
|
||||||
|
case STATE_RX_IDLE:
|
||||||
|
usRcvBufferPos = 0;
|
||||||
|
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||||
|
eRcvState = STATE_RX_RCV;
|
||||||
|
|
||||||
|
/* Enable t3.5 timers. */
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* We are currently receiving a frame. Reset the timer after
|
||||||
|
* every character received. If more than the maximum possible
|
||||||
|
* number of bytes in a modbus frame is received the frame is
|
||||||
|
* ignored.
|
||||||
|
*/
|
||||||
|
case STATE_RX_RCV:
|
||||||
|
if( usRcvBufferPos < MB_SER_PDU_SIZE_MAX )
|
||||||
|
{
|
||||||
|
ucRTUBuf[usRcvBufferPos++] = ucByte;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eRcvState = STATE_RX_ERROR;
|
||||||
|
}
|
||||||
|
vMBPortTimersEnable( );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return xTaskNeedSwitch;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBRTUTransmitFSM( void )
|
||||||
|
{
|
||||||
|
_Bool xNeedPoll = FALSE;
|
||||||
|
|
||||||
|
assert( eRcvState == STATE_RX_IDLE );
|
||||||
|
|
||||||
|
switch ( eSndState )
|
||||||
|
{
|
||||||
|
/* We should not get a transmitter event if the transmitter is in
|
||||||
|
* idle state. */
|
||||||
|
case STATE_TX_IDLE:
|
||||||
|
/* enable receiver/disable transmitter. */
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
break;
|
||||||
|
|
||||||
|
case STATE_TX_XMIT:
|
||||||
|
/* check if we are finished. */
|
||||||
|
if( usSndBufferCount != 0 )
|
||||||
|
{
|
||||||
|
xMBPortSerialPutByte( ( CHAR )*pucSndBufferCur );
|
||||||
|
pucSndBufferCur++; /* next byte in sendbuffer. */
|
||||||
|
usSndBufferCount--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
xNeedPoll = xMBPortEventPost( EV_FRAME_SENT );
|
||||||
|
/* Disable transmitter. This prevents another transmit buffer
|
||||||
|
* empty interrupt. */
|
||||||
|
vMBPortSerialEnable( TRUE, FALSE );
|
||||||
|
eSndState = STATE_TX_IDLE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return xNeedPoll;
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool
|
||||||
|
xMBRTUTimerT35Expired( void )
|
||||||
|
{
|
||||||
|
_Bool xNeedPoll = FALSE;
|
||||||
|
|
||||||
|
switch ( eRcvState )
|
||||||
|
{
|
||||||
|
/* Timer t35 expired. Startup phase is finished. */
|
||||||
|
case STATE_RX_INIT:
|
||||||
|
xNeedPoll = xMBPortEventPost( EV_READY );
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* A frame was received and t35 expired. Notify the listener that
|
||||||
|
* a new frame was received. */
|
||||||
|
case STATE_RX_RCV:
|
||||||
|
xNeedPoll = xMBPortEventPost( EV_FRAME_RECEIVED );
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* An error occured while receiving the frame. */
|
||||||
|
case STATE_RX_ERROR:
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* Function called in an illegal state. */
|
||||||
|
default:
|
||||||
|
assert( ( eRcvState == STATE_RX_INIT ) ||
|
||||||
|
( eRcvState == STATE_RX_RCV ) || ( eRcvState == STATE_RX_ERROR ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
vMBPortTimersDisable( );
|
||||||
|
eRcvState = STATE_RX_IDLE;
|
||||||
|
|
||||||
|
return xNeedPoll;
|
||||||
|
}
|
||||||
51
Core/Src/Modbus/rtu/mbrtu.h
Normal file
51
Core/Src/Modbus/rtu/mbrtu.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbrtu.h,v 1.9 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_RTU_H
|
||||||
|
#define _MB_RTU_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
|
||||||
|
eMBParity eParity );
|
||||||
|
void eMBRTUStart( void );
|
||||||
|
void eMBRTUStop( void );
|
||||||
|
eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );
|
||||||
|
eMBErrorCode eMBRTUSend( UCHAR slaveAddress, const UCHAR * pucFrame, USHORT usLength );
|
||||||
|
_Bool xMBRTUReceiveFSM( void );
|
||||||
|
_Bool xMBRTUTransmitFSM( void );
|
||||||
|
_Bool xMBRTUTimerT15Expired( void );
|
||||||
|
_Bool xMBRTUTimerT35Expired( void );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
158
Core/Src/Modbus/tcp/mbtcp.c
Normal file
158
Core/Src/Modbus/tcp/mbtcp.c
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* ----------------------- System includes ----------------------------------*/
|
||||||
|
#include "stdlib.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
|
/* ----------------------- Platform includes --------------------------------*/
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
/* ----------------------- Modbus includes ----------------------------------*/
|
||||||
|
#include "mb.h"
|
||||||
|
#include "mbconfig.h"
|
||||||
|
#include "mbtcp.h"
|
||||||
|
#include "mbframe.h"
|
||||||
|
#include "mbport.h"
|
||||||
|
|
||||||
|
#if MB_TCP_ENABLED > 0
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
|
||||||
|
/* ----------------------- MBAP Header --------------------------------------*/
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
|
||||||
|
* <----------- MODBUS PDU (1') ---------------->
|
||||||
|
* +-----------+---------------+------------------------------------------+
|
||||||
|
* | TID | PID | Length | UID |Code | Data |
|
||||||
|
* +-----------+---------------+------------------------------------------+
|
||||||
|
* | | | | |
|
||||||
|
* (2) (3) (4) (5) (6)
|
||||||
|
*
|
||||||
|
* (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte)
|
||||||
|
* (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte)
|
||||||
|
* (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte)
|
||||||
|
* (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte)
|
||||||
|
* (6) ... MB_TCP_FUNC = 7 (Modbus Function Code)
|
||||||
|
*
|
||||||
|
* (1) ... Modbus TCP/IP Application Data Unit
|
||||||
|
* (1') ... Modbus Protocol Data Unit
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define MB_TCP_TID 0
|
||||||
|
#define MB_TCP_PID 2
|
||||||
|
#define MB_TCP_LEN 4
|
||||||
|
#define MB_TCP_UID 6
|
||||||
|
#define MB_TCP_FUNC 7
|
||||||
|
|
||||||
|
#define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */
|
||||||
|
|
||||||
|
|
||||||
|
/* ----------------------- Start implementation -----------------------------*/
|
||||||
|
eMBErrorCode
|
||||||
|
eMBTCPDoInit( USHORT ucTCPPort )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
if( xMBTCPPortInit( ucTCPPort ) == FALSE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EPORTERR;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBTCPStart( void )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
eMBTCPStop( void )
|
||||||
|
{
|
||||||
|
/* Make sure that no more clients are connected. */
|
||||||
|
vMBTCPPortDisable( );
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_EIO;
|
||||||
|
UCHAR *pucMBTCPFrame;
|
||||||
|
USHORT usLength;
|
||||||
|
USHORT usPID;
|
||||||
|
|
||||||
|
if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
|
||||||
|
{
|
||||||
|
usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
|
||||||
|
usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
|
||||||
|
|
||||||
|
if( usPID == MB_TCP_PROTOCOL_ID )
|
||||||
|
{
|
||||||
|
*ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
|
||||||
|
*pusLength = usLength - MB_TCP_FUNC;
|
||||||
|
eStatus = MB_ENOERR;
|
||||||
|
|
||||||
|
/* Modbus TCP does not use any addresses. Fake the source address such
|
||||||
|
* that the processing part deals with this frame.
|
||||||
|
*/
|
||||||
|
*pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
eMBErrorCode
|
||||||
|
eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
|
||||||
|
{
|
||||||
|
eMBErrorCode eStatus = MB_ENOERR;
|
||||||
|
UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
|
||||||
|
USHORT usTCPLength = usLength + MB_TCP_FUNC;
|
||||||
|
|
||||||
|
/* The MBAP header is already initialized because the caller calls this
|
||||||
|
* function with the buffer returned by the previous call. Therefore we
|
||||||
|
* only have to update the length in the header. Note that the length
|
||||||
|
* header includes the size of the Modbus PDU and the UID Byte. Therefore
|
||||||
|
* the length is usLength plus one.
|
||||||
|
*/
|
||||||
|
pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
|
||||||
|
pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
|
||||||
|
if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
|
||||||
|
{
|
||||||
|
eStatus = MB_EIO;
|
||||||
|
}
|
||||||
|
return eStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
53
Core/Src/Modbus/tcp/mbtcp.h
Normal file
53
Core/Src/Modbus/tcp/mbtcp.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
* FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
|
||||||
|
* Copyright (c) 2006 Christian Walter <wolti@sil.at>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. The name of the author may not be used to endorse or promote products
|
||||||
|
* derived from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||||
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
* File: $Id: mbtcp.h,v 1.2 2006/12/07 22:10:34 wolti Exp $
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MB_TCP_H
|
||||||
|
#define _MB_TCP_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_BEGIN_EXTERN_C
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* ----------------------- Defines ------------------------------------------*/
|
||||||
|
#define MB_TCP_PSEUDO_ADDRESS 255
|
||||||
|
|
||||||
|
/* ----------------------- Function prototypes ------------------------------*/
|
||||||
|
eMBErrorCode eMBTCPDoInit( USHORT ucTCPPort );
|
||||||
|
void eMBTCPStart( void );
|
||||||
|
void eMBTCPStop( void );
|
||||||
|
eMBErrorCode eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame,
|
||||||
|
USHORT * pusLength );
|
||||||
|
eMBErrorCode eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame,
|
||||||
|
USHORT usLength );
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
PR_END_EXTERN_C
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
341
Core/Src/YouSuan.c
Normal file
341
Core/Src/YouSuan.c
Normal file
@ -0,0 +1,341 @@
|
|||||||
|
#include "APPDEF.H"
|
||||||
|
#include "main.h"
|
||||||
|
#include "cmsis_os.h"
|
||||||
|
|
||||||
|
float ADCData;
|
||||||
|
float slope1,slope2;
|
||||||
|
float SetmL = 20;
|
||||||
|
float UseuL;
|
||||||
|
_Bool CuiQuFlag = false;
|
||||||
|
_Bool ZhongHeFlag = false;
|
||||||
|
uint16_t CCRV = 300;
|
||||||
|
float Blank = 0;
|
||||||
|
const uint8_t SampleCupMax = 8;
|
||||||
|
float ABSB; // 下吸光度
|
||||||
|
struct RunSet
|
||||||
|
{
|
||||||
|
uint16_t Speed_Stirrer; // 搅拌器速度
|
||||||
|
_Bool SampleCup[SampleCupMax]; // 样品杯是否存在
|
||||||
|
float AbsU; // 上吸光度
|
||||||
|
float ABSB; // 下吸光度
|
||||||
|
float SetmL; // 添加萃取液体积
|
||||||
|
float UseuL; // 中和液体积
|
||||||
|
float Sample_g; // 样品重量
|
||||||
|
|
||||||
|
_Bool RunFlag; // 启动标志位
|
||||||
|
};
|
||||||
|
float ABSBuf[200];
|
||||||
|
float UseuLBuf[200];
|
||||||
|
uint16_t BufIndex = 0;
|
||||||
|
uint32_t step = 100;
|
||||||
|
uint32_t ADCDataBuf[10];
|
||||||
|
extern void StirrerCtrl( uint16_t CCRV );
|
||||||
|
uint32_t AllStep = 51200;
|
||||||
|
float ADCRef = 0;
|
||||||
|
_Bool HasSample[8] = {false};
|
||||||
|
_Bool SetSample[8] = {true,true,true,true,true,true,true,true};
|
||||||
|
float WeigthSample[8];
|
||||||
|
float UseduL[8] = {0};
|
||||||
|
float mg_g[8] = {0};
|
||||||
|
float uL_Blank = 50;
|
||||||
|
_Bool SampleRunFlag = false;
|
||||||
|
|
||||||
|
|
||||||
|
float AverageData()
|
||||||
|
{
|
||||||
|
return (
|
||||||
|
(ADCDataBuf[0] >> 16) + (uint16_t)ADCDataBuf[0] +
|
||||||
|
(ADCDataBuf[1] >> 16) + (uint16_t)ADCDataBuf[1] +
|
||||||
|
(ADCDataBuf[2] >> 16) + (uint16_t)ADCDataBuf[2] +
|
||||||
|
(ADCDataBuf[3] >> 16) + (uint16_t)ADCDataBuf[3] ) / 8.0f;
|
||||||
|
|
||||||
|
}
|
||||||
|
const uint16_t SingelCupStep = 6400;
|
||||||
|
|
||||||
|
void HasSampleCheck()
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
SpeedSet[M3] = 10000;
|
||||||
|
CoordinateSet[M3] = - SingelCupStep /2;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
|
||||||
|
osDelay(100);
|
||||||
|
ADCData =AverageData();
|
||||||
|
ADCRef = Blank = ADCData;
|
||||||
|
CoordinateSet[M3] = AllStep;
|
||||||
|
uint8_t j = 0;
|
||||||
|
while( CoordinateSet[M3] != CoordinatePosition[M3])
|
||||||
|
{
|
||||||
|
ADCData =AverageData();
|
||||||
|
if( j < 8 )
|
||||||
|
{
|
||||||
|
|
||||||
|
if( CoordinatePosition[M3] > (- SingelCupStep / 4 + j * SingelCupStep - 850 )) // 正负850 共1700
|
||||||
|
{
|
||||||
|
if( CoordinatePosition[M3] < (- SingelCupStep / 4 + j * SingelCupStep + 850 ))
|
||||||
|
{
|
||||||
|
if( ADCRef - ADCData > 500 )
|
||||||
|
{
|
||||||
|
HasSample[j] = true;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
HasSample[j] = false;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf( "%2f,%3f\n",CoordinatePosition[M3] / 100.0f,ADCData);
|
||||||
|
osDelay(10);
|
||||||
|
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
|
||||||
|
SpeedSet[M3] = 20000;
|
||||||
|
|
||||||
|
}
|
||||||
|
osThreadId_t SampleRunTaskHandle;
|
||||||
|
const osThreadAttr_t SampleRunTask_attributes = {
|
||||||
|
.name = "SampleRunTask",
|
||||||
|
.stack_size = 128 * 4,
|
||||||
|
.priority = (osPriority_t) osPriorityNormal,
|
||||||
|
};
|
||||||
|
#define ZHF 0.30f
|
||||||
|
#define DDF 0.27f
|
||||||
|
int16_t j = 0;
|
||||||
|
float dlteamin = 0.3f;
|
||||||
|
int16_t dlteaminCount = 0;;
|
||||||
|
void SampleRunTask( void *arg)
|
||||||
|
{
|
||||||
|
uint8_t i;
|
||||||
|
int count = 0;
|
||||||
|
if( !HasSample[0] )
|
||||||
|
{
|
||||||
|
vTaskDelete( NULL );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(i = 1; i < 8; i ++)
|
||||||
|
{
|
||||||
|
osDelay(1500);
|
||||||
|
if( HasSample[i] && SetSample[i])
|
||||||
|
{
|
||||||
|
|
||||||
|
CoordinateSet[M3] = i* SingelCupStep;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
|
||||||
|
osDelay(1500);
|
||||||
|
Typestr("%d号杯萃取",i);
|
||||||
|
CoordinateSet[M2] = SetmL *slope1;
|
||||||
|
CuiQuFlag = false;
|
||||||
|
StirrerCtrl(CCRV);
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M2] != CoordinatePosition[M2]) );
|
||||||
|
StirrerCtrl(0);
|
||||||
|
|
||||||
|
osDelay(1500);
|
||||||
|
CoordinateSet[M3] = 0;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
osDelay(1000);
|
||||||
|
SpeedSet[0] = 1000;
|
||||||
|
CoordinateSet[M1] = 1000/slope2;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M1] != CoordinatePosition[M1]) );
|
||||||
|
SpeedSet[0] = 20;
|
||||||
|
|
||||||
|
osDelay(1000);
|
||||||
|
CoordinateSet[M3] = i* SingelCupStep;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
osDelay(1500);
|
||||||
|
BufIndex = 0;
|
||||||
|
UseuL = 0;
|
||||||
|
Typestr("%d号杯中和",i);
|
||||||
|
ADCData =AverageData();
|
||||||
|
Blank = ADCData;
|
||||||
|
CoordinateSet[M1] = 6550000;
|
||||||
|
StirrerCtrl(CCRV);
|
||||||
|
do {
|
||||||
|
ADCData = AverageData();
|
||||||
|
ABSB = (uint16_t)(log10(Blank/ADCData) *1000 ) / 1000.0f;
|
||||||
|
UseuL = (uint32_t)(PluseCount[M1] * slope2 * 1000) / 1000.0f;
|
||||||
|
ABSBuf[BufIndex] = ABSB;
|
||||||
|
UseuLBuf[BufIndex] = UseuL;
|
||||||
|
pfp( "%.3f,%3f\n",ABSB,ADCData);
|
||||||
|
if(ABSB > ZHF)
|
||||||
|
{
|
||||||
|
count ++;
|
||||||
|
if( count >= 5)
|
||||||
|
{
|
||||||
|
dlteamin = ZHF;
|
||||||
|
CoordinateSet[M1] = 0;
|
||||||
|
ZhongHeFlag = false;
|
||||||
|
StirrerCtrl(100);
|
||||||
|
osDelay(1000);
|
||||||
|
j = BufIndex;
|
||||||
|
while( j > 0)
|
||||||
|
{
|
||||||
|
j--;
|
||||||
|
if( fabs(ABSBuf[j] - DDF) < dlteamin )
|
||||||
|
{
|
||||||
|
dlteamin = fabs(ABSBuf[j] - DDF);
|
||||||
|
dlteaminCount = j;
|
||||||
|
}
|
||||||
|
if( ABSBuf[j] < DDF )
|
||||||
|
{
|
||||||
|
UseduL[i] = UseuLBuf[j];
|
||||||
|
j = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( j == 0 )
|
||||||
|
{
|
||||||
|
for( j = 199; j > BufIndex; j -- )
|
||||||
|
{
|
||||||
|
if( fabs(ABSBuf[j] - DDF) < dlteamin )
|
||||||
|
{
|
||||||
|
dlteamin = fabs(ABSBuf[j] - DDF);
|
||||||
|
dlteaminCount = j;
|
||||||
|
}
|
||||||
|
if( ABSBuf[j] < DDF )
|
||||||
|
{
|
||||||
|
UseduL[i] = UseuLBuf[j];
|
||||||
|
j = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( j == BufIndex )
|
||||||
|
{
|
||||||
|
if( fabs(ABSBuf[j] - DDF) < dlteamin )
|
||||||
|
{
|
||||||
|
dlteamin = fabs(ABSBuf[j] - DDF);
|
||||||
|
dlteaminCount = j;
|
||||||
|
}
|
||||||
|
UseduL[i] = UseuLBuf[dlteaminCount];
|
||||||
|
//没有小于0.27的数则取最接近的数
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
count = 0;
|
||||||
|
}
|
||||||
|
if(++BufIndex >=200)
|
||||||
|
BufIndex = 0;
|
||||||
|
osDelay(50);
|
||||||
|
} while( CoordinateSet[M1] != CoordinatePosition[M1] );
|
||||||
|
if(WeigthSample[i]!= 0)
|
||||||
|
{
|
||||||
|
mg_g[i] = (int32_t)((UseduL[i] - uL_Blank)*0.02*56.1/WeigthSample[i] * 1000 ) /1000000.0f;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uL_Blank = UseduL[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Typestr("空位");
|
||||||
|
CoordinateSet[M1] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StirrerCtrl(0);
|
||||||
|
osDelay(1500);
|
||||||
|
CoordinateSet[M3] = 0;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
osDelay(1000);
|
||||||
|
vTaskDelete( NULL );
|
||||||
|
}
|
||||||
|
|
||||||
|
_Bool FillFlag = false;
|
||||||
|
void YouSuanRun( void )
|
||||||
|
{
|
||||||
|
Typestr("进入系统" );
|
||||||
|
MODBUS_Init(1);
|
||||||
|
Debuglog("");
|
||||||
|
slope1 = 82700;
|
||||||
|
slope2 = 0.01166;
|
||||||
|
MotorCTRLInitiate();
|
||||||
|
Debuglog("");
|
||||||
|
|
||||||
|
CoordinateSet[M1] = 0;
|
||||||
|
CoordinateSet[M2] = 0;
|
||||||
|
CoordinateSet[M3] = 0;
|
||||||
|
|
||||||
|
MotorZero_Init(M3);
|
||||||
|
Debuglog("");
|
||||||
|
HasSampleCheck();
|
||||||
|
Debuglog("n");
|
||||||
|
SpeedSet[0] = 20;
|
||||||
|
SpeedSet[0] = 1000;
|
||||||
|
SpeedSet[3] = 5000;
|
||||||
|
ADCData =AverageData();
|
||||||
|
Blank = ADCData;
|
||||||
|
for(;;)
|
||||||
|
{
|
||||||
|
if(FillFlag)
|
||||||
|
{
|
||||||
|
FillFlag = 0;
|
||||||
|
CoordinateSet[M3] = 0;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
|
||||||
|
CoordinateSet[M2] = 1 *slope1;
|
||||||
|
CuiQuFlag = false;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M2] != CoordinatePosition[M2]) );
|
||||||
|
SpeedSet[0] = 1000;
|
||||||
|
CoordinateSet[M1] = 1000 / slope2;
|
||||||
|
do {
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
while( (CoordinateSet[M1] != CoordinatePosition[M1]) );
|
||||||
|
SpeedSet[0] = 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(SampleRunFlag)
|
||||||
|
{
|
||||||
|
SampleRunTaskHandle = osThreadNew( SampleRunTask, NULL, &SampleRunTask_attributes);
|
||||||
|
SampleRunFlag = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if( FindZero[M3] )
|
||||||
|
{
|
||||||
|
Typestr("找零")
|
||||||
|
Debuglog("");
|
||||||
|
MotorZero_Init(M3);
|
||||||
|
Debuglog("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
osDelay(200);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
BIN
MDK-ARM/OutPut/YouSuan.axf
Normal file
BIN
MDK-ARM/OutPut/YouSuan.axf
Normal file
Binary file not shown.
2520
MDK-ARM/OutPut/YouSuan.hex
Normal file
2520
MDK-ARM/OutPut/YouSuan.hex
Normal file
File diff suppressed because it is too large
Load Diff
4291
MDK-ARM/OutPut/YouSuan.map
Normal file
4291
MDK-ARM/OutPut/YouSuan.map
Normal file
File diff suppressed because it is too large
Load Diff
329
YouSuan.ioc
Normal file
329
YouSuan.ioc
Normal file
@ -0,0 +1,329 @@
|
|||||||
|
#MicroXplorer Configuration settings - do not modify
|
||||||
|
ADC1.Channel-1\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-10\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-11\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-12\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-13\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-14\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-15\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-16\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.Channel-2\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-3\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-4\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-5\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-6\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-7\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-8\#ChannelRegularConversion=ADC_CHANNEL_8
|
||||||
|
ADC1.Channel-9\#ChannelRegularConversion=ADC_CHANNEL_9
|
||||||
|
ADC1.ContinuousConvMode=ENABLE
|
||||||
|
ADC1.IPParameters=Rank-1\#ChannelRegularConversion,Channel-1\#ChannelRegularConversion,SamplingTime-1\#ChannelRegularConversion,NbrOfConversionFlag,master,Rank-2\#ChannelRegularConversion,Channel-2\#ChannelRegularConversion,SamplingTime-2\#ChannelRegularConversion,NbrOfConversion,Rank-3\#ChannelRegularConversion,Channel-3\#ChannelRegularConversion,SamplingTime-3\#ChannelRegularConversion,Rank-4\#ChannelRegularConversion,Channel-4\#ChannelRegularConversion,SamplingTime-4\#ChannelRegularConversion,Rank-5\#ChannelRegularConversion,Channel-5\#ChannelRegularConversion,SamplingTime-5\#ChannelRegularConversion,Rank-6\#ChannelRegularConversion,Channel-6\#ChannelRegularConversion,SamplingTime-6\#ChannelRegularConversion,Rank-7\#ChannelRegularConversion,Channel-7\#ChannelRegularConversion,SamplingTime-7\#ChannelRegularConversion,Rank-8\#ChannelRegularConversion,Channel-8\#ChannelRegularConversion,SamplingTime-8\#ChannelRegularConversion,Rank-9\#ChannelRegularConversion,Channel-9\#ChannelRegularConversion,SamplingTime-9\#ChannelRegularConversion,Rank-10\#ChannelRegularConversion,Channel-10\#ChannelRegularConversion,SamplingTime-10\#ChannelRegularConversion,Rank-11\#ChannelRegularConversion,Channel-11\#ChannelRegularConversion,SamplingTime-11\#ChannelRegularConversion,Rank-12\#ChannelRegularConversion,Channel-12\#ChannelRegularConversion,SamplingTime-12\#ChannelRegularConversion,Rank-13\#ChannelRegularConversion,Channel-13\#ChannelRegularConversion,SamplingTime-13\#ChannelRegularConversion,Rank-14\#ChannelRegularConversion,Channel-14\#ChannelRegularConversion,SamplingTime-14\#ChannelRegularConversion,Rank-15\#ChannelRegularConversion,Channel-15\#ChannelRegularConversion,SamplingTime-15\#ChannelRegularConversion,Rank-16\#ChannelRegularConversion,Channel-16\#ChannelRegularConversion,SamplingTime-16\#ChannelRegularConversion,ContinuousConvMode
|
||||||
|
ADC1.NbrOfConversion=16
|
||||||
|
ADC1.NbrOfConversionFlag=1
|
||||||
|
ADC1.Rank-1\#ChannelRegularConversion=1
|
||||||
|
ADC1.Rank-10\#ChannelRegularConversion=10
|
||||||
|
ADC1.Rank-11\#ChannelRegularConversion=11
|
||||||
|
ADC1.Rank-12\#ChannelRegularConversion=12
|
||||||
|
ADC1.Rank-13\#ChannelRegularConversion=13
|
||||||
|
ADC1.Rank-14\#ChannelRegularConversion=14
|
||||||
|
ADC1.Rank-15\#ChannelRegularConversion=15
|
||||||
|
ADC1.Rank-16\#ChannelRegularConversion=16
|
||||||
|
ADC1.Rank-2\#ChannelRegularConversion=2
|
||||||
|
ADC1.Rank-3\#ChannelRegularConversion=3
|
||||||
|
ADC1.Rank-4\#ChannelRegularConversion=4
|
||||||
|
ADC1.Rank-5\#ChannelRegularConversion=5
|
||||||
|
ADC1.Rank-6\#ChannelRegularConversion=6
|
||||||
|
ADC1.Rank-7\#ChannelRegularConversion=7
|
||||||
|
ADC1.Rank-8\#ChannelRegularConversion=8
|
||||||
|
ADC1.Rank-9\#ChannelRegularConversion=9
|
||||||
|
ADC1.SamplingTime-1\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-10\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-11\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-12\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-13\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-14\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-15\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-16\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-2\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-3\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-4\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-5\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-6\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-7\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-8\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.SamplingTime-9\#ChannelRegularConversion=ADC_SAMPLETIME_239CYCLES_5
|
||||||
|
ADC1.master=1
|
||||||
|
CAD.formats=
|
||||||
|
CAD.pinconfig=
|
||||||
|
CAD.provider=
|
||||||
|
Dma.ADC1.0.Direction=DMA_PERIPH_TO_MEMORY
|
||||||
|
Dma.ADC1.0.Instance=DMA1_Channel1
|
||||||
|
Dma.ADC1.0.MemDataAlignment=DMA_MDATAALIGN_HALFWORD
|
||||||
|
Dma.ADC1.0.MemInc=DMA_MINC_ENABLE
|
||||||
|
Dma.ADC1.0.Mode=DMA_CIRCULAR
|
||||||
|
Dma.ADC1.0.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD
|
||||||
|
Dma.ADC1.0.PeriphInc=DMA_PINC_DISABLE
|
||||||
|
Dma.ADC1.0.Priority=DMA_PRIORITY_LOW
|
||||||
|
Dma.ADC1.0.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority
|
||||||
|
Dma.Request0=ADC1
|
||||||
|
Dma.RequestsNb=1
|
||||||
|
FREERTOS.IPParameters=Tasks01,configTOTAL_HEAP_SIZE
|
||||||
|
FREERTOS.Tasks01=defaultTask,24,128,StartDefaultTask,Default,NULL,Dynamic,NULL,NULL
|
||||||
|
FREERTOS.configTOTAL_HEAP_SIZE=7680
|
||||||
|
File.Version=6
|
||||||
|
GPIO.groupedBy=Group By Peripherals
|
||||||
|
KeepUserPlacement=false
|
||||||
|
Mcu.CPN=STM32F103RCT6
|
||||||
|
Mcu.Family=STM32F1
|
||||||
|
Mcu.IP0=ADC1
|
||||||
|
Mcu.IP1=DMA
|
||||||
|
Mcu.IP10=USART1
|
||||||
|
Mcu.IP11=USART3
|
||||||
|
Mcu.IP2=FREERTOS
|
||||||
|
Mcu.IP3=NVIC
|
||||||
|
Mcu.IP4=RCC
|
||||||
|
Mcu.IP5=SYS
|
||||||
|
Mcu.IP6=TIM2
|
||||||
|
Mcu.IP7=TIM3
|
||||||
|
Mcu.IP8=TIM5
|
||||||
|
Mcu.IP9=TIM8
|
||||||
|
Mcu.IPNb=12
|
||||||
|
Mcu.Name=STM32F103R(C-D-E)Tx
|
||||||
|
Mcu.Package=LQFP64
|
||||||
|
Mcu.Pin0=PD0-OSC_IN
|
||||||
|
Mcu.Pin1=PD1-OSC_OUT
|
||||||
|
Mcu.Pin10=PA6
|
||||||
|
Mcu.Pin11=PA7
|
||||||
|
Mcu.Pin12=PC4
|
||||||
|
Mcu.Pin13=PC5
|
||||||
|
Mcu.Pin14=PB0
|
||||||
|
Mcu.Pin15=PB1
|
||||||
|
Mcu.Pin16=PB10
|
||||||
|
Mcu.Pin17=PB11
|
||||||
|
Mcu.Pin18=PB12
|
||||||
|
Mcu.Pin19=PC6
|
||||||
|
Mcu.Pin2=PC0
|
||||||
|
Mcu.Pin20=PA8
|
||||||
|
Mcu.Pin21=PA9
|
||||||
|
Mcu.Pin22=PA10
|
||||||
|
Mcu.Pin23=PA13
|
||||||
|
Mcu.Pin24=PA14
|
||||||
|
Mcu.Pin25=PC10
|
||||||
|
Mcu.Pin26=PC11
|
||||||
|
Mcu.Pin27=VP_FREERTOS_VS_CMSIS_V2
|
||||||
|
Mcu.Pin28=VP_SYS_VS_tim1
|
||||||
|
Mcu.Pin29=VP_TIM2_VS_ClockSourceINT
|
||||||
|
Mcu.Pin3=PC1
|
||||||
|
Mcu.Pin30=VP_TIM3_VS_ClockSourceINT
|
||||||
|
Mcu.Pin31=VP_TIM8_VS_ClockSourceINT
|
||||||
|
Mcu.Pin4=PC2
|
||||||
|
Mcu.Pin5=PA0-WKUP
|
||||||
|
Mcu.Pin6=PA1
|
||||||
|
Mcu.Pin7=PA2
|
||||||
|
Mcu.Pin8=PA3
|
||||||
|
Mcu.Pin9=PA5
|
||||||
|
Mcu.PinsNb=32
|
||||||
|
Mcu.ThirdPartyNb=0
|
||||||
|
Mcu.UserConstants=
|
||||||
|
Mcu.UserName=STM32F103RCTx
|
||||||
|
MxCube.Version=6.15.0
|
||||||
|
MxDb.Version=DB.6.0.150
|
||||||
|
NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
NVIC.DMA1_Channel1_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
|
||||||
|
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
NVIC.EXTI0_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||||
|
NVIC.ForceEnableDMAVector=true
|
||||||
|
NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:false\:false
|
||||||
|
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
|
||||||
|
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false\:false
|
||||||
|
NVIC.SavedPendsvIrqHandlerGenerated=true
|
||||||
|
NVIC.SavedSvcallIrqHandlerGenerated=true
|
||||||
|
NVIC.SavedSystickIrqHandlerGenerated=true
|
||||||
|
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:true\:false
|
||||||
|
NVIC.TIM1_UP_IRQn=true\:15\:0\:false\:false\:true\:false\:false\:true\:true
|
||||||
|
NVIC.TIM2_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||||
|
NVIC.TIM3_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||||
|
NVIC.TIM5_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
|
||||||
|
NVIC.TimeBase=TIM1_UP_IRQn
|
||||||
|
NVIC.TimeBaseIP=TIM1
|
||||||
|
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
|
||||||
|
PA0-WKUP.Locked=true
|
||||||
|
PA0-WKUP.Signal=GPIO_Output
|
||||||
|
PA1.Locked=true
|
||||||
|
PA1.Signal=S_TIM2_CH2
|
||||||
|
PA10.Locked=true
|
||||||
|
PA10.Mode=Asynchronous
|
||||||
|
PA10.Signal=USART1_RX
|
||||||
|
PA13.Mode=Serial_Wire
|
||||||
|
PA13.Signal=SYS_JTMS-SWDIO
|
||||||
|
PA14.Mode=Serial_Wire
|
||||||
|
PA14.Signal=SYS_JTCK-SWCLK
|
||||||
|
PA2.Locked=true
|
||||||
|
PA2.Signal=S_TIM5_CH3
|
||||||
|
PA3.Locked=true
|
||||||
|
PA3.Signal=GPIO_Output
|
||||||
|
PA5.Locked=true
|
||||||
|
PA5.Signal=GPIO_Output
|
||||||
|
PA6.Locked=true
|
||||||
|
PA6.Signal=S_TIM3_CH1
|
||||||
|
PA7.Locked=true
|
||||||
|
PA7.Signal=GPIO_Output
|
||||||
|
PA8.Locked=true
|
||||||
|
PA8.Signal=GPIO_Output
|
||||||
|
PA9.Locked=true
|
||||||
|
PA9.Mode=Asynchronous
|
||||||
|
PA9.Signal=USART1_TX
|
||||||
|
PB0.Locked=true
|
||||||
|
PB0.Signal=ADCx_IN8
|
||||||
|
PB1.Locked=true
|
||||||
|
PB1.Signal=ADCx_IN9
|
||||||
|
PB10.Locked=true
|
||||||
|
PB10.Signal=GPIO_Output
|
||||||
|
PB11.Locked=true
|
||||||
|
PB11.Signal=GPIO_Output
|
||||||
|
PB12.Locked=true
|
||||||
|
PB12.Signal=GPIO_Output
|
||||||
|
PC0.GPIOParameters=GPIO_ModeDefaultEXTI
|
||||||
|
PC0.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
|
||||||
|
PC0.Locked=true
|
||||||
|
PC0.Signal=GPXTI0
|
||||||
|
PC1.Locked=true
|
||||||
|
PC1.Signal=GPIO_Input
|
||||||
|
PC10.Locked=true
|
||||||
|
PC10.Mode=Asynchronous
|
||||||
|
PC10.Signal=USART3_TX
|
||||||
|
PC11.Locked=true
|
||||||
|
PC11.Mode=Asynchronous
|
||||||
|
PC11.Signal=USART3_RX
|
||||||
|
PC2.Locked=true
|
||||||
|
PC2.Signal=GPIO_Input
|
||||||
|
PC4.Locked=true
|
||||||
|
PC4.Signal=GPIO_Output
|
||||||
|
PC5.Locked=true
|
||||||
|
PC5.Signal=GPIO_Output
|
||||||
|
PC6.Locked=true
|
||||||
|
PC6.Signal=S_TIM8_CH1
|
||||||
|
PD0-OSC_IN.Mode=HSE-External-Oscillator
|
||||||
|
PD0-OSC_IN.Signal=RCC_OSC_IN
|
||||||
|
PD1-OSC_OUT.Mode=HSE-External-Oscillator
|
||||||
|
PD1-OSC_OUT.Signal=RCC_OSC_OUT
|
||||||
|
PinOutPanel.RotationAngle=0
|
||||||
|
ProjectManager.AskForMigrate=true
|
||||||
|
ProjectManager.BackupPrevious=false
|
||||||
|
ProjectManager.CompilerLinker=GCC
|
||||||
|
ProjectManager.CompilerOptimize=6
|
||||||
|
ProjectManager.ComputerToolchain=false
|
||||||
|
ProjectManager.CoupleFile=false
|
||||||
|
ProjectManager.CustomerFirmwarePackage=
|
||||||
|
ProjectManager.DefaultFWLocation=true
|
||||||
|
ProjectManager.DeletePrevious=true
|
||||||
|
ProjectManager.DeviceId=STM32F103RCTx
|
||||||
|
ProjectManager.FirmwarePackage=STM32Cube FW_F1 V1.8.6
|
||||||
|
ProjectManager.FreePins=false
|
||||||
|
ProjectManager.HalAssertFull=false
|
||||||
|
ProjectManager.HeapSize=0x200
|
||||||
|
ProjectManager.KeepUserCode=true
|
||||||
|
ProjectManager.LastFirmware=true
|
||||||
|
ProjectManager.LibraryCopy=1
|
||||||
|
ProjectManager.MainLocation=Core/Src
|
||||||
|
ProjectManager.NoMain=false
|
||||||
|
ProjectManager.PreviousToolchain=
|
||||||
|
ProjectManager.ProjectBuild=false
|
||||||
|
ProjectManager.ProjectFileName=YouSuan.ioc
|
||||||
|
ProjectManager.ProjectName=YouSuan
|
||||||
|
ProjectManager.ProjectStructure=
|
||||||
|
ProjectManager.RegisterCallBack=
|
||||||
|
ProjectManager.StackSize=0x400
|
||||||
|
ProjectManager.TargetToolchain=MDK-ARM V5.32
|
||||||
|
ProjectManager.ToolChainLocation=
|
||||||
|
ProjectManager.UAScriptAfterPath=
|
||||||
|
ProjectManager.UAScriptBeforePath=
|
||||||
|
ProjectManager.UnderRoot=false
|
||||||
|
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_ADC1_Init-ADC1-false-HAL-true,5-MX_TIM3_Init-TIM3-false-HAL-true,6-MX_TIM2_Init-TIM2-false-HAL-true,7-MX_TIM5_Init-TIM5-false-HAL-true,8-MX_TIM8_Init-TIM8-false-HAL-true,9-MX_USART1_UART_Init-USART1-false-HAL-true,10-MX_USART3_UART_Init-USART3-false-HAL-true
|
||||||
|
RCC.ADCFreqValue=12000000
|
||||||
|
RCC.ADCPresc=RCC_ADCPCLK2_DIV6
|
||||||
|
RCC.AHBFreq_Value=72000000
|
||||||
|
RCC.APB1CLKDivider=RCC_HCLK_DIV2
|
||||||
|
RCC.APB1Freq_Value=36000000
|
||||||
|
RCC.APB1TimFreq_Value=72000000
|
||||||
|
RCC.APB2Freq_Value=72000000
|
||||||
|
RCC.APB2TimFreq_Value=72000000
|
||||||
|
RCC.FCLKCortexFreq_Value=72000000
|
||||||
|
RCC.FamilyName=M
|
||||||
|
RCC.HCLKFreq_Value=72000000
|
||||||
|
RCC.I2S2Freq_Value=72000000
|
||||||
|
RCC.I2S3Freq_Value=72000000
|
||||||
|
RCC.IPParameters=ADCFreqValue,ADCPresc,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,I2S2Freq_Value,I2S3Freq_Value,MCOFreq_Value,PLLCLKFreq_Value,PLLMCOFreq_Value,PLLMUL,PLLSourceVirtual,SDIOFreq_Value,SDIOHCLKDiv2FreqValue,SYSCLKFreq_VALUE,SYSCLKSource,TimSysFreq_Value,USBFreq_Value,VCOOutput2Freq_Value
|
||||||
|
RCC.MCOFreq_Value=72000000
|
||||||
|
RCC.PLLCLKFreq_Value=72000000
|
||||||
|
RCC.PLLMCOFreq_Value=36000000
|
||||||
|
RCC.PLLMUL=RCC_PLL_MUL9
|
||||||
|
RCC.PLLSourceVirtual=RCC_PLLSOURCE_HSE
|
||||||
|
RCC.SDIOFreq_Value=72000000
|
||||||
|
RCC.SDIOHCLKDiv2FreqValue=36000000
|
||||||
|
RCC.SYSCLKFreq_VALUE=72000000
|
||||||
|
RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLK
|
||||||
|
RCC.TimSysFreq_Value=72000000
|
||||||
|
RCC.USBFreq_Value=72000000
|
||||||
|
RCC.VCOOutput2Freq_Value=8000000
|
||||||
|
SH.ADCx_IN8.0=ADC1_IN8,IN8
|
||||||
|
SH.ADCx_IN8.ConfNb=1
|
||||||
|
SH.ADCx_IN9.0=ADC1_IN9,IN9
|
||||||
|
SH.ADCx_IN9.ConfNb=1
|
||||||
|
SH.GPXTI0.0=GPIO_EXTI0
|
||||||
|
SH.GPXTI0.ConfNb=1
|
||||||
|
SH.S_TIM2_CH2.0=TIM2_CH2,PWM Generation2 CH2
|
||||||
|
SH.S_TIM2_CH2.ConfNb=1
|
||||||
|
SH.S_TIM3_CH1.0=TIM3_CH1,PWM Generation1 CH1
|
||||||
|
SH.S_TIM3_CH1.ConfNb=1
|
||||||
|
SH.S_TIM5_CH3.0=TIM5_CH3,PWM Generation3 CH3
|
||||||
|
SH.S_TIM5_CH3.ConfNb=1
|
||||||
|
SH.S_TIM8_CH1.0=TIM8_CH1,PWM Generation1 CH1
|
||||||
|
SH.S_TIM8_CH1.ConfNb=1
|
||||||
|
TIM2.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||||
|
TIM2.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2
|
||||||
|
TIM2.IPParameters=Channel-PWM Generation2 CH2,Prescaler,Period,AutoReloadPreload,Pulse-PWM Generation2 CH2,TIM_MasterOutputTrigger
|
||||||
|
TIM2.Period=999
|
||||||
|
TIM2.Prescaler=72
|
||||||
|
TIM2.Pulse-PWM\ Generation2\ CH2=499
|
||||||
|
TIM2.TIM_MasterOutputTrigger=TIM_TRGO_UPDATE
|
||||||
|
TIM3.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||||
|
TIM3.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
|
||||||
|
TIM3.IPParameters=Prescaler,Period,AutoReloadPreload,TIM_MasterOutputTrigger,Channel-PWM Generation1 CH1,Pulse-PWM Generation1 CH1
|
||||||
|
TIM3.Period=999
|
||||||
|
TIM3.Prescaler=72
|
||||||
|
TIM3.Pulse-PWM\ Generation1\ CH1=499
|
||||||
|
TIM3.TIM_MasterOutputTrigger=TIM_TRGO_UPDATE
|
||||||
|
TIM5.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||||
|
TIM5.Channel-PWM\ Generation3\ CH3=TIM_CHANNEL_3
|
||||||
|
TIM5.IPParameters=Channel-PWM Generation3 CH3,Prescaler,Period,TIM_MasterOutputTrigger,AutoReloadPreload,Pulse-PWM Generation3 CH3
|
||||||
|
TIM5.Period=999
|
||||||
|
TIM5.Prescaler=72
|
||||||
|
TIM5.Pulse-PWM\ Generation3\ CH3=499
|
||||||
|
TIM5.TIM_MasterOutputTrigger=TIM_TRGO_UPDATE
|
||||||
|
TIM8.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
|
||||||
|
TIM8.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
|
||||||
|
TIM8.IPParameters=Channel-PWM Generation1 CH1,Prescaler,Period,AutoReloadPreload,Pulse-PWM Generation1 CH1,TIM_MasterOutputTrigger
|
||||||
|
TIM8.Period=999
|
||||||
|
TIM8.Prescaler=72
|
||||||
|
TIM8.Pulse-PWM\ Generation1\ CH1=0
|
||||||
|
TIM8.TIM_MasterOutputTrigger=TIM_TRGO_UPDATE
|
||||||
|
USART1.IPParameters=VirtualMode
|
||||||
|
USART1.VirtualMode=VM_ASYNC
|
||||||
|
USART3.IPParameters=VirtualMode,Mode
|
||||||
|
USART3.Mode=MODE_TX
|
||||||
|
USART3.VirtualMode=VM_ASYNC
|
||||||
|
VP_FREERTOS_VS_CMSIS_V2.Mode=CMSIS_V2
|
||||||
|
VP_FREERTOS_VS_CMSIS_V2.Signal=FREERTOS_VS_CMSIS_V2
|
||||||
|
VP_SYS_VS_tim1.Mode=TIM1
|
||||||
|
VP_SYS_VS_tim1.Signal=SYS_VS_tim1
|
||||||
|
VP_TIM2_VS_ClockSourceINT.Mode=Internal
|
||||||
|
VP_TIM2_VS_ClockSourceINT.Signal=TIM2_VS_ClockSourceINT
|
||||||
|
VP_TIM3_VS_ClockSourceINT.Mode=Internal
|
||||||
|
VP_TIM3_VS_ClockSourceINT.Signal=TIM3_VS_ClockSourceINT
|
||||||
|
VP_TIM8_VS_ClockSourceINT.Mode=Internal
|
||||||
|
VP_TIM8_VS_ClockSourceINT.Signal=TIM8_VS_ClockSourceINT
|
||||||
|
board=custom
|
||||||
|
rtos.0.ip=FREERTOS
|
||||||
Loading…
Reference in New Issue
Block a user