Using SAM4S - Cortex M4

TL;DR: I am trying to learn how to use the SAM4S Atmel chips with the Arduino IDE. I don't know how one goes about that. Would I have to write a custom bootloader? That seems hard!

I've been working on a small signal analysis project that I am seeking to commercialise down the road. I've initially developed with dsPIC33s but want to move to something compatible with the Arduino IDE to allow my technical end users to more easily tinker with the product. A key feature I require is decent FFT support, and my research shows me the Teensy is out ahead here because of the Cortex M4 chips having some sort of hardware specifically for these sort of operations.

One obvious road would be to use the Teensy 3.1 to develop, and to use the pre-programmed MINI54 chips from PJRC to make my product "Teensy compatible", and easy to use with the Arduino IDE. From what I have read, the MINI54 is just a bootloader. Would it then be feasible instead for me to use something like the SAM4S Cortex M4 chips from ATMEL and hook in USB directly to them for programming with the Arduino IDE? Or is there another step involved that I am missing here? Any guidance would be greatly appreciated as while I have some microcontroller experience, I am new to the world of USB programming/bootloaders/Arduino, etc. Back in my day you had a dedicated programmer and that was it!

Thanks!

I was looking at SAM4S recently, it looks like a nice chip. It follows the same programming method as SAM8X (Due), an ERASE line is held high during reset to erase the chip and boot from ROM bootloader. The user can perform the erase sequence via buttons, or the 16u2 on the Due does it when requested.

If you want the seamless user experience, and avoid the use of extra hardware, then a support chip such as 16u2 is essential.

Of course, uploading to the chip is less than half the story. The other part is porting the Arduino API to the new chip, and also any libraries that you want to support, that is the major work. After that, creating a "plugin" framework for the Arduino IDE is relatively easy, I have done this for LPC810 and Maple STM32 boards.

I'd like to create an ARM board for Arduino with a JTAG capable interface, based on CMSIS-DAP, that would add breakpoint and single step capability, I am waiting to see what sort of IDE the Arduino Zero will have.

Ah yes. Porting the API and libraries is probably too much work at the moment. Not confident I could get familiar enough with the bare-metal assembly to do it. Thanks for the pointers!

louii:
Ah yes. Porting the API and libraries is probably too much work at the moment. Not confident I could get familiar enough with the bare-metal assembly to do it. Thanks for the pointers!

Have you tried running any FFT benchmarks on Due vs Teensy 3.1?

The Teensy 3.1 CPU has DSP instructions but does not have a hardware FPU, and a slightly lower clock speed, it would be interesting to see how it compares to an M3 or M4 with FPU.

You would need libraries optimised for the Cortex DSP instructions to make use of Teensy 3.1, otherwise I would expect a similar performance to a Due. I know Paul and others have implemented FFT code for Teensy 3x, so worth asking over there.

This may be even more work that Bob's suggestion, which is probably why he's not mentioned it, but you could consider the STM32F4 series devices

e.g.

STM32F407VGT6

http://www.ebay.com/itm/161363564348

which operates at 160Mhz

or

STM32F429
http://www.ebay.com/itm/161361542829

which AFIK runs at 180Mhz

http://www.st.com/web/en/catalog/mmc/FM141/SC1169/SS1577/LN1806?ecmp=stm32f429-439_pron_pr-ces2014_nov2013&sc=stm32f429-439-pr

Bob did work to bring support for STM32F1xx series to Arduino, which I've basically picked up and run with. Bob's work is based on the work for the LeafLabs "Maple" and "Maple mini" boards, which are STM32F103 . Maple no longer make the boards, but there are plenty of cheap generic STM32 boards available from suppliers in the far east, and I have succesfully got 2 generic STM32F103 boards working using a USB to serial

But unfortunately the STM32F1xx series doesn't have FPU etc, so is probably too slow for you. (eve though it runs at 72mhz)

And I'm not sure how different the F4xx series devices if from the F1xx devices and hence how difficult it would be to add the necessary low level hardware variant files to the current working system for F1

I have been meaning to order an F4 board, but as I've still not got SPI working with the Arduino IDE on the F1, it would just end up sitting on the shelf.

bobcousins:
Have you tried running any FFT benchmarks on Due vs Teensy 3.1?

The Teensy 3.1 CPU has DSP instructions but does not have a hardware FPU, and a slightly lower clock speed, it would be interesting to see how it compares to an M3 or M4 with FPU.

You would need libraries optimised for the Cortex DSP instructions to make use of Teensy 3.1, otherwise I would expect a similar performance to a Due. I know Paul and others have implemented FFT code for Teensy 3x, so worth asking over there.

Very interesting. Yeah, I'll investigate that for sure. Still waiting for my Due to ship but very keen. If I were to develop on the Due, what are the legalities involved with replicating parts of that in my design? i.e. if my product has the same MCU as the Due and a 16u2, and uses the same bootloader etc. to enable it to be Arduino IDE compatible and show up as an Arduino DUE to the software, is that "okay" or would I then be stealing intellectual property? Is it illegal or legal or "frowned upon"? Trying to get some scope because I'm aware some parts of Arduino are open source but yeah.

As an aside, where does one start if they want to port Arduino to a new MCU platform? Not necessarily sure I'm up to tackling it just yet but it would be good to know what is involved.

Are there any good writeups where someone has walked through the process of porting Arduino to a new platform?

The only resources I know of are Arduino IDE 1.5 3rd party Hardware specification · arduino/Arduino Wiki · GitHub and Arduino Hardware Cores migration guide from 1.0 to 1.6 · arduino/Arduino Wiki · GitHub may be useful.

Mostly though, I studied the Arduino Due, Maple and LPC810 ports to figure out what is needed. ChrisMicro did the Arduino API for the LPC810, and it might be a good starting point as a minimal implementation (the Due is quite a beast!)

Since a compiled Arduino sketch is basically just a C++ program with two user provided functions, setup() and loop(), I would try to do main development outside of Arduino IDE. Then you can use a JTAG debugger (e.g. SAMICE).

I think my general plan would be :

  1. get a project working under a native tool, e.g. Atmel Studio in this case
  2. implement the Arduino API functions, at a minimum digitalWrite etc
  3. create a makefile version of the project.
  4. create platforms.txt and boards.txt for your board, and a file structure that the IDE expects

At this point, you should be able to start the Arduino IDE and select your board variant, and then compile a blinky sketch.

You can then implement as much of the API as you like, then go on to create standard libraries such as SPI, Wire.

The next tricky thing would be to implement a programmer/bootloader that the IDE can use to upload sketches, but in the case of SAM4S that should be ready to go as you can use the builtin SAMBA bootloader (like Due "native" option). It should just be a matter of calling bossac with the right parameters.

If you want to implement the Due "programmer" option then you will need something equivalent to the 16u2, possibly it might work for SAM4S with few changes. Basically all the 16u2 does is automate the Erase sequence.

To answer your other questions, most of Arduino is open source, so you can use it within the restrictions placed by the Open Source licenses. Note that "Arduino" is a trademark, so you should not use it as part of a name. If you create hardware or software that is entirely your own work, and distribute it as a separate entity, then it does not need to be Open Source to be "Arduino compatible".

Actually, having said all that I was just looking through the Arduino Due code. There is some support there already for SAM4S and other SAM3 chips in libsam, it might be the best starting point.

If you looking to create a new variant of existing chip (e.g. Due), then you might be able to create just the variant folder and reference the Due core. That is something I haven't tried yet.

Ah, I see! I understood almost all of that, I'll have to look into it once I can get some boards spun! Thanks for the info bobcousins, that was super informative :slight_smile:

louii : Although I am fond of using my Arduino Due boards for many things, it has been pointed out that they contain a Cortex M3 processor which will NOT excel at DSP calculations. Even the Teensy board (thanks for the tip on that, it was new to me) contains the version of the M4 core that lacks floating-point hardware, although it can do DSP instructions via floating-point emulation, i.e. more slowly in software.

Another poster mentioned the STM32F407 and STM32F429 boards from ST Micro. (I have and use one of each!) These would be more suitable, since they contain the Cortex M4F processor with hardware floating-point math and DSP instructions. Although they are not Arduino-compatible, ST Micro sells very inexpensive "Discovery Boards" with these processors and it is possible to obtain an IDE that is at least as powerful as the Arduino IDE at no charge, and I find it just as easy to use as the Arduino IDE. So that should not discourage you too much. ST even provides a software DSP library with source code that you can use on the board. I don't know what hardware you were planning to add to the basic processor board, as you will obviously need some way to get signals into the board and processing output out of the board, and you haven't really described that in great detail. Clearly, that MIGHT be a show-stopper for you, since the STM hardware lacks the excellent hardware support of the Arduino environment.

Another direction to consider, depending on what sort of digital signals you want to process, could be a project known as the STM32-SDR project. It is built around the STM32F407 processor. I believe that originally, the developers used a Discovery board to get started, but they now have an inexpensive board of their own design. It has the processor and significant interface hardware that allows them to plug the board into an appropriate RF front end board (such as a SoftRock Ensemble RxTx or a UHFSDR board). The SoftRock or UHFSDR board transforms radio frequency signals into baseband quadrature (I & Q) digital signals, and then the STM32F407 processes the I & Q signals into demodulated audio and an FFT spectrum display on a small LCD screen. It can also run in the opposite direction, i.e. taking microphone audio input or Morse Code key input and generating modulated RF output (if you have an Amateur Radio license to authorize you to do that). The whole system runs separate from a PC - the PC is only needed for developing and loading software, and they now even have a way to bootload new firmware from a USB flash drive. As you may have guessed, I also have one of these. (I'm such a software-defined radio junkie!)

I hope that some of this helps.
John P. Toscano, W0JT

@W0JT

My repo now has some basic support for F4 (with work done by Bob Cousins), however only the very basics currently work e.g. blink.ino works ! but Serial doesnt (and I'm not sure is SPI or I2C would work).

There is now another project which now supports STM32F3 boards, which is not based on the old LibMaple code, but is a clean room implementation based on STM's own source code files for hardware abstraction

See

They plan to support F4 at some point, and I think they will get F4 working before we do, as they only require minimal coding changes because they use existing files for the F4 from STM

I notice the OP hasnt posted for a while, so perhaps they have given up on the idea :wink:

Hi roger :slight_smile:

rogerClark:
See

GitHub - avikde/koduino: Arduino code for STM32 microcontrollers

They plan to support F4 at some point, and I think they will get F4 working before we do, as they only require minimal coding changes because they use existing files for the F4 from STM

You say "they" but it is just one guy, so I would not expect much from that. I had a look at the repo and there is still a lot of work to get to the level of where the Maple codebase is, and the support for other architectures is minimal. I think he has only scratched the surface on getting multiple platforms supported, it is not quite a cut and paste :slight_smile:

I still think the Maple codebase is the best Arduino like STM32 implementation, it might be that using ST's SPL or something like OpenCM3 would be a better base, but it's like choosing which bricks to use : you still need to build the house!

I've been looking at https://mchck.org/ recently, the Freescale Kinetis chips have Cortex M4F versions cheaper and faster than STM's offerings. The Kinetis chips only have a primitive bootloader, but I think that can be worked around.

Software wise, the Teensy 3 uses K20 series, so it should be fairly easy to move teensy code to K22 or similar. There are people running teensyduino with McHck, but McHck guys are writing their (non-Arduino) code.

Hi Bob

You say "they" but it is just one guy, so I would not expect much from that. I had a look at the repo and there is still a lot of work to get to the level of where the Maple codebase is, and the support for other architectures is minimal. I think he has only scratched the surface on getting multiple platforms supported, it is not quite a cut and paste :slight_smile:

OK.

I was just going on the emials that were sent to me by the author of koduino who claimed that virtually everything was working apart from shiftOut and a few other things.

I've don't have an F3 board, so have not been able to compile the GitHub - avikde/koduino: Arduino code for STM32 microcontrollers code
to see how much actually works.
(and I've not had time to look at getting his Variant code for the F103 or F407 working)

I was hoping that perhaps koduino was a much cleaner approach to STM32, as it uses the same code that Em:Blocks etc use, as its from STM, and saves having to write all the hardware support stuff from scratch like LeafLabs did.

But it sounds like there is a still a long way to go, and as you say, there only appears to be one person doing the dev work at the moment.

I've been looking at https://mchck.org/ recently, the Freescale Kinetis chips have Cortex M4F versions cheaper and faster than STM's offerings. The Kinetis chips only have a primitive bootloader, but I think that can be worked around.

Interesting..

You know about Teensy 3.1? It is a Cortex M4 and supports, literally, the majority of the AVR code without changes, due to an amazing emulation library.
Also, new Teensy3LC - low cost. < $12 for M4.

(I have no stake in Teensy’s company, PJRC)

stevech:
You know about Teensy 3.1? It is a Cortex M4 and supports, literally, the majority of the AVR code without changes, due to an amazing emulation library.
Also, new Teensy3LC - low cost. < $12 for M4.

(I have no stake in Teensy’s company, PJRC)

The Teensy was discussed several times already, did you even bother reading the thread before posting your spam?

Bob

@stevech also posted a strange comment on the STM32 tread saying we should use the ATMega1284 if we need more ram.

Obviously he'd not read that thread either, or looked at the specs on the stm32f103 series of devices.

TI Tiva launchpad has cm4f and is supported by energia...

bobcousins:
The Teensy was discussed several times already, did you even bother reading the thread before posting your spam?

Yeah, I'm not really interested in the Teensy. The problem I have is I need my M4-powered product to be user-reprogrammable in the Arduino IDE because that's what my customer wants. Short of putting a Teensy 3.1 in every product, or paying PJRC $8 a chip for their MINI54 bootloader chip, it's not happening.

I'm trying to research now if I can find a closely pin compatible M4 chip that I can drop into an Arduino Due after lifting the SAM3X8E. The SAM4E8E looks promising!

Okay unlike DIP PICs that's not going to work, they've got completely different pinouts. :frowning: I might try and spin a board with a 16U2 with the Due bootloader on it, and a SAM4E8E, then try and modify relevant libraries and such.