serial_protocol.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * serial_protocol.c
  3. *
  4. * Created on: Aug 16, 2024
  5. * Author: jakubski
  6. */
  7. #include <stdint.h>
  8. #include <string.h>
  9. #include "serial_protocol.h"
  10. #include <main.h>
  11. extern CRC_HandleTypeDef hcrc;
  12. void WriteShortWordToBuffer (uint8_t* buff, uint8_t* buffPos, uint16_t data) {
  13. uint8_t i = 0;
  14. uint8_t newBuffPos = *buffPos;
  15. for (i = 0; i < sizeof (data); i++) {
  16. buff[newBuffPos++] = (uint8_t)((data >> (i * 8)) & 0xFF);
  17. }
  18. *buffPos = newBuffPos;
  19. }
  20. void WriteWordToBuffer (uint8_t* buff, uint8_t* buffPos, uint32_t data) {
  21. uint8_t i = 0;
  22. uint8_t newBuffPos = *buffPos;
  23. for (i = 0; i < sizeof (data); i++) {
  24. buff[newBuffPos++] = (uint8_t)((data >> (i * 8)) & 0xFF);
  25. }
  26. *buffPos = newBuffPos;
  27. }
  28. void WriteFloatToBuffer (uint8_t* buff, uint8_t* buffPos, float data) {
  29. uint32_t* uDataPtr = (uint32_t*)&data;
  30. uint32_t uData = *uDataPtr;
  31. uint8_t i = 0;
  32. uint8_t newBuffPos = *buffPos;
  33. for (i = 0; i < sizeof (uData); i++) {
  34. buff[newBuffPos++] = (uint8_t)((uData >> (i * 8)) & 0xFF);
  35. }
  36. *buffPos = newBuffPos;
  37. }
  38. void WriteDataToBuffer (uint8_t* buff, uint16_t* buffPos, void* data, uint8_t dataSize) {
  39. uint32_t* uDataPtr = data;
  40. uint32_t uData = *uDataPtr;
  41. uint8_t i = 0;
  42. uint8_t newBuffPos = *buffPos;
  43. for (i = 0; i < dataSize; i++) {
  44. buff[newBuffPos++] = (uint8_t)((uData >> (i * 8)) & 0xFF);
  45. }
  46. *buffPos = newBuffPos;
  47. }
  48. void ReadFloatFromBuffer(uint8_t* buff, uint16_t* buffPos, float* data)
  49. {
  50. uint32_t* word = (uint32_t *)data;
  51. *word = CONVERT_BYTES_TO_WORD(&buff[*buffPos]);
  52. *buffPos += sizeof(float);
  53. }
  54. void ReadShortWordFromBufer(uint8_t* buff, uint16_t* buffPos, uint16_t* data)
  55. {
  56. *data = CONVERT_BYTES_TO_SHORT_WORD(&buff[*buffPos]);
  57. *buffPos += sizeof(uint16_t);
  58. }
  59. void ReadWordFromBufer(uint8_t* buff, uint16_t* buffPos, uint32_t* data)
  60. {
  61. *data = CONVERT_BYTES_TO_WORD(&buff[*buffPos]);
  62. *buffPos += sizeof(uint32_t);
  63. }
  64. void ReadByteFromBufer(uint8_t* buff, uint16_t* buffPos, uint8_t* data)
  65. {
  66. *data = CONVERT_BYTES_TO_SHORT_WORD(&buff[*buffPos]);
  67. *buffPos += sizeof(uint8_t);
  68. }
  69. uint16_t PrepareReqFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, uint8_t* dataBuffer, uint16_t dataLength) {
  70. uint16_t crc = 0;
  71. uint16_t txBufferPos = 0;
  72. uint16_t frameCmd = ((uint16_t)frameCommand);
  73. memset (txBuffer, 0x00, dataLength);
  74. txBuffer[txBufferPos++] = FRAME_INDICATOR;
  75. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameId);
  76. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameId);
  77. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameCmd);
  78. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameCmd);
  79. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (dataLength);
  80. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (dataLength);
  81. txBuffer[txBufferPos++] = 0x00;
  82. if (dataLength > 0) {
  83. memcpy (&txBuffer[txBufferPos], dataBuffer, dataLength);
  84. txBufferPos += dataLength;
  85. }
  86. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)txBuffer, txBufferPos);
  87. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (crc);
  88. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (crc);
  89. return txBufferPos;
  90. }
  91. uint16_t PrepareRespFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, SerialProtocolRespStatus respStatus, uint8_t* dataBuffer, uint16_t dataLength) {
  92. uint16_t crc = 0;
  93. uint16_t txBufferPos = 0;
  94. uint16_t frameCmd = ((uint16_t)frameCommand) | 0x8000; // MSB set means response
  95. memset (txBuffer, 0x00, dataLength);
  96. txBuffer[txBufferPos++] = FRAME_INDICATOR;
  97. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameId);
  98. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameId);
  99. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameCmd);
  100. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameCmd);
  101. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (dataLength);
  102. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (dataLength);
  103. txBuffer[txBufferPos++] = (uint8_t)respStatus;
  104. if (dataLength > 0) {
  105. memcpy (&txBuffer[txBufferPos], dataBuffer, dataLength);
  106. txBufferPos += dataLength;
  107. }
  108. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)txBuffer, txBufferPos);
  109. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (crc);
  110. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (crc);
  111. return txBufferPos;
  112. }