only for LPC824 chip to match hardware I2C channels(2->4)

Dependencies:   mbed

Committer:
k4zuki
Date:
Thu Oct 23 16:18:03 2014 +0000
Revision:
4:bbea62f185e1
Parent:
3:8e33af9ea1f7
Child:
5:655e58295f1e
printf debug lines added(switch matrix, clock sourcing)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
k4zuki 0:9d12c615c8ea 1 #include "mbed.h"
k4zuki 0:9d12c615c8ea 2
k4zuki 0:9d12c615c8ea 3 Serial pc(USBTX,USBRX);
k4zuki 2:096289a04a1f 4 /*
k4zuki 2:096289a04a1f 5 D5 = P0_28,
k4zuki 2:096289a04a1f 6 D6 = P0_16, // LED_GREEN
k4zuki 2:096289a04a1f 7 D7 = P0_17,
k4zuki 2:096289a04a1f 8 D8 = P0_13,
k4zuki 2:096289a04a1f 9 D9 = P0_27, // LED_BLUE
k4zuki 2:096289a04a1f 10 D10 = P0_15,
k4zuki 2:096289a04a1f 11 D11 = P0_26,
k4zuki 2:096289a04a1f 12 D12 = P0_25,
k4zuki 2:096289a04a1f 13 D13 = P0_24,
k4zuki 2:096289a04a1f 14 */
k4zuki 4:bbea62f185e1 15 I2C dev1(I2C_SDA,I2C_SCL);//11,10 hard coded
k4zuki 4:bbea62f185e1 16 I2C dev2(A0,A1);//6,14
k4zuki 4:bbea62f185e1 17 I2C dev3(A2,A3);//23,22
k4zuki 4:bbea62f185e1 18 I2C dev4(A4,A5);//21,20
k4zuki 2:096289a04a1f 19 //BusOut bus(LED1,LED2,LED3,LED4);
k4zuki 2:096289a04a1f 20 DigitalOut led1(LED1);
k4zuki 0:9d12c615c8ea 21 //DigitalOut led2(LED2);
k4zuki 0:9d12c615c8ea 22 //DigitalOut led3(LED3);
k4zuki 0:9d12c615c8ea 23 //DigitalOut led4(LED4);
k4zuki 0:9d12c615c8ea 24
k4zuki 0:9d12c615c8ea 25 //Table 3. ASCII commands supported by SC18IM700
k4zuki 0:9d12c615c8ea 26 //ASCII command Hex value Command function
k4zuki 1:5817c3e6ce1e 27 //[X] S 0x53 I2C-bus START
k4zuki 1:5817c3e6ce1e 28 //[X] P 0x50 I2C-bus STOP
k4zuki 1:5817c3e6ce1e 29 //[_] R 0x52 read SC18IM700 internal register
k4zuki 1:5817c3e6ce1e 30 //[_] W 0x57 write to SC18IM700 internal register
k4zuki 1:5817c3e6ce1e 31 //[_] I 0x49 read GPIO port
k4zuki 1:5817c3e6ce1e 32 //[_] O 0x4F write to GPIO port
k4zuki 1:5817c3e6ce1e 33 //[_] Z 0x5A power down
k4zuki 2:096289a04a1f 34 //[X] C 0x?? change channel
k4zuki 0:9d12c615c8ea 35
k4zuki 0:9d12c615c8ea 36 int main()
k4zuki 0:9d12c615c8ea 37 {
k4zuki 2:096289a04a1f 38 I2C* dev=&dev1;
k4zuki 2:096289a04a1f 39 pc.baud(9600);
k4zuki 0:9d12c615c8ea 40 dev1.frequency(100000);//100k
k4zuki 1:5817c3e6ce1e 41 dev2.frequency(100000);//100k
k4zuki 2:096289a04a1f 42 dev3.frequency(100000);//100k
k4zuki 2:096289a04a1f 43 dev4.frequency(100000);//100k
k4zuki 0:9d12c615c8ea 44
k4zuki 4:bbea62f185e1 45 pc.printf("assign1: %08X\n\r",LPC_SWM->PINASSIGN[9] );//23_14_06_??
k4zuki 4:bbea62f185e1 46 pc.printf("assign2: %08X\n\r",LPC_SWM->PINASSIGN[10] );//??_20_21_22
k4zuki 4:bbea62f185e1 47 pc.printf("clock: %08X\n\r",LPC_SYSCON->SYSAHBCLKCTRL );// ??_(?|E)?_??_(?|2)?
k4zuki 2:096289a04a1f 48 // int data=0x50;
k4zuki 2:096289a04a1f 49 // int ack=0;
k4zuki 0:9d12c615c8ea 50 int plength=0;
k4zuki 0:9d12c615c8ea 51 char recieve[256];
k4zuki 2:096289a04a1f 52 // char send[256];
k4zuki 0:9d12c615c8ea 53 char read=0;
k4zuki 0:9d12c615c8ea 54 int address=0,length=0,channel=0;
k4zuki 0:9d12c615c8ea 55 enum reg {
k4zuki 0:9d12c615c8ea 56 I2C0adr='0',
k4zuki 0:9d12c615c8ea 57 I2C1adr='1',
k4zuki 0:9d12c615c8ea 58 I2C2adr='2',
k4zuki 0:9d12c615c8ea 59 I2C3adr='3',
k4zuki 0:9d12c615c8ea 60 };
k4zuki 0:9d12c615c8ea 61 //"C0P"
k4zuki 0:9d12c615c8ea 62 //"S(0x80)(4)(0xDEADBEAF)P"
k4zuki 0:9d12c615c8ea 63 //"C1P"
k4zuki 0:9d12c615c8ea 64 //"S(0x80)(4)(0xDEADBEAF)P"
k4zuki 0:9d12c615c8ea 65 //"C2P"
k4zuki 0:9d12c615c8ea 66 //"S(0x80)(4)(0xDEADBEAF)P"
k4zuki 0:9d12c615c8ea 67 //"C3P"
k4zuki 0:9d12c615c8ea 68 //"S(0x80)(4)(0xDEADBEAF)P"
k4zuki 2:096289a04a1f 69 //dev1.start();
k4zuki 2:096289a04a1f 70 //dev1.write(address);
k4zuki 2:096289a04a1f 71 //dev1.stop();
k4zuki 0:9d12c615c8ea 72
k4zuki 0:9d12c615c8ea 73 int i=0;
k4zuki 0:9d12c615c8ea 74 while(1) {
k4zuki 0:9d12c615c8ea 75 while(true) {
k4zuki 2:096289a04a1f 76 led1=0;
k4zuki 0:9d12c615c8ea 77 read=pc.getc();
k4zuki 0:9d12c615c8ea 78 recieve[i]=read;
k4zuki 2:096289a04a1f 79 //pc.printf("%02X,",read); //debug
k4zuki 0:9d12c615c8ea 80 i++;
k4zuki 0:9d12c615c8ea 81 if(read == 'P') {
k4zuki 0:9d12c615c8ea 82 plength=i;
k4zuki 2:096289a04a1f 83 led1=1;
k4zuki 2:096289a04a1f 84 //pc.printf("P\n\r");
k4zuki 0:9d12c615c8ea 85 break;
k4zuki 0:9d12c615c8ea 86 }
k4zuki 0:9d12c615c8ea 87 }
k4zuki 0:9d12c615c8ea 88 i=0;
k4zuki 0:9d12c615c8ea 89 while(i<plength) {
k4zuki 0:9d12c615c8ea 90 switch(recieve[i]) {
k4zuki 0:9d12c615c8ea 91 case 'C':
k4zuki 0:9d12c615c8ea 92 channel=recieve[i+1];
k4zuki 0:9d12c615c8ea 93 switch(channel) {
k4zuki 0:9d12c615c8ea 94 case I2C0adr:
k4zuki 2:096289a04a1f 95 //// bus=0x01;
k4zuki 2:096289a04a1f 96 //pc.printf("ch0 is selected,");
k4zuki 1:5817c3e6ce1e 97 channel='0';
k4zuki 0:9d12c615c8ea 98 dev=&dev1;
k4zuki 0:9d12c615c8ea 99 break;
k4zuki 0:9d12c615c8ea 100 case I2C1adr:
k4zuki 2:096289a04a1f 101 //// bus=0x02;
k4zuki 2:096289a04a1f 102 //pc.printf("ch1 is selected,");
k4zuki 0:9d12c615c8ea 103 channel='1';
k4zuki 0:9d12c615c8ea 104 dev=&dev2;
k4zuki 0:9d12c615c8ea 105 break;
k4zuki 0:9d12c615c8ea 106 case I2C2adr:
k4zuki 2:096289a04a1f 107 //// bus=0x04;
k4zuki 2:096289a04a1f 108 //pc.printf("ch2 is selected,");
k4zuki 2:096289a04a1f 109 channel='2';
k4zuki 2:096289a04a1f 110 dev=&dev3;
k4zuki 0:9d12c615c8ea 111 break;
k4zuki 0:9d12c615c8ea 112 case I2C3adr:
k4zuki 2:096289a04a1f 113 //// bus=0x08;
k4zuki 2:096289a04a1f 114 //pc.printf("ch3 is selected,");
k4zuki 2:096289a04a1f 115 channel='3';
k4zuki 2:096289a04a1f 116 dev=&dev4;
k4zuki 0:9d12c615c8ea 117 break;
k4zuki 0:9d12c615c8ea 118 default:
k4zuki 2:096289a04a1f 119 //pc.printf("ch? is not implemented,");
k4zuki 0:9d12c615c8ea 120 channel='0';
k4zuki 0:9d12c615c8ea 121 dev=&dev1;
k4zuki 0:9d12c615c8ea 122 break;
k4zuki 0:9d12c615c8ea 123 }
k4zuki 0:9d12c615c8ea 124 i+=(2);
k4zuki 0:9d12c615c8ea 125 break;
k4zuki 0:9d12c615c8ea 126 case 'S':
k4zuki 0:9d12c615c8ea 127 address=recieve[i+1];
k4zuki 0:9d12c615c8ea 128 length=recieve[i+2];
k4zuki 2:096289a04a1f 129 //pc.printf("addr=%02X, length=%d,",address,length);
k4zuki 3:8e33af9ea1f7 130 dev1.start();
k4zuki 3:8e33af9ea1f7 131 dev1.write(address);
k4zuki 3:8e33af9ea1f7 132 //// dev->start();
k4zuki 3:8e33af9ea1f7 133 //// dev->write(address);
k4zuki 0:9d12c615c8ea 134 if( (address&0x01)) {//read
k4zuki 2:096289a04a1f 135 //pc.printf("read from ch%c,",channel);
k4zuki 0:9d12c615c8ea 136 for(int j=0; j<length; j++) {
k4zuki 2:096289a04a1f 137 //pc.putc(dev->read(0));
k4zuki 2:096289a04a1f 138 pc.printf("%02X,",dev->read(0));
k4zuki 0:9d12c615c8ea 139 }
k4zuki 0:9d12c615c8ea 140 i+=(3);
k4zuki 0:9d12c615c8ea 141 } else {//write
k4zuki 2:096289a04a1f 142 //pc.printf("write to ch%c, ",channel);
k4zuki 0:9d12c615c8ea 143 for(int j=0; j<length; j++) {
k4zuki 2:096289a04a1f 144 //pc.printf("%02X,",recieve[3+j]);
k4zuki 3:8e33af9ea1f7 145 dev1.write(recieve[3+j]);
k4zuki 3:8e33af9ea1f7 146 //// dev->write(recieve[3+j]);
k4zuki 0:9d12c615c8ea 147 }
k4zuki 0:9d12c615c8ea 148 i+=(3+length);
k4zuki 0:9d12c615c8ea 149 }
k4zuki 0:9d12c615c8ea 150 break;
k4zuki 0:9d12c615c8ea 151 case 'P':
k4zuki 3:8e33af9ea1f7 152 dev1.stop();
k4zuki 3:8e33af9ea1f7 153 //// dev->stop();
k4zuki 0:9d12c615c8ea 154 i=plength;
k4zuki 0:9d12c615c8ea 155 pc.printf("ok\n\r");
k4zuki 0:9d12c615c8ea 156 break;
k4zuki 0:9d12c615c8ea 157 case 'R':
k4zuki 0:9d12c615c8ea 158 pc.printf("command R is not implemented\n\r");
k4zuki 0:9d12c615c8ea 159 i=plength;
k4zuki 0:9d12c615c8ea 160 break;
k4zuki 0:9d12c615c8ea 161 case 'W':
k4zuki 0:9d12c615c8ea 162 pc.printf("command W is not implemented\n\r");
k4zuki 0:9d12c615c8ea 163 i=plength;
k4zuki 0:9d12c615c8ea 164 break;
k4zuki 0:9d12c615c8ea 165 case 'I':
k4zuki 0:9d12c615c8ea 166 pc.printf("command I is not implemented\n\r");
k4zuki 0:9d12c615c8ea 167 i=plength;
k4zuki 0:9d12c615c8ea 168 break;
k4zuki 0:9d12c615c8ea 169 case 'O':
k4zuki 0:9d12c615c8ea 170 pc.printf("command O is not implemented\n\r");
k4zuki 0:9d12c615c8ea 171 i=plength;
k4zuki 0:9d12c615c8ea 172 break;
k4zuki 0:9d12c615c8ea 173 case 'Z':
k4zuki 0:9d12c615c8ea 174 pc.printf("command Z is not implemented\n\r");
k4zuki 0:9d12c615c8ea 175 i=plength;
k4zuki 0:9d12c615c8ea 176 break;
k4zuki 0:9d12c615c8ea 177 default:
k4zuki 0:9d12c615c8ea 178 pc.printf("command ? is not implemented\n\r");
k4zuki 0:9d12c615c8ea 179 i=plength;
k4zuki 0:9d12c615c8ea 180 break;
k4zuki 0:9d12c615c8ea 181 }
k4zuki 0:9d12c615c8ea 182 }
k4zuki 0:9d12c615c8ea 183 i=0;
k4zuki 0:9d12c615c8ea 184
k4zuki 0:9d12c615c8ea 185 }
k4zuki 0:9d12c615c8ea 186 }