November Update 2017

So, I didn’t realize it has been so long since I wrote the last update. It was a pretty eventful summer but with some good progress. Unfortunately, I did not get as much done as I had hoped. I faced a major that was a struggle to overcome because it was both beyond my knowledge and I didn’t quite have the right priorities.

I have essentially finished migrating the entire RN-9090 onto the STM32. I have written libraries to control all the peripherals that I need and verified that they work. I migrated the algorithm over and ended up redoing most of it because I had originally written the code before I knew much about proper data structures, programming, and processor behavior. My code was a mess of global variables thanks to the Arduino IDE and I just had to re-do everything. In the process of upgrading the code, I implemented a dynamically allocated memory system for everything because why not? Dynamic memory is an efficient use of space and good programming techniques. Or at least that was what I thought until I started testing the new code I had written. I spent many days and nights struggling to understand why I was getting hardfault errors at benign locations in my code. Sometimes it would fault at seemingly meaningless lines like incrementing a variable. Most other times it would fault when I would try to free memory (but not always at the same line). This significantly slowed down my progress. I researched on the internet and found some articles talking about the problems of dynamic memory allocation for embedded systems. I tried to create my own memory allocation functions but that didn’t help. I never could figure out the solution even if it was obvious the whole time.

Eventually, I learned that dynamic memory allocation for an embedded system is not so cut and dry as it might be when programming on a PC with virtually unlimited memory. Dynamic memory allocation suffers from two problems: determinism and fragmentation. Fragmentation occurs when a block of memory is freed but subsequent further requests cannot fit in the freed block. Memory locations become fragmented and lost and it is not an efficient use of the limited amount of memory in the heap. Determinism was the problem that I was suffering from. Allocating and freeing memory is non-deterministic meaning that the time it takes to perform the action can vary. This was the source of my frustrations. It took me a while to come to grips that the things my CS teacher always tried to hammer home about dynamic memory allocation was not a rule carved in stone. I am back at school now and on the first day I asked my microprocessors professor about this problem I was experiencing. He imparted some wise knowledge to me that I have since taken to heart: “Just get the thing working and you can optimize later”. So, I came around to using static memory allocations, and I don’t know what took me so long. I’m not even using 50% of the available memory anyways. Problem solved!

I am now at the point where I am ready to finish the PCB design for the new RN-9090. Ordering the new PCBs could cost up to $200. I have not designed a PCB with such a chip as this STM32 and I am not a 100% certain that it will work the first time. I am considering making a smaller “dev board” first just to make sure my design works. While that will slow things down, I think it could save me some money if there is a problem. Fortunately, I have the design finished, I just need to place the order. I will definitely update when that happens.

I will finish this project!