Blue pill and CANbus

Post here first, or if you can't find a relevant section!
Rx7man
Posts: 3
Joined: Mon Nov 08, 2021 6:21 am

Blue pill and CANbus

Post by Rx7man »

I'm trying to find an example of using CAN bus, with libraries for a blue pill.. I found an instructable but they only showed the hardware setup and didn't link to any libraries.. I've looked at libraries and there's nothing that's kinda complete, I've downloaded dozens now, including the Arduino-STM32-CAN-master and nothing wants to work, getting pretty frustrating

Here's the error list from trying to compile the example

Code: Select all

Arduino: 1.8.13 (Windows 7), Board: "Generic STM32F103C series, STM32F103CB (20k RAM. 128k Flash), STM32duino bootloader, 72Mhz (Normal), Smallest (default)"

C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master\stm32f103\stm32f103.ino: In function 'void CANSetFilter(uint8_t, uint8_t, uint8_t, uint8_t, uint32_t, uint32_t)':

stm32f103:63:3: error: 'CAN1' was not declared in this scope

   CAN1->FA1R &= ~(0x1UL<<index);               // Deactivate filter

   ^

C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master\stm32f103\stm32f103.ino: In function 'bool CANInit(BITRATE, int)':

stm32f103:105:3: error: 'RCC' was not declared in this scope

   RCC->APB1ENR |= 0x2000000UL;       // Enable CAN clock 

   ^

stm32f103:107:3: error: 'AFIO' was not declared in this scope

   AFIO->MAPR   &= 0xFFFF9FFF;        // reset CAN remap

   ^

stm32f103:112:12: error: 'struct gpio_dev' has no member named 'CRH'

     GPIOA->CRH   &= ~(0xFF000UL);    // Configure PA12(0b0000) and PA11(0b0000)

            ^

stm32f103:117:12: error: 'struct gpio_dev' has no member named 'CRH'

     GPIOA->CRH   |= 0xB8FFFUL;       // Configure PA12(0b1011) and PA11(0b1000)

            ^

stm32f103:125:12: error: 'struct gpio_dev' has no member named 'ODR'

     GPIOA->ODR |= 0x1UL << 12;       // PA12 Upll-up

            ^

stm32f103:134:12: error: 'struct gpio_dev' has no member named 'CRH'

     GPIOB->CRH   &= ~(0xFFUL);       // Configure PB9(0b0000) and PB8(0b0000)

            ^

stm32f103:139:12: error: 'struct gpio_dev' has no member named 'CRH'

     GPIOB->CRH   |= 0xB8UL;          // Configure PB9(0b1011) and PB8(0b1000)

            ^

stm32f103:147:12: error: 'struct gpio_dev' has no member named 'ODR'

     GPIOB->ODR |= 0x1UL << 8;        // PB8 Upll-up

            ^

stm32f103:155:12: error: 'struct gpio_dev' has no member named 'CRL'

     GPIOD->CRL   &= ~(0xFFUL);       // Configure PD1(0b0000) and PD0(0b0000)

            ^

stm32f103:160:12: error: 'struct gpio_dev' has no member named 'CRH'

     GPIOD->CRH   |= 0xB8UL;          // Configure PD1(0b1011) and PD0(0b1000)

            ^

stm32f103:168:12: error: 'struct gpio_dev' has no member named 'ODR'

     GPIOD->ODR |= 0x1UL << 0;        // PD0 Upll-up

            ^

stm32f103:171:3: error: 'CAN1' was not declared in this scope

   CAN1->MCR |= 0x1UL;                   // Require CAN1 to Initialization mode 

   ^

C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master\stm32f103\stm32f103.ino: In function 'void CANReceive(CAN_msg_t*)':

stm32f103:243:17: error: 'CAN1' was not declared in this scope

   uint32_t id = CAN1->sFIFOMailBox[0].RIR;

                 ^

C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master\stm32f103\stm32f103.ino: In function 'void CANSend(CAN_msg_t*)':

stm32f103:301:3: error: 'CAN1' was not declared in this scope

   CAN1->sTxMailBox[0].TDTR &= ~(0xF);

   ^

C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master\stm32f103\stm32f103.ino: In function 'uint8_t CANMsgAvail()':

stm32f103:337:10: error: 'CAN1' was not declared in this scope

   return CAN1->RF0R & 0x3UL;

          ^

exit status 1

'CAN1' was not declared in this scope

Invalid library found in C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master: no headers files (.h) found in C:\Users\Y2D\Documents\Arduino\libraries\Arduino-STM32-CAN-master



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Blue pill and CANbus

Post by mrburnette »

First:

https://www.google.com/search?q=%22blue ... 2duino.com
Rx7man wrote: Mon Nov 08, 2021 7:38 am I'm trying to find an example of using CAN bus, with libraries for a blue pill.. I found an instructable but they only showed the hardware setup and didn't link to any libraries.. I've looked at libraries and there's nothing that's kinda complete, I've downloaded dozens now, including the Arduino-STM32-CAN-master and nothing wants to work, getting pretty frustrating
https://github.com/exothink/eXoCAN/tree/master/examples
getting pretty frustrating
I know nothing about your experience, knowledge-level, or even what you are trying to accomplish; but, if you are frustrated at "this point" may I suggest finding a simple project to tackle. Moving conceptually from Arduino 8-bit boards to 32-bit STM32 (or Espressif, or Raspberry, or even Due) is on the surface easy; however the more advanced microcontrollers are significantly more complex and more difficult to master.

Some resources: viewtopic.php?f=2&t=301


Ray
Rx7man
Posts: 3
Joined: Mon Nov 08, 2021 6:21 am

Re: Blue pill and CANbus

Post by Rx7man »

I've got a fair bit of experience with the 8 bit boards, I've fumbled my way through getting basic blink sketches working on the STM platform, gotten bootloaders installed and such...
From my reading, the native CAN pins on the bluepill boards interfere with the USB, so it's sounding like I'll go with the MCP2515/2551 combo boards, and I have a fair bit of experience working with those on the 8 bit platform... though I guess I'll find out if the CAN library and SPI play nicely on the 32 bit boards.. To start with I'll use SPI1 and get it working there, but ideally I'd like to use the SPI2 channel so I don't lose too many analog inputs
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Blue pill and CANbus

Post by ag123 »

I've not worked with can bus. perhaps you could post an issue on the github repository if you need help there.
https://github.com/nopnop2002/Arduino-STM32-CAN

among the things in your ide you need to select the appropriate board e.g. stm32f103 blue pill , stm32f103 generic etc.
https://github.com/stm32duino/wiki/wiki
The thing about selecting the appropriate board is that it sets some #define symbols which are used to include the appropriate #include files.

but looking at the codes, it'd seem CAN1 isn't defined.
https://github.com/nopnop2002/Arduino-S ... 32f103.ino
It may help if a #include clause is inserted to insert the header file that contains the definition
and the ino sketch neds to be copied into the sketch folder for the build.

hope you have reviewed the relevant faqs etc
viewtopic.php?f=2&t=3
viewtopic.php?f=2&t=301

stm32 i'd guess is pretty different from things like atmega328, the features and the way things work are different even though a basic Arduino api may be common. if you are using stm32f103c8 blue pills, be a little wary of the small amount of sram 20k and flash 64k available. it is normally adequate, but that if you have a sketch that is a pretty big app, u could run out of memory or flash. it is normally hard to detect as the stack simply build downwards and overwrite global variables.
Rx7man
Posts: 3
Joined: Mon Nov 08, 2021 6:21 am

Re: Blue pill and CANbus

Post by Rx7man »

If needed, I'll write my own CAN bus library/port a working Arduino/Atmel version to work, but I'm going to start a step back and get SPI to work.. I have looked at the examples and links you posted, but see nothing related to SPI/CAN there
My exact processor is STM32F103C8T6

I'm not looking for a whole lot of processing to be done on the chip, some ADC pin readings and putting them onto the CAN bus and vice versa, and then maybe some PID control loop.. I will have to keep an eye on memory usage for sure though.
umejopa
Posts: 9
Joined: Sat Oct 16, 2021 7:37 pm
Location: Stockholm

Re: Blue pill and CANbus

Post by umejopa »

I am using code from this on F303 and it works good.
https://github.com/nopnop2002/Arduino-STM32-CAN
It should work for F103 to using a serie to USB , Upload with ST-link
stefan
Posts: 7
Joined: Wed Feb 02, 2022 12:31 am

Re: Blue pill and CANbus

Post by stefan »

Hello,
I'm using this same lowlevel example, and works OK.
But having trouble with Serial. When i enable "generic USART Serial support" and "CDC generic Serial supersede", CAN stops working.
When i disable these hw serial supports, CAN works fine. Serial printing is important for developing code phase, so i had to add software serial, and hook up FTDI cable on defined pins.
btw. When i use hw serial with mentioned settings on another simple example, like 'serial pass through' (or similair) , usb serial works fine.
Is there any way to use hw usb serial and can in same time ?
(currently i'm using blue pill F103C8T6, flashing with st-link2)
looking forward to some solution.
djix123
Posts: 8
Joined: Mon Oct 25, 2021 11:34 pm

Re: Blue pill and CANbus

Post by djix123 »

You will need to change the CAN peripheral initialization code to use PB8/PB9 since, by default, the CAN peripheral and the USB peripheral share the same pins (PA11 / PA12) on the STM32F103Cx.

(More details in the datasheet / https://www.st.com/resource/en/datashee ... f103c8.pdf)
stefan
Posts: 7
Joined: Wed Feb 02, 2022 12:31 am

Re: Blue pill and CANbus

Post by stefan »

I am already using PB8/PB9 mapping.
Problem is somewhere else. Even when i enable CDC and generic Serial, COM port is not shown in Device Manager with this CAN low level code flashed.
Something is blocking USB CDC HW serial. I have solution with software serial, but this is only temporary fix.
Just to mention again usb cdc with hw serial is working fine with other examples, it is not hardware problem.
djix123
Posts: 8
Joined: Mon Oct 25, 2021 11:34 pm

Re: Blue pill and CANbus

Post by djix123 »

The reference manual (page 654) also references an issue related to using the USB and CAN peripherals at the same time
In low, medium-, high- and XL-density devices the USB and CAN share a dedicated 512- byte SRAM memory for data transmission and reception, and so they cannot be used concurrently (the shared SRAM is accessed through CAN and USB exclusively). The USB and CAN can be used in the same application but not at the same time.
Without some serious juggling it doesn't look like its possible to use both simultaneously unless the application has distinct phases of communication between CAN and USB.

https://www.st.com/resource/en/referenc ... ronics.pdf
Post Reply

Return to “General discussion”