2320 views|3 replies

6841

Posts

11

Resources
The OP
 

【GD32F310G-START】OLED HELLO EEWORLD——Hardware I2C [Copy link]

【GD32F310G-START】HELLO WORLD - GD32 MCU - Electronic Engineering World - Forum (eeworld.com.cn)

Following the serial port driver, after a night of hard work, I finally drove the hardware I2C and submitted the project to everyone using the actual SSD1306:

The actual initialization of hardware I2C is divided into three steps

1. Turn on the clock of GPIO and I2C0:

/*!
  \brief enable the peripheral clock
  \param[in] none
  \param[out] none
  \retval none
*/
static void rcu_config(void)
{
    /* enable GPIOB clock */
    rcu_periph_clock_enable(RCU_GPIOB);
    /* enable I2C0 clock */
    rcu_periph_clock_enable(RCU_I2C0);

}

2. Configure IO output:


/*!
  \brief configure the GPIO ports
  \param[in] none
  \param[out] none
  \retval none
*/
static void gpio_config(void)
{
    /* connect PB6 to I2C0_SCL */
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);
    /* connect PB7 to I2C0_SDA */
    gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);
    /* cofigure GPIO pins of I2C0 */
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_7);
    gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_7);
}

3. Configure I2C output mode, slave address, etc.

/*!
  \brief   configure the I2C0 and I2C1 inter faces
  \param[in] none
  \param[out] none
  \retval none
*/
static void i2c_config(void)
{
    /* I2C clock configure */
    i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
    /* I2C address configure */
    i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
    /* enable I2C0 */
    i2c_enable(I2C0);
    /* enable acknowledge */
    i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}

4. Send data:

/*!
  \brief   I2C0发送数据
  \param[in] 需要发送的buff
  \param[in] 发送数据的长度
  \param[in] 从机地址
  \param[out] 向从机发送len长度的数据
  \retval none
*/
void i2c0_send(uint8_t *buff, int len, uint16_t slave_address)
{
  int i;
      /* wait until I2C bus is idle */
    while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
    /* send a start condition to I2C0 bus */
    i2c_start_on_bus(I2C0);
    /* wait until SBSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
    /* send slave address to I2C bus */
    i2c_master_addressing(I2C0, slave_address, I2C_TRANSMITTER);
    /* wait until ADDSEND bit is set */
    while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
    /* clear ADDSEND bit */
    i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
    /* wait until the transmit data buffer is empty */
    while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));

    for(i = 0; i < len; i++)
    {
        /* data transmission */
        i2c_data_transmit(I2C0, buff[i]);
        /* wait until the TBE bit is set */
        while(!i2c_flag_get(I2C0, I2C_FLAG_TBE));
    }
    /* send a stop condition to I2C bus */
    i2c_stop_on_bus(I2C0);
    while(I2C_CTL0(I2C0) & 0x0200);
}

Yesterday I typed it word by word based on other libraries of GD, which deepened my impression of the initialization and use of I2C. I found that if I have time, I still have to type it myself, even if I copy others.

Then I wrote the SSD1306 driver again. Now it is shown as follows:

I'll share the project package with you: Template-i2c.7z (487.27 KB, downloads: 20)
I always thought I could just copy the homework and use it. Now I think, don't use CTRL+C or CTRL+V. Type one word at a time, and it will leave a deep impression. But it's just a waste of time. Yesterday, I accidentally reached 12 o'clock and missed a sentence. I couldn't find it on the oscilloscope or analyzer. I couldn't sleep all night. I got up at 6 o'clock in the morning to continue. I found the reason when I was awake: a stop signal was not given. . .

Alas, it is not easy. Please give a thumbs up and buy SPI below.

This post is from GD32 MCU

Latest reply

It's hard work, pay attention to the combination of work and rest. Thumbs up.   Details Published on 2022-5-6 09:34
 

1w

Posts

204

Resources
2
 

I give you a thumbs up! Take care of yourself and go to bed early at night, don't stay up too late

This post is from GD32 MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Comments

Thank you for your concern! I can't handle SPI + LCD today, so I plan to take a rest early. I will continue tomorrow.  Details Published on 2022-5-5 21:44
 
 
 

6841

Posts

11

Resources
3
 
okhxyyo posted on 2022-5-5 21:39 I give you a thumbs up! Take care of yourself, and go to bed early at night, don't stay up too late

Thank you for your concern! I can't handle SPI + LCD today, so I plan to take a rest early. I will continue tomorrow.

This post is from GD32 MCU
 
 
 

2w

Posts

74

Resources
4
 

It's hard work, pay attention to the combination of work and rest. Thumbs up.

This post is from GD32 MCU
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
Personal signature

加油!在电子行业默默贡献自己的力量!:)

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list