Using RMT to decode VAN bus protocol

Captain.Chode
Posts: 15
Joined: Mon Mar 11, 2019 4:23 pm

Using RMT to decode VAN bus protocol

Postby Captain.Chode » Mon Mar 11, 2019 5:03 pm

Hi,

I would like to use the RMT peripherial to read VAN bus protocol, but I have a hard time to figure out how to get the correct timings. I wrote an analyzer plugin for Saleae https://github.com/morcibacsi/VanAnalyzer so I know how the protocol looks like. There is also a description on it here: http://graham.auld.me.uk/projects/vanbu ... tocol.html. The point is that I need to sample the input in every 8us (microseconds) but I cannot get my head around the formula to calculating it. I found a simple (at least it seems so) example here in the forums: https://www.esp32.com/viewtopic.php?t=5787, but still no luck.

Code: Select all

#define RMT_RX_CHANNEL 0 /* RMT channel for receiver */
#define RMT_RX_GPIO_NUM PIN_RX /* GPIO number for receiver */
#define RMT_CLK_DIV 100 /* RMT counter clock divider */
#define rmt_item32_tIMEOUT_US 9500 /*!< RMT receiver timeout value(us) */

#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000) /* RMT counter value for 10 us.(Source clock is APB clock) */
#define ITEM_DURATION(d) ((d & 0x7fff)*10/RMT_TICK_10_US)

#define PIN_RX 22

static void rx_init()
{
    rmt_config_t rmt_rx;
    rmt_rx.channel = RMT_RX_CHANNEL;
    rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
    rmt_rx.clk_div = RMT_CLK_DIV;
    rmt_rx.mem_block_num = 30;
    rmt_rx.rmt_mode = RMT_MODE_RX;
    rmt_rx.rx_config.filter_en = false;
    rmt_rx.rx_config.filter_ticks_thresh = 100;
    rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
    rmt_config(&rmt_rx);
    rmt_driver_install(rmt_rx.channel, 1000, 0);
}
Could somebody please help me how to setup a channel to have the samples on every 8us? And another question: Sometimes it a bit pulse a a little bit longer than 8us (like 8.8 us) to transfer a bit. Could it cause any issues?

Thanks
Last edited by Captain.Chode on Sat Sep 05, 2020 4:01 pm, edited 1 time in total.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using RMT to decode protocol

Postby ESP_Angus » Mon Mar 11, 2019 10:49 pm

Nice project! I used to have a Peugeot 406 and I messed around with VANbus decoding once upon a time (well before ESP32, unfortunately!)
Captain.Chode wrote:
Mon Mar 11, 2019 5:03 pm
Could somebody please help me how to setup a channel to have the samples on every 8us? And another question: Sometimes it a bit pulse a a little bit longer than 8us (like 8.8 us) to transfer a bit. Could it cause any issues?
The RMT peripheral doesn't sample the level (high/low) at a regular period. Instead, it measures the duration of the high and low pulses.

So if you set the clock timing period to be (say) 1us (1MHz clock), then each RMT sample will tell you how many ticks (microseconds in this case) the line was low for, and then how many ticks it was high for.

So altogether, data from the RMT is going to be like "the line was high for X time, then low for X time, then high for X time, then low for X time". Because the VAN data is Manchester encoded, you should be able recover the clock period and data bits from this information.

If you actually want to read the status of the RMT line every Xus instead, you can do this by (mis)using the I2S or SPI peripherals (basically, have the SPI master sample the MISO line at XkHz but don't do anything with the other SPI signals from the peripheral). You will need to oversample though (ie if the time period is 8us, you'll need to sample at least every 4us to make sure you don't miss a transition due to inaccuracy or jitter). I think RMT will probably be easier to get a good result from.

Captain.Chode
Posts: 15
Joined: Mon Mar 11, 2019 4:23 pm

Re: Using RMT to decode protocol

Postby Captain.Chode » Tue Mar 19, 2019 5:14 pm

Thank you, based on your answer, I successfully modified this nice example and I was able to get the following values:

Code: Select all

32,-32,8,-8,8,-16,8,-8,8,-16,8,-8,8,-8,16,-24,16,-8,32,-8,32,-8,32,-8,32,-8,32,-32,8,-8,32,-8,8,-8,16,-40,8,-32,8,-32,8,-32,40,-8,32,-40,8,-32,8,-32,8,-32,8,-32,8,-32,40,-8,32,-32,8,-8,16,-16,24,-8,8,-8,16,-8,16,0
This equals to

Code: Select all

664 C 00 00 0E 04 FF FF 00 FF FF FF 00 E3 22 N
which is distributed on the VAN network by an odometer from a Peugeot 307 on the bench.

Now it is time to write some functions to decode the bytes from those values :D How would you proceed with that? I was wondering about creating an array where I can store zeroes and ones based on the above values (leaving out the manchester bits) and in a second run I can build the bytes from the elements in the array.

And another question about your VAN bus experience: what hardware did you use back then when you were experimenting with it?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: Using RMT to decode protocol

Postby ESP_Angus » Wed Mar 20, 2019 4:14 am

Captain.Chode wrote:
Tue Mar 19, 2019 5:14 pm
Now it is time to write some functions to decode the bytes from those values :D How would you proceed with that? I was wondering about creating an array where I can store zeroes and ones based on the above values (leaving out the manchester bits) and in a second run I can build the bytes from the elements in the array.
I think I'd do something like that, also.
Captain.Chode wrote: And another question about your VAN bus experience: what hardware did you use back then when you were experimenting with it?
It was almost ten years ago so I don't remember exactly. I think I had an 8-bit micro of some kind (probably AVR) and was just using a GPIO timer interrupt to read pulse widths. I just played around a little bit, I didn't get particularly far. :)

Captain.Chode
Posts: 15
Joined: Mon Mar 11, 2019 4:23 pm

Re: Using RMT to decode protocol

Postby Captain.Chode » Thu Mar 21, 2019 3:15 pm

So I created the reader, and uploaded it here: https://github.com/morcibacsi/esp32_rmt_van_rx

Is there a way to rename the topic to include the term VAN bus, like "Using RMT to decode VAN bus protocol" I could not find a way.

dmaxben
Posts: 108
Joined: Thu Nov 16, 2017 6:04 pm

Re: Using RMT to decode protocol

Postby dmaxben » Tue Jul 16, 2019 1:56 pm

Captain.Chode wrote:
Thu Mar 21, 2019 3:15 pm
So I created the reader, and uploaded it here: https://github.com/morcibacsi/esp32_rmt_van_rx

Is there a way to rename the topic to include the term VAN bus, like "Using RMT to decode VAN bus protocol" I could not find a way.
Very cool project!!

I wonder if this code could be adapted to decode the SAE J1850-VPW bus, used on older GM vehicles?

It basically just requires the timing of 64uS and 128uS pulses.

https://www.cnblogs.com/shangdawei/p/4769620.html

Captain.Chode
Posts: 15
Joined: Mon Mar 11, 2019 4:23 pm

Re: Using RMT to decode protocol

Postby Captain.Chode » Wed Jul 17, 2019 5:02 pm

I took a quick look on that protocol, and it seems that it wouldn't be so complicated to write an RMT receiver library for it. Basically the rmt_van_rx_parse_byte method and the crc calculation method should be implemented, and the rest would be almost the same.

Who is online

Users browsing this forum: VinayDand and 445 guests