Skip to content

Commit

Permalink
minor bitbang refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Jun 11, 2018
1 parent ed2920c commit cb92733
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions board/drivers/canbitbang.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,34 @@ void set_bitbanged_gmlan(int val) {
}

void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) {
puts("called bitbang_gmlan\n");

char pkt_stuffed[MAX_BITS_CAN_PACKET];
int len = get_bit_message(pkt_stuffed, to_bang);

// TODO: interrupts are disabled for a long time...
enter_critical_section();

// actual bitbang loop
set_bitbanged_gmlan(1); // recessive
set_gpio_mode(GPIOB, 13, MODE_OUTPUT);
enter_critical_section();

// 33.3 kbps
#define SPEEED 30

// wait for bus silent for 7 frames
int silent_count = 0;
while (silent_count < 7) {
if (silent_count > 0) {
// bit time delay
int lwait = TIM2->CNT;
while (get_ts_elapsed(TIM2->CNT, lwait) < SPEEED);
}

// check for silent
int read = get_gpio_input(GPIOB, 12);
silent_count++;
if (read == 0) {
silent_count = 0;
}
int lwait = TIM2->CNT;
while (get_ts_elapsed(TIM2->CNT, lwait) < SPEEED);
}

// send my message with optional failure
Expand All @@ -159,10 +164,8 @@ void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) {

fail:
set_bitbanged_gmlan(1); // recessive
exit_critical_section();
set_gpio_mode(GPIOB, 13, MODE_INPUT);

puts("bitbang done\n");
exit_critical_section();
}

#endif
Expand Down

0 comments on commit cb92733

Please sign in to comment.