๐ 1. ์ฃผ์ I2C HAL ํจ์
STM32Cube HAL ๊ธฐ๋ฅ์ ๊ธฐ๋ฐ์ผ๋ก ์ฐจ๋จ ๋ชจ๋, ์ธํฐ๋ฝํธ ๋ชจ๋ ๋๋ DMA ๋ชจ๋์ 3๊ฐ์ง ๋ชจ๋๋ก I2C ๋ฐ์ดํฐ ์ ์ก์ ์ํํ ์ ์๋ค.
โ๏ธ 1. Blocking Mode
ํต์ ์ ํด๋ง ๋ชจ๋๋ก ์ํ๋๋ฉฐ, ๋ชจ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ํ๋ ์ ์ก ์๋ฃ ํ ๋์ผํ ๊ธฐ๋ฅ์ผ๋ก ๋ฐํ๋๋ค.
- HAL_I2C_Master_Transmit()
- HAL_I2C_Master_Receive()
- HAL_I2C_Slave_Transmit()
- HAL_I2C_Slave_Receive()
- HAL_I2C_Mem_Write()
- HAL_I2C_Mem_Read()
โ๏ธ 2. Non-Blocking Mode
ํต์ ์ Interrupts ๋๋ DMA๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๋๋ฉฐ, ์ด๋ฌํ ํจ์๋ ์ ์ก ์์ ์ํ๋ฅผ ๋ฐํํ๋ค.
์ธํฐ๋ฝํธ ๋ชจ๋๋ฅผ ์ฌ์ฉํ ๋๋ ์ ์ฉ I2C IRQ๋ฅผ ํตํด, DMA ๋ชจ๋๋ฅผ ์ฌ์ฉํ ๋๋ DMA IRQ๋ฅผ ํตํด ๋ฐ์ดํฐ ์ฒ๋ฆฌ๊ฐ ์ข
๋ฃ๋จ์ ์ ์ ์๋ค.
Interrupt Mode
- HAL_I2C_Master_Transmit_IT()
- HAL_I2C_Master_Receive_IT()
- HAL_I2C_Slave_Transmit_IT()
- HAL_I2C_Slave_Receive_IT()
- HAL_I2C_Mem_Write_IT()
- HAL_I2C_Mem_Read_IT()
DMA Mode
- HAL_I2C_Master_Transmit_DMA()
- HAL_I2C_Master_Receive_DMA()
- HAL_I2C_Slave_Transmit_DMA()
- HAL_I2C_Slave_Receive_DMA()
- HAL_I2C_Mem_Write_DMA()
- HAL_I2C_Mem_Read_DMA()
๐ 2. I2C ์ค์
โ๏ธ 1. STM32CubeMX ํ๋ก์ ํธ ์ด๊ธฐ
โ๏ธ 2. I2C ์ค์
โ๏ธ 3. I2C ํ๋ผ๋ฏธํฐ ์ค์
๐ 3. ์ฝ๋
๐ 1. HAL_StatusTypeDef
HAL_I2C ํจ์๋ค์ ๋ฐํ ์๋ฃํ์ธ StatusTypeDef์ ์ ์๋ ๋ค์๊ณผ ๊ฐ๋ค.
typedef enum{
HAL_I2C_STATE_RESET = 0x00U, /*!< Peripheral is not yet Initialized */
HAL_I2C_STATE_READY = 0x20U, /*!< Peripheral Initialized and ready for use */
HAL_I2C_STATE_BUSY = 0x24U, /*!< An internal process is ongoing */
HAL_I2C_STATE_BUSY_TX = 0x21U, /*!< Data Transmission process is ongoing */
HAL_I2C_STATE_BUSY_RX = 0x22U, /*!< Data Reception process is ongoing */
HAL_I2C_STATE_LISTEN = 0x28U, /*!< Address Listen Mode is ongoing */
HAL_I2C_STATE_BUSY_TX_LISTEN = 0x29U, /*!< Address Listen Mode and Data Transmission
process is ongoing */
HAL_I2C_STATE_BUSY_RX_LISTEN = 0x2AU, /*!< Address Listen Mode and Data Reception
process is ongoing */
HAL_I2C_STATE_ABORT = 0x60U, /*!< Abort user request ongoing */
HAL_I2C_STATE_TIMEOUT = 0xA0U, /*!< Timeout state */
HAL_I2C_STATE_ERROR = 0xE0U /*!< Error */
}HAL_I2C_StateTypeDef;
๐ 2. HAL_I2C_Master_Transmit
ํน์ I2C Slave์ ๋ฐ์ดํธ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๋ ํจ์์ด๋ค.
HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- hi2c: I2C ํธ๋ค์ ์ฃผ์
- DevAddress: I2C Slave์ 8bit ์ฃผ์
- pData: ๋ฒํผ ๋ฐฐ์ด์ ์ฃผ์
- Size: ์ ์กํ ๋ฐ์ดํฐ์ ๋ฐ์ดํธ ์
- Timeout: ์ต๋ ์ง์ฐ ์๊ฐ [ms]
๐ 3. HAL_I2C_Mem_Write
I2C๋ฅผ ํตํ์ฌ Repeated Start ๋ช
๋ น์ ์ฃผ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ํจ์๋ค์ ์ด์ฉํ๋ค.
๋จ์ํ HAL_I2C_Master_Write์ HAL_I2C_Master_Read๋ฅผ ์ฐ๋ฌ์ ํธ์ถํ๋ ๊ฒ์ผ๋ก๋ ์ ์ ์๋ ํ์ง ์๋๋ค.
ํ์ง๋ง ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ํตํ์ฌ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- hi2c : I2C ํธ๋ค์ ์ฃผ์
- DevAddress : I2C Slave์ ์ฃผ์MemAddress : I2C Slave์์ ์ฐ๊ธฐ ํ ๋ ์ง์คํฐ์ ์ฃผ์
- MemAddSize : ์ฃผ์์ ๋ฐ์ดํธ ์(1byte->1, 2byte->2)
- pData : ๋ฒํผ ๋ฐฐ์ด์ ์ฃผ์
- Size : ๋ ์ง์คํฐ์ ์ฐ๊ธฐ ํ ๋ฐ์ดํธ ์
- Timeout : ์ต๋ ์ง์ฐ ์๊ฐ [ms]
๐ 4. HAL_I2C_GetState
ํ์ฌ I2C Peripheral์ ์ํ๋ฅผ ๋ฐํํ๋ค.
HAL_I2C_StateTypeDef HAL_I2C_GetState(I2C_HandleTypeDef *hi2c)
#define SLAVE_ADDR 0x27 << 1
HAL_StatusTypeDef error;
uint8_t buffer[128] = {0x3F, 0xDD, 0x54};
error = HAL_I2C_Master_Transmit(&hi2c1, SLAVE_ADDR, buffer, 3, HAL_MAX_DELAY);
if (error != HAL_OK) while();
// I2C์ ์ ์ก์ด ์๋ฃ๋ ๋๊น์ง ๋๊ธฐ
while(HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY);
HAL_I2C_GetError
์ ์ ํํ์ ์๋ฌ ์ฝ๋๋ฅผ ๋ฐํํ๋ ์ฝ๋์ด๋ค.
uint32_t HAL_I2C_GetError(I2C_HandleTypeDef *hi2c)
#define HAL_I2C_ERROR_NONE 0x00000000U /*!< No error */
#define HAL_I2C_ERROR_BERR 0x00000001U /*!< BERR error */
#define HAL_I2C_ERROR_ARLO 0x00000002U /*!< ARLO error */
#define HAL_I2C_ERROR_AF 0x00000004U /*!< AF error */
#define HAL_I2C_ERROR_OVR 0x00000008U /*!< OVR error */
#define HAL_I2C_ERROR_DMA 0x00000010U /*!< DMA transfer error */
#define HAL_I2C_ERROR_TIMEOUT 0x00000020U /*!< Timeout Error */
'๐ง STM32' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
18. STM32 I2C ํต์ - AM1008W-K-P ๋์ ๊ตฌํ (0) | 2025.01.05 |
---|---|
[STM32] 16. I2C ์ผ์ ์๊ฐ (๋ฏธ์ธ๋จผ์ง/์ด์ฐํํ์ ๋ณตํฉ ๊ณต๊ธฐ์ง ์ผ์ CUBIC - AM1008W-K-P) (0) | 2024.12.04 |
[STM32] 15. I2C ํต์ ๊ฐ์ (0) | 2024.12.04 |
[STM32] 14. ์ฝ๋ฐฑ ํจ์ ํธ์ถ ๊ณผ์ (0) | 2024.11.30 |
[STM32] 13. UART Interrupt ๊ตฌํ (0) | 2024.11.30 |