In the event you’ve labored with a microcontroller, you’re most likely considerably acquainted with vendor-supplied {hardware} abstraction layers. A {hardware} abstraction layer (HAL) is a software program layer that gives a high-level interface to {hardware} gadgets or peripherals, abstracting away the main points of the {hardware} from the remainder of the software program utility or system. The HAL permits software program to work together with {hardware} by means of a constant set of APIs (utility programming interfaces), whatever the underlying {hardware} specifics.
Whereas many microcontrollers include some type of HAL immediately, they’re usually designed in such a manner that they don’t actually give you flexibility. Positive, you don’t have to fret about low-level {hardware} interfacing, however your utility code turns into tightly coupled to the supplied HAL, making it troublesome to pivot to different {hardware}.
On this publish, I’ll present you a easy instance on the way to create your individual HALs utilizing your favourite GPT (Generative Pre-Educated Transformer). I’ll be utilizing ChatGPT.
Rules for Efficient Prompts
If you end up utilizing AI to generate code, you must just remember to comply with a couple of finest practices to get the perfect outcomes. Similar to with any machine that’s being programmed, rubbish in offers you rubbish out. There are a number of ideas that I like to recommend you comply with when making an attempt to generate code with an AI:
-
Use technical language the place applicable
-
Define the specified logic or algorithm in pseudocode
-
Point out the programming language the consequence will likely be in
-
Embrace examples or use circumstances
The extra particular you’re, the larger the probabilities that you just’ll obtain the specified output. Don’t anticipate a miracle although. You’re going to must iterate and refine what’s produced by the AI. In reality, it’s possible you’ll even in some unspecified time in the future must ditch the AI and carry out the ultimate changes your self.
Getting ready to Generate a Digital Enter/Output HAL
A great first HAL to create for any venture or instance is a digital enter/output (dio) HAL. It’s extra generally referred to as GPIO, however I discover that digital enter/output is a bit more particular. There are a number of the reason why dio is an efficient first interface to sort out resembling:
-
It lets you get all of the pins in your microcontroller configured and doing one thing
-
You possibly can create a “Hey Blinky” utility to show that it really works
-
Requirements like Arms CMSIS don’t embody a GPIO or dio HAL, so even should you plan to make use of standardized HALs, you’ll must create this one from scratch.
If you first get began, I like to recommend that you just spend the primary couple of prompts getting ready the AI for what you need to do. For instance, I’d begin by telling the AI that I don’t need any motion but, however that I’m going to be producing a dio hal in C that makes use of the next coding commonplace. I’d then present particulars about naming conventions, formatting, and so forth. Normally, the AI can’t assist however generate one thing, so it should normally analyze or repeat again what you’ve already advised it. Do just remember to present it should data like:
-
Remark and documentation construction
-
Error dealing with preferences
-
Optimizations like velocity or code dimension
It will also be useful to supply examples. If you’re utilizing STM32 HALs you would possibly present an instance of their HALs as a reference with the enhancements you’d wish to see. For this instance, I supplied an instance from my e-book Reusable Firmware Improvement:
void Dio_Init(DioConfig_t const * const Config);
DioPinState_t Dio_ChannelRead(DioChannel_t const Channel);
void Dio_ChannelWrite(DioChannel_t const Channel, DioPinState_t const State);
void Dio_ChannelToggle(DioChannel_t const Channel);
void Dio_ChannelModeSet(DioChannel_t const Channel, DioMode_t const Mode);
void Dio_ChannelDirectionSet(DioChannel_t const Channel, PinModeEnum_t const Mode);
void Dio_RegisterWrite(uint32_t const Handle, TYPE const Worth);
TYPE Dio_RegisterRead(uint32_t const Handle);
void Dio_CallbackRegister(DioCallback_t const Operate, TYPE (*CallbackFunction)(sort));
Whereas this HAL may very well be used as is, it doesn’t actually present a mechanism to simply assign the {hardware} perform. The HAL mainly anticipated a C module to be supplied that had implementations for every of the desired capabilities. Whereas this does summary the {hardware}, it suffers the identical subject that many vendor supplied HALs have which is coupling of the appliance to the implementation. We will do higher!
Producing a First-Go dio HAL
To generate the dio HAL, I present a couple of easy directions to the AI:
“Utilizing the knowledge I’ve supplied about coding model, replace the supplied HAL in order that it’s encapsulated inside a dioInterface_t struct. Every member of the construction must be a perform pointer in order that its use appears to be like like an object: dio.Init()”
The consequence generated by the AI was one thing like the next:
SOURCE: JACOB BENINGO
The profit to utilizing the sort of interface is that we do in reality have a HAL! I can assign capabilities to these perform pointers as follows:
dioInterface_t dio =
{
.Init = McuDioInit,
.ChannelWrite = McuDioChannelWrite,
// and many others
}
We may reassign what capabilities are used primarily based on our construct. So, if I’m constructing for my goal microcontroller, I could have one definition for dio. If I’m constructing for a bunch construct or simulator, then I’d have a distinct definition for dio the place the assigned capabilities to the interface are completely different. My utility code wouldn’t change, however the underlying code would!
Extra Issues to Do with AI
To this point, I’ve checked out modifying an instance I already had. There’s much more that I may do with AI although. I may:
-
Ask it to create a really helpful interface
-
Checklist capabilities that must be included within the interface
-
Make enhancements to the interface
-
Create a C++ interface utilizing the identical specification
-
Generate different peripheral interfaces primarily based on the authorized dio HAL
You possibly can iterate and drive the AI to construct out not only a single interface however a number of interfaces fairly quickly. The true energy I consider is that when you solidify on a specific model of HAL, you possibly can ask the AI to generate the same interface for SPI, I2C, PWM, ADC, and so forth. In seconds, you’ll have a constant interface of HALs that can be utilized to really summary your {hardware} and give you the pliability to develop scalable, transportable, and reusable embedded software program.
Conclusions
On this publish we’ve simply scratched the floor on how AI can be utilized that can assist you develop embedded software program sooner. We checked out how we’d generate {hardware} abstraction layers, that are a important piece of the puzzle for writing reusable firmware. In reality, it’s a important piece of the puzzle for writing fashionable embedded software program. In the event you don’t have good abstractions, you possibly can’t simulate or take a look at your code nicely! Which means extra bugs, extra handbook testing, which results in costlier growth cycles.
Whereas AI is a more recent know-how and positively not excellent, you possibly can nonetheless leverage it to get you to the ~80% mark. With a couple of tweaks and changes, you would possibly discover that it will probably assist you to speed up your growth a bit. Utilizing it to create a HAL is an easy, and first step to experiment with.