Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Porting Atmel studio to an arduino library - SAMD BSP - Edit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Porting Atmel studio to an arduino library - SAMD BSP - Edit

by moonie223 on Wed Sep 16, 2020 1:12 am

Hello again,

I am attempting to create a SAME51 variant for the canbus controller, in an attempt to shrink a project currently using a SAMD51 and MCP2515 controller. The end goal is something in the shape of a oil pressure gauge in a car that reads out live data. This is my first version, using a teensy 3.6, where I learned you can't run 4 foot of high frequency SPI wire through your car and expect it to work...

https://www.youtube.com/watch?v=5pkusOi-U0U

So I decided to just use a smaller microcontroller so my SPI lines can all be on board, so I picked a itsy m4 to develop around. Here is a more recent version, using the itsy/samd51/MCP2515. This works a lot better, no display corruption! I also built a 24 bit SK6812-EC1515 "neopixel" ring and light sensing dimming for the LEDs. These tiny little leds are awesome, should check them out!

https://www.youtube.com/watch?v=kqzYxQuiMGU

I've designed and built a SAME51G19A board, with a built in TCAN332 and digital bus termination using a mosfet array. The board files are here if you want to take a look.

I also struggled through forking the adafruit SAMD BSP and getting it to compile with a custom variant, building a UF2 bootloader, as well as building a new bossa that will upload code. Also went ahead and changed VIDs and PIDs and somehow made a windows driver that works properly. Somehow, I managed to make an arduino compatible SAME51 board! It even blinks LED_BULLETIN, other things might work too, but led!

I've also managed to make some atmel start examples into workable code and have a few of my boards communicating over canbus with a SAME54Xplained board. Not much, but I think I understand the main bits of clocks on these things, having the debugger and breakpoints is nice. My mosfet array for programmable bus termination seems to work too, and small! Smaller than a 2 pin 2.54 header!

The part I am now lost at, what would be the best way to take my working start code and making a library from it? I believe there is a good example here but this is for a SAMC21.

I know asking something as vague as "how do I port this to that" is pretty much impossible to answer and so isn't my question, but what would be the best way to follow through the arduino style BSP so I can compare with atmel studio? There's gotta be a better way than picking through files with a notepad editor and manually searching for references, right?

As an example, not sure if this would be the best way to do it, but I made a __SAME51__ variant for the __SAMD51__ variant. I am pretty sure I can use this to make another startup config so I can divide out a 40MHz or whatever I end up using gclk for the can peripheral, and doing it this way means I can keep all the SAMD51 stuff untouched when I want to use my genuine adafruit stuff.

But, I went through everything manually and added or ifdefs everywhere using notepad. There has to be a better way!

I guess my real question is, if you were to set out doing something similar what would you use to do it? And, Is there any way to debug the Arduino environment like atmel studio does?

Thank you in advance, and thanks for reading!

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by adafruit_support_mike on Thu Sep 17, 2020 3:00 am

Unfortunately, the real solution is to be familiar with both source trees.

The Arduino environment isn't too bad. Most of the code lives in a board support package's cores/arduino/ folder. The files there provide the low-level code that makes the Arduino environment's convenience functions like digitalRead() work.

Vendor source trees are harder to work with, and in some cases are deliberately obfuscated. I don't remember the chip family off the top of my head, but remember seeing one with a layer of recursive macros and includes whose only purpose was to make it impossible to trace dependencies by reading the files directly. For those, the only effective solution is to get familiar with the C preprocessor and have it trace the connections and print the dependency graph, along with the fully included-and-expanded source that goes to the compiler.

At some level, both environments are built on the same core source libraries. The part you have to do the long way is learn that common core, then learn the basic mindset of the environments built on it, then learn how to move ideas from one environment to the other.

adafruit_support_mike
 
Posts: 61407
Joined: Thu Feb 11, 2010 2:51 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Thu Sep 17, 2020 1:33 pm

I think I have something set up and working, but I think I am missing reference files, mainly DFP files?

I have my board working with a fork of the adafruit BSP, in boards.txt I still have it defined as -D__SAMD51G19A__ and everything works. I've also perused through all of the adafruit/cores/arduino files and found where Wvariant actually calls specific processors for timers. I believe that's the last of anything from the adafruit BSP I could change.

But if I change to -D__SAME51G19A__ everything breaks and if I had to guess it's missing DFP files.

Where's that come from? I'm pretty sure with arduino SAMD it's arduino/tools/cmsis-atmel/1.2, and with adafruit SAMD it's adafruit/tools/cmsis/5.4. I can see where you could add DFPs to cmsis-atmel, but not to cmsis 5.4?

"For those, the only effective solution is to get familiar with the C preprocessor and have it trace the connections and print the dependency graph, along with the fully included-and-expanded source that goes to the compiler."

What tools would one usually use for doing this? Would this be how I could find exactly how sam.h and similar are sourced?

Thank you!!

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Thu Sep 17, 2020 4:32 pm

I found a way to open the BSP folder in VScode, which sure makes looking at everything at once much easier but I still can't be sure where some external files are coming from. It looks like the adafruit SAMD BSP depends on the official SAMD core for compilation? If adafruit were using the packages/arduino/tools/cmsis-atmel folder, I can see why my SAMC21 example would work and not my SAME51 example. But adding the missing files for same51 isn't enough...

It took me long enough to figure out silly path problems building simple stuff, this one is gonna take me a while!

I think I may have found a solution, the adafruit SAMD fork appears to have a list of what it needs to build in /workflows/ and github does it for you? I could test all the libraries at once, and make github do it?! But I could use or copy this environment and then somehow make VScode use those paths for includes. Then I should be able to see where most of these files are coming from, especially if I found a way to simulate these compiler defines so it quits greying things out I'm trying to use.

Super nooby, but does it sound like I am on the right track at least?

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Thu Sep 17, 2020 7:26 pm

I think I got it!

It was arduino/tools/CMSIS-Atmel/1.2.0/CMSIS/Device/ATMEL. It doesn't know anything at all about the SAME51 series, so if you try and compile with a micro that's undefined in sam.h from the above location it looses references to pretty much everything! So simple now that I know it...

I did some hacking slashing and edited auto generated files that told me not to, but added the DFP files for the same51 family. I went ahead and removed all my __SAME51__ calls because that really doesn't matter, and if I'm not mistaken I only really need to edit one file in the BSP outside boards.txt, and that's Wvariant.h

So now when I compile asking for ID_CAN1, I get a "'ID_CAN1' was not declared in this scope; did you mean 'ID_CAN0'?"

If I recompile with a itsy m4 selected, it starts asking if I want "tan". So, that's a bonus! The SAME51G19 I am using only has one can peripheral, and a SAMD51 always has none. It's doing something!

I might go back and try and generate the auto generated files properly, maybe ask for a pull request from the arduino tools. There's all sorts of processor support there that's totally unused, so I don't think they would mind.

Here is what I'm working with, if anyone wants to take a look.

https://github.com/deezums/ArduinoCore-same

https://github.com/deezums/ArduinoModule-CMSIS-Atmel

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Sat Sep 19, 2020 12:49 am

Managed to make it work, I've got canbus comms between my SAME51 board and a MCP2515, all using arduino!

https://github.com/deezums/same51_can

Probably won't work with a SAME51N, I might have broke dual canbus and left a lot of remnants. My CPU doesn't have dual can, so I can't leave CAN_1 references, or I didn't take the time to make CPU specific code at least.

You guys really should offer a SAME51 variant, might need an external transceiver but you could just swap chips on a itsy, D12 and D13 are open!

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by westfw on Sat Sep 19, 2020 4:05 am

you could just swap chips on a itsy

You should be able to find the full .h files and such here: http://packs.download.atmel.com/
(a ".atpack" file is just a .zip. It may take a bit of creative copying to get the right include files in the right places, but ... it should all be at a file/directory level, rather than editing individual files.)

Be careful not to get the new Microchip-style include files (pack version 3+ ?) (ie from https://packs.download.microchip.com/ )
They changed style (for better compatibility with PIC chips?), and everything is different and incompatible with existing code :-(

westfw
 
Posts: 1715
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Sat Sep 19, 2020 6:25 pm

There's a slight bit of inconsistencies between those atmel files and what arduino uses (cmsis-atmel 1.2.0)

Someone smarter than me can probably read the makefiles included in the repo and have it automatically create the right folder structures and samx.h files from the atmel files or at least that would make sense, I just copied the folder hierarchy from samd51 and edited a few files.

It also looks like someone other than me added same51 support to the adafruit/uf2-samdx1 bootloader, it appears to be an exact copy of the same51 DFP files. Pretty handy for taking a quick glance, That's how I found can is peripheral mux 8, or "I" on this particular CPU.

Looking at finding the fastest I can clock the thing now and maybe adding an additional gclk. Then, lots of testing filters and buffers and all that.

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by westfw on Sun Sep 20, 2020 4:42 am

https://github.com/WestfW/Atmel_ATDF_parser

Teaser:
Code: Select all | TOGGLE FULL SIZE
ATDF File for Device: /Volumes/MacOS/HD-Downloads/Downloads/Atmel.SAME51_DFP.1.1.139/atdf/ATSAME51G19A.atdf

showInstance("CAN0")
PA22 CAN0 TX pmuxI
PA23 CAN0 RX pmuxI
PA24 CAN0 TX pmuxI
PA25 CAN0 RX pmuxI

showPortPin("PA22")
  ADC0: X16 pmux:B
  ADC0: Y16 pmux:B
  CAN0: TX pmux:I
  CCL: IN6 pmux:N
  EIC: EXTINT6 pmux:A
  PCC: DATA6 pmux:K
  PORT: P22 pmux:default
  SERCOM3: PAD0 pmux:C
  SERCOM5: PAD1 pmux:D
  TCC0: WO2 pmux:G
  TCC1: WO6 pmux:F

showPortPin("PA23")
  ADC0: X17 pmux:B
  ADC0: Y17 pmux:B
  CAN0: RX pmux:I
  CCL: IN7 pmux:N
  EIC: EXTINT7 pmux:A
  PCC: DATA7 pmux:K
  PORT: P23 pmux:default
  SERCOM3: PAD1 pmux:C
  SERCOM5: PAD0 pmux:D
  TCC0: WO3 pmux:G
  TCC1: WO7 pmux:F
  USB: SOF_1KHZ pmux:H


PA24 and PA25 conflict with USB...

Code: Select all | TOGGLE FULL SIZE
showInstance("USB")
PA23 USB SOF_1KHZ pmuxH ioset: 1
PA24 USB DM pmuxH
PA25 USB DP pmuxH
PB22 USB SOF_1KHZ pmuxH ioset: 2

westfw
 
Posts: 1715
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Porting Atmel studio to an arduino library - SAMD BSP -

by moonie223 on Sun Sep 20, 2020 2:40 pm

I've been using PA22 and PA23 for CAN so I can still use 24/25 for USB. USB SOF_1K can't be used then, but from what I know so far I don't think that matters since the CPU has a semi-accurate USB clock.

I'm not sure if going "crystalless" is going to bite me on can bit timings, must not on USB, but I have both external clock input pins open and available if it becomes a problem.

That parser is pretty cool, sure beats picking everything out manually! Thank you!

Now I am trying to make an arduino BSP separate from adafruit's using what I've made except uses the external tools I have made (bossa, cmsis-atmel) so I can eventually use github's neat workflows thing to test all my builds and libraries for me. Would be nice to install everything I've made through arduino package manager too, for my other pcs. Seems simple enough.

moonie223
 
Posts: 15
Joined: Sun Feb 05, 2017 2:44 pm

Please be positive and constructive with your questions and comments.