AD9249 ADC

Fork of adc_ad9249 by wimbeaumont Project

Committer:
NickRyder
Date:
Tue Oct 07 21:31:16 2014 +0000
Revision:
2:f300a1dafedc
Parent:
1:01459a6ab296
Tidying up.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbeaumont 0:9efb460e962b 1 #include <mbed.h>
wbeaumont 0:9efb460e962b 2
wbeaumont 0:9efb460e962b 3 #include "AD9249.h"
wbeaumont 0:9efb460e962b 4 #include "SWSPI_BI.h"
wbeaumont 0:9efb460e962b 5
wbeaumont 1:01459a6ab296 6
wbeaumont 1:01459a6ab296 7 #define AD9249_SRC_VER "1.22"
wbeaumont 1:01459a6ab296 8
wbeaumont 0:9efb460e962b 9 #define CS_POL 1
wbeaumont 0:9efb460e962b 10 #define NCS_POL 0
wbeaumont 0:9efb460e962b 11 /*
wbeaumont 0:9efb460e962b 12 const u16 AD9249::configreg= 0x0;
wbeaumont 0:9efb460e962b 13 const u16 AD9249::chip_id_reg= 0x1;
wbeaumont 0:9efb460e962b 14 const u16 AD9249::chip_grade_reg= 0x2;
wbeaumont 0:9efb460e962b 15 // const u16 AD9249::unused_reg= { 0x3, 0x07, 0xA, 0xE, 0xF ,0x11, 0x12,0x13, 0x17, 0x1D, 0x1E, 0x1F,0x20 };
wbeaumont 0:9efb460e962b 16 const u16 AD9249::dev_index2_reg= 0x4;
wbeaumont 0:9efb460e962b 17 const u16 AD9249::dev_index1_reg= 0x4;
wbeaumont 0:9efb460e962b 18 const u16 AD9249::transfer_reg= 0xFF;
wbeaumont 0:9efb460e962b 19 const u16 AD9249::power_mode_reg= 0x8;
wbeaumont 0:9efb460e962b 20 const u16 AD9249::clock_gobal_reg= 0x9;
wbeaumont 0:9efb460e962b 21 const u16 AD9249::clock_divide_reg= 0xB;
wbeaumont 0:9efb460e962b 22 const u16 AD9249::enhancement_ctr_reg= 0xC;
wbeaumont 0:9efb460e962b 23 const u16 AD9249::test_mode_reg= 0xD;
wbeaumont 0:9efb460e962b 24 const u16 AD9249::offset_adj_reg= 0x10;
wbeaumont 0:9efb460e962b 25 const u16 AD9249::output_mode_reg= 0x14;
wbeaumont 0:9efb460e962b 26 const u16 AD9249::output_adj_reg= 0x15;
wbeaumont 0:9efb460e962b 27 const u16 AD9249::output_phase_reg= 0x16;
wbeaumont 0:9efb460e962b 28 const u16 AD9249::vref_reg= 0x18;
wbeaumont 0:9efb460e962b 29 const u16 AD9249::usserpatt1_LSB_reg= 0x19;
wbeaumont 0:9efb460e962b 30 const u16 AD9249::usserpatt1_MSB_reg= 0x1A;
wbeaumont 0:9efb460e962b 31 const u16 AD9249::usserpatt2_LSB_reg= 0x1B;
wbeaumont 0:9efb460e962b 32 const u16 AD9249::usserpatt2_MSB_reg= 0x1C;
wbeaumont 0:9efb460e962b 33 const u16 AD9249::serial_out_cntr_reg= 0x21;
wbeaumont 0:9efb460e962b 34 const u16 AD9249::serial_status_reg= 0x22;
wbeaumont 0:9efb460e962b 35 const u16 AD9249::sample_rate_reg= 0x100;
wbeaumont 0:9efb460e962b 36 const u16 AD9249::user_io_ctr2_reg= 0x101;
wbeaumont 0:9efb460e962b 37 const u16 AD9249::user_io_ctr3_reg= 0x102;
wbeaumont 0:9efb460e962b 38 const u16 AD9249::sync_reg= 0x109;
wbeaumont 0:9efb460e962b 39 */
wbeaumont 0:9efb460e962b 40
wbeaumont 0:9efb460e962b 41
NickRyder 2:f300a1dafedc 42 AD9249::AD9249(SWSPI_BI * spi_dev, DigitalOut * csb_pin):
NickRyder 2:f300a1dafedc 43 getVersion(AD9249_HDR_VER, AD9249_SRC_VER, __TIME__, __DATE__)
NickRyder 2:f300a1dafedc 44 {
NickRyder 2:f300a1dafedc 45 spi = spi_dev;
NickRyder 2:f300a1dafedc 46 csb = csb_pin;
wbeaumont 0:9efb460e962b 47 }
wbeaumont 0:9efb460e962b 48
wbeaumont 0:9efb460e962b 49
NickRyder 2:f300a1dafedc 50 AD9249::u32 AD9249::spi_cycle(u16 reg, bool rw, u16 nrbytes, u32 data) {
wbeaumont 0:9efb460e962b 51 // format instruction
NickRyder 2:f300a1dafedc 52 u32 read = 0;
NickRyder 2:f300a1dafedc 53 reg = 0x1FFF & reg;
NickRyder 2:f300a1dafedc 54 if (rw) reg |= 0x8000;
NickRyder 2:f300a1dafedc 55 if (nrbytes > 2) return 0; // this function doesn't support stream
NickRyder 2:f300a1dafedc 56 u32 stnrbytes = (nrbytes - 1);
NickRyder 2:f300a1dafedc 57 stnrbytes = stnrbytes << 13;
NickRyder 2:f300a1dafedc 58 reg |= stnrbytes; // reg is now the instruction
NickRyder 2:f300a1dafedc 59 spi->format(16, 0); // should make sure the sclk is high
NickRyder 2:f300a1dafedc 60 printf("send %04X \n\r", reg);
NickRyder 2:f300a1dafedc 61 if (rw) {
NickRyder 2:f300a1dafedc 62 spi->write(reg, csb, false, CS_POL, true);
NickRyder 2:f300a1dafedc 63 spi->format((nrbytes) * 8, 0);
NickRyder 2:f300a1dafedc 64 read=spi->read(csb, true, CS_POL);
NickRyder 2:f300a1dafedc 65 } else{
NickRyder 2:f300a1dafedc 66 spi->write(reg, csb, false, CS_POL);
NickRyder 2:f300a1dafedc 67 spi->format((nrbytes) * 8, 0);
NickRyder 2:f300a1dafedc 68 spi->write(data, csb, true, CS_POL);
NickRyder 2:f300a1dafedc 69 }
NickRyder 2:f300a1dafedc 70 return read;
NickRyder 2:f300a1dafedc 71 }
wbeaumont 0:9efb460e962b 72
wbeaumont 0:9efb460e962b 73
NickRyder 2:f300a1dafedc 74 bool AD9249::getDevInfo(u8& chipid, u8& grade, u16 &rb) {
NickRyder 2:f300a1dafedc 75 u32 data = spi_cycle(chip_grade_reg, true, 2, 0);
NickRyder 2:f300a1dafedc 76 rb = data;
NickRyder 2:f300a1dafedc 77 chipid = data & 0XFF;
NickRyder 2:f300a1dafedc 78 grade = (data >> 8) & 0xFF;
NickRyder 2:f300a1dafedc 79 return true;
NickRyder 2:f300a1dafedc 80 }
wbeaumont 0:9efb460e962b 81
NickRyder 2:f300a1dafedc 82 bool AD9249::getDevId(u8& chipid){
NickRyder 2:f300a1dafedc 83 bool rv = readReg8(chip_id_reg, chipid);
NickRyder 2:f300a1dafedc 84 return rv;
NickRyder 2:f300a1dafedc 85 }
NickRyder 2:f300a1dafedc 86
NickRyder 2:f300a1dafedc 87 bool AD9249::getGrade(u8& chipid){
NickRyder 2:f300a1dafedc 88 bool rv = readReg8(chip_grade_reg, chipid);
NickRyder 2:f300a1dafedc 89 return rv;
NickRyder 2:f300a1dafedc 90 }
wbeaumont 0:9efb460e962b 91
NickRyder 2:f300a1dafedc 92 bool AD9249::readReg16(u16 regaddr, u16& data) {
NickRyder 2:f300a1dafedc 93 u32 datai = spi_cycle(regaddr, true, 2, 0);
NickRyder 2:f300a1dafedc 94 data = (u16) (datai & 0XFFFF);
NickRyder 2:f300a1dafedc 95 return true;
NickRyder 2:f300a1dafedc 96 }
wbeaumont 0:9efb460e962b 97
NickRyder 2:f300a1dafedc 98 bool AD9249::readReg8(u16 regaddr, u8& data) {
NickRyder 2:f300a1dafedc 99 u32 datai = spi_cycle(regaddr, true, 1, 0);
NickRyder 2:f300a1dafedc 100 data = (u8) (datai & 0XFF);
NickRyder 2:f300a1dafedc 101 return true;
NickRyder 2:f300a1dafedc 102 }
NickRyder 2:f300a1dafedc 103
NickRyder 2:f300a1dafedc 104 bool AD9249::setReg16(u16 regaddr, u16 data) {
NickRyder 2:f300a1dafedc 105 u32 datai = spi_cycle(regaddr, false, 2, (u32) data);
NickRyder 2:f300a1dafedc 106 return true;
NickRyder 2:f300a1dafedc 107 }
wbeaumont 0:9efb460e962b 108
NickRyder 2:f300a1dafedc 109 bool AD9249::setReg8 (u16 regaddr, u8 data) {
NickRyder 2:f300a1dafedc 110 u32 datai = spi_cycle(regaddr, false, 1, (u32) data);
NickRyder 2:f300a1dafedc 111 return true;
NickRyder 2:f300a1dafedc 112 }
wbeaumont 0:9efb460e962b 113
wbeaumont 0:9efb460e962b 114
NickRyder 2:f300a1dafedc 115 bool AD9249::setPattern1(u16 pattern) {
NickRyder 2:f300a1dafedc 116 bool rv = setReg16(usserpatt1_MSB_reg, pattern);
NickRyder 2:f300a1dafedc 117 return rv;
NickRyder 2:f300a1dafedc 118 }
NickRyder 2:f300a1dafedc 119
NickRyder 2:f300a1dafedc 120 bool AD9249::setPattern2(u16 pattern) {
NickRyder 2:f300a1dafedc 121 bool rv = setReg16(usserpatt2_MSB_reg, pattern);
NickRyder 2:f300a1dafedc 122 return rv;
NickRyder 2:f300a1dafedc 123 }
wbeaumont 0:9efb460e962b 124
NickRyder 2:f300a1dafedc 125 bool AD9249::readPattern1(u16& pattern) {
NickRyder 2:f300a1dafedc 126 bool rv = readReg16(usserpatt1_MSB_reg, pattern);
NickRyder 2:f300a1dafedc 127 return rv;
NickRyder 2:f300a1dafedc 128 }
NickRyder 2:f300a1dafedc 129
NickRyder 2:f300a1dafedc 130 bool AD9249::readPattern2(u16& pattern) {
NickRyder 2:f300a1dafedc 131 bool rv = readReg16(usserpatt2_MSB_reg, pattern);
NickRyder 2:f300a1dafedc 132 return rv;
NickRyder 2:f300a1dafedc 133 }
wbeaumont 0:9efb460e962b 134
NickRyder 2:f300a1dafedc 135 void AD9249::init1(){}
NickRyder 2:f300a1dafedc 136
NickRyder 2:f300a1dafedc 137 void AD9249::init2(){}