Fork

Dependencies:   mbed libscpi

Committer:
bavovanachte
Date:
Thu Jun 10 12:45:11 2021 +0000
Revision:
30:b463e1f3cae3
Parent:
29:595fd1f5e63c
Child:
31:0475756cede6
First trials with command_interpreter successful

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bavovanachte 26:beaf802e2456 1 /*-
bavovanachte 26:beaf802e2456 2 * BSD 2-Clause License
bavovanachte 26:beaf802e2456 3 *
bavovanachte 26:beaf802e2456 4 * Copyright (c) 2012-2018, Jan Breuer
bavovanachte 26:beaf802e2456 5 * All rights reserved.
bavovanachte 26:beaf802e2456 6 *
bavovanachte 26:beaf802e2456 7 * Redistribution and use in source and binary forms, with or without
bavovanachte 26:beaf802e2456 8 * modification, are permitted provided that the following conditions are met:
bavovanachte 26:beaf802e2456 9 *
bavovanachte 26:beaf802e2456 10 * * Redistributions of source code must retain the above copyright notice, this
bavovanachte 26:beaf802e2456 11 * list of conditions and the following disclaimer.
bavovanachte 26:beaf802e2456 12 *
bavovanachte 26:beaf802e2456 13 * * Redistributions in binary form must reproduce the above copyright notice,
bavovanachte 26:beaf802e2456 14 * this list of conditions and the following disclaimer in the documentation
bavovanachte 26:beaf802e2456 15 * and/or other materials provided with the distribution.
bavovanachte 26:beaf802e2456 16 *
bavovanachte 26:beaf802e2456 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bavovanachte 26:beaf802e2456 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bavovanachte 26:beaf802e2456 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bavovanachte 26:beaf802e2456 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bavovanachte 26:beaf802e2456 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bavovanachte 26:beaf802e2456 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bavovanachte 26:beaf802e2456 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bavovanachte 26:beaf802e2456 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bavovanachte 26:beaf802e2456 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bavovanachte 26:beaf802e2456 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bavovanachte 26:beaf802e2456 27 */
bavovanachte 26:beaf802e2456 28
bavovanachte 26:beaf802e2456 29 /**
bavovanachte 26:beaf802e2456 30 * @file scpi-def.c
bavovanachte 26:beaf802e2456 31 * @date Thu Nov 15 10:58:45 UTC 2012
bavovanachte 26:beaf802e2456 32 *
bavovanachte 26:beaf802e2456 33 * @brief SCPI parser test
bavovanachte 26:beaf802e2456 34 *
bavovanachte 26:beaf802e2456 35 *
bavovanachte 26:beaf802e2456 36 */
bavovanachte 26:beaf802e2456 37
bavovanachte 26:beaf802e2456 38 #include <stdio.h>
bavovanachte 26:beaf802e2456 39 #include <stdlib.h>
bavovanachte 26:beaf802e2456 40 #include <string.h>
bavovanachte 26:beaf802e2456 41 #include "uart_mbed.h"
bavovanachte 28:fdc12762a42d 42 #include "main_init.h"
bavovanachte 26:beaf802e2456 43 #include "i2c_mbed_fpga.h"
bavovanachte 26:beaf802e2456 44 #include "scpi/scpi.h"
bavovanachte 26:beaf802e2456 45 #include "scpi/parser.h"
bavovanachte 26:beaf802e2456 46 #include "scpi-def.h"
bavovanachte 26:beaf802e2456 47
bavovanachte 26:beaf802e2456 48 extern i2c_mbed_fpga i2c;
bavovanachte 26:beaf802e2456 49
bavovanachte 26:beaf802e2456 50 static scpi_result_t set_duty(scpi_t * context) {
bavovanachte 26:beaf802e2456 51 int input = 0;
bavovanachte 26:beaf802e2456 52 int duty_cycle_scaled = 0;
bavovanachte 26:beaf802e2456 53
bavovanachte 26:beaf802e2456 54 /* read first parameter if present */
bavovanachte 26:beaf802e2456 55 if (!SCPI_ParamInt(context, &input, TRUE)) {
bavovanachte 26:beaf802e2456 56 return SCPI_RES_ERR;
bavovanachte 26:beaf802e2456 57 }
bavovanachte 26:beaf802e2456 58 duty_cycle_scaled = (input * 0xFFFF) / 100u;
bavovanachte 26:beaf802e2456 59 i2c.i2c_set_open_loop_duty(duty_cycle_scaled);
bavovanachte 26:beaf802e2456 60 return SCPI_RES_OK;
bavovanachte 26:beaf802e2456 61 }
bavovanachte 26:beaf802e2456 62
bavovanachte 27:9118d8c2509b 63 scpi_result_t get_duty(scpi_t* context)
bavovanachte 27:9118d8c2509b 64 {
bavovanachte 27:9118d8c2509b 65 int duty_cycle;
bavovanachte 27:9118d8c2509b 66 i2c.i2c_word_read_simple(I2C_SPEED_DUTY, &duty_cycle);
bavovanachte 27:9118d8c2509b 67 SCPI_ResultInt(context, duty_cycle);
bavovanachte 27:9118d8c2509b 68 return SCPI_RES_OK;
bavovanachte 27:9118d8c2509b 69 }
bavovanachte 27:9118d8c2509b 70
bavovanachte 28:fdc12762a42d 71
bavovanachte 28:fdc12762a42d 72 const scpi_choice_def_t led_selects[] = {
bavovanachte 28:fdc12762a42d 73 { "OFF", 0 },
bavovanachte 28:fdc12762a42d 74 { "ON", 1 },
bavovanachte 28:fdc12762a42d 75 SCPI_CHOICE_LIST_END
bavovanachte 28:fdc12762a42d 76 };
bavovanachte 28:fdc12762a42d 77 static scpi_result_t set_led1(scpi_t * context) {
bavovanachte 28:fdc12762a42d 78 int input = 0;
bavovanachte 28:fdc12762a42d 79
bavovanachte 28:fdc12762a42d 80 if (!SCPI_ParamChoice(context, led_selects, &input, TRUE))
bavovanachte 28:fdc12762a42d 81 return SCPI_RES_ERR;
bavovanachte 28:fdc12762a42d 82
bavovanachte 28:fdc12762a42d 83 led1 = input;
bavovanachte 28:fdc12762a42d 84 return SCPI_RES_OK;
bavovanachte 28:fdc12762a42d 85 }
bavovanachte 28:fdc12762a42d 86
bavovanachte 28:fdc12762a42d 87 scpi_result_t get_led1(scpi_t* context)
bavovanachte 28:fdc12762a42d 88 {
bavovanachte 30:b463e1f3cae3 89 int led_value = led1;
bavovanachte 30:b463e1f3cae3 90 SCPI_ResultText(context, led_selects[led_value].name);
bavovanachte 28:fdc12762a42d 91 return SCPI_RES_OK;
bavovanachte 28:fdc12762a42d 92 }
bavovanachte 28:fdc12762a42d 93
bavovanachte 28:fdc12762a42d 94
bavovanachte 28:fdc12762a42d 95 scpi_result_t read_word(scpi_t* context)
bavovanachte 28:fdc12762a42d 96 {
bavovanachte 28:fdc12762a42d 97 int address = 0;
bavovanachte 30:b463e1f3cae3 98 int value;
bavovanachte 28:fdc12762a42d 99
bavovanachte 28:fdc12762a42d 100 /* read first parameter if present */
bavovanachte 28:fdc12762a42d 101 if (!SCPI_ParamInt(context, &address, TRUE)) {
bavovanachte 28:fdc12762a42d 102 return SCPI_RES_ERR;
bavovanachte 28:fdc12762a42d 103 }
bavovanachte 28:fdc12762a42d 104
bavovanachte 29:595fd1f5e63c 105 if((address >= 0x1000) && (address < 0x1200)) {
bavovanachte 29:595fd1f5e63c 106 address = (address - 0x1000) / 2;
bavovanachte 30:b463e1f3cae3 107 i2c.i2c_word_read_simple(address, &value);
bavovanachte 29:595fd1f5e63c 108 } else {
bavovanachte 30:b463e1f3cae3 109 i2c.i2c_word_read_interpreter(address, &value);
bavovanachte 30:b463e1f3cae3 110 }
bavovanachte 28:fdc12762a42d 111
bavovanachte 29:595fd1f5e63c 112 SCPI_ResultIntBase(context, value, 16);
bavovanachte 28:fdc12762a42d 113 return SCPI_RES_OK;
bavovanachte 28:fdc12762a42d 114 }
bavovanachte 28:fdc12762a42d 115
bavovanachte 26:beaf802e2456 116
bavovanachte 26:beaf802e2456 117 /**
bavovanachte 26:beaf802e2456 118 * Reimplement IEEE488.2 *TST?
bavovanachte 26:beaf802e2456 119 *
bavovanachte 26:beaf802e2456 120 * Result should be 0 if everything is ok
bavovanachte 26:beaf802e2456 121 * Result should be 1 if something goes wrong
bavovanachte 26:beaf802e2456 122 *
bavovanachte 26:beaf802e2456 123 * Return SCPI_RES_OK
bavovanachte 26:beaf802e2456 124 */
bavovanachte 26:beaf802e2456 125 static scpi_result_t My_CoreTstQ(scpi_t * context) {
bavovanachte 26:beaf802e2456 126
bavovanachte 26:beaf802e2456 127 SCPI_ResultInt(context, 0);
bavovanachte 26:beaf802e2456 128
bavovanachte 26:beaf802e2456 129 return SCPI_RES_OK;
bavovanachte 26:beaf802e2456 130 }
bavovanachte 26:beaf802e2456 131
bavovanachte 26:beaf802e2456 132 const scpi_command_t scpi_commands[] = {
bavovanachte 26:beaf802e2456 133 /* IEEE Mandated Commands (SCPI std V1999.0 4.1.1) */
bavovanachte 26:beaf802e2456 134 {"*CLS", SCPI_CoreCls, 0},
bavovanachte 26:beaf802e2456 135 {"*ESE", SCPI_CoreEse, 0},
bavovanachte 26:beaf802e2456 136 {"*ESE?", SCPI_CoreEseQ, 0},
bavovanachte 26:beaf802e2456 137 {"*ESR?", SCPI_CoreEsrQ, 0},
bavovanachte 26:beaf802e2456 138 {"*IDN?", SCPI_CoreIdnQ, 0},
bavovanachte 26:beaf802e2456 139 {"*OPC", SCPI_CoreOpc, 0},
bavovanachte 26:beaf802e2456 140 {"*OPC?", SCPI_CoreOpcQ, 0},
bavovanachte 26:beaf802e2456 141 {"*RST", SCPI_CoreRst, 0},
bavovanachte 26:beaf802e2456 142 {"*SRE", SCPI_CoreSre, 0},
bavovanachte 26:beaf802e2456 143 {"*SRE?", SCPI_CoreSreQ, 0},
bavovanachte 26:beaf802e2456 144 {"*STB?", SCPI_CoreStbQ, 0},
bavovanachte 26:beaf802e2456 145 {"*TST?", My_CoreTstQ, 0},
bavovanachte 26:beaf802e2456 146 {"*WAI", SCPI_CoreWai, 0},
bavovanachte 26:beaf802e2456 147
bavovanachte 26:beaf802e2456 148 /* Required SCPI commands (SCPI std V1999.0 4.2.1) */
bavovanachte 26:beaf802e2456 149 {"SYSTem:ERRor[:NEXT]?", SCPI_SystemErrorNextQ, 0},
bavovanachte 26:beaf802e2456 150 {"SYSTem:ERRor:COUNt?", SCPI_SystemErrorCountQ, 0},
bavovanachte 26:beaf802e2456 151 {"SYSTem:VERSion?", SCPI_SystemVersionQ, 0},
bavovanachte 26:beaf802e2456 152
bavovanachte 26:beaf802e2456 153 //{"STATus:OPERation?", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 154 //{"STATus:OPERation:EVENt?", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 155 //{"STATus:OPERation:CONDition?", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 156 //{"STATus:OPERation:ENABle", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 157 //{"STATus:OPERation:ENABle?", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 158
bavovanachte 26:beaf802e2456 159 {"STATus:QUEStionable[:EVENt]?", SCPI_StatusQuestionableEventQ, 0},
bavovanachte 26:beaf802e2456 160 //{"STATus:QUEStionable:CONDition?", scpi_stub_callback, 0},
bavovanachte 26:beaf802e2456 161 {"STATus:QUEStionable:ENABle", SCPI_StatusQuestionableEnable, 0},
bavovanachte 26:beaf802e2456 162 {"STATus:QUEStionable:ENABle?", SCPI_StatusQuestionableEnableQ, 0},
bavovanachte 26:beaf802e2456 163
bavovanachte 26:beaf802e2456 164 {"STATus:PRESet", SCPI_StatusPreset, 0},
bavovanachte 26:beaf802e2456 165
bavovanachte 26:beaf802e2456 166 /* DMM */
bavovanachte 26:beaf802e2456 167 {"TARGet:DUTYcycle", set_duty, 0},
bavovanachte 27:9118d8c2509b 168 {"TARGet:DUTYcycle?", get_duty, 0},
bavovanachte 28:fdc12762a42d 169 {"LED1", set_led1, 0},
bavovanachte 28:fdc12762a42d 170 {"LED1?", get_led1, 0},
bavovanachte 28:fdc12762a42d 171 {"MEMory:WORD?", read_word, 0},
bavovanachte 28:fdc12762a42d 172
bavovanachte 26:beaf802e2456 173
bavovanachte 26:beaf802e2456 174 SCPI_CMD_LIST_END
bavovanachte 26:beaf802e2456 175 };
bavovanachte 26:beaf802e2456 176
bavovanachte 26:beaf802e2456 177 scpi_interface_t scpi_interface = {
bavovanachte 26:beaf802e2456 178 /*.error = */ SCPI_Error,
bavovanachte 26:beaf802e2456 179 /*.write = */ SCPI_Write,
bavovanachte 26:beaf802e2456 180 /*.control = */ SCPI_Control,
bavovanachte 26:beaf802e2456 181 /*.flush = */ SCPI_Flush,
bavovanachte 26:beaf802e2456 182 /*.reset = */ SCPI_Reset,
bavovanachte 26:beaf802e2456 183 };
bavovanachte 26:beaf802e2456 184
bavovanachte 26:beaf802e2456 185 char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH];
bavovanachte 26:beaf802e2456 186 int scpi_error_queue_data[SCPI_ERROR_QUEUE_SIZE];
bavovanachte 26:beaf802e2456 187 static scpi_reg_val_t scpi_regs[SCPI_REG_COUNT];
bavovanachte 26:beaf802e2456 188
bavovanachte 26:beaf802e2456 189 scpi_t scpi_context = {
bavovanachte 26:beaf802e2456 190 cmdlist : scpi_commands,
bavovanachte 26:beaf802e2456 191 buffer : {
bavovanachte 26:beaf802e2456 192 length : SCPI_INPUT_BUFFER_LENGTH,
bavovanachte 26:beaf802e2456 193 position : 0,
bavovanachte 26:beaf802e2456 194 data : scpi_input_buffer
bavovanachte 26:beaf802e2456 195 },
bavovanachte 26:beaf802e2456 196 param_list : { 0 },
bavovanachte 26:beaf802e2456 197 interface : &scpi_interface,
bavovanachte 26:beaf802e2456 198 output_count : 0,
bavovanachte 26:beaf802e2456 199 input_count : 0,
bavovanachte 26:beaf802e2456 200 cmd_error : false,
bavovanachte 26:beaf802e2456 201 error_queue : NULL,
bavovanachte 26:beaf802e2456 202 registers : scpi_regs,
bavovanachte 26:beaf802e2456 203 units : scpi_units_def,
bavovanachte 26:beaf802e2456 204 user_context : NULL,
bavovanachte 26:beaf802e2456 205 parser_state : {
bavovanachte 26:beaf802e2456 206 programHeader : { SCPI_TOKEN_UNKNOWN, NULL, 0 },
bavovanachte 26:beaf802e2456 207 programData : { SCPI_TOKEN_UNKNOWN, NULL, 0 },
bavovanachte 26:beaf802e2456 208 numberOfParameters : 0,
bavovanachte 26:beaf802e2456 209 termination : SCPI_MESSAGE_TERMINATION_NONE
bavovanachte 26:beaf802e2456 210 },
bavovanachte 26:beaf802e2456 211 idn : {"semtech", "na-mote", NULL, "01-02"}
bavovanachte 26:beaf802e2456 212 };
bavovanachte 26:beaf802e2456 213
bavovanachte 26:beaf802e2456 214