Skip to main content
Kanan
Visitor II
May 30, 2023
Question

Hi everyone, I am using STM32F411re for a project and I I have to define I2C communication. Below I am will mention the both cpp and header file and the error I got while compiling the code.

  • May 30, 2023
  • 0 replies
  • 717 views

Header file:

/**
 * @file I2CCommunication.h
 * @brief Header file for controlling I2C messages
 */
#ifndef INC_I2CCOMMUNICATION_H_
#define INC_I2CCOMMUNICATION_H_
#include "DefaultDefines.h"
/**
 * @brief The I2CCommunication class
 *
 * This class will handle the sending and receiving of messages over I2C
 */
class I2CCommunication {
public:
	I2CCommunication(I2C_HandleTypeDef _hi2c);
	virtual ~I2CCommunication() = default;
	HAL_StatusTypeDef	i2cWrite8(uint8_t, uint8_t);
	HAL_StatusTypeDef	i2cRead8(uint8_t, uint8_t, uint8_t*);
	HAL_StatusTypeDef 	i2cRead16(uint8_t, uint8_t, uint16_t*);
private:
	I2C_HandleTypeDef mI2CHandle;
};
#endif /* INC_I2CCOMMUNICATION_H_ */

CPP file:

/**
 * @file I2CCommunication.cpp
 * @brief Source file for the wrapper over the HAL_i2c functions
 */
#include <I2CCommunication.h>
/**
 * @brief Constructor for I2CCommunication
 * @param[in] I2C_HandleTypeDef _hi2c The handle for the HAL I2C communication
 * @retval None
 */
I2CCommunication::I2CCommunication(I2C_HandleTypeDef _hi2c) {
	mI2CHandle = _hi2c;
}
/**
 * @brief Send one byte over I2C
 * @param[in] uint8_t address The address of the I2C slave
 * @param[in] uint8_t msg The message for the I2C slave
 * @retval HAL_StatusTypeDef The status of the HAL functions that are called within this method
 *
 * This method will call the HAL_I2C function to send an 8 bit message over I2C
 */
HAL_StatusTypeDef I2CCommunication::i2cWrite8(uint8_t _address, uint8_t _msg) {
	__HAL_I2C_CLEAR_FLAG(&mI2CHandle, I2C_FLAG_AF);
	HAL_StatusTypeDef ret = HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_msg, 1, 100);
	return ret;
}
/**
 * @brief Receive one byte over I2C
 * @param[in] uint8_t address The address of the I2C slave
 * @param[in] uint8_t command The command for the I2C slave
 * @param[out] uint8_t* data The message for the I2C slave
 * @retval HAL_StatusTypeDef The status of the HAL functions that are called within this method
 *
 * This method will call the HAL_I2C function to receive an 8 bit message over I2C
 */
HAL_StatusTypeDef I2CCommunication::i2cRead8(uint8_t _address, uint8_t _command, uint8_t* _data) {
	uint8_t ret				= 0; 				//HAL_OK
	ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
	ret |= (uint8_t)HAL_I2C_Master_Receive( &mI2CHandle, SHIFT_ADDRESS(_address), _data, 1, 100);
	return static_cast<HAL_StatusTypeDef>(ret);
}
/**
 * @brief Receive two bytes over I2C
 * @param[in] uint8_t address The address of the I2C slave
 * @param[in] uint8_t command The command for the I2C slave
 * @param[out] uint16_t* data The message for the I2C slave
 * @retval HAL_StatusTypeDef The status of the HAL functions that are called within this method
 *
 * This method will call the HAL_I2C function to receive an 16 bit message over I2C
 */
HAL_StatusTypeDef I2CCommunication::i2cRead16(uint8_t _address, uint8_t _command, uint16_t* _data) {
	uint8_t ret 			= 0; 				//HAL_OK
	uint8_t readVal[2]		= { 0 };
	ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
	ret |= (uint8_t)HAL_I2C_Master_Receive( &mI2CHandle, SHIFT_ADDRESS(_address), readVal, 2, 100);
	*_data = (((uint16_t)readVal[1] << 8) | ((uint16_t)readVal[0] & 0xFF));
	return static_cast<HAL_StatusTypeDef>(ret);
}

Error I get:

../Core/Inc/I2CCommunication.h:16:36: error: expected ')' before '_hi2c'
 16 | I2CCommunication(I2C_HandleTypeDef _hi2c);
 | ~ ^~~~~~
 | )
../Core/Inc/I2CCommunication.h:22:2: error: 'I2C_HandleTypeDef' does not name a type; did you mean 'DMA_HandleTypeDef'?
 22 | I2C_HandleTypeDef mI2CHandle;
 | ^~~~~~~~~~~~~~~~~
 | DMA_HandleTypeDef
../Core/Src/I2CCommunication.cpp:11:35: error: expected constructor, destructor, or type conversion before '(' token
 11 | I2CCommunication::I2CCommunication(I2C_HandleTypeDef _hi2c) {
 | ^
../Core/Src/I2CCommunication.cpp: In member function 'HAL_StatusTypeDef I2CCommunication::i2cWrite8(uint8_t, uint8_t)':
../Core/Src/I2CCommunication.cpp:23:24: error: 'mI2CHandle' was not declared in this scope
 23 | __HAL_I2C_CLEAR_FLAG(&mI2CHandle, I2C_FLAG_AF);
 | ^~~~~~~~~~
../Core/Src/I2CCommunication.cpp:23:36: error: 'I2C_FLAG_AF' was not declared in this scope
 23 | __HAL_I2C_CLEAR_FLAG(&mI2CHandle, I2C_FLAG_AF);
 | ^~~~~~~~~~~
../Core/Src/I2CCommunication.cpp:23:2: error: '__HAL_I2C_CLEAR_FLAG' was not declared in this scope
 23 | __HAL_I2C_CLEAR_FLAG(&mI2CHandle, I2C_FLAG_AF);
 | ^~~~~~~~~~~~~~~~~~~~
../Core/Src/I2CCommunication.cpp:24:26: error: 'HAL_I2C_Master_Transmit' was not declared in this scope
 24 | HAL_StatusTypeDef ret = HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_msg, 1, 100);
 | ^~~~~~~~~~~~~~~~~~~~~~~
../Core/Src/I2CCommunication.cpp: In member function 'HAL_StatusTypeDef I2CCommunication::i2cRead8(uint8_t, uint8_t, uint8_t*)':
../Core/Src/I2CCommunication.cpp:38:43: error: 'mI2CHandle' was not declared in this scope
 38 | ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
 | ^~~~~~~~~~
../Core/Src/I2CCommunication.cpp:38:18: error: 'HAL_I2C_Master_Transmit' was not declared in this scope
 38 | ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
 | ^~~~~~~~~~~~~~~~~~~~~~~
../Core/Src/I2CCommunication.cpp:39:18: error: 'HAL_I2C_Master_Receive' was not declared in this scope
 39 | ret |= (uint8_t)HAL_I2C_Master_Receive( &mI2CHandle, SHIFT_ADDRESS(_address), _data, 1, 100);
 | ^~~~~~~~~~~~~~~~~~~~~~
../Core/Src/I2CCommunication.cpp: In member function 'HAL_StatusTypeDef I2CCommunication::i2cRead16(uint8_t, uint8_t, uint16_t*)':
../Core/Src/I2CCommunication.cpp:54:43: error: 'mI2CHandle' was not declared in this scope
 54 | ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
 | ^~~~~~~~~~
../Core/Src/I2CCommunication.cpp:54:18: error: 'HAL_I2C_Master_Transmit' was not declared in this scope
 54 | ret |= (uint8_t)HAL_I2C_Master_Transmit(&mI2CHandle, SHIFT_ADDRESS(_address), &_command, 1, 100);
 | ^~~~~~~~~~~~~~~~~~~~~~~
../Core/Src/I2CCommunication.cpp:55:18: error: 'HAL_I2C_Master_Receive' was not declared in this scope
 55 | ret |= (uint8_t)HAL_I2C_Master_Receive( &mI2CHandle, SHIFT_ADDRESS(_address), readVal, 2, 100);
 | ^~~~~~~~~~~~~~~~~~~~~~
make: *** [Core/Src/subdir.mk:41: Core/Src/I2CCommunication.o] Error 1
make: *** Waiting for unfinished jobs....
"make -j8 all" terminated with exit code 2. Build might be incomplete.

I would like to ask your feedback.

I have tried fix the code but wasn't successful. That's why i would appreciate another pair of eye.

This topic has been closed for replies.