Newbie questions about writing apps for microcontrollers - Page 1
Forum section: electronics / general

author: Winfried
author_rank: Contributor
timestamp: July 10, 2022, 02:05:12 pm
content: Hello,I only have a shallow experience with embedded software, and have a couple of questions, especially regarding writing software for ESP and Arduino processors:1; What are the alternatives to run code on those processors?ArduinoAdafruit CircuitPythonesp-idf (= CircuitPython?)other?2. What is the advantage of using the Arduino IDE (ie. C) instead of eg. CircuitPython? Smaller + faster code?Thank you.

author: Ian.M
author_rank: Super Contributor
timestamp: July 10, 2022, 02:24:24 pm
content: ESP and Arduino aren't processors, they are names of classes of stuff, in the case of 'Arduino', of 'maker' friendly MCU boards with many many different MCUs ('processors') - I count ten in Wikipedia's list of official boards, and in the case of 'ESP' its the prefix Espressif use for all their MCU part numbers, but ESP-IDF only applies to their ESP32, ESP32-S and ESP32-C families of SoCs (also 'processors').The IDE you use has nothing to do with the programming language you use, except that the IDE has to support the language.Most of us would say the Arduino IDE is a disadvantage, not an advantage! The only things to be said in its favor is it is a convenient way for wannabee programmers who are 'hard of thinking' to get a GCC cross-compiler toolchain + bootloader utility + USB serial device drivers installed on a Windows PC or Mac, (i.e. where you cant just do: e.g.sudo apt-get install -y gcc-avrto install the AVR GCC cross-compiler for Atmel AVR MCUs), and its a convenient source of low-quality peripheral libraries, for when the effort of 'doing the job right' cant be justified for a one-off, you aren't pushing the performance envelope, and you *REALLY* don't care about efficiency or future maintainability.

author: pcprogrammer
author_rank: Super Contributor
timestamp: July 10, 2022, 04:22:00 pm
content: Well the title of the thread gives it away. "Writing apps for microcontrollers" show that you have to do some more studying.Writing software for microcontrollers is a very different ballgame then writing apps for Android or whatever OS based system. But that is the trend nowadays, we call all software "apps".To be a bit pedantic here (as if I was not already), the Arduino IDE works with C++ which is also different from writing plain C.As Ian.M already pointed out, the Arduino IDE is nice for some rapid testing of an idea or some dev board you bought on Aliexpress or Ebay, but not for real embedded programming.Also the Phyton based systems, sure they are fun to play with and get something working quick, but if you need some performance the way to go is C. Others will come out of the woodworks and start yelling C++ is what you need, and others might come up with other "high" level languages, but trust me, everything you want can be done with plain old C.Now onto your question. There are basically three targets to consider here.a) AVR based MCU'sb) ARM based MCU'sc)ESP32 basedMCU's from Espressif. (Based on different architectures)Sure there are more, but these are the main ones.For AVR Ian.M already pointed to the GNU gcc-avr tools.For ARM there are the GNU gcc-arm tools. (There are some varieties, google for them)TheESP32tools for the Espressif line of MCU's I'm not familiar with yet but I'm sure they are out there.Edit: to reflect the comments made by brucehoult, I changed the bits about ESP32.

author: westfw
author_rank: Super Contributor
timestamp: July 11, 2022, 12:08:00 am
content: QuoteNonsense. Or rather, only true for a narrow definition of "real embedded programming" that many applications don't require.QuoteMuch wider choice of hardware. Using Arduino will require you to be become somewhat conversant with C, which can be used on an even broader range of hardware. (whereas CircuitPyton or MicroPython will only run on pretty "substantial" hardware. It may not matter, either. "More substantial hardware" isn't necessarily more expensive or difficult to use than simpler hardware, given the help of the tools available.The main thing you left out is Vendor-supplied IDEs and toolkits. AVRs and SAMD chips have "Microchip Studio and ASF or Atmel Start", or "MPLabX and associated tools." ESP has a C-based SDK that can be used without either Arduino or Python. This way you get a whole different (and less useful?) set of poorly-written peripheral libraries.("Less useful" in the sense that I can probably go out and find an Arduino library for an XYZ123 9dof inertial navigation chip (and half-a-dozen tutorials on how to use it), while the "vendor libraries" probably don't go much beyond "using the I2C peripheral in the micro." - Poorly-written library code that does what you want is generally better than non-existent code...

author: brucehoult
author_rank: Super Contributor
timestamp: July 11, 2022, 01:45:05 am
content: Quote from: pcprogrammer on July 10, 2022, 04:22:00 pmIf you want to be pedantic, the Arduino IDE works with a language called "Wiring" that is a compatible subset of both C++ and Java.If you are working with a board that is programmed in C++ then the IDE prefixes your code with some #include and also generates prototypes for all your functions so that (unlike in C++, but like in Java) the order in which you write functions in the source code does not matter.If you are working with a board that is programmed in Java then the IDE wraps your code with some Java class bollocks and puts some imports at the top.As embedded Java has lost popularity since the mid-2000s a lot of Arduino code has started using C++-specific features, but you won't find that in the standard example code.QuoteRubbish. A professional can do "real programming" using whatever tools are available. Arduino IDE wraps exactly the same compilers and download tools that you might otherwise use "by hand". It provides libraries, but you don't have to use them and you can do chip-specific register programming if you want.QuoteThere is no such thing as "ESP32 based MCUs". Older ESP chips used Cadence's Xtensa ISA. Newer ones use (and Espressif have said all future products will use) the RISC-V ISA.Being snarky and pedantic is a bad look at the best of times, but it's even worse when you don't actually know what you're talking about.

author: ledtester
author_rank: Super Contributor
timestamp: July 11, 2022, 02:54:31 am
content: Quote from: Winfried on July 10, 2022, 02:05:12 pmThe main advantage is faster development time -- if you can accomplish your task in those environments. You code will, in general, not be smaller nor faster, but it might be fast enough for your use case.There is nothing wrong with using the Arduino platform or Circuit Python. However, modern microcontrollers have a lot of powerful functionality that these platforms do not give you access to. To use this functionality you'll need to use a C development environment tailored for the device.Another aspect to consider is that some development environments have versatile debugging capabilities which can be very helpful when developing complex applications. The videos in this playlist, for instance, can give you an idea of what progamming and debugging is like using the STM32CubeIDE:https://www.youtube.com/playlist?list=PL7tUZeMaichqrlJN4PGu3-n6DbYrvoG-s(ignore the raccoon video at the end)

author: brucehoult
author_rank: Super Contributor
timestamp: July 11, 2022, 03:14:55 am
content: Quote from: ledtester on July 11, 2022, 02:54:31 amThere are many many things you might want to do with a microcontroller that only require interacting with the outside world -- taking an ADC reading, or toggling an output pin -- a thousand times a second. Or 100, or 10, or once a minute, for that matter.For anything working at the millisecond level or slower, a 16 MHz AVR using the Arduino libraries is JUST FINE. A millisecond is long enough to run 16000 instructions. It's long enough to do 200 floating point operations (at least add, subtract, multiply) using the software FP library.If you need to work on shorter timescales than that, you can splash out $20 for a Teensy 4.0 with a 600 MHz Cortex M7 CPU on which you can still use the Arduino libraries -- just much much faster. Hell, it's fast enough that you could use Python if you wanted.If you're designing a product for sale and it's going to be produced in the millions or billions of units then you'll want to use the slowest cheapest microcontroller you can get away with, and program it as efficiently as possible.If you're doing a personal project and only building one copy (or ten, for that matter), then there is absolutely nothing wrong with spending a bit more on the hardware (like $20, see above) and using the development environment that will minimise how much of your -- far more valuable -- time is needed to create the thing you want.

author: westfw
author_rank: Super Contributor
timestamp: July 11, 2022, 03:22:40 am
content: QuoteI'm pretty sure that the Arduino IDE has never supported Java, nor has any "Arduino" board ever supported embedded Java. The original thesis project investigated a Java based board, but discarded it very early because "no open source", and it never made it into the Arduino IDE."Processing" was an IDE for simplified Java programming (as you describe), and the Arduino IDE essentially used most of that IDE code, but it's target was always the PC-side, not embedded. (Open Source can make for convoluted code histories.)https://arduinohistory.github.io/

author: pcprogrammer
author_rank: Super Contributor
timestamp: July 11, 2022, 03:38:13 am
content: Quote from: brucehoult on July 11, 2022, 01:45:05 amBeing picky on specific parts of someones post that also contains good parts is just the same.About the ESP32 I realize you are right. In hindsight I remember that Espressif used different architectures. As I pointed out at the end of my post I do not yet know about the ESP(32) parts yet. Just recently acquired some dev boards with ESP32 modules and have not played with them yet.Quote from: ledtester on July 11, 2022, 02:54:31 amThere are IDE's that allow you to use whatever compiler you need and make it possible to work on different architectures from the same trusted IDE. A bit like the Arduino IDE, but with much more nice features to make programming easier. Like visual studio, netbeans or eclipse. They allow debugging with a JTAG or SWD debugger.This debugging is also available in the new Arduino IDE which shows they are working hard to make it better, but I personaly still would only use it for rapid testing.Having all the best tools in the world does not make you a good or even fast programmer. Understanding what you are doing and the ability to solve problems when they occur are the tools that can make you a good programmer.

author: MarkR42
author_rank: Regular Contributor
timestamp: July 11, 2022, 11:19:43 am
content: Answering your subject:Microcontrollers do not run "apps" - they run firmware. Microcontrollers run a single application which is generally made by the hardware manufacturer (excluding things like dev boards) - and interfaces with various hardware.Microcontrollers either do not have an operating system, or have a very specific one (RTOS etc) which only runs a single application, and is part of the firmware.---Some advantages of using C instead of Circuitpython (etc)* You are more likely to find vendor examples in C (e.g. for interfacing with I2C, SPI, sensors, other chips)* Embedded developers will usually be more familiar with C* C programs generally use less memory (RAM / Flash) - which might be helpful as microcontrollers often have very little* It is a simpler environment to set up on most microcontrollers - e.g. if it doesn't already have a "prebuilt" Circuitpython (like Pi Pico)* It is likely to be easier to use third party libraries in C as most will be CBut the advantages of using Python* Some things are much easier and less error-prone e.g. string handling* Your source code is likely to be smaller and may be easier to understand (=fewer bugs?)* You can transfer skills if you already know "real" PythonRuntime performance might be an issue but it is impossible to know in advance.

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 11, 2022, 06:17:27 pm
content: Quote from: westfw on July 11, 2022, 03:22:40 amThe history can be found here:https://en.wikipedia.org/wiki/ArduinoThe very early history comes with "Wiring" and there was some Java compatibility there, although I don't know the full details.But certainly the Arduino framework hasn't dealt with Java in ages now. (Other than the fact the IDE itself was written in Java.)The "language" is described on the official Arduino web site. I have to say that this language "report" is very, uh, unimpressive for a language report.The language itself uses a subset of C++ (avoiding any construct that would use heap memory allocation AFAIK) supporting almost none of the standard C++ libraries. It also uses some specific preprocessing as Bruce said, to "dumb down" some aspects such as the need for prototypes. But the description of the "language" looks very "shallow" to me, and good luck figuring out exactly what parts of C++ are usable or not. To add to this, these days, Arduino is supported by many different targets, and thus different compilers (most commonly either avr-gcc for the AVR targets or gcc for ARM targets), themselves supporting different revisions of C++. A nice mess.But contrary to xxPython, which is interpreted, Arduino stuff is compiled, again using a small subset of C++, so execution-wise, that'll be uh... comparing an interpreted language with a compiled one.

author: Winfried
author_rank: Contributor
timestamp: July 16, 2022, 12:06:22 pm
content: Thanks much everyone for the infos.

author: westfw
author_rank: Super Contributor
timestamp: July 17, 2022, 07:10:54 am
content: QuoteThere is some disagreement as to whether "Arduino" is a "language" or not. Arduino (the company) says "Yes, it's the Arduino Language." Most experienced programmers say "the pre-processed program goes into a gcc C++ compiler, so you have thelanguagefeatures of C++, and can/should refer to C++ (and C) documentation for details (or if you want to understand things that the arduino reference doesn't document, like the ternary or comma operators.) The degree to which C++librariesare supported depends on when CPU you're using (and how much memory there is. And there isn't a multi-user OS involved, so some things will never be like they are in a desktop C++ environment."Even the AVR Arduinos use the heap to support their "String" type (which is not the same as C++ strings, and doesn't work very well, largely due to heap fragmentation.) A bunch of Arduino libraries use the heap.QuoteDo you have any reference for that "Wiring had some Java compatibility" part? It doesn't say so on the WP page you linked, and it's not mentioned athttps://wiring.org.coor the "Hernando Barragán" documet I linked...

author: eugene
author_rank: Frequent Contributor
timestamp: July 18, 2022, 11:56:47 pm
content: Quote from: Winfried on July 10, 2022, 02:05:12 pmMost of those choices come down to either personal preference or suitability to a certain application. Being a newbie you have not developed a sense of personal preference. For that matter, you probably don't really know what applications will become interesting to you in the future.So my advice is to try both (Arduino and CircuitPython.) Go to Adafruit and pick out one of their Feathers that will run CircuitPython or be programmed with the Arduino IDE. It doesn't need to be the fanciest model available. In fact, I suggest that you start out with the attitude that you're going to buy more hardware anyway, so expect that whatever you choose today will be replaced/augmented with something else in a month or two. That's all part of the journey.When it comes to grey haired professional programmers grousing about Arduino or Python, it's safe to ignore them, at least for now. The Arduino IDE is insanely popular for a reason. It has made it possible for an uncountable number of newbies to get up and running with a minimum of pain and a maximum of fun. That describes you right now. When you have 20 years of professional full-time embedded coding under your belt, then you can complain about the Arduino IDE. Right now, you want it to be easy and fun. You can switch over to VS Code and Platformio when you're ready. Then thumb your nose at the people still using Arduino.I have zero experience with CircuitPython, but it seems to be well supported by Adafruit. Again, you'll hear the grumpy professionals complain about them, but they're not the customer base that Adafruit is trying to please anyway. It's an extremely supportive community of makers. And if you buy one of their Feathers, you can try CircuitPython, Arduino IDE, and who knows what else. See for yourself what makes sense to you.

author: tooki
author_rank: Super Contributor
timestamp: July 20, 2022, 04:18:51 pm
content: I think it’s really important to do something nobody here has done so far, and that’s to separate the Arduino IDE (the desktop software) from the Arduino framework (the language and libraries), because they aren’t the indivisible monolith many people think they are.In fact, one of the most common IDEs used for programming with the Arduino framework is PlatformIO in Visual Studio Code. This is particularly popular among advanced users, because the two Arduino IDEs (the original Java-based “Arduino 1.x” and the new, currently in beta “Arduino IDE” 2.0) are both just too dumbed-down for advanced use. (2.0 adds a lot, but it’s still nowhere near PlatformIO in VS Code.) There are other alternatives, too, like VisualMicro (a commercial Arduino extension for the “big” Visual Studio), the Arduino extension for VS Code, and emCode, another extension for VS Code (and the spiritual successor to embedXcode, the same author’s now discontinued extension for Xcode).In PlatformIO, when you create a new project using the Arduino framework, it literally creates a C++ file, with the Arduino.h header already included. The only change from regular .ino files is needing to create function prototypes yourself. But you can also rename your .cpp file to .ino, and then you don’t need to add the header or prototypes. (IIRC, code auto-completion works better when set to .cpp.)Other than features to speed up text editing (autocomplete, etc) thanks to VS Code’s infinitely better editor and autocomplete engine, PlatformIO also supports hardware debugging on a wide array of MCUs, including boards not supported by Arduino IDE 2.0 (which itself only supports debugging on a small handful of boards). The official ESP32 debugger is just $11 or so, while there is a wide array of inexpensive STM32 boards (Nucleo and Discovery) with built-in ST-Link debuggers, which perform better than the ESP32 debugger. The last major advantage of PlatformIO is per-project, versioned library management, rather than Arduino’s global libraries. No more breaking one project when you update another project’s libraries!PlatformIO also supports other frameworks, including Mbed, ESP-IDF, STM32 CMSIS, and STM32 Cube.

author: peter-h
author_rank: Super Contributor
timestamp: July 20, 2022, 06:25:30 pm
content: The main decision is whether the application is a hobby or something commercial.That will lead down mostly very different avenues. For a hobby job you can do anything that works. For a real product there are many considerations.

author: DiTBho
author_rank: Super Contributor
timestamp: July 20, 2022, 06:56:43 pm
content: Quote from: westfw on July 11, 2022, 12:08:00 amMassimo Banziwas serious when he said "with Arduino you have the power at your fingertips ... you can even use it to create an interface for a spaceship"It must be a very serious business, like Rat-man

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 20, 2022, 07:25:19 pm
content: That's some serious shit!

author: westfw
author_rank: Super Contributor
timestamp: July 20, 2022, 09:03:34 pm
content: What a lot of people tend to forget is that MANY "apps for microcontrollers" are NOT "complex projects." You can write them "in Arduino", you can use circuit- or micro- Python; you could probably use a BASIC Stamp running interpreted bytecodes at ~1000 "instructions" per second, or Basic-52 entered on an ASR-33. Or one of those weird languages like Forth, JAL, or even assembly language.Stop giving me these "Arduino's IDE is unsuitable for advanced use" comments. Especially if you can't demonstrate that your project is "advanced." People have done more, with less, for 60 years...(AND ... that's how you become an "advanced programmer." By writing a lot of NOT advanced programs!)

author: Ian.M
author_rank: Super Contributor
timestamp: July 20, 2022, 09:27:41 pm
content: A case in point is the Marlin firmware for 3D printers -v1.1.9is a *very* large Arduino project, which can actually be run on an Arduino Mega2560. However the .ino file of the sketch is essentially empty (just comments) as all the code is in the associated .cpp and .h source files to avoid the Arduino IDE's tendency to mung perfectly valid C++ code when it injects forward declarations, and its a real PITA to work on under the Arduino IDE, chasing down declarations and references in its multitude of source files, so many that the IDE cant display all the tabs in the usual way and you have to jump around from a massive scrolling dropdown list of files at the top right of the tab bar.Its telling that Marlin 2.x uses PlatformIO as its preferred development environment/IDE.

author: Bassman59
author_rank: Super Contributor
timestamp: July 20, 2022, 10:07:32 pm
content: Quote from: Ian.M on July 20, 2022, 09:27:41 pmIt is that exact .ino thing that makes me hate Arduino. Where is the actual fucking code?

author: brucehoult
author_rank: Super Contributor
timestamp: July 20, 2022, 10:38:37 pm
content: Quote from: tooki on July 20, 2022, 04:18:51 pmI don't recall whether it is in this particular thread or not, but this is a point I and others have made many many times on this board.QuoteWhet few people seem to remember now is that PlatformIO used to cost $10/month (?) if you wanted to use the debugger and some other features, until in mid 2019 RISC-V companies SiFive and Western Digital stepped in and sponsored PlatformIO to make the product completely free to use.https://www.westerndigital.com/en-au/company/newsroom/press-releases/2019/2019-06-18-western-digital-extends-openness-of-platformio-and-enhances-its-risc-v-portfolioThere was also the slightly bizarre (but welcome) sight a little earlier in December 2018 of WD CTO Martin Fink narrating a series of videos on how to get started programming SiFive's HiFive1 in assembly language using VSCode and PlatformIO:https://www.youtube.com/playlist?list=PL6noQ0vZDAdh_aGvqKvxd0brXImHXMuLY

author: tooki
author_rank: Super Contributor
timestamp: July 21, 2022, 04:45:07 am
content: Quote from: westfw on July 20, 2022, 09:03:34 pmAgain, one must distinguish between the people who think using the Arduinoframeworkis bad (utter nonsense IMHO) and those who think the Arduino IDE (the desktop apps) is bad.In the first case, I think that’s nonsense, since it’s just libraries that simplify some things. It doesn’t block you from digging deeper if (and when) you need to. And if anything, Arduino has become something quite rare in the MCU world: a fairly portable framework. The vast majority of Arduino code is portable across hardware architectures. This is proving to be a huge advantage right now, with the widespread component shortages.In the second case:canyou code complex things in it? Sure. But why torture yourself unnecessarily?

author: brucehoult
author_rank: Super Contributor
timestamp: July 21, 2022, 05:52:54 am
content: Quote from: tooki on July 21, 2022, 04:45:07 amSome people complain that things such as digitalWrite() take perhaps a µs or two longer than direct port writes because it has to translate a logical pin number to a physical pin number, and then convert that to a port address and mask.Which is true enough, but totally irrelevant to the vast majority of users, who are doing things on a ms or longer time scale.It might be interesting if there was a getPinPortAddress() and getPinMask() that could be called at startup and the values saved. But, having to load those those values from global variables each time instead of having them as hard-coded constants still wouldn't satisfy to moaners so ... whatever ...QuoteAbsolutely correct.

author: pcprogrammer
author_rank: Super Contributor
timestamp: July 21, 2022, 06:20:16 am
content: Quote from: westfw on July 20, 2022, 09:03:34 pmI beg to differ. You can write a lot of not advanced programs, but when these programs are basically stitching together bits and pieces of other peoples work and you never have to dig in deep to solve a bug you won't become an "advanced programmer".To me an "advanced programmer" knows how the hardware works to some extend, and is able to come up with solutions to solve problems when they arise. And is also able to write something completely from scratch with just plain C or assembler. No libraries what so ever. Just your own code and understanding of what you are doing.So to become an "advanced programmer" more is needed then just write a lot of code. And that is why I advise against using Arduino Framework if you really want to learn how to program. Start with the basics.About the Arduino stuff, I did not know it was possible to use the framework in other IDE's, but then still I prefer to do completely bare metal development and write my own code. Sure, until you write up a solid code base, it is a lot of work, but when there is a bug it is your own and most of the time much easier to find then wading through other peoples code.I sometimes use code from other people to save time, but end up formatting it to my own preference anyway.And how did I learn programming. It started with basic on a TRS-80, but soon evolved to Z80 assembler and during my working live it went from 6502, 8051, 6800 etc assembler onto C/C++/C# and things like ASP, PHP, HTML, basic script, javascript, sql, etc. Programming is not in a language it is a skill.

author: woody
author_rank: Frequent Contributor
timestamp: July 21, 2022, 08:00:40 am
content: Quote from: pcprogrammer on July 21, 2022, 06:20:16 amSo true. I still remember the magic when I managed to light my first LED on a PIA connected to an 6800. Using assembler. And even today, when I start using a new-to-me CPU, the very first thing I do is connect a led and make it blink. Using C.I dabbled with lots of alternatives like Arduino and made LEDs blink with them, but that never gives me the same feeling of total understanding and control that using a datasheet and assembler or C gives me.

author: brucehoult
author_rank: Super Contributor
timestamp: July 21, 2022, 10:19:20 am
content: Quote from: pcprogrammer on July 21, 2022, 06:20:16 amSimilar.Actually, I started programming on TI (58, 59, then bought a 57 for myself: NZ$117 in 1979) and HP (67, 97) calculators. I touched a Pet and a Trash in shops and wrote '10 PRINT "I AM COOL":GOTO 10' on them. But the first serious contact was when my high school bought an Apple ][ at the end of my final year. Technically school had already finished, but the Math HoD phoned me and said "Bruce, this Apple ][ has arrived. Can you take it home for a week or two and figure it out and then tell me about it?". Sure. Incidentally I still visit that teacher sometimes, 42 years later. He is 99 now.My first useful program was written in BASIC but called the "RWTS" (Read/Write Track Sector) machine code function in DOS. The program was a very basic but functional GUI disk sector hex editor, which I used to repair a number of disks, and I'm sure other people did too. I passed it on to the local Apple agent.My second useful program replaced the keyboard input function. Or, I should say, wrapped it. My code (6502 machine code obviously) maintained a queue of names of text files to print, kept one disk sector of the current file in RAM, checked the RDY pin from the printer on its IO port, poked a byte at it if it was ready, then chained to the original keyboard input function. Even at that time the Seikosha GP80 30 CPS printer and the faster C-Itoh 8510 had a couple of lines of input buffer, so unless the BASIC program being run in the foreground was really CPU-bound the printer would be kept busy.Not bad for a 100% self-taught 17 year old in the arse-end of the world, if I do say so myself :p

author: tooki
author_rank: Super Contributor
timestamp: July 21, 2022, 04:07:27 pm
content: Quote from: brucehoult on July 21, 2022, 05:52:54 amWhile I completely agree with you that for the vast majority of use(r)s, it doesn't matter at all, the digitalWrite function is actually insanely slow for what it is: on a standard Arduino Uno, toggling a pin is about 18 times slower than direct port manipulation! But of course, there are libraries for faster pin manipulation, or you can just manipulate them yourself.The flip side is that Arduino makes it almost foolproof to dip your toes, and I think the satisfaction of getting something running quickly is a great way to get people interested in embedded programming. The talented ones will still advance quickly, the totally untalented ones can still be happy making things blink, and somewhere-in-the-middle coders like me still get enough satisfaction to go on and learn more, rather than getting stuck at the huge hurdle that traditional MCU toolchains present, which would have put me off it for sure.

author: ozcar
author_rank: Frequent Contributor
timestamp: July 21, 2022, 06:13:23 pm
content: Quote from: brucehoult on July 21, 2022, 05:52:54 amSome of the time taken is due to making sure that timer PWM is not active for the pin every time.Quote from: brucehoult on July 21, 2022, 05:52:54 amYou mean something different to the portOutputRegister() and digitalPinToBitMask() macros?

author: westfw
author_rank: Super Contributor
timestamp: July 21, 2022, 09:27:12 pm
content: QuoteWell, setting/clearing a pin with direct port manipulation is a single sbi or cbi instruction, so it doesn't take much to multiply the execution time. And that single instruction only works for a constant pin and a constant value. As soon as you want variables there, you're talking about loading a 16bit address into a register, reading, modifying, and writing the port via that pointer, so you're looking at about 6x slower than sbi/cbi. Add the pin mapping and checking for PWM, plus function call overhead, and ~20 instructions doesn't seem so bad...(The frustrating part is the code on newer processors that goes to significant trouble to preserve AVR-like behavior. "digitalWrite(pin, 1) turns on the input pullup if the pin is in input mode" is just how the AVR works. But it takes special checks and much more convoluted code to make it happen on an ARM...)

author: tooki
author_rank: Super Contributor
timestamp: July 21, 2022, 09:57:34 pm
content: Quote from: westfw on July 21, 2022, 09:27:12 pmI guess it’s frustrating if you expect each MCU’s “native” behavior, but it makes total sense for code portability for it to behave like AVR. As I said, I think that’s a huge advantage of the Arduino framework, even if it does entail some compromises.

author: brucehoult
author_rank: Super Contributor
timestamp: July 22, 2022, 01:07:03 am
content: Quote from: ozcar on July 21, 2022, 06:13:23 pmWell, there ya go! They thought of everything. Plain digitalWrite() has always been fast enough for my needs, so I hadn't investigated further.

author: westfw
author_rank: Super Contributor
timestamp: July 22, 2022, 02:42:09 am
content: QuoteIMNSHO, there weren't enough applications written beforepinMode(pin, INPUT_PULLUP)was implemented, to justify carrying that behavior forward, rather than "insisting" that people convert to the more explicit API.Extending the portability requirement quickly gets ridiculous (like the PORT emulation on the 4809 boards, which attempts to coercePORTB=xinto some scatter/gather of the pins that are actually connected where the originals were. (which would be a sort-of neat C++ hack, if people weren't using the direct ports for speed purposes, and the emulation wasn't horribly (and I think of necessity) slow (though faster than N*DigitialWrite()))

author: Nominal Animal
author_rank: Super Contributor
timestamp: July 22, 2022, 04:37:08 am
content: On many 32-bit ARM microcontrollers one can write code for in the Arduino environment – my favourites being the Teensies by PJRC.com – each GPIO bank tends to be either 16 or 32 bits/pins wide, and usually haveoutput,set,clear, andtoggleregisters.Writing a value to the output registers sets all pins in that bank to the specified states.Writing a value to the other three registers only affects the pins corresponding to bits set in the written value. Writing to the set register, those pins are set high. Writing to the clear register, those pins are set low. Writing to the toggle register, those pins will have their state toggled between high and low.(This assumes the ones whose state will change are outputs. Some do use the output state for input pullup enable or similar, when the affected pin is an input.)The toggle is most interesting to me, because it allows one to treat any subset of a bank as a parallel bus without affecting any of the other pins in the same bank. The key to setting the "bus" to a specific state is remembering the state it has been previously set to, since exclusive-OR between the current state and the desired state yields the toggle set. Since many of these ARM MCUs have DMA capabilities, and the aforementioned operation is trivial to do when filling in a buffer of (future) bus states, it makes for an excellent parallel bus DMA mechanism.Of course, the annoying thing is that rarely do the pins you can use for the parallel bus correspond to bits 0..N-1 in the bank. Every GPIO pin tends to have a set of alternative functions (like say SCK pin of a SPI bus, SDA or SCK of an I2C bus, PWM from a specific timer, and so on). One solution to this is to use a lookup array withN32-bit words, with index corresponding to bus state, and output corresponding to GPIO bank bits. ForN>8, the arrays become too large, but instead of a single lookup, one can use multiple arrays, each dealing with a subset of the bus width. For example, if one has a 16-bit parallel bus, you can pick any 16 pins in a single bank, in any order, and do one lookup each into two separate 256-entry lookup arrays, that only need 2048 bytes of memory total. (A single lookup array would need 262144 bytes of memory.) With four lookups and a 16-bit parallel bus, you only need 128 bytes for the four tables, 16 entries in each.Right now, I'm playing with a BuyDisplay ER-TFT028A2-4 display module, which provides both parallel and serial interfaces to its ILI9341 controller (controlling the 2.8" 240×320-pixel 18-bit color IPS panel on it), with a Teensy 4.0 microcontroller. Since I will be doing framebuffery pixel-manipulation stuff, splitting the 18-bit parallel bus to three separate 6-bit buses (RGB, each with 2⁶=64 intensity levels), I also only need 3 lookup tables with 64 entries of 32-bit words each –– except that none of the banks really have enough GPIO pins for me to use, so I'll have to split them into different banks.(The framebuffery pixel-manipulation stuff means things where I will be manipulating the pixel color in a way that I will end up with some kind of value where I can easily pick off the 6 most significant bits for each color component. Mapping them through a lookup table is a minimal extra cost, since I'll be doing something like four multiplications per pixel (for color blending) anyway. All I want to be able to do, is do all this faster than the refresh on the display module, so I won't see annoying tearing on it.)However, one must also send 8-bit commands and data to the display, and those cover one 6-bit bus, plus two least significant bits from another bus. So, if I could put 12 pins in the same bank, I could have both the three RGB-lookup tables (768 bytes total) and the 256-entry (1024-byte) command/data lookup table making things easy.So, what I have been doing, is experimenting with the pin selection! (And also the backlight for the display module. I do code well, but on the electronics side, I'm rather a beginner hobbyist.) Of course, the pins on the microcontroller evaluation/development boards is not according to the internal bank system, so one ends up with spreadsheets with pin mappings, their alternate functions, and playing with the numerous options on which pins to use for which purpose.Often, the microcontroller processors –– especially those using BGA footprints –– have many more possible outputs than are exposed on the cheap evaluation/development boards one uses with Arduino, making that kind of selection annoying and frustration, which is why people doing anything like this will very soon move on to designing their own boards, so they have more options in picking which pin to use for which purpose.The above means that if you want your Arduino code to be portable, you must stick to the Arduino (library) features, and not use the hardware directly, as the direct hardware access (through ports and registers) will vary from microcontroller to microcontroller. On the other hand, some of the more interesting stuff (like the parallel bus to a display module above) really needs hardware access, and is not as easily portable at all.As a result, I would say it is very important to play with all kinds of peripherals and sensors and breakout boards, and understand how they are controlled and communicated with. Not just "I used this library; that's all I know", because that only teaches you how to use libraries written by others, you do need to be interested in finding out exactly how stuff happens, too.While I do use Arduino for experiments and stuff I want those that use Arduino to use too, I do kinda-sorta like bare-metal development more. I don't see them as one excluding the other; they're just different environments, and I use both. In the mean time, there are others like PlatformIO that can use Arduino libraries, but do not rely on it or its IDE, that one should probably also experiment with too, if they have a supported microcontroller.As in all programming,documentationshould never be overlooked, either. Always write comments that describe your intent, not what the code does. (Anyone can read the code and see what it does, but the code does not tell what it is trying to accomplish, how, nor why.) And documentation showing wiring and real-life test cases, rationale for the design choices, and so on, is always useful. So practice that, too, from the get go. It isnotsomething you can just pick up later on, and "for now concentrate on the code": it becomes much, much harder to learn to do afterwards! I know from hard-fought experience. Make good documentation a habit from the get go, and you'll end up being very happy you did so, guaranteed.

author: DiTBho
author_rank: Super Contributor
timestamp: July 22, 2022, 08:32:10 am
content: Quote from: Nominal Animal on July 22, 2022, 04:37:08 amWell said. Not the same, different stuff, that can solve different needs.

author: DiTBho
author_rank: Super Contributor
timestamp: July 22, 2022, 09:03:05 am
content: at the moment I am writing apolymorphic arbitrary linear system solverthroughpivoting-Gauss reduction; Wait!!What??Code:A * x = bx = ?A is a matrix, it can be asparsematrix; x and b are vectorssize(..) = custom defined, even at run-timetype..) = custom defined { floating point, fixed point, complex fp64, complex fx ... }so you have to define methods and operators for each type, and write the solver in an abstract way ... that's the job for C++ (and probably on a PC), but I want to use C.That's definitively not what you would like to do with MPUs ... even because there is a price to pay(1) to have this level of abstraction.To say: different needs, different tools(1) more CPU-cycles and more code size