199 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			199 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
/****************************************************************************
 | 
						|
 | 
						|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | 
						|
 | 
						|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | 
						|
be copied by any method or incorporated into another program without
 | 
						|
the express written consent of Aerospace C.Power. This Information or any portion
 | 
						|
thereof remains the property of Aerospace C.Power. The Information contained herein
 | 
						|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | 
						|
liability for its use in any way and conveys no license or title under
 | 
						|
any patent or copyright and makes no representation or warranty that this
 | 
						|
Information is free from patent or copyright infringement.
 | 
						|
 | 
						|
****************************************************************************/
 | 
						|
 | 
						|
#ifndef _IOT_MODBUS_TASK_H_
 | 
						|
#define _IOT_MODBUS_TASK_H_
 | 
						|
 | 
						|
#include "iot_task_api.h"
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
extern "C" {
 | 
						|
#endif
 | 
						|
 | 
						|
#if PLC_SUPPORT_CCO_ROLE
 | 
						|
#define IOT_MODBUS_RT_TABLE_SUPPORT          1
 | 
						|
#endif
 | 
						|
 | 
						|
/** Message type defination: */
 | 
						|
#define IOT_MODBUS_TASK_MT_UART              1 /** message from uart. */
 | 
						|
#define IOT_MODBUS_TASK_MT_TIMER             2 /** message from timer. */
 | 
						|
#define IOT_MODBUS_TASK_MT_INTERNAL          3 /** message from modbus-task. */
 | 
						|
 | 
						|
/* message id for IOT_MODBUS_TASK_MT_TIMER */
 | 
						|
#define IOT_MODBUS_TASK_MSG_ID_TMR_GE_START_MONITOR   (1)
 | 
						|
#define IOT_MODBUS_TASK_MSG_ID_TMR_UART_MB_TIMEOUT    (2)
 | 
						|
#define IOT_MODBUS_TASK_MSG_ID_CMD_HANDLE             (3)
 | 
						|
#if IOT_MODBUS_RT_TABLE_SUPPORT
 | 
						|
#define IOT_MODBUS_TASK_MSG_ID_TMR_RT_UPDATE_TIMEOUT  (4)
 | 
						|
 | 
						|
/* rout table update period, 20S */
 | 
						|
#define IOT_MODBUS_TASK_RT_TIMER_PERIOD   (20*1000) /* units:ms */
 | 
						|
/* iot mac address hash table handle */
 | 
						|
typedef void *iot_modbus_addr_rout_table_h;
 | 
						|
#endif
 | 
						|
#define IOT_MODBUS_MSG_ID_TMR_MB_EXT_RESP_TIMEOUT     (5)
 | 
						|
 | 
						|
#define IOT_MODBUS_TASK_ID                          IOT_MODBUS_MID
 | 
						|
 | 
						|
/* stack size of task */
 | 
						|
#if (IOT_PSRAM_ENABLE)
 | 
						|
#define IOT_MODBUS_TASK_TASK_STACK_SIZE             (2048)
 | 
						|
#else
 | 
						|
#define IOT_MODBUS_TASK_TASK_STACK_SIZE             (512)
 | 
						|
#endif
 | 
						|
 | 
						|
#define IOT_MODBUS_TASK_PROTO_TASK_PRIO             (6)
 | 
						|
 | 
						|
/* number of messages pending in queue. */
 | 
						|
#if (IOT_PSRAM_ENABLE)
 | 
						|
#define IOT_MODBUS_TASK_TASK_POOL_SIZE              (128)
 | 
						|
#else
 | 
						|
#define IOT_MODBUS_TASK_TASK_POOL_SIZE              (32)
 | 
						|
#endif
 | 
						|
 | 
						|
#define IOT_MODBUS_TASK_GE_START_TIMER_PERIOD       (100) /* units:ms */
 | 
						|
/* should greater than IOT_UART_NO_FMT_TIMEOUT(default 100ms) */
 | 
						|
#define IOT_MODBUS_TASK_UART_MB_TIMER_PERIOD        (120) /* units:ms */
 | 
						|
 | 
						|
#define IOT_FN160_TYPE_MODBUS                       (0x01)
 | 
						|
 | 
						|
/* define uart recv buffer. */
 | 
						|
#define IOT_MODBUS_MAX_BUF_SIZE             (1024)
 | 
						|
 | 
						|
#pragma pack(push)  /* save the pack status */
 | 
						|
#pragma pack(1)     /* 1 byte align         */
 | 
						|
 | 
						|
typedef struct _iot_mb_task_t {
 | 
						|
    /* task handle */
 | 
						|
    iot_task_h          task;
 | 
						|
    /* uart handle */
 | 
						|
    iot_uart_h          uart_h;
 | 
						|
    /* timer handle */
 | 
						|
    timer_id_t          ge_start_timer;
 | 
						|
    timer_id_t          uart_timer;
 | 
						|
    timer_id_t          rt_update_timer;
 | 
						|
    timer_id_t          mb_ext_resp_timer;
 | 
						|
    /* local mac address. */
 | 
						|
    uint8_t             local_mac[IOT_MAC_ADDR_LEN];
 | 
						|
#if PLC_SUPPORT_STA_ROLE
 | 
						|
    /* dest cco mac */
 | 
						|
    uint8_t             dest_cco_mac[IOT_MAC_ADDR_LEN];
 | 
						|
#endif
 | 
						|
    /* local device slave address for modbus */
 | 
						|
    uint8_t             slave_addr;
 | 
						|
    /* buf to save uart modbus data */
 | 
						|
    uint8_t             uart_mb_buf[IOT_MODBUS_MAX_BUF_SIZE];
 | 
						|
    /* uart modbus len */
 | 
						|
    uint32_t            uart_mb_len;
 | 
						|
    /* buf to save ge modbus data */
 | 
						|
    uint8_t             ge_mb_buf[IOT_MODBUS_MAX_BUF_SIZE];
 | 
						|
    /* ge modbus len */
 | 
						|
    uint32_t            ge_mb_len;
 | 
						|
 | 
						|
#if IOT_MODBUS_RT_TABLE_SUPPORT
 | 
						|
    /* route table */
 | 
						|
    iot_modbus_addr_rout_table_h rt_table;
 | 
						|
#endif
 | 
						|
} iot_mb_task_t;
 | 
						|
 | 
						|
typedef struct _ge_fn160_appdata_mb_t {
 | 
						|
    uint8_t type;               /* trans proto type */
 | 
						|
    uint8_t src_mac[IOT_MAC_ADDR_LEN];
 | 
						|
    uint8_t frame_total : 4,    /* total frames */
 | 
						|
            frame_idx   : 4;    /* index current frame */
 | 
						|
    uint8_t data[0];            /* modbus raw data */
 | 
						|
} ge_fn160_appdata_mb_t;
 | 
						|
#pragma pack(pop)
 | 
						|
 | 
						|
/** modbus func code:
 | 
						|
## bit access
 | 
						|
0x02(2)         Read Input Discrete
 | 
						|
0x01(1)         Read Coils
 | 
						|
0x05(5)         Write Single Coil
 | 
						|
0x0F(15)        Write Multiple Coils
 | 
						|
## 16 bits access
 | 
						|
0x04(4)         Read Input Register
 | 
						|
0x03(3)         Read Multiple Registers
 | 
						|
0x06(6)         Write Single Register
 | 
						|
0x10(16)        Write Multiple Registers
 | 
						|
0x17(23)        Read/Write Multiple Registers
 | 
						|
0x16(22)        Mask Write Register
 | 
						|
## error function code
 | 
						|
the msb is 1 when above function code is error.
 | 
						|
*/
 | 
						|
typedef  struct _iot_mb_data_check_t {
 | 
						|
     uint8_t func_code;     /* modbus function code */
 | 
						|
     uint8_t min_len;       /* modbus min len */
 | 
						|
     uint8_t not_fix_len;   /* 0:use min_len;1:use min_len+append data len */
 | 
						|
     uint8_t byte_count_idx;/* append data len index */
 | 
						|
} iot_mb_data_check_t;
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_modbus_rtu_crc16() - Compute the MODBUS RTU CRC
 | 
						|
 * @param buf:  the pointer of data.
 | 
						|
 * @param len : data length.
 | 
						|
 * @return    : crc16 of the modbus rtu.
 | 
						|
 */
 | 
						|
uint16_t iot_modbus_rtu_crc16(uint8_t *buf, uint32_t len);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_modbus_fn160_appdata_pack() - pack fn160 appdata.
 | 
						|
 * @param mb_data: the pointer of data.
 | 
						|
 * @param mb_len : data length.
 | 
						|
 * @return    :  the packed data of fn160 appdata.
 | 
						|
 */
 | 
						|
iot_pkt_t *iot_modbus_fn160_appdata_pack(uint8_t *mb_data, uint32_t mb_len,
 | 
						|
    uint32_t curr_idx, uint32_t total_idx);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_mb_task_post_uart_cmd() - recveived command from uart, send to
 | 
						|
 *                                       common proto
 | 
						|
 * @param data: command data.
 | 
						|
 * @param len : command data length.
 | 
						|
 * @param param : other param.
 | 
						|
 * @return    : ERR_OK   - send successfully.
 | 
						|
 *              ERR_FAIL - send failed.
 | 
						|
 */
 | 
						|
uint8_t iot_mb_task_post_uart_cmd(uint8_t * data, uint16_t len, uint32_t param);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_mb_task_msg_post() - port pkt data.
 | 
						|
 * @param msg_type: message type.
 | 
						|
 * @param msg_id  : message id.
 | 
						|
 * @param data    : iot_pkt data will be post.
 | 
						|
 */
 | 
						|
void iot_mb_task_msg_post(uint16_t msg_type, uint16_t msg_id, iot_pkt_t *data);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief iot_mb_task_msg_post_to_uart() - send data to uart port.
 | 
						|
 * @param p_pkt_frame: pointer of pkt frame data buffer.
 | 
						|
 * @return ERR_OK
 | 
						|
 */
 | 
						|
uint32_t iot_mb_task_msg_post_to_uart(iot_uart_h uart_h, iot_pkt_t *p_pkt_frame);
 | 
						|
 | 
						|
/**
 | 
						|
 * @brief app_modbus_task_entry() - ge modbus app.
 | 
						|
 * @param none.
 | 
						|
 * @return: ERR_OK - successfully.
 | 
						|
 *   ERR_FAIL -      failed.
 | 
						|
 */
 | 
						|
uint32_t app_modbus_task_entry(void);
 | 
						|
 | 
						|
#ifdef __cplusplus
 | 
						|
}
 | 
						|
#endif
 | 
						|
 | 
						|
#endif /* _IOT_MODBUS_TASK_H_ */
 |