ublox_device.h 3.9 KB

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