Why Do It the Hard Way? - 2018 Hackaday Superconference
For the 2018 Hackaday Supercon I decided to submit a proposal for a talk on how to program microcontrollers more efficiently and they accepted my it! Proposals are submitted in September for a conference in November so I knew I had some time to write the talk, but when I submitted the talk I really only had a rough idea of what I wanted to talk about. There is a lot of functionality built-in to microcontrollers that I wish I had known about before trying to do something similar myself in code. For my talk, I wanted to mention a few of those features to help others find them so they could save time and effort by using all of the tools given to them in the hardware. I also wanted to touch on some of the very specific vocabulary that is necessary to find more information an what peripherals are available to the developer.
I wanted the talk to be as vendor agnostic as possible so the first step of my writing process was to start collecting datasheets. I had a couple of datasheets from past projects, but my MCU choices have always leaned pretty Atmel heavy with a few STM32 chips in the mix so I knew that wouldn’t make for a very well rounded representation of the industry. I remembered Jay Carlson’s incredibly impressive Amazing $1 Microcontroller project and found all of the datasheets from the MCUs he looked at to give me a much better look at what is out there. Between my datasheets, Jay Carlson’s choices, and a few others that I wanted to look at (like the Nordic nRF52832) I had a total of 22 datasheets from 15 different manufactures to look through. I’m certain that there are probably holes in my coverage of the market, but this was at least a good starting place.
My previous experience had given me the general feeling that most datasheets come from the same mold and that once I got used to looking at them it would be pretty easy to parse through them to find the interesting bits. For about half of the chips this was the case, but for some of the chips less focused the the US based consumer market the datasheets were a lot more challenging to skim through. My first main takeaway was that the Analog Comparator that started the whole idea for this talk was found in almost every chip. I really began to regret all of the time I wasted trying to use analog reads to accomplish this. The other big thing that jumped out is that the PSoC 4 that I bought for my 2017 badge hack is actually a really awesome chip that I should be using more often. I could have used it’s built-in random duty cycle PWM function, I/O remapping, and built-in boolean logic functions to make all 8 of the RGB LEDs on my old badge hack independently blink randomly with less than 20 lines of code, but I decided to focus on writing the talk instead of having interesting props (maybe next year).
Because I wanted this talk to be generally helpful I decided to not focus on a peripheral if it was only available on a single chip. This eliminates some of the more interesting built-in functionalities, but I think helped focus the talk on the intended audiance. I started to put together a list of what the most common tools were. There were a lot of built-in functions in common but I decided to focus on the ones I thought would be most useful to a someone tinkering with hardware that is past the point of using Arduino libraries but not yet a seasoned hardware developer. This lead me to focus on built-in pull-up/down resistors, analog comparators, and pin-change interrupts for MCUs that are not ARM based and bit banding for ARM based chips. I would have loved to dive in depth on interrupts and timers, but because I was trying to cover a wide variety of material I knew there wouldn’t be time for that. I also tried to focus on what different vendors call these tools. I would never expect someone to hear my talk and immediately be able to implement one of the peripherals that I spoke about, but if I could get them to remember the exact words that the manufacture used to describe the peripheral then they will be able for time the information they need in the datasheet and example code. The best example of this is how most of the industry uses the term Pin-Change interrupt, but PIC processors are their own special flower and instead call the same function Interrupt-on-Change. Not exactly a Google friendly difference in terms.
Initially I didn’t want to have code on the slides because I thought that would be confusing, but the more I thought about actually giving the presentation I realized that I needed it on screen to demonstrate how easy it is to use the built-in functions and to show how much time it would save. For bit-banding I used Compiler Explorer instruction counts to demonstrate the advantage of the built-in tools because I didn’t think it was wise for me to try and decode multiple bitwise operations in a single line while standing on a stage in front of people. In the end, I feel that both methods achieved the goal of presenting the necessary information without getting in too deep on the details.
I’m happy I proposed and gave my talk. There are lots of things that I wish I had done differently, but it still encourages me to give another talk in the future. All of the talks from the Supercon are posted on Youtube. I you would like to download my slides you can find them here and when they post my talk I will update this page.