/* * File: modbus_master.h * Author: root * * Created on April 27, 2022, 4:02 PM */ /* * Simple MODBUS data polling master for PIC32MK on USART6 using Harmony 3 * the USART port uses interrupt driven buffered I/O * hard coded for data collection from a EM540 3-phase power monitor * https://www.gavazzionline.com/pdf/EM540_DS_ENG.pdf */ #ifndef MODBUS_MASTER_H #define MODBUS_MASTER_H #ifdef __cplusplus extern "C" { #endif #define MB_MASTER #define MB_EM540 #define SWMBMVER 0X0033 // master SW version #define MADDR 0x01 // modbus client address /* * fake good received bytes from client */ #define DBUG_R // true || /* * Display MODBUS debugging info screen */ //#define MODBUS_DEBUG #include "vcan.h" #include #include #include #include #define MAX_DATA 128 //#define LOCAL_ECHO 1 #define FASTQ // MODBUS query speed #define TDELAY 2 // half-duplex delay #define RDELAY 300 // receive timeout #define CDELAY 40 // fast query delay #define QDELAY 1 // query delay #define TODELAY 4 // misc delay #define SPACING 50 // control loop cpu usage factor /* * RS485 port defines * port is configured in harmony3 for PIC32MK */ #define Swrite UART6_Write #define Strmt U6STA&_U6STA_TRMT_MASK #define Serror UART6_ErrorGet #define Sread UART6_Read /* * serial communications states */ typedef enum comm_type { CLEAR = 0, INIT, SEND, RECV, } comm_type; typedef enum cmd_type { G_ID = 0, G_DATA, G_CONFIG, // keep sequence G_PASSWD, // keep sequence G_LAST, } cmd_type; typedef enum trace_type { T_begin = 1, T_clear, T_passwd, T_config, T_data, T_id, T_init, T_init_d, T_send, T_send_d, T_recv, T_recv_r, T_misc1, T_misc2, T_misc3, T_spacing, } trace_type; union PWMDC { uint16_t lpwm; uint8_t bpwm[2]; }; union MREG { int16_t value; char bytes[2]; }; union MREG32 { int32_t value; char bytes[4]; }; typedef struct C_data { // client state machine data uint8_t mcmd; comm_type cstate; cmd_type modbus_command; uint16_t req_length; uint8_t trace; bool id_ok, passwd_ok, config_ok, data_ok; uint32_t data_count, data_prev; } C_data; typedef struct M_data { // ISR used, mainly for non-atomic mod problems uint32_t clock_500hz; uint32_t clock_10hz; uint32_t clock_2hz; uint8_t clock_blinks; uint8_t num_blinks; uint8_t blink_lock : 1; uint8_t config : 1; uint8_t stable : 1; uint8_t boot_code : 1; uint8_t power_on : 1; uint8_t send_count, recv_count, pwm_volts; uint16_t error, crc_data, crc_calc; uint32_t crc_error; uint32_t to_error; uint32_t sends; } M_data; typedef struct EM_data { int32_t vl1n, vl2n, vl3n, vl1l2, vl2l3, vl3l1, al1, al2, al3, wl1, wl2, wl3; } EM_data; // code from libmodbus: https://raw.githubusercontent.com/stephane/libmodbus/master/src/modbus-rtu.c /* Table of CRC values for high-order byte */ static const uint8_t table_crc_hi[] = { 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 }; /* Table of CRC values for low-order byte */ static const uint8_t table_crc_lo[] = { 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, 0x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 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; /*******************************ModBus Functions*******************************/ #define READ_COILS 1 #define READ_DISCRETE_INPUTS 2 #define READ_HOLDING_REGISTERS 3 #define READ_INPUT_REGISTERS 4 #define WRITE_SINGLE_COIL 5 #define WRITE_SINGLE_REGISTER 6 #define WRITE_MULTIPLE_COILS 15 #define WRITE_MULTIPLE_REGISTERS 16 #define BOFF 0 #define BON 255 #define MM_ERROR_S BSP_LED1_Set() #define MM_ERROR_C BSP_LED1_Clear() uint16_t crc16(volatile uint8_t *, uint16_t); uint16_t modbus_rtu_send_msg(void *, const void *, uint16_t); void my_modbus_rx_32(UART_EVENT, uintptr_t); uint8_t init_stream_params(void); void init_mb_master_timers(void); int8_t master_controller_work(C_data *); int32_t mb32_swap(int32_t); void clear_2hz(void); void clear_10hz(void); void clear_500hz(void); uint32_t get_2hz(uint8_t); uint32_t get_10hz(uint8_t); uint32_t get_500hz(uint8_t); bool set_led_blink(uint8_t); void timer_500ms_tick(uint32_t, uintptr_t); void timer_2ms_tick(uint32_t, uintptr_t); extern volatile struct V_type V; extern C_data C; // MODBUS client state data extern volatile M_data M; extern EM_data em; #ifdef __cplusplus } #endif #endif /* MODBUS_MASTER_H */