-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2CPeripheral_STM32HAL.c
More file actions
43 lines (38 loc) · 1.59 KB
/
I2CPeripheral_STM32HAL.c
File metadata and controls
43 lines (38 loc) · 1.59 KB
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
/**
******************************************************************************
* File Name : I2CPeripheral_STM32HAL.c
* Description : Example polling I2C Peripheral Driver using STM32 HAL
* Authors : Chris (cjchanx)
******************************************************************************
*/
/* Includes ------------------------------------------------------- */
#include "I2CPeripheral_Interface.h"
#include "stm32l1xx_hal.h" // Change this to the target processor family
/* I2C Line ------------------------------------------------------- */
#define I2C_LINE hi2c1 // Change this to the target I2C Handle
extern I2C_HandleTypeDef I2C_LINE;
/* Functions ------------------------------------------------------ */
/**
* @brief I2C Write Function
* @param device_addr Address of the I2C Device (8-bit address)
* @param data Pointer to the data to be written
* @param len Length of write data in bytes
* @return 0 on success, 1 on failure
*/
uint8_t I2C_Write(uint8_t device_addr, uint8_t* data, uint8_t len) {
if (HAL_I2C_Master_Transmit(&I2C_LINE, device_addr, data, len, 1000) == HAL_OK)
return 0;
return 1;
}
/**
* @brief I2C Read Function
* @param device_addr Address of the I2C Device (8-bit address)
* @param dest Pointer to the buffer to store the data (must be >= len in size)
* @param len Length of the read data in bytes
* @return 0 on success, 1 on failure
*/
uint8_t I2C_Read(uint8_t device_addr, uint8_t* dest, uint8_t len) {
if (HAL_I2C_Master_Receive(&I2C_LINE, device_addr, dest, len, 1000) == HAL_OK)
return 0;
return 1;
}