I2C BMP-280 temperature sensor sample code.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Hello_world123
Level 1
Level 1
5 sign-ins First reply posted First question asked
I need required PSOC 6 I2C with interfacing with BMP 280 temperature sensor sample code.
0 Likes
4 Replies
ADSW
Moderator
Moderator
Moderator
250 sign-ins 50 solutions authored 25 likes received

Hello @Hello_world123 , 

You may try the BMP280 library from this thread 
And initialize the I2C into the library. 

For example 

 

cy_stc_scb_i2c_context_t i2cContext;

extern bmp280_obj bmp280;
uint32_t bmp_write_bytes(uint8_t addr, uint8_t *data, uint8_t dataLen, uint32_t timeout)
{
	cy_en_scb_i2c_status_t status=0;
	if( Cy_SCB_I2C_MasterSendStart(i2c_HW, (uint32_t)addr, CY_SCB_I2C_WRITE_XFER, timeout, &i2cContext) == CY_SCB_I2C_SUCCESS )
	{
		for(uint8_t i = 0; i<dataLen; i++)
		status |= Cy_SCB_I2C_MasterWriteByte (i2c_HW, data[i], timeout,&i2cContext);
	}
	status = Cy_SCB_I2C_MasterSendStop(i2c_HW, 10UL, &i2cContext);
	return (uint32_t)status;
}

uint32_t bmp_read_bytes(uint8_t addr, uint8_t reg, uint8_t *val, uint8_t dataLen, uint32_t timeout)
{
	cy_en_scb_i2c_status_t status=0;
	if( Cy_SCB_I2C_MasterSendStart(i2c_HW, (uint32_t)addr, CY_SCB_I2C_WRITE_XFER, timeout, &i2cContext) == CY_SCB_I2C_SUCCESS )
	{
		if( CY_SCB_I2C_SUCCESS == Cy_SCB_I2C_MasterWriteByte (i2c_HW, reg, timeout,&i2cContext) )
		{
			Cy_SCB_I2C_MasterSendReStart(i2c_HW, (uint32_t)addr, CY_SCB_I2C_READ_XFER, timeout, &i2cContext);
			for( uint8_t i = 0; i< dataLen-1; i++)
				Cy_SCB_I2C_MasterReadByte (i2c_HW, CY_SCB_I2C_ACK, val++, timeout, &i2cContext);
			Cy_SCB_I2C_MasterReadByte (i2c_HW, CY_SCB_I2C_NAK, val, timeout, &i2cContext);
		}
	}
	status = Cy_SCB_I2C_MasterSendStop(i2c_HW, 10UL, &i2cContext);
	return (uint32_t)status;
}

int main(void)
{
	cy_rslt_t result;

	#if defined (CY_DEVICE_SECURE)
	cyhal_wdt_t wdt_obj;

	/* Clear watchdog timer so that it doesn't trigger a reset */
	result = cyhal_wdt_init(&wdt_obj, cyhal_wdt_get_max_timeout_ms());
	CY_ASSERT(CY_RSLT_SUCCESS == result);
	cyhal_wdt_free(&wdt_obj);
	#endif

	/* Initialize the device and board peripherals */
	result = cybsp_init();

	/* Board init failed. Stop program execution */
	if (result != CY_RSLT_SUCCESS)
	{
	CY_ASSERT(0);
	}

	/* Enable global interrupts */
	__enable_irq();
	cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
	printf("BMP280 APP\r\n");
	if( Cy_SCB_I2C_Init(i2c_HW, &i2c_config, &i2cContext) == CY_SCB_I2C_SUCCESS )
	{
		printf("I2C success \r\n");
		Cy_SCB_I2C_Enable(i2c_HW);
	}
	if( bmp280.init(BMP280_ADDR, bmp_read_bytes, bmp_write_bytes) == BMP280_SUCCESS )
		printf("Device Found !\r\n");
	float tp, press;

	for (;;)
	{
		bmp280.readValue(&tp, &press, 100);
		printf("temperature : %f \t pressure : %f \r\n", tp, press);
		cyhal_system_delay_ms(1000);
	}
}

 


Warm regards
Andri

0 Likes
Hello_world123
Level 1
Level 1
5 sign-ins First reply posted First question asked

Hi,

 

These functions are not available in PSOC6- 

Cy_SCB_I2C_MasterSendStart(i2c_HW, (uint32_t)addr, CY_SCB_I2C_WRITE_XFER, timeout, &i2cContext)

Can i change the above function like below -

uint32_t bmp_write_bytes(uint8_t addr, uint8_t *data, uint8_t dataLen, uint32_t timeout)
{
cy_en_scb_i2c_status_t status;
status=cyhal_i2c_master_write(mI2C,(uint16_t) addr, data,(uint16_t) dataLen, timeout, true);
return (uint32_t)status;
}

Here I have cyhal_i2c_master_write and cyhal_i2c_master_read functions in PSOC6

Please guide me in this.

Thanks

0 Likes
ADSW
Moderator
Moderator
Moderator
250 sign-ins 50 solutions authored 25 likes received

Hello @Hello_world123 ,

Cy_SCB_I2C_MasterSendStart(i2c_HW, (uint32_t)addr, CY_SCB_I2C_WRITE_XFER, timeout, &i2cContext)

These function is using PDL, you need to configure the peripheral from Device Configurator.

You can use HAL also. 
It would be easier for you. 

Warm regards
Andri

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Once upon a time, I posted the following sample code to www.electromaker.io 

as ModusToolbox project(s) are too HUGE to post here.

https://www.electromaker.io/project/view/a-simple-sensor-server-using-cy8ckit-062-wifi-bt

moto

0 Likes