2023-04-07 10:34 AM
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();
}
2023-04-08 02:37 AM
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);
2023-04-08 06:07 AM
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.
2023-04-08 11:42 AM
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.
2023-04-09 10:20 AM
Ok thanks i got it
2023-04-09 10:59 AM
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:
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.