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
examples/drvdiag/main.cpp@13:66c8e4ce4ff1, 2016-04-29 (annotated)
- Committer:
- Adrian Suciu
- Date:
- Fri Apr 29 16:19:00 2016 +0300
- Revision:
- 13:66c8e4ce4ff1
- Parent:
- 8:6e96c6b3c83f
- Child:
- 17:b8356808e8ad
Added driver for AD7791 and example for CN0216
Who changed what in which revision?
User | Revision | Line number | New 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 |
13:66c8e4ce4ff1 | 86 | |
Adrian Suciu |
13:66c8e4ce4ff1 | 87 | #ifdef AD7791_PRESENT |
Adrian Suciu |
13:66c8e4ce4ff1 | 88 | {"adrst", 0, [](){ad7791diag.reset();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 89 | {"adwrm", 1, [](){ad7791diag.write_mode();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 90 | {"adrdm", 0, [](){ad7791diag.read_mode();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 91 | {"adwrf", 1, [](){ad7791diag.write_filter();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 92 | {"adrdf", 0, [](){ad7791diag.read_filter();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 93 | {"adrdd", 0, [](){ad7791diag.read_data();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 94 | {"adrds", 0, [](){ad7791diag.read_status();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 95 | {"adread32", 0, [](){ad7791diag.read();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 96 | {"adread", 0, [](){ad7791diag.read_u16();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 97 | {"adreadv", 0, [](){ad7791diag.read_voltage();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 98 | {"adsetc", 1, [](){ad7791diag.set_continous_mode();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 99 | {"adsetref", 1, [](){ad7791diag.set_reference_voltage();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 100 | {"adsetch", 1, [](){ad7791diag.set_channel();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 101 | #endif |
Adrian Suciu |
13:66c8e4ce4ff1 | 102 | |
Adrian Suciu |
13:66c8e4ce4ff1 | 103 | #ifdef CN0216_PRESENT |
Adrian Suciu |
13:66c8e4ce4ff1 | 104 | {"cninit" , 1, [](){cn0216diag.init();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 105 | {"cncal" , 1, [](){cn0216diag.calibrate();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 106 | {"cnrdw" , 0, [](){cn0216diag.read_weight();}}, |
Adrian Suciu |
13:66c8e4ce4ff1 | 107 | #endif |
Adrian Suciu |
7:b62398a1d17a | 108 | |
Adrian Suciu |
7:b62398a1d17a | 109 | #ifdef AD7790_PRESENT |
Adrian Suciu |
7:b62398a1d17a | 110 | {"adrst", 0, [](){ad7790diag.reset();}}, |
Adrian Suciu |
7:b62398a1d17a | 111 | {"adwrm", 1, [](){ad7790diag.write_mode();}}, |
Adrian Suciu |
7:b62398a1d17a | 112 | {"adrdm", 0, [](){ad7790diag.read_mode();}}, |
Adrian Suciu |
7:b62398a1d17a | 113 | {"adwrf", 1, [](){ad7790diag.write_filter();}}, |
Adrian Suciu |
7:b62398a1d17a | 114 | {"adrdf", 0, [](){ad7790diag.read_filter();}}, |
Adrian Suciu |
7:b62398a1d17a | 115 | {"adrdd", 0, [](){ad7790diag.read_data();}}, |
Adrian Suciu |
7:b62398a1d17a | 116 | {"adrds", 0, [](){ad7790diag.read_status();}}, |
Adrian Suciu |
7:b62398a1d17a | 117 | {"adread", 0, [](){ad7790diag.read_u16();}}, |
Adrian Suciu |
7:b62398a1d17a | 118 | {"adreadv", 0, [](){ad7790diag.read_voltage();}}, |
Adrian Suciu |
7:b62398a1d17a | 119 | {"adsetc", 1, [](){ad7790diag.set_continous_mode();}}, |
Adrian Suciu |
7:b62398a1d17a | 120 | {"adsetref", 1, [](){ad7790diag.set_reference_voltage();}}, |
Adrian Suciu |
7:b62398a1d17a | 121 | {"adsetch", 1, [](){ad7790diag.set_channel();}}, |
Adrian Suciu |
7:b62398a1d17a | 122 | #endif |
Adrian Suciu |
7:b62398a1d17a | 123 | |
Adrian Suciu |
7:b62398a1d17a | 124 | #ifdef AD5270_PRESENT |
Adrian Suciu |
7:b62398a1d17a | 125 | {"rdwrr" , 1, [](){ad5270diag.write_RDAC();}}, |
Adrian Suciu |
7:b62398a1d17a | 126 | {"rdrdr" , 0, [](){ad5270diag.read_RDAC();}}, |
Adrian Suciu |
7:b62398a1d17a | 127 | {"rdwrcmd" , 2, [](){ad5270diag.write_cmd();}}, |
Adrian Suciu |
7:b62398a1d17a | 128 | {"rdsetz" , 0, [](){ad5270diag.set_HiZ();}}, |
Adrian Suciu |
7:b62398a1d17a | 129 | {"rd50en", 0, [](){ad5270diag.enable_50TP_programming();}}, |
Adrian Suciu |
7:b62398a1d17a | 130 | {"rd50ds", 0, [](){ad5270diag.disable_50TP_programming();}}, |
Adrian Suciu |
7:b62398a1d17a | 131 | {"rd50st", 0, [](){ad5270diag.store_50TP();}}, |
Adrian Suciu |
7:b62398a1d17a | 132 | {"rd50a" , 0, [](){ad5270diag.read_50TP_last_address();}}, |
Adrian Suciu |
7:b62398a1d17a | 133 | {"rd50m" , 1, [](){ad5270diag.read_50TP_memory();}}, |
Adrian Suciu |
7:b62398a1d17a | 134 | {"rdwrc" , 1, [](){ad5270diag.write_ctrl_reg();}}, |
Adrian Suciu |
7:b62398a1d17a | 135 | {"rdrdc" , 0, [](){ad5270diag.read_ctrl_reg();}}, |
Adrian Suciu |
7:b62398a1d17a | 136 | {"rdrst" , 0, [](){ad5270diag.reset_RDAC();}}, |
Adrian Suciu |
7:b62398a1d17a | 137 | {"rdchm" , 1, [](){ad5270diag.change_mode();}}, |
Adrian Suciu |
7:b62398a1d17a | 138 | {"rdwrw" , 1, [](){ad5270diag.write_wiper_reg();}}, |
Adrian Suciu |
7:b62398a1d17a | 139 | {"rdrdw" , 0, [](){ad5270diag.read_wiper_reg();}}, |
Adrian Suciu |
7:b62398a1d17a | 140 | #endif |
Adrian Suciu |
7:b62398a1d17a | 141 | |
Adrian Suciu |
7:b62398a1d17a | 142 | #ifdef CN0357_PRESENT |
Adrian Suciu |
7:b62398a1d17a | 143 | {"cnwrdac" , 1, [](){cn0357diag.set_RDAC();}}, |
Adrian Suciu |
7:b62398a1d17a | 144 | {"cnrppm" , 0, [](){cn0357diag.read_ppm();}}, |
Adrian Suciu |
7:b62398a1d17a | 145 | {"cnparam" , 2, [](){cn0357diag.set_sensor_param();}} |
Adrian Suciu |
7:b62398a1d17a | 146 | #endif |
Adrian Suciu |
7:b62398a1d17a | 147 | |
Adrian Suciu |
7:b62398a1d17a | 148 | }; |
Adrian Suciu |
7:b62398a1d17a | 149 | // *INDENT-ON* |
Adrian Suciu |
7:b62398a1d17a | 150 | |
Adrian Suciu |
7:b62398a1d17a | 151 | |
Adrian Suciu |
7:b62398a1d17a | 152 | |
Adrian Suciu |
7:b62398a1d17a | 153 | void read_from_console() |
Adrian Suciu |
7:b62398a1d17a | 154 | { |
Adrian Suciu |
7:b62398a1d17a | 155 | char buffer[100] = {0}; |
Adrian Suciu |
7:b62398a1d17a | 156 | size_t readPosition = 0; |
Adrian Suciu |
7:b62398a1d17a | 157 | |
Adrian Suciu |
7:b62398a1d17a | 158 | // read from console until newline |
Adrian Suciu |
7:b62398a1d17a | 159 | while(1) { |
Adrian Suciu |
7:b62398a1d17a | 160 | buffer[readPosition] = pc.getc(); |
Adrian Suciu |
7:b62398a1d17a | 161 | if(buffer[readPosition] == '\n' || buffer[readPosition] == '\r') { |
Adrian Suciu |
7:b62398a1d17a | 162 | buffer[readPosition] = ' '; |
Adrian Suciu |
7:b62398a1d17a | 163 | break; |
Adrian Suciu |
7:b62398a1d17a | 164 | } |
Adrian Suciu |
7:b62398a1d17a | 165 | readPosition++; |
Adrian Suciu |
7:b62398a1d17a | 166 | } |
Adrian Suciu |
7:b62398a1d17a | 167 | readPosition++; |
Adrian Suciu |
7:b62398a1d17a | 168 | buffer[readPosition] = '\0'; |
Adrian Suciu |
7:b62398a1d17a | 169 | //create std::string from char buffer |
Adrian Suciu |
7:b62398a1d17a | 170 | string s(buffer); |
Adrian Suciu |
7:b62398a1d17a | 171 | |
Adrian Suciu |
7:b62398a1d17a | 172 | // create std::vector of std:string, each string contains parameter |
Adrian Suciu |
7:b62398a1d17a | 173 | size_t pos = 0; |
Adrian Suciu |
7:b62398a1d17a | 174 | string delimiter = " "; |
Adrian Suciu |
7:b62398a1d17a | 175 | string token; |
Adrian Suciu |
7:b62398a1d17a | 176 | while ((pos = s.find(delimiter)) != std::string::npos) { |
Adrian Suciu |
7:b62398a1d17a | 177 | token = s.substr(0, pos); |
Adrian Suciu |
7:b62398a1d17a | 178 | cmdbuffer.push_back(token); |
Adrian Suciu |
7:b62398a1d17a | 179 | s.erase(0, pos + delimiter.length()); |
Adrian Suciu |
7:b62398a1d17a | 180 | } |
Adrian Suciu |
7:b62398a1d17a | 181 | |
Adrian Suciu |
7:b62398a1d17a | 182 | } |
Adrian Suciu |
7:b62398a1d17a | 183 | |
Adrian Suciu |
7:b62398a1d17a | 184 | void run_command() |
Adrian Suciu |
7:b62398a1d17a | 185 | { |
Adrian Suciu |
7:b62398a1d17a | 186 | for(auto i : cmdlist) { |
Adrian Suciu |
7:b62398a1d17a | 187 | if(i.cmd_str == cmdbuffer[0]) { |
Adrian Suciu |
7:b62398a1d17a | 188 | if(static_cast<int>(cmdbuffer.size()) - 1 == i.nr_of_param || i.nr_of_param == commands::VAR) { |
Adrian Suciu |
7:b62398a1d17a | 189 | pc.printf("RX> "); |
Adrian Suciu |
7:b62398a1d17a | 190 | i.fktPtr(); |
Adrian Suciu |
7:b62398a1d17a | 191 | } else { |
Adrian Suciu |
7:b62398a1d17a | 192 | pc.printf("RX> "); |
Adrian Suciu |
7:b62398a1d17a | 193 | 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 | 194 | } |
Adrian Suciu |
7:b62398a1d17a | 195 | return; |
Adrian Suciu |
7:b62398a1d17a | 196 | } |
Adrian Suciu |
7:b62398a1d17a | 197 | } |
Adrian Suciu |
7:b62398a1d17a | 198 | pc.printf("RX> "); |
Adrian Suciu |
7:b62398a1d17a | 199 | pc.printf("Command %s not found", cmdbuffer[0].c_str()); |
Adrian Suciu |
7:b62398a1d17a | 200 | |
Adrian Suciu |
7:b62398a1d17a | 201 | } |
Adrian Suciu |
7:b62398a1d17a | 202 | |
Adrian Suciu |
7:b62398a1d17a | 203 | int main() |
Adrian Suciu |
7:b62398a1d17a | 204 | |
Adrian Suciu |
13:66c8e4ce4ff1 | 205 | { |
Adrian Suciu |
13:66c8e4ce4ff1 | 206 | ad7791.frequency(100000); |
Adrian Suciu |
7:b62398a1d17a | 207 | pc.printf("\r\n#### DrvDiag ####\r\n"); |
Adrian Suciu |
13:66c8e4ce4ff1 | 208 | |
Adrian Suciu |
7:b62398a1d17a | 209 | while(1) { |
Adrian Suciu |
7:b62398a1d17a | 210 | pc.printf("\r\nTX> "); |
Adrian Suciu |
7:b62398a1d17a | 211 | read_from_console(); |
Adrian Suciu |
7:b62398a1d17a | 212 | run_command(); |
Adrian Suciu |
7:b62398a1d17a | 213 | cmdbuffer.clear(); |
Adrian Suciu |
7:b62398a1d17a | 214 | } |
Adrian Suciu |
7:b62398a1d17a | 215 | |
Adrian Suciu |
7:b62398a1d17a | 216 | } |
Adrian Suciu |
7:b62398a1d17a | 217 |