Please note that the Chinese and Japanese versions are currently being updated and are not yet complete. Stay tuned for the final versions!

TLV API

通过API通信的TLV API使用以下格式的简单TLV帧。

TLV框架

类型

长度

价值

校验和

第1字节

第二字节

  • 无校验和的有效载荷长度

有效负载字节数从第3位到最大值

  • SPI上最大252字节

  • UART上最大255字节

最后一个字节

  • 类型255是保留类型,模块会忽略它。

  • 未知的TLV类型被拒绝。

  • TLV响应确认每个TLV请求。

  • 校验和算法是CRC8,参考值为 crc8 (多项式构造函数为 x8 + x5 + x4 +1),它将用于计算整个包的校验和。TLV长度不包括此校验和。

    • 代码示例:

    #include <stdlib.h>
    #include "checksum.h"
    
    static uint8_t crc_table[] = {
    
       0,   49,  98,  83,  196, 245, 166, 151, 185, 136, 219, 234, 125, 76,  31,  46,
       67,  114, 33,  16,  135, 182, 229, 212, 250, 203, 152, 169, 62,  15,  92,  109,
       134, 183, 228, 213, 66,  115, 32,  17,  63,  14,  93,  108, 251, 202, 153, 168,
       197, 244, 167, 150, 1,   48,  99,  82,  124, 77,  30,  47,  184, 137, 218, 235,
       61,  12,  95,  110, 249, 200, 155, 170, 132, 181, 230, 215, 64,  113, 34,  19,
       126, 79,  28,  45,  186, 139, 216, 233, 199, 246, 165, 148, 3,   50,  97,  80,
       187, 138, 217, 232, 127, 78,  29,  44,  2,   51,  96,  81,  198, 247, 164, 149,
       248, 201, 154, 171, 60,  13,  94,  111, 65,  112, 35,  18,  133, 180, 231, 214,
       122, 75,  24,  41,  190, 143, 220, 237, 195, 242, 161, 144, 7,   54,  101, 84,
       57,  8,   91,  106, 253, 204, 159, 174, 128, 177, 226, 211, 68,  117, 38,  23,
       252, 205, 158, 175, 56,  9,   90,  107, 69,  116, 39,  22,  129, 176, 227, 210,
       191, 142, 221, 236, 123, 74,  25,  40,  6,   55,  100, 85,  194, 243, 160, 145,
       71,  118, 37,  20,  131, 178, 225, 208, 254, 207, 156, 173, 58,  11,  88,  105,
       4,   53,  102, 87,  192, 241, 162, 147, 189, 140, 223, 238, 121, 72,  27,  42,
       193, 240, 163, 146, 5,   52,  103, 86,  120, 73,  26,  43,  188, 141, 222, 239,
       130, 179, 224, 209, 70,  119, 36,  21,  59,  10,  89,  104, 255, 206, 157, 172
    };
    
    /*
    * The function crc_8() calculates the 8 bit wide CRC of an input string of a
    * given length.
    */
    
    uint8_t crc_8( const unsigned char *input_str, size_t num_bytes ) {
    
       size_t a;
       uint8_t crc;
       const unsigned char *ptr;
    
       crc = CRC_START_8;
       ptr = input_str;
    
       if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
    
          crc = crc_table[(*ptr++) ^ crc];
       }
    
       return crc;
    
    }
    
    void main(void) {
       uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
       uint8_t crc;
    
       crc = crc_8(data, sizeof(data));
    }
    
    crc_table = bytes([
       0,   49,  98,  83,  196, 245, 166, 151, 185, 136, 219, 234, 125, 76,  31,  46,
       67,  114, 33,  16,  135, 182, 229, 212, 250, 203, 152, 169, 62,  15,  92,  109,
       134, 183, 228, 213, 66,  115, 32,  17,  63,  14,  93,  108, 251, 202, 153, 168,
       197, 244, 167, 150, 1,   48,  99,  82,  124, 77,  30,  47,  184, 137, 218, 235,
       61,  12,  95,  110, 249, 200, 155, 170, 132, 181, 230, 215, 64,  113, 34,  19,
       126, 79,  28,  45,  186, 139, 216, 233, 199, 246, 165, 148, 3,   50,  97,  80,
       187, 138, 217, 232, 127, 78,  29,  44,  2,   51,  96,  81,  198, 247, 164, 149,
       248, 201, 154, 171, 60,  13,  94,  111, 65,  112, 35,  18,  133, 180, 231, 214,
       122, 75,  24,  41,  190, 143, 220, 237, 195, 242, 161, 144, 7,   54,  101, 84,
       57,  8,   91,  106, 253, 204, 159, 174, 128, 177, 226, 211, 68,  117, 38,  23,
       252, 205, 158, 175, 56,  9,   90,  107, 69,  116, 39,  22,  129, 176, 227, 210,
       191, 142, 221, 236, 123, 74,  25,  40,  6,   55,  100, 85,  194, 243, 160, 145,
       71,  118, 37,  20,  131, 178, 225, 208, 254, 207, 156, 173, 58,  11,  88,  105,
       4,   53,  102, 87,  192, 241, 162, 147, 189, 140, 223, 238, 121, 72,  27,  42,
       193, 240, 163, 146, 5,   52,  103, 86,  120, 73,  26,  43,  188, 141, 222, 239,
       130, 179, 224, 209, 70,  119, 36,  21,  59,  10,  89,  104, 255, 206, 157, 172
    ])
    
    def crc_8(input_str):
       crc = 0
    
       for data in input_str:
          crc = crc_table[data ^ crc]
    
       return crc
    
    data = []
    
    data.append(0x85)
    data.append(0x04)
    data.append(0x07)
    data.append(0x00)
    data.append(0x05)
    data.append(0xff)
    
    print (crc_8(data))
    
    byte[] crc_table = new byte[] {
       0,   49,  98,  83,  196, 245, 166, 151, 185, 136, 219, 234, 125, 76,  31,  46,
       67,  114, 33,  16,  135, 182, 229, 212, 250, 203, 152, 169, 62,  15,  92,  109,
       134, 183, 228, 213, 66,  115, 32,  17,  63,  14,  93,  108, 251, 202, 153, 168,
       197, 244, 167, 150, 1,   48,  99,  82,  124, 77,  30,  47,  184, 137, 218, 235,
       61,  12,  95,  110, 249, 200, 155, 170, 132, 181, 230, 215, 64,  113, 34,  19,
       126, 79,  28,  45,  186, 139, 216, 233, 199, 246, 165, 148, 3,   50,  97,  80,
       187, 138, 217, 232, 127, 78,  29,  44,  2,   51,  96,  81,  198, 247, 164, 149,
       248, 201, 154, 171, 60,  13,  94,  111, 65,  112, 35,  18,  133, 180, 231, 214,
       122, 75,  24,  41,  190, 143, 220, 237, 195, 242, 161, 144, 7,   54,  101, 84,
       57,  8,   91,  106, 253, 204, 159, 174, 128, 177, 226, 211, 68,  117, 38,  23,
       252, 205, 158, 175, 56,  9,   90,  107, 69,  116, 39,  22,  129, 176, 227, 210,
       191, 142, 221, 236, 123, 74,  25,  40,  6,   55,  100, 85,  194, 243, 160, 145,
       71,  118, 37,  20,  131, 178, 225, 208, 254, 207, 156, 173, 58,  11,  88,  105,
       4,   53,  102, 87,  192, 241, 162, 147, 189, 140, 223, 238, 121, 72,  27,  42,
       193, 240, 163, 146, 5,   52,  103, 86,  120, 73,  26,  43,  188, 141, 222, 239,
       130, 179, 224, 209, 70,  119, 36,  21,  59,  10,  89,  104, 255, 206, 157, 172
    };
    
    public byte crc_8(byte[] data) {
    
       byte crc = 0;
    
       foreach (byte b in data) { crc = crc_table[b ^ crc]; }
    
       return crc;
    }
    
    public void calculateChecksum() {
    
       byte[] data = new byte[6];
    
       data[0] = 0x85;
       data[1] = 0x04;
       data[2] = 0x07;
       data[3] = 0x00;
       data[4] = 0x5;
       data[5] = 0xff;
    
       byte crc = crc_8(data);
    }
    

    TLV 请求

    类型

    长度

    价值

    校验和

    0x85

    0x04

    0x07 0x00 0x05 0xff

    0x80

    TLV 响应

    类型

    长度

    价值

    校验和

    0x40

    0x01

    0x00

    0x06

如果配置启用了某些内部事件,则可以在某些接口上生成TLV帧,而无需之前的请求(例如在UART接口或BLE接口上)。CORE_INT GPIO引脚用于通知UART/SPI上的内部事件。

事件可以通过以下方式配置 leaps_int_cfg_set。新事件将设置CORE_INT引脚。在用户在UART或SPI接口上发起任何活动后,CORE_INT引脚会自动清除。多字节数据部分按小字节序排列。