Code clean up and handling FIFO errors

Revision:
23:bb685f35b08b
Parent:
15:b5d57daeb7c0
Child:
32:52445bef314d
diff -r 2835e9e9f59f -r bb685f35b08b common/utils.c
--- a/common/utils.c	Thu Sep 26 05:56:58 2019 +0000
+++ b/common/utils.c	Thu Sep 26 06:15:39 2019 +0000
@@ -49,12 +49,9 @@
 {
     ADMW_LOG_INFO("Status Summary:");
 
-    if (pStatus->deviceStatus == 0)
-    {
+    if (pStatus->deviceStatus == 0) {
         ADMW_LOG_INFO("\tNo errors detected");
-    }
-    else
-    {
+    } else {
         if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_BUSY)
             ADMW_LOG_INFO("\tCommand running");
         if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_DATAREADY)
@@ -67,27 +64,23 @@
             ADMW_LOG_INFO("\tActive Configuration Errors - ATTENTION REQUIRED");
         if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_LUT_ERROR)
             ADMW_LOG_INFO("\tActive Look-Up Table Errors - ATTENTION REQUIRED");
-        
+
 
-        if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR)
-        {
+        if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) {
             ADMW_LOG_INFO("\tActive Errors - ATTENTION REQUIRED");
             ADMW_LOG_INFO("\t\tLast Error Code: %u (0x%X)",
-                               pStatus->errorCode, pStatus->errorCode);
+                          pStatus->errorCode, pStatus->errorCode);
 
-            if (pStatus->diagnosticsStatus == 0)
-            {
+            if (pStatus->diagnosticsStatus == 0) {
                 ADMW_LOG_INFO("\t\tNo diagnostics faults detected");
-            }
-            else
-            {
+            } else {
                 ADMW_LOG_INFO("\t\tActive diagnostics faults:");
 
                 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CHECKSUM_ERROR)
                     ADMW_LOG_INFO("\t\t\tInternal Checksum fault detected");
-               
-                
-                
+
+
+
                 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CONVERSION_ERROR)
                     ADMW_LOG_INFO("\t\t\tInternal ADC Conversions fault detected");
                 if (pStatus->diagnosticsStatus & ADMW_DIAGNOSTICS_STATUS_CALIBRATION_ERROR)
@@ -95,21 +88,19 @@
             }
         }
 
-        if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)
-        {
+        if (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT) {
             ADMW_LOG_INFO("\tActive Alerts - ATTENTION REQUIRED:");
             ADMW_LOG_INFO("\t\tLast Alert Code: %u (0x%X)",
-                               pStatus->alertCode, pStatus->alertCode);
+                          pStatus->alertCode, pStatus->alertCode);
 
-            for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++)
-            {
+            for (unsigned i = 0; i < ADMW1001_MAX_CHANNELS; i++) {
                 if (pStatus->channelAlerts[i] == 0)
                     continue;
 
                 ADMW_LOG_INFO("\t\tChannel #%u:", i);
                 ADMW_LOG_INFO("\t\t\tLast Alert Code: %u (0x%X)",
-                                   pStatus->channelAlertCodes[i],
-                                   pStatus->channelAlertCodes[i]);
+                              pStatus->channelAlertCodes[i],
+                              pStatus->channelAlertCodes[i]);
                 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_TIMEOUT)
                     ADMW_LOG_INFO("\t\t\tTimeout alert detected");
                 if (pStatus->channelAlerts[i] & ADMW_CHANNEL_ALERT_UNDER_RANGE)
@@ -140,11 +131,10 @@
         }
 
         if ((pStatus->deviceStatus & ADMW_DEVICE_STATUS_ERROR) ||
-            (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT))
-        {
+                (pStatus->deviceStatus & ADMW_DEVICE_STATUS_ALERT)) {
             ADMW_LOG_INFO("\t\tLast Debug Code: %u-%u",
-                               (pStatus->debugCode >> 16) & 0xFFFF,
-                               (pStatus->debugCode >> 0) & 0xFFFF);
+                          (pStatus->debugCode >> 16) & 0xFFFF,
+                          (pStatus->debugCode >> 0) & 0xFFFF);
         }
     }
 }
@@ -154,16 +144,15 @@
     uint32_t nNumSamples,
     ADMW_MEASUREMENT_MODE eMeasurementMode)
 {
- 
-    for (uint32_t i = 0; i < nNumSamples; i++)
-    {
-            ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %0X %f :: Processed %f :: flags:%s%s",
-                               i+1,
-                               pSampleBuffer[i].channelId,
-                               pSampleBuffer[i].rawValue, (pSampleBuffer[i].rawValue/1024.0),
-                               pSampleBuffer[i].processedValue,
-                               pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "",
-                               pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : "");
+
+    for (uint32_t i = 0; i < nNumSamples; i++) {
+        ADMW_LOG_INFO("Sample # %2d Channel # %2d :: Raw %0X %f :: Processed %f :: flags:%s%s",
+                      i+1,
+                      pSampleBuffer[i].channelId,
+                      pSampleBuffer[i].rawValue, (pSampleBuffer[i].rawValue/1024.0),
+                      pSampleBuffer[i].processedValue,
+                      pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ERROR ? " ERROR" : "",
+                      pSampleBuffer[i].status & ADMW_DEVICE_STATUS_ALERT ? " ALERT" : "");
     }
 }
 
@@ -183,28 +172,24 @@
     bool state;
 
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
-                                         gpioCallbackFn, (void *)pbDataReady);
-    if (res != ADMW_SUCCESS)
-    {
+                                    gpioCallbackFn, (void *)pbDataReady);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to register DATAREADY callback");
         return res;
     }
 
     res = admw_GetGpioState(hDevice, ADMW_GPIO_PIN_ALERT_ERROR, &state);
-    if (res != ADMW_SUCCESS)
-    {
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to get current ERROR state");
         return res;
     }
-    if (state)
-    {
+    if (state) {
         ADMW_LOG_ERROR("ERROR signal already asserted");
         return ADMW_FAILURE;
     }
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
-                                         gpioCallbackFn, (void *)pbError);
-    if (res != ADMW_SUCCESS)
-    {
+                                    gpioCallbackFn, (void *)pbError);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to register ERROR callback");
         return res;
     }
@@ -221,9 +206,8 @@
         return ADMW_FAILURE;
     }*/
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
-                                         gpioCallbackFn, (void *)pbAlert);
-    if (res != ADMW_SUCCESS)
-    {
+                                    gpioCallbackFn, (void *)pbAlert);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to register ALERT callback");
         return res;
     }
@@ -237,25 +221,22 @@
     ADMW_RESULT res;
 
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_DATAREADY,
-                                         NULL, NULL);
-    if (res != ADMW_SUCCESS)
-    {
+                                    NULL, NULL);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to deregister DATAREADY callback");
         return res;
     }
 
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
-                                         NULL, NULL);
-    if (res != ADMW_SUCCESS)
-    {
+                                    NULL, NULL);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to deregister ERROR callback");
         return res;
     }
 
     res = admw_RegisterGpioCallback(hDevice, ADMW_GPIO_PIN_ALERT_ERROR,
-                                         NULL, NULL);
-    if (res != ADMW_SUCCESS)
-    {
+                                    NULL, NULL);
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_INFO("Failed to deregister ALERT callback");
         return res;
     }
@@ -286,12 +267,12 @@
     uint32_t nSamplesPerCycle;
     uint8_t nBytesPerSample;
     res = admw1001_GetDataReadyModeInfo(hDevice,
-                                              eMeasurementMode,
-                                              &eOperatingMode,
-                                              &eDataReadyMode,
-                                              &nSamplesPerDataready,
-                                              &nSamplesPerCycle,
-                                              &nBytesPerSample);
+                                        eMeasurementMode,
+                                        &eOperatingMode,
+                                        &eDataReadyMode,
+                                        &nSamplesPerDataready,
+                                        &nSamplesPerCycle,
+                                        &nBytesPerSample);
     if (res != ADMW_SUCCESS)
         return res;
 
@@ -301,8 +282,7 @@
     ADMW_DATA_SAMPLE *pSampleBuffer;
     pSampleBuffer = malloc(sizeof(ADMW_DATA_SAMPLE) *
                            nSamplesPerDataready);
-    if (pSampleBuffer == NULL)
-    {
+    if (pSampleBuffer == NULL) {
         ADMW_LOG_ERROR("Failed to allocate sample buffer");
         return ADMW_NO_MEM;
     }
@@ -312,8 +292,7 @@
      */
     ADMW_LOG_INFO("Starting measurement");
     res = admw_StartMeasurement(hDevice, eMeasurementMode);
-    if (res != ADMW_SUCCESS)
-    {
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to start measurement");
         return res;
     }
@@ -323,8 +302,7 @@
      */
     uint32_t nSampleCount = 0;
     uint32_t nReturned;
-    while (true)
-    {
+    while (true) {
         ADMW_STATUS status;
 
         /*
@@ -334,29 +312,24 @@
         while (! (bDataReady || bError))
             ;
 
-        if (!bError)
-        {
+        if (!bError) {
             /*
              * Get data samples from the measurement cycle, if no error has occurred
              */
             bDataReady = false;
             res = admw_GetData(hDevice, eMeasurementMode, pSampleBuffer,
-                                    nBytesPerSample, nSamplesPerDataready,
-                                    &nReturned);
-            if (res != ADMW_SUCCESS)
-            {
-                if (res == ADMW_INCOMPLETE)
-                {
+                               nBytesPerSample, nSamplesPerDataready,
+                               &nReturned);
+            if (res != ADMW_SUCCESS) {
+                if (res == ADMW_INCOMPLETE) {
                     /*
                      * This is expected in cases where cycleSkipCount may
                      * be non-zero for some channels, resulting in
                      * variable-length sequences
                      */
                     ADMW_LOG_DEBUG("Retrieved %u of %u requested data samples",
-                                        nReturned, nSamplesPerDataready);
-                }
-                else
-                {
+                                   nReturned, nSamplesPerDataready);
+                } else {
                     ADMW_LOG_WARN("Failed to get data samples from device");
                     return res;
                 }
@@ -377,18 +350,15 @@
         /*
          * Check and print device status if errors/alerts have been triggered
          */
-        if (bError || bAlert)
-        {
+        if (bError || bAlert) {
             res = admw_GetStatus(hDevice, &status);
-            if (res != ADMW_SUCCESS)
-            {
+            if (res != ADMW_SUCCESS) {
                 ADMW_LOG_ERROR("Failed to retrieve device status");
                 return res;
             }
 
             if (status.deviceStatus &
-                (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT))
-            {
+                    (ADMW_DEVICE_STATUS_ERROR | ADMW_DEVICE_STATUS_ALERT)) {
                 utils_printStatus(&status);
 
                 /* Break out of the loop if any errors are raised */
@@ -397,8 +367,7 @@
             }
         }
 
-        if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE)
-        {
+        if (eOperatingMode == ADMW1001_OPERATING_MODE_SINGLECYCLE) {
             /*
              * In this mode, break out of the loop when the measurement command
              * has completed.
@@ -414,8 +383,7 @@
              */
             bool bCommandRunning;
             res = admw_GetCommandRunningState(hDevice, &bCommandRunning);
-            if (res != ADMW_SUCCESS)
-            {
+            if (res != ADMW_SUCCESS) {
                 ADMW_LOG_ERROR("Failed to get command-running status");
                 return res;
             }
@@ -427,8 +395,7 @@
 
     ADMW_LOG_INFO("Stopping measurement");
     res = admw_StopMeasurement(hDevice);
-    if (res != ADMW_SUCCESS)
-    {
+    if (res != ADMW_SUCCESS) {
         ADMW_LOG_ERROR("Failed to send stop measurement");
         return res;
     }