Demonstration of very basic functions of sc16is740 UART connected via i2c. Demonstrates clock setting, input, output
main.cpp@0:ef968b96c286, 2016-07-30 (annotated)
- Committer:
- oliverb
- Date:
- Sat Jul 30 13:28:03 2016 +0000
- Revision:
- 0:ef968b96c286
Demo program runs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oliverb | 0:ef968b96c286 | 1 | #include "mbed.h" |
oliverb | 0:ef968b96c286 | 2 | |
oliverb | 0:ef968b96c286 | 3 | I2C i2c(p28, p27); |
oliverb | 0:ef968b96c286 | 4 | |
oliverb | 0:ef968b96c286 | 5 | const int addr = 0x90; |
oliverb | 0:ef968b96c286 | 6 | |
oliverb | 0:ef968b96c286 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
oliverb | 0:ef968b96c286 | 8 | |
oliverb | 0:ef968b96c286 | 9 | BusOut myleds(LED1, LED2, LED3, LED4); |
oliverb | 0:ef968b96c286 | 10 | |
oliverb | 0:ef968b96c286 | 11 | int writereg(int reg,int val) |
oliverb | 0:ef968b96c286 | 12 | { |
oliverb | 0:ef968b96c286 | 13 | i2c.start(); |
oliverb | 0:ef968b96c286 | 14 | i2c.write(addr); |
oliverb | 0:ef968b96c286 | 15 | i2c.write(reg <<3); |
oliverb | 0:ef968b96c286 | 16 | int aa=i2c.write(val); |
oliverb | 0:ef968b96c286 | 17 | i2c.stop(); |
oliverb | 0:ef968b96c286 | 18 | return(aa); |
oliverb | 0:ef968b96c286 | 19 | } |
oliverb | 0:ef968b96c286 | 20 | int readreg(int reg) |
oliverb | 0:ef968b96c286 | 21 | { |
oliverb | 0:ef968b96c286 | 22 | i2c.start(); |
oliverb | 0:ef968b96c286 | 23 | i2c.write(addr); |
oliverb | 0:ef968b96c286 | 24 | i2c.write(reg <<3); |
oliverb | 0:ef968b96c286 | 25 | i2c.start(); |
oliverb | 0:ef968b96c286 | 26 | i2c.write(addr|1); |
oliverb | 0:ef968b96c286 | 27 | int aa=i2c.read(0); //0=nack |
oliverb | 0:ef968b96c286 | 28 | i2c.stop(); |
oliverb | 0:ef968b96c286 | 29 | return(aa); |
oliverb | 0:ef968b96c286 | 30 | } |
oliverb | 0:ef968b96c286 | 31 | |
oliverb | 0:ef968b96c286 | 32 | |
oliverb | 0:ef968b96c286 | 33 | int main() |
oliverb | 0:ef968b96c286 | 34 | { |
oliverb | 0:ef968b96c286 | 35 | { |
oliverb | 0:ef968b96c286 | 36 | int bb; |
oliverb | 0:ef968b96c286 | 37 | i2c.start(); |
oliverb | 0:ef968b96c286 | 38 | bb=i2c.write(addr); |
oliverb | 0:ef968b96c286 | 39 | i2c.stop(); |
oliverb | 0:ef968b96c286 | 40 | pc.putc(48+bb); |
oliverb | 0:ef968b96c286 | 41 | writereg(0x03,0x83); //LCR |
oliverb | 0:ef968b96c286 | 42 | writereg(0x00,24); //DLL |
oliverb | 0:ef968b96c286 | 43 | writereg(0x01,00); //DLH |
oliverb | 0:ef968b96c286 | 44 | bb=readreg(0x00); //DLL |
oliverb | 0:ef968b96c286 | 45 | bb|=readreg(1)<<8; //DLH |
oliverb | 0:ef968b96c286 | 46 | pc.printf("baud %d ",bb); |
oliverb | 0:ef968b96c286 | 47 | writereg(0x03,0x03); //LCR |
oliverb | 0:ef968b96c286 | 48 | // writereg(0x02,0x01); //FCR |
oliverb | 0:ef968b96c286 | 49 | pc.printf("lsr: %d ",readreg(0x05)); //LSR |
oliverb | 0:ef968b96c286 | 50 | while(1) |
oliverb | 0:ef968b96c286 | 51 | { |
oliverb | 0:ef968b96c286 | 52 | int aa=readreg(0x05); //LSR |
oliverb | 0:ef968b96c286 | 53 | if ((aa & 0x20) && pc.readable()) |
oliverb | 0:ef968b96c286 | 54 | { |
oliverb | 0:ef968b96c286 | 55 | writereg(0x00,pc.getc()); //THR |
oliverb | 0:ef968b96c286 | 56 | //pc.printf("fifo: %d",readreg(0x08)); //TXLVL |
oliverb | 0:ef968b96c286 | 57 | } |
oliverb | 0:ef968b96c286 | 58 | if ((aa & 0x01) && pc.writeable()) |
oliverb | 0:ef968b96c286 | 59 | { |
oliverb | 0:ef968b96c286 | 60 | pc.putc(readreg(0x00)); //RHR |
oliverb | 0:ef968b96c286 | 61 | |
oliverb | 0:ef968b96c286 | 62 | } |
oliverb | 0:ef968b96c286 | 63 | } |
oliverb | 0:ef968b96c286 | 64 | |
oliverb | 0:ef968b96c286 | 65 | |
oliverb | 0:ef968b96c286 | 66 | |
oliverb | 0:ef968b96c286 | 67 | } |
oliverb | 0:ef968b96c286 | 68 | |
oliverb | 0:ef968b96c286 | 69 | |
oliverb | 0:ef968b96c286 | 70 | } |
oliverb | 0:ef968b96c286 | 71 |