[MM32硬件] 灵动微 MM32F103CxT6硬件IIC

[复制链接]
1094|13
 楼主| 豆杀包 发表于 2022-2-28 15:00 | 显示全部楼层 |阅读模式
  1. /**
  2.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Configures I2C Device port.
  3.   * @param  ulSpeed I2C baudrate.
  4.   * @retval None
  5.   */
  6. static void Config_I2cDevice ( uint32_t ulSpeed )
  7. {
  8.         GPIO_InitTypeDef xGPIO_InitStruct;
  9.         I2C_InitTypeDef xI2C_InitStruct;
  10.        
  11.         /* Enable clock. */
  12.         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );       
  13.         RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE );
  14.        
  15.         /* Configure scl & sda GPIO parameters. */
  16.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
  17.         xGPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  18.         xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
  19.         GPIO_Init( GPIOB, &xGPIO_InitStruct );
  20.        
  21.         /* Configure I2C parameters. */
  22.         xI2C_InitStruct.I2C_ClockSpeed = ulSpeed;
  23.         xI2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;
  24.         xI2C_InitStruct.I2C_OwnAddress = 0x0;
  25.         if ( ulSpeed <= 100000 ) { /* Configure speed in standard mode */
  26.                 xI2C_InitStruct.I2C_Speed = I2C_Speed_STANDARD;
  27.         } else { /* Configure speed in fast mode */
  28.                 xI2C_InitStruct.I2C_Speed = I2C_Speed_FAST;
  29.         }
  30.         I2C_Init( I2C1, &xI2C_InitStruct );
  31.        
  32.         /* Enable i2c1 port. */
  33.         I2C_Cmd( I2C1, ENABLE );
  34. }
I2C 硬件驱动

(a)、 根据官方提供源码修改,并且将其封装在 HAL_i2c 库文件内;
(b)、 该OLED 仅需要 写操作;
(c)、 在发送 STOP 停止信号前需延时 5us 才能正常显示(后续再找原因)。

  1. /**
  2.   * @brief  Writes buffer of data to the I2C EEPROM.
  3.   * @param  pxIO :typedef struct { GPIO_TypeDef* xPort; uint16_t usScl; uint16_t usSda; }I2C_GPIO_t;
  4.   * @param  pBuffer : pointer to the buffer  containing the data to be written
  5.   *         to the EEPROM.
  6.   * @param  WriteAddr : EEPROM's internal address to write to.
  7.   * @param  NumByteToWrite : number of bytes to write to the EEPROM.
  8.   * @retval None
  9.   */
  10. ErrorStatus I2C_WriteBuffer( const I2C_GPIO_t* pxIO, I2C_TypeDef* I2Cx, uint8_t DeviceAddr, uint8_t WriteAddr, uint16_t NumByteToWrite, uint8_t* pBuffer)
  11. {
  12.         uint32_t I2C_Timeout;
  13.         uint16_t write_Num;
  14.        
  15.         GPIO_InitTypeDef xGPIO_InitStruct;

  16.         xGPIO_InitStruct.GPIO_Pin  = pxIO->usScl | pxIO->usSda;
  17.         xGPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
  18.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
  19.         GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );

  20.         I2C_Cmd(I2Cx, DISABLE);
  21.         I2C_Send7bitAddress( I2Cx, DeviceAddr, I2C_Direction_Transmitter );
  22.         I2C_Cmd(I2Cx, ENABLE);
  23.        
  24.         xGPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_OD;
  25.         GPIO_Init( pxIO->xPort, &xGPIO_InitStruct );
  26.        
  27.         I2C_SendData( I2Cx, WriteAddr );
  28.         I2C_Timeout = 0x1000;
  29.        
  30.         while ( ( I2C_GetFlagStatus( I2Cx, I2C_STATUS_FLAG_TFE ) ) == RESET ) {
  31.                 if ( ( I2C_Timeout-- ) == 0 ) {
  32.                         return ERROR;
  33.                 }
  34.         }
  35.        
  36.         for ( write_Num=0; write_Num<NumByteToWrite; write_Num++ ) {
  37.                 I2C_Timeout = 0x1000;
  38.        
  39.                 while ( ( I2C_GetFlagStatus( I2Cx, I2C_STATUS_FLAG_TFE ) ) == RESET ) {
  40.                         if ( ( I2C_Timeout-- ) == 0 ) {
  41.                                 return ERROR;
  42.                         }
  43.                 }
  44.                 I2C_SendData( I2Cx, *( pBuffer + write_Num ) );
  45.         }
  46.        
  47.         /* 96MHz 工作频率下,延时5us. */
  48.         for ( uint16_t i=480; i>0; i-- );
  49.        
  50.         I2C_GenerateSTOP( I2Cx, ENABLE );
  51.         I2C_Timeout = 0x1000;
  52.        
  53.         while ( ( I2C_GetITStatus( I2Cx, I2C_IT_STOP_DET ) ) == RESET ) {
  54.                 if ( ( I2C_Timeout-- ) == 0 ) {
  55.                         return ERROR;
  56.                 }
  57.         }
  58.        
  59.         /* If all operations OK, return sEE_OK (0) */
  60.   return SUCCESS;
  61. }
OLED 程序模块

仅提供 Cmd & Data 部分,大部分和网络上的大同小异,可参考(正点原子、中景园电子、etc)。这篇**主要是 MM32 的 I2C

  1. /**
  2.   * @brief  Write commands to the screen.
  3.   * @param  ucCmd    Write command to be written.
  4.   * @retval None.
  5.   */
  6. static void Screen_WriteCmd( uint8_t ucCmd )
  7. {
  8.         I2C_WriteBuffer( &xConfig, I2C_DEVICE, I2C_ADDR, 0x00, 1, &ucCmd );
  9. }

  10. /**
  11.   * @brief  Write data to the screen.
  12.   * @param  ucCmd    Write data to be written.
  13.   * @retval None.
  14.   */
  15. static void Screen_WriteDat( uint8_t ucDat )
  16. {
  17.         I2C_WriteBuffer( &xConfig, I2C_DEVICE, I2C_ADDR, 0x40, 1, &ucDat );
  18. }


asmine 发表于 2022-3-1 14:31 | 显示全部楼层
这就可以了?
麻花油条 发表于 2022-3-2 15:10 | 显示全部楼层
tpgf 发表于 2022-3-8 14:38 | 显示全部楼层
应该都需要 一定的延时吧
drer 发表于 2022-3-8 15:32 | 显示全部楼层
中景源电子主要开发哪个型号的开发板啊
nawu 发表于 2022-3-8 15:45 | 显示全部楼层
也可以进行模拟
zljiu 发表于 2022-3-8 15:57 | 显示全部楼层
对时钟源要求是不是比较高了啊
kxsi 发表于 2022-3-8 16:09 | 显示全部楼层
如果时钟源不可靠了  会不会就会通讯失败了啊
wiba 发表于 2022-3-8 16:17 | 显示全部楼层
延时的原因是什么呢
单片小菜 发表于 2022-3-9 15:15 | 显示全部楼层
之前使用ST的芯片,在I2C这部分就总是出现问题,现在灵动的应该好用多了。
averyleigh 发表于 2022-3-16 20:49 | 显示全部楼层
这个没有bug吗  
lzbf 发表于 2022-3-16 20:59 | 显示全部楼层
使用中断了吗
pmp 发表于 2022-3-16 21:13 | 显示全部楼层
模拟iic它不香吗
mickit 发表于 2022-4-1 12:57 | 显示全部楼层
硬件IIC不太习惯的  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

49

主题

323

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部