Skip to content
This repository has been archived by the owner on May 19, 2022. It is now read-only.

Commit

Permalink
Convert a huge number of configASSERT()s to if (!x) throw. #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jtikalsky committed Nov 15, 2018
1 parent 9ddcd31 commit 6853100
Show file tree
Hide file tree
Showing 32 changed files with 194 additions and 88 deletions.
10 changes: 5 additions & 5 deletions Vivado/ipmc_zynq_vivado.sdk/IPMC/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackT
void vApplicationMallocFailedHook( void )
{
void __real_xil_printf( const char8 *ctrl1, ...);
__real_xil_printf( "HALT: vApplicationMallocFailedHook() called\n" );
configASSERT(0);
__real_xil_printf("HALT: vApplicationMallocFailedHook() called\n");
abort();
}

void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
Expand All @@ -174,7 +174,7 @@ void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )

void __real_xil_printf( const char8 *ctrl1, ...);
__real_xil_printf( "HALT: Task %s overflowed its stack.", pcOverflowingTaskName );
configASSERT(0);
abort();
}

/*-----------------------------------------------------------*/
Expand Down Expand Up @@ -241,11 +241,11 @@ int main() {

//std::terminate();

configASSERT(UWTaskCreate("init", TASK_PRIORITY_WATCHDOG, []() -> void {
UWTaskCreate("init", TASK_PRIORITY_WATCHDOG, []() -> void {
driver_init(true);
ipmc_service_init();
LOG.log(std::string("\n") + generate_banner(), LogTree::LOG_NOTICE); // This is the ONLY place that should EVER log directly to LOG rather than a subtree.
}));
});

/* Start the tasks and timer running. */
vTaskStartScheduler();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <exception>

namespace {

/// A debugging command to transition between M-states.
Expand All @@ -16,7 +18,8 @@ class ConsoleCommand_transition : public CommandParser::Command {
return;
}
std::shared_ptr<HotswapSensor> hotswap = std::dynamic_pointer_cast<HotswapSensor>(ipmc_sensors.find_by_name("Hotswap"));
configASSERT(hotswap);
if (!hotswap)
throw std::out_of_range("No sensor named \"Hotswap\" found!");
hotswap->transition(mstate, static_cast<HotswapSensor::StateTransitionReason>(static_cast<uint8_t>(reason)));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "InterruptBasedDriver.h"
#include <FreeRTOS.h>
#include <xscugic.h>
#include <libs/except.h>

extern XScuGic xInterruptController;

Expand Down Expand Up @@ -48,8 +49,9 @@ void InterruptBasedDriver::connectInterrupt(uint32_t intr, uint8_t trigger) {
}

void InterruptBasedDriver::connectInterrupt() {
configASSERT(XST_SUCCESS ==
XScuGic_Connect(&xInterruptController, this->intr, InterruptBasedDriver::_InterruptWrapper, (void*)this));
if (XST_SUCCESS !=
XScuGic_Connect(&xInterruptController, this->intr, InterruptBasedDriver::_InterruptWrapper, (void*)this))
throw except::hardware_error("Unable to connect handler to interrupt controller.");
this->enableInterrupts();

this->connected = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <queue.h>
#include <task.h>
#include <libs/printf.h>
#include <libs/except.h>

static void XIicPs_VariableLengthSlaveInterruptHandler(XIicPs *InstancePtr);

Expand Down Expand Up @@ -45,7 +46,8 @@ PS_IPMB::PS_IPMB(u16 DeviceId, u32 IntrId, u8 IPMBAddr) :
configASSERT(this->sendresult_q);

XIicPs_Config *Config = XIicPs_LookupConfig(DeviceId);
configASSERT(XST_SUCCESS == XIicPs_CfgInitialize(&this->IicInst, Config, Config->BaseAddress));
if (XST_SUCCESS != XIicPs_CfgInitialize(&this->IicInst, Config, Config->BaseAddress))
throw except::hardware_error(stdsprintf("Unable to initialize PS I2C for PS_IPMB(%hu, %u, %hhu)", DeviceId, IntrId, IPMBAddr));

this->setup_slave();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <mgmt_zone_ctrl.h>
#include <drivers/mgmt_zone/MGMTZone.h>
#include "FreeRTOS.h"
#include <libs/printf.h>
#include <libs/except.h>

/**
* Instantiate a MZ.
Expand All @@ -17,7 +19,8 @@
*/
MGMT_Zone::MGMT_Zone(u16 DeviceId, u32 MZNo)
: DeviceId(DeviceId), MZNo(MZNo) {
configASSERT(XST_SUCCESS == Mgmt_Zone_Ctrl_Initialize(&this->zone, DeviceId));
if (XST_SUCCESS != Mgmt_Zone_Ctrl_Initialize(&this->zone, DeviceId))
throw except::hardware_error(stdsprintf("Unable to initialize MGMT_Zone(%hu, %lu)", DeviceId, MZNo));
}

MGMT_Zone::~MGMT_Zone() {
Expand Down Expand Up @@ -71,7 +74,8 @@ u64 MGMT_Zone::get_hardfault_status(bool apply_mask) {
* @param pen_config An array of the correct number of OutputConfig entries.
*/
void MGMT_Zone::set_pen_config(const std::vector<OutputConfig> &pen_config) {
configASSERT(pen_config.size() == this->get_pen_count());
if (pen_config.size() != this->get_pen_count())
throw std::logic_error(stdsprintf("Supplied PEN config vector specifies an incorrect number of PENs (%u/%lu) for MZ %lu", pen_config.size(), this->get_pen_count(), this->MZNo));
MZ_config config;
Mgmt_Zone_Ctrl_Get_MZ_Cfg(&this->zone, this->MZNo, &config);
for (uint32_t i = 0; i < this->get_pen_count(); ++i) {
Expand Down Expand Up @@ -125,7 +129,7 @@ void MGMT_Zone::set_power_state(PowerAction action) {
case ON: Mgmt_Zone_Ctrl_Pwr_ON_Seq(&this->zone, this->MZNo); break;
case OFF: Mgmt_Zone_Ctrl_Pwr_OFF_Seq(&this->zone, this->MZNo); break;
case KILL: Mgmt_Zone_Ctrl_Dispatch_Soft_Fault(&this->zone, this->MZNo); break;
default: configASSERT(0); // Programming Error.
default: throw std::domain_error(stdsprintf("Invalid PowerAction %u supplied to set_power_state() for MZ %lu", static_cast<unsigned int>(action), this->MZNo));
}
}

Expand All @@ -144,7 +148,7 @@ bool MGMT_Zone::get_power_state(bool *in_transition) {
case MZ_PWR_ON: active = true; transitioning = false; break;
case MZ_PWR_TRANS_OFF: active = false; transitioning = true; break;
case MZ_PWR_TRANS_ON: active = true; transitioning = true; break;
default: configASSERT(0); // Programming Error.
default: throw except::hardware_error("Invalid power state read from MGMT_Zone driver"); break;
}
if (in_transition)
*in_transition = transitioning;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,20 @@ Network *pNetworkInstance = NULL;

Network::Network(LogTree &logtree, uint8_t mac[6], std::function<void(Network*)> net_ready_cb) :
logtree(logtree) {
configASSERT(!pNetworkInstance);
if (pNetworkInstance)
throw std::logic_error("Network already constructed & initialized.");
pNetworkInstance = this;
memcpy(this->mac, mac, sizeof(uint8_t) * 6);
memset(&(this->netif), 0, sizeof(struct netif));

configASSERT(UWTaskCreate("network_start", TCPIP_THREAD_PRIO, [this,net_ready_cb]() -> void {
UWTaskCreate("network_start", TCPIP_THREAD_PRIO, [this,net_ready_cb]() -> void {
// It is imperative that lwIP gets initialized before the network start thread gets launched
// otherwise there will be problems with TCP requests
lwip_init();
this->thread_network_start();
if (net_ready_cb)
net_ready_cb(this);
}));
});
}

void Network::thread_network_start() {
Expand Down Expand Up @@ -102,7 +103,7 @@ void Network::thread_network_start() {
netif_set_up(&(this->netif));

// Start packet receive thread, required for lwIP operation
configASSERT(UWTaskCreate("xemacifd", TCPIP_THREAD_XEMACIFD_PRIO, [this]() -> void { xemacif_input_thread(&this->netif); }));
UWTaskCreate("xemacifd", TCPIP_THREAD_XEMACIFD_PRIO, [this]() -> void { xemacif_input_thread(&this->netif); });

#if LWIP_DHCP==1
// If DHCP is enabled then start it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

#include <drivers/pim400/PIM400.h>
#include <libs/printf.h>
#include <libs/except.h>
#include <services/console/CommandParser.h>
#include <services/console/ConsoleSvc.h>

PIM400::PIM400(I2C &i2c, uint8_t i2c_addr) :
i2c(i2c), i2c_addr(i2c_addr>>1) {
configASSERT(this->i2c_addr > 0);
if (this->i2c_addr == 0)
throw std::domain_error("Invalid I2C address (0) for PIM400.");
}

PIM400::~PIM400() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@

#include "PLGPIO.h"
#include "xparameters.h"
#include <libs/printf.h>
#include <libs/except.h>

PL_GPIO::PL_GPIO(uint16_t DeviceId)
: InterruptBasedDriver(), callback(nullptr) {
// Initialize the Gpio driver so that it's ready to use.
configASSERT(XST_SUCCESS == XGpio_Initialize(&(this->Gpio), DeviceId));
if (XST_SUCCESS != XGpio_Initialize(&(this->Gpio), DeviceId))
throw except::hardware_error(stdsprintf("Unable to initialize PL_GPIO(%hu)", DeviceId));

// Perform a self-test to ensure that the hardware was built correctly.
configASSERT(XST_SUCCESS == XGpio_SelfTest(&(this->Gpio)));
if (XST_SUCCESS != XGpio_SelfTest(&(this->Gpio)))
throw except::hardware_error(stdsprintf("Self-test failed for PL_GPIO(%hu)", DeviceId));
}

PL_GPIO::PL_GPIO(uint16_t DeviceId, uint32_t IntrId)
: InterruptBasedDriver(), callback(nullptr) {
// Initialize the Gpio driver so that it's ready to use.
configASSERT(XST_SUCCESS == XGpio_Initialize(&(this->Gpio), DeviceId));
if (XST_SUCCESS != XGpio_Initialize(&(this->Gpio), DeviceId))
throw except::hardware_error(stdsprintf("Unable to initialize PL_GPIO(%hu, %lu)", DeviceId, IntrId));

// Perform a self-test to ensure that the hardware was built correctly.
configASSERT(XST_SUCCESS == XGpio_SelfTest(&(this->Gpio)));
if (XST_SUCCESS != XGpio_SelfTest(&(this->Gpio)))
throw except::hardware_error(stdsprintf("Self-test failed for PL_GPIO(%hu)", DeviceId));

if (this->Gpio.InterruptPresent) {
this->connectInterrupt(IntrId, 0x03);
Expand Down
18 changes: 12 additions & 6 deletions Vivado/ipmc_zynq_vivado.sdk/common/uw-ipmc/drivers/pl_i2c/PLI2C.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <xiic.h>
#include <drivers/pl_i2c/PLI2C.h>
#include <xscugic.h>
#include <libs/printf.h>
#include <libs/except.h>

/*
* It has been observed that interrupted based IIC takes 5 times
Expand Down Expand Up @@ -64,12 +66,15 @@ DeviceId(DeviceId), IntrId(IntrId), I2C() {

// Initialize the IIC driver so that it is ready to use.
ConfigPtr = XIic_LookupConfig(this->DeviceId);
configASSERT(ConfigPtr != NULL);
if (ConfigPtr == NULL)
throw except::hardware_error(stdsprintf("No XIic config available for PL_I2C(%hu, %lu)", DeviceId, IntrId));

configASSERT(XST_SUCCESS ==
XIic_CfgInitialize(&(this->IicInst), ConfigPtr, ConfigPtr->BaseAddress));
if (XST_SUCCESS !=
XIic_CfgInitialize(&(this->IicInst), ConfigPtr, ConfigPtr->BaseAddress))
throw except::hardware_error(stdsprintf("Unable to initialize PL_I2C(%hu, %lu)", DeviceId, IntrId));

configASSERT(XST_SUCCESS == XIic_SelfTest(&(this->IicInst)));
if (XST_SUCCESS != XIic_SelfTest(&(this->IicInst)))
throw except::hardware_error(stdsprintf("Self-test failed for PL_I2C(%hu, %lu)", DeviceId, IntrId));

// Enable dynamic mode - it seems that dynamic mode doesn't support variable length receives
//XIic_DynamicInitialize(&(this->IicInst));
Expand All @@ -86,8 +91,9 @@ DeviceId(DeviceId), IntrId(IntrId), I2C() {
XIic_MultiMasterInclude();

// Enable interrupts
configASSERT(XST_SUCCESS ==
XScuGic_Connect(&xInterruptController, this->IntrId, XIic_InterruptHandler, (void*)&(this->IicInst)));
if (XST_SUCCESS !=
XScuGic_Connect(&xInterruptController, this->IntrId, XIic_InterruptHandler, (void*)&(this->IicInst)))
throw except::hardware_error(stdsprintf("Failed to connect PL_I2C(%hu, %lu) to the GIC.", DeviceId, IntrId));
XScuGic_Enable(&xInterruptController, this->IntrId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/

#include <drivers/pl_spi/PLSPI.h>
#include <libs/printf.h>
#include <libs/except.h>

void XSpi_Abort(XSpi *InstancePtr)
{
Expand Down Expand Up @@ -221,13 +223,16 @@ PL_SPI::PL_SPI(uint16_t DeviceId, uint32_t IntrId)
configASSERT(this->sync);

// Initialize the XSpi driver so that it's ready to use
configASSERT(XST_SUCCESS == XSpi_Initialize(&(this->xspi), DeviceId));
if (XST_SUCCESS != XSpi_Initialize(&(this->xspi), DeviceId))
throw except::hardware_error(stdsprintf("Unable to initialize PL_SPI(%hu, %lu)", DeviceId, IntrId));

// Perform a self-test to ensure that the hardware was built correctly
configASSERT(XST_SUCCESS == XSpi_SelfTest(&(this->xspi)));
if (XST_SUCCESS != XSpi_SelfTest(&(this->xspi)))
throw except::hardware_error(stdsprintf("Self-test for PL_SPI(%hu, %lu) failed.", DeviceId, IntrId));

// Apply proper settings to the IP.
configASSERT(XST_SUCCESS == XSpi_SetOptions(&this->xspi, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION));
if (XST_SUCCESS != XSpi_SetOptions(&this->xspi, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION))
throw except::hardware_error(stdsprintf("Unable to XSpi_SetOptions on PL_SPI(%hu, %lu)", DeviceId, IntrId));
}

PL_SPI::~PL_SPI() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <stdio.h>
#include "xil_exception.h"
#include "xuartlite_l.h"
#include <libs/printf.h>
#include <libs/except.h>

extern XScuGic xInterruptController;

Expand Down Expand Up @@ -42,19 +44,22 @@ void PL_UART::_InterruptHandler(PL_UART *uart)
PL_UART::PL_UART(uint16_t DeviceId, uint32_t IntrId, size_t ibufsize, size_t obufsize) :
IntrId(IntrId) {
// Initialize the UartLite driver so that it's ready to use.
configASSERT(XST_SUCCESS == XUartLite_Initialize(&(this->UartLite), DeviceId));
if (XST_SUCCESS != XUartLite_Initialize(&(this->UartLite), DeviceId))
throw except::hardware_error(stdsprintf("Unable to initialize PL_UART(%hu, %lu, ...)", DeviceId, IntrId));

// Perform a self-test to ensure that the hardware was built correctly.
configASSERT(XST_SUCCESS == XUartLite_SelfTest(&(this->UartLite)));
if (XST_SUCCESS != XUartLite_SelfTest(&(this->UartLite)))
throw except::hardware_error(stdsprintf("Self-test failed for PL_UART(%hu, %lu, ...)", DeviceId, IntrId));

// UartLite interrupt works differently than expected, adjust trigger type.
uint8_t priority, trigger;
XScuGic_GetPriorityTriggerType(&xInterruptController, this->IntrId, &priority, &trigger);
XScuGic_SetPriorityTriggerType(&xInterruptController, this->IntrId, priority, 0x3);

// Connect the driver to the interrupt subsystem.
configASSERT(XST_SUCCESS ==
XScuGic_Connect(&xInterruptController, this->IntrId, (Xil_InterruptHandler)PL_UART::_InterruptHandler, (void*)this));
if (XST_SUCCESS !=
XScuGic_Connect(&xInterruptController, this->IntrId, (Xil_InterruptHandler)PL_UART::_InterruptHandler, (void*)this))
throw except::hardware_error(stdsprintf("Unable to connect PL_UART(%hu, %lu, ...) to the GIC.", DeviceId, IntrId));
XScuGic_Enable(&xInterruptController, this->IntrId);

this->recvstream = xStreamBufferCreate(ibufsize, 0);
Expand Down
Loading

0 comments on commit 6853100

Please sign in to comment.