Single UART with I2C-bus/SPI interface, 64 bytes of transmit and receive FIFOs, IrDA SIR built-in support

Dependencies:   mbed

Committer:
wataaki
Date:
Wed Feb 04 06:14:43 2015 +0000
Revision:
0:e35a417c6590
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wataaki 0:e35a417c6590 1 #include "mbed.h"
wataaki 0:e35a417c6590 2 #include "SC16IS750.h"
wataaki 0:e35a417c6590 3
wataaki 0:e35a417c6590 4 I2C i2c(p28,p27); // sda, scl
wataaki 0:e35a417c6590 5 Serial pc(USBTX, USBRX); // tx, rx
wataaki 0:e35a417c6590 6 char cmd[128];
wataaki 0:e35a417c6590 7 char str[128];
wataaki 0:e35a417c6590 8 char i, j;
wataaki 0:e35a417c6590 9
wataaki 0:e35a417c6590 10 void set_ch(char sel)
wataaki 0:e35a417c6590 11 { // PCA9541のサンプル
wataaki 0:e35a417c6590 12 // MST_0側の自分にスレーブ側の制御権を得る場合
wataaki 0:e35a417c6590 13 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:e35a417c6590 14 i2c.write( 0xe2, cmd, 1); // Cont Regを指定
wataaki 0:e35a417c6590 15 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:e35a417c6590 16 wait(0.1); // 0.1s待つ
wataaki 0:e35a417c6590 17 switch(cmd[0] & 0xf)
wataaki 0:e35a417c6590 18 {
wataaki 0:e35a417c6590 19 case 0: // bus off, has control
wataaki 0:e35a417c6590 20 case 1: // bus off, no control
wataaki 0:e35a417c6590 21 case 5: // bus on, no control
wataaki 0:e35a417c6590 22 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:e35a417c6590 23 cmd[1] = 4; // bus on, has control
wataaki 0:e35a417c6590 24 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:e35a417c6590 25 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:e35a417c6590 26 break;
wataaki 0:e35a417c6590 27 case 2: // bus off, no control
wataaki 0:e35a417c6590 28 case 3: // bus off, has control
wataaki 0:e35a417c6590 29 case 6: // bus on, no control
wataaki 0:e35a417c6590 30 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:e35a417c6590 31 cmd[1] = 5; // bus on, has control
wataaki 0:e35a417c6590 32 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:e35a417c6590 33 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:e35a417c6590 34 break;
wataaki 0:e35a417c6590 35 case 9: // bus on, no control
wataaki 0:e35a417c6590 36 case 0xc: // bus on, no control
wataaki 0:e35a417c6590 37 case 0xd: // bus off, no control
wataaki 0:e35a417c6590 38 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:e35a417c6590 39 cmd[1] = 0; // bus on, has control
wataaki 0:e35a417c6590 40 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:e35a417c6590 41 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:e35a417c6590 42 break;
wataaki 0:e35a417c6590 43 case 0xa: // bus on, no control
wataaki 0:e35a417c6590 44 case 0xe: // bus off, no control
wataaki 0:e35a417c6590 45 case 0xf: // bus on, has control
wataaki 0:e35a417c6590 46 cmd[0] = 1; // PCA9541 コマンドコード Cont Reg
wataaki 0:e35a417c6590 47 cmd[1] = 1; // bus on, has control
wataaki 0:e35a417c6590 48 i2c.write( 0xe2, cmd, 2); // Cont Regにcmd[1]を書込み
wataaki 0:e35a417c6590 49 i2c.read( 0xe2, cmd, 1); // Cont Regを読込み
wataaki 0:e35a417c6590 50 break;
wataaki 0:e35a417c6590 51 default:
wataaki 0:e35a417c6590 52 break;
wataaki 0:e35a417c6590 53 }
wataaki 0:e35a417c6590 54
wataaki 0:e35a417c6590 55 cmd[0] = sel; // PCA9546 Cont Reg sel channel enabled
wataaki 0:e35a417c6590 56 i2c.write( 0xe8, cmd, 1); // Send command string
wataaki 0:e35a417c6590 57 }
wataaki 0:e35a417c6590 58
wataaki 0:e35a417c6590 59 int main ()
wataaki 0:e35a417c6590 60 {
wataaki 0:e35a417c6590 61 i2c.frequency(100000);
wataaki 0:e35a417c6590 62 pc.printf("\r\nSC16IS750 Sample Program\r\n");
wataaki 0:e35a417c6590 63
wataaki 0:e35a417c6590 64 set_ch(1); // SC16IS750はch0に接続
wataaki 0:e35a417c6590 65 //
wataaki 0:e35a417c6590 66
wataaki 0:e35a417c6590 67 cmd[0] = LCR << 3;
wataaki 0:e35a417c6590 68 cmd[1] = 0x83; // DLL,H Latch enable
wataaki 0:e35a417c6590 69 i2c.write(SC16IS750_ADDR, cmd, 2);
wataaki 0:e35a417c6590 70
wataaki 0:e35a417c6590 71 cmd[0] = DLL << 3;
wataaki 0:e35a417c6590 72 cmd[1] = 117; // baud = 18e6 / 16 /117 = 9615
wataaki 0:e35a417c6590 73 i2c.write(SC16IS750_ADDR, cmd, 2);
wataaki 0:e35a417c6590 74
wataaki 0:e35a417c6590 75 cmd[0] = DLH << 3;
wataaki 0:e35a417c6590 76 cmd[1] = 0x0;
wataaki 0:e35a417c6590 77 i2c.write(SC16IS750_ADDR, cmd, 2);
wataaki 0:e35a417c6590 78
wataaki 0:e35a417c6590 79 cmd[0] = LCR << 3;
wataaki 0:e35a417c6590 80 cmd[1] = 0x03; // NoParity 1Stop 8bits
wataaki 0:e35a417c6590 81 i2c.write(SC16IS750_ADDR, cmd, 2);
wataaki 0:e35a417c6590 82
wataaki 0:e35a417c6590 83 cmd[0] = FCR << 3;
wataaki 0:e35a417c6590 84 cmd[1] = 0x07; // reset TX,RX FIFO, FIFO enable
wataaki 0:e35a417c6590 85 i2c.write(SC16IS750_ADDR, cmd, 2);
wataaki 0:e35a417c6590 86
wataaki 0:e35a417c6590 87 while(1)
wataaki 0:e35a417c6590 88 {
wataaki 0:e35a417c6590 89 pc.printf("\r\n文字列を入力してください>");
wataaki 0:e35a417c6590 90 pc.scanf("%s" , str);
wataaki 0:e35a417c6590 91 j = strlen(str);
wataaki 0:e35a417c6590 92 pc.printf("\r\n送信された文字列 = %s %d文字\r\n" , str, j);
wataaki 0:e35a417c6590 93 cmd[0] = THR << 3;
wataaki 0:e35a417c6590 94 for (i = 0; i < j; i++) cmd[i + 1] = str[i];
wataaki 0:e35a417c6590 95 i2c.write(SC16IS750_ADDR, cmd, j + 1);
wataaki 0:e35a417c6590 96
wataaki 0:e35a417c6590 97 wait(1);
wataaki 0:e35a417c6590 98
wataaki 0:e35a417c6590 99 cmd[0] = RHR << 3; //
wataaki 0:e35a417c6590 100 i2c.write(SC16IS750_ADDR, cmd, 1, true);
wataaki 0:e35a417c6590 101 i2c.read(SC16IS750_ADDR, cmd, j);
wataaki 0:e35a417c6590 102 cmd[j] = 0;
wataaki 0:e35a417c6590 103 pc.printf("受信した文字列 = %s\r\n", cmd);
wataaki 0:e35a417c6590 104 }
wataaki 0:e35a417c6590 105 }