Added LinBus class to libopeninv

Post Reply
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Added LinBus class to libopeninv

Post by johu »

I wrote a class for LIN communication with STM32. It is completely non-blocking and uses DMA for sending. Works with USART1, 2, 3

It works like this:

Code: Select all

LinBus* lin;

//Would be called by scheduler in fixed time interval, say 100ms
void CyclicTask()
{
   lin->Send(22, 0, 0); //Read request feature ID 22
   //or send command, controlled by some state machine (only send one lin command per cycle!):
   uint8_t lindata[4] = { 1, 2, 3, 4 };
   lin->Send(21, lindata, sizeof(lindata)); //Send data "1 2 3 4" to ID 21
   
   //check if we received a reply on ID 22 that is 8 bytes long
   if (lin->HasReceived(22, 8))
   {
      uint8_t* data = lin->GetReceivedBytes();

      Param::SetInt(Param::tmpheater, data[1] - 40);
   }
}

extern "C" int main(void)
{
   clock_setup();
   //various init
   LinBus l(USART1, 9600);
   lin = &l;
}
I think I will change HasReceived() so you don't need to specify the encoded feature ID.
HasReceived now takes ID without parity.

https://github.com/jsphuebner/libopenin ... linbus.cpp

EDIT: Updated example
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
Jack Bauer
Posts: 3563
Joined: Wed Dec 12, 2018 5:24 pm
Location: Ireland
Has thanked: 1 time
Been thanked: 87 times
Contact:

Re: Added LinBus class to libopeninv

Post by Jack Bauer »

You da man Johannes! :)
I'm going to need a hacksaw
Dilbert
Posts: 410
Joined: Mon Aug 12, 2019 7:21 pm
Location: Dublin, Ireland
Been thanked: 4 times

Re: Added LinBus class to libopeninv

Post by Dilbert »

Nice work.

For the lin master we used to run the entire state machine from the rx interrupt, as the transceiver will echo back the tx bytes on the rx pin. We also used to change baud rate for the sync break.
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: Added LinBus class to libopeninv

Post by johu »

Thanks :)
Oh yes, l.Receive() could also be placed in the rx interrupt
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

Ok that looks pretty simple.

now if i want to implment Lim on zombie. do i need to configure it first before i send the messages?

an actual example of a function sending will be appreciated.

thanks
karim
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: Added LinBus class to libopeninv

Post by johu »

I thought Damien had already done something with LIN in Zombie, but can't find it right now. Above example is a bit outdated! Will change it.
Anyway, I've used it in the Volvo project: https://github.com/jsphuebner/stm32-car ... ar.cpp#L95

See main() in the same file on how to initialize the LinBus object. Also in hwinit.cpp you need to enable the clock for the USART you want to use. Interrupts are not needed.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

you are awesome Johu,

I just tested the VW Heater code today and got some toasty water out. Now i will implement in Zombie. as far as i noticed the Zombie has Lin but nothing is being controlled by Lin in Zombie files.

I will give it a try tomorrow and test the Heater by zombie.

thanks,
Karim
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

I took a walk in your code and it looks like you used the Digital pots, is it right ? coming on my list is to used the fuel gauge to reflect SOC. the BMW e60 uses two fuel senders each is about 500 Ohm

im planning to use the digital Pots to do that.

is the implementation in set fuel gauge function doing what i want to do ?

if so again what init needed to be working ?

thanks again
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
m.art.y
Posts: 550
Joined: Sat Jun 06, 2020 6:54 pm
Location: UK
Has thanked: 24 times
Been thanked: 17 times

Re: Added LinBus class to libopeninv

Post by m.art.y »

7yatna wrote: Mon Jul 03, 2023 8:30 amNow i will implement in Zombie. as far as i noticed the Zombie has Lin but nothing is being controlled by Lin in Zombie files.
Hm, in the Zombie there is a list of heaters you can select and one of them is VW. Not tested if it works myself but wouldn't it be controller by LIN?
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

m.art.y wrote: Mon Jul 03, 2023 8:42 am Hm, in the Zombie there is a list of heaters you can select and one of them is VW. Not tested if it works myself but wouldn't it be controller by LIN?
Not implemented to be worked on. I`m the one who is working on it.
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

Hi Johu,

it looks like the Job is bigger than what i though. im so stuck and lost between the pointers. the Zombie is a little bit bigger than your Volvo.

so here where i`m. all the modified Files are attached everything else before this lin implementation was working, compiling and running on my VCU.

I needed to add vwheater.cpp and vwheater.h

I Initialized the USART for Lin it is connected to USART1 and wasn't initialized. I initialized it in hwinit.cpp

then i went to do it like you did on the example and i modified the STM32_vcu.cpp and STM_vcu.h to include inbus header. in the STM32_VCU.cpp i added :

static LinBus* lin; and LinBus l(USART1, 19200); and lin = &l; in their respected location attached the stm32_vcu.cpp and stm32_vcu.h

then finally the vwheater.cpp and vwheater.h both files are attached.
i wrote the function to implement control only of the vw heater. i can work on the feedback later but i need to send messages throw lin.

and now when i compile for some reason my pointer is goin to null. what im missing im personally not follwing the pointers.

here is a screen of the error.
image.png
any ideas? as soon as i can get the lin comms working the rest is easy. and BTW i know my actual control code might be off but my bench test code is working fine. that can be worked later after i get the LIN comms out of Zombie.

can you take a look over the header files, pointers locatiosn, initializeing and that sort.

thanks.
Karim
Attachments
vwheater.cpp
(4.68 KiB) Downloaded 34 times
vwheater.h
(1.18 KiB) Downloaded 37 times
stm32_vcu.h
(2.15 KiB) Downloaded 30 times
stm32_vcu.cpp
(24.3 KiB) Downloaded 32 times
hwinit.cpp
(8.95 KiB) Downloaded 32 times
vwheater.h
(1.18 KiB) Downloaded 38 times
vwheater.cpp
(4.68 KiB) Downloaded 46 times
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

and ignore the warning of the suburau class, it is because the digital pots are not implemented yet.
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
royhen99
Posts: 195
Joined: Sun Feb 20, 2022 4:23 am
Location: N. Wiltshire. UK
Has thanked: 14 times
Been thanked: 94 times

Re: Added LinBus class to libopeninv

Post by royhen99 »

Have you added linbus.o to the OBJSL list in the makefile?
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: Added LinBus class to libopeninv

Post by johu »

In vwheater.cpp you have a static member LinBus* lin but that pointer is never initialized.
You need to pass a pointer of your LinBus object that is created in main() to your vwheater class

EDIT: you'll find that the inverter or vehicle classes all have a function SetCanInterface(). So you could do something similar for LIN.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

Hi Guys,

I took both of your advice and now we compile with no issues. I made a setlininterface in the vwheater and added linbus .o and we are rolling.

Unfortunately there is no lin comms going out. which after thinking about stuff, i didnt call SetlinInterface function to link the pointers.

so i went to stm32_vcu.cpp and added a conditional line to call setlininterface if vwheater is selected.

now it is complaining about classes.

here are my updated files.

any ideas how to call the setlininterface?

I need a crash course in pointers, * and & and how to pass them across the full code.

anyway im very sure that we are close. to get this zombie to spit Lin comms.

The Error:
image.png
Attachments
vwheater.h
(1.21 KiB) Downloaded 37 times
vwheater.cpp
(4.74 KiB) Downloaded 38 times
stm32_vcu.cpp
(24.38 KiB) Downloaded 40 times
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

i found something else that will make no Comms,

in zombie there is a pin called linnslp which causes the TJA chip to sleep i measured the pin and it is LOW which in my bench testsetup will kill all comms.

it is on GPIO11. any ideas where we can make this pin goes HIGH ? im thinking we can put it in setlininterface function. am i Right ?

Edit: I just fixed that and now that pin goes HIGH when we select the VWheater see attached file.
Attachments
stm32_vcu.cpp
(24.42 KiB) Downloaded 41 times
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: Added LinBus class to libopeninv

Post by johu »

There are static classes which are essentially just C-files with a slightly nicer syntax. And then there are non-static classes which can be "instantiated". That's what you see on top of stm32_vcu where all possible inverters are created. But only of of them is linked to the pointer "selectedInverter".

You'd do the same for the heater, have a pointer of type "Heater" (the base class) called "selectedHeater and depending on user selection that pointer is set. Make sure you add SetLinInterface() to the base class, not to your specialized class. Again, check inverter.h/cpp on how this all plays together.

That way you can always call selectedHeater->SetLinInterface() (and selectedHeater->SetCanInterface() for that matter) and don't need nasty conditionals.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

I tried doing and replicating the setcanhardware() but obviously i have a problem with keeping up where the poiinters and what declared and not.

i moved the setlininterface to the base class. and really not follwoing why and which variables i need to declare.

even the amperaheater files .cpp/.h that i didnt touch now it is upset.

im sorry that im dragging you guys this long but between damiens code and OI code and being sperated as each module has its own files.

I want to learn how to pass pointeers and navigate between classes.

anyway here are updated files and new errors. im looking online as well to figure my way but i did encounter and fixed other issues like the sleep for the TJA chip that worked and now the chip should be ready to take usart info.

Sorry for being a headach and a noope.
image.png
Attachments
vwheater.h
(1.19 KiB) Downloaded 36 times
amperaheater.h
(1.14 KiB) Downloaded 30 times
heater.h
(747 Bytes) Downloaded 45 times
amperaheater.cpp
(9.44 KiB) Downloaded 33 times
vwheater.cpp
(4.81 KiB) Downloaded 46 times
stm32_vcu.cpp
(24.35 KiB) Downloaded 43 times
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: Added LinBus class to libopeninv

Post by johu »

heater.h needs to include linbus.h otherwise it doesn't know the type LinBus ;)
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
7yatna
Posts: 65
Joined: Wed Jul 06, 2022 2:49 am
Location: San Diego, CA
Has thanked: 4 times
Been thanked: 3 times

Re: Added LinBus class to libopeninv

Post by 7yatna »

johu wrote: Wed Jul 05, 2023 5:37 am heater.h needs to include linbus.h otherwise it doesn't know the type LinBus ;)
Oh My god, i was that close. I just added linbus.h into heater.h and compiled with no warnings at all.

i will test tomorrow the lin communications today i switched gears and worked on getting the Gs450H spinning. after two hours chasing my tail because of a clean unmodified hex, it was corrupted.

i put back my modified Hex all is working and as expected. ready for HV and power on in the morning.

I hope we get motor spinning tomorrow than test the heater from Zombie.

I will report back but thanks Johu for your help, im still lost but happy lost that somnething compiled and worked.

Best regards,
Karim
Hammer, Duct tape, WD-40 that`s it :D
1973 Beetle with SDU swap.
Post Reply