Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LG by
DeviceI2C.c@161:efd949e8d536, 2016-05-15 (annotated)
- Committer:
 - Diletant
 - Date:
 - Sun May 15 19:12:12 2016 +0000
 - Revision:
 - 161:efd949e8d536
 - Parent:
 - 156:e68ee0bcdcda
 - Child:
 - 167:bedc0a9d559a
 
Device & ... update. Not final!!!
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| Diletant | 156:e68ee0bcdcda | 1 | #include "Device.h" | 
| Diletant | 156:e68ee0bcdcda | 2 | |
| Diletant | 156:e68ee0bcdcda | 3 | //I2C Control Set Register | 
| Diletant | 156:e68ee0bcdcda | 4 | #define I2CONSET_I2EN 0x0000040 | 
| Diletant | 156:e68ee0bcdcda | 5 | #define I2CONSET_AA 0x0000004 | 
| Diletant | 156:e68ee0bcdcda | 6 | #define I2CONSET_SI 0x0000008 | 
| Diletant | 156:e68ee0bcdcda | 7 | #define I2CONSET_STO 0x0000010 | 
| Diletant | 156:e68ee0bcdcda | 8 | #define I2CONSET_STA 0x0000020 | 
| Diletant | 156:e68ee0bcdcda | 9 | //I2C Control clear Register | 
| Diletant | 156:e68ee0bcdcda | 10 | #define I2CONCLR_AAC 0x0000004 | 
| Diletant | 156:e68ee0bcdcda | 11 | #define I2CONCLR_SIC 0x0000008 | 
| Diletant | 156:e68ee0bcdcda | 12 | #define I2CONCLR_STAC 0x0000020 | 
| Diletant | 156:e68ee0bcdcda | 13 | #define I2CONCLR_I2ENC 0x0000040 | 
| Diletant | 156:e68ee0bcdcda | 14 | //I2C SCL Duty Cycle High Reg | 
| Diletant | 156:e68ee0bcdcda | 15 | #define I2SCLH_SCLH 0x00000080 //High | 
| Diletant | 156:e68ee0bcdcda | 16 | #define I2SCLL_SCLL 0x00000080 //Low | 
| Diletant | 156:e68ee0bcdcda | 17 | //Address | 
| Diletant | 156:e68ee0bcdcda | 18 | #define I2C_A_ADDRESS 0x0000005E //address of slave potentiometer A (table 6-2 of pot's manual) | 
| Diletant | 156:e68ee0bcdcda | 19 | #define I2C_B_ADDRESS 0x0000005C //address of slave potentiometer B (table 6-2 of pot's manual) | 
| Diletant | 156:e68ee0bcdcda | 20 | #define I2C_WRITE_CMD 0x00000000 //address of wiper0 register in memory map | 
| Diletant | 156:e68ee0bcdcda | 21 | //Timeout | 
| Diletant | 156:e68ee0bcdcda | 22 | #define I2C_TIMEOUT 1000 | 
| Diletant | 156:e68ee0bcdcda | 23 | |
| Diletant | 156:e68ee0bcdcda | 24 | extern Device device; | 
| Diletant | 156:e68ee0bcdcda | 25 | |
| Diletant | 156:e68ee0bcdcda | 26 | // | 
| Diletant | 156:e68ee0bcdcda | 27 | //Warning!!! | 
| Diletant | 156:e68ee0bcdcda | 28 | // Different LPC17xx.h versions declare different I2C registers names: | 
| Diletant | 156:e68ee0bcdcda | 29 | // LPC_I2C0->CONCLR (Keil?)/ LPC_I2C0->I2CONCLR (mbed) | 
| Diletant | 156:e68ee0bcdcda | 30 | // ... | 
| Diletant | 156:e68ee0bcdcda | 31 | // | 
| Diletant | 156:e68ee0bcdcda | 32 | |
| Diletant | 156:e68ee0bcdcda | 33 | void InitI2CWithDefaults(void){ | 
| Diletant | 156:e68ee0bcdcda | 34 | device.controller.I2C.settings.trigger = 1000; | 
| Diletant | 156:e68ee0bcdcda | 35 | } | 
| Diletant | 156:e68ee0bcdcda | 36 | |
| Diletant | 156:e68ee0bcdcda | 37 | void InitI2C(void) { | 
| Diletant | 156:e68ee0bcdcda | 38 | device.controller.I2C.state.CON0 = 0; | 
| Diletant | 156:e68ee0bcdcda | 39 | device.controller.I2C.state.buffer[0] = I2C_A_ADDRESS; | 
| Diletant | 156:e68ee0bcdcda | 40 | device.controller.I2C.state.buffer[1] = I2C_WRITE_CMD; | 
| Diletant | 161:efd949e8d536 | 41 | device.controller.I2C.state.buffer[2] = device.isacs.potentiometers.settings.a; | 
| Diletant | 156:e68ee0bcdcda | 42 | device.controller.I2C.state.buffer[3] = I2C_B_ADDRESS; | 
| Diletant | 156:e68ee0bcdcda | 43 | device.controller.I2C.state.buffer[4] = I2C_WRITE_CMD; | 
| Diletant | 161:efd949e8d536 | 44 | device.controller.I2C.state.buffer[5] = device.isacs.potentiometers.settings.b; | 
| Diletant | 156:e68ee0bcdcda | 45 | device.controller.I2C.state.position = 0; | 
| Diletant | 156:e68ee0bcdcda | 46 | device.controller.I2C.state.counter = 0; | 
| Diletant | 156:e68ee0bcdcda | 47 | device.controller.I2C.state.trigger = device.controller.I2C.settings.trigger; | 
| Diletant | 156:e68ee0bcdcda | 48 | |
| Diletant | 156:e68ee0bcdcda | 49 | LPC_SC->PCONP |= (1 << 7);//I2C0 on | 
| Diletant | 156:e68ee0bcdcda | 50 | //LPC_SC->PCONP |= (1 << 19);//I2C1 on | 
| Diletant | 156:e68ee0bcdcda | 51 | |
| Diletant | 156:e68ee0bcdcda | 52 | // set PIO0.27 and PIO0.28 to I2C0 SDA and SCK | 
| Diletant | 156:e68ee0bcdcda | 53 | // function to 01 on both SDA and SCK | 
| Diletant | 156:e68ee0bcdcda | 54 | LPC_PINCON->PINSEL1 &= ~0x03C00000; //P0.27 - SDA | 
| Diletant | 156:e68ee0bcdcda | 55 | LPC_PINCON->PINSEL1 |= 0x01400000; //P0.28 - SCK | 
| Diletant | 156:e68ee0bcdcda | 56 | |
| Diletant | 156:e68ee0bcdcda | 57 | LPC_I2C0->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC; //Master mode | 
| Diletant | 156:e68ee0bcdcda | 58 | //LPC_I2C0->I2CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC; //Master mode | 
| Diletant | 156:e68ee0bcdcda | 59 | |
| Diletant | 156:e68ee0bcdcda | 60 | LPC_I2C0->SCLL = I2SCLL_SCLL; // - счетчик scl low time period | 
| Diletant | 156:e68ee0bcdcda | 61 | //LPC_I2C0->I2SCLL = I2SCLL_SCLL; // - счетчик scl low time period | 
| Diletant | 156:e68ee0bcdcda | 62 | LPC_I2C0->SCLH = I2SCLH_SCLH; // - счетчик scl high time period | 
| Diletant | 156:e68ee0bcdcda | 63 | //LPC_I2C0->I2SCLH = I2SCLH_SCLH; // - счетчик scl high time period | 
| Diletant | 156:e68ee0bcdcda | 64 | |
| Diletant | 156:e68ee0bcdcda | 65 | LPC_I2C0->CONSET = I2CONSET_I2EN; //включение интерфейса I2C | 
| Diletant | 156:e68ee0bcdcda | 66 | //LPC_I2C0->I2CONSET = I2CONSET_I2EN; //включение интерфейса I2C | 
| Diletant | 156:e68ee0bcdcda | 67 | |
| Diletant | 156:e68ee0bcdcda | 68 | device.controller.I2C.state.CON0 = LPC_I2C0->CONSET & (~LPC_I2C0->CONCLR); | 
| Diletant | 156:e68ee0bcdcda | 69 | //device.controller.I2C.state.CON0 = LPC_I2C0->I2CONSET & (~LPC_I2C0->I2CONCLR); | 
| Diletant | 156:e68ee0bcdcda | 70 | } | 
| Diletant | 156:e68ee0bcdcda | 71 | |
| Diletant | 156:e68ee0bcdcda | 72 | void DeviceI2CTransmit(void) { | 
| Diletant | 156:e68ee0bcdcda | 73 | if (!device.controller.I2C.state.enabled) return; //Transmission disabled, go away | 
| Diletant | 156:e68ee0bcdcda | 74 | |
| Diletant | 156:e68ee0bcdcda | 75 | device.controller.I2C.state.counter++; | 
| Diletant | 156:e68ee0bcdcda | 76 | if (device.controller.I2C.state.counter > device.controller.I2C.state.trigger){//Time out, go away | 
| Diletant | 156:e68ee0bcdcda | 77 | device.controller.I2C.state.counter = 0; | 
| Diletant | 156:e68ee0bcdcda | 78 | device.controller.I2C.state.enabled = 0; | 
| Diletant | 156:e68ee0bcdcda | 79 | LPC_I2C0->CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 80 | //LPC_I2C0->I2CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 81 | LPC_I2C0->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC; | 
| Diletant | 156:e68ee0bcdcda | 82 | //LPC_I2C0->I2CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC; | 
| Diletant | 156:e68ee0bcdcda | 83 | return; | 
| Diletant | 156:e68ee0bcdcda | 84 | } | 
| Diletant | 156:e68ee0bcdcda | 85 | |
| Diletant | 156:e68ee0bcdcda | 86 | if (!(LPC_I2C0->CONSET & I2CONSET_SI)) return; //State of I2C bus has not been changed | 
| Diletant | 156:e68ee0bcdcda | 87 | //if (!(LPC_I2C0->I2CONSET & I2CONSET_SI)) return; //State of I2C bus has not been changed | 
| Diletant | 156:e68ee0bcdcda | 88 | |
| Diletant | 156:e68ee0bcdcda | 89 | uint32_t StatValue = LPC_I2C0->STAT; | 
| Diletant | 156:e68ee0bcdcda | 90 | //uint32_t StatValue = LPC_I2C0->I2STAT; | 
| Diletant | 156:e68ee0bcdcda | 91 | switch ( StatValue ) { | 
| Diletant | 156:e68ee0bcdcda | 92 | //Start condition issued (write data for the first potentiometer) | 
| Diletant | 156:e68ee0bcdcda | 93 | case 0x08: | 
| Diletant | 156:e68ee0bcdcda | 94 | device.controller.I2C.state.position = 0; //Init buffer position | 
| Diletant | 156:e68ee0bcdcda | 95 | device.controller.I2C.state.counter = 0; //Init countdown | 
| Diletant | 156:e68ee0bcdcda | 96 | LPC_I2C0->DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send address | 
| Diletant | 156:e68ee0bcdcda | 97 | //LPC_I2C0->I2DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send address | 
| Diletant | 156:e68ee0bcdcda | 98 | LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //Clear interrupt bit and start bit | 
| Diletant | 156:e68ee0bcdcda | 99 | //LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //Clear interrupt bit and start bit | 
| Diletant | 156:e68ee0bcdcda | 100 | break; | 
| Diletant | 156:e68ee0bcdcda | 101 | |
| Diletant | 156:e68ee0bcdcda | 102 | //Repeated started is issued (write data for the second potentiometer) | 
| Diletant | 156:e68ee0bcdcda | 103 | case 0x10: | 
| Diletant | 156:e68ee0bcdcda | 104 | LPC_I2C0->DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send address | 
| Diletant | 156:e68ee0bcdcda | 105 | //LPC_I2C0->I2DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send address | 
| Diletant | 156:e68ee0bcdcda | 106 | LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //Clear interrupt bit and start bit | 
| Diletant | 156:e68ee0bcdcda | 107 | //LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); //Clear interrupt bit and start bit | 
| Diletant | 156:e68ee0bcdcda | 108 | break; | 
| Diletant | 156:e68ee0bcdcda | 109 | |
| Diletant | 156:e68ee0bcdcda | 110 | //ACK after slave address reading | 
| Diletant | 156:e68ee0bcdcda | 111 | case 0x18: | 
| Diletant | 156:e68ee0bcdcda | 112 | LPC_I2C0->DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send another byte | 
| Diletant | 156:e68ee0bcdcda | 113 | //LPC_I2C0->I2DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++]; //Send another byte | 
| Diletant | 156:e68ee0bcdcda | 114 | LPC_I2C0->CONCLR = I2CONCLR_SIC; //Clear interrupt bit | 
| Diletant | 156:e68ee0bcdcda | 115 | //LPC_I2C0->I2CONCLR = I2CONCLR_SIC; //Clear interrupt bit | 
| Diletant | 156:e68ee0bcdcda | 116 | break; | 
| Diletant | 156:e68ee0bcdcda | 117 | |
| Diletant | 156:e68ee0bcdcda | 118 | //ACK after data byte | 
| Diletant | 156:e68ee0bcdcda | 119 | case 0x28: | 
| Diletant | 156:e68ee0bcdcda | 120 | if (device.controller.I2C.state.position == I2C_BUFFER_LENGTH) { //Data transmission finished | 
| Diletant | 156:e68ee0bcdcda | 121 | device.controller.I2C.state.enabled = 0; | 
| Diletant | 156:e68ee0bcdcda | 122 | LPC_I2C0->CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 123 | //LPC_I2C0->I2CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 124 | //Output.Str.Cnt_Dif = 300; | 
| Diletant | 156:e68ee0bcdcda | 125 | } else if (device.controller.I2C.state.position == 3) { | 
| Diletant | 156:e68ee0bcdcda | 126 | LPC_I2C0->CONSET = I2CONSET_STA; | 
| Diletant | 156:e68ee0bcdcda | 127 | //LPC_I2C0->I2CONSET = I2CONSET_STA; | 
| Diletant | 156:e68ee0bcdcda | 128 | //Output.Str.Cnt_Dif = 200; | 
| Diletant | 156:e68ee0bcdcda | 129 | } else { | 
| Diletant | 156:e68ee0bcdcda | 130 | LPC_I2C0->DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++];//Send another byte | 
| Diletant | 156:e68ee0bcdcda | 131 | //LPC_I2C0->I2DAT = device.controller.I2C.state.buffer[device.controller.I2C.state.position++];//Send another byte | 
| Diletant | 156:e68ee0bcdcda | 132 | //Output.Str.Cnt_Dif = 100; | 
| Diletant | 156:e68ee0bcdcda | 133 | } | 
| Diletant | 156:e68ee0bcdcda | 134 | LPC_I2C0->CONCLR = I2CONCLR_SIC; //Clear interrupt bit | 
| Diletant | 156:e68ee0bcdcda | 135 | //LPC_I2C0->I2CONCLR = I2CONCLR_SIC; //Clear interrupt bit | 
| Diletant | 156:e68ee0bcdcda | 136 | break; | 
| Diletant | 156:e68ee0bcdcda | 137 | |
| Diletant | 156:e68ee0bcdcda | 138 | //No acknoledgement after address transmitting | 
| Diletant | 156:e68ee0bcdcda | 139 | case 0x20: | 
| Diletant | 156:e68ee0bcdcda | 140 | //No acknoledgement after data block transmitting | 
| Diletant | 156:e68ee0bcdcda | 141 | case 0x30: | 
| Diletant | 156:e68ee0bcdcda | 142 | LPC_I2C0->CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 143 | //LPC_I2C0->I2CONSET = I2CONSET_STO; //Set Stop flag | 
| Diletant | 156:e68ee0bcdcda | 144 | LPC_I2C0->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); | 
| Diletant | 156:e68ee0bcdcda | 145 | //LPC_I2C0->I2CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC); | 
| Diletant | 156:e68ee0bcdcda | 146 | device.controller.I2C.state.enabled = 0; //Disable transmission | 
| Diletant | 156:e68ee0bcdcda | 147 | break; | 
| Diletant | 156:e68ee0bcdcda | 148 | } | 
| Diletant | 156:e68ee0bcdcda | 149 | } | 
| Diletant | 156:e68ee0bcdcda | 150 | |
| Diletant | 156:e68ee0bcdcda | 151 | void StartI2CTransmission(void){ | 
| Diletant | 156:e68ee0bcdcda | 152 | device.controller.I2C.state.position = 0; | 
| Diletant | 156:e68ee0bcdcda | 153 | device.controller.I2C.state.counter = I2C_TIMEOUT; | 
| Diletant | 156:e68ee0bcdcda | 154 | LPC_I2C0->CONSET = I2CONSET_STA; //Set start flag | 
| Diletant | 156:e68ee0bcdcda | 155 | //LPC_I2C0->I2CONSET = I2CONSET_STA; //Set start flag | 
| Diletant | 156:e68ee0bcdcda | 156 | device.controller.I2C.state.enabled = 1; //Enable transmission | 
| Diletant | 156:e68ee0bcdcda | 157 | } | 
