serial_protocol.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. uint16_t PrepareReqFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, uint8_t* dataBuffer, uint16_t dataLength) {
  55. uint16_t crc = 0;
  56. uint16_t txBufferPos = 0;
  57. uint16_t frameCmd = ((uint16_t)frameCommand);
  58. memset (txBuffer, 0x00, dataLength);
  59. txBuffer[txBufferPos++] = FRAME_INDICATOR;
  60. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameId);
  61. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameId);
  62. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameCmd);
  63. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameCmd);
  64. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (dataLength);
  65. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (dataLength);
  66. txBuffer[txBufferPos++] = 0x00;
  67. if (dataLength > 0) {
  68. memcpy (&txBuffer[txBufferPos], dataBuffer, dataLength);
  69. txBufferPos += dataLength;
  70. }
  71. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)txBuffer, txBufferPos);
  72. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (crc);
  73. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (crc);
  74. return txBufferPos;
  75. }
  76. uint16_t PrepareRespFrame (uint8_t* txBuffer, uint16_t frameId, SerialProtocolCommands frameCommand, SerialProtocolRespStatus respStatus, uint8_t* dataBuffer, uint16_t dataLength) {
  77. uint16_t crc = 0;
  78. uint16_t txBufferPos = 0;
  79. uint16_t frameCmd = ((uint16_t)frameCommand) | 0x8000; // MSB set means response
  80. memset (txBuffer, 0x00, dataLength);
  81. txBuffer[txBufferPos++] = FRAME_INDICATOR;
  82. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameId);
  83. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameId);
  84. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (frameCmd);
  85. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (frameCmd);
  86. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (dataLength);
  87. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (dataLength);
  88. txBuffer[txBufferPos++] = (uint8_t)respStatus;
  89. if (dataLength > 0) {
  90. memcpy (&txBuffer[txBufferPos], dataBuffer, dataLength);
  91. txBufferPos += dataLength;
  92. }
  93. crc = HAL_CRC_Calculate (&hcrc, (uint32_t*)txBuffer, txBufferPos);
  94. txBuffer[txBufferPos++] = GET_SHORT_WORD_FIRST_BYTE (crc);
  95. txBuffer[txBufferPos++] = GET_SHORT_WORD_SECOND_BYTE (crc);
  96. return txBufferPos;
  97. }