Library for the MAX11300
Dependents: MAXREFDES130_131_Demo MAXREFDES130_Demo MAX11300_test
MAX11300.cpp@0:bfae6930d2ff, 2016-07-26 (annotated)
- Committer:
- j3
- Date:
- Tue Jul 26 00:33:40 2016 +0000
- Revision:
- 0:bfae6930d2ff
- Child:
- 1:90e0ff21a740
init commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
j3 | 0:bfae6930d2ff | 1 | /********************************************************************** |
j3 | 0:bfae6930d2ff | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
j3 | 0:bfae6930d2ff | 3 | * |
j3 | 0:bfae6930d2ff | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
j3 | 0:bfae6930d2ff | 5 | * copy of this software and associated documentation files (the "Software"), |
j3 | 0:bfae6930d2ff | 6 | * to deal in the Software without restriction, including without limitation |
j3 | 0:bfae6930d2ff | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
j3 | 0:bfae6930d2ff | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
j3 | 0:bfae6930d2ff | 9 | * Software is furnished to do so, subject to the following conditions: |
j3 | 0:bfae6930d2ff | 10 | * |
j3 | 0:bfae6930d2ff | 11 | * The above copyright notice and this permission notice shall be included |
j3 | 0:bfae6930d2ff | 12 | * in all copies or substantial portions of the Software. |
j3 | 0:bfae6930d2ff | 13 | * |
j3 | 0:bfae6930d2ff | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
j3 | 0:bfae6930d2ff | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
j3 | 0:bfae6930d2ff | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
j3 | 0:bfae6930d2ff | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
j3 | 0:bfae6930d2ff | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
j3 | 0:bfae6930d2ff | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
j3 | 0:bfae6930d2ff | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
j3 | 0:bfae6930d2ff | 21 | * |
j3 | 0:bfae6930d2ff | 22 | * Except as contained in this notice, the name of Maxim Integrated |
j3 | 0:bfae6930d2ff | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
j3 | 0:bfae6930d2ff | 24 | * Products, Inc. Branding Policy. |
j3 | 0:bfae6930d2ff | 25 | * |
j3 | 0:bfae6930d2ff | 26 | * The mere transfer of this software does not imply any licenses |
j3 | 0:bfae6930d2ff | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
j3 | 0:bfae6930d2ff | 28 | * trademarks, maskwork rights, or any other form of intellectual |
j3 | 0:bfae6930d2ff | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
j3 | 0:bfae6930d2ff | 30 | * ownership rights. |
j3 | 0:bfae6930d2ff | 31 | **********************************************************************/ |
j3 | 0:bfae6930d2ff | 32 | |
j3 | 0:bfae6930d2ff | 33 | #include "MAX11300.h" |
j3 | 0:bfae6930d2ff | 34 | |
j3 | 0:bfae6930d2ff | 35 | static const uint16_t port_config_design_vals[20] = { |
j3 | 0:bfae6930d2ff | 36 | port_cfg_00_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 37 | port_cfg_01_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 38 | port_cfg_02_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 39 | port_cfg_03_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 40 | port_cfg_04_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 41 | port_cfg_05_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 42 | port_cfg_06_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 43 | port_cfg_07_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 44 | port_cfg_08_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 45 | port_cfg_09_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 46 | port_cfg_10_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 47 | port_cfg_11_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 48 | port_cfg_12_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 49 | port_cfg_13_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 50 | port_cfg_14_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 51 | port_cfg_15_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 52 | port_cfg_16_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 53 | port_cfg_17_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 54 | port_cfg_18_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 55 | port_cfg_19_DESIGNVALUE}; |
j3 | 0:bfae6930d2ff | 56 | |
j3 | 0:bfae6930d2ff | 57 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 58 | MAX11300::MAX11300(SPI & spi_bus, PinName cs, PinName interrupt, PinName cnvt): |
j3 | 0:bfae6930d2ff | 59 | m_spi_bus(spi_bus), m_cs(cs, 1), m_int(interrupt), m_cnvt(cnvt) |
j3 | 0:bfae6930d2ff | 60 | { |
j3 | 0:bfae6930d2ff | 61 | init(); |
j3 | 0:bfae6930d2ff | 62 | } |
j3 | 0:bfae6930d2ff | 63 | |
j3 | 0:bfae6930d2ff | 64 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 65 | MAX11300::~MAX11300() |
j3 | 0:bfae6930d2ff | 66 | { |
j3 | 0:bfae6930d2ff | 67 | } |
j3 | 0:bfae6930d2ff | 68 | |
j3 | 0:bfae6930d2ff | 69 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 70 | void MAX11300::write_register(MAX11300RegAddress_t reg, uint16_t data) |
j3 | 0:bfae6930d2ff | 71 | { |
j3 | 0:bfae6930d2ff | 72 | m_cs = 0; |
j3 | 0:bfae6930d2ff | 73 | m_spi_bus.write(MAX11300Addr_SPI_Write(reg)); |
j3 | 0:bfae6930d2ff | 74 | m_spi_bus.write(static_cast<uint8_t>((0xFF00 & data) >> 8)); |
j3 | 0:bfae6930d2ff | 75 | m_spi_bus.write(static_cast<uint8_t>(0x00FF & data)); |
j3 | 0:bfae6930d2ff | 76 | m_cs = 1; |
j3 | 0:bfae6930d2ff | 77 | } |
j3 | 0:bfae6930d2ff | 78 | |
j3 | 0:bfae6930d2ff | 79 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 80 | uint16_t MAX11300::read_register(MAX11300RegAddress_t reg) |
j3 | 0:bfae6930d2ff | 81 | { |
j3 | 0:bfae6930d2ff | 82 | uint16_t rtn_val = 0; |
j3 | 0:bfae6930d2ff | 83 | |
j3 | 0:bfae6930d2ff | 84 | m_cs = 0; |
j3 | 0:bfae6930d2ff | 85 | m_spi_bus.write(MAX11300Addr_SPI_Read(reg)); |
j3 | 0:bfae6930d2ff | 86 | rtn_val |= (m_spi_bus.write(0xFF) << 8); |
j3 | 0:bfae6930d2ff | 87 | rtn_val |= m_spi_bus.write(0xFF); |
j3 | 0:bfae6930d2ff | 88 | m_cs = 1; |
j3 | 0:bfae6930d2ff | 89 | |
j3 | 0:bfae6930d2ff | 90 | return rtn_val; |
j3 | 0:bfae6930d2ff | 91 | } |
j3 | 0:bfae6930d2ff | 92 | |
j3 | 0:bfae6930d2ff | 93 | void MAX11300::block_write(MAX11300RegAddress_t reg, uint16_t * data, uint8_t num_bytes) |
j3 | 0:bfae6930d2ff | 94 | { |
j3 | 0:bfae6930d2ff | 95 | for(uint8_t idx = 0; idx < num_bytes; idx++) |
j3 | 0:bfae6930d2ff | 96 | { |
j3 | 0:bfae6930d2ff | 97 | write_register(static_cast<MAX11300RegAddress_t>(reg + idx), data[idx]); |
j3 | 0:bfae6930d2ff | 98 | } |
j3 | 0:bfae6930d2ff | 99 | } |
j3 | 0:bfae6930d2ff | 100 | |
j3 | 0:bfae6930d2ff | 101 | void MAX11300::block_read(MAX11300RegAddress_t reg, uint16_t * data, uint8_t num_bytes) |
j3 | 0:bfae6930d2ff | 102 | { |
j3 | 0:bfae6930d2ff | 103 | for(uint8_t idx = 0; idx < num_bytes; idx++) |
j3 | 0:bfae6930d2ff | 104 | { |
j3 | 0:bfae6930d2ff | 105 | data[idx] = read_register(static_cast<MAX11300RegAddress_t>(reg + idx)); |
j3 | 0:bfae6930d2ff | 106 | } |
j3 | 0:bfae6930d2ff | 107 | } |
j3 | 0:bfae6930d2ff | 108 | |
j3 | 0:bfae6930d2ff | 109 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 110 | MAX11300::CmdResult MAX11300::gpio_write(MAX11300_Ports port, uint8_t state) |
j3 | 0:bfae6930d2ff | 111 | { |
j3 | 0:bfae6930d2ff | 112 | return MAX11300::OpFailure; |
j3 | 0:bfae6930d2ff | 113 | } |
j3 | 0:bfae6930d2ff | 114 | |
j3 | 0:bfae6930d2ff | 115 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 116 | MAX11300::CmdResult MAX11300::gpio_read(MAX11300_Ports port, uint8_t & state) |
j3 | 0:bfae6930d2ff | 117 | { |
j3 | 0:bfae6930d2ff | 118 | return MAX11300::OpFailure; |
j3 | 0:bfae6930d2ff | 119 | } |
j3 | 0:bfae6930d2ff | 120 | |
j3 | 0:bfae6930d2ff | 121 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 122 | MAX11300::CmdResult MAX11300::adc_read(MAX11300_Ports port, uint16_t & data) |
j3 | 0:bfae6930d2ff | 123 | { |
j3 | 0:bfae6930d2ff | 124 | return MAX11300::OpFailure; |
j3 | 0:bfae6930d2ff | 125 | } |
j3 | 0:bfae6930d2ff | 126 | |
j3 | 0:bfae6930d2ff | 127 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 128 | MAX11300::CmdResult MAX11300::dac_write(MAX11300_Ports port, uint16_t data) |
j3 | 0:bfae6930d2ff | 129 | { |
j3 | 0:bfae6930d2ff | 130 | return MAX11300::OpFailure; |
j3 | 0:bfae6930d2ff | 131 | } |
j3 | 0:bfae6930d2ff | 132 | |
j3 | 0:bfae6930d2ff | 133 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 134 | MAX11300::CmdResult MAX11300::asw_cntrl(MAX11300_Ports port, uint8_t state) |
j3 | 0:bfae6930d2ff | 135 | { |
j3 | 0:bfae6930d2ff | 136 | return MAX11300::OpFailure; |
j3 | 0:bfae6930d2ff | 137 | } |
j3 | 0:bfae6930d2ff | 138 | |
j3 | 0:bfae6930d2ff | 139 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 140 | void MAX11300::init(void) |
j3 | 0:bfae6930d2ff | 141 | { |
j3 | 0:bfae6930d2ff | 142 | //see datasheet 19-7318; Rev 3; 4/16; page 49 |
j3 | 0:bfae6930d2ff | 143 | //https://datasheets.maximintegrated.com/en/ds/MAX11300.pdf |
j3 | 0:bfae6930d2ff | 144 | //for description of configuration process |
j3 | 0:bfae6930d2ff | 145 | |
j3 | 0:bfae6930d2ff | 146 | uint8_t idx; |
j3 | 0:bfae6930d2ff | 147 | uint16_t mode_bit_mask = 0; |
j3 | 0:bfae6930d2ff | 148 | |
j3 | 0:bfae6930d2ff | 149 | //figure out port modes used |
j3 | 0:bfae6930d2ff | 150 | for(idx = 0; idx < 20; idx++) |
j3 | 0:bfae6930d2ff | 151 | { |
j3 | 0:bfae6930d2ff | 152 | mode_bit_mask |= (1 << ((port_config_design_vals[idx] & 0xf000) >> 12)); |
j3 | 0:bfae6930d2ff | 153 | } |
j3 | 0:bfae6930d2ff | 154 | |
j3 | 0:bfae6930d2ff | 155 | //STEP 1: configure BRST, THSHDN, ADCCONV |
j3 | 0:bfae6930d2ff | 156 | uint16_t device_control_local = (device_control_DESIGNVALUE & (device_control_BRST | device_control_THSHDN | device_control_ADCCONV)); |
j3 | 0:bfae6930d2ff | 157 | write_register(device_control, device_control_local); |
j3 | 0:bfae6930d2ff | 158 | |
j3 | 0:bfae6930d2ff | 159 | //STEP 2: If any port is configured for modes 1,3,4,5,6, or 10 |
j3 | 0:bfae6930d2ff | 160 | if(mode_bit_mask & 0x047A) |
j3 | 0:bfae6930d2ff | 161 | { |
j3 | 0:bfae6930d2ff | 162 | config_process_1(); |
j3 | 0:bfae6930d2ff | 163 | } |
j3 | 0:bfae6930d2ff | 164 | |
j3 | 0:bfae6930d2ff | 165 | //STEP 3: If any port is configured for modes 7,8, or 9 |
j3 | 0:bfae6930d2ff | 166 | if(mode_bit_mask & 0x0380) |
j3 | 0:bfae6930d2ff | 167 | { |
j3 | 0:bfae6930d2ff | 168 | config_process_2(); |
j3 | 0:bfae6930d2ff | 169 | } |
j3 | 0:bfae6930d2ff | 170 | |
j3 | 0:bfae6930d2ff | 171 | //STEP 4: If any port is configured for modes 2,11, or 12 |
j3 | 0:bfae6930d2ff | 172 | if(mode_bit_mask & 0x1804) |
j3 | 0:bfae6930d2ff | 173 | { |
j3 | 0:bfae6930d2ff | 174 | config_process_3(); |
j3 | 0:bfae6930d2ff | 175 | } |
j3 | 0:bfae6930d2ff | 176 | |
j3 | 0:bfae6930d2ff | 177 | //STEP 5: Are Temperature sensors used? |
j3 | 0:bfae6930d2ff | 178 | if(device_control_DESIGNVALUE & (device_control_TMPCTLEXT1 | device_control_TMPCTLEXT0 | device_control_TMPCTLINT)) |
j3 | 0:bfae6930d2ff | 179 | { |
j3 | 0:bfae6930d2ff | 180 | } |
j3 | 0:bfae6930d2ff | 181 | |
j3 | 0:bfae6930d2ff | 182 | //STEP 6: Configure interrupt masks and IRQ modes |
j3 | 0:bfae6930d2ff | 183 | uint16_t interrupt_data[4] = {interrupt_mask_DESIGNVALUE, gpi_irqmode_7_to_0_DESIGNVALUE, |
j3 | 0:bfae6930d2ff | 184 | gpi_irqmode_15_to_8_DESIGNVALUE, gpi_irqmode_19_to_16_DESIGNVALUE}; |
j3 | 0:bfae6930d2ff | 185 | |
j3 | 0:bfae6930d2ff | 186 | block_write(interrupt_mask, interrupt_data, 4); |
j3 | 0:bfae6930d2ff | 187 | } |
j3 | 0:bfae6930d2ff | 188 | |
j3 | 0:bfae6930d2ff | 189 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 190 | void MAX11300::config_process_1(void) |
j3 | 0:bfae6930d2ff | 191 | { |
j3 | 0:bfae6930d2ff | 192 | } |
j3 | 0:bfae6930d2ff | 193 | |
j3 | 0:bfae6930d2ff | 194 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 195 | void MAX11300::config_process_2(void) |
j3 | 0:bfae6930d2ff | 196 | { |
j3 | 0:bfae6930d2ff | 197 | } |
j3 | 0:bfae6930d2ff | 198 | |
j3 | 0:bfae6930d2ff | 199 | //********************************************************************* |
j3 | 0:bfae6930d2ff | 200 | void MAX11300::config_process_3(void) |
j3 | 0:bfae6930d2ff | 201 | { |
j3 | 0:bfae6930d2ff | 202 | } |
j3 | 0:bfae6930d2ff | 203 |