A hacked library to offer I2C functionality similar to the standard mbed library for I2C1 only. Is seemingly required when using the v29 beta mbed library as of 12.05.2011, as the I2C does not seem to work otherwise. Has been developed and tested with the ADXL345 accelerometer.

Dependents:   SensorsThingSpeak

Committer:
ShockSoc
Date:
Thu May 12 09:24:27 2011 +0000
Revision:
0:375ecd0ed73d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ShockSoc 0:375ecd0ed73d 1 #include "I2CR.h"
ShockSoc 0:375ecd0ed73d 2
ShockSoc 0:375ecd0ed73d 3 //Constructor
ShockSoc 0:375ecd0ed73d 4 I2CR::I2CR() {
ShockSoc 0:375ecd0ed73d 5
ShockSoc 0:375ecd0ed73d 6 //set pins to i2c1
ShockSoc 0:375ecd0ed73d 7 LPC_PINCON->PINSEL0 = LPC_PINCON->PINSEL0 | 0xF;
ShockSoc 0:375ecd0ed73d 8 //set to no pull up/down resistors
ShockSoc 0:375ecd0ed73d 9 LPC_PINCON->PINMODE0 = LPC_PINCON->PINMODE0 | 0xA;
ShockSoc 0:375ecd0ed73d 10 //set to open drain mode
ShockSoc 0:375ecd0ed73d 11 LPC_PINCON->PINMODE_OD0 = LPC_PINCON->PINMODE_OD0 | 0x3;
ShockSoc 0:375ecd0ed73d 12
ShockSoc 0:375ecd0ed73d 13 //turn on power
ShockSoc 0:375ecd0ed73d 14 LPC_SC->PCONP = LPC_SC->PCONP | 0x00080000;
ShockSoc 0:375ecd0ed73d 15
ShockSoc 0:375ecd0ed73d 16 //set clock to 100kHz
ShockSoc 0:375ecd0ed73d 17 LPC_I2C1->I2SCLL = 120;
ShockSoc 0:375ecd0ed73d 18 LPC_I2C1->I2SCLH = 120;
ShockSoc 0:375ecd0ed73d 19
ShockSoc 0:375ecd0ed73d 20 //enable i2c1 i2en
ShockSoc 0:375ecd0ed73d 21 LPC_I2C1->I2CONSET = 0x40;
ShockSoc 0:375ecd0ed73d 22 //clear sta si and aa
ShockSoc 0:375ecd0ed73d 23 LPC_I2C1->I2CONCLR = 0x2C;
ShockSoc 0:375ecd0ed73d 24
ShockSoc 0:375ecd0ed73d 25
ShockSoc 0:375ecd0ed73d 26 }
ShockSoc 0:375ecd0ed73d 27
ShockSoc 0:375ecd0ed73d 28 //Write function
ShockSoc 0:375ecd0ed73d 29 int I2CR::write(int address, const char* data, int length) {
ShockSoc 0:375ecd0ed73d 30 //MASTER TRANSMITTER
ShockSoc 0:375ecd0ed73d 31
ShockSoc 0:375ecd0ed73d 32 //send start condition
ShockSoc 0:375ecd0ed73d 33 LPC_I2C1->I2CONSET = 0x20;
ShockSoc 0:375ecd0ed73d 34
ShockSoc 0:375ecd0ed73d 35 //block until interrupt
ShockSoc 0:375ecd0ed73d 36 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 37
ShockSoc 0:375ecd0ed73d 38 //i2stat should be 0x08. could check
ShockSoc 0:375ecd0ed73d 39 if (LPC_I2C1->I2STAT != 0x08) {
ShockSoc 0:375ecd0ed73d 40 printf("START %x\r\n",LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 41 //stop if failed
ShockSoc 0:375ecd0ed73d 42 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 43 return -1;
ShockSoc 0:375ecd0ed73d 44 }
ShockSoc 0:375ecd0ed73d 45
ShockSoc 0:375ecd0ed73d 46 //clear start condition
ShockSoc 0:375ecd0ed73d 47 LPC_I2C1->I2CONCLR = 0x20;
ShockSoc 0:375ecd0ed73d 48
ShockSoc 0:375ecd0ed73d 49 //load i2dat
ShockSoc 0:375ecd0ed73d 50 LPC_I2C1->I2DAT = address;
ShockSoc 0:375ecd0ed73d 51
ShockSoc 0:375ecd0ed73d 52 //reset si
ShockSoc 0:375ecd0ed73d 53 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 54
ShockSoc 0:375ecd0ed73d 55 //block until interrupt
ShockSoc 0:375ecd0ed73d 56 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 57
ShockSoc 0:375ecd0ed73d 58 //i2stat should be ack received
ShockSoc 0:375ecd0ed73d 59 if (LPC_I2C1->I2STAT != 0x18) {
ShockSoc 0:375ecd0ed73d 60 printf("ADD %x\r\n",LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 61 //stop
ShockSoc 0:375ecd0ed73d 62 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 63 return -1;
ShockSoc 0:375ecd0ed73d 64 }
ShockSoc 0:375ecd0ed73d 65
ShockSoc 0:375ecd0ed73d 66 for (int i=0; i<length; i++) {
ShockSoc 0:375ecd0ed73d 67
ShockSoc 0:375ecd0ed73d 68 //load i2dat
ShockSoc 0:375ecd0ed73d 69 LPC_I2C1->I2DAT = data[i];
ShockSoc 0:375ecd0ed73d 70
ShockSoc 0:375ecd0ed73d 71 //reset si
ShockSoc 0:375ecd0ed73d 72 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 73
ShockSoc 0:375ecd0ed73d 74 //block until interrupt
ShockSoc 0:375ecd0ed73d 75 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 76
ShockSoc 0:375ecd0ed73d 77 //i2stat should be ack received
ShockSoc 0:375ecd0ed73d 78 if (LPC_I2C1->I2STAT != 0x28) {
ShockSoc 0:375ecd0ed73d 79 printf("DATA%d %x\r\n",i,LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 80 //stop
ShockSoc 0:375ecd0ed73d 81 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 82 return -1;
ShockSoc 0:375ecd0ed73d 83 }
ShockSoc 0:375ecd0ed73d 84 }
ShockSoc 0:375ecd0ed73d 85
ShockSoc 0:375ecd0ed73d 86 //stop
ShockSoc 0:375ecd0ed73d 87 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 88
ShockSoc 0:375ecd0ed73d 89 //reset si
ShockSoc 0:375ecd0ed73d 90 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 91
ShockSoc 0:375ecd0ed73d 92 return 0;
ShockSoc 0:375ecd0ed73d 93 }
ShockSoc 0:375ecd0ed73d 94
ShockSoc 0:375ecd0ed73d 95
ShockSoc 0:375ecd0ed73d 96 int I2CR::read(int address, char* data, int length) {
ShockSoc 0:375ecd0ed73d 97
ShockSoc 0:375ecd0ed73d 98 //MASTER RECEIVER
ShockSoc 0:375ecd0ed73d 99
ShockSoc 0:375ecd0ed73d 100 //send start condition
ShockSoc 0:375ecd0ed73d 101 LPC_I2C1->I2CONSET = 0x20;
ShockSoc 0:375ecd0ed73d 102
ShockSoc 0:375ecd0ed73d 103 //block until interrupt
ShockSoc 0:375ecd0ed73d 104 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 105
ShockSoc 0:375ecd0ed73d 106 //i2stat should be 0x08. could check
ShockSoc 0:375ecd0ed73d 107 if (LPC_I2C1->I2STAT != 0x08) {
ShockSoc 0:375ecd0ed73d 108 printf("RSTART %x\r\n",LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 109 //stop if failed
ShockSoc 0:375ecd0ed73d 110 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 111 return -1;
ShockSoc 0:375ecd0ed73d 112 }
ShockSoc 0:375ecd0ed73d 113
ShockSoc 0:375ecd0ed73d 114 //clear start condition
ShockSoc 0:375ecd0ed73d 115 LPC_I2C1->I2CONCLR = 0x20;
ShockSoc 0:375ecd0ed73d 116
ShockSoc 0:375ecd0ed73d 117 //load i2dat with read bit set
ShockSoc 0:375ecd0ed73d 118 LPC_I2C1->I2DAT = address | 0x1;
ShockSoc 0:375ecd0ed73d 119
ShockSoc 0:375ecd0ed73d 120 //reset si
ShockSoc 0:375ecd0ed73d 121 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 122
ShockSoc 0:375ecd0ed73d 123 //block until interrupt
ShockSoc 0:375ecd0ed73d 124 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 125
ShockSoc 0:375ecd0ed73d 126 //i2stat should be 0x40 - ack received
ShockSoc 0:375ecd0ed73d 127 if (LPC_I2C1->I2STAT != 0x40) {
ShockSoc 0:375ecd0ed73d 128 printf("RADD %x\r\n",LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 129 //stop
ShockSoc 0:375ecd0ed73d 130 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 131 return -1;
ShockSoc 0:375ecd0ed73d 132 }
ShockSoc 0:375ecd0ed73d 133
ShockSoc 0:375ecd0ed73d 134 for (int i=0; i<length-1; i++) {
ShockSoc 0:375ecd0ed73d 135
ShockSoc 0:375ecd0ed73d 136 //ack next byte
ShockSoc 0:375ecd0ed73d 137 LPC_I2C1->I2CONSET = 0x4;
ShockSoc 0:375ecd0ed73d 138
ShockSoc 0:375ecd0ed73d 139 //reset si
ShockSoc 0:375ecd0ed73d 140 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 141
ShockSoc 0:375ecd0ed73d 142 //block until interrupt
ShockSoc 0:375ecd0ed73d 143 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 144
ShockSoc 0:375ecd0ed73d 145 //i2stat should be 0x50, byte received, ack sent
ShockSoc 0:375ecd0ed73d 146 if (LPC_I2C1->I2STAT != 0x50) {
ShockSoc 0:375ecd0ed73d 147 printf("rDATA%d %x\r\n",i,LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 148 //stop
ShockSoc 0:375ecd0ed73d 149 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 150 return -1;
ShockSoc 0:375ecd0ed73d 151 }
ShockSoc 0:375ecd0ed73d 152
ShockSoc 0:375ecd0ed73d 153 data[i] = LPC_I2C1->I2DAT;
ShockSoc 0:375ecd0ed73d 154 }
ShockSoc 0:375ecd0ed73d 155
ShockSoc 0:375ecd0ed73d 156 //last byte
ShockSoc 0:375ecd0ed73d 157 //nack next byte
ShockSoc 0:375ecd0ed73d 158 LPC_I2C1->I2CONCLR = 0x4;
ShockSoc 0:375ecd0ed73d 159
ShockSoc 0:375ecd0ed73d 160 //reset si
ShockSoc 0:375ecd0ed73d 161 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 162
ShockSoc 0:375ecd0ed73d 163 //block until interrupt
ShockSoc 0:375ecd0ed73d 164 while ((LPC_I2C1->I2CONSET & 0x8) != 0x8) {}
ShockSoc 0:375ecd0ed73d 165
ShockSoc 0:375ecd0ed73d 166 //i2stat should be 0x58, byte received, nack sent
ShockSoc 0:375ecd0ed73d 167 if (LPC_I2C1->I2STAT != 0x58) {
ShockSoc 0:375ecd0ed73d 168 printf("lastd %x\r\n",LPC_I2C1->I2STAT);
ShockSoc 0:375ecd0ed73d 169 //stop
ShockSoc 0:375ecd0ed73d 170 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 171 return -1;
ShockSoc 0:375ecd0ed73d 172 }
ShockSoc 0:375ecd0ed73d 173
ShockSoc 0:375ecd0ed73d 174 data[length-1] = LPC_I2C1->I2DAT;
ShockSoc 0:375ecd0ed73d 175
ShockSoc 0:375ecd0ed73d 176 //stop
ShockSoc 0:375ecd0ed73d 177 LPC_I2C1->I2CONSET = 0x10;
ShockSoc 0:375ecd0ed73d 178
ShockSoc 0:375ecd0ed73d 179 //reset si
ShockSoc 0:375ecd0ed73d 180 LPC_I2C1->I2CONCLR = 0x08;
ShockSoc 0:375ecd0ed73d 181
ShockSoc 0:375ecd0ed73d 182 return 0;
ShockSoc 0:375ecd0ed73d 183
ShockSoc 0:375ecd0ed73d 184 }