Kazuki Yamamoto / Mbed 2 deprecated uart_i2c_conv

Dependencies:   mbed

Committer:
Kazuki Yamamoto
Date:
Thu Dec 04 17:39:09 2014 +0900
Revision:
9:844e301ab0fd
Parent:
5:07f7000ec747
Child:
11:0d8359a6e2c2
[ HAL ] [ LPC824 ] cut comment and additions

- cut comment
- added E and V command for future
- start implementation of R, W, I, O commands

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Kazuki Yamamoto 9:844e301ab0fd 1 /** uart_i2c_conv for LPC824
Kazuki Yamamoto 9:844e301ab0fd 2 */
Kazuki Yamamoto 9:844e301ab0fd 3
Kazuki Yamamoto 5:07f7000ec747 4 #include "mbed.h"
Kazuki Yamamoto 5:07f7000ec747 5
Kazuki Yamamoto 5:07f7000ec747 6 Serial pc(USBTX,USBRX);
Kazuki Yamamoto 9:844e301ab0fd 7 I2C dev1(I2C_SDA, I2C_SCL);//11,10 hard coded
Kazuki Yamamoto 9:844e301ab0fd 8 I2C dev2(P0_6, P0_14);//6,14
Kazuki Yamamoto 9:844e301ab0fd 9 I2C dev3(P0_23, P0_22);//23,22
Kazuki Yamamoto 9:844e301ab0fd 10 I2C dev4(P0_21, P0_20);//21,20
Kazuki Yamamoto 9:844e301ab0fd 11
Kazuki Yamamoto 9:844e301ab0fd 12 //DigitalInOut _GPIO0(D0);
Kazuki Yamamoto 9:844e301ab0fd 13 //DigitalInOut _GPIO1(D1);
Kazuki Yamamoto 9:844e301ab0fd 14 //DigitalInOut _GPIO2(D2);
Kazuki Yamamoto 9:844e301ab0fd 15 //DigitalInOut _GPIO3(D3);
Kazuki Yamamoto 9:844e301ab0fd 16 //DigitalInOut _GPIO4(D4);
Kazuki Yamamoto 9:844e301ab0fd 17 //DigitalInOut _GPIO5(D5);
Kazuki Yamamoto 9:844e301ab0fd 18 //DigitalInOut _GPIO6(D6);
Kazuki Yamamoto 9:844e301ab0fd 19 //DigitalInOut _GPIO7(D7);
Kazuki Yamamoto 5:07f7000ec747 20
Kazuki Yamamoto 5:07f7000ec747 21 //Table 3. ASCII commands supported by SC18IM700
Kazuki Yamamoto 5:07f7000ec747 22 //ASCII command Hex value Command function
Kazuki Yamamoto 5:07f7000ec747 23 //[X] S 0x53 I2C-bus START
Kazuki Yamamoto 5:07f7000ec747 24 //[X] P 0x50 I2C-bus STOP
Kazuki Yamamoto 9:844e301ab0fd 25 //[?] R 0x52 read SC18IM700 internal register
Kazuki Yamamoto 9:844e301ab0fd 26 //[?] W 0x57 write to SC18IM700 internal register
Kazuki Yamamoto 9:844e301ab0fd 27 //[?] I 0x49 read GPIO port
Kazuki Yamamoto 9:844e301ab0fd 28 //[?] O 0x4F write to GPIO port
Kazuki Yamamoto 5:07f7000ec747 29 //[_] Z 0x5A power down
Kazuki Yamamoto 5:07f7000ec747 30 //[X] C 0x43 change channel
Kazuki Yamamoto 9:844e301ab0fd 31 //[_] E 0x45 enable chip
Kazuki Yamamoto 9:844e301ab0fd 32 //[_] V 0x__ enable VDDIO output to chip
Kazuki Yamamoto 5:07f7000ec747 33
Kazuki Yamamoto 5:07f7000ec747 34 int main()
Kazuki Yamamoto 5:07f7000ec747 35 {
Kazuki Yamamoto 5:07f7000ec747 36 I2C* dev=&dev1;
Kazuki Yamamoto 5:07f7000ec747 37 pc.baud(115200);
Kazuki Yamamoto 9:844e301ab0fd 38
Kazuki Yamamoto 9:844e301ab0fd 39 LPC_IOCON->PIO0_11 &= ~(0x02<<8);
Kazuki Yamamoto 9:844e301ab0fd 40 LPC_IOCON->PIO0_11 |= (0x02<<8);
Kazuki Yamamoto 9:844e301ab0fd 41 LPC_IOCON->PIO0_10 &= ~(0x02<<8);
Kazuki Yamamoto 9:844e301ab0fd 42 LPC_IOCON->PIO0_10 |= (0x02<<8);
Kazuki Yamamoto 5:07f7000ec747 43
Kazuki Yamamoto 9:844e301ab0fd 44 bool s = false;
Kazuki Yamamoto 9:844e301ab0fd 45 dev1.frequency(800000);//800k not work at 1M? too large pullup?
Kazuki Yamamoto 9:844e301ab0fd 46 dev2.frequency(400000);//400k
Kazuki Yamamoto 9:844e301ab0fd 47 dev3.frequency(400000);//400k
Kazuki Yamamoto 9:844e301ab0fd 48 dev4.frequency(400000);//400k
Kazuki Yamamoto 5:07f7000ec747 49
Kazuki Yamamoto 5:07f7000ec747 50 int ack=0;
Kazuki Yamamoto 5:07f7000ec747 51 int plength=0;
Kazuki Yamamoto 5:07f7000ec747 52 char recieve[256];
Kazuki Yamamoto 9:844e301ab0fd 53 char send[256];
Kazuki Yamamoto 5:07f7000ec747 54 char read=0;
Kazuki Yamamoto 9:844e301ab0fd 55 int address=0;
Kazuki Yamamoto 9:844e301ab0fd 56 int length=0;
Kazuki Yamamoto 9:844e301ab0fd 57 int channel=0;
Kazuki Yamamoto 9:844e301ab0fd 58 bool CE=false;
Kazuki Yamamoto 9:844e301ab0fd 59 enum channel_e {
Kazuki Yamamoto 9:844e301ab0fd 60 CH0 = '0',
Kazuki Yamamoto 9:844e301ab0fd 61 CH1 = '1',
Kazuki Yamamoto 9:844e301ab0fd 62 CH2 = '2',
Kazuki Yamamoto 9:844e301ab0fd 63 CH3 = '3',
Kazuki Yamamoto 5:07f7000ec747 64 };
Kazuki Yamamoto 9:844e301ab0fd 65 enum register_e {
Kazuki Yamamoto 9:844e301ab0fd 66 CHIP_ID = '0'
Kazuki Yamamoto 9:844e301ab0fd 67 GPIO_STAT = '1',
Kazuki Yamamoto 9:844e301ab0fd 68 GPIO_CONF = '2',
Kazuki Yamamoto 9:844e301ab0fd 69 };
Kazuki Yamamoto 9:844e301ab0fd 70 enum chipID_e {
Kazuki Yamamoto 9:844e301ab0fd 71 ID_LPC824 = '0',
Kazuki Yamamoto 9:844e301ab0fd 72 ID_LPC1768 = '1',
Kazuki Yamamoto 9:844e301ab0fd 73 ID_LPC11UXX = '2',
Kazuki Yamamoto 9:844e301ab0fd 74 };
Kazuki Yamamoto 9:844e301ab0fd 75 uint8_t chip_id=ID_LPC824;
Kazuki Yamamoto 9:844e301ab0fd 76 uint8_t registers[]={
Kazuki Yamamoto 9:844e301ab0fd 77 chip_id,
Kazuki Yamamoto 9:844e301ab0fd 78 0b00000000,
Kazuki Yamamoto 9:844e301ab0fd 79 0b00000000,
Kazuki Yamamoto 9:844e301ab0fd 80 };
Kazuki Yamamoto 9:844e301ab0fd 81
Kazuki Yamamoto 5:07f7000ec747 82 //"C0P"
Kazuki Yamamoto 5:07f7000ec747 83 //"C1P"
Kazuki Yamamoto 5:07f7000ec747 84 //"C2P"
Kazuki Yamamoto 5:07f7000ec747 85 //"C3P"
Kazuki Yamamoto 9:844e301ab0fd 86 //"S| 0x_8 _0| 0x_0 _4| 0x_D _E _A _D _B _E _A _F| P"
Kazuki Yamamoto 9:844e301ab0fd 87 //"S| 0x_8 _0| 0x_0 _4| 0x_D _E _A _D _B _E _A _F| S| 0x_8 _1| 0x_0 _4| P"
Kazuki Yamamoto 9:844e301ab0fd 88 //"S| 0x_8 _1| 0x_0 _4| P"
Kazuki Yamamoto 9:844e301ab0fd 89 //"R| '0'| P"
Kazuki Yamamoto 9:844e301ab0fd 90 //"R| '0'| '1'| P"
Kazuki Yamamoto 9:844e301ab0fd 91 //"W| '0' 0x_a _a| P"
Kazuki Yamamoto 9:844e301ab0fd 92 //"W| '0' 0x_a _a| '1' 0x_b _b| P"
Kazuki Yamamoto 9:844e301ab0fd 93 //"I| P"
Kazuki Yamamoto 9:844e301ab0fd 94 //"O| 0x_a _a| P"
Kazuki Yamamoto 5:07f7000ec747 95
Kazuki Yamamoto 5:07f7000ec747 96 int i=0;
Kazuki Yamamoto 5:07f7000ec747 97 while(1) {
Kazuki Yamamoto 5:07f7000ec747 98 while(true) {
Kazuki Yamamoto 5:07f7000ec747 99 read=pc.getc();
Kazuki Yamamoto 5:07f7000ec747 100 recieve[i]=read;
Kazuki Yamamoto 5:07f7000ec747 101 i++;
Kazuki Yamamoto 5:07f7000ec747 102 if(read == 'P') {
Kazuki Yamamoto 5:07f7000ec747 103 plength=i;
Kazuki Yamamoto 5:07f7000ec747 104 break;
Kazuki Yamamoto 5:07f7000ec747 105 }
Kazuki Yamamoto 5:07f7000ec747 106 }
Kazuki Yamamoto 5:07f7000ec747 107 i=0;
Kazuki Yamamoto 9:844e301ab0fd 108 while(i < plength) {
Kazuki Yamamoto 5:07f7000ec747 109 switch(recieve[i]) {
Kazuki Yamamoto 5:07f7000ec747 110 case 'C':
Kazuki Yamamoto 5:07f7000ec747 111 {
Kazuki Yamamoto 5:07f7000ec747 112 channel=recieve[i+1];
Kazuki Yamamoto 5:07f7000ec747 113 switch(channel) {
Kazuki Yamamoto 9:844e301ab0fd 114 case CH0:
Kazuki Yamamoto 5:07f7000ec747 115 {
Kazuki Yamamoto 5:07f7000ec747 116 channel='0';
Kazuki Yamamoto 5:07f7000ec747 117 dev=&dev1;
Kazuki Yamamoto 5:07f7000ec747 118 break;
Kazuki Yamamoto 5:07f7000ec747 119 }
Kazuki Yamamoto 9:844e301ab0fd 120 case CH1:
Kazuki Yamamoto 5:07f7000ec747 121 {
Kazuki Yamamoto 5:07f7000ec747 122 channel='1';
Kazuki Yamamoto 5:07f7000ec747 123 dev=&dev2;
Kazuki Yamamoto 5:07f7000ec747 124 break;
Kazuki Yamamoto 5:07f7000ec747 125 }
Kazuki Yamamoto 9:844e301ab0fd 126 case CH2:
Kazuki Yamamoto 5:07f7000ec747 127 {
Kazuki Yamamoto 5:07f7000ec747 128 channel='2';
Kazuki Yamamoto 5:07f7000ec747 129 dev=&dev3;
Kazuki Yamamoto 5:07f7000ec747 130 break;
Kazuki Yamamoto 5:07f7000ec747 131 }
Kazuki Yamamoto 9:844e301ab0fd 132 case CH3:
Kazuki Yamamoto 5:07f7000ec747 133 {
Kazuki Yamamoto 5:07f7000ec747 134 channel='3';
Kazuki Yamamoto 5:07f7000ec747 135 dev=&dev4;
Kazuki Yamamoto 5:07f7000ec747 136 break;
Kazuki Yamamoto 5:07f7000ec747 137 }
Kazuki Yamamoto 5:07f7000ec747 138 default:
Kazuki Yamamoto 5:07f7000ec747 139 {
Kazuki Yamamoto 5:07f7000ec747 140 channel='0';
Kazuki Yamamoto 5:07f7000ec747 141 dev=&dev1;
Kazuki Yamamoto 5:07f7000ec747 142 break;
Kazuki Yamamoto 5:07f7000ec747 143 }
Kazuki Yamamoto 5:07f7000ec747 144 }
Kazuki Yamamoto 5:07f7000ec747 145 i+=(2);
Kazuki Yamamoto 5:07f7000ec747 146 break;
Kazuki Yamamoto 5:07f7000ec747 147 }
Kazuki Yamamoto 5:07f7000ec747 148 case 'S':
Kazuki Yamamoto 5:07f7000ec747 149 {
Kazuki Yamamoto 9:844e301ab0fd 150 s = true;
Kazuki Yamamoto 9:844e301ab0fd 151 ack = plength - 2 - (i+1) + (recieve[i+2] & 0x01);
Kazuki Yamamoto 9:844e301ab0fd 152 if( ack >= 4){ //valid packet
Kazuki Yamamoto 9:844e301ab0fd 153 address = 0xff & (recieve[i+1] << 4 | (recieve[i+2] & 0x0F));
Kazuki Yamamoto 9:844e301ab0fd 154 length = 0xff & (recieve[i+3] << 4 | (recieve[i+4] & 0x0F));
Kazuki Yamamoto 9:844e301ab0fd 155 }else{
Kazuki Yamamoto 9:844e301ab0fd 156 pc.printf("bad packet! %d, %d, %02X, %d\n\r",plength,i,recieve[(i+2)]&0x0F,ack);
Kazuki Yamamoto 9:844e301ab0fd 157 i = plength + 1;
Kazuki Yamamoto 9:844e301ab0fd 158 break;
Kazuki Yamamoto 9:844e301ab0fd 159 }
Kazuki Yamamoto 9:844e301ab0fd 160 /* hidden
Kazuki Yamamoto 5:07f7000ec747 161 dev->start();
Kazuki Yamamoto 9:844e301ab0fd 162 ack=dev->write(address);
Kazuki Yamamoto 9:844e301ab0fd 163 */
Kazuki Yamamoto 5:07f7000ec747 164 if( (address&0x01)) {//read
Kazuki Yamamoto 9:844e301ab0fd 165 dev->read(address, send, length, false); //added
Kazuki Yamamoto 9:844e301ab0fd 166 s=false; //added
Kazuki Yamamoto 9:844e301ab0fd 167 /* hidden
Kazuki Yamamoto 5:07f7000ec747 168 for(int j=0; j<length; j++) {
Kazuki Yamamoto 9:844e301ab0fd 169 send[j] = dev->read(1);
Kazuki Yamamoto 5:07f7000ec747 170 }
Kazuki Yamamoto 9:844e301ab0fd 171 */
Kazuki Yamamoto 5:07f7000ec747 172 i+=(5);
Kazuki Yamamoto 5:07f7000ec747 173 } else {//write
Kazuki Yamamoto 9:844e301ab0fd 174 for(int j=0; j < (length * 2); j+=2) {
Kazuki Yamamoto 9:844e301ab0fd 175 ack = dev->write( 0xff&(recieve[5+j] << 4 | (recieve[6+j] & 0x0F)) );
Kazuki Yamamoto 9:844e301ab0fd 176 *(send+(j/2)) = ack; //added
Kazuki Yamamoto 5:07f7000ec747 177 }
Kazuki Yamamoto 9:844e301ab0fd 178 dev->write(address, send, length, true); //added
Kazuki Yamamoto 9:844e301ab0fd 179 i+=(5 + length * 2);
Kazuki Yamamoto 5:07f7000ec747 180 length=0;
Kazuki Yamamoto 5:07f7000ec747 181 }
Kazuki Yamamoto 5:07f7000ec747 182 break;
Kazuki Yamamoto 5:07f7000ec747 183 }
Kazuki Yamamoto 5:07f7000ec747 184 case 'P':
Kazuki Yamamoto 9:844e301ab0fd 185 {
Kazuki Yamamoto 5:07f7000ec747 186 if(s){
Kazuki Yamamoto 5:07f7000ec747 187 dev->stop();
Kazuki Yamamoto 5:07f7000ec747 188 s=false;
Kazuki Yamamoto 5:07f7000ec747 189 }
Kazuki Yamamoto 5:07f7000ec747 190 i=plength;
Kazuki Yamamoto 5:07f7000ec747 191 for(int j=0; j<length; j++) {
Kazuki Yamamoto 5:07f7000ec747 192 pc.printf("%02X,",send[j]);
Kazuki Yamamoto 5:07f7000ec747 193 }
Kazuki Yamamoto 5:07f7000ec747 194 pc.printf("ok\n\r");
Kazuki Yamamoto 5:07f7000ec747 195 break;
Kazuki Yamamoto 9:844e301ab0fd 196 }
Kazuki Yamamoto 5:07f7000ec747 197 case 'R':
Kazuki Yamamoto 9:844e301ab0fd 198 {
Kazuki Yamamoto 5:07f7000ec747 199 pc.printf("command R is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 200 i=plength;
Kazuki Yamamoto 5:07f7000ec747 201 break;
Kazuki Yamamoto 9:844e301ab0fd 202 }
Kazuki Yamamoto 5:07f7000ec747 203 case 'W':
Kazuki Yamamoto 9:844e301ab0fd 204 {
Kazuki Yamamoto 5:07f7000ec747 205 pc.printf("command W is not implemented\n\r");
Kazuki Yamamoto 9:844e301ab0fd 206 i=plength;
Kazuki Yamamoto 9:844e301ab0fd 207 break;
Kazuki Yamamoto 9:844e301ab0fd 208 }
Kazuki Yamamoto 9:844e301ab0fd 209 case 'I':
Kazuki Yamamoto 9:844e301ab0fd 210 {
Kazuki Yamamoto 9:844e301ab0fd 211 pc.printf("command I is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 212 i=plength;
Kazuki Yamamoto 5:07f7000ec747 213 break;
Kazuki Yamamoto 9:844e301ab0fd 214 }
Kazuki Yamamoto 9:844e301ab0fd 215 case 'O':
Kazuki Yamamoto 9:844e301ab0fd 216 {
Kazuki Yamamoto 9:844e301ab0fd 217 pc.printf("command O is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 218 i=plength;
Kazuki Yamamoto 5:07f7000ec747 219 break;
Kazuki Yamamoto 9:844e301ab0fd 220 }
Kazuki Yamamoto 9:844e301ab0fd 221 case 'Z':
Kazuki Yamamoto 9:844e301ab0fd 222 {
Kazuki Yamamoto 9:844e301ab0fd 223 pc.printf("command Z is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 224 i=plength;
Kazuki Yamamoto 5:07f7000ec747 225 break;
Kazuki Yamamoto 9:844e301ab0fd 226 }
Kazuki Yamamoto 9:844e301ab0fd 227 case 'E':
Kazuki Yamamoto 9:844e301ab0fd 228 {
Kazuki Yamamoto 9:844e301ab0fd 229 pc.printf("command E is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 230 i=plength;
Kazuki Yamamoto 5:07f7000ec747 231 break;
Kazuki Yamamoto 9:844e301ab0fd 232 }
Kazuki Yamamoto 9:844e301ab0fd 233 case 'V':
Kazuki Yamamoto 9:844e301ab0fd 234 {
Kazuki Yamamoto 9:844e301ab0fd 235 pc.printf("command V is not implemented\n\r");
Kazuki Yamamoto 5:07f7000ec747 236 i=plength;
Kazuki Yamamoto 5:07f7000ec747 237 break;
Kazuki Yamamoto 9:844e301ab0fd 238 }
Kazuki Yamamoto 9:844e301ab0fd 239 default:
Kazuki Yamamoto 9:844e301ab0fd 240 {
Kazuki Yamamoto 9:844e301ab0fd 241 pc.printf("command ? is not implemented\n\r");
Kazuki Yamamoto 9:844e301ab0fd 242 i=plength;
Kazuki Yamamoto 9:844e301ab0fd 243 break;
Kazuki Yamamoto 9:844e301ab0fd 244 }
Kazuki Yamamoto 5:07f7000ec747 245 }
Kazuki Yamamoto 5:07f7000ec747 246 }
Kazuki Yamamoto 5:07f7000ec747 247 i=0;
Kazuki Yamamoto 5:07f7000ec747 248 length=0;
Kazuki Yamamoto 5:07f7000ec747 249 }
Kazuki Yamamoto 5:07f7000ec747 250 }