Date: March 13, 2024
In our ongoing effort to continuous improvement, we’ve embraced the emergence of Artificial Intelligence and integrated it into our workflow. The adoption of such technologies has improved the quality of our code while also freeing developers to focus on higher level decision making throughout a project. By trialing a number of AI tools, we’ve seen improvements in our development practices, albeit not without encountering a set of challenges.
This piece aims to share how AI has improved our firmware and what we’ve found useful, what AI doesn’t do well and how we’ve mitigated this, and what we hope the next year will bring in terms of Artificial Intelligence with regards to embedded systems firmware.
Although there are a lot of AI tools out there, this article focuses in on our experience with using GitHub Co-pilot
GitHub Copilot is an AI-powered code completion tool that integrates into our code editor (Visual Studio Code). It assists developers with writing code by generating suggestions for small to medium chunks of code by leveraging a generative AI model trained on public code repositories. Copilot predicts and generates code suggestions as developers type. You may have noticed tools in other fields implementing similar features to help predict user actions. Gmail for example are developing a tool to help you draft emails which you can read about
here.
GitHub Copilot can accurately anticipate the developer's next steps based on the context of the code being written. This predictive capability enables software and firmware engineers to focus on higher-level design aspects and complex problem-solving, rather than getting bogged down in repetitive coding tasks. Take this scenario:
A developers needs to write a driver for a TMP451 temperature sensor they are using in a weather station they are developing. They write the function below which reads the temperature back over a communication bus.
They test the function and if all went well they move on to the next function which is an API to get the humidity. Before AI assistance, the developer would now have to write a very similar function to get the humidity, but with small and important details changed.
With Co-pilot however, it sees how the get temperature function was made and makes a prediction on what the humidity function will be.
The AI suggestion has copied the logic and style from the "get temperature" function but changed the important parts so it is now reading back the humidity. We reviewed this code and it is indeed correct!
This example highlights some important features of developing with AI:
It’s important to mention that even when writing the original temperature function, Co-pilot was making suggestions here too. Co-pilot saw the temperature was calculated above so there is a high probability that we would want to "print" (log) this reading. The handy thing here is it gives a nicely formatted readable message.
Another thing that AI does really well is its documentation. GitHub Co-pilot has an inbuilt command specifically to force it to suggest documentation. You can see below that it’s generated a doxygen style comment for our get temperature function.
Limitations and Tips
While the integration of AI tools into firmware development has brought significant advancements, it has not been without its set of challenges. These limitations, ranging from issues of code relevance to the intricacies of managing AI-generated suggestions, have necessitated a thoughtful approach to leveraging AI technology effectively.
By the previous section you might be thinking AI tools could just write the whole program by itself. Fortunately for software developers, it’s not THAT good…. yet.
Unlike human developers who possess an innate contextual understanding and have project-specific knowledge, AI tools can sometimes offer solutions that are not relevant to the specific problem at hand and for really specific/niche problems, sometimes it just makes stuff ups!
Here is an example where the developer wants to use the law of sines to calculate the ground distance and GitHub Co-pilot just makes up an equation. This points out the importance of code review to add an additional layer of scrutiny, ensuring that AI tools aren’t slowing down and making the development process worse.
AI tools need to understand the problem space to generate relevant solutions.This often involves detailed input from developers, which can be time-consuming and seem like a waste of time. However, refining the process of inputting context can markedly improve the quality of their outputs, making them more applicable and useful.
You can see the developer here has given the AI contextual information in the form of a comment explaining the steps to correctly initalise the sensor. Without this the AI cannot really give an accurate suggestion. As you can see its done a pretty good job now that it does have the information.
In the specific case of Github Copilot, it also grabs contextual information from open tabs in the IDE. This gives the AI context beyond other code written in the file. Other tools have specific places to input contextual information. For example Open AI’s GPT playground has a system message for this very reason.
It's clear that the landscape of AI is continuously changing, with new advancements on the horizon that promise to further revolutionise our development processes. Looking ahead, we anticipate several key trends and developments that could shape the future of firmware development with AI.
Finding the balance between leveraging AI-generated assistance and relying on human expertise is crucial. Developers need to discern when to accept AI suggestions and when to proceed based on their professional judgment and understanding of the project. This delicate balance underscores the increasing importance of establishing comprehensive project specifications at the outset. Doing so lays a good foundation for the collaboration between AI tools and human developers. Clear project requirements early on, allows developers to harness AI technology effectively, enhance productivity, and elevate the overall quality of firmware.
If you have a project that involves firmware development, feel free to reach out to us to discuss the potential benefits we can offer with this AI-integrated approach.