scpi-parser, adopted from github
libscpi/error.c@0:d8aca47812e0, 2014-11-14 (annotated)
- Committer:
- iesensor
- Date:
- Fri Nov 14 01:07:59 2014 +0000
- Revision:
- 0:d8aca47812e0
tested on K64F
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
iesensor | 0:d8aca47812e0 | 1 | /*- |
iesensor | 0:d8aca47812e0 | 2 | * Copyright (c) 2012-2013 Jan Breuer, |
iesensor | 0:d8aca47812e0 | 3 | * |
iesensor | 0:d8aca47812e0 | 4 | * All Rights Reserved |
iesensor | 0:d8aca47812e0 | 5 | * |
iesensor | 0:d8aca47812e0 | 6 | * Redistribution and use in source and binary forms, with or without |
iesensor | 0:d8aca47812e0 | 7 | * modification, are permitted provided that the following conditions are |
iesensor | 0:d8aca47812e0 | 8 | * met: |
iesensor | 0:d8aca47812e0 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
iesensor | 0:d8aca47812e0 | 10 | * this list of conditions and the following disclaimer. |
iesensor | 0:d8aca47812e0 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
iesensor | 0:d8aca47812e0 | 12 | * notice, this list of conditions and the following disclaimer in the |
iesensor | 0:d8aca47812e0 | 13 | * documentation and/or other materials provided with the distribution. |
iesensor | 0:d8aca47812e0 | 14 | * |
iesensor | 0:d8aca47812e0 | 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
iesensor | 0:d8aca47812e0 | 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
iesensor | 0:d8aca47812e0 | 17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
iesensor | 0:d8aca47812e0 | 18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE |
iesensor | 0:d8aca47812e0 | 19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
iesensor | 0:d8aca47812e0 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
iesensor | 0:d8aca47812e0 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
iesensor | 0:d8aca47812e0 | 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
iesensor | 0:d8aca47812e0 | 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
iesensor | 0:d8aca47812e0 | 24 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
iesensor | 0:d8aca47812e0 | 25 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
iesensor | 0:d8aca47812e0 | 26 | */ |
iesensor | 0:d8aca47812e0 | 27 | |
iesensor | 0:d8aca47812e0 | 28 | /** |
iesensor | 0:d8aca47812e0 | 29 | * @file scpi_error.c |
iesensor | 0:d8aca47812e0 | 30 | * @date Thu Nov 15 10:58:45 UTC 2012 |
iesensor | 0:d8aca47812e0 | 31 | * |
iesensor | 0:d8aca47812e0 | 32 | * @brief Error handling and storing routines |
iesensor | 0:d8aca47812e0 | 33 | * |
iesensor | 0:d8aca47812e0 | 34 | * |
iesensor | 0:d8aca47812e0 | 35 | */ |
iesensor | 0:d8aca47812e0 | 36 | |
iesensor | 0:d8aca47812e0 | 37 | #include <stdint.h> |
iesensor | 0:d8aca47812e0 | 38 | |
iesensor | 0:d8aca47812e0 | 39 | #include "scpi/parser.h" |
iesensor | 0:d8aca47812e0 | 40 | #include "scpi/ieee488.h" |
iesensor | 0:d8aca47812e0 | 41 | #include "scpi/error.h" |
iesensor | 0:d8aca47812e0 | 42 | #include "scpi/fifo.h" |
iesensor | 0:d8aca47812e0 | 43 | |
iesensor | 0:d8aca47812e0 | 44 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 45 | static fifo_t local_error_queue; |
iesensor | 0:d8aca47812e0 | 46 | |
iesensor | 0:d8aca47812e0 | 47 | |
iesensor | 0:d8aca47812e0 | 48 | |
iesensor | 0:d8aca47812e0 | 49 | void SCPI_ErrorInit(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 50 | /* |
iesensor | 0:d8aca47812e0 | 51 | * // FreeRTOS |
iesensor | 0:d8aca47812e0 | 52 | * context->error_queue = (scpi_error_queue_t)xQueueCreate(100, sizeof(int16_t)); |
iesensor | 0:d8aca47812e0 | 53 | */ |
iesensor | 0:d8aca47812e0 | 54 | |
iesensor | 0:d8aca47812e0 | 55 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 56 | context->error_queue = (scpi_error_queue_t)&local_error_queue; |
iesensor | 0:d8aca47812e0 | 57 | fifo_init((fifo_t *)context->error_queue); |
iesensor | 0:d8aca47812e0 | 58 | } |
iesensor | 0:d8aca47812e0 | 59 | |
iesensor | 0:d8aca47812e0 | 60 | /** |
iesensor | 0:d8aca47812e0 | 61 | * Clear error queue |
iesensor | 0:d8aca47812e0 | 62 | * @param context - scpi context |
iesensor | 0:d8aca47812e0 | 63 | */ |
iesensor | 0:d8aca47812e0 | 64 | void SCPI_ErrorClear(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 65 | /* |
iesensor | 0:d8aca47812e0 | 66 | * // FreeRTOS |
iesensor | 0:d8aca47812e0 | 67 | * xQueueReset((xQueueHandle)context->error_queue); |
iesensor | 0:d8aca47812e0 | 68 | */ |
iesensor | 0:d8aca47812e0 | 69 | |
iesensor | 0:d8aca47812e0 | 70 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 71 | fifo_clear((fifo_t *)context->error_queue); |
iesensor | 0:d8aca47812e0 | 72 | } |
iesensor | 0:d8aca47812e0 | 73 | |
iesensor | 0:d8aca47812e0 | 74 | /** |
iesensor | 0:d8aca47812e0 | 75 | * Pop error from queue |
iesensor | 0:d8aca47812e0 | 76 | * @param context - scpi context |
iesensor | 0:d8aca47812e0 | 77 | * @return error number |
iesensor | 0:d8aca47812e0 | 78 | */ |
iesensor | 0:d8aca47812e0 | 79 | int16_t SCPI_ErrorPop(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 80 | int16_t result = 0; |
iesensor | 0:d8aca47812e0 | 81 | |
iesensor | 0:d8aca47812e0 | 82 | /* |
iesensor | 0:d8aca47812e0 | 83 | * // FreeRTOS |
iesensor | 0:d8aca47812e0 | 84 | * if (pdFALSE == xQueueReceive((xQueueHandle)context->error_queue, &result, 0)) { |
iesensor | 0:d8aca47812e0 | 85 | * result = 0; |
iesensor | 0:d8aca47812e0 | 86 | * } |
iesensor | 0:d8aca47812e0 | 87 | */ |
iesensor | 0:d8aca47812e0 | 88 | |
iesensor | 0:d8aca47812e0 | 89 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 90 | fifo_remove((fifo_t *)context->error_queue, &result); |
iesensor | 0:d8aca47812e0 | 91 | |
iesensor | 0:d8aca47812e0 | 92 | return result; |
iesensor | 0:d8aca47812e0 | 93 | } |
iesensor | 0:d8aca47812e0 | 94 | |
iesensor | 0:d8aca47812e0 | 95 | /** |
iesensor | 0:d8aca47812e0 | 96 | * Return number of errors/events in the queue |
iesensor | 0:d8aca47812e0 | 97 | * @param context |
iesensor | 0:d8aca47812e0 | 98 | * @return |
iesensor | 0:d8aca47812e0 | 99 | */ |
iesensor | 0:d8aca47812e0 | 100 | int32_t SCPI_ErrorCount(scpi_t * context) { |
iesensor | 0:d8aca47812e0 | 101 | int16_t result = 0; |
iesensor | 0:d8aca47812e0 | 102 | |
iesensor | 0:d8aca47812e0 | 103 | /* |
iesensor | 0:d8aca47812e0 | 104 | * // FreeRTOS |
iesensor | 0:d8aca47812e0 | 105 | * result = uxQueueMessagesWaiting((xQueueHandle)context->error_queue); |
iesensor | 0:d8aca47812e0 | 106 | */ |
iesensor | 0:d8aca47812e0 | 107 | |
iesensor | 0:d8aca47812e0 | 108 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 109 | fifo_count((fifo_t *)context->error_queue, &result); |
iesensor | 0:d8aca47812e0 | 110 | |
iesensor | 0:d8aca47812e0 | 111 | return result; |
iesensor | 0:d8aca47812e0 | 112 | } |
iesensor | 0:d8aca47812e0 | 113 | |
iesensor | 0:d8aca47812e0 | 114 | static void SCPI_ErrorAddInternal(scpi_t * context, int16_t err) { |
iesensor | 0:d8aca47812e0 | 115 | /* |
iesensor | 0:d8aca47812e0 | 116 | * // FreeRTOS |
iesensor | 0:d8aca47812e0 | 117 | * xQueueSend((xQueueHandle)context->error_queue, &err, 0); |
iesensor | 0:d8aca47812e0 | 118 | */ |
iesensor | 0:d8aca47812e0 | 119 | |
iesensor | 0:d8aca47812e0 | 120 | /* basic FIFO */ |
iesensor | 0:d8aca47812e0 | 121 | fifo_add((fifo_t *)context->error_queue, err); |
iesensor | 0:d8aca47812e0 | 122 | } |
iesensor | 0:d8aca47812e0 | 123 | |
iesensor | 0:d8aca47812e0 | 124 | struct error_reg { |
iesensor | 0:d8aca47812e0 | 125 | int16_t from; |
iesensor | 0:d8aca47812e0 | 126 | int16_t to; |
iesensor | 0:d8aca47812e0 | 127 | scpi_reg_val_t bit; |
iesensor | 0:d8aca47812e0 | 128 | }; |
iesensor | 0:d8aca47812e0 | 129 | |
iesensor | 0:d8aca47812e0 | 130 | #define ERROR_DEFS_N 8 |
iesensor | 0:d8aca47812e0 | 131 | |
iesensor | 0:d8aca47812e0 | 132 | static const struct error_reg errs[ERROR_DEFS_N] = { |
iesensor | 0:d8aca47812e0 | 133 | {-100, -199, ESR_CER}, /* Command error (e.g. syntax error) ch 21.8.9 */ |
iesensor | 0:d8aca47812e0 | 134 | {-200, -299, ESR_EER}, /* Execution Error (e.g. range error) ch 21.8.10 */ |
iesensor | 0:d8aca47812e0 | 135 | {-300, -399, ESR_DER}, /* Device specific error -300, -399 ch 21.8.11 */ |
iesensor | 0:d8aca47812e0 | 136 | {-400, -499, ESR_QER}, /* Query error -400, -499 ch 21.8.12 */ |
iesensor | 0:d8aca47812e0 | 137 | {-500, -599, ESR_PON}, /* Power on event -500, -599 ch 21.8.13 */ |
iesensor | 0:d8aca47812e0 | 138 | {-600, -699, ESR_URQ}, /* User Request Event -600, -699 ch 21.8.14 */ |
iesensor | 0:d8aca47812e0 | 139 | {-700, -799, ESR_REQ}, /* Request Control Event -700, -799 ch 21.8.15 */ |
iesensor | 0:d8aca47812e0 | 140 | {-800, -899, ESR_OPC}, /* Operation Complete Event -800, -899 ch 21.8.16 */ |
iesensor | 0:d8aca47812e0 | 141 | }; |
iesensor | 0:d8aca47812e0 | 142 | |
iesensor | 0:d8aca47812e0 | 143 | /** |
iesensor | 0:d8aca47812e0 | 144 | * Push error to queue |
iesensor | 0:d8aca47812e0 | 145 | * @param context - scpi context |
iesensor | 0:d8aca47812e0 | 146 | * @param err - error number |
iesensor | 0:d8aca47812e0 | 147 | */ |
iesensor | 0:d8aca47812e0 | 148 | void SCPI_ErrorPush(scpi_t * context, int16_t err) { |
iesensor | 0:d8aca47812e0 | 149 | |
iesensor | 0:d8aca47812e0 | 150 | int i; |
iesensor | 0:d8aca47812e0 | 151 | |
iesensor | 0:d8aca47812e0 | 152 | SCPI_ErrorAddInternal(context, err); |
iesensor | 0:d8aca47812e0 | 153 | |
iesensor | 0:d8aca47812e0 | 154 | for(i = 0; i < ERROR_DEFS_N; i++) { |
iesensor | 0:d8aca47812e0 | 155 | if ((err <= errs[i].from) && (err >= errs[i].to)) { |
iesensor | 0:d8aca47812e0 | 156 | SCPI_RegSetBits(context, SCPI_REG_ESR, errs[i].bit); |
iesensor | 0:d8aca47812e0 | 157 | } |
iesensor | 0:d8aca47812e0 | 158 | } |
iesensor | 0:d8aca47812e0 | 159 | |
iesensor | 0:d8aca47812e0 | 160 | if (context) { |
iesensor | 0:d8aca47812e0 | 161 | if (context->interface && context->interface->error) { |
iesensor | 0:d8aca47812e0 | 162 | context->interface->error(context, err); |
iesensor | 0:d8aca47812e0 | 163 | } |
iesensor | 0:d8aca47812e0 | 164 | |
iesensor | 0:d8aca47812e0 | 165 | context->cmd_error = TRUE; |
iesensor | 0:d8aca47812e0 | 166 | } |
iesensor | 0:d8aca47812e0 | 167 | } |
iesensor | 0:d8aca47812e0 | 168 | |
iesensor | 0:d8aca47812e0 | 169 | /** |
iesensor | 0:d8aca47812e0 | 170 | * Translate error number to string |
iesensor | 0:d8aca47812e0 | 171 | * @param err - error number |
iesensor | 0:d8aca47812e0 | 172 | * @return Error string representation |
iesensor | 0:d8aca47812e0 | 173 | */ |
iesensor | 0:d8aca47812e0 | 174 | const char * SCPI_ErrorTranslate(int16_t err) { |
iesensor | 0:d8aca47812e0 | 175 | switch (err) { |
iesensor | 0:d8aca47812e0 | 176 | case 0: return "No error"; |
iesensor | 0:d8aca47812e0 | 177 | #define X(def, val, str) case def: return str; |
iesensor | 0:d8aca47812e0 | 178 | LIST_OF_ERRORS |
iesensor | 0:d8aca47812e0 | 179 | #undef X |
iesensor | 0:d8aca47812e0 | 180 | default: return "Unknown error"; |
iesensor | 0:d8aca47812e0 | 181 | } |
iesensor | 0:d8aca47812e0 | 182 | } |
iesensor | 0:d8aca47812e0 | 183 | |
iesensor | 0:d8aca47812e0 | 184 |