schrader.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license.
  3. *
  4. * Schrader TPMS. Usually 443.92 Mhz OOK, 120us pulse len.
  5. *
  6. * 500us high pulse + Preamble + Manchester coded bits where:
  7. * 1 = 10
  8. * 0 = 01
  9. *
  10. * 60 bits of data total (first 4 nibbles is the preamble, 0xF).
  11. *
  12. * Used in FIAT-Chrysler, Mercedes, ... */
  13. #include "../../app.h"
  14. #define USE_TEST_VECTOR 0
  15. static const char* test_vector =
  16. "000000111101010101011010010110010110101001010110100110011001100101010101011010100110100110011010101010101010101010101010101010101010101010101010";
  17. static bool decode(uint8_t* bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo* info) {
  18. if(USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
  19. bitmap_set_pattern(bits, numbytes, 0, test_vector);
  20. numbits = strlen(test_vector);
  21. }
  22. if(numbits < 64) return false; /* Preamble + data. */
  23. const char* sync_pattern = "1111010101"
  24. "01011010";
  25. uint64_t off = bitmap_seek_bits(bits, numbytes, 0, numbits, sync_pattern);
  26. if(off == BITMAP_SEEK_NOT_FOUND) return false;
  27. FURI_LOG_E(TAG, "Schrader TPMS gap+preamble found");
  28. info->start_off = off;
  29. off += 10; /* Skip just the long pulse and the first 3 bits of sync, so
  30. that we have the first byte of data with the sync nibble
  31. 0011 = 0x3. */
  32. uint8_t raw[8];
  33. uint8_t id[4];
  34. uint32_t decoded = convert_from_line_code(
  35. raw, sizeof(raw), bits, numbytes, off, "01", "10"); /* Manchester code. */
  36. FURI_LOG_E(TAG, "Schrader TPMS decoded bits: %lu", decoded);
  37. if(decoded < 64) return false; /* Require the full 8 bytes. */
  38. raw[0] |= 0xf0; // Fix the preamble nibble for checksum computation.
  39. uint8_t cksum = crc8(raw, sizeof(raw) - 1, 0xf0, 0x7);
  40. if(cksum != raw[7]) {
  41. FURI_LOG_E(TAG, "Schrader TPMS checksum mismatch");
  42. return false;
  43. }
  44. info->pulses_count = (off + 8 * 8 * 2) - info->start_off;
  45. float kpa = (float)raw[5] * 2.5;
  46. int temp = raw[6] - 50;
  47. id[0] = raw[1] & 7;
  48. id[1] = raw[2];
  49. id[2] = raw[3];
  50. id[3] = raw[4];
  51. fieldset_add_bytes(info->fieldset, "Tire ID", id, 4 * 2);
  52. fieldset_add_float(info->fieldset, "Pressure kpa", kpa, 2);
  53. fieldset_add_int(info->fieldset, "Temperature C", temp, 8);
  54. return true;
  55. }
  56. ProtoViewDecoder SchraderTPMSDecoder =
  57. {.name = "Schrader TPMS", .decode = decode, .get_fields = NULL, .build_message = NULL};