30 #define SWBB_FAIL 65535
32 #define SWBB_NOT_ASSIGNED 255
42 #ifndef SWBB_RECEIVE_TIME
43 #define SWBB_RECEIVE_TIME 1000
51 uint32_t back_off(uint8_t attempts)
53 uint32_t result = attempts;
54 for(uint8_t d = 0; d < SWBB_BACK_OFF_DEGREE; d++) {
55 result *= (uint32_t)(attempts);
64 bool begin(uint8_t did = 0)
66 PJON_DELAY(PJON_RANDOM(SWBB_INITIAL_DELAY) + did);
76 PJON_IO_MODE(_input_pin, INPUT);
78 PJON_DELAY_MICROSECONDS(SWBB_BIT_SPACER / 2);
79 if(PJON_IO_READ(_input_pin)) {
82 PJON_DELAY_MICROSECONDS((SWBB_BIT_SPACER / 2));
83 if(PJON_IO_READ(_input_pin)) {
86 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH / 2);
87 for(uint8_t i = 0; i < 9; i++) {
88 if(PJON_IO_READ(_input_pin)) {
91 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH);
93 if(PJON_IO_READ(_input_pin)) {
97 PJON_DELAY_MICROSECONDS(SWBB_LATENCY);
98 if(PJON_IO_READ(_input_pin)) {
102 PJON_DELAY_MICROSECONDS(PJON_RANDOM(SWBB_COLLISION_DELAY));
103 if(PJON_IO_READ(_input_pin)) {
112 static uint8_t get_max_attempts()
114 return SWBB_MAX_ATTEMPTS;
120 static uint16_t get_receive_time()
122 return SWBB_RECEIVE_TIME;
128 void handle_collision()
130 PJON_DELAY_MICROSECONDS(PJON_RANDOM(SWBB_COLLISION_DELAY));
138 uint8_t byte_value = 0B00000000;
140 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH / 2);
141 for(uint8_t i = 0; i < 7; i++) {
143 byte_value += PJON_IO_READ(_input_pin) << i;
145 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH);
148 byte_value += PJON_IO_READ(_input_pin) << 7;
150 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH / 2);
157 uint16_t receive_byte()
172 uint16_t receive_response()
174 if(_output_pin != _input_pin && _output_pin != SWBB_NOT_ASSIGNED) {
175 PJON_IO_WRITE(_output_pin,
LOW);
177 uint16_t response = SWBB_FAIL;
178 uint32_t time = PJON_MICROS();
179 while((uint32_t)(PJON_MICROS() - time) < _timeout) {
180 PJON_IO_WRITE(_input_pin,
LOW);
182 response = receive_byte();
184 if(response == SWBB_FAIL) {
185 PJON_IO_MODE(_output_pin, OUTPUT);
186 PJON_IO_WRITE(_output_pin,
HIGH);
187 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH / 4);
188 PJON_IO_PULL_DOWN(_output_pin);
199 uint16_t receive_frame(uint8_t *
data, uint16_t max_length)
202 if(max_length == PJON_PACKET_MAX_LENGTH) {
203 uint32_t time = PJON_MICROS();
205 if(!sync_preamble() || !sync() || !sync()) {
210 (uint32_t)(PJON_MICROS() - time) <
211 (((SWBB_BIT_WIDTH * 3) + (SWBB_BIT_SPACER * 3)) - SWBB_ACCEPTANCE)
216 result = receive_byte();
217 if(result == SWBB_FAIL) {
244 void send_byte(uint8_t b)
247 for(uint8_t mask = 0x01; mask; mask <<= 1) {
248 PJON_IO_WRITE(_output_pin, b & mask);
249 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH);
260 void send_response(uint8_t response)
262 PJON_IO_PULL_DOWN(_input_pin);
263 uint32_t time = PJON_MICROS();
265 ((uint32_t)(PJON_MICROS() - time) < SWBB_BIT_WIDTH) &&
266 !PJON_IO_READ(_input_pin)
268 time = PJON_MICROS();
270 ((uint32_t)(PJON_MICROS() - time) < (SWBB_BIT_WIDTH / 4)) &&
271 PJON_IO_READ(_input_pin)
273 PJON_IO_MODE(_output_pin, OUTPUT);
276 PJON_IO_PULL_DOWN(_output_pin);
293 void send_frame(uint8_t *
data, uint16_t length)
295 _timeout = (length * SWBB_RESPONSE_OFFSET) + SWBB_LATENCY;
296 PJON_IO_MODE(_output_pin, OUTPUT);
298 for(uint16_t b = 0; b < length; b++) {
301 PJON_IO_PULL_DOWN(_output_pin);
322 bool sync(uint32_t spacer)
324 PJON_IO_PULL_DOWN(_input_pin);
325 if((_output_pin != _input_pin) && (_output_pin != SWBB_NOT_ASSIGNED)) {
326 PJON_IO_PULL_DOWN(_output_pin);
328 uint32_t time = PJON_MICROS();
330 PJON_IO_READ(_input_pin) &&
331 ((uint32_t)(PJON_MICROS() - time) <= spacer)
333 time = PJON_MICROS() - time;
334 if(time < SWBB_ACCEPTANCE) {
337 PJON_DELAY_MICROSECONDS((SWBB_BIT_WIDTH / 2) - SWBB_READ_DELAY);
338 if(!PJON_IO_READ(_input_pin)) {
339 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH / 2);
348 return sync(SWBB_BIT_SPACER);
353 return sync(SWBB_BIT_SPACER * SWBB_MAX_PREAMBLE);
358 void pulse(uint8_t n)
360 #if SWBB_PREAMBLE != 1
363 PJON_IO_WRITE(_output_pin,
HIGH);
364 PJON_DELAY_MICROSECONDS(SWBB_BIT_SPACER * SWBB_PREAMBLE);
365 PJON_IO_WRITE(_output_pin,
LOW);
366 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH);
371 PJON_IO_WRITE(_output_pin,
HIGH);
372 PJON_DELAY_MICROSECONDS(SWBB_BIT_SPACER);
373 PJON_IO_WRITE(_output_pin,
LOW);
374 PJON_DELAY_MICROSECONDS(SWBB_BIT_WIDTH);
380 void set_pin(uint8_t pin)
382 PJON_IO_PULL_DOWN(pin);
391 uint8_t input_pin = SWBB_NOT_ASSIGNED,
392 uint8_t output_pin = SWBB_NOT_ASSIGNED
395 PJON_IO_PULL_DOWN(input_pin);
396 PJON_IO_PULL_DOWN(output_pin);
397 _input_pin = input_pin;
398 _output_pin = output_pin;