i2c test

測試I2C及量測波形


更新記錄

item note
20160422 第一版

目錄


以ADXL345測試

ADXL345

  • Fixed 10bit resolution
  • Digital Accelerometer (三軸加速度計)
  • SPI(3 and 4 wire) and I2C digital interface

I2C /ADL345 I2C Address

  • It supports standard (100 kHz) and fast (400 kHz) data transfer modes
  • Single- or multiple-byte reads/writes are supported
  • 7bit I2C address (0x1D) + bit8 R/W bit
    0x3A for a write / 0x3B for a read

  • 目前硬体:ALT high

  • i2c address 7bit : 0x1D

Device Address ALT
Write: 0x3A High
READ: 0x3B High
Write: 0x6A Low
READ: 0x6B Low
  • Slave Addr(7bit) + W/R = 8bit

目前程式是採用SINGLE-BYTE-READ

device t1 t2 t3 t4 t5 t6 t7 t8
Master Start&SlaveAdr+W - RegAdr Start&SlaveAdr+R - - NACK STOP
Salve - ACK - - ACK DATA - -

ADXL345

測試波形

程序如下:

seq. type description
1 Start start condigion
2 byteframe master送出 7bit slave addr + 1bit W
3 ACK slave ack(0)
4 byteframe master送出 8bit register addr(0x0 即device ID)
5 ACK slave ack(0)
6 byteframe master送出 7bit slave addr + 1bit R
7 ACK slave ack(0)
8 byteframe slave send data
9 NACK -
10 stop stop condition
1
2
3
cx004 :[/test]# ./i2c_read 0x3A 0x0
device_addr:0x3a; reg_addr:0x 0.
0xe5
  • read i2c

程式程序

  • gpio_i2c_read
1
2
3
4
5
6
7
8
9
10
11
i2c_start_bit();
i2c_send_byte((unsigned char)(devaddress));
i2c_receive_ack();
i2c_send_byte(address);
i2c_receive_ack();
i2c_start_bit();
i2c_send_byte((unsigned char)(devaddress) | 1);
i2c_receive_ack();
rxdata = i2c_receive_byte();
//i2c_send_ack();
i2c_stop_bit();
  • i2c_start_bit
1
2
3
4
5
DELAY(1);
i2c_set(SDA | SCL);
DELAY(1);
i2c_clr(SDA);
DELAY(1);
  • i2c_stop_bit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* clock the ack */
DELAY(1);
i2c_set(SCL);
DELAY(1);
i2c_clr(SCL);

/* actual stop bit */
DELAY(1);
i2c_clr(SDA);
DELAY(1);
i2c_set(SCL);
DELAY(1);
i2c_set(SDA);
DELAY(1);
  • i2c_send_byte(c)

    • 離開時,CLK為low
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 local_irq_disable();
for (i=0; i<8; i++)
{
DELAY(1);
i2c_clr(SCL);
DELAY(1);

if (c & (1<<(7-i)))
i2c_set(SDA);
else
i2c_clr(SDA);

DELAY(1);
i2c_set(SCL);
DELAY(1);
i2c_clr(SCL);
}
DELAY(1);
// i2c_set(SDA);
local_irq_enable();
  • i2c_receive_ack

    • value: 0–Ack received; 1–Nack received
    • 離開時,CLK為low
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  DELAY(1);

regvalue = HW_REG(GPIO_0_DIR);
regvalue &= (~SDA);
HW_REG(GPIO_0_DIR) = regvalue;

DELAY(1);
i2c_clr(SCL);
DELAY(1);
i2c_set(SCL);
DELAY(1);



nack = i2c_data_read();

DELAY(1);
i2c_clr(SCL);
DELAY(1);
// i2c_set(SDA);
// DELAY(1);

if (nack == 0)
return 1;

return 0;