MLRelated.com
Blogs

3 Tips for using ChatGPT for Embedded Software

Jacob BeningoJanuary 13, 2023

Unless you’ve been hiding under a rock, the internet has been ablaze with conversations, videos, and blogs about ChatGPT. ChatGPT is a chatbot that interacts with a user conversationally. The chatbot can answer questions, request clarification, and even write and debug code for us embedded folks. ChatGPT is a trained AI model which finished its training in early 2022 and has been available in public beta since November.

This post will discuss a few exciting ways to use ChatGPT to accelerate embedded software development.

Tip #1 – Use ChatGPT to gather background information

This article is available in PDF format for easy printing

Sometimes you might start on something and not know where to start. Playing around with ChatGPT, I found that it can often provide reasonably accurate information to point you in the right direction. For example, if I wanted to design a class that would handle LEDs but wasn’t sure what to include in the API set, I could ask ChatGPT the following:

List the apis that should be included in an LED class

The response was:

The specific APIs that should be included in an LED class would depend on the intended use and functionality of the class. However, some common APIs that may be included in an LED class are:

  • A constructor method that initializes the LED and sets its initial state (e.g. on or off)
  • A method to turn the LED on
  • A method to turn the LED off
  • A method to toggle the state of the LED
  • A method to check the current state of the LED (e.g. is it on or off)
  • A method to adjust the brightness of the LED
  • A method to set the color of the LED (if it is a RGB LED)
  • A method to blink the LED at a certain interval
  • A method to clean up resources when the class is no longer needed

It's worth noting that the specific implementation of each of these methods would depend on the underlying hardware being used, so the exact implementation details of the class would vary.

Pretty impressive! I may or may not want all those APIs, but the AI gave a pretty good explanation about what we should and should not include.

Tip #2 – Use ChatGPT to create code drafts

In playing around with ChatGPT and the openai playground, I’ve been pleasantly surprised at how well it can provide some basic baseline code. Don’t get me wrong, I don’t think you can trust what the AI delivers to you, but developers can leverage it. For example, if I request that the AI generate an abstract class for an LED in C++, I get the following:

class LED {

private:
    int brightness;
    string color;
    int blinkRate;
    bool isOn;

public:
    LED();
    virtual void setBrightness(int level)=0;
    virtual void setColor(string color)=0;
    virtual void setBlinkRate(int rate)=0;
    virtual void turnOn()=0;
    virtual void turnOff()=0;
    virtual bool isOn()=0;
    virtual bool isOff()=0;
    virtual int getBrightness()=0;
    virtual string getColor()=0;
    virtual int getBlinkRate()=0;
};

You can see that the AI did a pretty good job! However, there are some minor gripes that I would have about the code. First, it’s standard to put the public members first and then the private members second. A cut-and-paste quickly solves that. (Not copy and paste!). Next, the AI created a basic constructor and several pure abstract members. Pretty cool, but for the functions that tool parameters, we should specify the parameters as const, and if a parameter is not taken, pass void.

The critical point here is that the AI-generated 80% useful code that I can directly take and leverage with some minor modification. So, for example, if I decided that there were too many member functions, I could delete the functions I didn’t want.

Out of curiosity, I decided to see if the open ai model could generate the LED-derived class using the STM32 HAL. Below is what it created:

#include "stm32f7xx_hal.h"

class LED_STM32 : public LED {

private:
    GPIO_TypeDef *port;
    uint16_t pin;
    TIM_HandleTypeDef *htim;
    uint32_t channel;
    int brightness;
    string color;
    int blinkRate;
    bool isOn;

public:
    LED_STM32(GPIO_TypeDef *port, uint16_t pin, TIM_HandleTypeDef *htim, uint32_t channel);
    void setBrightness(int level) override;
    void setColor(string color) override;
    void setBlinkRate(int rate) override;
    void turnOn() override;
    void turnOff() override;
    bool isOn() override;
    bool isOff() override;
    int getBrightness() override;
    string getColor() override;
    int getBlinkRate() override;
};

It’s not the way I would do it, but it isn’t too bad. Again, a lot of qualification is missing from the code, along with some clarity. However, it gets us in the right direction quick and with small modifications I can make this code work just fine.

Tip #3 – Don’t trust ChatGPT

If you play around with ChatGPT and the openai tools enough, it can become convincing that the tool knows what it is doing. It’s impressively good, far better than I expected, but it can’t be trusted. The AI is a great tool to help you accelerate software development and design. It’s using its model to tap into a large knowledge base, but that base may not be right, and it may not retrieve its responses accurately.

I also discovered that just making small adjustments to the language I use to ask a question or to have the AI generate something dramatically changed the results I would get. When asking about LED interfaces, sometimes I would get a verbose conversational response and sometimes I would just get the code. Other times requesting the same thing with a slight variation would dramatically alter the code that was generated.

Some of these differences might be due to the chat building on my previous requests within the conversation, although I did my best to start a new one to test consistency.

Conclusions

ChatGPT is a powerful example of how AI can accelerate and aid development. You shouldn’t trust it to generate your code or provide an accurate response. However, I have found that it can point you in the right direction, generate draft code that you can modify, and help speed up development. I really wasn’t expecting it to work as well as it did, and I can see where in the future every developer could leverage a tool like this to help them improve their throughput.



To post reply to a comment, click on the 'reply' button attached to each comment. To post a new comment (not a reply to a comment) check out the 'Write a Comment' tab at the top of the comments.

Please login (on the right) if you already have an account on this platform.

Otherwise, please use this form to register (free) an join one of the largest online community for Electrical/Embedded/DSP/FPGA/ML engineers: