[Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
[Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
I am working on using a MCP 2515 CAN Bus module on my STM32F103C8 generic board running the STM32duino core.
(Yes, I understand the STM32F103 has a built in CAN controller, but I also require USB)
I am using the (wonderful!) MCP_CAN-lib (https://github.com/coryjfowler/MCP_CAN_lib)
As I understand it, it is written to use the default (and only) SPI bus for an Uno/Nano.
How can I remap the STM32F103 SPI2 bus so that it works with the MCP_CAN_lib?
Thanks in advance for any suggestions
(Yes, I understand the STM32F103 has a built in CAN controller, but I also require USB)
I am using the (wonderful!) MCP_CAN-lib (https://github.com/coryjfowler/MCP_CAN_lib)
As I understand it, it is written to use the default (and only) SPI bus for an Uno/Nano.
How can I remap the STM32F103 SPI2 bus so that it works with the MCP_CAN_lib?
Thanks in advance for any suggestions
Last edited by Franke39 on Mon Jan 18, 2021 7:35 pm, edited 1 time in total.
-
- Posts: 26
- Joined: Tue Jan 07, 2020 4:22 pm
Re: Can I use SPI2 with Uno/Nano Library that uses SPI?
if you see this post https://github.com/stm32duino/Arduino_C ... issues/669 you will see you can setup spi_2 class with the line
i would try changing the default spi class to use the second spi port as if it was SPI_1 unless you need to refer to spi1 separate, in which case the other option is fork the library and just change all spi.xxxx to spi2.xxx ,
you could even do it as a define rule, i've provided one as a example but it's not tested ,
hope this helps,
darkspr1te
Code: Select all
#define MOSI2 PB15
#define MISO2 PB14
#define SCLK2 PB13
#define SS2 PB12
//SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel)
SPIClass SPI_2(MOSI2, MISO2, SCLK2, SS2);
you could even do it as a define rule, i've provided one as a example but it's not tested ,
Code: Select all
*******************************************************************************************************/
INT8U MCP_CAN::mcp2515_readStatus(void)
{
INT8U i;
#ifndef USE_SPI_2
SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
#else
SPI_2.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
#endif
MCP2515_SELECT();
spi_readwrite(MCP_READ_STATUS);
i = spi_read();
MCP2515_UNSELECT();
#ifndef USE_SPI_2
SPI.endTransaction();
#else
SPI_2.endTransaction();
#endif
return i;
}
hope this helps,
darkspr1te
Re: Can I use SPI2 with Uno/Nano Library that uses SPI?
Thank you VERY much for your help!
I will try it later this morning.
I will try it later this morning.
Re: Can I use SPI2 with Uno/Nano Library that uses SPI?
Update:
Before I changed the MCP_CAN_lib, I remapped the default SPI to use SPI2 pins, as it is 5 volt tolerant
AND IT WORKED!
Now the weird part:
Yesterday I started to use the WONDERFUL HID Bootloader (https://github.com/Serasidis/STM32_HID_Bootloader), however the program wouldn't start.
I added a Serial.print("Hello!") right after setup() and a while(1).... The program would upload as normal, but nothing would show on the serial monitor.
I loaded the example ASCII table program and it uploaded and printed as it should.
Once again, I tried the program that uses the MCP_CAN_lib and it uploaded. Nothing printed.
Thinking there might be a conflict between my code and the bootloader, I switch over to USB to TTL programming
AND IT WORKED!
I hope my experience is helpful to others.
Before I changed the MCP_CAN_lib, I remapped the default SPI to use SPI2 pins, as it is 5 volt tolerant
Code: Select all
SPI.setMOSI(PB15);
SPI.setMISO(PB14);
SPI.setSCLK(PB13);
Now the weird part:
Yesterday I started to use the WONDERFUL HID Bootloader (https://github.com/Serasidis/STM32_HID_Bootloader), however the program wouldn't start.
I added a Serial.print("Hello!") right after setup() and a while(1).... The program would upload as normal, but nothing would show on the serial monitor.
I loaded the example ASCII table program and it uploaded and printed as it should.
Once again, I tried the program that uses the MCP_CAN_lib and it uploaded. Nothing printed.
Thinking there might be a conflict between my code and the bootloader, I switch over to USB to TTL programming
AND IT WORKED!
I hope my experience is helpful to others.
-
- Posts: 26
- Joined: Tue Jan 07, 2020 4:22 pm
Re: [Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
The USB boot loader need to be flashed first and at a different location of memory, e.g
bootloader start address 0x0000000
user code start address 0x00005000
also depending on which ide you are using you will have to change programming type.
i use platformio and Black Magic probe( the stm32 jatg software) so i edit my platformio.ini 's upload_method = and change that ,
in arduino ide you change adapter type.
So my advise is that you download the compiled USB loader and flash using st-link software then the usb side should work,
This though is based on assumptions of your hardware , i am assuming you are using a black/bluepill or similar.
darkspr1te
bootloader start address 0x0000000
user code start address 0x00005000
also depending on which ide you are using you will have to change programming type.
i use platformio and Black Magic probe( the stm32 jatg software) so i edit my platformio.ini 's upload_method = and change that ,
in arduino ide you change adapter type.
So my advise is that you download the compiled USB loader and flash using st-link software then the usb side should work,
This though is based on assumptions of your hardware , i am assuming you are using a black/bluepill or similar.
darkspr1te
Re: [Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
Thanks for the suggestion.
I did flash the HID bootloader (https://github.com/Serasidis/STM32_HID_Bootloader) for the STM32F103 using ST-Link 2 & STMCubeProgrammer at 0x8000000 before I loaded this program. I changed the required settings in the Arduino IDE and it worked great on 5-6 different programs I used to test it.
It was so nice, not having to move a jumper and hit reset.
However, when this program was loaded, it didn't start. I flashed other programs and it worked. I assumed there is some kind of conflict and went back to serial programming.
I will try loading the bootloader again tomorrow, just to see.
I did flash the HID bootloader (https://github.com/Serasidis/STM32_HID_Bootloader) for the STM32F103 using ST-Link 2 & STMCubeProgrammer at 0x8000000 before I loaded this program. I changed the required settings in the Arduino IDE and it worked great on 5-6 different programs I used to test it.
It was so nice, not having to move a jumper and hit reset.
However, when this program was loaded, it didn't start. I flashed other programs and it worked. I assumed there is some kind of conflict and went back to serial programming.
I will try loading the bootloader again tomorrow, just to see.
-
- Posts: 26
- Joined: Tue Jan 07, 2020 4:22 pm
Re: [Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
Hi,
When you say it worked on 5-6 programs , I assume you mean the board accepted 5 - different .bin files and flashed them and they booted (be it blink or what ever) but when you upload your code it wont function,
I assume then yes there is a conflict, i would start by putting serial.print right at the start of your code (in setup) to see if it's even booting it, or a blink gpio can do the same,
as i am often building code for a board i've never even seen using code I dont know I will often replace main functions like setup() main() with mini "hello worlds" just to see if i get that far, this is mostly when working with bootloaders.
Also what serial adapter are you using, if ftdi it could support jtag if wired correctly this will not only help in debug but also up loading as the jtag will support swd and swd+reset with the correct openocd and arduino.IDE changes (platformio supports already)
finally check you code for any defines that may change the arduino defaults for bootloading address. I dont use the arduino system myself only when needing to compile other's code so I'am not sure what keywords you would need to search but as the stm32duino code is built on top of stm32 cubemx code then it will also suffer it's failures. but look out for items like
#define HSE_VALUE 8000000
and
#define VECT_TAB_OFFSET 0x0
finally the jumpers may effect the boot process, as you can boot from SRAM which arduino ide can support, just make sure you not doing that ( i did and it had me for days )
darkspr1te
When you say it worked on 5-6 programs , I assume you mean the board accepted 5 - different .bin files and flashed them and they booted (be it blink or what ever) but when you upload your code it wont function,
I assume then yes there is a conflict, i would start by putting serial.print right at the start of your code (in setup) to see if it's even booting it, or a blink gpio can do the same,
as i am often building code for a board i've never even seen using code I dont know I will often replace main functions like setup() main() with mini "hello worlds" just to see if i get that far, this is mostly when working with bootloaders.
Also what serial adapter are you using, if ftdi it could support jtag if wired correctly this will not only help in debug but also up loading as the jtag will support swd and swd+reset with the correct openocd and arduino.IDE changes (platformio supports already)
finally check you code for any defines that may change the arduino defaults for bootloading address. I dont use the arduino system myself only when needing to compile other's code so I'am not sure what keywords you would need to search but as the stm32duino code is built on top of stm32 cubemx code then it will also suffer it's failures. but look out for items like
#define HSE_VALUE 8000000
and
#define VECT_TAB_OFFSET 0x0
finally the jumpers may effect the boot process, as you can boot from SRAM which arduino ide can support, just make sure you not doing that ( i did and it had me for days )
darkspr1te
Re: Can I use SPI2 with Uno/Nano Library that uses SPI?
Thanks again for this. As I move along with the project it was very helpful today as I now need to use both SPI ports.darkspr1te wrote: ↑Mon Jan 18, 2021 12:05 pm if you see this post https://github.com/stm32duino/Arduino_C ... issues/669 you will see you can setup spi_2 class with the linei would try changing the default spi class to use the second spi port as if it was SPI_1 unless you need to refer to spi1 separate, in which case the other option is fork the library and just change all spi.xxxx to spi2.xxx ,Code: Select all
#define MOSI2 PB15 #define MISO2 PB14 #define SCLK2 PB13 #define SS2 PB12 //SPIClass(uint8_t mosi, uint8_t miso, uint8_t sclk, uint8_t ssel) SPIClass SPI_2(MOSI2, MISO2, SCLK2, SS2);
you could even do it as a define rule, i've provided one as a example but it's not tested ,hope this helps,Code: Select all
INT8U MCP_CAN::mcp2515_readStatus(void) { INT8U i; #ifndef USE_SPI_2 SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); #else SPI_2.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); #endif MCP2515_SELECT(); spi_readwrite(MCP_READ_STATUS); i = spi_read(); MCP2515_UNSELECT(); #ifndef USE_SPI_2 SPI.endTransaction(); #else SPI_2.endTransaction(); #endif return i; }
darkspr1te
Here is the setup:
• SD card on SPI1 and all works well.
• MCP2515 CAN Bus module on SPI2, but the mcp_can_lib / mcp_can.cpp is coded to "SPI"
• I tried your code above and this is the error I received:
Code: Select all
/Users/frank/Documents/Arduino/libraries/MCP_CAN_lib/mcp_can_spi_2.cpp: In member function 'void MCP_CAN::mcp2515_reset()':
/Users/frank/Documents/Arduino/libraries/MCP_CAN_lib/mcp_can_spi_2.cpp:37:5: error: 'SPI_2' was not declared in this scope; did you mean 'SPI2'?
37 | SPI_2.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
| ^~~~~
| SPI2
Is there anyway I can set SPI to use SPI_2 that was just created?
Thanks in advance!
Re: [Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
That is correct.darkspr1te wrote: ↑Wed Jan 20, 2021 9:14 am Hi,
When you say it worked on 5-6 programs , I assume you mean the board accepted 5 - different .bin files and flashed them and they booted (be it blink or what ever) but when you upload your code it wont function,
I assume then yes there is a conflict, i would start by putting serial.print right at the start of your code (in setup) to see if it's even booting it, or a blink gpio can do the same,
as i am often building code for a board i've never even seen using code I dont know I will often replace main functions like setup() main() with mini "hello worlds" just to see if i get that far, this is mostly when working with bootloaders.
Now today, while programming with the serial connection (via STM32CubeProgrammer) if I put in a delay(1000); right after setup(), the program would run right after the bin file was downloaded. I wonder if that would for for the USB boot loader... I test that in a few days.
I am using one with the FT232RL IC. After I learn a little bit more on the programming side, I will look into how to debug using the ST-Link and serial connections.Also what serial adapter are you using, if ftdi it could support jtag if wired correctly this will not only help in debug but also up loading as the jtag will support swd and swd+reset with the correct openocd and arduino.IDE changes (platformio supports already)
I will double check for those #defines.finally check you code for any defines that may change the arduino defaults for bootloading address. I dont use the arduino system myself only when needing to compile other's code so I'am not sure what keywords you would need to search but as the stm32duino code is built on top of stm32 cubemx code then it will also suffer it's failures. but look out for items like
#define HSE_VALUE 8000000
and
#define VECT_TAB_OFFSET 0x0
finally the jumpers may effect the boot process, as you can boot from SRAM which arduino ide can support, just make sure you not doing that ( i did and it had me for days )
darkspr1te
Thanks again for taking the time to help me. I really appreciate it!
Re: [Solved] Can I use SPI2 with Uno/Nano Library that uses SPI?
I have resolved the errors and will post what I did in a new thread.
I think it may be useful to others see one method to how to get an Arduino library that uses SPI.something working on the STM32F1 SPI2 5 volt tolerant bus.
Once I have done the post, I will add the link here.
I think it may be useful to others see one method to how to get an Arduino library that uses SPI.something working on the STM32F1 SPI2 5 volt tolerant bus.
Once I have done the post, I will add the link here.