I’ve found a weird problem …
I didn’t know why using my device (magnetometer), when I pluged the device into my PC my mouse froze my whole PC, my friend said it is because the USB was interpreted as Mouse and not as Serial communication using COM. And he said it is because my device sends data so fast and interpreted the “M” in string as mouse I don’t get it. The initialization takes 1s though
Can someone tell me why it is interpreted as Mouse and not as COM Serial communication ?
Why sending data so fast makes it happen ? Although it was 1s for me
Why it freezes the whole PC ?
Here is the code :
int16_t x, y, z;
float x_s,y_s,z_s;
LIS3MDLTR_params LIS3MDLTR;
LIS3MDLTR.POWER_MODE = 0;
LIS3MDLTR.OUTPUT_DATA_RATE = 1;
LIS3MDLTR.SCALE_CONFIGURATION = 0; // REG_2 Scale 4,8,12,16 Guass
LIS3MDLTR.OPERATION_MODE = 1; //REG_3 Single/Continuous Mode
LIS3MDLTR_Init(&LIS3MDLTR);
//Terminal variables
char log_buf[1024];
uint16_t log_len = 0;
while (1)
{
HAL_UART_Receive_IT(&huart1, loggs, 1);
setSingleConversionMode(&LIS3MDLTR);
while (!isMeasurementReady())
{
HAL_Delay(10);
}
readMagnetometerData(&x, &y, &z);
convertMagnetometerData(&x, &y, &z, &x_s, &y_s, &z_s);
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "-------------------------------------------------------- rn");
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "LIS3MDLTR Status : rn");
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "Oś X : %.2f rn", x_s);
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "Oś Y : %.2f rn", y_s);
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "Oś Z : %.2f rn", z_s);
log_len += snprintf(log_buf+log_len, sizeof(log_buf)-log_len, "-------------------------------------------------------- rn");
HAL_UART_Transmit(&huart1, (uint8_t *)log_buf, log_len, HAL_MAX_DELAY);
HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin);
log_len = 0;
HAL_Delay(500);
}
And here is the library I made
#include <stdbool.h>
#include <LIS3MDLTR-Controll.h>
#include "stm32f4xx_hal.h"
extern I2C_HandleTypeDef hi2c1;
void LIS3MDLTR_Init (LIS3MDLTR_params *param)
{
HAL_Delay(1000);
uint8_t data_to_send = (param->POWER_MODE)<<5|(param->OUTPUT_DATA_RATE)<<2;
HAL_I2C_Mem_Write(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_REG_1, 1, &data_to_send, 1, 500);
data_to_send = (param->SCALE_CONFIGURATION)<<5;
HAL_I2C_Mem_Write(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_REG_2, 1, &data_to_send, 1, 500);
data_to_send = 1<<6;
HAL_I2C_Mem_Write(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_REG_5, 1, &data_to_send, 1, 500);
}
bool isMeasurementReady()
{
uint8_t status;
HAL_I2C_Mem_Read(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_X_Y_Z_Status, 1, &status, 1, HAL_MAX_DELAY);
return (status & 0x08) != 0;
}
void readMagnetometerData(int16_t* x, int16_t* y, int16_t* z)
{
uint8_t data[6];
HAL_I2C_Mem_Read(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_X_Y_Z_Axis, 1, data, 6, HAL_MAX_DELAY);
*x = ((int16_t)data[1] << 8 | (int16_t)data[0]);
*y = ((int16_t)data[3] << 8 | (int16_t)data[2]);
*z = ((int16_t)data[5] << 8 | (int16_t)data[4]);
}
void setSingleConversionMode(LIS3MDLTR_params *param)
{
uint8_t config = (param->OPERATION_MODE);
HAL_I2C_Mem_Write(&hi2c1, LIS3MDLTR_Address, LIS3MDLTR_REG_3, 1, &config, 1, HAL_MAX_DELAY);
}
void convertMagnetometerData(int16_t* x, int16_t* y, int16_t* z, float* x_s, float* y_s, float* z_s) {
// Konwersja surowych danych do wartości w Gaussach
*x_s = (float)(*x) / SENSITIVITY;
*y_s = (float)(*y) / SENSITIVITY;
*z_s = (float)(*z) / SENSITIVITY;
*x = 0;
*y = 0;
*z = 0;
}
The initialization takes like 1s so it should be enough but still it freezes the PC.
I don’t get it why it works this way and how my friend knew it was because the device sends data to fast ! Although I would like to know, because he asked me how long the data sends I didn’t know how to answer it he assumes 5us but in my case initialization takes 1s because of HAL_delay(1000) so I don’t get it.
1