UartBus source documentation
ub.h
1 /*
2  * ub.h
3  *
4  * Created on: 2017.05.08.
5  * Author: Dankó Dávid
6  */
7 
8 #ifndef UB_H_
9 #define UB_H_
10 
11 #include "stdint.h"
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdlib.h>
15 
16 #ifndef UINT32_MAX
17  #define UINT32_MAX 0xffffffff
18 #endif
19 
20 /* Endianess detection
21  * http://forum.arduino.cc/index.php?topic=254143.0
22  **/
23 /*
24 static const union { unsigned char bytes[4]; uint32_t value; } o32_host_order =
25  { { 0, 1, 2, 3 } };
26 
27 enum
28 {
29  O32_LITTLE_ENDIAN = 0x03020100ul,
30  O32_BIG_ENDIAN = 0x00010203ul,
31  O32_PDP_ENDIAN = 0x01000302ul
32 };
33 
34 #define O32_HOST_ORDER (o32_host_order.value)
35 *****/
36 
37 enum uartbus_status
38 {
39  ub_stat_disconnected, //0
40  ub_stat_connecting, //1
41  ub_stat_idle, //2
42  ub_stat_sending, //3
43  ub_stat_fairwait, //4
44  ub_stat_receiving, //5
45  ub_stat_collision //6
46 };
47 
48 enum uartbus_event
49 {
50  ub_event_nothing, //0
51 
52  ub_event_receive_start, //1
53  ub_event_receive_end, //2
54 
55  ub_event_send_start, //3
56  ub_event_send_end, //4
57 
58  ub_event_collision_start, //5
59  ub_event_collision_end, //6
60 };
61 
62 enum uartbus_cfg
63 {
64  /**
65  * After sending a packet, connection stays in ub_stat_sending_fairwait
66  * state to avoid a talkative device to lock the bus for too long time.
67  * */
68  ub_cfg_fairwait_after_send_low = 1,
69  ub_cfg_fairwait_after_send_high = 2,
70 
71  //TODO invalidated: use or not the external read but we must get the sent values
72  //back anyway
73  ub_cfg_read_with_interrupt = 4,
74  ub_cfg_skip_collision_data = 8
75  //TODO invalidated: we handle the collision anyway
76 };
77 
78 struct uartbus
79 {
80  /**
81  * receive one byte (0-255) on error: any negative value or higher than 255
82  * */
83  int16_t (*do_receive_byte)(struct uartbus* bus);
84 
85 
86  /**
87  * function called when a byte received from serial line.
88  * */
89  void (*serial_byte_received)
90  (struct uartbus* bus, uint8_t data_byte);
91 
92  void (*serial_event)
93  (struct uartbus* bus, enum uartbus_event event);
94 
95  uint8_t (*do_send_byte)
96  (struct uartbus* bus, uint8_t);
97 
98  uint32_t (*current_usec)
99  (struct uartbus* bus);
100 
101  uint8_t (*rand)
102  (struct uartbus* bus);
103 
104  void* user_data;
105 
106  uint8_t* to_send;
107 
108  volatile uint16_t to_send_size;
109 
110  volatile enum uartbus_status status;
111 
112  //time to transmit 1 byte in microsec, 11 byte reqires 11 bit to transmit
113  //in microsecounds
114  // 1 start + 8 payload bits + 1 parity + 1 stop bit = 11 bit
115  //
116  // baud => us
117  // 300 => 36666
118  // 9600 => 1145
119  // 57600 => 190
120  // 115200 => 96
121  //not used by the library
122  uint16_t byte_time_us;
123 
124  uint8_t packet_timeout;
125 
126  uint8_t cfg;
127 
128  //time to the bus go idle (time to no activity)
129  //uint32_t bus_idle_time;
130 
131  //last bus activity in millisec (overflow may occurs)
132  uint32_t last_bus_activity;
133 
134  //in the state `sending` it contains the last transmitted byte
135  //in case of collision (enter to fairwait) it constains the extra wait time.
136  volatile int16_t wi;
137 };
138 
139 uint16_t ub_calc_baud_cycle_time(uint32_t baud);
140 
141 uint32_t ub_calc_timeout(uint32_t baud, uint8_t cycles);
142 
143 //uint32_t ub_impl_get_current_usec();
144 
145 uint8_t crc8(uint8_t* data, uint8_t length);
146 
147 
148 /**
149  * Call this method from the outside to
150  *
151  * */
152 void ub_out_rec_byte(struct uartbus* bus, uint16_t data);
153 
154 void ub_init(struct uartbus*);
155 
156 void ub_init_infrastructure();
157 
158 void ub_predict_transmission_start(struct uartbus* bus);
159 
160 bool ub_prewait(struct uartbus* bus, uint8_t cycles);
161 
162 enum uartbus_status ub_get_bus_state(struct uartbus* bus);
163 
164 int8_t ub_send_packet
165  (struct uartbus* bus, uint8_t* addr, uint16_t size);
166 
167 int8_t ub_manage_connection
168 (
169  struct uartbus* bus,
170  uint8_t (*send_on_idle)(struct uartbus*, uint8_t** data, uint16_t* size)
171 );
172 
173 void ub_init_baud
174 (
175  struct uartbus* bus,
176  uint32_t baud,
177  uint8_t timeoutCycles
178 );
179 
180 /*#define static_ub_calc_baud_cycle_time(baud) ((uint16_t) (11000000.0/baud))
181 
182 #define static_ub_calc_timeout(baud, cycles) ((uint32_t) ((11000000.0/baud)*cycles))*/
183 
184 #endif /* UB_H_ */
int16_t(* do_receive_byte)(struct uartbus *bus)
Definition: ub.h:83
void(* serial_byte_received)(struct uartbus *bus, uint8_t data_byte)
Definition: ub.h:90
Definition: direct.c:49