【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.
|