xil-se / xildebug_sw

XilDebug is a CMSIS-DAP compliant debugger, UART bridge and power profiler all in one package.
5 stars 1 forks source link

errors: Introduce error code handling #14

Closed kbeckmann closed 6 years ago

kbeckmann commented 6 years ago

This is a draft to show what I mean in issue #11

kbeckmann commented 6 years ago

In GDB i can do this now:

0x080062dc in main () at app/main.c:120
120     ERR_CHECK(r);
(gdb) print g_status
$1 = HAL_ERROR
kbeckmann commented 6 years ago

Got a crazy idea when doing this. What about reserving the least 2 significant bits for HAL_StatusTypeDef?

typedef enum
{
  HAL_OK       = 0x00,
  HAL_ERROR    = 0x01,
  HAL_BUSY     = 0x02,
  HAL_TIMEOUT  = 0x03
} HAL_StatusTypeDef;

That way we can have good errors, but still encode the HAL error without the need of a global, and no worries about race conditions in case you want to check the hal error code programatically.

Edit: Maybe better to store it in bit 14 and 15. Our errors begin at 0x80000000, i.e. bit 31 will always be set. The module is encoded in bits 16-30. 0-15 are left per module. So if we OR in the status in bits 14 and 15, it will just magically work out.

Example:

#define LED_BASE        (ERR_BASE + (0x04 << 16))

// led.c
#define ELED_FOO    (LED_BASE + 2)

ELED_FOO is returned with hal error 0x3:
return (0x80000000 + 0x40000 + 2) | (0x3 << 14) = 0x8004C002  

14 bits for the individual errors should be enough! 16k...

kbeckmann commented 6 years ago

Updated the branch with above changes. Think it works out pretty well.

kbeckmann commented 6 years ago

Oh crap. There are more. fixing. Thanks officer 🚔

kbeckmann commented 6 years ago

It seems there is no way to get gcc to warn about this type of error, i.e. when returning an uint32_t when it should be an err_t, since they resolve to the same type.