-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
// returns out_len | ||
int do_bitstuff(char *out, char *in, int in_len) { | ||
int last_bit = -1; | ||
int bit_cnt = 0; | ||
int j = 0; | ||
for (int i = 0; i < in_len; i++) { | ||
char bit = in[i]; | ||
out[j++] = bit; | ||
|
||
// do the stuffing | ||
if (bit == last_bit) { | ||
bit_cnt++; | ||
if (bit_cnt == 5) { | ||
// 5 in a row the same, do stuff | ||
last_bit = !bit; | ||
out[j++] = last_bit; | ||
bit_cnt = 1; | ||
} | ||
} else { | ||
// this is a new bit | ||
last_bit = bit; | ||
bit_cnt = 1; | ||
} | ||
} | ||
return j; | ||
} | ||
|
||
int get_bit_message(char *out) { | ||
char test_pkt[] = { | ||
0, // SOF | ||
0,0,0,0, // ID10-ID7 | ||
//1, // bitstuff | ||
0,0,1,0,1,0,0, // ID6-ID0 | ||
0, // RTR | ||
0, // IDE | ||
0, // reserved | ||
//1, // bitstuff | ||
0,0,0,1, // len | ||
0,0,0,0,0, // 1st byte 7-3 | ||
//1, // bitstuff | ||
0,0,1, // 1st byte 2-0 | ||
|
||
// CRC from real message | ||
1,1,1,0,1,1,1,0,1,0,1,0,0,1,1, | ||
}; | ||
char footer[] = { | ||
1, // CRC delimiter | ||
|
||
1, // ACK | ||
1, // ACK delimiter | ||
1,1,1,1,1,1,1, // EOF | ||
1,1,1, // IFS | ||
}; | ||
#define SPEEED 30 | ||
|
||
// testing | ||
//char *test_pkt_stuffed = test_pkt; | ||
//int len = sizeof(test_pkt); | ||
|
||
// do bitstuffing | ||
int len = do_bitstuff(out, test_pkt, sizeof(test_pkt)); | ||
|
||
// append footer | ||
for (int i = 0; i < sizeof(footer); i++) { | ||
out[len++] = footer[i]; | ||
} | ||
return len; | ||
} | ||
|
||
// hardware stuff below this line | ||
|
||
#ifdef PANDA | ||
|
||
void set_bitbanged_gmlan(int val) { | ||
if (val) { | ||
GPIOB->ODR |= (1 << 13); | ||
} else { | ||
GPIOB->ODR &= ~(1 << 13); | ||
} | ||
} | ||
|
||
void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) { | ||
puts("called bitbang_gmlan\n"); | ||
puth(can_num_lookup[3]); | ||
puts("\n"); | ||
|
||
char pkt_stuffed[64+44+25]; | ||
int len = get_bit_message(pkt_stuffed); | ||
|
||
// actual bitbang loop | ||
set_bitbanged_gmlan(1); // recessive | ||
set_gpio_mode(GPIOB, 13, MODE_OUTPUT); | ||
enter_critical_section(); | ||
int init = TIM2->CNT; | ||
|
||
for (int i = 0; i < len; i++) { | ||
while ((TIM2->CNT - init) < (SPEEED*i)); | ||
set_bitbanged_gmlan(pkt_stuffed[i]); | ||
} | ||
|
||
exit_critical_section(); | ||
set_gpio_mode(GPIOB, 13, MODE_INPUT); | ||
|
||
puts("bitbang done\n"); | ||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include <stdio.h> | ||
#include "../../board/drivers/canbitbang.h" | ||
|
||
int main() { | ||
char out[100]; | ||
int len = get_bit_message(out); | ||
for (int i = 0; i < len; i++) { | ||
printf("%d", out[i]); | ||
} | ||
printf("\n"); | ||
return 0; | ||
} | ||
|
||
|
||
|