have been a user of STM32Duino for a while and liked its simplicity but not the IDE. Never bothered to do anything about it, however.
Only recently a friend of mine got me thinking of porting Arduino to some PIC24F/dsPIC chips and I started to take the same approach to port Arduino on STM8S chips.
Once that’s done, I took it to a more modular level and got the code running on a generic Cortex-M chip – in this case, a STM32F100 chip on the STM32VLDiscovery board. Otherwise, the code should also work for STM32F10x chips.
The implementation is fairly barebone at this point, but most Arduino functions are implemented, short of serial comm, analog read/write and external interrupts.
It is implemented via a layered / compartmentalized approach where hardware dependent code is separated from hardware independent code, allowing the user to hook in different modules for his/her target mcu and the code will compile and run with no or minimum changes.
So far, I have got it running on STM32F100/103, MSP432 and LM4F120. The details / code are available here: https://dannyelectronics.wordpress.com/ … x-m-chips/
it is not complete so watch out for updates -> the code is now at v0.11, vs. v0.10 posted, for example.
Your comments are greatly appreciated.
do you know the STM32GENERIC project? There you can easily add every board you have.
STM also started to support more MCUs:
viewtopic.php?f=48&t=2098
Several other people and already use the name ARMduino
https://www.google.com.au/search?q=armduino
I dont think its trademarked, but it is rather confusinb if you search for that term
PS. i also moved it to Cores
PPS.
As well as @danieleff’s STMGeneric core, and STMs own Cores, there is Koduino which is also an Arduino core for STM32F3 and STM32F4, plus our old faithful of Libmaple, plus several abandoned cores like the MakerlabMe core
However you core appears to be the only core that spans different manufacturers, apart from of course mbed
But mbed is not Arduino, its just a similar dev paradigm
1. our approaches to gpio are identical.
2. the stm32generic project isolated the device-specific things into individual files.
we differ in that his aim is to produce a generic template for STM32, and my goal is to produce a generic template for all ARM chpis – but not writing the device-specific things myself.
but two projects are quite expandable into each other’s spheres.
Rick: i’m taking a C-approach to the porting so all Cpp functions have to be replicated in C, sometimes with slightly different names. like Random(min, max) gets maxed to Random2(min, max), as the Random(max) already exists.
Ken: the STM8S project is linked on the ARMduino page but you can click it here: https://dannyelectronics.wordpress.com/ … tm8s-mcus/
I just ported the analogWrite() and analogRead() functions but have not had the time to update the code yet. will do that later.
Sadly, the problem with that approach is that you miss out on all the rich libraries that people have written for the Arduino platform. I think it is unlikely people will port C++ libraries to C to work with your core. The 3rd party libraries are why people put up with the inefficient Arduino platform in the first place. At least that is how I see it.
Unfortunately true: if the compiler doesn’t support cop, there is no way around that.
But if you step back and the situation may be a little different, as you have two options here:
1. Don’t support Arduino as the compiler doesn’t support all Arduino code. Or
2. Support at least some Arduino code (those coded in C).
I took the later approach knowing that some of the cop code will have to be rewritten.
The downside of that approach, particularly to the Arduino crowd, is that they may find it too challenging to e ven attempt.
But then you are no different from if you had taken approach 1.
So approach 2 gives you some upside but no downside.
I don’t mind having to pay for a quality compiler. You get more than your moneys worth in one project.
What matters to me is that the vendor offers quality products and services. On that front, microchip does a good job here. I do hope that they can invest more in marketing the pic24f however.
analogWrite() offers up to 16 bit of resolution and is available on 16 separate pins.
DACnWrite() utilizes the 12 bit dac channels to produce analog output.
Next is exti and serial comms.
yes. my code is not v0.11a, added pulseIn(), tone()/noTone(), analogWrite(), analogRead(), attachInterrupt() and detachInterrupt() – ugly implementation and I’m thinking of revamping it soon.
I wanted to get it to a critical state and then push it out, but can publish an intermediate version if you want to try it.
if you would consider hosting it on GitHub?
don’t know about that. don’t want to create an account when I don’t have to, ![]()
I’m watching this project on github and on blog.
Hope to find time to test it soon.
With that said, the code is fairly standard c so I wouldn’t expect much issue porting it over to a decent c compiler.
Of the two other compilers for stm8, I have limited experience with SDCC – played around it briefly and discovered that it would discard unused sections and moved on.
Porting to cosmic should be fairly easy – mostly around ist declarations.
If you want, show me the error log for SDCC and I can go through the document to help you port it to that compiler.
In the mean time I will make the code work for cosmic.
edit: the existing STM8Sduino code base actually was written to allow compilation under COSMIC and RAISONANCE compilers but I commented out the cross-platform stuff. so I have re-instated the support now for those two compilers and the code now should compile under all three compilers, IAR, COSMIC and RAISONANCE.
I also took a quick look at SDCC compiler manual and the only difference I saw was the declaration of interrupts. The INTERRUPT_HANDLER macro could be written fairly easily to map back to the SDCC style – ST showed how to do that towards the end of stm8s.h.
edit 2: I uploaded the revised files to provide support for COSMIC (tested) and RAISONANCE. aside from interrupt handling, COSMIC doesn’t support “inline” – fairly easy to deal with. Should compile for RAISONANCE – not tested however.
But, I have noticed a lot of cheap devices use the STM8, so having a way to repurpose those devices would be good.
e.g. I have a several LED panel mount volt meters which use the STM8, and I recently took a USB voltage and current dongle apart and it also contains a STM8.
Considering STM have literally sold billions of STM8 MCUs, I am sure having a usable STM8 Arduino toolchain would be very beneficial to device hackers
edit: the existing STM8Sduino code base actually was written to allow compilation under COSMIC and RAISONANCE compilers but I commented out the cross-platform stuff. so I have re-instated the support now for those two compilers and the code now should compile under all three compilers, IAR, COSMIC and RAISONANCE.
I probably will never use them, but I like to be prepared in case I need something like that ![]()
Testing with SDCC I get
In file included from stm8sduino.h:56,
from example.c:1:
stm8s.h:90:3: error: #error "Unsupported Compiler!"
stm8s.h:2735:25: fatal error: intrinsics.h: No such file or directory
compilation terminated.The complaints around line 590 on tmp is because it was declared in the middle of that function. I have since moved it to the beginning of the function.
Fixed that and see what else may pop up.
The complaints about unreachable issue to infinite loop.
Not sure about Evelyn and Dog.
Fixed that and see what else may pop up.
Again, those mentions of Evelyn / Dog mean nothing to me. Looks to be very SDCC specific.
[dannyf – Sat Jun 17, 2017 4:50 pm] –
I cannot find anything of interest on line 249 / 250, or 899 and 935. Maybe you can copy those lines over, +/- 5 lines?Again, those mentions of Evelyn / Dog mean nothing to me. Looks to be very SDCC specific.
I got rid of the ” Evelyn / Dog” messages by removing inline from
- void pinMode(PIN_TypeDef pin, uint8_t mode)
- void digitalWrite(PIN_TypeDef pin, uint8_t val)
- int digitalRead(PIN_TypeDef pin)
The “warning 116: left shifting more than size of object changed to zero” and “error 9: FATAL Compiler Internal Error” messages are in void mcu_init(void) at the following lines:
TIM1->ARRL = (PWM_PR); //top at 0x0fff
I will take a look at interrupts. It maps back to ei and I will see how interrupts are enabled or disabled in sdcc.
You can recreate that via assembly instructions “rim” and “sim”.
To make it more seamless, you can change the stm8s.h file, especially in the beginning where it complains about unsupported compiler – simply insert another #elif for SDCC and you are ready to go.
[dannyf – Sun Jun 18, 2017 9:37 pm] –
I took a look at the SDCC manual. The issue with interrupts() is its use of an IAR-specific call.You can recreate that via assembly instructions “rim” and “sim”.
To make it more seamless, you can change the stm8s.h file, especially in the beginning where it complains about unsupported compiler – simply insert another #elif for SDCC and you are ready to go.
I use stm8s.h from STM8S Standard Peripheral Library Ported to SDCC for multiple STM8 targets which contain the required #idefs for SDCC, including what is needed for interrupts, but still “error 9: FATAL Compiler Internal Error” is in void mcu_init(void). I have a feeling SDCC is not happy about something else in this function, and not about that specific line ![]()
Update:
I placed a simple for loop after the line SDCC complains about, and now interrupts() no longer generates the error, but the for loop after it… Will have to dig deeper
but the for loop after it.
that would suggest that the issue is with the preceding statements – you can confirm it by commenting out interrupts() in mcu_init().
What do your USE_XXX macros look like?
[dannyf – Sat Jun 17, 2017 1:19 pm] –
Those 4 digit volt meters use rx and tx pins routed out, making it possible for then to be daise chained together as a 4 digit standalone or 8 digit or 12 digit displays.
I ordered a couple of those units from eBay to test out that concept. While waiting for them, I wrote up a simple piece of code on a PIC16F193x (@16Mhz). So far, it has lived nicely on a simulator – I daisy-chained two of them together to form an 8-digit display. The code is available from github.
I would expect minimum changes to retarget the code for STM8.
I got SDCC installed and used it to compile stm8sduino.c. didn’t get any error, especially the critical error that you got. Just three warnings.
I used the latest SDCC for windows. 3.6.0 I think.
Here is what I am using:
- SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.6.6 #9921 (Mac OS X i386)
- stm8s.h from STM8S Standard Peripheral Library Ported to SDCC for multiple STM8 targets which contain the required #idefs for SDCC, including what is needed for interrupts
- the latest STM8Sduino code from github: https://github.com/dannyf00/STM8Sduino
- a “STM8S103F3P6 Minimum System Development Board” similar to these: https://goo.gl/9pkUuD
I am using your simple blink.c example:
#include "stm8sduino.h" //include stm8suduino defs
#define LED PB5 //LED attached to pin 13 (=PB5 on STM8Sblue)
#define LED_DLY 500 //waste some time, in ms
//user setup
void setup(void) {
pinMode(LED, OUTPUT); //set LED as output
}
//user loop
void loop(void) {
digitalWrite(LED, !digitalRead(LED)); //flip LED
delay(LED_DLY); //waste some time
}
[dannyf – Mon Jun 26, 2017 2:48 pm] –
Pb4 and pb5 are od pins and unable to source current. You can either sink current on those pins or move to a different pin.
Using your initial release (version: v0.10) and IAR EMSTM8, everything compiles and the LED on PB5 is blinking. However, with the latest release and IAR EMSTM8, the LED on PB5 does not blink. I will connect a LED to another pin and try again ![]()
your comments / feedback are most welcome.
edit: I added analogRead(), attachInterrupts(), detachInterrupts() and uart-related routines. PWM coming up next.
Probably an interval timer would be a good idea. With this many peripherals could be implemented in software ( as long there is no hardware driver ).
DanielEff did it here with PWM, Tone, analogWrite
So now I have 17 “virtual timers” that you can set their periods independently and install handlers independently. I can add a few more but I guess that’s more than one would need for 99% of the applications.
They can obviously be used to more accurately measure analog values, including temperature or Vdda; and they can also be used as another form of UID – which STM32F0 has “undocumentedly”, ![]()

