cancel
Showing results for 
Search instead for 
Did you mean: 

how can i convert my simple few lines i2c wire library based arduino code to STM HAL api based code ?

Nverm.1
Associate II

Could someone please help me to convert My tested arduino code on STM32, now using HAL apis to drive same code :

#include <Wire.h>

int16_t X, Y, Z = 0;

const uint8_t STK8321 = 0x0F;

const uint8_t startRegister = 0x02;

void printAcceleration() {

 byte acceleration[6];

 Wire.beginTransmission(STK8321);

 Wire.write(startRegister);

 Wire.endTransmission();

 Wire.requestFrom(STK8321, 6);

 uint16_t XL = Wire.read();

 uint16_t XH = Wire.read();

 X = (XH << 8) | XL;

 X = X >> 4;

 uint16_t YL = Wire.read();

 uint16_t YH = Wire.read();

  

 Y = (YH << 8) | YL;

 Y = Y >> 4;

  

 uint16_t ZL = Wire.read();

 uint16_t ZH = Wire.read();

 Z = (ZH << 8) | ZL;

 Z = Z >> 4;

  

 Serial.print("    X  "); Serial.print(X); Serial.print("/"); 

Serial.print("    Y  "); Serial.print(Y);  Serial.print("/");

Serial.print("    Z  ");  Serial.println(Z);

}

void setup() {

 Serial.begin(115200);

 Wire.begin();

}

void loop() {

 delay(500);

 printAcceleration();

}

5 REPLIES 5
MM..1
Chief III

When your hal is initialized

struct accel_type accel;    // int16 xyz
 
void ...
{
 
  Cmd= 0x02;	
  HAL_I2C_Master_Transmit(&hi2c3, STK8321, (uint8_t *)&Cmd,1,2);
  HAL_I2C_Master_Receive(&hi2c3, STK8321, (uint8_t *)&accel,6,2);

Slave address apt to be (STK8321 << 1) on the STM32 as the address is the upper 7-bits

uint16_t array[3];

HAL_I2C_Master_Receive(&hi2c3, (STK8321 << 1), (uint8_t *)&array, sizeof(array), 10);

Example seems to have bytes combine Little Endian so should be able to directly cast.

You could use a byte array and reconstruct more manually.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

Here is a demo code for STM32C0 using I2C bit bang (SW+GPIO).

There are many slave vendor chips supported code example in it.

Most slave transactions can be done with a single API.

Nverm.1
Associate II

Ok thanks i got it

areo.1
Associate

Converting an I2C wire library based Arduino code to STM HAL API based code involves a few steps. Here's a general outline of the process:

  1. Import the required STM HAL libraries for I2C communication. You can find these libraries in the STM32Cube package, which is available for download from the STMicroelectronics website.
  2. Initialize the I2C peripheral in the STM HAL API code. You'll need to set up the GPIO pins for I2C communication, configure the I2C timing parameters, and enable the I2C peripheral.
  3. Rewrite the I2C communication code to use the STM HAL API functions. You'll need to replace the Wire library functions (e.g. Wire.beginTransmission(), Wire.write(), Wire.endTransmission()) with the corresponding STM HAL API functions (e.g. HAL_I2C_Master_Transmit(), HAL_I2C_Master_Receive()).
  4. Make any necessary changes to the code to account for differences between the Arduino and STM platforms. For example, you may need to change the data types or the way the code handles interrupts.

Here's an example of what the converted code might look like: #include "stm32f4xx_hal.h"

#define I2C_ADDRESS 0x3C

I2C_HandleTypeDef hi2c1;

void setup() {

 // Initialize the I2C peripheral

 hi2c1.Instance = I2C1;

 hi2c1.Init.ClockSpeed = 100000;

 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

 hi2c1.Init.OwnAddress1 = 0;

 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 hi2c1.Init.OwnAddress2 = 0;

 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 HAL_I2C_Init(&hi2c1);

}

void loop() {

 uint8_t data[2] = {0x00, 0x01};

  

 // Write to the I2C device

 HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)I2C_ADDRESS << 1, data, 2, 1000);

  

 // Read from the I2C device

 uint8_t buffer[2];

 HAL_I2C_Master_Receive(&hi2c1, (uint16_t)I2C_ADDRESS << 1, buffer, 2, 1000);

}

Note that this is just a simple example, and your code may require additional modifications to work properly with the STM HAL API. Also, be sure to consult the STM HAL API documentation for detailed information on the available functions and their usage.