A collection of Analog Devices drivers for the mbed platform

For additional information check out the mbed page of the Analog Devices wiki: https://wiki.analog.com/resources/tools-software/mbed-drivers-all

Committer:
Adrian Suciu
Date:
Wed Apr 20 17:37:32 2016 +0300
Revision:
8:6e96c6b3c83f
Parent:
7:b62398a1d17a
Child:
13:66c8e4ce4ff1
Added license

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Adrian Suciu 8:6e96c6b3c83f 1 /**
Adrian Suciu 8:6e96c6b3c83f 2 * @file main.cpp
Adrian Suciu 8:6e96c6b3c83f 3 * @brief Main file of the driver diag tool
Adrian Suciu 8:6e96c6b3c83f 4 * @author Analog Devices Inc.
Adrian Suciu 8:6e96c6b3c83f 5 *
Adrian Suciu 8:6e96c6b3c83f 6 * For support please go to:
Adrian Suciu 8:6e96c6b3c83f 7 * Github: https://github.com/analogdevicesinc/mbed-adi
Adrian Suciu 8:6e96c6b3c83f 8 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
Adrian Suciu 8:6e96c6b3c83f 9 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
Adrian Suciu 8:6e96c6b3c83f 10
Adrian Suciu 8:6e96c6b3c83f 11 ********************************************************************************
Adrian Suciu 8:6e96c6b3c83f 12 * Copyright 2016(c) Analog Devices, Inc.
Adrian Suciu 8:6e96c6b3c83f 13 *
Adrian Suciu 8:6e96c6b3c83f 14 * All rights reserved.
Adrian Suciu 8:6e96c6b3c83f 15 *
Adrian Suciu 8:6e96c6b3c83f 16 * Redistribution and use in source and binary forms, with or without
Adrian Suciu 8:6e96c6b3c83f 17 * modification, are permitted provided that the following conditions are met:
Adrian Suciu 8:6e96c6b3c83f 18 * - Redistributions of source code must retain the above copyright
Adrian Suciu 8:6e96c6b3c83f 19 * notice, this list of conditions and the following disclaimer.
Adrian Suciu 8:6e96c6b3c83f 20 * - Redistributions in binary form must reproduce the above copyright
Adrian Suciu 8:6e96c6b3c83f 21 * notice, this list of conditions and the following disclaimer in
Adrian Suciu 8:6e96c6b3c83f 22 * the documentation and/or other materials provided with the
Adrian Suciu 8:6e96c6b3c83f 23 * distribution.
Adrian Suciu 8:6e96c6b3c83f 24 * - Neither the name of Analog Devices, Inc. nor the names of its
Adrian Suciu 8:6e96c6b3c83f 25 * contributors may be used to endorse or promote products derived
Adrian Suciu 8:6e96c6b3c83f 26 * from this software without specific prior written permission.
Adrian Suciu 8:6e96c6b3c83f 27 * - The use of this software may or may not infringe the patent rights
Adrian Suciu 8:6e96c6b3c83f 28 * of one or more patent holders. This license does not release you
Adrian Suciu 8:6e96c6b3c83f 29 * from the requirement that you obtain separate licenses from these
Adrian Suciu 8:6e96c6b3c83f 30 * patent holders to use this software.
Adrian Suciu 8:6e96c6b3c83f 31 * - Use of the software either in source or binary form, must be run
Adrian Suciu 8:6e96c6b3c83f 32 * on or directly connected to an Analog Devices Inc. component.
Adrian Suciu 8:6e96c6b3c83f 33 *
Adrian Suciu 8:6e96c6b3c83f 34 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
Adrian Suciu 8:6e96c6b3c83f 35 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
Adrian Suciu 8:6e96c6b3c83f 36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Adrian Suciu 8:6e96c6b3c83f 37 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
Adrian Suciu 8:6e96c6b3c83f 38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Adrian Suciu 8:6e96c6b3c83f 39 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
Adrian Suciu 8:6e96c6b3c83f 40 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Adrian Suciu 8:6e96c6b3c83f 41 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Adrian Suciu 8:6e96c6b3c83f 42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Adrian Suciu 8:6e96c6b3c83f 43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Adrian Suciu 8:6e96c6b3c83f 44 *
Adrian Suciu 8:6e96c6b3c83f 45 ********************************************************************************/
Adrian Suciu 8:6e96c6b3c83f 46
Adrian Suciu 7:b62398a1d17a 47 #include "mbed.h"
Adrian Suciu 7:b62398a1d17a 48 #include "stdio.h"
Adrian Suciu 7:b62398a1d17a 49 #include <iostream>
Adrian Suciu 7:b62398a1d17a 50 #include <string>
Adrian Suciu 7:b62398a1d17a 51 #include <vector>
Adrian Suciu 7:b62398a1d17a 52
Adrian Suciu 7:b62398a1d17a 53 #include "config.h"
Adrian Suciu 7:b62398a1d17a 54
Adrian Suciu 7:b62398a1d17a 55 Serial pc(SERIAL_TX, SERIAL_RX);
Adrian Suciu 7:b62398a1d17a 56 vector<string> cmdbuffer;
Adrian Suciu 7:b62398a1d17a 57
Adrian Suciu 7:b62398a1d17a 58 class commands
Adrian Suciu 7:b62398a1d17a 59 {
Adrian Suciu 7:b62398a1d17a 60 public:
Adrian Suciu 7:b62398a1d17a 61 commands(string str, int p, void (*h)() ) : cmd_str(str), nr_of_param(p), fktPtr(h) { }
Adrian Suciu 7:b62398a1d17a 62 const static int VAR = -1; // variable number of params
Adrian Suciu 7:b62398a1d17a 63 string cmd_str;
Adrian Suciu 7:b62398a1d17a 64 int nr_of_param;
Adrian Suciu 7:b62398a1d17a 65 void (*fktPtr)(void);
Adrian Suciu 7:b62398a1d17a 66 };
Adrian Suciu 7:b62398a1d17a 67
Adrian Suciu 7:b62398a1d17a 68 // *INDENT-OFF*
Adrian Suciu 8:6e96c6b3c83f 69 const vector<commands> cmdlist = {
Adrian Suciu 7:b62398a1d17a 70 {"nop" , 0, [](){ }},
Adrian Suciu 7:b62398a1d17a 71 {"echo",commands::VAR, [](){ for(auto i = begin(cmdbuffer) + 1, e = end(cmdbuffer); i!=e; ++i)
Adrian Suciu 7:b62398a1d17a 72 printf("%s ", i->c_str()); printf("\r\n");}},
Adrian Suciu 7:b62398a1d17a 73
Adrian Suciu 7:b62398a1d17a 74 {"help", 0 , [](){ for(auto command : cmdlist)
Adrian Suciu 7:b62398a1d17a 75 pc.printf("%s ",command.cmd_str.c_str()); }},
Adrian Suciu 7:b62398a1d17a 76 /* #### SPI ####*/
Adrian Suciu 7:b62398a1d17a 77 #ifdef SPI_LOW_LEVEL
Adrian Suciu 7:b62398a1d17a 78 {"csa", 0, [](){ spibus.format(8, 3);CSA_pin = !CSA_pin; wait_us(2);
Adrian Suciu 7:b62398a1d17a 79 pc.printf("CS ADC pin set %s", ((CSA_pin.read()) ? "high" : "low") ); }},
Adrian Suciu 7:b62398a1d17a 80 {"csr", 0, [](){ spibus.format(8, 1);CSR_pin = !CSR_pin; wait_us(2);
Adrian Suciu 7:b62398a1d17a 81 pc.printf("CS RDAC pin set %s", ((CSR_pin.read()) ? "high" : "low") );} },
Adrian Suciu 7:b62398a1d17a 82 {"spi", 1, [](){ uint8_t spibyte = strtol(cmdbuffer[1].c_str(), NULL, 16);
Adrian Suciu 7:b62398a1d17a 83 pc.printf("writing 0x%x to SPI", spibyte);
Adrian Suciu 7:b62398a1d17a 84 pc.printf("\r\nreturned: 0x%x ", spibus.write(spibyte)); }},
Adrian Suciu 7:b62398a1d17a 85 #endif
Adrian Suciu 7:b62398a1d17a 86 /* #### AD7790 #### */
Adrian Suciu 7:b62398a1d17a 87
Adrian Suciu 7:b62398a1d17a 88 #ifdef AD7790_PRESENT
Adrian Suciu 7:b62398a1d17a 89 {"adrst", 0, [](){ad7790diag.reset();}},
Adrian Suciu 7:b62398a1d17a 90 {"adwrm", 1, [](){ad7790diag.write_mode();}},
Adrian Suciu 7:b62398a1d17a 91 {"adrdm", 0, [](){ad7790diag.read_mode();}},
Adrian Suciu 7:b62398a1d17a 92 {"adwrf", 1, [](){ad7790diag.write_filter();}},
Adrian Suciu 7:b62398a1d17a 93 {"adrdf", 0, [](){ad7790diag.read_filter();}},
Adrian Suciu 7:b62398a1d17a 94 {"adrdd", 0, [](){ad7790diag.read_data();}},
Adrian Suciu 7:b62398a1d17a 95 {"adrds", 0, [](){ad7790diag.read_status();}},
Adrian Suciu 7:b62398a1d17a 96 {"adread", 0, [](){ad7790diag.read_u16();}},
Adrian Suciu 7:b62398a1d17a 97 {"adreadv", 0, [](){ad7790diag.read_voltage();}},
Adrian Suciu 7:b62398a1d17a 98 {"adsetc", 1, [](){ad7790diag.set_continous_mode();}},
Adrian Suciu 7:b62398a1d17a 99 {"adsetref", 1, [](){ad7790diag.set_reference_voltage();}},
Adrian Suciu 7:b62398a1d17a 100 {"adsetch", 1, [](){ad7790diag.set_channel();}},
Adrian Suciu 7:b62398a1d17a 101 #endif
Adrian Suciu 7:b62398a1d17a 102
Adrian Suciu 7:b62398a1d17a 103 #ifdef AD5270_PRESENT
Adrian Suciu 7:b62398a1d17a 104 {"rdwrr" , 1, [](){ad5270diag.write_RDAC();}},
Adrian Suciu 7:b62398a1d17a 105 {"rdrdr" , 0, [](){ad5270diag.read_RDAC();}},
Adrian Suciu 7:b62398a1d17a 106 {"rdwrcmd" , 2, [](){ad5270diag.write_cmd();}},
Adrian Suciu 7:b62398a1d17a 107 {"rdsetz" , 0, [](){ad5270diag.set_HiZ();}},
Adrian Suciu 7:b62398a1d17a 108 {"rd50en", 0, [](){ad5270diag.enable_50TP_programming();}},
Adrian Suciu 7:b62398a1d17a 109 {"rd50ds", 0, [](){ad5270diag.disable_50TP_programming();}},
Adrian Suciu 7:b62398a1d17a 110 {"rd50st", 0, [](){ad5270diag.store_50TP();}},
Adrian Suciu 7:b62398a1d17a 111 {"rd50a" , 0, [](){ad5270diag.read_50TP_last_address();}},
Adrian Suciu 7:b62398a1d17a 112 {"rd50m" , 1, [](){ad5270diag.read_50TP_memory();}},
Adrian Suciu 7:b62398a1d17a 113 {"rdwrc" , 1, [](){ad5270diag.write_ctrl_reg();}},
Adrian Suciu 7:b62398a1d17a 114 {"rdrdc" , 0, [](){ad5270diag.read_ctrl_reg();}},
Adrian Suciu 7:b62398a1d17a 115 {"rdrst" , 0, [](){ad5270diag.reset_RDAC();}},
Adrian Suciu 7:b62398a1d17a 116 {"rdchm" , 1, [](){ad5270diag.change_mode();}},
Adrian Suciu 7:b62398a1d17a 117 {"rdwrw" , 1, [](){ad5270diag.write_wiper_reg();}},
Adrian Suciu 7:b62398a1d17a 118 {"rdrdw" , 0, [](){ad5270diag.read_wiper_reg();}},
Adrian Suciu 7:b62398a1d17a 119 #endif
Adrian Suciu 7:b62398a1d17a 120
Adrian Suciu 7:b62398a1d17a 121 #ifdef CN0357_PRESENT
Adrian Suciu 7:b62398a1d17a 122 {"cnwrdac" , 1, [](){cn0357diag.set_RDAC();}},
Adrian Suciu 7:b62398a1d17a 123 {"cnrppm" , 0, [](){cn0357diag.read_ppm();}},
Adrian Suciu 7:b62398a1d17a 124 {"cnparam" , 2, [](){cn0357diag.set_sensor_param();}}
Adrian Suciu 7:b62398a1d17a 125 #endif
Adrian Suciu 7:b62398a1d17a 126
Adrian Suciu 7:b62398a1d17a 127 };
Adrian Suciu 7:b62398a1d17a 128 // *INDENT-ON*
Adrian Suciu 7:b62398a1d17a 129
Adrian Suciu 7:b62398a1d17a 130
Adrian Suciu 7:b62398a1d17a 131
Adrian Suciu 7:b62398a1d17a 132 void read_from_console()
Adrian Suciu 7:b62398a1d17a 133 {
Adrian Suciu 7:b62398a1d17a 134 char buffer[100] = {0};
Adrian Suciu 7:b62398a1d17a 135 size_t readPosition = 0;
Adrian Suciu 7:b62398a1d17a 136
Adrian Suciu 7:b62398a1d17a 137 // read from console until newline
Adrian Suciu 7:b62398a1d17a 138 while(1) {
Adrian Suciu 7:b62398a1d17a 139 buffer[readPosition] = pc.getc();
Adrian Suciu 7:b62398a1d17a 140 if(buffer[readPosition] == '\n' || buffer[readPosition] == '\r') {
Adrian Suciu 7:b62398a1d17a 141 buffer[readPosition] = ' ';
Adrian Suciu 7:b62398a1d17a 142 break;
Adrian Suciu 7:b62398a1d17a 143 }
Adrian Suciu 7:b62398a1d17a 144 readPosition++;
Adrian Suciu 7:b62398a1d17a 145 }
Adrian Suciu 7:b62398a1d17a 146 readPosition++;
Adrian Suciu 7:b62398a1d17a 147 buffer[readPosition] = '\0';
Adrian Suciu 7:b62398a1d17a 148 //create std::string from char buffer
Adrian Suciu 7:b62398a1d17a 149 string s(buffer);
Adrian Suciu 7:b62398a1d17a 150
Adrian Suciu 7:b62398a1d17a 151 // create std::vector of std:string, each string contains parameter
Adrian Suciu 7:b62398a1d17a 152 size_t pos = 0;
Adrian Suciu 7:b62398a1d17a 153 string delimiter = " ";
Adrian Suciu 7:b62398a1d17a 154 string token;
Adrian Suciu 7:b62398a1d17a 155 while ((pos = s.find(delimiter)) != std::string::npos) {
Adrian Suciu 7:b62398a1d17a 156 token = s.substr(0, pos);
Adrian Suciu 7:b62398a1d17a 157 cmdbuffer.push_back(token);
Adrian Suciu 7:b62398a1d17a 158 s.erase(0, pos + delimiter.length());
Adrian Suciu 7:b62398a1d17a 159 }
Adrian Suciu 7:b62398a1d17a 160
Adrian Suciu 7:b62398a1d17a 161 }
Adrian Suciu 7:b62398a1d17a 162
Adrian Suciu 7:b62398a1d17a 163 void run_command()
Adrian Suciu 7:b62398a1d17a 164 {
Adrian Suciu 7:b62398a1d17a 165 for(auto i : cmdlist) {
Adrian Suciu 7:b62398a1d17a 166 if(i.cmd_str == cmdbuffer[0]) {
Adrian Suciu 7:b62398a1d17a 167 if(static_cast<int>(cmdbuffer.size()) - 1 == i.nr_of_param || i.nr_of_param == commands::VAR) {
Adrian Suciu 7:b62398a1d17a 168 pc.printf("RX> ");
Adrian Suciu 7:b62398a1d17a 169 i.fktPtr();
Adrian Suciu 7:b62398a1d17a 170 } else {
Adrian Suciu 7:b62398a1d17a 171 pc.printf("RX> ");
Adrian Suciu 7:b62398a1d17a 172 pc.printf("Command %s expects %d parameters, found %d", i.cmd_str.c_str(), i.nr_of_param, cmdbuffer.size() - 1);
Adrian Suciu 7:b62398a1d17a 173 }
Adrian Suciu 7:b62398a1d17a 174 return;
Adrian Suciu 7:b62398a1d17a 175 }
Adrian Suciu 7:b62398a1d17a 176 }
Adrian Suciu 7:b62398a1d17a 177 pc.printf("RX> ");
Adrian Suciu 7:b62398a1d17a 178 pc.printf("Command %s not found", cmdbuffer[0].c_str());
Adrian Suciu 7:b62398a1d17a 179
Adrian Suciu 7:b62398a1d17a 180 }
Adrian Suciu 7:b62398a1d17a 181
Adrian Suciu 7:b62398a1d17a 182 int main()
Adrian Suciu 7:b62398a1d17a 183 {
Adrian Suciu 7:b62398a1d17a 184
Adrian Suciu 7:b62398a1d17a 185 pc.printf("\r\n#### DrvDiag ####\r\n");
Adrian Suciu 7:b62398a1d17a 186 while(1) {
Adrian Suciu 7:b62398a1d17a 187 pc.printf("\r\nTX> ");
Adrian Suciu 7:b62398a1d17a 188 read_from_console();
Adrian Suciu 7:b62398a1d17a 189 run_command();
Adrian Suciu 7:b62398a1d17a 190 cmdbuffer.clear();
Adrian Suciu 7:b62398a1d17a 191 }
Adrian Suciu 7:b62398a1d17a 192
Adrian Suciu 7:b62398a1d17a 193 }
Adrian Suciu 7:b62398a1d17a 194