Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Create the Lin ID parity */
- #define BIT(data,shift) ((addr & (1 << shift)) >> shift)
- uint8_t Lin_addrParity(uint8_t addr)
- {
- uint8_t p0 = BIT(addr, 0) ^ BIT(addr, 1) ^ BIT(addr, 2) ^ BIT(addr, 4);
- uint8_t p1 = ~(BIT(addr, 1) ^ BIT(addr, 3) ^ BIT(addr, 4) ^ BIT(addr, 5));
- return (p0 | (p1 << 1)) << 6;
- }
- /* Lin defines its checksum as an inverted 8 bit sum with carry */
- uint8_t Lin_dataChecksum(const uint8_t* message, char nBytes, uint16_t sum)
- {
- while (nBytes-- > 0) sum += *(message++);
- // Add the carry
- while(sum >> 8) // In case adding the carry causes another carry
- sum = (sum & 255) + (sum >> 8);
- return (~sum);
- }
- /* Send a message across the Lin bus */
- void Lin_send_master(uint8_t addr, const uint8_t* message, uint8_t nBytes,uint8_t proto)
- {
- uint8_t addrbyte = (addr & 0x3f) | addrParity(addr);
- uint8_t cksum = dataChecksum(message, nBytes, addrbyte);
- serialBreak(); // Generate the low signal that exceeds 1 char.
- write(0x55); // Sync byte
- write(addrbyte); // ID byte
- write(message, nBytes); // data bytes
- write(cksum); // checksum
- }
RAW Paste Data