![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
06/21/18
Fork of rtos_threading_with_callback by
I2C_read.cpp@4:61f1412310af, 2020-04-02 (annotated)
- Committer:
- CactusSemi
- Date:
- Thu Apr 02 17:44:59 2020 +0000
- Revision:
- 4:61f1412310af
- Parent:
- 3:3e8c16b6620c
Jacaranda Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CactusSemi | 3:3e8c16b6620c | 1 | #include "mbed.h" |
CactusSemi | 3:3e8c16b6620c | 2 | |
CactusSemi | 3:3e8c16b6620c | 3 | extern Serial pc; |
CactusSemi | 3:3e8c16b6620c | 4 | extern int frequ = 50; //unit kHz |
CactusSemi | 3:3e8c16b6620c | 5 | extern char dev_addr = 0x00; //fixed |
CactusSemi | 3:3e8c16b6620c | 6 | |
CactusSemi | 3:3e8c16b6620c | 7 | char get_half_byte(int *flag) { |
CactusSemi | 3:3e8c16b6620c | 8 | char cur_char; |
CactusSemi | 3:3e8c16b6620c | 9 | cur_char = pc.getc(); |
CactusSemi | 3:3e8c16b6620c | 10 | if ( cur_char >= '0' && cur_char <= '9' ) { |
CactusSemi | 3:3e8c16b6620c | 11 | cur_char = cur_char - '0'; |
CactusSemi | 3:3e8c16b6620c | 12 | *flag = 1; |
CactusSemi | 3:3e8c16b6620c | 13 | } |
CactusSemi | 3:3e8c16b6620c | 14 | else if ( cur_char >= 'a' && cur_char <= 'f') { |
CactusSemi | 3:3e8c16b6620c | 15 | cur_char = cur_char - 'a'+10; |
CactusSemi | 3:3e8c16b6620c | 16 | *flag = 1; |
CactusSemi | 3:3e8c16b6620c | 17 | } |
CactusSemi | 3:3e8c16b6620c | 18 | else if( cur_char >= 'A' && cur_char <= 'F') { |
CactusSemi | 3:3e8c16b6620c | 19 | cur_char = cur_char - 'A'+10; |
CactusSemi | 3:3e8c16b6620c | 20 | *flag = 1; |
CactusSemi | 3:3e8c16b6620c | 21 | } |
CactusSemi | 3:3e8c16b6620c | 22 | else *flag = 0; |
CactusSemi | 3:3e8c16b6620c | 23 | return cur_char; |
CactusSemi | 3:3e8c16b6620c | 24 | } |
CactusSemi | 3:3e8c16b6620c | 25 | |
CactusSemi | 3:3e8c16b6620c | 26 | void i2c_read() { |
CactusSemi | 3:3e8c16b6620c | 27 | char reg_ptr = 0x55; |
CactusSemi | 3:3e8c16b6620c | 28 | char reg_data = 0x00; |
CactusSemi | 3:3e8c16b6620c | 29 | char temp; |
CactusSemi | 3:3e8c16b6620c | 30 | int flag1 = 0; |
CactusSemi | 3:3e8c16b6620c | 31 | char count = 0; |
CactusSemi | 3:3e8c16b6620c | 32 | I2C i2c(p9,p10); |
CactusSemi | 3:3e8c16b6620c | 33 | LPC_PINCON->PINMODE_OD0 = (LPC_PINCON->PINMODE_OD0 | 0x0003); // To make p9 & P10 open_drain |
CactusSemi | 3:3e8c16b6620c | 34 | pc.printf("\n\n\n \r\tYahoooo! Lets read a Register"); |
CactusSemi | 3:3e8c16b6620c | 35 | //Read the number of Registers |
CactusSemi | 3:3e8c16b6620c | 36 | while (!flag1) { |
CactusSemi | 3:3e8c16b6620c | 37 | pc.printf("\n\n\n\r\tEnter the number of registers to read (1 - 16) and hit 'enter' key: "); |
CactusSemi | 3:3e8c16b6620c | 38 | temp = '0'; |
CactusSemi | 3:3e8c16b6620c | 39 | count = 0; |
CactusSemi | 3:3e8c16b6620c | 40 | while (temp != '\r') { //look for enter key |
CactusSemi | 3:3e8c16b6620c | 41 | count = (count * 10) + (temp - '0'); // converting to number |
CactusSemi | 3:3e8c16b6620c | 42 | temp = pc.getc(); |
CactusSemi | 3:3e8c16b6620c | 43 | if ((temp < '0' || temp > '9') && temp !='\r') { |
CactusSemi | 3:3e8c16b6620c | 44 | pc.printf(" \n\r\t\033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); |
CactusSemi | 3:3e8c16b6620c | 45 | break; |
CactusSemi | 3:3e8c16b6620c | 46 | } |
CactusSemi | 3:3e8c16b6620c | 47 | } |
CactusSemi | 3:3e8c16b6620c | 48 | if (count >= 1 && count <= 16 && temp == '\r') flag1 = 1; |
CactusSemi | 3:3e8c16b6620c | 49 | else if (temp == '\r') pc.printf(" \n\r\t\033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); |
CactusSemi | 3:3e8c16b6620c | 50 | } |
CactusSemi | 3:3e8c16b6620c | 51 | Re_enter: |
CactusSemi | 3:3e8c16b6620c | 52 | flag1 = 0; |
CactusSemi | 3:3e8c16b6620c | 53 | //Read Pointer to the Register that needs to be read |
CactusSemi | 3:3e8c16b6620c | 54 | while (!flag1) { |
CactusSemi | 3:3e8c16b6620c | 55 | pc.printf("\n\n \r\tEnter Register Address in hex (0 to f): "); |
CactusSemi | 3:3e8c16b6620c | 56 | reg_ptr = get_half_byte(&flag1); |
CactusSemi | 3:3e8c16b6620c | 57 | if (flag1==0) pc.printf(" \033[%dm Invalid Chracter!! No worries, let us try again \033[%dm",41,40); |
CactusSemi | 3:3e8c16b6620c | 58 | } |
CactusSemi | 3:3e8c16b6620c | 59 | |
CactusSemi | 3:3e8c16b6620c | 60 | pc.printf("\n\n\n\r\t\033[%dm", 44);//change backround to blue |
CactusSemi | 3:3e8c16b6620c | 61 | pc.printf(" Device Address (fixed) = 0x%2.2X ; Register Pointer = 0x%2.2X ; Count = %d ",dev_addr, reg_ptr, count); |
CactusSemi | 3:3e8c16b6620c | 62 | pc.printf("\033[%dm", 40);//change backround to black |
CactusSemi | 3:3e8c16b6620c | 63 | |
CactusSemi | 3:3e8c16b6620c | 64 | pc.printf("\n\n\n \r\tContinue Read? (y/n) [Press 'e' to re-enter Pointer]: "); |
CactusSemi | 3:3e8c16b6620c | 65 | temp = 0; |
CactusSemi | 3:3e8c16b6620c | 66 | while ( temp!='y' && temp!='n' && temp!='e') { |
CactusSemi | 3:3e8c16b6620c | 67 | temp = pc.getc(); |
CactusSemi | 3:3e8c16b6620c | 68 | } |
CactusSemi | 3:3e8c16b6620c | 69 | if (temp == 'n') { |
CactusSemi | 3:3e8c16b6620c | 70 | pc.printf("\n\n \r\tAborting Register Read :(\n\n\r"); |
CactusSemi | 3:3e8c16b6620c | 71 | return; |
CactusSemi | 3:3e8c16b6620c | 72 | } |
CactusSemi | 3:3e8c16b6620c | 73 | if (temp == 'e') { |
CactusSemi | 3:3e8c16b6620c | 74 | pc.printf(" \n\n\n \r\tOk, let us try again :)"); |
CactusSemi | 3:3e8c16b6620c | 75 | goto Re_enter; |
CactusSemi | 3:3e8c16b6620c | 76 | } |
CactusSemi | 3:3e8c16b6620c | 77 | i2c.frequency(frequ*1000); |
CactusSemi | 3:3e8c16b6620c | 78 | i2c.stop(); //add a stop after hot-read |
CactusSemi | 3:3e8c16b6620c | 79 | wait_us(10); |
CactusSemi | 3:3e8c16b6620c | 80 | for ( ; (count > 0) && (reg_ptr < 0x10); count = count - 1) { |
CactusSemi | 3:3e8c16b6620c | 81 | i2c.start(); |
CactusSemi | 3:3e8c16b6620c | 82 | flag1 = i2c.write(dev_addr); |
CactusSemi | 3:3e8c16b6620c | 83 | if (flag1 != 1) pc.printf("\n\n\n \r\tNo Ack for dev addr :("); |
CactusSemi | 3:3e8c16b6620c | 84 | flag1 = i2c.write(reg_ptr); |
CactusSemi | 3:3e8c16b6620c | 85 | if (flag1 != 1) pc.printf("\n\n \r\tNo Ack reg pointer :("); |
CactusSemi | 3:3e8c16b6620c | 86 | i2c.stop(); |
CactusSemi | 3:3e8c16b6620c | 87 | wait_us(10); |
CactusSemi | 3:3e8c16b6620c | 88 | i2c.start(); |
CactusSemi | 3:3e8c16b6620c | 89 | flag1 = i2c.write(dev_addr | 0x01); //lsb 1 for read |
CactusSemi | 3:3e8c16b6620c | 90 | if (flag1 != 1) pc.printf("\n\n \r\tNo Ack for dev addr :("); |
CactusSemi | 3:3e8c16b6620c | 91 | reg_data = i2c.read(0); //0- donot send ack after read is done |
CactusSemi | 3:3e8c16b6620c | 92 | pc.printf("\n\n\n \r\tRead 0x%2.2X = 0x%2.2X ", reg_ptr, reg_data); |
CactusSemi | 3:3e8c16b6620c | 93 | wait_us(5); |
CactusSemi | 3:3e8c16b6620c | 94 | i2c.stop(); |
CactusSemi | 3:3e8c16b6620c | 95 | if (reg_data == 0xff) pc.printf(" |\033[%dm Register read might not be sucessfull \033[%dm ",45,40); |
CactusSemi | 3:3e8c16b6620c | 96 | reg_ptr = reg_ptr + 1; |
CactusSemi | 3:3e8c16b6620c | 97 | wait_us(120); |
CactusSemi | 3:3e8c16b6620c | 98 | } |
CactusSemi | 3:3e8c16b6620c | 99 | pc.printf("\n\n\n \r\tYeahhh! Register Read Complete\n\n\r"); |
CactusSemi | 3:3e8c16b6620c | 100 | i2c.~I2C(); |
CactusSemi | 3:3e8c16b6620c | 101 | } |