공부/HW

210316 Serial EEPROM CAT24C32 (I2C 펌웨어)

강경국 2021. 3. 17. 12:58
반응형

 

 

 

 

 

 

Serial EEPROM CAT24C32를 I2C interface로 STM32F103를 이용하여 제어.

Compiler는 System WorkbenchCMSIS library를 사용.

 

Byte Write를 우선 구현. Sequence는 아래와 같다.

 

 

 

I2C 설정 부터 한다.

 

아래의 순서로 구현.

1. IO 설정.

2. I2C 초기화.

3. Serial EEPROM Byte write sequence 구현. 

 

아래의 코드 참고

1. IO 설정.

1
2
3
4
5
6
7
8
9
10
11
GPIO_InitTypeDef GPIO_InitStructure;
 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 
// PORTB
// Alternate Output Push Pull
GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Pin    = PIN_I2C_SCL | PIN_I2C_SDA;
GPIO_Init(GPIOB, &GPIO_InitStructure);
 
cs

 

2. I2C 초기화.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void hal_i2c_init() {
    I2C_DeInit(I2C2);
 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
 
    I2C_InitTypeDef i2c_initStruct;
    I2C_StructInit(&i2c_initStruct);
 
    i2c_initStruct.I2C_Mode = I2C_Mode_I2C;
    i2c_initStruct.I2C_DutyCycle = I2C_DUTYCYCLE;
    i2c_initStruct.I2C_ClockSpeed = I2C_SPEED;
 
    I2C_Init(I2C2, &i2c_initStruct);
 
    I2C_Cmd(I2C2, ENABLE);
}
 
cs

 

3. Serial EEPROM Byte write sequence 구현. 

Device address는 0xA0로 하였다. A2, A2, A0에 모두 0 연결

 

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
int hal_i2c_writeByte(uint8_t slaveAddr, uint16_t regAddr, uint8_t byte) {
 
    I2C_AcknowledgeConfig(I2C2, ENABLE);
 
    I2C_GenerateSTART(I2C2, ENABLE);
 
    int timeout = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) {
        if((timeout--== 0) {
            // Error
            return -1;
        }
        delay_us(1);
    }
 
    I2C_Send7bitAddress(I2C2, slaveAddr, I2C_Direction_Transmitter);
 
    timeout = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) {
        if((timeout--== 0) {
            // Error
            return -2;
        }
        delay_us(1);
    }
 
    // Register Address 15-8
    I2C_SendData(I2C2, (uint8_t)((regAddr >> 8& 0x00FF));
 
    timeout = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
        if((timeout--== 0) {
            // Error 
            return -3;
        }
        delay_us(1);
    }
 
    // Register Address 7-0
    I2C_SendData(I2C2, (uint8_t)(regAddr & 0x00FF));
 
    timeout = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
        if((timeout--== 0) {
            // Error
            return -4;
        }
        delay_us(1);
    }
 
    I2C_SendData(I2C2, byte);
 
    timeout = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) {
        if((timeout--== 0) {
            // Error
            return -5;
        }
        delay_us(1);
    }
 
    I2C_GenerateSTOP(I2C2, ENABLE);
 
    timeout = I2C_TIMEOUT;
    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)) {
        if((timeout--== 0) {
            // Error
            return -6;
        }
        delay_us(1);
    }
 
    delay_us(100);
 
    return 0;
}
 
 
cs

 

 

 

 

 

728x90
반응형