Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Porting the library code of the sensor

Hello all,

The sensor (VCNL4035) is connected and found by the nrf52dk. Indeed, the twi_sensor/scanner have been quickly recoded and it shows some progress...

The manufacturer provides the so-called code library where demos and access to the registers is quick via the functions ... can be found on the manufacturers webpage...

Indeed, the i2c_functions are given, but of course not for the nRF52 dk , so , I am trying to workout the i2c/twi by myself, but got stuck...

In the manual of the library this is the order that should be followed with regard to the other uC:

  1. Create a new project in your selected IDE  (DONE)
  2. Configure the required pinouts, the I2C components as well as the USB CDC components (DONE) btw what is meant by the USB CDC components?
  3. Open the “C” folder / “C++” folder (DONE)
  4. Add the following files into the IDE:  (DONE)
    1.  I2C_Functions.c/cpp
    2. “Part Name”_Application _Library.c/cpp, “Part Name”_PS.c/cpp, “Part Name”_ALS.c/cpp, “Part Name”_RGB.c/cpp
    3. typedefinition.h
    4. I2C_Functions.h 
    5. “Part Name”_Application_Library.h, “Part Name”.h, “Part Name”_Prototypes.h
  5. Step 5: Add the I2C API code of your MCU into I2C_Functions.c/cpp or I2C_Functions.h within the #ifdef #endif identifier statement of your MCU. Please ensure that the restart condition is implemented correctly for the I2C read command
  6. Step 6: Activate the MCU-specific code in the files I2C_Functions.c/cpp, I2C_Functions.h, and “Part Name”_Application_Library.c/cpp by defining #define (write your MCU name) in typedefinition.h 

I got to the step 5, in addition, I have setup the twi_inti, twi_handlers... however, I am stuck at the step 5!

As you can see from the above it is required to add the TWI / I2C API ... here I am not sure what / how to use ... do you have any ideas ? Indeed, the step 6 will as well be of importance.

Any help , ideas how to handle this is very much appreciated!

Best.

Parents
  • Thanks, the other slave devices is meant, the nordic nrf will be the master, and that's it :)

    However, I am not really sure, the idea is that the nrf5 series uC is put to sleep and the sensor wakes up the uC via interrupt, however, the uC is actually the one that will be calling what happens when interrupt is triggered, etc. A bit confusing to understand, who is really the master, apparently, it depends on the context?

    Indeed, thank you again will have to rewrite the examples with the nrf SDK, will come back to this...

    Best..

  • Hi,

    Most likely the MCU is the master. The master is always the one that controls the SCLK signal

    regards

    Jared 

  • OK, so I have started with porting the code from the STM example, namely starting with the "int WriteI2C_Bus (struct TransferData *Data) {...} "  but before I would like to understand should the handler from the nrf be given here in code or can be the "extern" used as in the STM example, see below:

    //STM32F specific I2C API call
    #ifdef STM32F
    	//All of the I2C API functions (For Example: HAL_I2C_Master_Transmit()) are being called from stm32f4xx_hal.h
    
    	//hi2c1 - The variable to the I2C handler which is needed later in the Write and Read I2C function.
    	//hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef hi2c1;.
    	extern I2C_HandleTypeDef hi2c1;
    
    	//Master sends I2C write command via pointer *Data from the Sensor API.
    	//The function returns HAL_OK (=0) when there is no error and -1 when there is an error.
    	int WriteI2C_Bus(struct TransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Define an array of 3 Bytes to be sent as I2C Write Command as shown in Fig. 10 in the Datasheet Page 7
    		uint8_t WData[3]={Data->RegisterAddress, Data->WData[0], Data->WData[1]};
    
    		//Send I2C Write Command as shown in Fig. 10 in the Datasheet Page 7 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Master_Transmit() is used for transmitting data to the I2C device. It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler. hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef *hi2c1;.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint8_t *pData  - is the pointer to the data to be transmitted.
    		*uint16_t Size  - is the size of the transmitted data in bytes.
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*/
    		Error = HAL_I2C_Master_Transmit(&hi2c1, (Data->Slave_Address)<<1, WData, 3, 100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error != HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    
    	//Master sends I2C Read command and save the read data via pointer *Data.
    	//The function returns HAL_OK (=0) when no error and -1 when there is error.
    	int ReadI2C_Bus (struct TransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Send I2C Read Command as shown in Fig. 10 in the Datasheet Page 7 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c1, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Mem_Read () is used for reading data from the I2C device. It has been used instead of HAL_I2C_Master_Receive() because it sends restart condition
    		*while the latter which sends stop condition and thus not working. It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint16_t MemAddress - is the Internal memory address in the slave which is the register address.
    		*uint16_t MemAddSize - the size of memory address (in Byte) which is the size of register address (In the case of Vishay's Sensor always 1 Byte).
    		*uint8_t *pData  - is the pointer to the data to be received.
    		*uint16_t Size  - is the size of the received data in bytes.
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*
    		*/
    		Error = HAL_I2C_Mem_Read(&hi2c1, (Data->Slave_Address)<<1, Data->RegisterAddress, 1,Data->RData,2,100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error !=HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    
    	//Master sends I2C Gesture Read command and save the read data via pointer *Data.
    	//The function returns HAL_OK (=0) when no error and -1 when there is error.
        int ReadI2C_Bus_Gesture_Mode(struct GestureTransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Send I2C Gesture Read Command as shown in the Application Note Page 19 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Master_Receive () is used for reading data from the I2C device. It has been used instead of HAL_I2C_Mem_Read() because no restart condition is *needed.
    		*It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler. hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef *hi2c1;.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint8_t *pData  - is the pointer to the data to be received.
    		*uint16_t Size  - is the size of the received data in bytes (For Gesture Stream 6 Bytes are needed).
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*
    		*/
    		Error = HAL_I2C_Master_Receive(&hi2c1,(Data->Slave_Address)<<1,Data->RData,6,100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error != HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    #endif

Reply
  • OK, so I have started with porting the code from the STM example, namely starting with the "int WriteI2C_Bus (struct TransferData *Data) {...} "  but before I would like to understand should the handler from the nrf be given here in code or can be the "extern" used as in the STM example, see below:

    //STM32F specific I2C API call
    #ifdef STM32F
    	//All of the I2C API functions (For Example: HAL_I2C_Master_Transmit()) are being called from stm32f4xx_hal.h
    
    	//hi2c1 - The variable to the I2C handler which is needed later in the Write and Read I2C function.
    	//hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef hi2c1;.
    	extern I2C_HandleTypeDef hi2c1;
    
    	//Master sends I2C write command via pointer *Data from the Sensor API.
    	//The function returns HAL_OK (=0) when there is no error and -1 when there is an error.
    	int WriteI2C_Bus(struct TransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Define an array of 3 Bytes to be sent as I2C Write Command as shown in Fig. 10 in the Datasheet Page 7
    		uint8_t WData[3]={Data->RegisterAddress, Data->WData[0], Data->WData[1]};
    
    		//Send I2C Write Command as shown in Fig. 10 in the Datasheet Page 7 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Master_Transmit() is used for transmitting data to the I2C device. It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler. hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef *hi2c1;.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint8_t *pData  - is the pointer to the data to be transmitted.
    		*uint16_t Size  - is the size of the transmitted data in bytes.
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*/
    		Error = HAL_I2C_Master_Transmit(&hi2c1, (Data->Slave_Address)<<1, WData, 3, 100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error != HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    
    	//Master sends I2C Read command and save the read data via pointer *Data.
    	//The function returns HAL_OK (=0) when no error and -1 when there is error.
    	int ReadI2C_Bus (struct TransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Send I2C Read Command as shown in Fig. 10 in the Datasheet Page 7 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c1, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Mem_Read () is used for reading data from the I2C device. It has been used instead of HAL_I2C_Master_Receive() because it sends restart condition
    		*while the latter which sends stop condition and thus not working. It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint16_t MemAddress - is the Internal memory address in the slave which is the register address.
    		*uint16_t MemAddSize - the size of memory address (in Byte) which is the size of register address (In the case of Vishay's Sensor always 1 Byte).
    		*uint8_t *pData  - is the pointer to the data to be received.
    		*uint16_t Size  - is the size of the received data in bytes.
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*
    		*/
    		Error = HAL_I2C_Mem_Read(&hi2c1, (Data->Slave_Address)<<1, Data->RegisterAddress, 1,Data->RData,2,100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error !=HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    
    	//Master sends I2C Gesture Read command and save the read data via pointer *Data.
    	//The function returns HAL_OK (=0) when no error and -1 when there is error.
        int ReadI2C_Bus_Gesture_Mode(struct GestureTransferData *Data)
    	{
    		//Initialization of intial Error = 0
    		int Error = 0;
    
    		//Send I2C Gesture Read Command as shown in the Application Note Page 19 with Error checking.
    		/*Format defined by STM:
    		*HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout).
    		*HAL_I2C_Master_Receive () is used for reading data from the I2C device. It has been used instead of HAL_I2C_Mem_Read() because no restart condition is *needed.
    		*It takes following arguments:
    		*
    		*I2C_HandleTypeDef *hi2c - is the pointer to the i2c handler. hi2c1 is defined in the stm32f4xx_hal.h and being called here via extern I2C_HandleTypeDef *hi2c1;.
    		*uint16_t DevAddress - is the Address of the I2C device (Need to be shifted by 1 to left since the input of function expects 8 bits).
    		*uint8_t *pData  - is the pointer to the data to be received.
    		*uint16_t Size  - is the size of the received data in bytes (For Gesture Stream 6 Bytes are needed).
    		*uint32_t Timeout - timeout in millisecond in case of any error.
    		*Return Error.
    		*
    		*/
    		Error = HAL_I2C_Master_Receive(&hi2c1,(Data->Slave_Address)<<1,Data->RData,6,100);
    
    		//Return -1 when there is error and return HAL_OK (= 0) when no error
    		if (Error != HAL_OK)
    		{
    			return -1;
    		}
    		else
    		{
    			return HAL_OK;
    		}
    	}
    #endif

Children
Related