Example host software for the Maxim Integrated MAX11131 SPI 12-bit 16-channel ADC with SampleSet. Hosted on the MAX32625MBED.

Dependencies:   MAX11131 CmdLine USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hello_MAX11131.cpp Source File

Hello_MAX11131.cpp

00001 // /*******************************************************************************
00002 // * Copyright (C) 2019 Maxim Integrated Products, Inc., All Rights Reserved.
00003 // *
00004 // * Permission is hereby granted, free of charge, to any person obtaining a
00005 // * copy of this software and associated documentation files (the "Software"),
00006 // * to deal in the Software without restriction, including without limitation
00007 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 // * and/or sell copies of the Software, and to permit persons to whom the
00009 // * Software is furnished to do so, subject to the following conditions:
00010 // *
00011 // * The above copyright notice and this permission notice shall be included
00012 // * in all copies or substantial portions of the Software.
00013 // *
00014 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 // * OTHER DEALINGS IN THE SOFTWARE.
00021 // *
00022 // * Except as contained in this notice, the name of Maxim Integrated
00023 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 // * Products, Inc. Branding Policy.
00025 // *
00026 // * The mere transfer of this software does not imply any licenses
00027 // * of trade secrets, proprietary technology, copyrights, patents,
00028 // * trademarks, maskwork rights, or any other form of intellectual
00029 // * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 // * ownership rights.
00031 // *******************************************************************************
00032 // */
00033 // example code includes
00034 // standard include for target platform -- Platform_Include_Boilerplate
00035 #include "mbed.h"
00036 // Platforms:
00037 //   - MAX32625MBED
00038 //      - supports mbed-os-5.11, requires USBDevice library
00039 //      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
00040 //      - remove max32630fthr library (if present)
00041 //      - remove MAX32620FTHR library (if present)
00042 //   - MAX32600MBED
00043 //      - remove max32630fthr library (if present)
00044 //      - remove MAX32620FTHR library (if present)
00045 //      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
00046 //   - NUCLEO_F446RE
00047 //      - remove USBDevice library
00048 //      - remove max32630fthr library (if present)
00049 //      - remove MAX32620FTHR library (if present)
00050 //   - NUCLEO_F401RE
00051 //      - remove USBDevice library
00052 //      - remove max32630fthr library (if present)
00053 //      - remove MAX32620FTHR library (if present)
00054 //   - MAX32630FTHR
00055 //      - #include "max32630fthr.h"
00056 //      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
00057 //      - remove MAX32620FTHR library (if present)
00058 //   - MAX32620FTHR
00059 //      - #include "MAX32620FTHR.h"
00060 //      - remove max32630fthr library (if present)
00061 //      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
00062 //      - not tested yet
00063 //   - MAX32625PICO
00064 //      - remove max32630fthr library (if present)
00065 //      - remove MAX32620FTHR library (if present)
00066 //      - not tested yet
00067 //
00068 // end Platform_Include_Boilerplate
00069 #include "MAX11131.h"
00070 
00071 // example code board support
00072 //MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00073 //DigitalOut rLED(LED1);
00074 //DigitalOut gLED(LED2);
00075 //DigitalOut bLED(LED3);
00076 //
00077 // Arduino "shield" connector port definitions (MAX32625MBED shown)
00078 #if defined(TARGET_MAX32625MBED)
00079 #define A0 AIN_0
00080 #define A1 AIN_1
00081 #define A2 AIN_2
00082 #define A3 AIN_3
00083 #define D0 P0_0
00084 #define D1 P0_1
00085 #define D2 P0_2
00086 #define D3 P0_3
00087 #define D4 P0_4
00088 #define D5 P0_5
00089 #define D6 P0_6
00090 #define D7 P0_7
00091 #define D8 P1_4
00092 #define D9 P1_5
00093 #define D10 P1_3
00094 #define D11 P1_1
00095 #define D12 P1_2
00096 #define D13 P1_0
00097 #endif
00098 
00099 // example code declare SPI interface
00100 #if defined(TARGET_MAX32625MBED)
00101 SPI spi(SPI1_MOSI, SPI1_MISO, SPI1_SCK); // mosi, miso, sclk spi1 TARGET_MAX32625MBED: P1_1 P1_2 P1_0 Arduino 10-pin header D11 D12 D13
00102 DigitalOut spi_cs(SPI1_SS); // TARGET_MAX32625MBED: P1_3 Arduino 10-pin header D10
00103 #elif defined(TARGET_MAX32600MBED)
00104 SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
00105 DigitalOut spi_cs(SPI2_SS); // Generic: Arduino 10-pin header D10
00106 #else
00107 SPI spi(D11, D12, D13); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
00108 DigitalOut spi_cs(D10); // Generic: Arduino 10-pin header D10
00109 #endif
00110 
00111 // example code declare GPIO interface pins
00112 DigitalOut CNVST_pin(D9); // Digital Trigger Input to MAX11131 device
00113 // AnalogOut REF_plus_pin(Px_x_PortName_To_Be_Determined); // Reference Input to MAX11131 device
00114 // AnalogOut REF_minus_slash_AIN15_pin(Px_x_PortName_To_Be_Determined); // Reference Input to MAX11131 device
00115 DigitalIn EOC_pin(D2); // Digital Event Output from MAX11131 device
00116 // example code declare device instance
00117 MAX11131 g_MAX11131_device(spi, spi_cs, CNVST_pin, EOC_pin, MAX11131::MAX11131_IC);
00118 
00119 //--------------------------------------------------
00120 // Declare the Serial driver
00121 // default baud rate settings are 9600 8N1
00122 // install device driver from http://developer.mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe
00123 // see docs https://docs.mbed.com/docs/mbed-os-handbook/en/5.5/getting_started/what_need/
00124 #if defined(TARGET_MAX32630)
00125     #include "USBSerial.h"
00126 // Hardware serial port over DAPLink
00127 // The default baud rate for the DapLink UART is 9600
00128 //Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
00129 //    #define HAS_DAPLINK_SERIAL 1
00130 // Virtual serial port over USB
00131 // The baud rate does not affect the virtual USBSerial UART.
00132 USBSerial serial;
00133 //--------------------------------------------------
00134 #elif defined(TARGET_MAX32625MBED)
00135     #include "USBSerial.h"
00136 // Hardware serial port over DAPLink
00137 // The default baud rate for the DapLink UART is 9600
00138 //Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
00139 //    #define HAS_DAPLINK_SERIAL 1
00140 // Virtual serial port over USB
00141 // The baud rate does not affect the virtual USBSerial UART.
00142 USBSerial serial;
00143 //--------------------------------------------------
00144 #elif defined(TARGET_MAX32600)
00145     #include "USBSerial.h"
00146 // Hardware serial port over DAPLink
00147 // The default baud rate for the DapLink UART is 9600
00148 Serial DAPLINKserial(P1_1, P1_0);     // tx, rx
00149     #define HAS_DAPLINK_SERIAL 1
00150 // Virtual serial port over USB
00151 // The baud rate does not affect the virtual USBSerial UART.
00152 USBSerial serial;
00153 //--------------------------------------------------
00154 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F401RE)
00155 Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
00156 //--------------------------------------------------
00157 #else
00158 #if defined(SERIAL_TX)
00159 #warning "target not previously tested; guess serial pins are SERIAL_TX, SERIAL_RX..."
00160 Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
00161 #elif defined(USBTX)
00162 #warning "target not previously tested; guess serial pins are USBTX, USBRX..."
00163 Serial serial(USBTX, USBRX);     // tx, rx
00164 #elif defined(UART_TX)
00165 #warning "target not previously tested; guess serial pins are UART_TX, UART_RX..."
00166 Serial serial(UART_TX, UART_RX);     // tx, rx
00167 #else
00168 #warning "target not previously tested; need to define serial pins..."
00169 #endif
00170 #endif
00171 //
00172 #include "CmdLine.h"
00173 CmdLine cmdLine(serial, "serial");
00174 
00175 // example code main function
00176 int main()
00177 {
00178     while (1)
00179     {
00180         g_MAX11131_device.Init();
00181 
00182         while(1) { // this code repeats forever
00183             // this code repeats forever
00184             // Measure ADC channels in sequence from AIN0 to channelNumber_0_15.
00185             // @param[in] g_MAX11131_device.channelNumber_0_15: AIN Channel Number
00186             // @param[in] g_MAX11131_device.PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby
00187             // @param[in] g_MAX11131_device.chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID
00188             int channelId_0_15 = 15;
00189             g_MAX11131_device.channelNumber_0_15 = channelId_0_15;
00190             g_MAX11131_device.PowerManagement_0_2 = 0;
00191             g_MAX11131_device.chan_id_0_1 = 1;
00192             g_MAX11131_device.NumWords = g_MAX11131_device.ScanStandardExternalClock();
00193 
00194             // Read raw ADC codes from device into AINcode[] and RAW_misoData16[]
00195             // @pre one of the MAX11311_Scan functions was called, setting g_MAX11131_device.NumWords
00196             g_MAX11131_device.ReadAINcode();
00197             // @post RAW_misoData16[index] contains the raw SPI Master-In,Slave-Out data
00198             // @post AINcode[NUM_CHANNELS] contains the latest readings in LSBs
00199 
00200             // wait(3.0);
00201             // Use Arduino Serial Plotter to view output: Tools | Serial Plotter
00202             cmdLine.serial().printf("%d", g_MAX11131_device.AINcode[0]);
00203             for (int index = 1; index <= channelId_0_15; index++) {
00204                 cmdLine.serial().printf(",%d", g_MAX11131_device.AINcode[index]);
00205             }
00206             cmdLine.serial().printf("\r\n");
00207 
00208         } // this code repeats forever
00209     }
00210 }