- Windows
- Linux/Ubuntu
- python3.0 is required
- pyqt5 -
pip install pyqt5
- USART
- TCP/UDP
- SPI
Edit a xml file: you can create a new one or modify by the example xml file. The packet generator will generate code based on a xml file.
Do not use any reserved keyword in xml file, otherwise conflicts will be caused
The following is an example for how to define macros in your template.
<macro name = "PARAM">
<var name = "PACK_LEN" val = "200" > </var>
<var name = "BOOT_MODE" val = "0" > </var>
<var name = "CRC_CHECK_LENGTH" val = "128" > </var>
<var name = "BL_FLASH_IO" val = "FLASH_0" > </var>
<var name = "BL_USART_IO" val = "USART_0" > </var>
<var name = "IS_BOOT_MODE" val = "(pin_mode == BOOT_MODE)" > </var>
<var name = "BL_ACK" val = "0XA5" > </var>
<var name = "BL_NACK" val = "0X7F" > </var>
</macro>
<enum name = "meditation_mode">
<var name = "meditation_on" val = "0"> </var>
<var name = "meditation_lv1" val = "1"> </var>
<var name = "meditation_lv2" val = "2"> </var>
<var name = "meditation_lv3" val = "3"> </var>
<var name = "meditation_off" val = "4"> </var>
<var name = "//reserved" val = "5"> </var>
</enum>
<union name = "joint" anonymous_type = "uint8_t">
<struct>
<var type="uint8_t" name="header_leg" bit ="1"> </var>
<var type="uint8_t" name="pillow" bit ="1"> </var>
<var type="uint8_t" name="neck" bit ="1"> </var>
<var type="uint8_t" name="back" bit ="1"> </var>
<var type="uint8_t" name="lumbar" bit ="1"> </var>
<var type="uint8_t" name="buttocks" bit ="1"> </var>
<var type="uint8_t" name="reserved" bit ="1"> </var>
</struct>
</union>
<struct name = "header" encode = "false" config = "true">
<var type="uint8_t" name="header"> </var>
<var type="uint8_t" name="len"> </var>
<var type="uint8_t" name="packet_type"> </var>
</struct>
- click
load
button to load an xml file. - Use default checksum(sum32), little-endian, align 1, align word.
- click generate
- copy source file and header file in to your project and name them packet.c/packet.h
- ensure that the .c/.h file are included in your project.
The communication can be splited into two actio- encoding and decoding
when trying to send a message from a master to a slave , you should encode
a message firstly and then send to the bus.
The following snippets shows how to encode a message and send it as a pack.
packet_encode(NULL,&pack,sizeof(packet_t));
uart_send(&pack,sizeof(pack));
After receieving a byte, a slave is attempt to decode
a pack until success. if a pack can be deocded by slave, the slave will know what message sended by a master.
parse_state_t parser;
parser_init(&parser);
...
uart_recv(&buf,1);
msg_id = packet_parser(buf2,buf,&parser);
if(msg_id){
message_decode(buf2, msg_id, &parser);
}
uint8_t message_decode(uint8_t* buf_,uint8_t msg_id, parse_state_t* ps){
switch (msg_id)
{
case 1:
start = ps->start_idx;
memcpy(&pack2,&buf_[ps->start_idx],sizeof(packet_t));
memset(buf_,0,200);
parser_init(ps);
break;
case 2:
memset(buf,0,200);
break;
case 3:
memset(buf,0,200);
break;
}
}
message_decode
is a weak function, the user can redefine this function and implement it.- msg_id is an uint8_t-type variable. The slave gets a message when the
paket_parser
return a non-zero value (msg_id is not zero). msg_id
is a unique value for each of message. The id value is devfined as xxx_ID in header file.
The following figure shows the general structure of a packet. A packet consists of header, payload and checksum.
header provides the basic information of a packet which includes: head : the start of packet which indicates the beginning of a packet. len : the length of following bytes. packet_type: the type of pack device_type: the type of device msg_id : reserved byte sub_id : reserved byte
Depends on packet type (i.e. packet_type).
To check the validation of a packet.