CAN Bus Library for STM32F103

Can someone help me port this library?
tjb12345
Posts: 11
Joined: Sun Aug 06, 2017 6:23 am

Re: CAN Bus Library for STM32F103

Post by tjb12345 » Mon Aug 14, 2017 8:26 pm

I was working on this right now, and was struggling to get the USB working on my olimexino STM32 board....was able to upload to is and everything, but couldn't get it to stay showing as a COM Port, that change fixed it right away, I have the BlinkNcount sketch running on it now. Going to move forward and try to get CANBUS function along with Serial on a hardware serial port with my FTDI (need serial to read the messages for prototyping.) If I can get this working, then I'm going to go back to trying it on the Blue Pill....

Thanks for the fix!

tjb12345
Posts: 11
Joined: Sun Aug 06, 2017 6:23 am

Re: CAN Bus Library for STM32F103

Post by tjb12345 » Tue Aug 15, 2017 1:59 am

I got the Olimexino STM32 working to send and receive CAN Messages like I want. The high speed is going to be a nice improvement over the Uno. I would still like to get the BluePill working but there must be some slight difference that is causing my problem.
To recap:

It successfully sends 1 message, but then freezes (or gets stuck in a loop somewhere). I am using a MPC2551 Transceiver, which is verified to have 5 volts supplied to it. The code doesn't work in Loopback Mode either though, so I am pretty sure it's either the BP hardware, or the something in the STMF103C Generic Code (it works with the Maple (rev 3) code for the Olimexino board).

BP has been modified with 1.5k resistor in place of the 10k at R10. I hit the USB pins with the soldering iron to make sure there wasn't a bad solder (not using USB though, uploading via serial to USART1 (PA9 and PA10). I was able to get the USB to work on the BP as well, running the BlinkNCount example, so that leads me to believe that the hardware is OK.

One difference is I used the USB to upload the CAN sketch to the Olimexino, but then used D7 , D8 for the Serial adapter to print the messages. on the Blue Pill, I am uploading right from the serial adapter, would that make any difference?

I am convinced that this will work, as it seems that someone else had it working earlier in the thread.

User avatar
RogerClark
Posts: 7157
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: CAN Bus Library for STM32F103

Post by RogerClark » Tue Aug 15, 2017 3:52 am

CAN and USB share hardware resources in the MCU.

You can't simply run CAN and expect USB to still run, unless you modify the USB system to co-exist

tjb12345
Posts: 11
Joined: Sun Aug 06, 2017 6:23 am

Re: CAN Bus Library for STM32F103

Post by tjb12345 » Tue Aug 15, 2017 1:37 pm

RogerClark wrote:
Tue Aug 15, 2017 3:52 am
CAN and USB share hardware resources in the MCU.

You can't simply run CAN and expect USB to still run, unless you modify the USB system to co-exist
I'm not trying to use both...that why I'm uploading and running Serial through USART1 and an FTDI adapter. I only used the USB on the BlinkNCount Example to make sure that part actually worked.

Similar with the Olimexino, I used USB to upload the CAN sketch, but did not try to use SerialUSB to communicate with the serial monitor.

I'm just trying to figure out why when loaded onto a generic STM32F103C8 that it behaves differently than a STM32103RB. I do agree that the USB is probably whats

User avatar
RogerClark
Posts: 7157
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: CAN Bus Library for STM32F103

Post by RogerClark » Tue Aug 15, 2017 9:18 pm

how do you upload using the bootloader?

Did you select the serial uploaded option, then export the binary and then manually run DFU from the command line?

If you select bootloader upload from the menu in the IDE it enables all the USB subsystem code so that the sketch can be automatically triggered via USB to upload again the next time.

tjb12345
Posts: 11
Joined: Sun Aug 06, 2017 6:23 am

Re: CAN Bus Library for STM32F103

Post by tjb12345 » Tue Aug 15, 2017 9:54 pm

For the Generic, I am just choosing Serial from the "Upload Method" and I have my FTDI adapter hooked up to PA9 and PA10. It uploads fine that way, but maybe it doesn't include the code to for the interrupt?

User avatar
RogerClark
Posts: 7157
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: CAN Bus Library for STM32F103

Post by RogerClark » Tue Aug 15, 2017 9:56 pm

Yes. If you choose Serial upload, the compile options remove all USB code, however most other options include the USB code

tjb12345
Posts: 11
Joined: Sun Aug 06, 2017 6:23 am

Re: CAN Bus Library for STM32F103

Post by tjb12345 » Fri Aug 18, 2017 10:02 pm

RogerClark wrote:
Tue Aug 15, 2017 9:56 pm
Yes. If you choose Serial upload, the compile options remove all USB code, however most other options include the USB code
I knew there was going to be some little thing I was doing wrong......I got it working now! In loopback mode anyway, but I'm confident it will work other ways as well.

I am still not having luck uploading with USB after I flashed the PC13 Generic Bootloader, but I am able to program it using my ST-Link Tool and that option in the upload method, and It doesn't make on difference to me anyway, since I can't use USB for serial anyway.

I also tried adding a line to the boards.txt file like this

Code: Select all

genericSTM32F103C.menu.upload_method.serialMethod.build.upload_flags=-DSERIAL_USB
and that worked to get the USB Code complied when I upload with the Serial method (which is preferred because I already have that connected to get serial data for analyzing CAN messages).

Thanks so much for the help!

User avatar
RogerClark
Posts: 7157
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: CAN Bus Library for STM32F103

Post by RogerClark » Fri Aug 18, 2017 10:10 pm

You can upload via the USB bootloader, and have Serial USB disabled in the core, however you will have to manually reset to upload, as the IDE uses USB to trigger the board into the bootloader in order to upload

But, I think there are more than one Define you may need to change

User avatar
And_Ru
Posts: 25
Joined: Thu Nov 10, 2016 1:16 pm
Location: Russia, Moscow

Re: CAN Bus Library for STM32F103

Post by And_Ru » Sun Sep 03, 2017 9:50 pm

1) I know I'm late, but if you have only first message sent to CAN and then hang up, it is good to check if receiver party works well (receive and acknowledge the message).

2) to remain USB serial in the board I just added the check of one pin in the setup:

Code: Select all

	Serial1.begin(115200);
	pinMode(28, INPUT); // B12 = 16+12 = 28
    digitalWrite(28, LOW);
	delay(50);
	Serial1.println("pin B12 set to input mode");
		
	usbMode = digitalRead(28);
    
	Serial1.print("pin B12 is ");
    Serial1.println(usbMode);
	
	if (usbMode)
	{
		Serial1.println("Entering USB mode");
		Serial.begin(9600);
		Serial.println("Entering USB mode");
		while (1)
		{
			Serial.print(".");
			Serial1.print(".");
			digitalWrite(PC13, (!digitalRead(PC13)));
			delay(1000);
		}
	}
	else
	{
	  digitalWrite(PC13, PC13OFF);
	  Serial1.println("Starting CAN module ...");
	  CANSetup() ;        // Initialize the CAN module and prepare the message structures.
	}
      Serial1.println("Setup finished, going to loop()");
      

Post Reply