我正在尝试实现一个中断驱动的I2C从器件,该器件在STM32L051上连续准备接收(发送时除外)。问题是我一直在HAL_I2C_ErrorCallback中收到HAL_I2C_ERROR_OVR错误。下面的代码只是一个示例,配置为接收3字节消息并以单字节状态响应。主机发送后运行此代码会触发OVR错误,然后会出现连续触发I2C1_IRQHandler,从而引发其他操作。从站无法使用拉伸,因此我已将其禁用,如下面的代码所示。我是HAL的新手,并且主要使用STM32Fxx系列和std外设库。我已经回顾了已发布的示例,甚至将一些示例复制到我更复杂的代码库中,只是为了获得相同的OVR错误。你们中的任何人都有任何建议/意见/等。那可能会让我走向可行的解决方案吗?
系统时钟速度:16MHz
I2C总线速度:10KHz
感谢。
//**** Slave Rx/Tx ****//
int main(void)
{
/* USER CODE BEGIN 1 */
int responseSize = 1;
int bytesToReceive = 3;
bool error = false;
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
/* Initialize interrupts */
MX_NVIC_Init();
i2c_response[0] = 0x01;
responseSize = 1;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//**** Slave Rx/Tx Interrupt based ****//
if(slaveRxCallbackTriggered)
{
slaveRxCallbackTriggered = false;
if(responseSize > 0)
{
if(!IOModule_TransmitToMaster(i2c_response,responseSize))
{
error = true;
}
}
IOModule_ReceiveFromMaster(bytesToReceive);
}
else
IOModule_ReceiveFromMaster(bytesToReceive);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/*------------------------------------------------------------------------------
I2C Receive data from master
*------------------------------------------------------------------------------*/
bool IOModule_ReceiveFromMaster(int numberOfBytes)
{
bool result = false;
if(HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_READY)
{
HAL_StatusTypeDef status = HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t *)i2c_rx_array, numberOfBytes);
if(status == HAL_OK)
{
result = true;
}
}
return result;
}
/*------------------------------------------------------------------------------
I2C Transmit data to master
*------------------------------------------------------------------------------*/
bool IOModule_TransmitToMaster(uint8_t txBuffer[], int bufferSize)
{
bool result = false;
while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY)
{
}
HAL_StatusTypeDef status = HAL_I2C_Slave_Transmit(&hi2c1, (uint8_t*)txBuffer, bufferSize, 10000);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_11,GPIO_PIN_RESET);
if(status == HAL_OK)
{
result = true;
}
return result;
}
/*------------------------------------------------------------------------------
I2C Slave Rx Complete Callback
*------------------------------------------------------------------------------*/
void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2CxHandle)
{
slaveRxCallbackTriggered = true;
}
/*------------------------------------------------------------------------------
I2C Error Callback
*------------------------------------------------------------------------------*/
void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
lastErrorCode = HAL_I2C_GetError(&hi2c1);
}
//I2C1 init
void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x000006C5;
hi2c1.Init.OwnAddress1 = 0x24;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
HAL_I2C_Init(&hi2c1);
/**Configure Analogue filter
*/
HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE);
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hi2c->Instance==I2C1)
{
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
}
}
Master Tx / Rx
int main(void)
{
//**** Master Tx/Rx ****//
ioModule_response[0] = 0xA0;
ioModule_response[1] = 0x01;
ioModule_response[2] = 0xE3;
while(1)
{
if(HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)0x24, (uint8_t*)ioModule_response, 3, 10000)== HAL_OK)
{
if(HAL_I2C_Master_Receive(&hi2c1, (uint16_t)0x24, (uint8_t *)i2c_rx_array, 1, 10000) != HAL_OK)
{
//Blink LED and Pause
LED_Show_StatusCode(2,false);
HAL_Delay(500);
}
else if(i2c_rx_array[0] != 1)
{
//Blink LED and Pause
LED_Show_StatusCode(3,false);
HAL_Delay(500);
}
}
}
}
这是STM32Cube MX生成的IRQ处理程序。
void I2C1_IRQHandler(void)
{
/* USER CODE BEGIN I2C1_IRQn 0 */
/* USER CODE END I2C1_IRQn 0 */
if (hi2c1.Instance->ISR & (I2C_FLAG_BERR | I2C_FLAG_ARLO | I2C_FLAG_OVR)) {
HAL_I2C_ER_IRQHandler(&hi2c1);
} else {
HAL_I2C_EV_IRQHandler(&hi2c1);
}
/* USER CODE BEGIN I2C1_IRQn 1 */
/* USER CODE END I2C1_IRQn 1 */
}
更新:我目前正在探索LL API,因为它看起来更像我过去使用的标准外设库。
答案 0 :(得分:3)
我的解决方案是完全抛弃HAL并按照imbearr的建议使用LL API。如上所述,我过去使用过STM32标准外设库,LL在很多方面都非常相似。
感谢imbearr的帮助。