| 1 | /*********************************************************************** |
| 2 | * Defines and sets the LMH6521 High Performance Dual Differential DVGA |
| 3 | * |
| 4 | * (c) 2013 Per Vices Corporation |
| 5 | * See LICENSE.txt for license and copyright information. |
| 6 | **********************************************************************/ |
| 7 | |
| 8 | #ifndef INCLUDED_LMH6521_REGS_HPP |
| 9 | #define INCLUDED_LMH6521_REGS_HPP |
| 10 | |
| 11 | /* Specifications */ |
| 12 | #define LMH6521_MAX_GAIN 31.5 /* dB */ |
| 13 | #define LMH6521_MIN_GAIN 31.5 /* dB */ |
| 14 | #define LMH6521_STEP_SIZE 0.5 /* dB */ |
| 15 | |
| 16 | #include <stdint.h> |
| 17 | #include <math.h> |
| 18 | #include <set> |
| 19 | #include <iostream> |
| 20 | #include <bitset> |
| 21 | |
| 22 | |
| 23 | class lmh6521_regs_t{ |
| 24 | public: |
| 25 | enum read_write_bit_t{ |
| 26 | READ_WRITE_BIT_WRITE = 0, |
| 27 | READ_WRITE_BIT_READ = 1 |
| 28 | }; |
| 29 | read_write_bit_t read_write_bit; |
| 30 | |
| 31 | enum address_t{ |
| 32 | CHANNEL_A = 0, |
| 33 | CHANNEL_B = 1 |
| 34 | }; |
| 35 | address_t address; |
| 36 | |
| 37 | enum enable_t{ |
| 38 | ENABLE_DISABLED = 0, |
| 39 | ENABLE_ENABLED = 1 |
| 40 | }; |
| 41 | enable_t enable; |
| 42 | |
| 43 | int gain; //Gain setting in 0.5dB increments. |
| 44 | |
| 45 | //Output registers A and B to stdout. |
| 46 | void show_regs(){ |
| 47 | std::cout << "\tRegister " << "=>\t" << std::hex << get_regs() << std::endl; |
| 48 | }; |
| 49 | |
| 50 | //Turn off channel |
| 51 | void turn_off(int u_channel){ |
| 52 | address = (address_t)(u_channel); |
| 53 | enable = ENABLE_DISABLED; |
| 54 | }; |
| 55 | |
| 56 | //Turn on channel |
| 57 | void turn_on(int u_channel){ |
| 58 | address = u_channel == 0 ? CHANNEL_A : CHANNEL_B; |
| 59 | enable = ENABLE_DISABLED; |
| 60 | }; |
| 61 | |
| 62 | //INITIALIZATION ROUTINE (Initialize in Safe State) |
| 63 | //POWER OFF, LOWEST POWER SETTING. |
| 64 | lmh6521_regs_t(){ |
| 65 | read_write_bit = READ_WRITE_BIT_READ; |
| 66 | address = CHANNEL_A; |
| 67 | enable = ENABLE_DISABLED; |
| 68 | gain = 0; |
| 69 | } |
| 70 | |
| 71 | //Generate the registers |
| 72 | uint32_t get_regs(){ |
| 73 | int32_t reg = 0; |
| 74 | //Start writing to the registers; |
| 75 | reg |= (uint32_t(gain) & 0x3f) << 1; |
| 76 | reg |= (uint32_t(enable) & 0x1) << 7; |
| 77 | reg |= (uint32_t(address) & 0x1) << 8; |
| 78 | reg |= (uint32_t(read_write_bit) & 0x1) << 15; |
| 79 | return reg; |
| 80 | }; |
| 81 | |
| 82 | //Set the gain (from 0-31.5) on specified channel (0 or 1) |
| 83 | double set_gain(double u_gain, int u_channel) { |
| 84 | read_write_bit = READ_WRITE_BIT_WRITE; |
| 85 | address = u_channel == 0 ? CHANNEL_A : CHANNEL_B; |
| 86 | enable = ENABLE_ENABLED; |
| 87 | gain = u_gain <= 0 ? 0 : int(u_gain); //Sanity check for min gain. |
| 88 | gain = u_gain >= 31.5 ? 63 : int(u_gain*2) ; //Sanity check for max gain. |
| 89 | return ((double) gain/2); |
| 90 | }; |
| 91 | |
| 92 | double read_channel(double u_channel) { |
| 93 | read_write_bit = READ_WRITE_BIT_READ; |
| 94 | address = u_channel == 0 ? CHANNEL_A : CHANNEL_B; |
| 95 | //enable = ENABLE_ENABLED; |
| 96 | //gain = u_gain <= 0 ? 0 : int(u_gain); //Sanity check for min gain. |
| 97 | //gain = u_gain >= 31.5 ? 63 : int(u_gain*2) ; //Sanity check for max gain. |
| 98 | return ((double) u_channel); |
| 99 | }; |
| 100 | |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | #endif /* INCLUDED_ADF4350_REGS_HPP */ |