Merge pull request #56 from mysterywolf/master

format code
This commit is contained in:
朱天龙 (Armink) 2021-04-11 10:38:23 +08:00 committed by GitHub
commit 39dfef0eee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 696 additions and 696 deletions

View File

@ -2,7 +2,7 @@
// Filename : delay_config.h // Filename : delay_config.h
// Version : V1.00 // Version : V1.00
// Programmer(s) : Liuqiuhu // Programmer(s) : Liuqiuhu
// funcion : This file is used to configure the delay time // funcion : This file is used to configure the delay time
/*********************************************************************************************************/ /*********************************************************************************************************/
#ifndef __DELAY_CONF_H__ #ifndef __DELAY_CONF_H__
#define __DELAY_CONF_H__ #define __DELAY_CONF_H__
@ -10,34 +10,34 @@
#include"rtconfig.h" #include"rtconfig.h"
#if RT_TICK_PER_SECOND == 1 #if RT_TICK_PER_SECOND == 1
#define DELAY_1S (RT_TICK_PER_SECOND) #define DELAY_1S (RT_TICK_PER_SECOND)
#define DELAY_S(X) (X*DELAY_1S) #define DELAY_S(X) (X*DELAY_1S)
#elif RT_TICK_PER_SECOND == 10 #elif RT_TICK_PER_SECOND == 10
#define DELAY_100MS(X) (X) #define DELAY_100MS(X) (X)
#define DELAY_S(X) (X*10) #define DELAY_S(X) (X*10)
#elif RT_TICK_PER_SECOND == 100 #elif RT_TICK_PER_SECOND == 100
#define DELAY_10MS(X) (X) #define DELAY_10MS(X) (X)
#define DELAY_100MS(X) (X*10) #define DELAY_100MS(X) (X*10)
#define DELAY_S(X) (X*100) #define DELAY_S(X) (X*100)
#elif (RT_TICK_PER_SECOND == 1000) #elif (RT_TICK_PER_SECOND == 1000)
#define DELAY_1MS (RT_TICK_PER_SECOND/1000) #define DELAY_1MS (RT_TICK_PER_SECOND/1000)
#define DELAY_MS(X) (X*DELAY_1MS) #define DELAY_MS(X) (X*DELAY_1MS)
#define DELAY_S(X) (X*1000*DELAY_1MS) #define DELAY_S(X) (X*1000*DELAY_1MS)
#elif (RT_TICK_PER_SECOND == 10000) #elif (RT_TICK_PER_SECOND == 10000)
#define DELAY_100US(X) (X*RT_TICK_PER_SECOND/10000) #define DELAY_100US(X) (X*RT_TICK_PER_SECOND/10000)
#define DELAY_1MS (RT_TICK_PER_SECOND/1000) #define DELAY_1MS (RT_TICK_PER_SECOND/1000)
#define DELAY_MS(X) (X*DELAY_1MS) #define DELAY_MS(X) (X*DELAY_1MS)
#define DELAY_S(X) (X*1000*DELAY_1MS) #define DELAY_S(X) (X*1000*DELAY_1MS)
#endif #endif
#define DELAY_SYS_RUN_LED DELAY_MS(500) #define DELAY_SYS_RUN_LED DELAY_MS(500)
#define DELAY_SYS_SLEEP_LED DELAY_MS(1000) #define DELAY_SYS_SLEEP_LED DELAY_MS(1000)
#endif #endif

View File

@ -3,16 +3,16 @@
#define __RTTHREAD_CFG_H__ #define __RTTHREAD_CFG_H__
/* RT_NAME_MAX*/ /* RT_NAME_MAX*/
#define RT_NAME_MAX 24 #define RT_NAME_MAX 24
/* RT_ALIGN_SIZE*/ /* RT_ALIGN_SIZE*/
#define RT_ALIGN_SIZE 8 #define RT_ALIGN_SIZE 8
/* PRIORITY_MAX */ /* PRIORITY_MAX */
#define RT_THREAD_PRIORITY_MAX 32 #define RT_THREAD_PRIORITY_MAX 32
/* Tick per Second */ /* Tick per Second */
#define RT_TICK_PER_SECOND 10000 //0.1ms #define RT_TICK_PER_SECOND 10000 //0.1ms
/* SECTION: RT_DEBUG */ /* SECTION: RT_DEBUG */
/* Thread Debug */ /* Thread Debug */
@ -26,9 +26,9 @@
/* Using Software Timer */ /* Using Software Timer */
#define RT_USING_TIMER_SOFT #define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4 #define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512 #define RT_TIMER_THREAD_STACK_SIZE 512
#define RT_TIMER_TICK_PER_SECOND 1000 #define RT_TIMER_TICK_PER_SECOND 1000
/* SECTION: IPC */ /* SECTION: IPC */
/* Using Semaphore*/ /* Using Semaphore*/
@ -69,7 +69,7 @@
/* SECTION: Console options */ /* SECTION: Console options */
/* the buffer size of console*/ /* the buffer size of console*/
#define RT_CONSOLEBUF_SIZE 128 #define RT_CONSOLEBUF_SIZE 128
/* SECTION: finsh, a C-Express shell */ /* SECTION: finsh, a C-Express shell */
//#define RT_USING_FINSH //#define RT_USING_FINSH

View File

@ -2,18 +2,18 @@
int main(void) int main(void)
{ {
rt_uint32_t UNUSED level; rt_uint32_t UNUSED level;
/* disable interrupt first */ /* disable interrupt first */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
/* init system setting */ /* init system setting */
SystemInit(); SystemInit();
/* startup RT-Thread RTOS */ /* startup RT-Thread RTOS */
rtthread_startup(); rtthread_startup();
return 0; return 0;
} }

View File

@ -8,15 +8,15 @@ extern int Image$$RW_IRAM1$$ZI$$Limit;
extern int __bss_end; extern int __bss_end;
#endif #endif
uint8_t CpuUsageMajor, CpuUsageMinor; //CPU使用率 uint8_t CpuUsageMajor, CpuUsageMinor; //CPU使用率
USHORT usModbusUserData[MB_PDU_SIZE_MAX]; USHORT usModbusUserData[MB_PDU_SIZE_MAX];
UCHAR ucModbusUserData[MB_PDU_SIZE_MAX]; UCHAR ucModbusUserData[MB_PDU_SIZE_MAX];
//====================操作系统各线程优先级================================== //====================操作系统各线程优先级==================================
#define thread_SysMonitor_Prio 11 #define thread_SysMonitor_Prio 11
#define thread_ModbusSlavePoll_Prio 10 #define thread_ModbusSlavePoll_Prio 10
#define thread_ModbusMasterPoll_Prio 9 #define thread_ModbusMasterPoll_Prio 9
ALIGN(RT_ALIGN_SIZE) ALIGN(RT_ALIGN_SIZE)
//====================操作系统各线程堆栈==================================== //====================操作系统各线程堆栈====================================
static rt_uint8_t thread_SysMonitor_stack[256]; static rt_uint8_t thread_SysMonitor_stack[256];
static rt_uint8_t thread_ModbusSlavePoll_stack[512]; static rt_uint8_t thread_ModbusSlavePoll_stack[512];
static rt_uint8_t thread_ModbusMasterPoll_stack[512]; static rt_uint8_t thread_ModbusMasterPoll_stack[512];
@ -25,170 +25,170 @@ struct rt_thread thread_SysMonitor;
struct rt_thread thread_ModbusSlavePoll; struct rt_thread thread_ModbusSlavePoll;
struct rt_thread thread_ModbusMasterPoll; struct rt_thread thread_ModbusMasterPoll;
//***************************系统监控线程*************************** //***************************系统监控线程***************************
//函数定义: void thread_entry_SysRunLed(void* parameter) //函数定义: void thread_entry_SysRunLed(void* parameter)
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注EditorArmink 2013-08-02 Company: BXXJS //备 注EditorArmink 2013-08-02 Company: BXXJS
//****************************************************************** //******************************************************************
void thread_entry_SysMonitor(void* parameter) void thread_entry_SysMonitor(void* parameter)
{ {
eMBMasterReqErrCode errorCode = MB_MRE_NO_ERR; eMBMasterReqErrCode errorCode = MB_MRE_NO_ERR;
uint16_t errorCount = 0; uint16_t errorCount = 0;
while (1) while (1)
{ {
cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor); cpu_usage_get(&CpuUsageMajor, &CpuUsageMinor);
usSRegHoldBuf[S_HD_CPU_USAGE_MAJOR] = CpuUsageMajor; usSRegHoldBuf[S_HD_CPU_USAGE_MAJOR] = CpuUsageMajor;
usSRegHoldBuf[S_HD_CPU_USAGE_MINOR] = CpuUsageMinor; usSRegHoldBuf[S_HD_CPU_USAGE_MINOR] = CpuUsageMinor;
LED_LED1_ON; LED_LED1_ON;
LED_LED2_ON; LED_LED2_ON;
rt_thread_delay(DELAY_SYS_RUN_LED); rt_thread_delay(DELAY_SYS_RUN_LED);
LED_LED1_OFF; LED_LED1_OFF;
LED_LED2_OFF; LED_LED2_OFF;
rt_thread_delay(DELAY_SYS_RUN_LED); rt_thread_delay(DELAY_SYS_RUN_LED);
IWDG_Feed(); //feed the dog IWDG_Feed(); //feed the dog
//Test Modbus Master //Test Modbus Master
usModbusUserData[0] = (USHORT)(rt_tick_get()/10); usModbusUserData[0] = (USHORT)(rt_tick_get()/10);
usModbusUserData[1] = (USHORT)(rt_tick_get()%10); usModbusUserData[1] = (USHORT)(rt_tick_get()%10);
ucModbusUserData[0] = 0x1F; ucModbusUserData[0] = 0x1F;
// errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER); // errorCode = eMBMasterReqReadDiscreteInputs(1,3,8,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER); // errorCode = eMBMasterReqWriteMultipleCoils(1,3,5,ucModbusUserData,RT_WAITING_FOREVER);
errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER); errorCode = eMBMasterReqWriteCoil(1,8,0xFF00,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER); // errorCode = eMBMasterReqReadCoils(1,3,8,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER); // errorCode = eMBMasterReqReadInputRegister(1,3,2,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER); // errorCode = eMBMasterReqWriteHoldingRegister(1,3,usModbusUserData[0],RT_WAITING_FOREVER);
// errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER); // errorCode = eMBMasterReqWriteMultipleHoldingRegister(1,3,2,usModbusUserData,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER); // errorCode = eMBMasterReqReadHoldingRegister(1,3,2,RT_WAITING_FOREVER);
// errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER); // errorCode = eMBMasterReqReadWriteMultipleHoldingRegister(1,3,2,usModbusUserData,5,2,RT_WAITING_FOREVER);
//记录出错次数 //记录出错次数
if (errorCode != MB_MRE_NO_ERR) { if (errorCode != MB_MRE_NO_ERR) {
errorCount++; errorCount++;
} }
} }
} }
//************************ Modbus从机轮训线程*************************** //************************ Modbus从机轮训线程***************************
//函数定义: void thread_entry_ModbusSlavePoll(void* parameter) //函数定义: void thread_entry_ModbusSlavePoll(void* parameter)
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注EditorArmink 2013-08-02 Company: BXXJS //备 注EditorArmink 2013-08-02 Company: BXXJS
//****************************************************************** //******************************************************************
void thread_entry_ModbusSlavePoll(void* parameter) void thread_entry_ModbusSlavePoll(void* parameter)
{ {
eMBInit(MB_RTU, 0x01, 1, 115200, MB_PAR_EVEN); eMBInit(MB_RTU, 0x01, 1, 115200, MB_PAR_EVEN);
eMBEnable(); eMBEnable();
while (1) while (1)
{ {
eMBPoll(); eMBPoll();
} }
} }
//************************ Modbus主机轮训线程*************************** //************************ Modbus主机轮训线程***************************
//函数定义: void thread_entry_ModbusMasterPoll(void* parameter) //函数定义: void thread_entry_ModbusMasterPoll(void* parameter)
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注EditorArmink 2013-08-28 Company: BXXJS //备 注EditorArmink 2013-08-28 Company: BXXJS
//****************************************************************** //******************************************************************
void thread_entry_ModbusMasterPoll(void* parameter) void thread_entry_ModbusMasterPoll(void* parameter)
{ {
eMBMasterInit(MB_RTU, 2, 115200, MB_PAR_EVEN); eMBMasterInit(MB_RTU, 2, 115200, MB_PAR_EVEN);
eMBMasterEnable(); eMBMasterEnable();
while (1) while (1)
{ {
eMBMasterPoll(); eMBMasterPoll();
} }
} }
//**********************系统初始化函数******************************** //**********************系统初始化函数********************************
//函数定义: int rt_application_init(void) //函数定义: int rt_application_init(void)
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注EditorLiuqiuhu 2013-1-31 Company: BXXJS //备 注EditorLiuqiuhu 2013-1-31 Company: BXXJS
//******************************************************************** //********************************************************************
int rt_application_init(void) int rt_application_init(void)
{ {
rt_thread_init(&thread_SysMonitor, "SysMonitor", thread_entry_SysMonitor, rt_thread_init(&thread_SysMonitor, "SysMonitor", thread_entry_SysMonitor,
RT_NULL, thread_SysMonitor_stack, sizeof(thread_SysMonitor_stack), RT_NULL, thread_SysMonitor_stack, sizeof(thread_SysMonitor_stack),
thread_SysMonitor_Prio, 5); thread_SysMonitor_Prio, 5);
rt_thread_startup(&thread_SysMonitor); rt_thread_startup(&thread_SysMonitor);
rt_thread_init(&thread_ModbusSlavePoll, "MBSlavePoll", rt_thread_init(&thread_ModbusSlavePoll, "MBSlavePoll",
thread_entry_ModbusSlavePoll, RT_NULL, thread_ModbusSlavePoll_stack, thread_entry_ModbusSlavePoll, RT_NULL, thread_ModbusSlavePoll_stack,
sizeof(thread_ModbusSlavePoll_stack), thread_ModbusSlavePoll_Prio, sizeof(thread_ModbusSlavePoll_stack), thread_ModbusSlavePoll_Prio,
5); 5);
rt_thread_startup(&thread_ModbusSlavePoll); rt_thread_startup(&thread_ModbusSlavePoll);
rt_thread_init(&thread_ModbusMasterPoll, "MBMasterPoll", rt_thread_init(&thread_ModbusMasterPoll, "MBMasterPoll",
thread_entry_ModbusMasterPoll, RT_NULL, thread_ModbusMasterPoll_stack, thread_entry_ModbusMasterPoll, RT_NULL, thread_ModbusMasterPoll_stack,
sizeof(thread_ModbusMasterPoll_stack), thread_ModbusMasterPoll_Prio, sizeof(thread_ModbusMasterPoll_stack), thread_ModbusMasterPoll_Prio,
5); 5);
rt_thread_startup(&thread_ModbusMasterPoll); rt_thread_startup(&thread_ModbusMasterPoll);
return 0; return 0;
} }
//**************************初始化RT-Thread函数************************************* //**************************初始化RT-Thread函数*************************************
//函数定义: void rtthread_startup(void) //函数定义: void rtthread_startup(void)
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注EditorArmink 2011-04-04 Company: BXXJS //备 注EditorArmink 2011-04-04 Company: BXXJS
//********************************************************************************** //**********************************************************************************
void rtthread_startup(void) void rtthread_startup(void)
{ {
/* init board */ /* init board */
rt_hw_board_init(); rt_hw_board_init();
/* show version */ /* show version */
rt_show_version(); rt_show_version();
/* init tick */ /* init tick */
rt_system_tick_init(); rt_system_tick_init();
/* init kernel object */ /* init kernel object */
rt_system_object_init(); rt_system_object_init();
/* init timer system */ /* init timer system */
rt_system_timer_init(); rt_system_timer_init();
#ifdef RT_USING_HEAP #ifdef RT_USING_HEAP
#ifdef __CC_ARM #ifdef __CC_ARM
rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END); rt_system_heap_init((void*)&Image$$RW_IRAM1$$ZI$$Limit, (void*)STM32_SRAM_END);
#elif __ICCARM__ #elif __ICCARM__
rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END); rt_system_heap_init(__segment_end("HEAP"), (void*)STM32_SRAM_END);
#else #else
/* init memory system */ /* init memory system */
rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END); rt_system_heap_init((void*)&__bss_end, (void*)STM32_SRAM_END);
#endif #endif
#endif #endif
/* init scheduler system */ /* init scheduler system */
rt_system_scheduler_init(); rt_system_scheduler_init();
/* init all device */ /* init all device */
rt_device_init_all(); rt_device_init_all();
/* init application */ /* init application */
rt_application_init(); rt_application_init();
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
/* init finsh */ /* init finsh */
finsh_system_init(); finsh_system_init();
finsh_set_device("uart1"); finsh_set_device("uart1");
#endif #endif
/* init timer thread */ /* init timer thread */
rt_system_timer_thread_init(); rt_system_timer_thread_init();
/* init idle thread */ /* init idle thread */
rt_thread_idle_init(); rt_thread_idle_init();
/* Add CPU usage to system */ /* Add CPU usage to system */
cpu_usage_init(); cpu_usage_init();
/* start scheduler */ /* start scheduler */
rt_system_scheduler_start(); rt_system_scheduler_start();
/* never reach here */ /* never reach here */
return; return;
} }

View File

@ -26,23 +26,23 @@
*/ */
/* board configuration */ /* board configuration */
// <o> SDCard Driver <1=>SDIO sdcard <0=>SPI MMC card // <o> SDCard Driver <1=>SDIO sdcard <0=>SPI MMC card
// <i>Default: 1 // <i>Default: 1
#define STM32_USE_SDIO 0 #define STM32_USE_SDIO 0
/* whether use board external SRAM memory */ /* whether use board external SRAM memory */
// <e>Use external SRAM memory on the board // <e>Use external SRAM memory on the board
// <i>Enable External SRAM memory // <i>Enable External SRAM memory
#define STM32_EXT_SRAM 0 #define STM32_EXT_SRAM 0
// <o>Begin Address of External SRAM // <o>Begin Address of External SRAM
// <i>Default: 0x68000000 // <i>Default: 0x68000000
#define STM32_EXT_SRAM_BEGIN 0x68000000 /* the begining address of external SRAM */ #define STM32_EXT_SRAM_BEGIN 0x68000000 /* the begining address of external SRAM */
// <o>End Address of External SRAM // <o>End Address of External SRAM
// <i>Default: 0x68080000 // <i>Default: 0x68080000
#define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */ #define STM32_EXT_SRAM_END 0x68080000 /* the end address of external SRAM */
// </e> // </e>
// <o> Internal SRAM memory size[Kbytes] <8-64> // <o> Internal SRAM memory size[Kbytes] <8-64>
// <i>Default: 64 // <i>Default: 64
#define STM32_SRAM_SIZE 20 #define STM32_SRAM_SIZE 20
#define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024) #define STM32_SRAM_END (0x20000000 + STM32_SRAM_SIZE * 1024)
@ -50,20 +50,20 @@
#define RT_USING_UART3 #define RT_USING_UART3
#define RT_USING_UART2 #define RT_USING_UART2
#define RT_USING_UART1 #define RT_USING_UART1
#define RT_UART_RX_BUFFER_SIZE 64 #define RT_UART_RX_BUFFER_SIZE 64
enum { enum {
/* modbus slave 485 receive and transmit control pin index */ /* modbus slave 485 receive and transmit control pin index */
MODBUS_SLAVE_RT_CONTROL_PIN_INDEX = 0, MODBUS_SLAVE_RT_CONTROL_PIN_INDEX = 0,
/* modbus master 485 receive and transmit control pin index */ /* modbus master 485 receive and transmit control pin index */
MODBUS_MASTER_RT_CONTROL_PIN_INDEX = 1, MODBUS_MASTER_RT_CONTROL_PIN_INDEX = 1,
}; };
#define LED_LED1_ON GPIO_SetBits (GPIOA,GPIO_Pin_11) //LED1 #define LED_LED1_ON GPIO_SetBits (GPIOA,GPIO_Pin_11) //LED1
#define LED_LED1_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_11) //LED1 #define LED_LED1_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_11) //LED1
#define LED_LED2_ON GPIO_SetBits (GPIOA,GPIO_Pin_12) //LED1 #define LED_LED2_ON GPIO_SetBits (GPIOA,GPIO_Pin_12) //LED1
#define LED_LED2_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_12) //LED2 #define LED_LED2_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_12) //LED2
void rt_hw_board_init(void); void rt_hw_board_init(void);
@ -74,18 +74,18 @@ void IWDG_Feed(void);
/*********************************************************************************************************/ /*********************************************************************************************************/
/** MACRO'S */ /** MACRO'S */
/***********************************************************************************************************/ /***********************************************************************************************************/
//Èí¡¢Ó²¼þ°æ±¾ºÅ¶¨Òå //软、硬件版本号定义
#define VERSION_SOFTWARE_MAJOR 1 #define VERSION_SOFTWARE_MAJOR 1
#define VERSION_SOFTWARE_MINOR 0 #define VERSION_SOFTWARE_MINOR 0
#define VERSION_HARDWARE_MAJOR 1 #define VERSION_HARDWARE_MAJOR 1
#define VERSION_HARDWARE_MINOR 0 #define VERSION_HARDWARE_MINOR 0
/***********************************************************************************************************/ /***********************************************************************************************************/
/* DATA TYPES */ /* DATA TYPES */
/***********************************************************************************************************/ /***********************************************************************************************************/

View File

@ -46,9 +46,9 @@
/** This function will initial STM32 board**/ /** This function will initial STM32 board**/
void rt_hw_board_init() void rt_hw_board_init()
{ {
BSP_Init(); BSP_Init();
stm32_hw_usart_init(); stm32_hw_usart_init();
stm32_hw_pin_init(); stm32_hw_pin_init();
} }
/******************************************************************************* /*******************************************************************************
@ -60,20 +60,20 @@ void rt_hw_board_init()
*******************************************************************************/ *******************************************************************************/
static void RCC_Configuration(void) static void RCC_Configuration(void)
{ {
//下面是给各模块开启时钟 //下面是给各模块开启时钟
//启动GPIO //启动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | \ RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | \
RCC_APB2Periph_GPIOE , RCC_APB2Periph_GPIOE ,
ENABLE); ENABLE);
//启动AFIO //启动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//配置ADC转换时钟 //配置ADC转换时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div8); //9M RCC_ADCCLKConfig(RCC_PCLK2_Div8); //9M
//启动DMA时钟 //启动DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA时钟 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//使能DMA时钟
/* Enable ADC1 and GPIOC clock */ /* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
} }
/******************************************************************************* /*******************************************************************************
@ -104,55 +104,55 @@ static void NVIC_Configuration(void)
*******************************************************************************/ *******************************************************************************/
static void GPIO_Configuration(void) static void GPIO_Configuration(void)
{ {
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure;
/***************数字输出IO初始化*********************/ /***************数字输出IO初始化*********************/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; //继电器1 LED1 LED2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12; //继电器1 LED1 LED2
GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_14 | GPIO_Pin_15; //蜂鸣器 继电器3 继电器2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_14 | GPIO_Pin_15; //蜂鸣器 继电器3 继电器2
GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure);
/*************数字输入IO初始化*********************/ /*************数字输入IO初始化*********************/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_6; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_6;
GPIO_Init(GPIOG, &GPIO_InitStructure); GPIO_Init(GPIOG, &GPIO_InitStructure);
} }
//*******************初始化独立看门狗************************************* //*******************初始化独立看门狗*************************************
//函数定义: void IWDG_Configuration(void) //函数定义: void IWDG_Configuration(void)
//描 述:初始化独立看门狗 //描 述:初始化独立看门狗
//入口参数:无 //入口参数:无
//出口参数:无 //出口参数:无
//备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值 2S超时 //备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值 2S超时
//Editorliuqh 2013-1-16 Company: BXXJS //Editorliuqh 2013-1-16 Company: BXXJS
//******************************************************************* //*******************************************************************
static void IWDG_Configuration(void) static void IWDG_Configuration(void)
{ {
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//使能对IWDG->PR和IWDG->RLR的写 IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);//使能对IWDG->PR和IWDG->RLR的写
IWDG_SetPrescaler(IWDG_Prescaler_64);//64分频 IWDG_SetPrescaler(IWDG_Prescaler_64);//64分频
IWDG_SetReload(1300); IWDG_SetReload(1300);
IWDG_ReloadCounter(); IWDG_ReloadCounter();
IWDG_Enable(); IWDG_Enable();
} }
//*******************喂独立看门狗************************************* //*******************喂独立看门狗*************************************
//函数定义: void IWDG_Feed(void) //函数定义: void IWDG_Feed(void)
//描 述:初始化独立看门狗 //描 述:初始化独立看门狗
//入口参数:无 //入口参数:无
//出口参数prer:分频数:0~7(只有低3位有效!)rlr:重装载寄存器值:低11位有效. //出口参数prer:分频数:0~7(只有低3位有效!)rlr:重装载寄存器值:低11位有效.
//备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值 //备 注:分频因子=4*2^prer.但最大值只能是256!时间计算(大概):Tout=40K/((4*2^prer)*rlr)值
//Editorliuqh 2013-1-16 Company: BXXJS //Editorliuqh 2013-1-16 Company: BXXJS
//******************************************************************* //*******************************************************************
void IWDG_Feed(void) void IWDG_Feed(void)
{ {
IWDG_ReloadCounter();//reload IWDG_ReloadCounter();//reload
} }
@ -165,15 +165,15 @@ void IWDG_Feed(void)
*******************************************************************************/ *******************************************************************************/
void SysTick_Configuration(void) void SysTick_Configuration(void)
{ {
RCC_ClocksTypeDef rcc_clocks; RCC_ClocksTypeDef rcc_clocks;
rt_uint32_t cnts; rt_uint32_t cnts;
RCC_GetClocksFreq(&rcc_clocks); RCC_GetClocksFreq(&rcc_clocks);
cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND; cnts = (rt_uint32_t)rcc_clocks.HCLK_Frequency / RT_TICK_PER_SECOND;
SysTick_Config(cnts); SysTick_Config(cnts);
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
} }
/** /**
* This is the timer interrupt service routine. * This is the timer interrupt service routine.
@ -181,13 +181,13 @@ void SysTick_Configuration(void)
*/ */
void rt_hw_timer_handler(void) void rt_hw_timer_handler(void)
{ {
/* enter interrupt */ /* enter interrupt */
rt_interrupt_enter(); rt_interrupt_enter();
rt_tick_increase(); rt_tick_increase();
/* leave interrupt */ /* leave interrupt */
rt_interrupt_leave(); rt_interrupt_leave();
} }
/* /*
@ -215,46 +215,46 @@ void rt_hw_timer_handler(void)
void BSP_Init (void) void BSP_Init (void)
{ {
RCC_Configuration(); RCC_Configuration();
NVIC_Configuration(); NVIC_Configuration();
SysTick_Configuration(); SysTick_Configuration();
GPIO_Configuration(); GPIO_Configuration();
// TODO 方便调试,暂时注释看门狗,正式发布时需要打开 // TODO 方便调试,暂时注释看门狗,正式发布时需要打开
// IWDG_Configuration(); // IWDG_Configuration();
} }
//****************************防超时程序******************************** //****************************防超时程序********************************
//函数定义: uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)()) //函数定义: uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)())
//描 述在TimeOfTimeout时间内每Period时间检测一次DetectCondition()返回的值是否有效 //描 述在TimeOfTimeout时间内每Period时间检测一次DetectCondition()返回的值是否有效
//入口参数TimeOfTimeout防超时总时间单位systick //入口参数TimeOfTimeout防超时总时间单位systick
// Period 每Period时间检测一次即时间因子单位systick // Period 每Period时间检测一次即时间因子单位systick
// (*DetectCondition)()检测条件等于ConditionValue则条件满足检测结束否则延时Period时间继续检测 // (*DetectCondition)()检测条件等于ConditionValue则条件满足检测结束否则延时Period时间继续检测
// ConditionValue ;条件成立的值 // ConditionValue ;条件成立的值
//出口参数0在TimeOfTimeout时间内检测到条件成立 //出口参数0在TimeOfTimeout时间内检测到条件成立
// 1在TimeOfTimeout时间内没有检测到条件成立 // 1在TimeOfTimeout时间内没有检测到条件成立
//备 注EditorArmink 2012-03-09 Company: BXXJS //备 注EditorArmink 2012-03-09 Company: BXXJS
//********************************************************************** //**********************************************************************
uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)(),uint8_t ConditionValue) uint8_t AvoidTimeout(uint32_t TimeOfTimeout,uint32_t Period,uint8_t (*DetectCondition)(),uint8_t ConditionValue)
{ {
uint32_t LastTimeLocal, CurTimeLocal; uint32_t LastTimeLocal, CurTimeLocal;
uint8_t ConditionValueLocal; uint8_t ConditionValueLocal;
LastTimeLocal = rt_tick_get(); LastTimeLocal = rt_tick_get();
CurTimeLocal = LastTimeLocal; CurTimeLocal = LastTimeLocal;
while(CurTimeLocal - LastTimeLocal < TimeOfTimeout) while(CurTimeLocal - LastTimeLocal < TimeOfTimeout)
{ {
CurTimeLocal = rt_tick_get(); CurTimeLocal = rt_tick_get();
ConditionValueLocal = DetectCondition(); ConditionValueLocal = DetectCondition();
if (ConditionValueLocal == ConditionValue) return 0; if (ConditionValueLocal == ConditionValue) return 0;
rt_thread_delay(Period); rt_thread_delay(Period);
} }
return 1; return 1;
} }
//************************************延时函数************************************** //************************************延时函数**************************************
//函数定义: void Delay(vu32 nCount) //函数定义: void Delay(vu32 nCount)
//入口参数nCount :延时函数中,循环的次数 //入口参数nCount :延时函数中,循环的次数
//出口参数:无 //出口参数:无
//备 注EditorArmink 2011-03-18 Company: BXXJS //备 注EditorArmink 2011-03-18 Company: BXXJS
//********************************************************************************** //**********************************************************************************
void Delay(vu32 nCount) void Delay(vu32 nCount)
{ {

View File

@ -1,7 +1,7 @@
/* /*
* File : usart.c * File : usart.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006-2013, RT-Thread Development Team * COPYRIGHT (C) 2006-2021, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -94,16 +94,16 @@ eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils ,LONG
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_COILS;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usCoilAddr >> 8;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usCoilAddr;
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8; ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF ] = usNCoils >> 8;
ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils; ucMBFrame[MB_PDU_REQ_READ_COILCNT_OFF + 1] = usNCoils;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
@ -123,11 +123,11 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
/* If this request is broadcast, and it's read mode. This request don't need execute. */ /* If this request is broadcast, and it's read mode. This request don't need execute. */
if ( xMBMasterRequestIsBroadcast() ) if ( xMBMasterRequestIsBroadcast() )
{ {
eStatus = MB_EX_NONE; eStatus = MB_EX_NONE;
} }
else if ( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) else if ( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;
@ -139,11 +139,11 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
* byte is only partially field with unused coils set to zero. */ * byte is only partially field with unused coils set to zero. */
if( ( usCoilCount & 0x0007 ) != 0 ) if( ( usCoilCount & 0x0007 ) != 0 )
{ {
ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 ); ucByteCount = ( UCHAR )( usCoilCount / 8 + 1 );
} }
else else
{ {
ucByteCount = ( UCHAR )( usCoilCount / 8 ); ucByteCount = ( UCHAR )( usCoilCount / 8 );
} }
/* Check if the number of registers to read is valid. If not /* Check if the number of registers to read is valid. If not
@ -152,7 +152,7 @@ eMBMasterFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
if( ( usCoilCount >= 1 ) && if( ( usCoilCount >= 1 ) &&
( ucByteCount == pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] ) ) ( ucByteCount == pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] ) )
{ {
/* Make callback to fill the buffer. */ /* Make callback to fill the buffer. */
eRegStatus = eMBMasterRegCoilsCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usCoilCount, MB_REG_READ ); eRegStatus = eMBMasterRegCoilsCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usCoilCount, MB_REG_READ );
/* If an error occured convert it into a Modbus exception. */ /* If an error occured convert it into a Modbus exception. */
@ -201,16 +201,16 @@ eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LO
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_SINGLE_COIL;
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8; ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usCoilAddr >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr; ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usCoilAddr;
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8; ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF ] = usCoilData >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData; ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usCoilData;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -285,7 +285,7 @@ eMBMasterFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
*/ */
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut) USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut)
{ {
UCHAR *ucMBFrame; UCHAR *ucMBFrame;
USHORT usRegIndex = 0; USHORT usRegIndex = 0;
@ -297,30 +297,30 @@ eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_COILS;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8; ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usCoilAddr >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr; ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usCoilAddr;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8; ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF] = usNCoils >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ; ucMBFrame[MB_PDU_REQ_WRITE_MUL_COILCNT_OFF + 1] = usNCoils ;
if( ( usNCoils & 0x0007 ) != 0 ) if( ( usNCoils & 0x0007 ) != 0 )
{ {
ucByteCount = ( UCHAR )( usNCoils / 8 + 1 ); ucByteCount = ( UCHAR )( usNCoils / 8 + 1 );
} }
else else
{ {
ucByteCount = ( UCHAR )( usNCoils / 8 ); ucByteCount = ( UCHAR )( usNCoils / 8 );
} }
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount; ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = ucByteCount;
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
while( ucByteCount > usRegIndex) while( ucByteCount > usRegIndex)
{ {
*ucMBFrame++ = pucDataBuffer[usRegIndex++]; *ucMBFrame++ = pucDataBuffer[usRegIndex++];
} }
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + ucByteCount );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -340,7 +340,7 @@ eMBMasterFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
/* If this request is broadcast, the *usLen is not need check. */ /* If this request is broadcast, the *usLen is not need check. */
if( ( *usLen == MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() ) if( ( *usLen == MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;

View File

@ -79,16 +79,16 @@ eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT u
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_DISCRETE_INPUTS;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usDiscreteAddr >> 8;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usDiscreteAddr;
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8; ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF ] = usNDiscreteIn >> 8;
ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn; ucMBFrame[MB_PDU_REQ_READ_DISCCNT_OFF + 1] = usNDiscreteIn;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -107,11 +107,11 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
/* If this request is broadcast, and it's read mode. This request don't need execute. */ /* If this request is broadcast, and it's read mode. This request don't need execute. */
if ( xMBMasterRequestIsBroadcast() ) if ( xMBMasterRequestIsBroadcast() )
{ {
eStatus = MB_EX_NONE; eStatus = MB_EX_NONE;
} }
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;
@ -123,26 +123,26 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
* byte is only partially field with unused coils set to zero. */ * byte is only partially field with unused coils set to zero. */
if( ( usDiscreteCnt & 0x0007 ) != 0 ) if( ( usDiscreteCnt & 0x0007 ) != 0 )
{ {
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 ); ucNBytes = ( UCHAR )( usDiscreteCnt / 8 + 1 );
} }
else else
{ {
ucNBytes = ( UCHAR )( usDiscreteCnt / 8 ); ucNBytes = ( UCHAR )( usDiscreteCnt / 8 );
} }
/* Check if the number of registers to read is valid. If not /* Check if the number of registers to read is valid. If not
* return Modbus illegal data value exception. * return Modbus illegal data value exception.
*/ */
if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF]) if ((usDiscreteCnt >= 1) && ucNBytes == pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF])
{ {
/* Make callback to fill the buffer. */ /* Make callback to fill the buffer. */
eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt ); eRegStatus = eMBMasterRegDiscreteCB( &pucFrame[MB_PDU_FUNC_READ_VALUES_OFF], usRegAddress, usDiscreteCnt );
/* If an error occured convert it into a Modbus exception. */ /* If an error occured convert it into a Modbus exception. */
if( eRegStatus != MB_ENOERR ) if( eRegStatus != MB_ENOERR )
{ {
eStatus = prveMBError2Exception( eRegStatus ); eStatus = prveMBError2Exception( eRegStatus );
} }
} }
else else
{ {

View File

@ -106,16 +106,16 @@ eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRe
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_REGISTER;
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8; ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF] = usRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr; ucMBFrame[MB_PDU_REQ_WRITE_ADDR_OFF + 1] = usRegAddr;
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8; ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF] = usRegData >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ; ucMBFrame[MB_PDU_REQ_WRITE_VALUE_OFF + 1] = usRegData ;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -167,7 +167,7 @@ eMBMasterFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
*/ */
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ) USHORT usRegAddr, USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut )
{ {
UCHAR *ucMBFrame; UCHAR *ucMBFrame;
USHORT usRegIndex = 0; USHORT usRegIndex = 0;
@ -177,23 +177,23 @@ eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr,
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_WRITE_MULTIPLE_REGISTERS;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8; ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] = usRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr; ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] = usRegAddr;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8; ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF] = usNRegs >> 8;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ; ucMBFrame[MB_PDU_REQ_WRITE_MUL_REGCNT_OFF + 1] = usNRegs ;
ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2; ucMBFrame[MB_PDU_REQ_WRITE_MUL_BYTECNT_OFF] = usNRegs * 2;
ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF; ucMBFrame += MB_PDU_REQ_WRITE_MUL_VALUES_OFF;
while( usNRegs > usRegIndex) while( usNRegs > usRegIndex)
{ {
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ; *ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
} }
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_WRITE_MUL_SIZE_MIN + 2*usNRegs );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -212,7 +212,7 @@ eMBMasterFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
/* If this request is broadcast, the *usLen is not need check. */ /* If this request is broadcast, the *usLen is not need check. */
if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() ) if( ( *usLen == MB_PDU_SIZE_MIN + MB_PDU_FUNC_WRITE_MUL_SIZE ) || xMBMasterRequestIsBroadcast() )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_WRITE_MUL_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;
@ -271,16 +271,16 @@ eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRe
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_HOLDING_REGISTER;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -298,11 +298,11 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
/* If this request is broadcast, and it's read mode. This request don't need execute. */ /* If this request is broadcast, and it's read mode. This request don't need execute. */
if ( xMBMasterRequestIsBroadcast() ) if ( xMBMasterRequestIsBroadcast() )
{ {
eStatus = MB_EX_NONE; eStatus = MB_EX_NONE;
} }
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN ) else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;
@ -355,8 +355,8 @@ eMBMasterFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
*/ */
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ) USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut )
{ {
UCHAR *ucMBFrame; UCHAR *ucMBFrame;
USHORT usRegIndex = 0; USHORT usRegIndex = 0;
@ -366,27 +366,27 @@ eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8; ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] = usReadRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr; ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] = usReadRegAddr;
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8; ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF] = usNReadRegs >> 8;
ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ; ucMBFrame[MB_PDU_REQ_READWRITE_READ_REGCNT_OFF + 1] = usNReadRegs ;
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8; ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF] = usWriteRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr; ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_ADDR_OFF + 1] = usWriteRegAddr;
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8; ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF] = usNWriteRegs >> 8;
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ; ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_REGCNT_OFF + 1] = usNWriteRegs ;
ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2; ucMBFrame[MB_PDU_REQ_READWRITE_WRITE_BYTECNT_OFF] = usNWriteRegs * 2;
ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF; ucMBFrame += MB_PDU_REQ_READWRITE_WRITE_VALUES_OFF;
while( usNWriteRegs > usRegIndex) while( usNWriteRegs > usRegIndex)
{ {
*ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8; *ucMBFrame++ = pusDataBuffer[usRegIndex] >> 8;
*ucMBFrame++ = pusDataBuffer[usRegIndex++] ; *ucMBFrame++ = pusDataBuffer[usRegIndex++] ;
} }
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READWRITE_SIZE_MIN + 2*usNWriteRegs );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -406,11 +406,11 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen
/* If this request is broadcast, and it's read mode. This request don't need execute. */ /* If this request is broadcast, and it's read mode. This request don't need execute. */
if ( xMBMasterRequestIsBroadcast() ) if ( xMBMasterRequestIsBroadcast() )
{ {
eStatus = MB_EX_NONE; eStatus = MB_EX_NONE;
} }
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN ) else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READWRITE_SIZE_MIN )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U ); usRegReadAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF] << 8U );
usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] ); usRegReadAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READWRITE_READ_ADDR_OFF + 1] );
usRegReadAddress++; usRegReadAddress++;
@ -434,8 +434,8 @@ eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen
if( eRegStatus == MB_ENOERR ) if( eRegStatus == MB_ENOERR )
{ {
/* Make the read callback. */ /* Make the read callback. */
eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF], eRegStatus = eMBMasterRegHoldingCB(&pucFrame[MB_PDU_FUNC_READWRITE_READ_VALUES_OFF],
usRegReadAddress, usRegReadCount, MB_REG_READ); usRegReadAddress, usRegReadCount, MB_REG_READ);
} }
if( eRegStatus != MB_ENOERR ) if( eRegStatus != MB_ENOERR )
{ {

View File

@ -79,16 +79,16 @@ eMBMasterReqReadInputRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs
else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY; else if ( xMBMasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = MB_MRE_MASTER_BUSY;
else else
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); vMBMasterGetPDUSndBuf(&ucMBFrame);
vMBMasterSetDestAddress(ucSndAddr); vMBMasterSetDestAddress(ucSndAddr);
ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER; ucMBFrame[MB_PDU_FUNC_OFF] = MB_FUNC_READ_INPUT_REGISTER;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] = usRegAddr >> 8;
ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr; ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] = usRegAddr;
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8; ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF] = usNRegs >> 8;
ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs; ucMBFrame[MB_PDU_REQ_READ_REGCNT_OFF + 1] = usNRegs;
vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE ); vMBMasterSetPDUSndLength( MB_PDU_SIZE_MIN + MB_PDU_REQ_READ_SIZE );
( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT ); ( void ) xMBMasterPortEventPost( EV_MASTER_FRAME_SENT );
eErrStatus = eMBMasterWaitRequestFinish( ); eErrStatus = eMBMasterWaitRequestFinish( );
} }
return eErrStatus; return eErrStatus;
} }
@ -104,13 +104,13 @@ eMBMasterFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
eMBErrorCode eRegStatus; eMBErrorCode eRegStatus;
/* If this request is broadcast, and it's read mode. This request don't need execute. */ /* If this request is broadcast, and it's read mode. This request don't need execute. */
if ( xMBMasterRequestIsBroadcast() ) if ( xMBMasterRequestIsBroadcast() )
{
eStatus = MB_EX_NONE;
}
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
{ {
vMBMasterGetPDUSndBuf(&ucMBFrame); eStatus = MB_EX_NONE;
}
else if( *usLen >= MB_PDU_SIZE_MIN + MB_PDU_FUNC_READ_SIZE_MIN )
{
vMBMasterGetPDUSndBuf(&ucMBFrame);
usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 ); usRegAddress = ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF] << 8 );
usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] ); usRegAddress |= ( USHORT )( ucMBFrame[MB_PDU_REQ_READ_ADDR_OFF + 1] );
usRegAddress++; usRegAddress++;

View File

@ -87,9 +87,9 @@ typedef enum
*/ */
typedef enum typedef enum
{ {
MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */ MB_TMODE_T35, /*!< Master receive frame T3.5 timeout. */
MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */ MB_TMODE_RESPOND_TIMEOUT, /*!< Master wait respond for slave. */
MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/ MB_TMODE_CONVERT_DELAY /*!< Master sent broadcast ,then delay sometime.*/
}eMBMasterTimerMode; }eMBMasterTimerMode;
/* ----------------------- Function prototypes ------------------------------*/ /* ----------------------- Function prototypes ------------------------------*/
@ -115,7 +115,7 @@ typedef enum
* - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error. * - eMBErrorCode::MB_EPORTERR IF the porting layer returned an error.
*/ */
eMBErrorCode eMBMasterInit( eMBMode eMode, UCHAR ucPort, eMBErrorCode eMBMasterInit( eMBMode eMode, UCHAR ucPort,
ULONG ulBaudRate, eMBParity eParity ); ULONG ulBaudRate, eMBParity eParity );
/*! \ingroup modbus /*! \ingroup modbus
* \brief Initialize the Modbus Master protocol stack for Modbus TCP. * \brief Initialize the Modbus Master protocol stack for Modbus TCP.
@ -260,7 +260,7 @@ eMBErrorCode eMBMasterRegisterCB( UCHAR ucFunctionCode,
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
*/ */
eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNRegs ); USHORT usNRegs );
/*! \ingroup modbus_registers /*! \ingroup modbus_registers
* \brief Callback function used if a <em>Holding Register</em> value is * \brief Callback function used if a <em>Holding Register</em> value is
@ -289,7 +289,7 @@ eMBErrorCode eMBMasterRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress,
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
*/ */
eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNRegs, eMBRegisterMode eMode ); USHORT usNRegs, eMBRegisterMode eMode );
/*! \ingroup modbus_registers /*! \ingroup modbus_registers
* \brief Callback function used if a <em>Coil Register</em> value is * \brief Callback function used if a <em>Coil Register</em> value is
@ -318,7 +318,7 @@ eMBErrorCode eMBMasterRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress,
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
*/ */
eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNCoils, eMBRegisterMode eMode ); USHORT usNCoils, eMBRegisterMode eMode );
/*! \ingroup modbus_registers /*! \ingroup modbus_registers
* \brief Callback function used if a <em>Input Discrete Register</em> value is * \brief Callback function used if a <em>Input Discrete Register</em> value is
@ -341,7 +341,7 @@ eMBErrorCode eMBMasterRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress,
* <b>ILLEGAL DATA ADDRESS</b> is sent as a response. * <b>ILLEGAL DATA ADDRESS</b> is sent as a response.
*/ */
eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, eMBErrorCode eMBMasterRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress,
USHORT usNDiscrete ); USHORT usNDiscrete );
/*! \ingroup modbus /*! \ingroup modbus
*\brief These Modbus functions are called for user when Modbus run in Master Mode. *\brief These Modbus functions are called for user when Modbus run in Master Mode.
@ -352,20 +352,20 @@ eMBMasterReqErrCode
eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut ); eMBMasterReqWriteHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usRegData, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, eMBMasterReqWriteMultipleHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr,
USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut ); USHORT usNRegs, USHORT * pusDataBuffer, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut ); eMBMasterReqReadHoldingRegister( UCHAR ucSndAddr, USHORT usRegAddr, USHORT usNRegs, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr, eMBMasterReqReadWriteMultipleHoldingRegister( UCHAR ucSndAddr,
USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer, USHORT usReadRegAddr, USHORT usNReadRegs, USHORT * pusDataBuffer,
USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut ); USHORT usWriteRegAddr, USHORT usNWriteRegs, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut ); eMBMasterReqReadCoils( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usNCoils, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut ); eMBMasterReqWriteCoil( UCHAR ucSndAddr, USHORT usCoilAddr, USHORT usCoilData, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr, eMBMasterReqWriteMultipleCoils( UCHAR ucSndAddr,
USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut ); USHORT usCoilAddr, USHORT usNCoils, UCHAR * pucDataBuffer, LONG lTimeOut );
eMBMasterReqErrCode eMBMasterReqErrCode
eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut ); eMBMasterReqReadDiscreteInputs( UCHAR ucSndAddr, USHORT usDiscreteAddr, USHORT usNDiscreteIn, LONG lTimeOut );
@ -390,7 +390,7 @@ eMBMasterFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen );
eMBException eMBException
eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen ); eMBMasterFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen );
/*£¡ \ingroup modbus /*! \ingroup modbus
*\brief These functions are interface for Modbus Master *\brief These functions are interface for Modbus Master
*/ */
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame ); void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame );

View File

@ -102,7 +102,7 @@ BOOL( *pxMBMasterFrameCBTransmitFSMCur ) ( void );
*/ */
static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = { static xMBFunctionHandler xMasterFuncHandlers[MB_FUNC_HANDLERS_MAX] = {
#if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0 #if MB_FUNC_OTHER_REP_SLAVEID_ENABLED > 0
//TODO Add Master function define //TODO Add Master function define
{MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID}, {MB_FUNC_OTHER_REPORT_SLAVEID, eMBFuncReportSlaveID},
#endif #endif
#if MB_FUNC_READ_INPUT_ENABLED > 0 #if MB_FUNC_READ_INPUT_ENABLED > 0
@ -140,56 +140,56 @@ eMBMasterInit( eMBMode eMode, UCHAR ucPort, ULONG ulBaudRate, eMBParity eParity
{ {
eMBErrorCode eStatus = MB_ENOERR; eMBErrorCode eStatus = MB_ENOERR;
switch (eMode) switch (eMode)
{ {
#if MB_MASTER_RTU_ENABLED > 0 #if MB_MASTER_RTU_ENABLED > 0
case MB_RTU: case MB_RTU:
pvMBMasterFrameStartCur = eMBMasterRTUStart; pvMBMasterFrameStartCur = eMBMasterRTUStart;
pvMBMasterFrameStopCur = eMBMasterRTUStop; pvMBMasterFrameStopCur = eMBMasterRTUStop;
peMBMasterFrameSendCur = eMBMasterRTUSend; peMBMasterFrameSendCur = eMBMasterRTUSend;
peMBMasterFrameReceiveCur = eMBMasterRTUReceive; peMBMasterFrameReceiveCur = eMBMasterRTUReceive;
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM; pxMBMasterFrameCBByteReceived = xMBMasterRTUReceiveFSM;
pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM; pxMBMasterFrameCBTransmitterEmpty = xMBMasterRTUTransmitFSM;
pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired; pxMBMasterPortCBTimerExpired = xMBMasterRTUTimerExpired;
eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity); eStatus = eMBMasterRTUInit(ucPort, ulBaudRate, eParity);
break; break;
#endif #endif
#if MB_MASTER_ASCII_ENABLED > 0 #if MB_MASTER_ASCII_ENABLED > 0
case MB_ASCII: case MB_ASCII:
pvMBMasterFrameStartCur = eMBMasterASCIIStart; pvMBMasterFrameStartCur = eMBMasterASCIIStart;
pvMBMasterFrameStopCur = eMBMasterASCIIStop; pvMBMasterFrameStopCur = eMBMasterASCIIStop;
peMBMasterFrameSendCur = eMBMasterASCIISend; peMBMasterFrameSendCur = eMBMasterASCIISend;
peMBMasterFrameReceiveCur = eMBMasterASCIIReceive; peMBMasterFrameReceiveCur = eMBMasterASCIIReceive;
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL; pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBMasterPortClose : NULL;
pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM; pxMBMasterFrameCBByteReceived = xMBMasterASCIIReceiveFSM;
pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM; pxMBMasterFrameCBTransmitterEmpty = xMBMasterASCIITransmitFSM;
pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired; pxMBMasterPortCBTimerExpired = xMBMasterASCIITimerT1SExpired;
eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity ); eStatus = eMBMasterASCIIInit(ucPort, ulBaudRate, eParity );
break; break;
#endif #endif
default: default:
eStatus = MB_EINVAL; eStatus = MB_EINVAL;
break; break;
} }
if (eStatus == MB_ENOERR) if (eStatus == MB_ENOERR)
{ {
if (!xMBMasterPortEventInit()) if (!xMBMasterPortEventInit())
{ {
/* port dependent event module initalization failed. */ /* port dependent event module initalization failed. */
eStatus = MB_EPORTERR; eStatus = MB_EPORTERR;
} }
else else
{ {
eMBState = STATE_DISABLED; eMBState = STATE_DISABLED;
} }
/* initialize the OS resource for modbus master. */ /* initialize the OS resource for modbus master. */
vMBMasterOsResInit(); vMBMasterOsResInit();
} }
return eStatus; return eStatus;
} }
eMBErrorCode eMBErrorCode
@ -314,73 +314,73 @@ eMBMasterPoll( void )
eException = MB_EX_ILLEGAL_FUNCTION; eException = MB_EX_ILLEGAL_FUNCTION;
/* If receive frame has exception .The receive function code highest bit is 1.*/ /* If receive frame has exception .The receive function code highest bit is 1.*/
if(ucFunctionCode >> 7) { if(ucFunctionCode >> 7) {
eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF]; eException = (eMBException)ucMBFrame[MB_PDU_DATA_OFF];
}
else
{
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */
if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
break;
}
else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
vMBMasterSetCBRunInMasterMode(TRUE);
/* If master request is broadcast,
* the master need execute function for all slave.
*/
if ( xMBMasterRequestIsBroadcast() ) {
usLength = usMBMasterGetPDUSndLength();
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
vMBMasterSetDestAddress(j);
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
}
}
else {
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
}
vMBMasterSetCBRunInMasterMode(FALSE);
break;
}
}
} }
else
{
for (i = 0; i < MB_FUNC_HANDLERS_MAX; i++)
{
/* No more function handlers registered. Abort. */
if (xMasterFuncHandlers[i].ucFunctionCode == 0) {
break;
}
else if (xMasterFuncHandlers[i].ucFunctionCode == ucFunctionCode) {
vMBMasterSetCBRunInMasterMode(TRUE);
/* If master request is broadcast,
* the master need execute function for all slave.
*/
if ( xMBMasterRequestIsBroadcast() ) {
usLength = usMBMasterGetPDUSndLength();
for(j = 1; j <= MB_MASTER_TOTAL_SLAVE_NUM; j++){
vMBMasterSetDestAddress(j);
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
}
}
else {
eException = xMasterFuncHandlers[i].pxHandler(ucMBFrame, &usLength);
}
vMBMasterSetCBRunInMasterMode(FALSE);
break;
}
}
}
/* If master has exception ,Master will send error process.Otherwise the Master is idle.*/ /* If master has exception ,Master will send error process.Otherwise the Master is idle.*/
if (eException != MB_EX_NONE) { if (eException != MB_EX_NONE) {
vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION); vMBMasterSetErrorType(EV_ERROR_EXECUTE_FUNCTION);
( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); ( void ) xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
} }
else { else {
vMBMasterCBRequestScuuess( ); vMBMasterCBRequestScuuess( );
vMBMasterRunResRelease( ); vMBMasterRunResRelease( );
} }
break; break;
case EV_MASTER_FRAME_SENT: case EV_MASTER_FRAME_SENT:
/* Master is busy now. */ /* Master is busy now. */
vMBMasterGetPDUSndBuf( &ucMBFrame ); vMBMasterGetPDUSndBuf( &ucMBFrame );
eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() ); eStatus = peMBMasterFrameSendCur( ucMBMasterGetDestAddress(), ucMBFrame, usMBMasterGetPDUSndLength() );
break; break;
case EV_MASTER_ERROR_PROCESS: case EV_MASTER_ERROR_PROCESS:
/* Execute specified error process callback function. */ /* Execute specified error process callback function. */
errorType = eMBMasterGetErrorType(); errorType = eMBMasterGetErrorType();
vMBMasterGetPDUSndBuf( &ucMBFrame ); vMBMasterGetPDUSndBuf( &ucMBFrame );
switch (errorType) { switch (errorType) {
case EV_ERROR_RESPOND_TIMEOUT: case EV_ERROR_RESPOND_TIMEOUT:
vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(), vMBMasterErrorCBRespondTimeout(ucMBMasterGetDestAddress(),
ucMBFrame, usMBMasterGetPDUSndLength()); ucMBFrame, usMBMasterGetPDUSndLength());
break; break;
case EV_ERROR_RECEIVE_DATA: case EV_ERROR_RECEIVE_DATA:
vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(), vMBMasterErrorCBReceiveData(ucMBMasterGetDestAddress(),
ucMBFrame, usMBMasterGetPDUSndLength()); ucMBFrame, usMBMasterGetPDUSndLength());
break; break;
case EV_ERROR_EXECUTE_FUNCTION: case EV_ERROR_EXECUTE_FUNCTION:
vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(), vMBMasterErrorCBExecuteFunction(ucMBMasterGetDestAddress(),
ucMBFrame, usMBMasterGetPDUSndLength()); ucMBFrame, usMBMasterGetPDUSndLength());
break; break;
} }
vMBMasterRunResRelease(); vMBMasterRunResRelease();
break; break;
default: default:
break; break;
@ -393,32 +393,32 @@ eMBMasterPoll( void )
/* Get whether the Modbus Master is run in master mode.*/ /* Get whether the Modbus Master is run in master mode.*/
BOOL xMBMasterGetCBRunInMasterMode( void ) BOOL xMBMasterGetCBRunInMasterMode( void )
{ {
return xMBRunInMasterMode; return xMBRunInMasterMode;
} }
/* Set whether the Modbus Master is run in master mode.*/ /* Set whether the Modbus Master is run in master mode.*/
void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode ) void vMBMasterSetCBRunInMasterMode( BOOL IsMasterMode )
{ {
xMBRunInMasterMode = IsMasterMode; xMBRunInMasterMode = IsMasterMode;
} }
/* Get Modbus Master send destination address. */ /* Get Modbus Master send destination address. */
UCHAR ucMBMasterGetDestAddress( void ) UCHAR ucMBMasterGetDestAddress( void )
{ {
return ucMBMasterDestAddress; return ucMBMasterDestAddress;
} }
/* Set Modbus Master send destination address. */ /* Set Modbus Master send destination address. */
void vMBMasterSetDestAddress( UCHAR Address ) void vMBMasterSetDestAddress( UCHAR Address )
{ {
ucMBMasterDestAddress = Address; ucMBMasterDestAddress = Address;
} }
/* Get Modbus Master current error event type. */ /* Get Modbus Master current error event type. */
eMBMasterErrorEventType eMBMasterGetErrorType( void ) eMBMasterErrorEventType eMBMasterGetErrorType( void )
{ {
return eMBMasterCurErrorType; return eMBMasterCurErrorType;
} }
/* Set Modbus Master current error event type. */ /* Set Modbus Master current error event type. */
void vMBMasterSetErrorType( eMBMasterErrorEventType errorType ) void vMBMasterSetErrorType( eMBMasterErrorEventType errorType )
{ {
eMBMasterCurErrorType = errorType; eMBMasterCurErrorType = errorType;
} }

View File

@ -37,7 +37,7 @@
PR_BEGIN_EXTERN_C PR_BEGIN_EXTERN_C
#endif #endif
eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate, eMBErrorCode eMBRTUInit( UCHAR slaveAddress, UCHAR ucPort, ULONG ulBaudRate,
eMBParity eParity ); eMBParity eParity );
void eMBRTUStart( void ); void eMBRTUStart( void );
void eMBRTUStop( void ); void eMBRTUStop( void );
eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength ); eMBErrorCode eMBRTUReceive( UCHAR * pucRcvAddress, UCHAR ** pucFrame, USHORT * pusLength );

View File

@ -262,11 +262,11 @@ xMBMasterRTUReceiveFSM( void )
* the timer of respond timeout . * the timer of respond timeout .
*/ */
case STATE_M_RX_IDLE: case STATE_M_RX_IDLE:
/* In time of respond timeout,the receiver receive a frame. /* In time of respond timeout,the receiver receive a frame.
* Disable timer of respond timeout and change the transmiter state to idle. * Disable timer of respond timeout and change the transmiter state to idle.
*/ */
vMBMasterPortTimersDisable( ); vMBMasterPortTimersDisable( );
eSndState = STATE_M_TX_IDLE; eSndState = STATE_M_TX_IDLE;
usMasterRcvBufferPos = 0; usMasterRcvBufferPos = 0;
ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte; ucMasterRTURcvBuf[usMasterRcvBufferPos++] = ucByte;
@ -331,11 +331,11 @@ xMBMasterRTUTransmitFSM( void )
* else master will enable timer of respond timeout. */ * else master will enable timer of respond timeout. */
if ( xFrameIsBroadcast == TRUE ) if ( xFrameIsBroadcast == TRUE )
{ {
vMBMasterPortTimersConvertDelayEnable( ); vMBMasterPortTimersConvertDelayEnable( );
} }
else else
{ {
vMBMasterPortTimersRespondTimeoutEnable( ); vMBMasterPortTimersRespondTimeoutEnable( );
} }
} }
break; break;
@ -350,97 +350,97 @@ xMBMasterRTUTransmitFSM( void )
BOOL BOOL
xMBMasterRTUTimerExpired(void) xMBMasterRTUTimerExpired(void)
{ {
BOOL xNeedPoll = FALSE; BOOL xNeedPoll = FALSE;
switch (eRcvState) switch (eRcvState)
{ {
/* Timer t35 expired. Startup phase is finished. */ /* Timer t35 expired. Startup phase is finished. */
case STATE_M_RX_INIT: case STATE_M_RX_INIT:
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_READY);
break; break;
/* A frame was received and t35 expired. Notify the listener that /* A frame was received and t35 expired. Notify the listener that
* a new frame was received. */ * a new frame was received. */
case STATE_M_RX_RCV: case STATE_M_RX_RCV:
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_FRAME_RECEIVED);
break; break;
/* An error occured while receiving the frame. */ /* An error occured while receiving the frame. */
case STATE_M_RX_ERROR: case STATE_M_RX_ERROR:
vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA); vMBMasterSetErrorType(EV_ERROR_RECEIVE_DATA);
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS ); xNeedPoll = xMBMasterPortEventPost( EV_MASTER_ERROR_PROCESS );
break; break;
/* Function called in an illegal state. */ /* Function called in an illegal state. */
default: default:
assert_param( assert_param(
( eRcvState == STATE_M_RX_INIT ) || ( eRcvState == STATE_M_RX_RCV ) || ( eRcvState == STATE_M_RX_INIT ) || ( eRcvState == STATE_M_RX_RCV ) ||
( eRcvState == STATE_M_RX_ERROR ) || ( eRcvState == STATE_M_RX_IDLE )); ( eRcvState == STATE_M_RX_ERROR ) || ( eRcvState == STATE_M_RX_IDLE ));
break; break;
} }
eRcvState = STATE_M_RX_IDLE; eRcvState = STATE_M_RX_IDLE;
switch (eSndState) switch (eSndState)
{ {
/* A frame was send finish and convert delay or respond timeout expired. /* A frame was send finish and convert delay or respond timeout expired.
* If the frame is broadcast,The master will idle,and if the frame is not * If the frame is broadcast,The master will idle,and if the frame is not
* broadcast.Notify the listener process error.*/ * broadcast.Notify the listener process error.*/
case STATE_M_TX_XFWR: case STATE_M_TX_XFWR:
if ( xFrameIsBroadcast == FALSE ) { if ( xFrameIsBroadcast == FALSE ) {
vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT); vMBMasterSetErrorType(EV_ERROR_RESPOND_TIMEOUT);
xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS); xNeedPoll = xMBMasterPortEventPost(EV_MASTER_ERROR_PROCESS);
} }
break; break;
/* Function called in an illegal state. */ /* Function called in an illegal state. */
default: default:
assert_param( assert_param(
( eSndState == STATE_M_TX_XFWR ) || ( eSndState == STATE_M_TX_IDLE )); ( eSndState == STATE_M_TX_XFWR ) || ( eSndState == STATE_M_TX_IDLE ));
break; break;
} }
eSndState = STATE_M_TX_IDLE; eSndState = STATE_M_TX_IDLE;
vMBMasterPortTimersDisable( ); vMBMasterPortTimersDisable( );
/* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */ /* If timer mode is convert delay, the master event then turns EV_MASTER_EXECUTE status. */
if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) { if (eMasterCurTimerMode == MB_TMODE_CONVERT_DELAY) {
xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE ); xNeedPoll = xMBMasterPortEventPost( EV_MASTER_EXECUTE );
} }
return xNeedPoll; return xNeedPoll;
} }
/* Get Modbus Master send RTU's buffer address pointer.*/ /* Get Modbus Master send RTU's buffer address pointer.*/
void vMBMasterGetRTUSndBuf( UCHAR ** pucFrame ) void vMBMasterGetRTUSndBuf( UCHAR ** pucFrame )
{ {
*pucFrame = ( UCHAR * ) ucMasterRTUSndBuf; *pucFrame = ( UCHAR * ) ucMasterRTUSndBuf;
} }
/* Get Modbus Master send PDU's buffer address pointer.*/ /* Get Modbus Master send PDU's buffer address pointer.*/
void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame ) void vMBMasterGetPDUSndBuf( UCHAR ** pucFrame )
{ {
*pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF]; *pucFrame = ( UCHAR * ) &ucMasterRTUSndBuf[MB_SER_PDU_PDU_OFF];
} }
/* Set Modbus Master send PDU's buffer length.*/ /* Set Modbus Master send PDU's buffer length.*/
void vMBMasterSetPDUSndLength( USHORT SendPDULength ) void vMBMasterSetPDUSndLength( USHORT SendPDULength )
{ {
usMasterSendPDULength = SendPDULength; usMasterSendPDULength = SendPDULength;
} }
/* Get Modbus Master send PDU's buffer length.*/ /* Get Modbus Master send PDU's buffer length.*/
USHORT usMBMasterGetPDUSndLength( void ) USHORT usMBMasterGetPDUSndLength( void )
{ {
return usMasterSendPDULength; return usMasterSendPDULength;
} }
/* Set Modbus Master current timer mode.*/ /* Set Modbus Master current timer mode.*/
void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode ) void vMBMasterSetCurTimerMode( eMBMasterTimerMode eMBTimerMode )
{ {
eMasterCurTimerMode = eMBTimerMode; eMasterCurTimerMode = eMBTimerMode;
} }
/* The master request is broadcast? */ /* The master request is broadcast? */
BOOL xMBMasterRequestIsBroadcast( void ){ BOOL xMBMasterRequestIsBroadcast( void ){
return xFrameIsBroadcast; return xFrameIsBroadcast;
} }
#endif #endif

View File

@ -30,11 +30,11 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#define INLINE #define INLINE
#define PR_BEGIN_EXTERN_C extern "C" { #define PR_BEGIN_EXTERN_C extern "C" {
#define PR_END_EXTERN_C } #define PR_END_EXTERN_C }
#define ENTER_CRITICAL_SECTION() EnterCriticalSection() #define ENTER_CRITICAL_SECTION() EnterCriticalSection()
#define EXIT_CRITICAL_SECTION() ExitCriticalSection() #define EXIT_CRITICAL_SECTION() ExitCriticalSection()
typedef uint8_t BOOL; typedef uint8_t BOOL;