wayne roberts / libscpi

Dependents:   scpi_sx127x scpi_sx127x_firstTest MLX90418_I2C_master

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers error.c Source File

error.c

00001 /*-
00002  * Copyright (c) 2012-2013 Jan Breuer,
00003  *
00004  * All Rights Reserved
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are
00008  * met:
00009  * 1. Redistributions of source code must retain the above copyright notice,
00010  *    this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00018  * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
00019  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
00022  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00023  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00024  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 /**
00029  * @file   scpi_error.c
00030  * @date   Thu Nov 15 10:58:45 UTC 2012
00031  *
00032  * @brief  Error handling and storing routines
00033  *
00034  *
00035  */
00036 
00037 #include <stdint.h>
00038 
00039 #include "scpi/parser.h"
00040 #include "scpi/ieee488.h"
00041 #include "scpi/error.h"
00042 #include "fifo_private.h"
00043 
00044 /* basic FIFO */
00045 static scpi_fifo_t local_error_queue;
00046 
00047 
00048 /**
00049  * Initialize error queue
00050  * @param context - scpi context
00051  */
00052 void SCPI_ErrorInit(scpi_t * context) {
00053     /*
00054      * // FreeRTOS
00055      * context->error_queue = (scpi_error_queue_t)xQueueCreate(100, sizeof(int16_t));
00056      */
00057 
00058     /* basic FIFO */
00059     context->error_queue = (scpi_error_queue_t)&local_error_queue;
00060     fifo_init((scpi_fifo_t *)context->error_queue);
00061 }
00062 
00063 /**
00064  * Emit no error
00065  * @param context scpi context
00066  */
00067 static void SCPI_ErrorEmitEmpty(scpi_t * context) {
00068     if ((SCPI_ErrorCount(context) == 0) && (SCPI_RegGet(context, SCPI_REG_STB) & STB_QMA)) {
00069         SCPI_RegClearBits(context, SCPI_REG_STB, STB_QMA);
00070 
00071         if (context->interface && context->interface->error) {
00072             context->interface->error(context, 0);
00073         }
00074     }
00075 }
00076 
00077 /**
00078  * Emit error
00079  * @param context scpi context
00080  * @param err Error to emit
00081  */
00082 static void SCPI_ErrorEmit(scpi_t * context, int16_t err) {
00083     SCPI_RegSetBits(context, SCPI_REG_STB, STB_QMA);
00084 
00085     if (context->interface && context->interface->error) {
00086         context->interface->error(context, err);
00087     }
00088 }
00089 
00090 /**
00091  * Clear error queue
00092  * @param context - scpi context
00093  */
00094 void SCPI_ErrorClear(scpi_t * context) {
00095     /*
00096      * // FreeRTOS
00097      * xQueueReset((xQueueHandle)context->error_queue);
00098      */
00099 
00100     /* basic FIFO */
00101     fifo_clear((scpi_fifo_t *)context->error_queue);
00102 
00103     SCPI_ErrorEmitEmpty(context);
00104 }
00105 
00106 /**
00107  * Pop error from queue
00108  * @param context - scpi context
00109  * @return error number
00110  */
00111 int16_t SCPI_ErrorPop(scpi_t * context) {
00112     int16_t result = 0;
00113 
00114     /*
00115      * // FreeRTOS
00116      * if (pdFALSE == xQueueReceive((xQueueHandle)context->error_queue, &result, 0)) {
00117      *   result = 0;
00118      * }
00119      */
00120 
00121     /* basic FIFO */
00122     fifo_remove((scpi_fifo_t *)context->error_queue, &result);
00123 
00124     SCPI_ErrorEmitEmpty(context);
00125 
00126     return result;
00127 }
00128 
00129 /**
00130  * Return number of errors/events in the queue
00131  * @param context
00132  * @return
00133  */
00134 int32_t SCPI_ErrorCount(scpi_t * context) {
00135     int16_t result = 0;
00136 
00137     /*
00138      * // FreeRTOS
00139      * result = uxQueueMessagesWaiting((xQueueHandle)context->error_queue);
00140      */
00141 
00142     /* basic FIFO */
00143     fifo_count((scpi_fifo_t *)context->error_queue, &result);
00144 
00145     return result;
00146 }
00147 
00148 static void SCPI_ErrorAddInternal(scpi_t * context, int16_t err) {
00149     /*
00150      * // FreeRTOS
00151      * xQueueSend((xQueueHandle)context->error_queue, &err, 0);
00152      */
00153 
00154     /* basic FIFO */
00155     fifo_add((scpi_fifo_t *)context->error_queue, err);
00156 }
00157 
00158 struct error_reg {
00159     int16_t from;
00160     int16_t to;
00161     scpi_reg_val_t bit;
00162 };
00163 
00164 #define ERROR_DEFS_N    9
00165 
00166 static const struct error_reg errs[ERROR_DEFS_N] = {
00167     {-100, -199, ESR_CER}, /* Command error (e.g. syntax error) ch 21.8.9    */
00168     {-200, -299, ESR_EER}, /* Execution Error (e.g. range error) ch 21.8.10  */
00169     {-300, -399, ESR_DER}, /* Device specific error -300, -399 ch 21.8.11    */
00170     {   1,32767, ESR_DER}, /* Device designer provided specific error 1, 32767 ch 21.8.11    */
00171     {-400, -499, ESR_QER}, /* Query error -400, -499 ch 21.8.12              */
00172     {-500, -599, ESR_PON}, /* Power on event -500, -599 ch 21.8.13           */
00173     {-600, -699, ESR_URQ}, /* User Request Event -600, -699 ch 21.8.14       */
00174     {-700, -799, ESR_REQ}, /* Request Control Event -700, -799 ch 21.8.15    */
00175     {-800, -899, ESR_OPC}, /* Operation Complete Event -800, -899 ch 21.8.16 */
00176 };
00177 
00178 /**
00179  * Push error to queue
00180  * @param context - scpi context
00181  * @param err - error number
00182  */
00183 void SCPI_ErrorPush(scpi_t * context, int16_t err) {
00184 
00185     int i;
00186 
00187     SCPI_ErrorAddInternal(context, err);
00188 
00189     for(i = 0; i < ERROR_DEFS_N; i++) {
00190         if ((err <= errs[i].from) && (err >= errs[i].to)) {
00191             SCPI_RegSetBits(context, SCPI_REG_ESR, errs[i].bit);
00192         }
00193     }
00194 
00195     SCPI_ErrorEmit(context, err);
00196 
00197     if (context) {
00198         context->cmd_error = TRUE;
00199     }
00200 }
00201 
00202 /**
00203  * Translate error number to string
00204  * @param err - error number
00205  * @return Error string representation
00206  */
00207 const char * SCPI_ErrorTranslate(int16_t err) {
00208     switch (err) {
00209         case 0: return "No error";
00210 #define X(def, val, str) case def: return str;
00211 #if USE_FULL_ERROR_LIST
00212 #define XE X
00213 #else
00214 #define XE(def, val, str)
00215 #endif
00216 LIST_OF_ERRORS
00217 
00218 #if USE_USER_ERROR_LIST
00219 LIST_OF_USER_ERRORS
00220 #endif
00221 #undef X
00222 #undef XE
00223         default: return "Unknown error";
00224     }
00225 }