Liqun Wu / Mbed 2 deprecated 90418_mbed_controller

Dependencies:   mbed

Committer:
wuliqunyy
Date:
Mon Jan 17 13:20:09 2022 +0000
Revision:
0:be95bfb06686
a working non_flat + adc_didt for ehp regulation version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wuliqunyy 0:be95bfb06686 1 /*-
wuliqunyy 0:be95bfb06686 2 * BSD 2-Clause License
wuliqunyy 0:be95bfb06686 3 *
wuliqunyy 0:be95bfb06686 4 * Copyright (c) 2012-2018, Jan Breuer
wuliqunyy 0:be95bfb06686 5 * All rights reserved.
wuliqunyy 0:be95bfb06686 6 *
wuliqunyy 0:be95bfb06686 7 * Redistribution and use in source and binary forms, with or without
wuliqunyy 0:be95bfb06686 8 * modification, are permitted provided that the following conditions are met:
wuliqunyy 0:be95bfb06686 9 *
wuliqunyy 0:be95bfb06686 10 * * Redistributions of source code must retain the above copyright notice, this
wuliqunyy 0:be95bfb06686 11 * list of conditions and the following disclaimer.
wuliqunyy 0:be95bfb06686 12 *
wuliqunyy 0:be95bfb06686 13 * * Redistributions in binary form must reproduce the above copyright notice,
wuliqunyy 0:be95bfb06686 14 * this list of conditions and the following disclaimer in the documentation
wuliqunyy 0:be95bfb06686 15 * and/or other materials provided with the distribution.
wuliqunyy 0:be95bfb06686 16 *
wuliqunyy 0:be95bfb06686 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
wuliqunyy 0:be95bfb06686 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
wuliqunyy 0:be95bfb06686 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
wuliqunyy 0:be95bfb06686 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
wuliqunyy 0:be95bfb06686 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
wuliqunyy 0:be95bfb06686 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
wuliqunyy 0:be95bfb06686 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
wuliqunyy 0:be95bfb06686 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
wuliqunyy 0:be95bfb06686 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
wuliqunyy 0:be95bfb06686 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
wuliqunyy 0:be95bfb06686 27 */
wuliqunyy 0:be95bfb06686 28 /**
wuliqunyy 0:be95bfb06686 29 * @file scpi_error.c
wuliqunyy 0:be95bfb06686 30 * @date Thu Nov 15 10:58:45 UTC 2012
wuliqunyy 0:be95bfb06686 31 *
wuliqunyy 0:be95bfb06686 32 * @brief Error handling and storing routines
wuliqunyy 0:be95bfb06686 33 *
wuliqunyy 0:be95bfb06686 34 *
wuliqunyy 0:be95bfb06686 35 */
wuliqunyy 0:be95bfb06686 36
wuliqunyy 0:be95bfb06686 37 #include <stdint.h>
wuliqunyy 0:be95bfb06686 38
wuliqunyy 0:be95bfb06686 39 #include "scpi/parser.h"
wuliqunyy 0:be95bfb06686 40 #include "scpi/ieee488.h"
wuliqunyy 0:be95bfb06686 41 #include "scpi/error.h"
wuliqunyy 0:be95bfb06686 42 #include "fifo_private.h"
wuliqunyy 0:be95bfb06686 43 #include "scpi/constants.h"
wuliqunyy 0:be95bfb06686 44
wuliqunyy 0:be95bfb06686 45 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
wuliqunyy 0:be95bfb06686 46 #define SCPI_ERROR_SETVAL(e, c, i) do { (e)->error_code = (c); (e)->device_dependent_info = (i); } while(0)
wuliqunyy 0:be95bfb06686 47 #else
wuliqunyy 0:be95bfb06686 48 #define SCPI_ERROR_SETVAL(e, c, i) do { (e)->error_code = (c); (void)(i);} while(0)
wuliqunyy 0:be95bfb06686 49 #endif
wuliqunyy 0:be95bfb06686 50
wuliqunyy 0:be95bfb06686 51 /**
wuliqunyy 0:be95bfb06686 52 * Initialize error queue
wuliqunyy 0:be95bfb06686 53 * @param context - scpi context
wuliqunyy 0:be95bfb06686 54 */
wuliqunyy 0:be95bfb06686 55 void SCPI_ErrorInit(scpi_t * context, scpi_error_t * data, int16_t size) {
wuliqunyy 0:be95bfb06686 56 fifo_init(&context->error_queue, data, size);
wuliqunyy 0:be95bfb06686 57 }
wuliqunyy 0:be95bfb06686 58
wuliqunyy 0:be95bfb06686 59 /**
wuliqunyy 0:be95bfb06686 60 * Emit no error
wuliqunyy 0:be95bfb06686 61 * @param context scpi context
wuliqunyy 0:be95bfb06686 62 */
wuliqunyy 0:be95bfb06686 63 static void SCPI_ErrorEmitEmpty(scpi_t * context) {
wuliqunyy 0:be95bfb06686 64 if ((SCPI_ErrorCount(context) == 0) && (SCPI_RegGet(context, SCPI_REG_STB) & STB_QMA)) {
wuliqunyy 0:be95bfb06686 65 SCPI_RegClearBits(context, SCPI_REG_STB, STB_QMA);
wuliqunyy 0:be95bfb06686 66
wuliqunyy 0:be95bfb06686 67 if (context->interface && context->interface->error) {
wuliqunyy 0:be95bfb06686 68 context->interface->error(context, 0);
wuliqunyy 0:be95bfb06686 69 }
wuliqunyy 0:be95bfb06686 70 }
wuliqunyy 0:be95bfb06686 71 }
wuliqunyy 0:be95bfb06686 72
wuliqunyy 0:be95bfb06686 73 /**
wuliqunyy 0:be95bfb06686 74 * Emit error
wuliqunyy 0:be95bfb06686 75 * @param context scpi context
wuliqunyy 0:be95bfb06686 76 * @param err Error to emit
wuliqunyy 0:be95bfb06686 77 */
wuliqunyy 0:be95bfb06686 78 static void SCPI_ErrorEmit(scpi_t * context, int16_t err) {
wuliqunyy 0:be95bfb06686 79 SCPI_RegSetBits(context, SCPI_REG_STB, STB_QMA);
wuliqunyy 0:be95bfb06686 80
wuliqunyy 0:be95bfb06686 81 if (context->interface && context->interface->error) {
wuliqunyy 0:be95bfb06686 82 context->interface->error(context, err);
wuliqunyy 0:be95bfb06686 83 }
wuliqunyy 0:be95bfb06686 84 }
wuliqunyy 0:be95bfb06686 85
wuliqunyy 0:be95bfb06686 86 /**
wuliqunyy 0:be95bfb06686 87 * Clear error queue
wuliqunyy 0:be95bfb06686 88 * @param context - scpi context
wuliqunyy 0:be95bfb06686 89 */
wuliqunyy 0:be95bfb06686 90 void SCPI_ErrorClear(scpi_t * context) {
wuliqunyy 0:be95bfb06686 91 #if USE_DEVICE_DEPENDENT_ERROR_INFORMATION
wuliqunyy 0:be95bfb06686 92 scpi_error_t error;
wuliqunyy 0:be95bfb06686 93 while (fifo_remove(&context->error_queue, &error)) {
wuliqunyy 0:be95bfb06686 94 SCPIDEFINE_free(&context->error_info_heap, error.device_dependent_info, false);
wuliqunyy 0:be95bfb06686 95 }
wuliqunyy 0:be95bfb06686 96 #endif
wuliqunyy 0:be95bfb06686 97 fifo_clear(&context->error_queue);
wuliqunyy 0:be95bfb06686 98
wuliqunyy 0:be95bfb06686 99 SCPI_ErrorEmitEmpty(context);
wuliqunyy 0:be95bfb06686 100 }
wuliqunyy 0:be95bfb06686 101
wuliqunyy 0:be95bfb06686 102 /**
wuliqunyy 0:be95bfb06686 103 * Pop error from queue
wuliqunyy 0:be95bfb06686 104 * @param context - scpi context
wuliqunyy 0:be95bfb06686 105 * @param error
wuliqunyy 0:be95bfb06686 106 * @return
wuliqunyy 0:be95bfb06686 107 */
wuliqunyy 0:be95bfb06686 108 scpi_bool_t SCPI_ErrorPop(scpi_t * context, scpi_error_t * error) {
wuliqunyy 0:be95bfb06686 109 if (!error || !context) return FALSE;
wuliqunyy 0:be95bfb06686 110 SCPI_ERROR_SETVAL(error, 0, NULL);
wuliqunyy 0:be95bfb06686 111 fifo_remove(&context->error_queue, error);
wuliqunyy 0:be95bfb06686 112
wuliqunyy 0:be95bfb06686 113 SCPI_ErrorEmitEmpty(context);
wuliqunyy 0:be95bfb06686 114
wuliqunyy 0:be95bfb06686 115 return TRUE;
wuliqunyy 0:be95bfb06686 116 }
wuliqunyy 0:be95bfb06686 117
wuliqunyy 0:be95bfb06686 118 /**
wuliqunyy 0:be95bfb06686 119 * Return number of errors/events in the queue
wuliqunyy 0:be95bfb06686 120 * @param context
wuliqunyy 0:be95bfb06686 121 * @return
wuliqunyy 0:be95bfb06686 122 */
wuliqunyy 0:be95bfb06686 123 int32_t SCPI_ErrorCount(scpi_t * context) {
wuliqunyy 0:be95bfb06686 124 int16_t result = 0;
wuliqunyy 0:be95bfb06686 125
wuliqunyy 0:be95bfb06686 126 fifo_count(&context->error_queue, &result);
wuliqunyy 0:be95bfb06686 127
wuliqunyy 0:be95bfb06686 128 return result;
wuliqunyy 0:be95bfb06686 129 }
wuliqunyy 0:be95bfb06686 130
wuliqunyy 0:be95bfb06686 131 static scpi_bool_t SCPI_ErrorAddInternal(scpi_t * context, int16_t err, char * info, size_t info_len) {
wuliqunyy 0:be95bfb06686 132 scpi_error_t error_value;
wuliqunyy 0:be95bfb06686 133 /* SCPIDEFINE_strndup is sometimes a dumy that does not reference it's arguments.
wuliqunyy 0:be95bfb06686 134 Since info_len is not referenced elsewhere caoing to void prevents unusd argument warnings */
wuliqunyy 0:be95bfb06686 135 (void) info_len;
wuliqunyy 0:be95bfb06686 136 char * info_ptr = info ? SCPIDEFINE_strndup(&context->error_info_heap, info, info_len) : NULL;
wuliqunyy 0:be95bfb06686 137 SCPI_ERROR_SETVAL(&error_value, err, info_ptr);
wuliqunyy 0:be95bfb06686 138 if (!fifo_add(&context->error_queue, &error_value)) {
wuliqunyy 0:be95bfb06686 139 SCPIDEFINE_free(&context->error_info_heap, error_value.device_dependent_info, true);
wuliqunyy 0:be95bfb06686 140 fifo_remove_last(&context->error_queue, &error_value);
wuliqunyy 0:be95bfb06686 141 SCPIDEFINE_free(&context->error_info_heap, error_value.device_dependent_info, true);
wuliqunyy 0:be95bfb06686 142 SCPI_ERROR_SETVAL(&error_value, SCPI_ERROR_QUEUE_OVERFLOW, NULL);
wuliqunyy 0:be95bfb06686 143 fifo_add(&context->error_queue, &error_value);
wuliqunyy 0:be95bfb06686 144 return FALSE;
wuliqunyy 0:be95bfb06686 145 }
wuliqunyy 0:be95bfb06686 146 return TRUE;
wuliqunyy 0:be95bfb06686 147 }
wuliqunyy 0:be95bfb06686 148
wuliqunyy 0:be95bfb06686 149 struct error_reg {
wuliqunyy 0:be95bfb06686 150 int16_t from;
wuliqunyy 0:be95bfb06686 151 int16_t to;
wuliqunyy 0:be95bfb06686 152 scpi_reg_val_t esrBit;
wuliqunyy 0:be95bfb06686 153 };
wuliqunyy 0:be95bfb06686 154
wuliqunyy 0:be95bfb06686 155 #define ERROR_DEFS_N 9
wuliqunyy 0:be95bfb06686 156
wuliqunyy 0:be95bfb06686 157 static const struct error_reg errs[ERROR_DEFS_N] = {
wuliqunyy 0:be95bfb06686 158 {-100, -199, ESR_CER}, /* Command error (e.g. syntax error) ch 21.8.9 */
wuliqunyy 0:be95bfb06686 159 {-200, -299, ESR_EER}, /* Execution Error (e.g. range error) ch 21.8.10 */
wuliqunyy 0:be95bfb06686 160 {-300, -399, ESR_DER}, /* Device specific error -300, -399 ch 21.8.11 */
wuliqunyy 0:be95bfb06686 161 { 1, 32767, ESR_DER}, /* Device designer provided specific error 1, 32767 ch 21.8.11 */
wuliqunyy 0:be95bfb06686 162 {-400, -499, ESR_QER}, /* Query error -400, -499 ch 21.8.12 */
wuliqunyy 0:be95bfb06686 163 {-500, -599, ESR_PON}, /* Power on event -500, -599 ch 21.8.13 */
wuliqunyy 0:be95bfb06686 164 {-600, -699, ESR_URQ}, /* User Request Event -600, -699 ch 21.8.14 */
wuliqunyy 0:be95bfb06686 165 {-700, -799, ESR_REQ}, /* Request Control Event -700, -799 ch 21.8.15 */
wuliqunyy 0:be95bfb06686 166 {-800, -899, ESR_OPC}, /* Operation Complete Event -800, -899 ch 21.8.16 */
wuliqunyy 0:be95bfb06686 167 };
wuliqunyy 0:be95bfb06686 168
wuliqunyy 0:be95bfb06686 169 /**
wuliqunyy 0:be95bfb06686 170 * Push error to queue
wuliqunyy 0:be95bfb06686 171 * @param context
wuliqunyy 0:be95bfb06686 172 * @param err - error number
wuliqunyy 0:be95bfb06686 173 * @param info - additional text information or NULL for no text
wuliqunyy 0:be95bfb06686 174 * @param info_len - length of text or 0 for automatic length
wuliqunyy 0:be95bfb06686 175 */
wuliqunyy 0:be95bfb06686 176 void SCPI_ErrorPushEx(scpi_t * context, int16_t err, char * info, size_t info_len) {
wuliqunyy 0:be95bfb06686 177 int i;
wuliqunyy 0:be95bfb06686 178 /* automatic calculation of length */
wuliqunyy 0:be95bfb06686 179 if (info && info_len == 0) {
wuliqunyy 0:be95bfb06686 180 info_len = SCPIDEFINE_strnlen(info, SCPI_STD_ERROR_DESC_MAX_STRING_LENGTH);
wuliqunyy 0:be95bfb06686 181 }
wuliqunyy 0:be95bfb06686 182 scpi_bool_t queue_overflow = !SCPI_ErrorAddInternal(context, err, info, info_len);
wuliqunyy 0:be95bfb06686 183
wuliqunyy 0:be95bfb06686 184 for (i = 0; i < ERROR_DEFS_N; i++) {
wuliqunyy 0:be95bfb06686 185 if ((err <= errs[i].from) && (err >= errs[i].to)) {
wuliqunyy 0:be95bfb06686 186 SCPI_RegSetBits(context, SCPI_REG_ESR, errs[i].esrBit);
wuliqunyy 0:be95bfb06686 187 }
wuliqunyy 0:be95bfb06686 188 }
wuliqunyy 0:be95bfb06686 189
wuliqunyy 0:be95bfb06686 190 SCPI_ErrorEmit(context, err);
wuliqunyy 0:be95bfb06686 191 if (queue_overflow) {
wuliqunyy 0:be95bfb06686 192 SCPI_ErrorEmit(context, SCPI_ERROR_QUEUE_OVERFLOW);
wuliqunyy 0:be95bfb06686 193 }
wuliqunyy 0:be95bfb06686 194
wuliqunyy 0:be95bfb06686 195 if (context) {
wuliqunyy 0:be95bfb06686 196 context->cmd_error = TRUE;
wuliqunyy 0:be95bfb06686 197 }
wuliqunyy 0:be95bfb06686 198 }
wuliqunyy 0:be95bfb06686 199
wuliqunyy 0:be95bfb06686 200 /**
wuliqunyy 0:be95bfb06686 201 * Push error to queue
wuliqunyy 0:be95bfb06686 202 * @param context - scpi context
wuliqunyy 0:be95bfb06686 203 * @param err - error number
wuliqunyy 0:be95bfb06686 204 */
wuliqunyy 0:be95bfb06686 205 void SCPI_ErrorPush(scpi_t * context, int16_t err) {
wuliqunyy 0:be95bfb06686 206 SCPI_ErrorPushEx(context, err, NULL, 0);
wuliqunyy 0:be95bfb06686 207 return;
wuliqunyy 0:be95bfb06686 208 }
wuliqunyy 0:be95bfb06686 209
wuliqunyy 0:be95bfb06686 210 /**
wuliqunyy 0:be95bfb06686 211 * Translate error number to string
wuliqunyy 0:be95bfb06686 212 * @param err - error number
wuliqunyy 0:be95bfb06686 213 * @return Error string representation
wuliqunyy 0:be95bfb06686 214 */
wuliqunyy 0:be95bfb06686 215 const char * SCPI_ErrorTranslate(int16_t err) {
wuliqunyy 0:be95bfb06686 216 switch (err) {
wuliqunyy 0:be95bfb06686 217 #define X(def, val, str) case def: return str;
wuliqunyy 0:be95bfb06686 218 #if USE_FULL_ERROR_LIST
wuliqunyy 0:be95bfb06686 219 #define XE X
wuliqunyy 0:be95bfb06686 220 #else
wuliqunyy 0:be95bfb06686 221 #define XE(def, val, str)
wuliqunyy 0:be95bfb06686 222 #endif
wuliqunyy 0:be95bfb06686 223 LIST_OF_ERRORS
wuliqunyy 0:be95bfb06686 224
wuliqunyy 0:be95bfb06686 225 #if USE_USER_ERROR_LIST
wuliqunyy 0:be95bfb06686 226 LIST_OF_USER_ERRORS
wuliqunyy 0:be95bfb06686 227 #endif
wuliqunyy 0:be95bfb06686 228 #undef X
wuliqunyy 0:be95bfb06686 229 #undef XE
wuliqunyy 0:be95bfb06686 230 default: return "Unknown error";
wuliqunyy 0:be95bfb06686 231 }
wuliqunyy 0:be95bfb06686 232 }
wuliqunyy 0:be95bfb06686 233
wuliqunyy 0:be95bfb06686 234
wuliqunyy 0:be95bfb06686 235