Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

stm32 - I2C doesn't work after uploading (only sometimes)

I wrote a little program that reads I2C values with interrupts: The Problem is that sometimes it just doesn't work after uploading. I just have to run it so many times until it works (not changing anything in the code). It seems like its just coincidence if it works or not.

Here are the Details:

Sensor: HMC 5983 or MPU-6050 (same problem)

Board: STM32F103RB (Nucleo 64)

Code (without the generated Cubemx stuff):

int main(void) {
    /*/////////////////////Setup/////////////////////////*/
    buf[0] = 0x00;
    buf[1] = 0x78;
    buf[2] = 0x80;
    buf[3] = 0x00;
    HAL_I2C_Master_Transmit(&hi2c1, Kompass_ADDR, buf, 4, 100);
    /*//////////////////////Start of IT Routine//////////////////*/
    buf[0] = 0x03;
    HAL_I2C_Master_Transmit_IT(&hi2c1, Kompass_ADDR, buf, 1);
 
    /* USER CODE END 2 */
 
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1) {
        /*/////////////////////printing the value////////////////////////*/
        sprintf((char*) Serial_buffer, " X %i 
", DataX);
        HAL_UART_Transmit(&huart2, (uint8_t*) Serial_buffer, strlen((char*) Serial_buffer), 100);
        HAL_Delay(500);
        /* USER CODE END WHILE */
 
        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}
 
 
void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) {
    HAL_I2C_Master_Receive_IT(&hi2c1, Kompass_ADDR, buf, 6);
}
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) {
    DataX = ((int16_t) buf[0]) << 8 | buf[1];
    buf[0] = 0x03;
    HAL_I2C_Master_Transmit_IT(&hi2c1, Kompass_ADDR, buf, 1);
}
////////////////////////////I2C Init Function////////////////////////
static void MX_I2C1_Init(void) {
 
    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;
    if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
        Error_Handler();
    }
}

After some Error searching I found out, that the error occurs when there I2C port is busy during the reading.

Does somebody have Experience with this I2C Problem?

question from:https://stackoverflow.com/questions/65863645/i2c-doesnt-work-after-uploading-only-sometimes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...