Files @ 752fd27f607a
Branch filter:

Location: therm-ng/src/system/error.c

Ethan Zonca
Basic code cleanup
//
// Error: Provides a simple interface for asserting and checking errors
//

#include "stm32f3xx_hal.h"

#include <stdio.h>

#include "config.h"
#include "error.h"

static volatile uint32_t err_reg;
static volatile uint8_t num_errors_asserted = 0;

// Moderately detailed messages corresponding to each error enum
//static const char *  error_message[] =
//{
//		"Info",
//		"General",
//};


// Set the passed error flag
void error_assert(const uint8_t errno)
{
	// Errno invalid: exceeds bit length of error register
	if(errno >= 32)
		return;

	// Don't count info messages as true errors
	if(errno > ERR_INFO)
	{
		// Set error flag
		err_reg |= (1<<errno);

		// Count how many errors have occurred
		num_errors_asserted++;
	}

	// Dispatch error message over IRdA and/or debug port
#ifdef DEBUG_ERROUT_ENABLE
	char outbuf[256];
	snprintf(outbuf, 256, "[%lu\] %s (no details) \r\n", HAL_GetTick(), error_message[errno]);
#endif
}


// Set the passed error flag with details about the error
void error_assert_info(const uint8_t errno, const char* details)
{
	// Errno invalid: exceeds bit length of error register
	if(errno >= 32)
		return;

	// Don't count info messages as true errors
	if(errno > ERR_INFO)
	{
		// Set error flag
		err_reg |= (1<<errno);

		// Count how many errors have occurred
		num_errors_asserted++;
	}

	// Dispatch error message over IRdA and/or debug port
#ifdef DEBUG_ERROUT_ENABLE
	char outbuf[256];
	if(errno == ERR_INFO)
		snprintf(outbuf, 256, "Info: %s \r\n", details);
	else
		snprintf(outbuf, 256, "[%lu\] %s (%s) \r\n", HAL_GetTick(), error_message[errno], details);
	usb_send_debug(outbuf);

	if(errno > ERR_INFO)
	{
		ir_efs_send(outbuf);
		error_sidechannel_addmsg(outbuf);
	}
#endif
}


// Check if the passed error flag has been asserted
inline uint8_t error_check(const uint8_t errno)
{
	return (err_reg & (1<<errno)) > 0;
}


// Return 1 if any error has occurred
inline uint8_t error_occurred(void)
{
	return err_reg > 0;
}


// Return the number of errors that have occurred
inline uint8_t error_count(void)
{
	return num_errors_asserted;
}