Converting Arduino Library for STM32

Post here first, or if you can't find a relevant section!
Post Reply
Ricy
Posts: 2
Joined: Wed Apr 05, 2023 11:37 am

Converting Arduino Library for STM32

Post by Ricy »

Hello everyone,

I'm having some trouble converting the DFRobot SEN0344 sensor library for STM32. The sensor uses the MAX30102 integrated sensor and DFRobot has provided a library specifically for this sensor. However, this library only works on the Arduino platform and I'm having trouble converting it to STM32.

Specifically, I'm stuck on the writereg() function, which is responsible for writing data to the sensor's registers. The function looks like this:

void DFRobot_BloodOxygen_S_I2C::writeReg(uint16_t reg_addr, uint8_t *data_buf, uint8_t len)
{
_pWire->beginTransmission(this->_I2C_addr);
_pWire->write(reg_addr);
for (uint8_t i = 0; i < len; i++)
{
_pWire->write(data_buf);
}
_pWire->endTransmission();
}

I've tried using all the correct registers and I2C addresses, but I'm still struggling with writing this code for STM32. If anyone could provide any help or guidance, I would be very grateful.

For context, I'm using the STM32L152RE microcontroller. Thank you in advance for any assistance!

Link of DFRobot SEN0344: https://wiki.dfrobot.com/Heart_Rate_and ... KU_SEN0344
Link of Libary Github: https://github.com/DFRobot/DFRobot_BloodOxygen_S
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Converting Arduino Library for STM32

Post by dannyf »

it follows the "send start condition, send address, write data and send stop condition" format. just need to find out the corresponding implementation on your chip/software.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Converting Arduino Library for STM32

Post by ag123 »

one of those things is to check that I2C is working correctly after all and that you are connecting to it at the correct address.

An I2C scanner may help there, e.g.
https://github.com/stm32duino/Arduino_C ... canner.ino

if it doesn't find a device / address, chances are that there is a 'hardware' issue somewhere, e.g. incorrect pin, connections, wires etc

there are quite a few libraries for MAX30102
https://github.com/search?q=MAX30102

and I actually stumbled into some here
https://github.com/MHEtLive/MH-ET-LIVE-max30102
https://github.com/Jasoji/stm32-max30102
https://github.com/huyangh/MAX30102-STM32F411RE

some are non Arduino, but I'd guess it may be possible to adapt them to stm32duino.
Ricy
Posts: 2
Joined: Wed Apr 05, 2023 11:37 am

Re: Converting Arduino Library for STM32

Post by Ricy »

ag123 wrote: Thu Apr 06, 2023 5:28 am one of those things is to check that I2C is working correctly after all and that you are connecting to it at the correct address.

An I2C scanner may help there, e.g.
https://github.com/stm32duino/Arduino_C ... canner.ino

if it doesn't find a device / address, chances are that there is a 'hardware' issue somewhere, e.g. incorrect pin, connections, wires etc

there are quite a few libraries for MAX30102
https://github.com/search?q=MAX30102

and I actually stumbled into some here
https://github.com/MHEtLive/MH-ET-LIVE-max30102
https://github.com/Jasoji/stm32-max30102
https://github.com/huyangh/MAX30102-STM32F411RE

some are non Arduino, but I'd guess it may be possible to adapt them to stm32duino.
Hello thanks for your answer.

I have managed to make it, but i dont really understand why it is that way. Instead if writeReg i can use HAL_I2C_Master_Transmit directly with the right register.

The register on DFRobot for SEN0344 (https://wiki.dfrobot.com/Heart_Rate_and ... KU_SEN0344):
Set the Sensor - Transmit From Adevice:
Bdevice Address: 0x20
Function Code: 0x06
Register Address: 0x00 0x10
Register Number: 0x00 0x01 (Start to Collect Data)
CRC Check: CRC_h CRC_l

Bdevice Address: 0x20
Function Code: 0x06
Register Address: 0x00 0x10
Register Number: 0x00 0x02 (Stop Collecting Data)
CRC Check: CRC_h CRC_l

Set the Sensor - Response From Bdevice:
Bdevice Address: 0x20
Function Code: 0x06
Valid Bytes: 0x00 0x10
Data: 0x00 0x01 (Start to Collect Data)
CRC Check: CRC_h CRC_l

Bdevice Address: 0x20
Function Code: 0x06
Valid Bytes: 0x00 0x10
Data: 0x00 0x02 (Stop Collecting Data)
CRC Check: CRC_h CRC_l

For example to tell the SEN0344 to start collect data.
What i though i needed to do:

uint8_t start_buf[8];
start_buf[0] = 0x20; //Bdevice Address
start_buf[1] = 0x06; //Function Code
start_buf[2] = 0x00; //Register Address High Byte
start_buf[3] = 0x10; //Register Address Low Byte
start_buf[4] = 0x00; //Register Value High Byte
start_buf[5] = 0x01; //Register Value Low Byte
uint16_t crc = calculate_CRC(start_buf, 6);
start_buf[6] = crc & 0xFF; // CRC Low Byte
start_buf[7] = (crc >> 8) & 0xFF; // CRC High Byte

HAL_I2C_Master_Transmit(hi2c1, SEN0344_ADDRESS << 1, start_buf, sizeof(start_buf), HAL_MAX_DELAY);

but that didnt seem to work, but for some reason this one worked:

uint8_t start_buf[3];
start_buf[0] = 0x20; //Bdevice Address
start_buf[1] = 0x00; //Register Address either High or Low Byte, both work for some reason
start_buf[2] = 0x01; //Register Number LowByte
HAL_I2C_Master_Transmit(hi2c1, SEN0344_ADDRESS << 1, start_buf, sizeof(start_buf), HAL_MAX_DELAY);
Post Reply

Return to “General discussion”