ublox_device.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // This is a personal academic project. Dear PVS-Studio, please check it.
  2. // PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
  3. #pragma once
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <stdbool.h>
  7. #include <furi.h>
  8. #define UBLOX_I2C_ADDRESS 0x42
  9. #define I2C_TIMEOUT_MS 20
  10. #define UBX_NAV_CLASS 0x01
  11. #define UBX_RXM_CLASS 0x02
  12. #define UBX_INF_CLASS 0x04
  13. #define UBX_ACK_CLASS 0x05
  14. #define UBX_CFG_CLASS 0x06
  15. #define UBX_UPD_CLASS 0x09
  16. #define UBX_MON_CLASS 0x0A
  17. #define UBX_AID_CLASS 0x0B
  18. #define UBX_TIM_CLASS 0x0D
  19. #define UBX_ESF_CLASS 0x10
  20. #define UBX_MGA_CLASS 0x13
  21. #define UBX_LOG_CLASS 0x21
  22. #define UBX_SEC_CLASS 0x27
  23. #define UBX_HNR_CLASS 0x28
  24. // The following are respective to a class.
  25. // ACK_CLASS
  26. #define UBX_ACK_ACK_MESSAGE 0x01
  27. // ACK and NAK have the same length
  28. #define UBX_ACK_ACK_MESSAGE_LENGTH (8+2)
  29. // NAV_CLASS
  30. #define UBX_NAV_PVT_MESSAGE 0x07
  31. #define UBX_NAV_PVT_MESSAGE_LENGTH (8+92)
  32. #define UBX_NAV_SAT_MESSAGE 0x35
  33. #define UBX_NAV_ODO_MESSAGE 0x09
  34. #define UBX_NAV_ODO_MESSAGE_LENGTH (8+20)
  35. #define UBX_NAV_RESETODO_MESSAGE 0x10
  36. // CFG_CLASS
  37. #define UBX_CFG_PMS_MESSAGE 0x86
  38. #define UBX_CFG_PMS_MESSAGE_LENGTH (8+8)
  39. #define UBX_CFG_ODO_MESSAGE 0x1e
  40. #define UBX_CFG_ODO_MESSAGE_LENGTH (8+20)
  41. #define UBX_CFG_NAV5_MESSAGE 0x24
  42. #define UBX_CFG_NAV5_MESSAGE_LENGTH (8+36)
  43. /** A frame is a message sent to the GPS. This app supports u-blox 8 devices. */
  44. typedef struct UbloxFrame {
  45. uint8_t sync1; // always 0xb5, or ISO-8859.1 for 'µ'
  46. uint8_t sync2; // always 0x62, or ASCII for 'b'
  47. // class and id together indicate what kind of message is being sent.
  48. uint8_t class; // message class
  49. uint8_t id; // message id
  50. uint16_t len; // length of the payload only, 2 bytes, little-endian
  51. uint8_t* payload; // any number of bytes
  52. // 2 bytes of checksum
  53. uint8_t ck_a;
  54. uint8_t ck_b;
  55. // metadata
  56. bool valid;
  57. } UbloxFrame;
  58. typedef struct UbloxMessage {
  59. uint8_t* message;
  60. uint8_t length;
  61. } UbloxMessage;
  62. // Field names taken directly from u-blox protocol manual.
  63. typedef struct Ublox_NAV_PVT_Message {
  64. uint32_t iTOW;
  65. uint16_t year;
  66. uint8_t month;
  67. uint8_t day;
  68. uint8_t hour;
  69. uint8_t min;
  70. uint8_t sec;
  71. uint8_t valid;
  72. uint32_t tAcc;
  73. int32_t nano;
  74. uint8_t fixType;
  75. uint8_t flags;
  76. uint8_t flags2;
  77. uint8_t numSV;
  78. int32_t lon;
  79. int32_t lat;
  80. int32_t height;
  81. int32_t hMSL;
  82. uint32_t hAcc;
  83. uint32_t vAcc;
  84. int32_t velN;
  85. int32_t velE;
  86. int32_t velD;
  87. int32_t gSpeed;
  88. int32_t headMot;
  89. uint32_t sAcc;
  90. uint32_t headAcc;
  91. uint16_t pDOP;
  92. uint16_t flags3;
  93. uint8_t reserved1;
  94. uint8_t reserved2;
  95. uint8_t reserved3;
  96. uint8_t reserved4;
  97. int32_t headVeh;
  98. int16_t magDec;
  99. uint16_t magAcc;
  100. } Ublox_NAV_PVT_Message;
  101. typedef struct Ublox_NAV_ODO_Message {
  102. uint8_t version;
  103. uint8_t reserved1;
  104. uint8_t reserved2;
  105. uint8_t reserved3;
  106. uint32_t iTOW;
  107. uint32_t distance;
  108. uint32_t totalDistance;
  109. uint32_t distanceStd;
  110. } Ublox_NAV_ODO_Message;
  111. /** For a given UbloxFrame, populate the sync bytes, calculate the
  112. * checksum bytes (storing them in `frame`), allocate a uint8_t array,
  113. * and fill it with the contents of the frame in an order ready to
  114. * send to the GPS. */
  115. UbloxMessage* ublox_frame_to_bytes(UbloxFrame* frame);
  116. /** For an array of uint8_ts, convert them to a frame. Returns NULL if
  117. something goes wrong, either with invalid data or malloc errors.*/
  118. UbloxFrame* ublox_bytes_to_frame(UbloxMessage* message);
  119. void ublox_message_free(UbloxMessage* message);
  120. void ublox_frame_free(UbloxFrame* frame);