Skip to main content
Visitor II
November 18, 2023
Solved

I2C slave mode, slave not respond to master

  • November 18, 2023
  • 4 replies
  • 4160 views

Hello everyone! I have been complaining about this for 1 week. I can't make "HAL_I2C_Slave_Seq_Transmit_IT" send data to the master, but Slave to master ACK works fine, does anyone have any advice to give me that can guide me a little? I am using the STM32F030. in another way i need the slave to respond to many ADDRESS for this i configure the mask to pass any address possible. Clock STRETCH or not dont affect this trouble.

 

The interruptions in "NVIC settings" are activated (i2C global interrupt), the frequency is the maximum possible (64Mhz) and the master speed is 20Khz, I have also tried with 100,400,1000 Khz and nothing worked, the Slave ACK perfectly in any frequency. in another way if i use "HAL_I2C_Slave_Seq_Receive_IT" slave receives data perfectly. in another way when i try to transmit data to master, im getting in

"HAL_I2C_GetError(hi2c);"

the error code 0x0A.... what this mean?

 

My code:

 

 

#define RxSIZE 100
uint8_t RxData[RxSIZE];
uint8_t TxData[RxSIZE];
uint8_t errorcode;
int countError = 0;



void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c){
HAL_I2C_EnableListen_IT(hi2c);
}
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){
if(TransferDirection == I2C_DIRECTION_TRANSMIT){
}else{
HAL_I2C_Slave_Seq_Transmit_IT(hi2c, TxData, 6, I2C_FIRST_AND_NEXT_FRAME);
}

}


void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){ // this function never called!!!!


}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){} //Not used But work if i Send data Master->Slave


/*
#define HAL_I2C_ERROR_NONE (0x00000000U) !< No error
#define HAL_I2C_ERROR_BERR (0x00000001U) !< BERR error
#define HAL_I2C_ERROR_ARLO (0x00000002U) !< ARLO error
#define HAL_I2C_ERROR_AF (0x00000004U) !< ACKF error
#define HAL_I2C_ERROR_OVR (0x00000008U) !< OVR error
#define HAL_I2C_ERROR_DMA (0x00000010U) !< DMA transfer error
#define HAL_I2C_ERROR_TIMEOUT (0x00000020U) !< Timeout error
#define HAL_I2C_ERROR_SIZE (0x00000040U) !< Size Management error
#define HAL_I2C_ERROR_DMA_PARAM (0x00000080U) !< DMA Parameter Error
*/

void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c){
countError++;
errorcode = HAL_I2C_GetError(hi2c); // this responds -> 0x0A ????

HAL_I2C_EnableListen_IT(hi2c);
}



///////////////////// main //////////////////////////////////
/* USER CODE BEGIN 2 */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
for(uint16_t i=0;i<100;i++)TxData[i] = i;


while(HAL_I2C_GetState(&hi2c2)!=HAL_I2C_STATE_READY);
if(HAL_I2C_EnableListen_IT(&hi2c2)!= HAL_OK){
countError++;
}

while (1);

 

 

awdaw.png

 

stcom.png

 

 

 

 


 

 

 

    This topic has been closed for replies.
    Best answer by modonga

    Hi everyone and thanks for taking your time in help me. I decide to search and make in LOW-Level implementation in Blocking mode, this Works perfect for me.

    void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){
    
    	I2C2->TXDR = 1;
    	while(!READ_BIT(I2C2->ISR, TXIS));
    	I2C2->TXDR = 2;
    	while(!READ_BIT(I2C2->ISR, TXIS));
    	I2C2->TXDR = 3;
    }

     

    PD.: Any HAL function return the data to the master included function in Blocked / non bloqued / DMA mode

     

    4 replies

    Super User
    November 18, 2023

    I used I2C_NEXT_FRAME, not I2C_FIRST_AND_NEXT_FRAME. The UM1940 - Rev 3 user manual says something about flag usage. I also used sending single bytes only (not 6) to keep fine grained control over what's happening on the bus. But is may (shall?) work for 6 too.

    hth

    KnarfB

    modongaAuthor
    Visitor II
    November 18, 2023

    the same with "I2C_NEXT_FRAME" and 1 or 6 bytes of response.... :(

    Graduate II
    November 18, 2023

    Why you use HAL_I2C_Slave_Seq_Transmit_IT, seems by comment in headers is designed for SMBUS and no real example founded in pack. 

    modongaAuthor
    Visitor II
    November 18, 2023

    Mmmm but in this case for me its the same as the SMBUS... i dont undertand your point.

    Super User
    November 19, 2023

    Don't have (your) hardware at hand, but my code used to work flawlessly: https://gitlab.com/stm32mcu/i2c_master_and_servant

    hth

    KnarfB

     

    modongaAuthorAnswer
    Visitor II
    November 19, 2023

    Hi everyone and thanks for taking your time in help me. I decide to search and make in LOW-Level implementation in Blocking mode, this Works perfect for me.

    void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){
    
    	I2C2->TXDR = 1;
    	while(!READ_BIT(I2C2->ISR, TXIS));
    	I2C2->TXDR = 2;
    	while(!READ_BIT(I2C2->ISR, TXIS));
    	I2C2->TXDR = 3;
    }

     

    PD.: Any HAL function return the data to the master included function in Blocked / non bloqued / DMA mode