# SAMD support working

**URL:** https://community.simplefoc.com/t/samd-support-working/492
**Category:** Uncategorized
**Created:** [January 26, 2021, 8:14pm UTC](https://community.simplefoc.com/t/samd-support-working/492 "2021-01-26T20:14:39Z")
**Posts on this page:** 20
**Page:** 3

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 13, 2021, 2:27pm UTC](https://community.simplefoc.com/t/samd-support-working/492/41 "2021-05-13T14:27:44Z")

</div>

Yeah I was about to ask the same question.  
[this one](https://www.sparkfun.com/products/14713) as very few pins  
[this one](https://www.sparkfun.com/products/16791) requires a [carrier board](https://www.sparkfun.com/products/16885)

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [May 13, 2021, 2:31pm UTC](https://community.simplefoc.com/t/samd-support-working/492/42 "2021-05-13T14:31:42Z")

</div>

I’ve ordered this one few days ago. It should be compatible I think.

> **[Adafruit Metro M4 feat. Microchip ATSAMD51](https://www.adafruit.com/product/3382)**
>
> Are you ready? Really ready? Cause here comes the fastest, most powerful Metro ever. The Adafruit Metro M4 featuring the Microchip ATSAMD51. This Metro is like a bullet train, with its ...

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 13, 2021, 2:34pm UTC](https://community.simplefoc.com/t/samd-support-working/492/43 "2021-05-13T14:34:24Z")

</div>

Nice! with QSPI flash! love it

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 13, 2021, 4:02pm UTC](https://community.simplefoc.com/t/samd-support-working/492/44 "2021-05-13T16:02:50Z")

</div>

That one is cool, but I didn’t order it so far (as you know I like the little boards 🙂 )  
I assume it will work. I think it might be able to do 6-PWM with 2 or more motors, which is unrealistic with any of the other boards based on the number of pins.

I have tested with the following boards:

SAMD51:  
Feather M4 express  
SAME51:  
Feather M4 can  
SAMD21:  
Nano 33 IoT  
MKR1000  
MKR1010 WiFi  
Seed Xiao  
Feather M0 express  
Feather M0 basic proto

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 13, 2021, 4:10pm UTC](https://community.simplefoc.com/t/samd-support-working/492/45 "2021-05-13T16:10:38Z")

</div>

> [@malem](#):
>
> I will also send you the MR for my drv8305 drvier… I really went haywire with this one… got my arm pulled in the architectural blender. It consumes a LOT of flash because I kinda coded the whole specheet verbatim, lol. It was tolerably quick to do with column-edit mode.

Hey that sounds awesome…

> [@malem](#):
>
> My goal with it is to expose all settings to Commander, and later to my upcomming spin to SimpleFOCStudio

That’s a good idea, I didn’t think to make this explicitly part of the driver code. So far I’ve been adding individual commands to the commander for the stuff I need.

> [@malem](#):
>
> BTW, I’m about to include current sensing, what’s the status in dev (you were talking about improvements…)
> 
> Do you use DMA like Paul Gould did?

I’d like to model the solution on his, using DMA, but I’m concentrating on other things atm. Antun is heavily at work on the core of the current sensing code for the next release, and the FOC algorithm side of things is not my expertise.  
So I’m taking it slowly, waiting for that to be stable before attacking the driver side (which is just moving bytes around in complex ways, I feel comfortable with that).

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 13, 2021, 4:35pm UTC](https://community.simplefoc.com/t/samd-support-working/492/46 "2021-05-13T16:35:45Z")

</div>

@Antun_Skuric What are your plans for current sensing? Maybe I could help.

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 18, 2021, 12:01am UTC](https://community.simplefoc.com/t/samd-support-working/492/47 "2021-05-18T00:01:44Z")

</div>

Hey malem,

I saw all your changes, thank you so much, its awesome. I’d like to merge them in, but your pull-request has conflicts in the Commander.cpp. There are too many changes to that file that happened in parallel.

Could you send the pull request again without the commander changes (for example by moving them over to a fresh copy of the dev branch)? Then I can merge your pull request.

Or would you prefer I merge your changes over? (absolutely no problem for me to do so, but then the contribution isn’t tracked by GitHub the way it would be in a pull request)

I’d love to try out your current sensing code!

Regards,

Richard

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 18, 2021, 1:11am UTC](https://community.simplefoc.com/t/samd-support-working/492/48 "2021-05-18T01:11:07Z")

</div>

You’re trying to merge my fork with the ADC code as-is?

I’m confused. I only sent a pull request with the samd debug code, and maybe I told you about my drv8305 code. What are-you trying to merge exactly, I’ll be happy to help…

on a side note. I wish I knew about that kind of C syntax with the :3 offset

```auto
typedef union {
	struct {
		uint8_t OCP_MODE:2;
		uint8_t OCP_LVL:1;
		uint8_t OCP_RETRY:1;
		uint8_t OCP_DEG:2;
		uint8_t OCP_CBC:1;
		uint8_t DRV_OFF:1;
	};
	uint8_t reg;
} Control__4;

```

I simply ignored it existed, my drv8305 code would be much better with it. I’ll try to improve it later on

for the ADC code, I’m still improving it. it’s not ready, frankly…

Oh and btw… the driver branch is named “Ardunio”… no biggie. I don’t think it’s too late to rename it

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 18, 2021, 1:27am UTC](https://community.simplefoc.com/t/samd-support-working/492/49 "2021-05-18T01:27:17Z")

</div>

> [@malem](#):
>
> I’m confused. I only sent a pull request with the samd debug code, and maybe I told you about my drv8305 code. What are-you trying to merge exactly, I’ll be happy to help…

Hey, when you push more changes to the same branch, they automatically get added to the pull request. So I see your ADC code as part of the pull request now.

> [@malem](#):
>
> for the ADC code, I’m still improving it. it’s not ready, frankly…

Ok, then we can certainly wait until you think it is a good time. Note that on the dev branch, and as an optional component that only affects people deliberately using it, it would not be a problem if it isn’t perfect yet. 🙂

> [@malem](#):
>
> What are-you trying to merge exactly, I’ll be happy to help…

The changes to the SAMD21 driver code, the refactoring of the Serial debug output.  
I’m very happy to just merge it myself, but I really don’t want it to look like I’m “stealing” your contribution… so if you’d be prepared to separate it from the commander changes, then I could directly accept your pull request.

When working with pull requests, it is a good idea to make separate branches for everything you’d like to contribute, and start a new branch each time for different features…  
Otherwise the commits you add later (e.g. after “sending off” the pull request, but before it is merged) become part of that pull request and it can become difficult to merge it in (likelihood of conflicts rises, harder to see what’s going on, etc…).

It’s also important to pull in the forked repos changes into your branch regularly to make sure your branch isn’t getting too far behind.

So in the context of what’s going on now it would mean creating a new branch of the SimpleFOC dev branch, and getting rid your commander changes, and keeping just the driver code changes in that branch.  
You could make an additional branch (from simpleFOC dev) for the ADC changes if you don’t want them merged yet…

Thank you so much for all the work you’re putting into this, and sorry to be a pain about the git!

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 18, 2021, 1:33am UTC](https://community.simplefoc.com/t/samd-support-working/492/50 "2021-05-18T01:33:30Z")

</div>

Oh that! I think @Antun_Skuric already merged it, that’s why I kept pushing. my bad. I thought you knew.

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 18, 2021, 1:34am UTC](https://community.simplefoc.com/t/samd-support-working/492/51 "2021-05-18T01:34:38Z")

</div>

But yeah you’re right, I’ve been using my fork as a a dev branch, forgotting about my MR.

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 18, 2021, 3:06pm UTC](https://community.simplefoc.com/t/samd-support-working/492/52 "2021-05-18T15:06:39Z")

</div>

I fixed the conflicts.  
Please read [Dev by maxlem · Pull Request #79 · simplefoc/Arduino-FOC · GitHub](https://github.com/simplefoc/Arduino-FOC/pull/79#issuecomment-843248792)

I’ll work in a cleanlier way next time.

~~**beware** LowSideCurrentSense.getPahseCurrents() return **volts** right now. I’m still debugging this.~~

After all I completely removed my LowSideCurrent class. using the new LowsideCurrent instead.  
To submit to the new hardware\_api I decided to to a new function \_startADC3PinConversionLowSide()

I did try to hook myself in the DMA complete event to trigger the next conversion immediately but that completely destroyed my control loop timings, motor became unstable)

Now I would need to hook the conversion to som PWM timer event like it was done in @Antun_Skuric esp32\_mcp.cpp

Do you have hints?

from an API perspective, I’d suggest a callback function passed to the Driver class. then mcu-specific code would call it from the right ISR

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 19, 2021, 2:33am UTC](https://community.simplefoc.com/t/samd-support-working/492/53 "2021-05-19T02:33:22Z")

</div>

Thank you so much for all that hard work! I have merged it into the dev branch, and will test it out as best I can in the next days.

> [@malem](#):
>
> Now I would need to hook the conversion to som PWM timer event like it was done in @Antun_Skuric esp32\_mcp.cpp

My approach was to try to use the SAMD’s event system for this. Your choices are basically events or interrupts, but I think events are faster, and need no additional MCU resources at all, i.e. would be completely parallel to your application code, which the interrupts are not (application code is halted while interrupts are executing).

I would completely ignore the TC units and only concentrate on the TCCs. People who want current control should use TCC pins.  
We do phase-correct PWM (up-down counting) - so the TOP value represents exactly the middle of the PWM on-period for the high-side, and the BOTTOM value represents the middle of the PWM on-period for the low-side (which has inverted output).

Personally, I would just attach to the high side, then the code can probably remain the same for 3-PWM and 6-PWM.  
So basically for low side sensing I think you always want to generate events when the counter reaches the bottom value (turns around). You do this by setting PWM mode “DSBOTTOM” and setting EVCTRL.OVFEO=1 (I think!).  
Then, on the ADC side, you trigger a START conversion on this event. You can set EVCTRL.STARTEI=1 on the ADC, not quite sure what if any other settings are needed.

Finally, in the event system, you have to route the event from the TCC to the ADC. I need to find an example for that, the datasheet isn’t super-clear…

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 19, 2021, 3:11pm UTC](https://community.simplefoc.com/t/samd-support-working/492/54 "2021-05-19T15:11:59Z")

</div>

Thanks a lot for the very detailed hints. I finish something my (fast, binary) datalogger/real-time plotter and I start working on this.

I’ll try to present you with a cleaner MR this time

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 19, 2021, 11:58pm UTC](https://community.simplefoc.com/t/samd-support-working/492/55 "2021-05-19T23:58:20Z")

</div>

I began looking into it. Enabling event generation from the tcc and event reaction on the adc is straight forward. The routing seems a bit trickier. Atmel doc isn’t so bad, I’ll manage.

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 20, 2021, 10:39am UTC](https://community.simplefoc.com/t/samd-support-working/492/56 "2021-05-20T10:39:25Z")

</div>

I found this thread for SAMD51, not sure how similar the configuration is in SAMD21:  
[https://community.atmel.com/forum/evsys-issue-atsamd51](https://community.atmel.com/forum/evsys-issue-atsamd51)

Maybe it helps?

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 20, 2021, 3:09pm UTC](https://community.simplefoc.com/t/samd-support-working/492/57 "2021-05-20T15:09:22Z")

</div>

a.w.e.s.o.m.e!

exactly what I needed!

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 20, 2021, 11:38pm UTC](https://community.simplefoc.com/t/samd-support-working/492/58 "2021-05-20T23:38:11Z")

</div>

After all, the examples in this forum weren’t directly usable. There are non-trivial (well to me, at least) register definitions differences which made the example hard to port to my samd21.

After many failed trials, I went back searching for a closer example and found [this](https://community.atmel.com/forum/atmelstart-suggestion)

I have a working POC which allows me to control the ADC sampling rate via TCC0 pwm frequency.

```auto
uint16_t result = 0;
int i = 0;
void ADC_Handler()
{
  result = ADC->RESULT.reg;
  i++;
}
void loop() 
{ 
    SerialUSB.print("adc( ");
    SerialUSB.print(i);
    i = 0;
    SerialUSB.print("): ");
    SerialUSB.print(result);
    SerialUSB.print(" - ");
    SerialUSB.println(result / 4095.0 * 5.0);
    // ADCsync();
    delay(1000);
}

```

where i varies predictably.

Tomorrow I wrap this up.

---

<div class="post-metadata">

### Author: ![runger](https://community.simplefoc.com/user_avatar/community.simplefoc.com/runger/32/395_2.png) [@runger](https://community.simplefoc.com/u/runger)
#### Post date: [May 21, 2021, 12:42pm UTC](https://community.simplefoc.com/t/samd-support-working/492/59 "2021-05-21T12:42:40Z")

</div>

Hey, that’s really great! I look forward to trying it out!

---

<div class="post-metadata">

### Author: ![malem](https://community.simplefoc.com/user_avatar/community.simplefoc.com/malem/32/1169_2.png) [@malem](https://community.simplefoc.com/u/malem)
#### Post date: [May 21, 2021, 11:24pm UTC](https://community.simplefoc.com/t/samd-support-working/492/60 "2021-05-21T23:24:10Z")

</div>

Hey,

After spending some time trying to cleanup SAMD21 variant.cop mess this morning, I continued on this topic.

You can find the changes [here](https://github.com/maxlem/Arduino-FOC/tree/feat_pwm_adc)

the commit message goes like this

> FEAT samd21 low side current sense, sync with pwm  
> use of EVSYS to trigger ADC conversion at each TCC OVF  
> removed DMA code as I couldn’t figure out how to combine both
> 
> Problem1: Currently I only get straight 1.71V +/- 0.01 no matter what happens  
> with the motor /control loop. Something is off with the timing(1)  
> Problem2: all OVF signal triggers the ADC, which is configured in INPUTSCAN mode and iterate over the 3 channel at each event trigger. while events come from different channels, they all sink into the same USER channel and there is now way of correctly mapping e.g. TCC0 to ADC pinA
> 
> (1) This was not the case with my previous FreeRun/DMA version, when I could see voltages reacting to my hand forcing the motor out of it’s target angle (angle controlo mode)

[edit: the nominal value for the output sence on the DRV8305 is 1.67V]

[Previous page](https://community.simplefoc.com/t/samd-support-working/492.md?page=2)

[Next page](https://community.simplefoc.com/t/samd-support-working/492.md?page=4)
