-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI2C_Wrapper.c
More file actions
48 lines (41 loc) · 1.77 KB
/
I2C_Wrapper.c
File metadata and controls
48 lines (41 loc) · 1.77 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
44
45
46
47
48
/**
******************************************************************************
* @file lib.c
* @author Auto-generated by STM32CubeIDE
* @version V1.0
* @date 16/11/2024 10:47:31
* @brief Default under dev library file.
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h7xx_hal.h"
#include "C://Users//diogo//STM32CubeIDE//workspace_1.16.1//I2C_Wrapper//Src//I2C_Wrapper.h"
/** Functions ----------------------------------------------------------------*/
uint8_t I2C_writeReg(I2C_INTERFACE *interface){
uint8_t status;
uint8_t temp_send[255];
temp_send[0] = interface->register_address;
memcpy(&temp_send[1], interface->send_data, interface->num_bytes);
/*
for (int i = 0; i < interface->num_bytes; i++) {
temp_send[i + 1] = interface->send_data[i];
}
*/
status = HAL_I2C_Master_Transmit(interface->hi2c, interface->device_address, temp_send, interface->num_bytes + 1, 1000); //resend
if(status != HAL_OK)
return status;
//Confirming the data
status = HAL_I2C_Master_Transmit(interface->hi2c, interface->device_address, interface->register_address, 1, 1000); //Send the register address
if(status != HAL_OK)
return status;
HAL_Delay(50); //Double check if this actually needed
status = HAL_I2C_Master_Receive(interface->hi2c, interface->device_address , interface->received_data, interface->num_bytes, 1000); //Listen for the value stored
if(status != HAL_OK)
return status;
for(int i=0; i<interface->num_bytes; i++){ // Check the data match what was sent
if(interface->send_data[i] != interface->received_data[i]){
return COMM_ERROR;
}
}
return HAL_OK;
}