Serial EEPROM CAT24C32 (32kbit)
STM32F103을 이용한 I2C Byte read 기능 구현.
환경) Compiler : System Workbench, CMSIS library
Sequence는 아래를 참조하여 구현한다.
구현한 코드
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 int EEPROM_i2c_readByte(uint8_t slaveAddr, uint16_t regAddr, uint8_t * retByte) { I2C_AcknowledgeConfig(I2C2, ENABLE); I2C_GenerateSTART(I2C2, ENABLE); int timeout = I2C_TIMEOUT; while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) { if((timeout--) == 0) { 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) { 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) { 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) { return -4; } delay_us(1); } I2C_GenerateSTART(I2C2, ENABLE); timeout = I2C_TIMEOUT; while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) { if((timeout--) == 0) { return -5; } delay_us(1); } I2C_Send7bitAddress(I2C2, slaveAddr, I2C_Direction_Receiver); timeout = I2C_TIMEOUT; while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { if((timeout--) == 0) { return -6; } delay_us(1); } timeout = I2C_TIMEOUT; while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)) { if((timeout--) == 0) { return -7; } delay_us(1); } *retByte = I2C_ReceiveData(I2C2); I2C_AcknowledgeConfig(I2C2, DISABLE); I2C_GenerateSTOP(I2C2, ENABLE); timeout = I2C_TIMEOUT; while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)) { if((timeout--) == 0) { return -8; } delay_us(1); } delay_us(100); return 0;} void delay_us(int us){ volatile u32 i; for(i = us; i > 0; i--) // 72clock = 1us { asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); }} // Use exampleuint8_t addr = 0xA0; // Device addressuint8_t value = 0;int targetaddr = 0x00;EEPROM_i2c_readByte(addr, targetaddr, &value); cs
'공부 > HW' 카테고리의 다른 글
210626 PADS 부품 분산 (0) | 2021.06.27 |
---|---|
210324 멀티탭의 정격 사양 (AC250V 16A) (0) | 2021.03.25 |
210318 C data types (C99 Standard) (0) | 2021.03.18 |
210316 Serial EEPROM CAT24C32 (I2C 펌웨어) (0) | 2021.03.17 |
210316 Serial EEPROM CAT24C32 (0) | 2021.03.16 |