program to set the MCP4728 address for the SOLID amplifier board
Dependencies: DevInterfaces I2Cinterfaces MCP4728setaddr mbed
main.cpp@0:d97e8b3802b3, 2016-01-12 (annotated)
- Committer:
- wbeaumont
- Date:
- Tue Jan 12 14:43:00 2016 +0000
- Revision:
- 0:d97e8b3802b3
- Child:
- 1:7e052cf2f2ee
tested to set the address
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wbeaumont | 0:d97e8b3802b3 | 1 | #include "mbed.h" |
wbeaumont | 0:d97e8b3802b3 | 2 | #include "I2C.h" |
wbeaumont | 0:d97e8b3802b3 | 3 | #define TARGET TARGET_KL05Z |
wbeaumont | 0:d97e8b3802b3 | 4 | #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z) |
wbeaumont | 0:d97e8b3802b3 | 5 | PinName const SDA = PTE25; |
wbeaumont | 0:d97e8b3802b3 | 6 | PinName const SCL = PTE24; |
wbeaumont | 0:d97e8b3802b3 | 7 | #elif defined (TARGET_KL05Z) |
wbeaumont | 0:d97e8b3802b3 | 8 | PinName const SDA = PTB4; |
wbeaumont | 0:d97e8b3802b3 | 9 | PinName const SCL = PTB3; |
wbeaumont | 0:d97e8b3802b3 | 10 | #elif defined (TARGET_K20D50M) |
wbeaumont | 0:d97e8b3802b3 | 11 | PinName const SDA = PTB1; |
wbeaumont | 0:d97e8b3802b3 | 12 | PinName const SCL = PTB0; |
wbeaumont | 0:d97e8b3802b3 | 13 | #else |
wbeaumont | 0:d97e8b3802b3 | 14 | #error TARGET NOT DEFINED |
wbeaumont | 0:d97e8b3802b3 | 15 | #endif |
wbeaumont | 0:d97e8b3802b3 | 16 | |
wbeaumont | 0:d97e8b3802b3 | 17 | #define MCP4728_readaddresscmd 0xC |
wbeaumont | 0:d97e8b3802b3 | 18 | #define MCP4728_baseaddr 0xC0 |
wbeaumont | 0:d97e8b3802b3 | 19 | |
wbeaumont | 0:d97e8b3802b3 | 20 | |
wbeaumont | 0:d97e8b3802b3 | 21 | class I2CInterface { |
wbeaumont | 0:d97e8b3802b3 | 22 | public : |
wbeaumont | 0:d97e8b3802b3 | 23 | I2CInterface(){}; //Create an I2C Master interface |
wbeaumont | 0:d97e8b3802b3 | 24 | virtual void frequency (int hz){};// Set the frequency of the I2C interface. |
wbeaumont | 0:d97e8b3802b3 | 25 | virtual int read (int address, char *data, int length, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 26 | return 0;};//Read from an I2C slave. |
wbeaumont | 0:d97e8b3802b3 | 27 | virtual int read (int ack){return 0;};// Read a single byte from the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 28 | virtual int write (int address, const char *data, int length, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 29 | return 0; |
wbeaumont | 0:d97e8b3802b3 | 30 | };// Write to an I2C slave. |
wbeaumont | 0:d97e8b3802b3 | 31 | virtual int write (int data){return 0;};// Write single byte out on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 32 | virtual void start (void){};// Creates a start condition on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 33 | virtual void stop (void){};// Creates a stop condition on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 34 | virtual int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event=I2C_EVENT_TRANSFER_COMPLETE, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 35 | return 0; |
wbeaumont | 0:d97e8b3802b3 | 36 | }; // Start non-blocking I2C transfer. |
wbeaumont | 0:d97e8b3802b3 | 37 | |
wbeaumont | 0:d97e8b3802b3 | 38 | }; |
wbeaumont | 0:d97e8b3802b3 | 39 | |
wbeaumont | 0:d97e8b3802b3 | 40 | class MBEDI2CInterface :public I2CInterface { |
wbeaumont | 0:d97e8b3802b3 | 41 | |
wbeaumont | 0:d97e8b3802b3 | 42 | I2C i2cdev; |
wbeaumont | 0:d97e8b3802b3 | 43 | public : |
wbeaumont | 0:d97e8b3802b3 | 44 | |
wbeaumont | 0:d97e8b3802b3 | 45 | MBEDI2CInterface(PinName sda, PinName scl): i2cdev(sda,scl){} ; |
wbeaumont | 0:d97e8b3802b3 | 46 | // next could perhaps more efficient but not yet investigated |
wbeaumont | 0:d97e8b3802b3 | 47 | virtual void frequency (int hz){return i2cdev.frequency(hz) ;}; |
wbeaumont | 0:d97e8b3802b3 | 48 | virtual int read (int address, char *data, int length, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 49 | return i2cdev.read ( address, data, length, repeated);}; |
wbeaumont | 0:d97e8b3802b3 | 50 | virtual int read (int ack){return i2cdev.read ( ack);};// Read a single byte from the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 51 | virtual int write (int address, const char *data, int length, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 52 | return i2cdev.write ( address, data, length, repeated); |
wbeaumont | 0:d97e8b3802b3 | 53 | } |
wbeaumont | 0:d97e8b3802b3 | 54 | virtual int write (int data){return i2cdev.write (data);};// Write single byte out on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 55 | virtual void start (void){i2cdev.start ();};// Creates a start condition on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 56 | virtual void stop (void){i2cdev.stop();};// Creates a stop condition on the I2C bus. |
wbeaumont | 0:d97e8b3802b3 | 57 | virtual int transfer (int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t &callback, int event=I2C_EVENT_TRANSFER_COMPLETE, bool repeated=false){ |
wbeaumont | 0:d97e8b3802b3 | 58 | return -1; // seems transfer not supported in mbed or not correctly called below |
wbeaumont | 0:d97e8b3802b3 | 59 | // return i2cdev.transfer (address, tx_buffer, tx_length, rx_buffer, rx_length, callback, event, repeated); |
wbeaumont | 0:d97e8b3802b3 | 60 | }; |
wbeaumont | 0:d97e8b3802b3 | 61 | |
wbeaumont | 0:d97e8b3802b3 | 62 | |
wbeaumont | 0:d97e8b3802b3 | 63 | } ; |
wbeaumont | 0:d97e8b3802b3 | 64 | |
wbeaumont | 0:d97e8b3802b3 | 65 | |
wbeaumont | 0:d97e8b3802b3 | 66 | MBEDI2CInterface mbedi2c( SDA, SCL); |
wbeaumont | 0:d97e8b3802b3 | 67 | |
wbeaumont | 0:d97e8b3802b3 | 68 | MBEDI2CInterface* mbedi2cp= &mbedi2c ; |
wbeaumont | 0:d97e8b3802b3 | 69 | |
wbeaumont | 0:d97e8b3802b3 | 70 | I2CInterface* i2cdev= mbedi2cp; |
wbeaumont | 0:d97e8b3802b3 | 71 | |
wbeaumont | 0:d97e8b3802b3 | 72 | |
wbeaumont | 0:d97e8b3802b3 | 73 | DigitalOut LDAC(PTA6); |
wbeaumont | 0:d97e8b3802b3 | 74 | DigitalOut CntPin(PTA7); |
wbeaumont | 0:d97e8b3802b3 | 75 | |
wbeaumont | 0:d97e8b3802b3 | 76 | |
wbeaumont | 0:d97e8b3802b3 | 77 | I2CInterface* i2cif; |
wbeaumont | 0:d97e8b3802b3 | 78 | |
wbeaumont | 0:d97e8b3802b3 | 79 | |
wbeaumont | 0:d97e8b3802b3 | 80 | class MPC4728_address_set{ |
wbeaumont | 0:d97e8b3802b3 | 81 | I2CInterface *i2cd; |
wbeaumont | 0:d97e8b3802b3 | 82 | DigitalOut *LDAC; |
wbeaumont | 0:d97e8b3802b3 | 83 | DigitalOut *Cntout; |
wbeaumont | 0:d97e8b3802b3 | 84 | InterruptIn cntin; |
wbeaumont | 0:d97e8b3802b3 | 85 | int sclcnt; |
wbeaumont | 0:d97e8b3802b3 | 86 | unsigned char oldaddress; |
wbeaumont | 0:d97e8b3802b3 | 87 | void count_down( ){ |
wbeaumont | 0:d97e8b3802b3 | 88 | sclcnt--; |
wbeaumont | 0:d97e8b3802b3 | 89 | *Cntout=1; |
wbeaumont | 0:d97e8b3802b3 | 90 | *Cntout=0; |
wbeaumont | 0:d97e8b3802b3 | 91 | if ( sclcnt==0){ |
wbeaumont | 0:d97e8b3802b3 | 92 | //disable this interrupt |
wbeaumont | 0:d97e8b3802b3 | 93 | int wait=10; while ( wait--); |
wbeaumont | 0:d97e8b3802b3 | 94 | *LDAC=0; |
wbeaumont | 0:d97e8b3802b3 | 95 | } |
wbeaumont | 0:d97e8b3802b3 | 96 | } |
wbeaumont | 0:d97e8b3802b3 | 97 | |
wbeaumont | 0:d97e8b3802b3 | 98 | public: |
wbeaumont | 0:d97e8b3802b3 | 99 | MPC4728_address_set(PinName sclcntpin, DigitalOut *LDACpin, I2CInterface* i2cdevice, DigitalOut *Cntoutpin ): cntin(sclcntpin) { |
wbeaumont | 0:d97e8b3802b3 | 100 | i2cd=i2cdevice; |
wbeaumont | 0:d97e8b3802b3 | 101 | LDAC=LDACpin; |
wbeaumont | 0:d97e8b3802b3 | 102 | Cntout=Cntoutpin; |
wbeaumont | 0:d97e8b3802b3 | 103 | *Cntoutpin=0; |
wbeaumont | 0:d97e8b3802b3 | 104 | *LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 105 | i2cd->frequency(40000); // interrupt is slow, so set to 40 kHz |
wbeaumont | 0:d97e8b3802b3 | 106 | |
wbeaumont | 0:d97e8b3802b3 | 107 | } |
wbeaumont | 0:d97e8b3802b3 | 108 | |
wbeaumont | 0:d97e8b3802b3 | 109 | |
wbeaumont | 0:d97e8b3802b3 | 110 | int getsclcnt( ){return sclcnt;} |
wbeaumont | 0:d97e8b3802b3 | 111 | |
wbeaumont | 0:d97e8b3802b3 | 112 | int readaddress(char* address, char *eepromaddr=0 , char *regaddr=0 ) { |
wbeaumont | 0:d97e8b3802b3 | 113 | *LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 114 | start_scl_cnt(18);// 1+9+8 faling edges |
wbeaumont | 0:d97e8b3802b3 | 115 | *address=0x0C; // not allowed value |
wbeaumont | 0:d97e8b3802b3 | 116 | char cmd = MCP4728_readaddresscmd; |
wbeaumont | 0:d97e8b3802b3 | 117 | int i2cresult=i2cd->write(0, &cmd, 1,true ); //1 byte don't send stop |
wbeaumont | 0:d97e8b3802b3 | 118 | if ( i2cresult ) { // <> 0 |
wbeaumont | 0:d97e8b3802b3 | 119 | //i2cd->abort_transfer ( ); |
wbeaumont | 0:d97e8b3802b3 | 120 | i2cd->stop(); |
wbeaumont | 0:d97e8b3802b3 | 121 | *address=0xFF; |
wbeaumont | 0:d97e8b3802b3 | 122 | return i2cresult ; |
wbeaumont | 0:d97e8b3802b3 | 123 | } |
wbeaumont | 0:d97e8b3802b3 | 124 | i2cresult=i2cd->read(MCP4728_baseaddr, &cmd, 1,false ); |
wbeaumont | 0:d97e8b3802b3 | 125 | if ( i2cresult) *address=0xFE; |
wbeaumont | 0:d97e8b3802b3 | 126 | else { |
wbeaumont | 0:d97e8b3802b3 | 127 | *address=cmd; |
wbeaumont | 0:d97e8b3802b3 | 128 | *regaddr= cmd >>1; |
wbeaumont | 0:d97e8b3802b3 | 129 | *regaddr= *regaddr& 0x7; |
wbeaumont | 0:d97e8b3802b3 | 130 | *eepromaddr = cmd>>5; |
wbeaumont | 0:d97e8b3802b3 | 131 | *eepromaddr = *eepromaddr & 0x7 ; |
wbeaumont | 0:d97e8b3802b3 | 132 | } |
wbeaumont | 0:d97e8b3802b3 | 133 | *LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 134 | return i2cresult ; |
wbeaumont | 0:d97e8b3802b3 | 135 | } |
wbeaumont | 0:d97e8b3802b3 | 136 | |
wbeaumont | 0:d97e8b3802b3 | 137 | |
wbeaumont | 0:d97e8b3802b3 | 138 | int setaddress(char currentaddress, char newaddress ){ // both address |
wbeaumont | 0:d97e8b3802b3 | 139 | *LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 140 | char oldaddr=0x7 & currentaddress;oldaddr=oldaddr<<1; |
wbeaumont | 0:d97e8b3802b3 | 141 | char newaddr=0x7 & newaddress; |
wbeaumont | 0:d97e8b3802b3 | 142 | newaddr= newaddr<<2; |
wbeaumont | 0:d97e8b3802b3 | 143 | char address=MCP4728_baseaddr | oldaddr;oldaddr=oldaddr<<1; |
wbeaumont | 0:d97e8b3802b3 | 144 | const char cmdtype= 0x60; // 0x011000000 0x011AAA0 |
wbeaumont | 0:d97e8b3802b3 | 145 | char data[3] ; |
wbeaumont | 0:d97e8b3802b3 | 146 | data[0]= cmdtype | oldaddr | 1; //0x011OOO01 |
wbeaumont | 0:d97e8b3802b3 | 147 | data[1]= cmdtype | newaddr | 2; //0x011NNN10 |
wbeaumont | 0:d97e8b3802b3 | 148 | data[2]= cmdtype | newaddr | 3; //0x011NNN11 |
wbeaumont | 0:d97e8b3802b3 | 149 | printf( "cmd %x %x data %x %x %x \n\r", newaddress, newaddr,(int)data[0],(int)data[1],(int)data[2]); |
wbeaumont | 0:d97e8b3802b3 | 150 | start_scl_cnt(18);//1+ 9+8 faling edges |
wbeaumont | 0:d97e8b3802b3 | 151 | int i2cresult=i2cd->write(address, data, 3,false ); //3 bytes sent and stop |
wbeaumont | 0:d97e8b3802b3 | 152 | *LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 153 | return i2cresult ; |
wbeaumont | 0:d97e8b3802b3 | 154 | } |
wbeaumont | 0:d97e8b3802b3 | 155 | |
wbeaumont | 0:d97e8b3802b3 | 156 | void start_scl_cnt(int cnts) { |
wbeaumont | 0:d97e8b3802b3 | 157 | sclcnt= cnts; |
wbeaumont | 0:d97e8b3802b3 | 158 | cntin.fall(this, &MPC4728_address_set::count_down); |
wbeaumont | 0:d97e8b3802b3 | 159 | } |
wbeaumont | 0:d97e8b3802b3 | 160 | |
wbeaumont | 0:d97e8b3802b3 | 161 | };//endclass |
wbeaumont | 0:d97e8b3802b3 | 162 | |
wbeaumont | 0:d97e8b3802b3 | 163 | int main(void){ |
wbeaumont | 0:d97e8b3802b3 | 164 | printf("\n\rMCP4728 set address program date : %s time :%s\n\r",__DATE__,__TIME__); |
wbeaumont | 0:d97e8b3802b3 | 165 | |
wbeaumont | 0:d97e8b3802b3 | 166 | LDAC=1; |
wbeaumont | 0:d97e8b3802b3 | 167 | char oldaddr, eeaddr, regaddr, newaddr= 2; |
wbeaumont | 0:d97e8b3802b3 | 168 | int newaddrint; |
wbeaumont | 0:d97e8b3802b3 | 169 | MPC4728_address_set mpc4728(PTB0,&LDAC,i2cdev, &CntPin); |
wbeaumont | 0:d97e8b3802b3 | 170 | int i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); |
wbeaumont | 0:d97e8b3802b3 | 171 | if ( i2cresult ) { |
wbeaumont | 0:d97e8b3802b3 | 172 | printf("failed to read current address error code: %d , scl cnt = %d \n", i2cresult,mpc4728.getsclcnt() ); |
wbeaumont | 0:d97e8b3802b3 | 173 | } |
wbeaumont | 0:d97e8b3802b3 | 174 | else { |
wbeaumont | 0:d97e8b3802b3 | 175 | printf( " current address = %d ,eeaddr %d , regaddr %d sclcnt= %d give new address : ", (int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); |
wbeaumont | 0:d97e8b3802b3 | 176 | scanf( "%d",&newaddrint); |
wbeaumont | 0:d97e8b3802b3 | 177 | printf("\n\r"); |
wbeaumont | 0:d97e8b3802b3 | 178 | if ( newaddrint >= 0 && newaddrint <= 8 ) { |
wbeaumont | 0:d97e8b3802b3 | 179 | newaddr= (char) newaddrint; |
wbeaumont | 0:d97e8b3802b3 | 180 | i2cresult=mpc4728.setaddress(oldaddr,newaddr); |
wbeaumont | 0:d97e8b3802b3 | 181 | if ( i2cresult ) printf("failed to set address to %d error : %d , sclcnt= %d \n",newaddr, i2cresult,mpc4728.getsclcnt()); |
wbeaumont | 0:d97e8b3802b3 | 182 | else { |
wbeaumont | 0:d97e8b3802b3 | 183 | printf("set new address \n"); |
wbeaumont | 0:d97e8b3802b3 | 184 | i2cresult =mpc4728.readaddress(&oldaddr, &eeaddr, ®addr); |
wbeaumont | 0:d97e8b3802b3 | 185 | if ( i2cresult ) printf("failed to read (new) address error : %d \n", i2cresult); |
wbeaumont | 0:d97e8b3802b3 | 186 | else printf( "new address = %d ,eeaddr %d , regaddr %d sclcnt= %d \n\r", (int) oldaddr,(int) eeaddr, (int)regaddr, mpc4728.getsclcnt()); printf( " new address = %d \n", (int) oldaddr); |
wbeaumont | 0:d97e8b3802b3 | 187 | } |
wbeaumont | 0:d97e8b3802b3 | 188 | } |
wbeaumont | 0:d97e8b3802b3 | 189 | else { |
wbeaumont | 0:d97e8b3802b3 | 190 | printf( " new address %d is not valid reset the up to restart the program \n", (int)newaddr ); |
wbeaumont | 0:d97e8b3802b3 | 191 | } |
wbeaumont | 0:d97e8b3802b3 | 192 | } |
wbeaumont | 0:d97e8b3802b3 | 193 | printf("restart !!!! \n\r"); |
wbeaumont | 0:d97e8b3802b3 | 194 | while (1); |
wbeaumont | 0:d97e8b3802b3 | 195 | //return 0; // never reach this |
wbeaumont | 0:d97e8b3802b3 | 196 | }; |
wbeaumont | 0:d97e8b3802b3 | 197 |