Way to analysis firmware

Thread Starter

Alex1700

Joined Jan 12, 2020
107
Hi all,

I always find it so hard to understand other people's firmware any better way to understand other people's firmware written in pic32 microcontroller anyone could share a better way?
 

Skspark

Joined May 31, 2023
1
It is best to post other engineers' code on Chat GPT section by section and tell the AI that it is developed for the PIC32 in order to comprehend it. The finest AI currently in use for debugging and understanding programs is Chat GPT.
 

Thread Starter

Alex1700

Joined Jan 12, 2020
107
It is best to post other engineers' code on Chat GPT section by section and tell the AI that it is developed for the PIC32 in order to comprehend it. The finest AI currently in use for debugging and understanding programs is Chat GPT.
thank you
 

WBahn

Joined Mar 31, 2012
30,077
It is best to post other engineers' code on Chat GPT section by section and tell the AI that it is developed for the PIC32 in order to comprehend it. The finest AI currently in use for debugging and understanding programs is Chat GPT.
Any why do you think that ChatGPT, which has NO comprehension of ANYTHING, is going to provide an accurate analysis of someone's code?
 

Papabravo

Joined Feb 24, 2006
21,228
Hi all,

I always find it so hard to understand other people's firmware any better way to understand other people's firmware written in pic32 microcontroller anyone could share a better way?
Like anything worth doing you cannot become proficient at it overnight. Similarly, you have to keep doing it to gain that proficiency. Your patience and tenacity will be rewarded eventually.
 

Irving

Joined Jan 30, 2016
3,897
Any why do you think that ChatGPT, which has NO comprehension of ANYTHING, is going to provide an accurate analysis of someone's code?
Don't be so sure.... here's a little experiment

I asked: what does this code do on an Arduino?
Code:
#define LED 2

void ledOn(){
    digitalWrite(LED, HIGH);
    }
void ledOff(){
    digitalWrite(LED, LOW);
    }
    
void setup(){
    pinMode(LED, OUTPUT);
    }
    
void loop(){
    ledOn();
    delay(500);
    ledOff();
    delay(500);
}
ChatGPT answered in less than 5secs

This code defines a constant LED with a value of 2. It then declares two functions: ledOn() and ledOff().
The ledOn() function sets the digital pin specified by LED (which is pin 2 in this case) to a HIGH state, turning on the connected LED.
The ledOff() function sets the digital pin specified by LED to a LOW state, turning off the connected LED.
In the setup() function, the pinMode() function is used to set the digital pin specified by LED as an output pin, indicating that it will be used to control the LED.
The loop() function contains the main logic of the program. It repeatedly calls the ledOn() function, which turns on the LED, then waits for 500 milliseconds using the delay() function. After that, it calls the ledOff() function to turn off the LED and waits for another 500 milliseconds. This sequence of turning the LED on and off is repeated indefinitely in a loop.
In summary, this code sets up pin 2 as an output pin and continuously blinks an LED connected to that pin with a 500 millisecond delay between on and off states.


Which I think is a pretty good summary... it even titled its answer as "Arduino LED Control"!

I then tried this question: What does this code do? (without mentioning platform)
Code:
#define LED 2

void changeLED(){
    digitalWrite(LED, !digtalRead(LED));
    }
    
void setup(){
    pinMode(LED, OUTPUT);
    }
    
void loop(){
    changeLED()
    delay(500);
}
ChatGPT answered, in about 3min, and titled its response "Toggle LED on Arduino"

The given code is written in the Arduino programming language and controls an LED connected to pin 2 of the Arduino board.
Here's a breakdown of what the code does:

  1. The line #define LED 2 creates a constant named "LED" with the value 2. This line is essentially assigning the LED pin number to the constant for easier reference throughout the code.
  2. The changeLED() function is defined. Inside the function, digitalWrite(LED, !digtalRead(LED)) toggles the state of the LED. It reads the current state of the LED using digitalRead(LED), negates it using the logical NOT operator !, and then writes the inverted state back to the LED pin using digitalWrite(LED, ...). This function is responsible for changing the LED's state.
  3. The setup() function is called once when the Arduino board is powered on or reset. Inside this function, pinMode(LED, OUTPUT) sets the LED pin (pin 2) as an output pin, indicating that it will be used to send electrical signals to control the LED.
  4. The loop() function is an infinite loop that continuously executes the code inside it. Within the loop, changeLED() is called to toggle the LED state, followed by a delay(500) statement that pauses the program for 500 milliseconds (0.5 seconds). This creates a blinking effect with a half-second delay between each change in the LED's state.
Overall, this code sets up an LED connected to pin 2 and continuously toggles its state on and off with a delay of 500 milliseconds between each change.

Again, quite impressive, and it even noticed and corrected my deliberate bug in changeLED()!

Now, I'm not saying it would handle anything more complex as well, after all, the Blink sketch is well known and documented. But I think it belies your statement @WBahn and on more complex code may give insight even if its not 100%
 

Irving

Joined Jan 30, 2016
3,897
OK, I'm getting more impressed:

Question: what does this do?
Code:
#include <SPI.h>
#include <mcp2515.h>

struct can_frame canMsg1;
struct can_frame canMsg2;
MCP2515 mcp2515(10);

#define sw 8

void setup() {

  pinMode(sw, INPUT_PULLUP);
  
  canMsg1.can_id  = 0x0c180001 |CAN_EFF_FLAG;
  canMsg1.can_dlc = 3;
  canMsg1.data[0] = 0x23;
  canMsg1.data[1] = 0x01;
  canMsg1.data[2] = 0x01;
  canMsg1.data[3] = 0xAA;
  canMsg1.data[4] = 0xAA;
  canMsg1.data[5] = 0xAA;
  canMsg1.data[6] = 0xAA;
  canMsg1.data[7] = 0xAA;

//  canMsg2.can_id  = 0x055;
//  canMsg2.can_dlc = 8;
//  canMsg2.data[0] = 0x00;
//  canMsg2.data[1] = 0x55;
//  canMsg2.data[2] = 0x55;
//  canMsg2.data[3] = 0x55;
//  canMsg2.data[4] = 0x55;
//  canMsg2.data[5] = 0x55;
//  canMsg2.data[6] = 0x55;
//  canMsg2.data[7] = 0x55;
//  
  while (!Serial);
  Serial.begin(115200);
  SPI.begin();
  
  mcp2515.reset();
  mcp2515.setBitrate(CAN_125KBPS, MCP_8MHZ);
  mcp2515.setNormalMode();
  
  Serial.println("Example: Write to CAN");
}

bool bPressed = false;
uint8_t nCount=0;
struct can_frame *frameAddr;
char buf[64];

void loop() {
  //send once each button press
  if(digitalRead(sw) == LOW && !bPressed)
  {
    bPressed=true;
    mcp2515.sendMessage(&canMsg1);
    delay(100);
  }
  if(digitalRead(sw) == HIGH){ //debounce release
    bPressed = false;
    delay(50);
  }
}
ChatGPT response in under a minute, entitled "Arduino CAN Bus write"


The code you provided is an Arduino sketch that demonstrates how to write to a CAN (Controller Area Network) bus using an MCP2515 CAN controller.
Here's a breakdown of the code:

  1. The code includes the necessary libraries: SPI.h for SPI communication and mcp2515.h for MCP2515 CAN controller functionality.
  2. Two instances of the can_frame struct are declared: canMsg1 and canMsg2. These structs represent CAN messages and hold the necessary data for transmission.
  3. An instance of the MCP2515 class is created, named mcp2515, with pin 10 as the CS (Chip Select) pin for the MCP2515 CAN controller.
  4. The sw pin (pin 8) is configured as an input with a pull-up resistor.
  5. In the setup() function:
    • The sw pin mode is set to INPUT_PULLUP to enable the internal pull-up resistor.
    • The canMsg1 struct is initialized with a specific CAN ID, data length code (DLC), and data bytes.
    • Serial communication is initiated with a baud rate of 115200.
    • SPI communication is started.
    • The MCP2515 is reset, and its bitrate is set to 125 kbps with an 8 MHz clock.
    • The MCP2515 is set to normal mode.
    • A message is printed to the serial monitor.
  6. Some variables are declared: bPressed to track the button press, nCount as a counter, frameAddr as a pointer to a can_frame struct, and buf as a character array.
  7. In the loop() function:
    • The code checks if the button connected to the sw pin is pressed and the bPressed flag is not set. If true, it sets bPressed to true, sends canMsg1 using the MCP2515's sendMessage() function, and waits for 100 milliseconds.
    • If the button is released (digitalRead(sw) == HIGH), the bPressed flag is reset to false with a small delay for debounce.
The purpose of this code is to demonstrate how to send a CAN message using an MCP2515 CAN controller when a button is pressed. When the button is pressed, it triggers the transmission of canMsg1 through the MCP2515. The code includes debouncing logic to avoid multiple transmissions due to mechanical switch noise.
 

Ya’akov

Joined Jan 27, 2019
9,170
One of the things that ChatGPT has had a lot of success with is code. I don’t use it for that so I can‘t personally comment but sources I trust say it can be really very good—or, unfortunately, bad.

The same weaknesses it shows elsewhere are still present and it will always give a plausible answer but not always a correct one. So, some of my informants have stopped using it and others say you must perform due diligence if using code supplied by it.

Still, in general, it appears to be on the cusp of reliability in code domain.
 

nsaspook

Joined Aug 27, 2009
13,315
Don't be so sure.... here's a little experiment
...
Now, I'm not saying it would handle anything more complex as well, after all, the Blink sketch is well known and documented. But I think it belies your statement @WBahn and on more complex code may give insight even if its not 100%
ChatGPT does well on code simply because there is no need for actual intelligence to splice bit and pieces of computer language grammars in to something that looks proper from a statistical match at a glance but may have huge numbers of bugs as a useful implementation. ChatGPT has no comprehension of what you entered and what it outputted because it's Large Language Model, not a programming code expert system or AI. Humans are responsible for every character of that code from the original sources, that to the computer, is just another language grammar output with a high statistical match.
Google Code search examples

ChatGPT did not write that code or understand it. Don't trust it, ever, use it with great caution.
 

MrAl

Joined Jun 17, 2014
11,496
I am not a SPICE user. This is the answer it gave me and I find it sensible and convincing but I have no idea if it is correct.
Hi there,

It looks pretty good to me and i've been using spice for at least 20 years or more now and have created many subcircuits.

It's funny this came up because i was just reading about some professor who was saying one of the jobs that will go in the future is programming. He said AI will be able to write programs so that anybody can become a programmer. I find it hard to believe, but with the above and other responses i have seen now maybe it's not too far off.

To take advantage of all this, maybe we need a new forum section dedicated to asking AI questions.
 

MrAl

Joined Jun 17, 2014
11,496
Any why do you think that ChatGPT, which has NO comprehension of ANYTHING, is going to provide an accurate analysis of someone's code?
I just read this morning that the job of programmer may soon be left in the past due to AI taking that over. I read that AI will be able to program and so anybody can become a programmer.
I haven't used it for that yet but maybe I'll do some experiments.
 

nsaspook

Joined Aug 27, 2009
13,315
Hi there,

It looks pretty good to me and i've been using spice for at least 20 years or more now and have created many subcircuits.

It's funny this came up because i was just reading about some professor who was saying one of the jobs that will go in the future is programming. He said AI will be able to write programs so that anybody can become a programmer. I find it hard to believe, but with the above and other responses i have seen now maybe it's not too far off.

To take advantage of all this, maybe we need a new forum section dedicated to asking AI questions.
The current 'AI' systems write CODE from previous human generated examples, they don't program. Code is the rote part of programming like holding a pencil and translating the actual intelligence of another person with the understanding a problem, the mental pictures of how things work, plans and interactions of thoughts into the proper syntax and grammar to be compiled or interpreted on a computer system.
 

Irving

Joined Jan 30, 2016
3,897
I'm not entirely convinced by the arguement "its a large language model" or maybe I'm easily impressed. In my CANBUs example earlier I was surprised by its ability to seemingly intuit macro functionality from the code eg the debouncing of the push button.

I'm trying to think of some code from lab projects that are relatively unique...
 

WBahn

Joined Mar 31, 2012
30,077
I'm not entirely convinced by the arguement "its a large language model" or maybe I'm easily impressed. In my CANBUs example earlier I was surprised by its ability to seemingly intuit macro functionality from the code eg the debouncing of the push button.

I'm trying to think of some code from lab projects that are relatively unique...
If you were to scour the internet for strings of text similar to the question posed, you would find a high likelihood of seeing strings of text similar to the answer it generates. It is merely playing the odds. It is not intuiting anything.
 

djsfantasi

Joined Apr 11, 2010
9,163
Pasting bits of code together is NOT programming. There are multiple threads on here from the same member, who pastes together bits of code. Hundreds of posts later and many years and a successful program is still out of his reach.

Programming is an art as well as a skill. For simple to mid-level programs, ChatGPT may work… But for very complicated solutions using a deep programming toolbox, I’m sure it would fail. I could be wrong and suffering from a bias.

I have an Arduino program that implements a proprietary language for a niche application. It is in two parts. A pre-processor that compiles the language into tokens and a run-time module that interpretively executes the tokenized program. One of these days, I’m going to ask ChatGPT to explain it.

I wonder, could ChatGPT explain ChatGPT?
 

WBahn

Joined Mar 31, 2012
30,077
I wonder, could ChatGPT explain ChatGPT?
I actually wouldn't be surprised if the answer to this question was pretty decent.

Anything that can be found on lots of sites with a simple search is likely to be something that ChatGPT is going to regurgitate fairly well.
 

djsfantasi

Joined Apr 11, 2010
9,163
I actually wouldn't be surprised if the answer to this question was pretty decent.

Anything that can be found on lots of sites with a simple search is likely to be something that ChatGPT is going to regurgitate fairly well.
Yes, but would it answer with the ChatGPT code?
 

nsaspook

Joined Aug 27, 2009
13,315
I'm not entirely convinced by the arguement "its a large language model" or maybe I'm easily impressed. In my CANBUs example earlier I was surprised by its ability to seemingly intuit macro functionality from the code eg the debouncing of the push button.

I'm trying to think of some code from lab projects that are relatively unique...
The models don't really "know what they know". They don't even know what "knowing" is. All they do is guess the next token in a stream, and this next token guess may be based on either well-founded acquired knowledge, or it may be a complete guess.
That's the problem. it obviously doesn't know and you, unless you already have at some familiarity with the information in detail, don't know. If you get a decent answer it's because the problem has been solved thousands of times in the internet universe, not because it's a good solution to a rare or unusual problem. Often, when you query the reason for the answer, Explainability, often goes right out the window. IMO, black box answers make for bad products.
 
Top