Skip to content

Commit

Permalink
bitbanging works
Browse files Browse the repository at this point in the history
  • Loading branch information
geohot committed Jun 11, 2018
1 parent 74af441 commit d917386
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
55 changes: 33 additions & 22 deletions board/drivers/canbitbang.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,54 @@ int append_crc(char *in, int in_len) {
return in_len;
}

int get_bit_message(char *out) {
char test_pkt[MAX_BITS_CAN_PACKET];
char test_pkt_src[] = {
0, // SOF
0,0,0,0, // ID10-ID7
0,0,1,0,1,0,0, // ID6-ID0
0, // RTR
0, // IDE
0, // reserved
0,0,0,1, // len
0,0,0,0,0, // 1st byte 7-3
0,0,1, // 1st byte 2-0
};
int append_bits(char *in, int in_len, char *app, int app_len) {
for (int i = 0; i < app_len; i++) {
in[in_len++] = app[i];
}
return in_len;
}

int append_int(char *in, int in_len, int val, int val_len) {
for (int i = val_len-1; i >= 0; i--) {
in[in_len++] = (val&(1<<i)) != 0;
}
return in_len;
}

int get_bit_message(char *out, CAN_FIFOMailBox_TypeDef *to_bang) {
char pkt[MAX_BITS_CAN_PACKET];
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

// copy packet
for (int i = 0; i < sizeof(test_pkt_src); i++) test_pkt[i] = test_pkt_src[i];
int len = 0;

// test packet
int dlc_len = to_bang->RDTR & 0xF;
len = append_int(pkt, len, 0, 1); // Start-of-frame
len = append_int(pkt, len, to_bang->RIR >> 21, 11); // Identifier
len = append_int(pkt, len, 0, 3); // RTR+IDE+reserved
len = append_int(pkt, len, dlc_len, 4); // Data length code

// append data
for (int i = 0; i < dlc_len; i++) {
unsigned char dat = ((unsigned char *)(&(to_bang->RDLR)))[i];
len = append_int(pkt, len, dat, 8);
}

// append crc
int len = append_crc(test_pkt, sizeof(test_pkt_src));
len = append_crc(pkt, len);

// do bitstuffing
len = do_bitstuff(out, test_pkt, len);
len = do_bitstuff(out, pkt, len);

// append footer
for (int i = 0; i < sizeof(footer); i++) {
out[len++] = footer[i];
}
len = append_bits(out, len, footer, sizeof(footer));
return len;
}

Expand All @@ -100,7 +111,7 @@ void bitbang_gmlan(CAN_FIFOMailBox_TypeDef *to_bang) {
puts("\n");

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

// actual bitbang loop
set_bitbanged_gmlan(1); // recessive
Expand Down
4 changes: 2 additions & 2 deletions tests/gmbitbang/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
# send w bitbang then without
while 1:
p1.set_gmlan(bus=None)
p1.can_send(20, "\x01", bus=3)
p1.can_send(123, "\x01\x02\x03\x04\x05\x06\x07\x08", bus=3)
p1.set_gmlan(bus=2)
p1.can_send(20, "\x01", bus=3)
p1.can_send(123, "\x01\x02\x03\x04\x05\x06\x07\x08", bus=3)
time.sleep(0.01)
print p2.can_recv()

18 changes: 16 additions & 2 deletions tests/gmbitbang/test_packer.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
#include <stdio.h>
#include <stdint.h>

typedef struct {
uint32_t RIR; /*!< CAN receive FIFO mailbox identifier register */
uint32_t RDTR; /*!< CAN receive FIFO mailbox data length control and time stamp register */
uint32_t RDLR; /*!< CAN receive FIFO mailbox data low register */
uint32_t RDHR; /*!< CAN receive FIFO mailbox data high register */
} CAN_FIFOMailBox_TypeDef;

#include "../../board/drivers/canbitbang.h"

int main() {
char out[100];
int len = get_bit_message(out);
char out[300];
CAN_FIFOMailBox_TypeDef to_bang = {0};
to_bang.RIR = 20 << 21;
to_bang.RDTR = 1;
to_bang.RDLR = 1;

int len = get_bit_message(out, &to_bang);
printf("T:");
for (int i = 0; i < len; i++) {
printf("%d", out[i]);
Expand Down

0 comments on commit d917386

Please sign in to comment.