Maxim Integrated MAX11131 SPI 12-bit 16-channel ADC with SampleSet
Dependents: MAX11131BOB_Tester MAX11131BOB_12bit_16ch_SampleSet_SPI_ADC MAX11131BOB_Serial_Tester
Diff: MAX11131.cpp
- Revision:
- 6:cb7bdeb185d0
- Parent:
- 5:6ef046dbe77e
- Child:
- 9:8d47cb713984
--- a/MAX11131.cpp Sun Aug 04 01:16:46 2019 -0700 +++ b/MAX11131.cpp Fri Sep 27 11:49:23 2019 +0000 @@ -43,6 +43,7 @@ // Device Name = MAX11131 // Device Description = 3Msps, Low-Power, Serial SPI 12-Bit, 16-Channel, Differential/Single-Ended Input, SAR ADC +// Device DeviceBriefDescription = 12-bit 3Msps 16-ch ADC // Device Manufacturer = Maxim Integrated // Device PartNumber = MAX11131ATI+ // Device RegValue_Width = DataWidth16bit_HL @@ -89,29 +90,19 @@ // SupplyPin Function = Digital // -// CODE GENERATOR: class constructor definition MAX11131::MAX11131(SPI &spi, DigitalOut &cs_pin, // SPI interface - // CODE GENERATOR: class constructor definition gpio InputPin pins DigitalOut &CNVST_pin, // Digital Trigger Input to MAX11131 device - // AnalogOut &REF__pin, // Reference Input to MAX11131 device - // AnalogOut &REF__AIN15_pin, // Reference Input to MAX11131 device - // CODE GENERATOR: class constructor definition gpio OutputPin pins + // AnalogOut &REF_plus_pin, // Reference Input to MAX11131 device + // AnalogOut &REF_minus_slash_AIN15_pin, // Reference Input to MAX11131 device DigitalIn &EOC_pin, // Digital Event Output from MAX11131 device - // CODE GENERATOR: class constructor definition ic_variant MAX11131_ic_t ic_variant) - // CODE GENERATOR: class constructor initializer list : m_spi(spi), m_cs_pin(cs_pin), // SPI interface - // CODE GENERATOR: class constructor initializer list gpio InputPin pins m_CNVST_pin(CNVST_pin), // Digital Trigger Input to MAX11131 device - // m_REF__pin(REF__pin), // Reference Input to MAX11131 device - // m_REF__AIN15_pin(REF__AIN15_pin), // Reference Input to MAX11131 device - // CODE GENERATOR: class constructor initializer list gpio OutputPin pins + // m_REF_plus_pin(REF_plus_pin), // Reference Input to MAX11131 device + // m_REF_minus_slash_AIN15_pin(REF_minus_slash_AIN15_pin), // Reference Input to MAX11131 device m_EOC_pin(EOC_pin), // Digital Event Output from MAX11131 device - // CODE GENERATOR: class constructor initializer list ic_variant m_ic_variant(ic_variant) { - // CODE GENERATOR: class constructor definition SPI interface initialization - // // SPI CS = ActiveLow // SPI FrameStart = CS m_SPI_cs_state = 1; @@ -121,7 +112,7 @@ // SPI CPHA = 1 // SPI MOSI and MISO Data are both stable on Rising edge of SCLK // SPI SCLK Idle High - m_SPI_dataMode = 3; //SPI_MODE3 // CPOL=1,CPHA=1: Rising Edge stable; SCLK idle High + m_SPI_dataMode = 3; //SPI_MODE3; // CPOL=1,CPHA=1: Rising Edge stable; SCLK idle High m_spi.format(8,m_SPI_dataMode); // int bits_must_be_8, int mode=0_3 CPOL=0,CPHA=0 // SPI SCLKMaxMHz = 48 @@ -142,48 +133,44 @@ #endif m_spi.frequency(m_SPI_SCLK_Hz); - // TODO1: CODE GENERATOR: class constructor definition gpio InputPin (Input to device) initialization + // + // CNVST Trigger Input to MAX11131 device + m_CNVST_pin = 1; // output logic high -- initial value in constructor // - m_CNVST_pin = 1; // output logic high -- initial value in constructor + // REF_plus Reference Input to MAX11131 device + // + // REF_minus_slash_AIN15 Reference Input to MAX11131 device + // + // EOC Event Output from device } -// CODE GENERATOR: class destructor definition MAX11131::~MAX11131() { // do nothing } -// CODE GENERATOR: spi_frequency setter definition -/// set SPI SCLK frequency +/// set SPI SCLK frequency void MAX11131::spi_frequency(int spi_sclk_Hz) { m_SPI_SCLK_Hz = spi_sclk_Hz; m_spi.frequency(m_SPI_SCLK_Hz); } -// CODE GENERATOR: omit global g_MAX11131_device -// CODE GENERATOR: extern function declarations -// CODE GENERATOR: extern function requirement MAX11131::SPIoutputCS // Assert SPI Chip Select // SPI chip-select for MAX11131 // void MAX11131::SPIoutputCS(int isLogicHigh) { - // CODE GENERATOR: extern function definition for function SPIoutputCS - // CODE GENERATOR: extern function definition for standard SPI interface function SPIoutputCS(int isLogicHigh) m_SPI_cs_state = isLogicHigh; m_cs_pin = m_SPI_cs_state; } -// CODE GENERATOR: extern function requirement MAX11131::SPIwrite16bits // SPI write 16 bits // SPI interface to MAX11131 shift 16 bits mosiData16 into MAX11131 DIN // ignoring MAX11131 DOUT // void MAX11131::SPIwrite16bits(int16_t mosiData16) { - // CODE GENERATOR: extern function definition for function SPIwrite16bits - // TODO1: CODE GENERATOR: extern function definition for standard SPI interface function SPIwrite16bits(int16_t mosiData16) size_t byteCount = 2; static char mosiData[2]; static char misoData[2]; @@ -204,6 +191,11 @@ // // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts() //~ interrupts(); + // Optional Diagnostic function to print SPI transactions + if (onSPIprint) + { + onSPIprint(byteCount, (uint8_t*)mosiData, (uint8_t*)misoData); + } // // VERIFY: SPIwrite24bits print diagnostic information //cmdLine.serial().printf(" MOSI->")); @@ -258,7 +250,6 @@ // return misoData16; } -// CODE GENERATOR: extern function requirement MAX11131::SPIwrite24bits // SPI write 17-24 bits // SPI interface to MAX11131 shift 16 bits mosiData16 into MAX11131 DIN // followed by one additional SCLK byte. @@ -266,8 +257,6 @@ // void MAX11131::SPIwrite24bits(int16_t mosiData16_FFFF00, int8_t mosiData8_0000FF) { - // CODE GENERATOR: extern function definition for function SPIwrite24bits - // TODO1: CODE GENERATOR: extern function definition for standard SPI interface function SPIwrite24bits(int16_t mosiData16_FFFF00, int8_t mosiData8_0000FF) // TODO: implement SPIwrite24bits(int16_t mosiData16_FFFF00, int8_t mosiData8_0000FF) size_t byteCount = 3; static char mosiData[3]; @@ -290,6 +279,11 @@ // // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts() //~ interrupts(); + // Optional Diagnostic function to print SPI transactions + if (onSPIprint) + { + onSPIprint(byteCount, (uint8_t*)mosiData, (uint8_t*)misoData); + } // // VERIFY: SPIwrite24bits print diagnostic information //cmdLine.serial().printf(" MOSI->")); @@ -343,14 +337,11 @@ // return misoData16; } -// CODE GENERATOR: extern function requirement MAX11131::SPIread16bits // SPI read 16 bits while MOSI (MAX11131 DIN) is 0 // SPI interface to capture 16 bits miso data from MAX11131 DOUT // int16_t MAX11131::SPIread16bits() { - // CODE GENERATOR: extern function definition for function SPIread16bits - // TODO1: CODE GENERATOR: extern function definition for standard SPI interface function int16_t SPIread16bits() int mosiData16 = 0; size_t byteCount = 2; static char mosiData[2]; @@ -372,6 +363,11 @@ // // Arduino: begin critical section: noInterrupts() masks all interrupt sources; end critical section with interrupts() //~ interrupts(); + // Optional Diagnostic function to print SPI transactions + if (onSPIprint) + { + onSPIprint(byteCount, (uint8_t*)mosiData, (uint8_t*)misoData); + } // // VERIFY: SPIwrite24bits print diagnostic information //cmdLine.serial().printf(" MOSI->")); @@ -426,58 +422,40 @@ return misoData16; } -// CODE GENERATOR: extern function requirement MAX11131::CNVSToutputPulseLow // Assert MAX11131 CNVST convert start. // Required when using any of the InternalClock modes with SWCNV 0. // Trigger measurement by driving CNVST/AIN14 pin low for a minimum active-low pulse duration of 5ns. (AIN14 is not available) // void MAX11131::CNVSToutputPulseLow() { - // CODE GENERATOR: extern function definition for function CNVSToutputPulseLow - // TODO1: CODE GENERATOR: extern function definition for gpio interface function CNVSToutputPulseLow - // TODO1: CODE GENERATOR: gpio pin CNVST assuming member function m_CNVST_pin - // TODO1: CODE GENERATOR: gpio direction output // m_CNVST_pin.output(); // only applicable to DigitalInOut - // TODO1: CODE GENERATOR: gpio function PulseLow m_CNVST_pin = 0; // output logic low wait(0.01); // pulse low delay time m_CNVST_pin = 1; // output logic high } -// CODE GENERATOR: extern function requirement MAX11131::EOCinputWaitUntilLow // Wait for MAX11131 EOC pin low, indicating end of conversion. // Required when using any of the InternalClock modes. // void MAX11131::EOCinputWaitUntilLow() { - // CODE GENERATOR: extern function definition for function EOCinputWaitUntilLow - // TODO1: CODE GENERATOR: extern function definition for gpio interface function EOCinputWaitUntilLow - // TODO1: CODE GENERATOR: gpio pin EOC assuming member function m_EOC_pin - // TODO1: CODE GENERATOR: gpio direction input // m_EOC_pin.input(); // only applicable to DigitalInOut - // TODO1: CODE GENERATOR: gpio function WaitUntilLow while (m_EOC_pin != 0) { // spinlock waiting for logic low pin state } } -// CODE GENERATOR: extern function requirement MAX11131::EOCinputValue // Return the status of the MAX11131 EOC pin. // int MAX11131::EOCinputValue() { - // CODE GENERATOR: extern function definition for function EOCinputValue - // TODO1: CODE GENERATOR: extern function definition for gpio interface function EOCinputValue - // TODO1: CODE GENERATOR: gpio pin EOC assuming member function m_EOC_pin - // TODO1: CODE GENERATOR: gpio direction input // m_EOC_pin.input(); // only applicable to DigitalInOut - // TODO1: CODE GENERATOR: gpio function Value return m_EOC_pin.read(); } -// CODE GENERATOR: class member function definitions //---------------------------------------- +// Menu item '!' // Initialize device void MAX11131::Init(void) { @@ -1217,6 +1195,7 @@ } //---------------------------------------- +// Menu item 'IS' // ADC Channels AIN(channelId), AIN(channelId+1) = Both Single-Ended, Unipolar // Full Scale = VREF // Voltage per LSB count = VREF/4096 @@ -1226,7 +1205,7 @@ // AIN(channelId) voltage must always be between 0 and VREF. // AIN(channelId+1) voltage must always be between 0 and VREF. // -void MAX11131::Reconfigure_SingleEnded(int channelNumber_0_15) +void MAX11131::Reconfigure_SingleEnded(int channel_0_15) { //---------------------------------------- @@ -1234,7 +1213,7 @@ // AIN(ch)/AIN(ch+1) two independent single-ended inputs, // unipolar code (Full Scale = VREF, LSB = VREF/4096) // - const int channelPairIndex = channelNumber_0_15 / 2; + const int channelPairIndex = channel_0_15 / 2; const int bitmask = (1 << (10 - channelPairIndex)); UNIPOLAR &= ~ bitmask; BIPOLAR &= ~ bitmask; @@ -1255,6 +1234,7 @@ } //---------------------------------------- +// Menu item 'IU' // ADC Channels AIN(channelId), AIN(channelId+1) = Differential Unipolar (AIN(channelId) > AIN(channelId+1)) // Full Scale = VREF // Voltage per LSB count = VREF/4096 @@ -1262,7 +1242,7 @@ // AIN(channelId) voltage must always be between 0 and VREF. // AIN(channelId+1) voltage must always be between 0 and VREF. // -void MAX11131::Reconfigure_DifferentialUnipolar(int channelNumber_0_15) +void MAX11131::Reconfigure_DifferentialUnipolar(int channel_0_15) { //---------------------------------------- @@ -1270,7 +1250,7 @@ // AIN(ch)/AIN(ch+1) differential input pair, // unipolar code (AIN(ch)>AIN(ch+1)) (Full Scale = VREF, LSB = VREF/4096) // - const int channelPairIndex = channelNumber_0_15 / 2; + const int channelPairIndex = channel_0_15 / 2; const int bitmask = (1 << (10 - channelPairIndex)); UNIPOLAR |= bitmask; BIPOLAR &= ~ bitmask; @@ -1292,6 +1272,7 @@ } //---------------------------------------- +// Menu item 'IB' // ADC Channels AIN(channelId), AIN(channelId+1) = Differential Bipolar // Full Scale = VREF // Voltage per LSB count = VREF/4096 @@ -1299,7 +1280,7 @@ // AIN(channelId) voltage must always be between 0 and VREF. // AIN(channelId+1) voltage must always be between 0 and VREF. // -void MAX11131::Reconfigure_DifferentialBipolarFSVref(int channelNumber_0_15) +void MAX11131::Reconfigure_DifferentialBipolarFSVref(int channel_0_15) { //---------------------------------------- @@ -1307,7 +1288,7 @@ // AIN(ch)/AIN(ch+1) differential input pair (+/-)(1/2)Vref, // bipolar code (Full Scale = VREF, LSB = VREF/4096) // - const int channelPairIndex = channelNumber_0_15 / 2; + const int channelPairIndex = channel_0_15 / 2; const int bitmask = (1 << (10 - channelPairIndex)); UNIPOLAR &= ~ bitmask; BIPOLAR |= bitmask; @@ -1328,6 +1309,7 @@ } //---------------------------------------- +// Menu item 'IR' // ADC Channels AIN(channelId), AIN(channelId+1) = Differential Bipolar // Full Scale = 2 * VREF // Voltage per LSB count = VREF/2048 @@ -1335,7 +1317,7 @@ // AIN(channelId) voltage must always be between 0 and VREF. // AIN(channelId+1) voltage must always be between 0 and VREF. // -void MAX11131::Reconfigure_DifferentialBipolarFS2Vref(int channelNumber_0_15) +void MAX11131::Reconfigure_DifferentialBipolarFS2Vref(int channel_0_15) { //---------------------------------------- @@ -1343,7 +1325,7 @@ // AIN(ch)/AIN(ch+1) differential input pair (+/-)Vref, // bipolar code (Full Scale = 2VREF, LSB = VREF/2048) // - const int channelPairIndex = channelNumber_0_15 / 2; + const int channelPairIndex = channel_0_15 / 2; const int bitmask = (1 << (10 - channelPairIndex)); UNIPOLAR &= ~ bitmask; BIPOLAR |= bitmask; @@ -1395,6 +1377,7 @@ // If internal clock mode with SWCNV=0, measurements will be triggered using CNVST pin. // // @pre one of the Scan functions was called, setting NumWords +// @param[in] NumWords: number of words to be read from the FIFO // @post RAW_misoData16[index] contains the raw SPI Master-In,Slave-Out data // @post AINcode[NUM_CHANNELS] contains the latest readings in LSBs // @@ -1563,6 +1546,7 @@ // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @param[in] chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For external clock modes, the data format depends on CHAN_ID. // when CHAN_ID = 0: misoData16 = 0 DATA[11:0] x x x // when CHAN_ID = 1: misoData16 = CH[3:0] DATA[11:0] @@ -1659,6 +1643,7 @@ // CNVST pin is not used. (AIN14 is available) // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For internal clock modes, the data format always includes the channel address. // misoData16 = CH[3:0] DATA[11:0] // @@ -1823,6 +1808,7 @@ // CS must be held low for minimum of 17 SCLK cycles. // CNVST pin is not used. (AIN14 is available) // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For internal clock modes, the data format always includes the channel address. // misoData16 = CH[3:0] DATA[11:0] // @@ -1959,6 +1945,7 @@ // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @param[in] chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For external clock modes, the data format depends on CHAN_ID. // when CHAN_ID = 0: misoData16 = 0 DATA[11:0] x x x // when CHAN_ID = 1: misoData16 = CH[3:0] DATA[11:0] @@ -2054,6 +2041,7 @@ // CS must be held low for minimum of 17 SCLK cycles. // CNVST pin is not used. (AIN14 is available) // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For internal clock modes, the data format always includes the channel address. // misoData16 = CH[3:0] DATA[11:0] // @@ -2190,6 +2178,7 @@ // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @param[in] chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For external clock modes, the data format depends on CHAN_ID. // when CHAN_ID = 0: misoData16 = 0 DATA[11:0] x x x // when CHAN_ID = 1: misoData16 = CH[3:0] DATA[11:0] @@ -2302,6 +2291,7 @@ // CS must be held low for minimum of 17 SCLK cycles. // CNVST pin is not used. (AIN14 is available) // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For internal clock modes, the data format always includes the channel address. // misoData16 = CH[3:0] DATA[11:0] // @@ -2496,6 +2486,7 @@ // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @param[in] chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For external clock modes, the data format depends on CHAN_ID. // when CHAN_ID = 0: misoData16 = 0 DATA[11:0] x x x // when CHAN_ID = 1: misoData16 = CH[3:0] DATA[11:0] @@ -2631,9 +2622,11 @@ // (((enabledChannelsPattern[0]) & 0x0F) << 4) | ((enabledChannelsPattern[1]) & 0x0F) // If it is an odd number of channels, additional nybbles will be ignored. // CS will be asserted low during the entire SAMPLESET pattern selection. +// @param[in] enabledChannelsPattern: array of channel select, one channel per byte // @param[in] PowerManagement_0_2: 0=Normal, 1=AutoShutdown, 2=AutoStandby // @param[in] chan_id_0_1: ADC_MODE_CONTROL.CHAN_ID // @return number of ScanRead() words needed to retrieve the data. +// @post NumWords = number of words to be read from the FIFO // For external clock modes, the data format depends on CHAN_ID. // when CHAN_ID = 0: misoData16 = 0 DATA[11:0] x x x // when CHAN_ID = 1: misoData16 = CH[3:0] DATA[11:0]