Error Handling in C (PI5)

Aus C und Assembler mit Raspberry

Since we currently don't have a way to display information directly to us, we'll use the LED to indicate when something goes wrong. To do this, we’ll create a function that displays an error code using the LED.

We will extend our "led.c" code with the following function:

void LED_Error(u32 errorcode) 
{
    while (TRUE) 
    {
        for (u32 z = 0; z < errorcode; z++) 
        {
            LED_on(); // Turn the LED on
            wait(0x100000); // Short wait time
            LED_off(); // Turn the LED off
            wait(0x100000); // Short wait time
        }
        wait(0x100000); // Additional wait time to make the error code recognizable
    }
}

This function will make the LED blink according to the error code provided. The error code indicates how many times the LED blinks before a short pause occurs.

To test the function, we will modify our "main.c" code as follows:

//
// kernel.c
//

#include "led.h"
#include "time.h"

int main (void)
{
   LED_off();       // Turn off the LED first
   LED_Error(2);    // Test error code 2
}

First, we turn off the LED. Then, we test the new function with "LED_Error(2)." This will cause the LED to blink twice in quick succession, pause briefly, and then repeat this sequence.

Until we have a better way to display errors, we will use this function.

You can download the source code as a ZIP file from the following link: https://www.satyria.de/arm/sources/C/error.zip


< Back (Making the LED Blink in C (PI5)) < Home > Next (Graphics in C (PI5)) >