UartBus source documentation
direct.c
1 
2 #include "ubh.h"
3 
4 #ifndef PACKET_ESCAPE
5  #define PACKET_ESCAPE (uint8_t) 0xff
6 #endif
7 
8 void try_dispatch_received_packet();
9 void USART_SendByte(uint8_t u8Data);
10 void ub_init_infrastructure();
11 
12 //Exact duplication of crc8 from `ub.c` (we want to exclude uartbus library but
13 //this very single function is required to imitate we are a single node bus)
14 
15 //https://www.ccsinfo.com/forum/viewtopic.php?t=37015
16 //x^8+x^5+x^4+x^0 //Dallas-Maxim CRC8
17 uint8_t crc8(uint8_t* data, uint8_t length)
18 {
19  uint8_t crc = 0;
20  uint8_t v;
21  uint8_t i;
22  for(i=0;i<length;++i)
23  {
24  v = (data[i] ^ crc) & 0xff;
25  crc = 0;
26  if(v & 1)
27  crc ^= 0x5e;
28  if(v & 2)
29  crc ^= 0xbc;
30  if(v & 4)
31  crc ^= 0x61;
32  if(v & 8)
33  crc ^= 0xc2;
34  if(v & 0x10)
35  crc ^= 0x9d;
36  if(v & 0x20)
37  crc ^= 0x23;
38  if(v & 0x40)
39  crc ^= 0x46;
40  if(v & 0x80)
41  crc ^= 0x8c;
42  }
43 
44  return crc;
45 }
46 
47 
48 //fake uartbus strucure and `bus` varaible
49 struct uartbus{} bus;
50 
51 uint8_t rando()
52 {
53  return 0;
54 }
55 
56 void init_bus()
57 {
58  ub_init_infrastructure();
59 };
60 
61 //copied and modified from uartbus_connector.cpp
62 
63 #include <avr/io.h>
64 
65 
66 bool mayCut = false;
67 /*
68 if(received_ep == MAX_PACKET_SIZE)
69  {
70  //brake the package manually
71  received_ep = 0;
72  }
73  else
74  {
75  received_data[received_ep++] = data_byte;
76  }
77 */
78 void ub_out_rec_byte(struct uartbus* bus, uint16_t data)
79 {
80  if(data < 0 || data > 255)
81  {
82  return;
83  }
84 
85  //if we have a pending packet
86  if(received)
87  {
88  return;
89  }
90 
91  if(received_ep >= MAX_PACKET_SIZE)
92  {
93  received_ep = 0;//break too the long packet
94  }
95 
96  uint8_t b = data;
97 
98  if(mayCut)
99  {
100  if(b == (uint8_t)~PACKET_ESCAPE)
101  {
102  //new packet received
103  received = true;
104  }
105  else
106  {
107  received_data[received_ep++] = b;
108  }
109  mayCut = false;
110  }
111  else
112  {
113  if(b == (uint8_t)PACKET_ESCAPE)
114  {
115  mayCut = true;
116  }
117  else
118  {
119  received_data[received_ep++] = b;
120  }
121  }
122 }
123 
124 void try_send_packet()
125 {
126  if(send_size != 0)
127  {
128  for(uint8_t i=0;i<send_size;++i)
129  {
130  uint8_t val = send_data[i];
131  if(PACKET_ESCAPE == val)
132  {
133  USART_SendByte((uint8_t)PACKET_ESCAPE);
134  }
135  USART_SendByte(val);
136  }
137  USART_SendByte((uint8_t)PACKET_ESCAPE);
138  USART_SendByte((uint8_t)~PACKET_ESCAPE);
139 
140  send_size = 0;
141  }
142 }
143 
144 
145 void ubh_manage_bus()
146 {
147  try_send_packet();
148  try_dispatch_received_packet();
149 }
Definition: direct.c:49