Problems with sercom1 SPI on Feather SAMD21 M0

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
kava60
 
Posts: 3
Joined: Sun Jan 01, 2023 1:04 pm

Problems with sercom1 SPI on Feather SAMD21 M0

Post by kava60 »

I'm using the Feather SAMD21 M0 WiFi in a project and want to also use an RFM95 LoRa Radio. I tried using the standard SPI for the RFM95 and it worked well until I enabled WiFi so it appears that for some reason (despite different CS) the two aren't coexisting on that SPI.

I followed the excellent information in the Adafruit tutorial to configure sercom1 as a second SPI (using exactly the same pins for MOSI, MISO, SCK). I also made sure to add the pinPeripheral calls to configure those pins as PIO_SERCOM.

It didn't work and when I look at things with my scope I can see the CS toggling as expected but there is no SCK present. I repeated my examination using the scope with just the RFM95 on the standard SPI and can see the expected SCK pulses corresponding to when CS is LOW. I have checked and double checked (and used a different Feather M0 that I have) but the behavior is the same. I've attached a simple sketch that demonstrates my problem.

Thanks for any help/suggestions!
Kurt V
Attachments
M0_SPI_Sercom1.txt
(2.38 KiB) Downloaded 6 times

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problems with sercom1 SPI on Feather SAMD21 M0

Post by adafruit_support_mike »

kava60 wrote: Sun Feb 05, 2023 1:43 pm to configure sercom1 as a second SPI (using exactly the same pins for MOSI, MISO, SCK
It shouldn't be possible to configure two SERCOMs to control the same group of pins, but if the code doesn't fail, it won't do what you want.

If you want a second SPI interface, you'll have to assign its signals to another set of pins.

User avatar
kava60
 
Posts: 3
Joined: Sun Jan 01, 2023 1:04 pm

Re: Problems with sercom1 SPI on Feather SAMD21 M0

Post by kava60 »

Sorry - my post wasn't very clear - I'm not using the same pins as the standard SPI - what I meant was that I'm using the same pins as in the example in the Adafruit tutorial on enabling a second SPI. If you look at the example code I included you will see that I'm using pins 11, 12 and 13 (and sercom1 not the default sercom0).

Kurt

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problems with sercom1 SPI on Feather SAMD21 M0

Post by adafruit_support_mike »

Post a photo showing your hardware and connections and we'll take a look. 800x600 images usually work best.

User avatar
kava60
 
Posts: 3
Joined: Sun Jan 01, 2023 1:04 pm

Re: Problems with sercom1 SPI on Feather SAMD21 M0

Post by kava60 »

I discovered the problem. The problem was that the LoRa library calls SPI.begin() within the LoRa.begin() call which resets the pinPeripheral types to defaults which undid my pinPeripheral calls to set the type to PIO_SERCOM right after my call to the SPI.begin() on my second SPI interface.

Recall that you have to set the pinPeripheral for each of the MISO/MOSI/SCK to PIO_SERCOM after the call to begin for the newly defined SPI. This is because in the SPI.begin() (see libraries/SPI/SPI.cpp in the samd Adafruit Ardino core) makes pinPeripheral calls itself:

// PIO init
pinPeripheral(_uc_pinMiso, g_APinDescription[_uc_pinMiso].ulPinType);
pinPeripheral(_uc_pinSCK, g_APinDescription[_uc_pinSCK].ulPinType);
pinPeripheral(_uc_pinMosi, g_APinDescription[_uc_pinMosi].ulPinType);

So for the second SPI interface that I defined (mySPI), I was correctly defining it, calling mySPI.begin() and then making the correct pinPeripheral calls. After that, I passed that SPI to the LoRa library (via LoRa.setSPI) and when I called LoRa.begin it again called begin on mySPI which then made the pinPeripheral calls with the default pin types for those pins (12, 11 and 13) which set them back to regular old D12, D11 and D13.

This is an unfortunate hidden behavior that SPI.begin sets the pins to the default types - perhaps it would be good if someone can update the Adafruit tutorial on creating a second SPI to highlight this fact (which explains why the pinPeripheral calls have to be after the call to begin on that second SPI).

I've worked around this by adding a bool flag to LoRa.begin to tell it not to do the _spi.begin().

Oh also, if anyone looks at my example code on my initial post, I mistakenly used sercom2 instead of sercom1. That was an error in transcribing my actual code (which does use sercom1) into a small example to demonstrate the problem.

Kurt

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Problems with sercom1 SPI on Feather SAMD21 M0

Post by adafruit_support_mike »

Glad to hear you got it working. Happy hacking!

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”