10#define PETITMODBUS_READ_COILS (1)
11#define PETITMODBUS_READ_DISCRETE_INPUTS (2)
12#define PETITMODBUS_READ_HOLDING_REGISTERS (3)
13#define PETITMODBUS_READ_INPUT_REGISTERS (4)
14#define PETITMODBUS_WRITE_SINGLE_COIL (5)
15#define PETITMODBUS_WRITE_SINGLE_REGISTER (6)
16#define PETITMODBUS_WRITE_MULTIPLE_COILS (15)
17#define PETITMODBUS_WRITE_MULTIPLE_REGISTERS (16)
20#define PETIT_FALSE_FUNCTION (0)
21#define PETIT_FALSE_SLAVE_ADDRESS (1)
22#define PETIT_DATA_NOT_READY (2)
23#define PETIT_DATA_READY (3)
25#define PETIT_ERROR_CODE_01 (0x01)
26#define PETIT_ERROR_CODE_02 (0x02)
27#define PETIT_ERROR_CODE_03 (0X03)
28#define PETIT_ERROR_CODE_04 (0x04)
30#define PETIT_BUF_FN_CODE_I (1)
31#define PETIT_BUF_BYTE_CNT_I (6)
36#define PETIT_BUF_DAT_M(Idx) (((pu16_t)(PetitBuffer[2*(Idx) + 2]) << 8) \
37 | (pu16_t) (PetitBuffer[2*(Idx) + 3]))
125#if PETIT_CRC == PETIT_CRC_TABULAR
132#elif PETIT_CRC == PETIT_CRC_BITWISE
138 for (i = 8; i > 0; i--)
146#elif PETIT_CRC == PETIT_CRC_EXTERNAL
147#define Petit_CRC16_Calc(Data) PetitPortCRC16Calc((Data), &Petit_CRC16)
149#error "No Valid CRC Algorithm!"
187#if PETITMODBUS_READ_COILS_ENABLED > 0
188void HandlePetitModbusReadCoils(
void)
190 pu16_t Petit_StartCoil = 0;
191 pu16_t Petit_NumberOfCoils = 0;
206 if ((Petit_StartCoil + Petit_NumberOfCoils)
209 Petit_NumberOfCoils == 0)
215 pu8_t Petit_CurrentData = 0;
222 for (Petit_i = 0; Petit_i < Petit_NumberOfCoils; Petit_i++)
224 pu8_t Petit_CurrentBit;
225 if ((Petit_i & 7) == 0)
228 Petit_CurrentData = 0;
230#if defined(PETIT_COIL) && \
231 (PETIT_COIL == PETIT_INTERNAL || PETIT_COIL == PETIT_BOTH)
234 (PetitCoils[Petit_StartCoil + Petit_i >> 3]
235 & 1 << (Petit_StartCoil + Petit_i & 7)) != 0;
236 Petit_CurrentData |= (
pu8_t) Petit_CurrentBit << (Petit_i & 7);
238#if defined(PETIT_COIL) && \
239 (PETIT_COIL == PETIT_EXTERNAL || PETIT_COIL == PETIT_BOTH)
246 Petit_CurrentData |= (
pu8_t) (Petit_CurrentBit != 0) << (Petit_i & 7);
261#if PETITMODBUS_READ_HOLDING_REGISTERS_ENABLED > 0
262void HandlePetitModbusReadHoldingRegisters(
void)
267 pu16_t Petit_StartAddress = 0;
268 pu16_t Petit_NumberOfRegisters = 0;
276 if ((Petit_StartAddress + Petit_NumberOfRegisters)
287 for (Petit_i = 0; Petit_i < Petit_NumberOfRegisters; Petit_i++)
290#if defined(PETIT_REG) && \
291 (PETIT_REG == PETIT_INTERNAL || PETIT_REG == PETIT_BOTH)
292 Petit_CurrentData = PetitRegisters[Petit_StartAddress
295#if defined(PETIT_REG) && PETIT_REG == PETIT_EXTERNAL
303 (
pu8_t) ((Petit_CurrentData & 0xFF00) >> 8);
305 (
pu8_t) (Petit_CurrentData & 0xFF);
322#if PETITMODBUS_READ_INPUT_REGISTERS_ENABLED > 0
323void HandlePetitModbusReadInputRegisters(
void)
325 pu16_t Petit_StartAddress = 0;
326 pu16_t Petit_NumberOfRegisters = 0;
334 if ((Petit_StartAddress + Petit_NumberOfRegisters)
345 for (Petit_i = 0; Petit_i < Petit_NumberOfRegisters; Petit_i++)
348#if defined(PETIT_INPUT_REG) && \
349 (PETIT_INPUT_REG == PETIT_INTERNAL ||\
350 PETIT_INPUT_REG == PETIT_BOTH)
351 Petit_CurrentData = PetitInputRegisters[Petit_StartAddress
354#if defined(PETIT_INPUT_REG) && PETIT_INPUT_REG == PETIT_EXTERNAL
355 if (!PetitPortInputRegRead(Petit_StartAddress + Petit_i, &Petit_CurrentData))
362 (
pu8_t) ((Petit_CurrentData & 0xFF00) >> 8);
364 (
pu8_t) (Petit_CurrentData & 0xFF);
378#if PETITMODBUS_WRITE_SINGLE_COIL_ENABLED > 0
379void HandlePetitModbusWriteSingleCoil(
void)
395 else if (Petit_Value != 0x0000 && Petit_Value != 0xFF00)
399#if defined(PETIT_COIL) && \
400 (PETIT_COIL == PETIT_INTERNAL || PETIT_COIL == PETIT_BOTH)
402 PetitCoils[Petit_Address >> 3] |= 1 << (Petit_Address & 7u);
404 PetitCoils[Petit_Address >> 3] &= ~(1 << (Petit_Address & 7u));
406#if defined(PETIT_COIL) && PETIT_COIL == PETIT_EXTERNAL
425#if PETITMODBUS_WRITE_SINGLE_REGISTER_ENABLED > 0
426void HandlePetitModbusWriteSingleRegister(
void)
445#if defined(PETIT_REG) && \
446 (PETIT_REG == PETIT_INTERNAL || PETIT_REG == PETIT_BOTH)
447 PetitRegisters[Petit_Address] = Petit_Value;
449#if defined(PETIT_REG) && PETIT_REG == PETIT_EXTERNAL
468#if PETITMODBUS_WRITE_MULTIPLE_COILS_ENABLED > 0
469void HandlePetitModbusWriteMultipleCoils(
void)
472 pu16_t Petit_StartCoil = 0;
473 pu8_t Petit_ByteCount = 0;
474 pu16_t Petit_NumberOfCoils = 0;
476 pu8_t Petit_CurrentBit = 0;
484 if ((Petit_StartCoil + Petit_NumberOfCoils)
487 else if (Petit_NumberOfCoils > (255 - 9) * 8 || Petit_NumberOfCoils == 0)
495 for (Petit_i = 0; Petit_i < Petit_NumberOfCoils; Petit_i++)
498 Petit_CurrentBit = (
PetitBuffer[(Petit_i >> 3) + 7]
499 & 1 << (Petit_i & 7))
501#if defined(PETIT_REG) && \
502 (PETIT_REG == PETIT_INTERNAL || PETIT_REG == PETIT_BOTH)
503 if (Petit_CurrentBit)
504 PetitCoils[Petit_StartCoil + Petit_i >> 3] |=
505 1 << (Petit_StartCoil + Petit_i & 7);
507 PetitCoils[Petit_StartCoil + Petit_i >> 3] &=
508 ~(1 << (Petit_StartCoil + Petit_i & 7));
510#if defined(PETIT_REG) && PETIT_REG == PETIT_EXTERNAL
528#if PETITMODBUS_WRITE_MULTIPLE_REGISTERS_ENABLED > 0
529void HandlePetitModbusWriteMultipleRegisters(
void)
532 pu16_t Petit_StartAddress = 0;
533 pu8_t Petit_ByteCount = 0;
534 pu16_t Petit_NumberOfRegisters = 0;
544 if ((Petit_StartAddress + Petit_NumberOfRegisters)
554 for (Petit_i = 0; Petit_i < Petit_NumberOfRegisters; Petit_i++)
559#if defined(PETIT_REG) && \
560 (PETIT_REG == PETIT_INTERNAL || PETIT_REG == PETIT_BOTH)
561 PetitRegisters[Petit_StartAddress + Petit_i] = Petit_Value;
563#if defined(PETIT_REG) && PETIT_REG == PETIT_EXTERNAL
633 pu8_t Petit_ReceiveBufferControl = 0;
645 for (Petit_i = 0; Petit_i <
PetitBufJ; ++Petit_i)
701#if PETITMODBUS_READ_COILS_ENABLED > 0
703 HandlePetitModbusReadCoils();
706#if PETITMODBUS_READ_HOLDING_REGISTERS_ENABLED > 0
708 HandlePetitModbusReadHoldingRegisters();
711#if PETITMODBUS_READ_INPUT_REGISTERS_ENABLED > 0
713 HandlePetitModbusReadInputRegisters();
716#if PETITMODBUS_WRITE_SINGLE_COIL_ENABLED > 0
718 HandlePetitModbusWriteSingleCoil();
721#if PETITMODBUS_WRITE_SINGLE_REGISTER_ENABLED > 0
723 HandlePetitModbusWriteSingleRegister();
726#if PETITMODBUS_WRITE_MULTIPLE_COILS_ENABLED > 0
728 HandlePetitModbusWriteMultipleCoils();
731#if PETITMODBUS_WRITE_MULTIPLE_REGISTERS_ENABLED > 0
733 HandlePetitModbusWriteMultipleRegisters();
754#if PETITMODBUS_PROCESS_POSITION >= 1
780#if PETITMODBUS_PROCESS_POSITION <= 0
792#if !defined(PETIT_COIL) || (PETIT_COIL != PETIT_INTERNAL && \
793 PETIT_COIL != PETIT_BOTH && PETIT_COIL != PETIT_EXTERNAL)
794#error "PETIT_COIL not defined or not valid."
797#if !defined(PETIT_REG) || (PETIT_REG != PETIT_INTERNAL && \
798 PETIT_REG != PETIT_BOTH && PETIT_REG != PETIT_EXTERNAL)
799#error "PETIT_REG not defined or not valid."
802#if !defined(PETIT_INPUT_REG) || (PETIT_INPUT_REG != PETIT_INTERNAL && \
803 PETIT_INPUT_REG != PETIT_BOTH && PETIT_INPUT_REG != PETIT_EXTERNAL)
804#error "PETTI_INPUT_REG not defined or not valid."
PETIT_CODE const pu16_t PetitCRCtable[256]
#define PETITMODBUS_READ_HOLDING_REGISTERS
#define PETITMODBUS_WRITE_SINGLE_COIL
#define PETIT_FALSE_FUNCTION
#define PETITMODBUS_READ_COILS
pu8_t PetitBuffer[PETITMODBUS_RXTX_BUFFER_SIZE]
pu16_t PetitExpectedReceiveCount
void HandlePetitModbusError(pu8_t ErrorCode)
void Petit_CRC16_Calc(const pu16_t Data)
#define PETIT_ERROR_CODE_02
#define PETIT_DATA_NOT_READY
pb_t PetitRxBufferInsert(pu8_t rcvd)
pb_t PetitTxBufferPop(pu8_t *tx)
void Petit_ResponseProcess(void)
PETIT_RXTX_STATE Petit_RxTx_State
#define PETIT_BUF_DAT_M(Idx)
void ProcessPetitModbus(void)
#define PETITMODBUS_WRITE_MULTIPLE_COILS
#define PETIT_ERROR_CODE_01
#define PETIT_ERROR_CODE_04
pu8_t CheckPetitModbusBufferComplete(void)
#define PETIT_ERROR_CODE_03
#define PETITMODBUS_WRITE_SINGLE_REGISTER
pb_t PetitSendMessage(void)
void PetitRxBufferReset()
#define PETIT_BUF_BYTE_CNT_I
#define PETITMODBUS_READ_INPUT_REGISTERS
#define PETIT_BUF_FN_CODE_I
#define PETIT_FALSE_SLAVE_ADDRESS
#define PETITMODBUS_WRITE_MULTIPLE_REGISTERS
#define PETITMODBUS_RXTX_BUFFER_SIZE
#define PetitLedErrFail()
#define PetitLedCrcFail()
unsigned char PetitRegChange
uint8_t PETITMODBUS_SLAVE_ADDRESS
pb_t PetitPortCoilRead(pu16_t Address, pu8_t *Data)
#define NUMBER_OF_PETITREGISTERS
void PetitPortTimerStart(void)
pb_t PetitPortCoilWrite(pu16_t Address, pu16_t Data)
pb_t PetitPortRegWrite(pu8_t Address, pu16_t Data)
uint8_t PETITMODBUS_DLY_TOP
pb_t PetitPortRegRead(pu8_t Address, pu16_t *Data)
#define NUMBER_OF_INPUT_PETITREGISTERS
void PetitPortTxBegin(pu8_t tx)
#define NUMBER_OF_REGISTERS_IN_BUFFER
void PetitPortTimerStop(void)