int I2C_EE_PageWrite( uint16_t WriteAddr, uint8_t* pBuffer, uint16_t NumByteToWrite ) { uint16_t timeout; int i; // While the bus is busy for( timeout = I2C_TIMEOUT; __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_BUSY ) != RESET; ) { if( ( timeout--) == 0 ) goto I2C_WRITE_ERROR; } // Send Touch address for write I2C_TransferConfig( &hi2c2, AT24CXX_ADDR, 2 + NumByteToWrite, I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE ); for( timeout = I2C_TIMEOUT; __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_TXIS ) == RESET; ) { if( ( timeout--) == 0 ) goto I2C_WRITE_ERROR; } for( i = 0; i < 2; i++ ) { // Test on EV8 and clear it for( timeout = I2C_TIMEOUT; __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_TXIS ) == RESET; ) { if( ( timeout--) == 0 ) goto I2C_WRITE_ERROR; } switch( i ) { case 0: hi2c2.Instance->TXDR = ( WriteAddr & 0xFF00 ) >> 8; break; case 1: hi2c2.Instance->TXDR = WriteAddr & 0x00FF; break; } } I2C_TransferConfig( &hi2c2, AT24CXX_ADDR, NumByteToWrite, I2C_AUTOEND_MODE, I2C_NO_STARTSTOP ); // While there is data to be written while( NumByteToWrite-- ) { // Test on EV8 and clear it for( timeout = I2C_TIMEOUT; __HAL_I2C_GET_FLAG( &hi2c2, I2C_FLAG_TXIS ) == RESET; ) { if( ( timeout--) == 0 ) goto I2C_WRITE_ERROR; } // Send the current byte hi2c2.Instance->TXDR = *pBuffer; // Point to the next byte to be written pBuffer++; } // Send STOP condition for( timeout = I2C_TIMEOUT; __HAL_I2C_GET_FLAG( &hi2c2, I2C_ISR_STOPF ) == RESET; ) { if( ( timeout--) == 0 ) goto I2C_WRITE_ERROR; } hi2c2.Instance->CR2 |= I2C_CR2_STOP; //发送结束信号 return TRUE; I2C_WRITE_ERROR: hi2c2.Instance->CR2 |= I2C_CR2_STOP; //发送结束信号 return FALSE; }
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if( hi2c->Instance == I2C2 ) { __HAL_RCC_GPIOA_CLK_ENABLE(); /* I2C2 GPIO Configuration PA11 ------> I2C2_SCL PA12 ------> I2C2_SDA */ GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM; GPIO_InitStruct.Alternate = GPIO_AF6_I2C2; HAL_GPIO_Init( GPIOA, &GPIO_InitStruct ); // Peripheral clock enable __HAL_RCC_I2C2_CLK_ENABLE(); } }
void I2C_DevInit(void) { hi2c2.Instance = I2C2; hi2c2.Init.Timing = 0x00602173; hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c2.Init.OwnAddress2 = 0; hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if( HAL_I2C_Init( &hi2c2 ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigAnalogFilter( &hi2c2, I2C_ANALOGFILTER_ENABLE ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigDigitalFilter( &hi2c2, 0 ) != HAL_OK ) { Error_Handler(); } }
void I2C_DevInit(void){ hi2c2.Instance = I2C2; hi2c2.Init.Timing = 0x00602173; hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c2.Init.OwnAddress2 = 0; hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if( HAL_I2C_Init( &hi2c2 ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigAnalogFilter( &hi2c2, I2C_ANALOGFILTER_ENABLE ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigDigitalFilter( &hi2c2, 0 ) != HAL_OK ) { Error_Handler(); }}
void I2C_DevInit(void) { hi2c2.Instance = I2C2; hi2c2.Init.Timing = 0x00602173; hi2c2.Init.OwnAddress1 = 0; hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c2.Init.OwnAddress2 = 0; hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if( HAL_I2C_Init( &hi2c2 ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigAnalogFilter( &hi2c2, I2C_ANALOGFILTER_ENABLE ) != HAL_OK ) { Error_Handler(); } if( HAL_I2CEx_ConfigDigitalFilter( &hi2c2, 0 ) != HAL_OK ) { Error_Handler(); } }
然后调用库函数 uint16_t timeout; int i; uint8_t buf[ 256 ]; buf[ 0 ] = ( WriteAddr & 0xFF00 ) >> 8; buf[ 1 ] = WriteAddr & 0x00FF; memcpy( &buf[ 2 ], pBuffer, NumByteToWrite ); HAL_I2C_Master_Transmit( &hi2c2, AT24CXX_ADDR, buf, NumByteToWrite + 2, 0x10 );
我用逻辑分析仪来监测,始终没有生成I2C波形
电路图
I2C2的时钟使能了嘛,还有引脚的配置也检查下啊?
主体代码是stmcube生成的,我检查过了,没有问题!时钟也使能了我猜测主要还是PA12(PA10)、PA11(PA9)这一块的配置,
我看到cube库生成的代码应该是static void MX_I2C2_Init(void),楼主看看有没有遗漏什么? 另外在调试界面看看寄存器有没有配置成功。
这是管脚的配置,我也反正查了!不知道什么原因,就是不产生I2C_FLAG_TXIS中断,用逻辑分析仪监控管脚上的电平,也没有看到波形输出,不知道什么原因