MAX11410 is a high speed, 24-bit Delta-Sigma ADC; small example program which can be changed by modifying the Hello_MAX11410.cpp source code and repeating the compile-build-upload cycle, providing straightforward code for easy adoption.

Dependencies:   MAX11410 CmdLine USBDevice

Hello_MAX11410.cpp

Committer:
whismanoid
Date:
2020-01-17
Revision:
2:2ba2666b8c4a
Parent:
1:441a44fe4c69
Child:
3:66f52e3fde4e

File content as of revision 2:2ba2666b8c4a:

// /*******************************************************************************
// * Copyright (C) 2020 Maxim Integrated Products, Inc., All Rights Reserved.
// *
// * Permission is hereby granted, free of charge, to any person obtaining a
// * copy of this software and associated documentation files (the "Software"),
// * to deal in the Software without restriction, including without limitation
// * the rights to use, copy, modify, merge, publish, distribute, sublicense,
// * and/or sell copies of the Software, and to permit persons to whom the
// * Software is furnished to do so, subject to the following conditions:
// *
// * The above copyright notice and this permission notice shall be included
// * in all copies or substantial portions of the Software.
// *
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
// * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// * OTHER DEALINGS IN THE SOFTWARE.
// *
// * Except as contained in this notice, the name of Maxim Integrated
// * Products, Inc. shall not be used except as stated in the Maxim Integrated
// * Products, Inc. Branding Policy.
// *
// * The mere transfer of this software does not imply any licenses
// * of trade secrets, proprietary technology, copyrights, patents,
// * trademarks, maskwork rights, or any other form of intellectual
// * property whatsoever. Maxim Integrated Products, Inc. retains all
// * ownership rights.
// *******************************************************************************
// */
//---------- CODE GENERATOR: helloCppCodeList
// CODE GENERATOR: example code includes

// example code includes
// standard include for target platform -- Platform_Include_Boilerplate
#include "mbed.h"
// Platforms:
//   - MAX32625MBED
//      - supports mbed-os-5.11, requires USBDevice library
//      - add https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/
//      - remove max32630fthr library (if present)
//      - remove MAX32620FTHR library (if present)
//   - MAX32600MBED
//      - remove max32630fthr library (if present)
//      - remove MAX32620FTHR library (if present)
//      - Windows 10 note:  Don't connect HDK until you are ready to load new firmware into the board.
//   - NUCLEO_F446RE
//      - remove USBDevice library
//      - remove max32630fthr library (if present)
//      - remove MAX32620FTHR library (if present)
//   - NUCLEO_F401RE
//      - remove USBDevice library
//      - remove max32630fthr library (if present)
//      - remove MAX32620FTHR library (if present)
//   - MAX32630FTHR
//      - #include "max32630fthr.h"
//      - add http://os.mbed.org/teams/MaximIntegrated/code/max32630fthr/
//      - remove MAX32620FTHR library (if present)
//   - MAX32620FTHR
//      - #include "MAX32620FTHR.h"
//      - remove max32630fthr library (if present)
//      - add https://os.mbed.com/teams/MaximIntegrated/code/MAX32620FTHR/
//      - not tested yet
//   - MAX32625PICO
//      - remove max32630fthr library (if present)
//      - remove MAX32620FTHR library (if present)
//      - not tested yet
//
// end Platform_Include_Boilerplate
#include "MAX11410.h"

// example code board support
//MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
//DigitalOut rLED(LED1);
//DigitalOut gLED(LED2);
//DigitalOut bLED(LED3);
//
// Arduino "shield" connector port definitions (MAX32625MBED shown)
#if defined(TARGET_MAX32625MBED)
#define A0 AIN_0
#define A1 AIN_1
#define A2 AIN_2
#define A3 AIN_3
#define D0 P0_0
#define D1 P0_1
#define D2 P0_2
#define D3 P0_3
#define D4 P0_4
#define D5 P0_5
#define D6 P0_6
#define D7 P0_7
#define D8 P1_4
#define D9 P1_5
#define D10 P1_3
#define D11 P1_1
#define D12 P1_2
#define D13 P1_0
#endif

// example code declare SPI interface
#if defined(TARGET_MAX32625MBED)
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
DigitalOut spi_cs(SPI1_SS); // TARGET_MAX32625MBED: P1_3 Arduino 10-pin header D10
#elif defined(TARGET_MAX32600MBED)
SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
DigitalOut spi_cs(SPI2_SS); // Generic: Arduino 10-pin header D10
#else
SPI spi(D11, D12, D13); // mosi, miso, sclk spi1 TARGET_MAX32600MBED: Arduino 10-pin header D11 D12 D13
DigitalOut spi_cs(D10); // Generic: Arduino 10-pin header D10
#endif

// example code declare GPIO interface pins
// example code declare device instance
MAX11410 g_MAX11410_device(spi, spi_cs, MAX11410::MAX11410_IC);

// CODE GENERATOR: example code for ADC: serial port declaration
//--------------------------------------------------
// Declare the Serial driver
// default baud rate settings are 9600 8N1
// install device driver from http://developer.mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe
// see docs https://docs.mbed.com/docs/mbed-os-handbook/en/5.5/getting_started/what_need/
#if defined(TARGET_MAX32630)
    #include "USBSerial.h"
// Hardware serial port over DAPLink
// The default baud rate for the DapLink UART is 9600
//Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
//    #define HAS_DAPLINK_SERIAL 1
// Virtual serial port over USB
// The baud rate does not affect the virtual USBSerial UART.
USBSerial serial;
//--------------------------------------------------
#elif defined(TARGET_MAX32625MBED)
    #include "USBSerial.h"
// Hardware serial port over DAPLink
// The default baud rate for the DapLink UART is 9600
//Serial DAPLINKserial(P2_1, P2_0);     // tx, rx
//    #define HAS_DAPLINK_SERIAL 1
// Virtual serial port over USB
// The baud rate does not affect the virtual USBSerial UART.
USBSerial serial;
//--------------------------------------------------
#elif defined(TARGET_MAX32600)
    #include "USBSerial.h"
// Hardware serial port over DAPLink
// The default baud rate for the DapLink UART is 9600
Serial DAPLINKserial(P1_1, P1_0);     // tx, rx
    #define HAS_DAPLINK_SERIAL 1
// Virtual serial port over USB
// The baud rate does not affect the virtual USBSerial UART.
USBSerial serial;
//--------------------------------------------------
#elif defined(TARGET_NUCLEO_F446RE) || defined(TARGET_NUCLEO_F401RE)
Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
//--------------------------------------------------
#else
#if defined(SERIAL_TX)
#warning "target not previously tested; guess serial pins are SERIAL_TX, SERIAL_RX..."
Serial serial(SERIAL_TX, SERIAL_RX);     // tx, rx
#elif defined(USBTX)
#warning "target not previously tested; guess serial pins are USBTX, USBRX..."
Serial serial(USBTX, USBRX);     // tx, rx
#elif defined(UART_TX)
#warning "target not previously tested; guess serial pins are UART_TX, UART_RX..."
Serial serial(UART_TX, UART_RX);     // tx, rx
#else
#warning "target not previously tested; need to define serial pins..."
#endif
#endif
//
#include "CmdLine.h"
CmdLine cmdLine(serial, "serial");

int verbose_debug = 1;

// example code main function
int main()
{
    // DIAGNOSTIC
    wait(3);
    cmdLine.serial().printf("\r\nHello_MAX11410\r\n");
       
    // CODE GENERATOR: example code: member function Init
    // DIAGNOSTIC
    if (verbose_debug) cmdLine.serial().printf("\r\ng_MAX11410_device.Init()\r\n");
    g_MAX11410_device.Init();

    //while (1)
    //{
        // CODE GENERATOR: example code: has no member function REF
        // CODE GENERATOR: example code for ADC: repeat-forever convert and print conversion result, one record per line
        // CODE GENERATOR: ResolutionBits = 24
        // CODE GENERATOR: FScode = None
        // CODE GENERATOR: NumChannels = 10
        //while(1) { // this code repeats forever
        for (int loop_counter = 0; loop_counter < 3; (verbose_debug?loop_counter++:0)) { // this code repeats forever
            // this code repeats forever
            // CODE GENERATOR: example code: has no member function ScanStandardExternalClock
            // CODE GENERATOR: example code: has no member function ReadAINcode
            // CODE GENERATOR: example code: member function Read_All_Voltages
            // Measure ADC channels in sequence from AIN0 to channelNumber_0_9.
            // @param[in] g_MAX11410_device.channelNumber_0_15: AIN Channel Number
            // @param[in] g_MAX11410_device.PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby
            // @param[in] g_MAX11410_device.chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID
            int channelId_0_9 = 9;
            //g_MAX11410_device.channelNumber_0_15 = channelId_0_9;
            //g_MAX11410_device.PowerManagement_0_2 = 0;
            //g_MAX11410_device.chan_id_0_1 = 1;
            // DIAGNOSTIC
            if (verbose_debug) cmdLine.serial().printf("\r\ng_MAX11410_device.Read_All_Voltages()\r\n");
            g_MAX11410_device.Read_All_Voltages(); // hard fault...

            // wait(3.0);
            // CODE GENERATOR: print conversion result
            // Use Arduino Serial Plotter to view output: Tools | Serial Plotter
            cmdLine.serial().printf("%d", g_MAX11410_device.AINcode[0]);
            for (int index = 1; index <= channelId_0_9; index++) {
                cmdLine.serial().printf(",%d", g_MAX11410_device.AINcode[index]);
            }
            cmdLine.serial().printf("\r\n");

            if (verbose_debug) cmdLine.serial().printf("\r\nReached end of loop\r\n");
        } // this code repeats forever
    //}
if (verbose_debug) cmdLine.serial().printf("\r\nReached end of main()\r\n");
}
//---------- CODE GENERATOR: end helloCppCodeList
/* NOT WORKING; MbedOS FaultType: HardFault
++ MbedOS Fault Handler ++

FaultType: HardFault

Context:
R0   : 00FFFFFF
R1   : 20000950
R2   : 00000002
R3   : 20000952
R4   : 20000E58
R5   : 00000002
R6   : 00000000
R7   : 00000002
R8   : 00000000
R9   : 00000001
R10  : 00000003
R11  : 00000000
R12  : 0000962D
SP   : 20002A70
LR   : 0000566F
PC   : 000056AC
xPSR : 01000000
PSP  : 20002A08
MSP  : 20027FD8
CPUID: 410FC241
HFSR : 40000000
MMFSR: 00000000
BFSR : 00000082
UFSR : 00000000
DFSR : 00000000
AFSR : 00000000
BFAR : 00FFFFFF
Mode : Thread
Priv : Privileged
Stack: PSP

-- MbedOS Fault Handler --



++ MbedOS Error Info ++
Error Status: 0x80FF013D Code: 317 Module: 255
Error Message: Fault exception
Location: 0x6E93
Error Value: 0x56AC
Current Thread: main  Id: 0x20002AA0 Entry: 0x7177 StackSize: 0x1000 StackMem: 0x20001AA0 SP: 0x20027F78
For more info, visit: https://armmbed.github.io/mbedos-error/?error=0x80FF013D
-- MbedOS Error Info --
*/