Maxim Integrated / Mbed OS MAX11410BOB_24bit_ADC

Dependencies:   MAX11410 CmdLine USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hello_MAX11410.cpp Source File

Hello_MAX11410.cpp

00001 // /*******************************************************************************
00002 // * Copyright (C) 2020 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 //---------- CODE GENERATOR: helloCppCodeList
00034 // CODE GENERATOR: example code includes
00035 
00036 // example code includes
00037 // standard include for target platform -- Platform_Include_Boilerplate
00038 #include "mbed.h"
00039 // Platforms:
00040 //   - MAX32625MBED
00041 //      - supports mbed-os-5.11, requires USBDevice library
00042 //      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
00043 //      - remove max32630fthr library (if present)
00044 //      - remove MAX32620FTHR library (if present)
00045 //   - MAX32600MBED
00046 //      - remove max32630fthr library (if present)
00047 //      - remove MAX32620FTHR library (if present)
00048 //      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
00049 //   - NUCLEO_F446RE
00050 //      - remove USBDevice library
00051 //      - remove max32630fthr library (if present)
00052 //      - remove MAX32620FTHR library (if present)
00053 //   - NUCLEO_F401RE
00054 //      - remove USBDevice library
00055 //      - remove max32630fthr library (if present)
00056 //      - remove MAX32620FTHR library (if present)
00057 //   - MAX32630FTHR
00058 //      - #include "max32630fthr.h"
00059 //      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
00060 //      - remove MAX32620FTHR library (if present)
00061 //   - MAX32620FTHR
00062 //      - #include "MAX32620FTHR.h"
00063 //      - remove max32630fthr library (if present)
00064 //      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
00065 //      - not tested yet
00066 //   - MAX32625PICO
00067 //      - #include "max32625pico.h"
00068 //      - add https://os.mbed.com/users/switches/code/max32625pico/
00069 //      - remove max32630fthr library (if present)
00070 //      - remove MAX32620FTHR library (if present)
00071 //      - not tested yet
00072 //      - see https://os.mbed.com/users/switches/code/max32625pico/
00073 //      - see https://os.mbed.com/users/switches/code/PICO_board_demo/
00074 //      - see https://os.mbed.com/users/switches/code/PICO_USB_I2C_SPI/
00075 //      - see https://os.mbed.com/users/switches/code/SerialInterface/
00076 //      - Note: To load the MAX32625PICO firmware, hold the button while
00077 //        connecting the USB cable, then copy firmware bin file 
00078 //        to the MAINTENANCE drive.
00079 //      - see https://os.mbed.com/platforms/MAX32625PICO/
00080 //      - see https://os.mbed.com/teams/MaximIntegrated/wiki/MAX32625PICO-Firmware-Updates
00081 //
00082 // end Platform_Include_Boilerplate
00083 #include "MAX11410.h"
00084 
00085 // example code board support
00086 //MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00087 //DigitalOut rLED(LED1);
00088 //DigitalOut gLED(LED2);
00089 //DigitalOut bLED(LED3);
00090 //
00091 // Arduino "shield" connector port definitions (MAX32625MBED shown)
00092 #if defined(TARGET_MAX32625MBED)
00093 #define A0 AIN_0
00094 #define A1 AIN_1
00095 #define A2 AIN_2
00096 #define A3 AIN_3
00097 #define D0 P0_0
00098 #define D1 P0_1
00099 #define D2 P0_2
00100 #define D3 P0_3
00101 #define D4 P0_4
00102 #define D5 P0_5
00103 #define D6 P0_6
00104 #define D7 P0_7
00105 #define D8 P1_4
00106 #define D9 P1_5
00107 #define D10 P1_3
00108 #define D11 P1_1
00109 #define D12 P1_2
00110 #define D13 P1_0
00111 #elif defined(TARGET_MAX32625PICO)
00112 #warning "TARGET_MAX32625PICO not previously tested; need to define pins..."
00113 #define A0 AIN_1
00114 #define A1 AIN_2
00115 // #define A2 AIN_3
00116 // #define A3 AIN_0
00117 #define D0 P0_0
00118 #define D1 P0_1
00119 #define D2 P0_2
00120 #define D3 P0_3
00121 #define D4 P1_7
00122 #define D5 P1_6
00123 #define D6 P4_4
00124 #define D7 P4_5
00125 #define D8 P4_6
00126 #define D9 P4_7
00127 #define D10 P0_7
00128 #define D11 P0_6
00129 #define D12 P0_5
00130 #define D13 P0_4
00131 #endif
00132 
00133 // example code declare SPI interface (GPIO controlled CS)
00134 #if defined(TARGET_MAX32625MBED)
00135 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
00136 DigitalOut spi_cs(SPI1_SS); // TARGET_MAX32625MBED: P1_3 Arduino 10-pin header D10
00137 #elif defined(TARGET_MAX32625PICO)
00138 #warning "TARGET_MAX32625PICO not previously tested; need to define pins..."
00139 SPI spi(SPI0_MOSI, SPI0_MISO, SPI0_SCK); // mosi, miso, sclk spi1 TARGET_MAX32625PICO: pin P0_5 P0_6 P0_4
00140 DigitalOut spi_cs(SPI0_SS); // TARGET_MAX32625PICO: pin P0_7
00141 #elif defined(TARGET_MAX32600MBED)
00142 SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
00143 DigitalOut spi_cs(SPI2_SS); // Generic: Arduino 10-pin header D10
00144 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F401RE)
00145 // TODO1: avoid resource conflict between P5_0, P5_1, P5_2 SPI and DigitalInOut
00146 // void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
00147 //
00148 // TODO1: NUCLEO_F446RE SPI not working; CS and MOSI data looks OK but no SCLK clock pulses.
00149 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk spi1 TARGET_NUCLEO_F446RE: Arduino 10-pin header D11 D12 D13
00150 DigitalOut spi_cs(SPI_CS); // TARGET_NUCLEO_F446RE: PB_6 Arduino 10-pin header D10
00151 //
00152 #else
00153 SPI spi(D11, D12, D13); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
00154 DigitalOut spi_cs(D10); // Generic: Arduino 10-pin header D10
00155 #endif
00156 
00157 // example code declare GPIO interface pins
00158 // example code declare device instance
00159 MAX11410 g_MAX11410_device(spi, spi_cs, MAX11410::MAX11410_IC);
00160 
00161 // CODE GENERATOR: example code for ADC: serial port declaration
00162 //--------------------------------------------------
00163 // Declare the Serial driver
00164 // default baud rate settings are 9600 8N1
00165 // install device driver from http://developer.mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe
00166 // see docs https://docs.mbed.com/docs/mbed-os-handbook/en/5.5/getting_started/what_need/
00167 #if defined(TARGET_MAX32630)
00168     #include "USBSerial.h"
00169 // Hardware serial port over DAPLink
00170 // The default baud rate for the DapLink UART is 9600
00171 //Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
00172 //    #define HAS_DAPLINK_SERIAL 1
00173 // Virtual serial port over USB
00174 // The baud rate does not affect the virtual USBSerial UART.
00175 USBSerial serial;
00176 //--------------------------------------------------
00177 #elif defined(TARGET_MAX32625MBED)
00178     #include "USBSerial.h"
00179 // Hardware serial port over DAPLink
00180 // The default baud rate for the DapLink UART is 9600
00181 //Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
00182 //    #define HAS_DAPLINK_SERIAL 1
00183 // Virtual serial port over USB
00184 // The baud rate does not affect the virtual USBSerial UART.
00185 USBSerial serial;
00186 //--------------------------------------------------
00187 #elif defined(TARGET_MAX32600)
00188     #include "USBSerial.h"
00189 // Hardware serial port over DAPLink
00190 // The default baud rate for the DapLink UART is 9600
00191 Serial DAPLINKserial(P1_1, P1_0);     // tx, rx
00192     #define HAS_DAPLINK_SERIAL 1
00193 // Virtual serial port over USB
00194 // The baud rate does not affect the virtual USBSerial UART.
00195 USBSerial serial;
00196 //--------------------------------------------------
00197 #elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F401RE)
00198 Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
00199 //--------------------------------------------------
00200 #else
00201 #if defined(SERIAL_TX)
00202 #warning "target not previously tested; guess serial pins are SERIAL_TX, SERIAL_RX..."
00203 Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
00204 #elif defined(USBTX)
00205 #warning "target not previously tested; guess serial pins are USBTX, USBRX..."
00206 Serial serial(USBTX, USBRX);     // tx, rx
00207 #elif defined(UART_TX)
00208 #warning "target not previously tested; guess serial pins are UART_TX, UART_RX..."
00209 Serial serial(UART_TX, UART_RX);     // tx, rx
00210 #else
00211 #warning "target not previously tested; need to define serial pins..."
00212 #endif
00213 #endif
00214 //
00215 #include "CmdLine.h"
00216 CmdLine cmdLine(serial, "serial");
00217 
00218 // example code main function
00219 int main()
00220 {
00221     // setup: put your setup code here, to run once
00222 
00223     // example code: serial port banner message
00224     wait(3); // 3000ms timing delay function, platform-specific
00225     cmdLine.serial().printf("\r\nHello_MAX11410\r\n");
00226 
00227     // CODE GENERATOR: example code: member function Init
00228     // Initialize MAX11410 and verify device ID
00229     uint32_t g_SPI_SCLK_Hz = 24000000; // platform limit 24MHz intSPI_SCLK_Platform_Max_MHz * 1000000
00230     if (g_MAX11410_device.Init() == 0)
00231     {  // init failed; try "safe mode" SPI at slower SCLK rate
00232         cmdLine.serial().printf("\r\nMAX11410 Init failed; retry at SPI SCLK frequency 2000000 Hz\r\n");
00233 
00234         g_SPI_SCLK_Hz = 2000000;
00235         g_MAX11410_device.spi_frequency(2000000);
00236         g_MAX11410_device.Init();
00237     }
00238     // CODE GENERATOR: get spi properties from device
00239     if (g_SPI_SCLK_Hz > g_MAX11410_device.get_spi_frequency())
00240     {  // Device limits SPI SCLK frequency
00241         g_SPI_SCLK_Hz = g_MAX11410_device.get_spi_frequency();
00242         cmdLine.serial().printf("\r\nMAX11410 limits SPI SCLK frequency to %ld Hz\r\n", g_SPI_SCLK_Hz);
00243 
00244         g_MAX11410_device.Init();
00245     }
00246     if (g_MAX11410_device.get_spi_frequency() > g_SPI_SCLK_Hz)
00247     {  // Platform limits SPI SCLK frequency
00248         g_MAX11410_device.spi_frequency(g_SPI_SCLK_Hz);
00249         cmdLine.serial().printf("\r\nPlatform limits MAX11410 SPI SCLK frequency to %ld Hz\r\n", g_SPI_SCLK_Hz);
00250 
00251         g_MAX11410_device.Init();
00252     }
00253     // g_SPI_dataMode = g_MAX11410_device.get_spi_dataMode();
00254     while (g_MAX11410_device.Init() == 0)
00255     {
00256         wait(3); // 3000ms timing delay function, platform-specific
00257         cmdLine.serial().printf("\r\nMAX11410 Init failed; retry...\r\n");
00258 
00259     }
00260 
00261     while (1)
00262     {
00263         // loop: put your main code here, to run repeatedly
00264 
00265         // CODE GENERATOR: example code: has no member function REF
00266         // CODE GENERATOR: example code for ADC: repeat-forever convert and print conversion result, one record per line
00267         // CODE GENERATOR: ResolutionBits = 24
00268         // CODE GENERATOR: FScode = 0xffffff
00269         // CODE GENERATOR: NumChannels = 10
00270         // CODE GENERATOR: banner before helloCppCodeList while(1)
00271         cmdLine.serial().printf("v_filter = 0x%2.2x\r\n", g_MAX11410_device.v_filter);
00272 
00273         cmdLine.serial().printf("v_pga = 0x%2.2x\r\n", g_MAX11410_device.v_pga);
00274 
00275         cmdLine.serial().printf("v_ctrl = 0x%2.2x\r\n", g_MAX11410_device.v_ctrl);
00276 
00277         // banner for csv data columns
00278         cmdLine.serial().printf("\"AIN0_LSB\",\"AIN1_LSB\",\"AIN2_LSB\",\"AIN3_LSB\",\"AIN4_LSB\",\"AIN5_LSB\",\"AIN6_LSB\",\"AIN7_LSB\",\"AIN8_LSB\",\"AIN9_LSB\"");
00279 
00280         cmdLine.serial().printf("\r\n");
00281 
00282         while(1) { // this code repeats forever
00283             // this code repeats forever
00284             // CODE GENERATOR: example code: has no member function ScanStandardExternalClock
00285             // CODE GENERATOR: example code: has no member function ReadAINcode
00286             // CODE GENERATOR: example code: member function Read_All_Voltages
00287             // Measure ADC channels in sequence from AIN0 to channelNumber_0_9.
00288             // @param[in] g_MAX11410_device.channelNumber_0_15: AIN Channel Number
00289             // @param[in] g_MAX11410_device.PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby
00290             // @param[in] g_MAX11410_device.chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID
00291             int channelId_0_9 = 9;
00292             //g_MAX11410_device.channelNumber_0_15 = channelId_0_9;
00293             //g_MAX11410_device.PowerManagement_0_2 = 0;
00294             //g_MAX11410_device.chan_id_0_1 = 1;
00295             g_MAX11410_device.Read_All_Voltages();
00296 
00297             // wait(3.0);
00298             // CODE GENERATOR: print conversion result
00299             // Use Arduino Serial Plotter to view output: Tools | Serial Plotter
00300             cmdLine.serial().printf("%d", g_MAX11410_device.AINcode[0]);
00301             for (int index = 1; index <= channelId_0_9; index++) {
00302                 cmdLine.serial().printf(",%d", g_MAX11410_device.AINcode[index]);
00303             }
00304             cmdLine.serial().printf("\r\n");
00305 
00306         } // this code repeats forever
00307     }
00308 }
00309 //---------- CODE GENERATOR: end helloCppCodeList