serial_protocol.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * serial_protocol.h
  3. *
  4. * Created on: Aug 15, 2024
  5. * Author: jakubski
  6. */
  7. #ifndef INC_SERIAL_PROTOCOL_H_
  8. #define INC_SERIAL_PROTOCOL_H_
  9. #include "node-red-config.h"
  10. /*
  11. * Request frame
  12. * |AA|ID0|ID1|CMD0|CMD1|LEN0|LEN1|0x00|DATA0|..|DATAn|CRC0|CRC1|
  13. *
  14. * Response frame
  15. * |AA|ID0|ID1|CMD0|CMD1|LEN0|LEN1|RESPSTAT|DATA0|..|DATAn|CRC0|CRC1|
  16. * MSB set in CMD1
  17. */
  18. #define FRAME_INDICATOR 0xAA
  19. #define FRAME_HEADER_LENGTH 8
  20. #define FRAME_DATALEN_LENGTH sizeof (uint16_t)
  21. #define FRAME_COMMAND_LENGTH sizeof (uint16_t)
  22. #define FRAME_ID_LENGTH sizeof (uint16_t)
  23. #define FRAME_RESP_STAT_LENGTH sizeof (uint8_t)
  24. #define FRAME_CRC_LENGTH sizeof (uint16_t)
  25. #define FRAME_TIMEOUT_MS 2000
  26. enum _SerialProtocolCommands {
  27. spGetElectricalMeasurments,
  28. spGetSensorMeasurments,
  29. spSetFanSpeed,
  30. spSetMotorXOn,
  31. spSetMotorYOn,
  32. spSetmotorXMaxCurrent,
  33. spSetmotorYMaxCurrent,
  34. spSetDiodeOn,
  35. spClearPeakMeasurments,
  36. spSetEncoderXValue,
  37. spSetEncoderYValue,
  38. spSetVoltageMeasGains,
  39. spSetVoltageMeasOffsets,
  40. spSetCurrentMeasGains,
  41. spSetCurrentMeasOffsets,
  42. spResetSystem,
  43. spSetPositonX,
  44. spSetPositonY,
  45. spUnknown
  46. };
  47. typedef enum _SerialProtocolCommands SerialProtocolCommands;
  48. enum _SerialProtocolRespStatus { spInternalError = -4, spUnknownCommand = -3, spCrcFail = -2, spTimeout, spOK };
  49. typedef enum _SerialProtocolRespStatus SerialProtocolRespStatus;
  50. #define GET_SHORT_WORD_FIRST_BYTE(cmd) ((uint8_t)(cmd & 0xFF))
  51. #define GET_SHORT_WORD_SECOND_BYTE(cmd) ((uint8_t)((cmd >> 8) & 0xFF))
  52. #define GET_WORD_FIRST_BYTE(cmd) ((uint8_t)(cmd & 0xFF))
  53. #define GET_WORD_SECOND_BYTE(cmd) ((uint8_t)((cmd >> 8) & 0xFF))
  54. #define GET_WORD_THIRD_BYTE(cmd) ((uint8_t)((cmd >> 16) & 0xFF))
  55. #define GET_WORD_FOURTH_BYTE(cmd) ((uint8_t)((cmd >> 24) & 0xFF))
  56. #define CONVERT_BYTES_TO_SHORT_WORD(buffer) ((uint16_t)((*(buffer + 1) << 8) | *buffer))
  57. #define CONVERT_BYTES_TO_WORD(buffer) ((uint32_t)((*(buffer + 3) << 24) | (*(buffer + 2) << 16) | (*(buffer + 1) << 8) | *buffer))
  58. void WriteDataToBuffer (uint8_t* buff, uint16_t* buffPos, void* data, uint8_t dataSize);
  59. void ReadFloatFromBuffer(uint8_t* buff, uint16_t* buffPos, float* data);
  60. void ReadShortWordFromBufer(uint8_t* buff, uint16_t* buffPos, uint16_t* data);
  61. void ReadWordFromBufer(uint8_t* buff, uint16_t* buffPos, uint32_t* data);
  62. void ReadByteFromBufer(uint8_t* buff, uint16_t* buffPos, uint8_t* data);
  63. uint16_t PrepareReqFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, uint8_t* dataBuffer,uint16_t dataLength);
  64. uint16_t PrepareRespFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, SerialProtocolRespStatus respStatus, uint8_t* dataBuffer, uint16_t dataLength);
  65. #endif /* INC_SERIAL_PROTOCOL_H_ */