今天搞了一下GD32 I2C0驱动EEPROM
原理图:
PB6:SCL
PB7:SDA
代码:
#include "gd32e23x.h"
#include "gd32e231c_start.h"
#include "gd32e23x_i2c.h"
#include "systick.h"
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
uchar a,b,c;
void led_flash(int times);
//-----------------------------------------------------------------------------------------
//????:Delay()
//? ?:?????
//-----------------------------------------------------------------------------------------
void Delay(unsigned char us) //5,7,9
{
while(--us);
}
void i2c_gpio_config(void)//I2C端口配置
{
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_I2C0);
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_6);//设置PB10为SCL
gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_7);//设置PB11为SDA
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);
i2c_clock_config(I2C0, 400000U, I2C_DTCY_2);//快速模式400KH
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xA0);
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
i2c_enable(I2C0);
}
int32_t i2c_buffer_read(uint8_t* p_buffer, uint8_t read_address, uint8_t device_address, uint16_t number_of_byte, uint16_t timeout)
{
uint32_t delay_time;
uint32_t i2c_delay_time = timeout * 48;
#ifdef I2C_MUTEX
xSemaphoreTake(g_i2c_mutex, portMAX_DELAY);
#endif
/* wait until I2C bus is idle */
delay_time = i2c_delay_time;
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
if(2 == number_of_byte){
i2c_ackpos_config(I2C0,I2C_ACKPOS_NEXT);
}
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_TBE));
/* enable I2C0*/
i2c_enable(I2C0);
/* send the EEPROM's internal address to write to */
i2c_data_transmit(I2C0, read_address);
/* wait until BTC bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, device_address, I2C_RECEIVER);
if(number_of_byte < 3){
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
/* wait until ADDSEND bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
if(1 == number_of_byte){
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* while there is data to be read */
delay_time = i2c_delay_time;
while((--delay_time) && number_of_byte){
if(3 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* disable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_DISABLE);
}
if(2 == number_of_byte){
/* wait until BTC bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
}
/* wait until the RBNE bit is set and clear it */
if(i2c_flag_get(I2C0, I2C_FLAG_RBNE)){
/* read a byte from the EEPROM */
*p_buffer = i2c_data_receive(I2C0);
/* point to the next location where the byte read will be saved */
p_buffer++;
/* decrement the read bytes counter */
number_of_byte--;
}
}
/* wait until the stop condition is finished */
delay_time = i2c_delay_time;
while((I2C_CTL0(I2C0)&0x0200));
/* enable acknowledge */
i2c_ack_config(I2C0,I2C_ACK_ENABLE);
i2c_ackpos_config(I2C0,I2C_ACKPOS_CURRENT);
#ifdef I2C_MUTEX
xSemaphoreGive(g_i2c_mutex);
#endif
return 0;
}
int32_t i2c_byte_write(uint8_t* p_buffer, uint8_t device_address, uint8_t write_address, uint16_t timeout)
{
uint32_t delay_time;
uint32_t i2c_delay_time = timeout * 48;
#ifdef I2C_MUTEX
xSemaphoreTake(g_i2c_mutex, portMAX_DELAY);
#endif
/* wait until I2C bus is idle */
delay_time = i2c_delay_time;
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
delay_time = i2c_delay_time;
i2c_master_addressing(I2C0, device_address, I2C_TRANSMITTER);
/* wait until ADDSEND bit is set */
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* clear the ADDSEND bit */
i2c_flag_clear(I2C0,I2C_FLAG_ADDSEND);
/* wait until the transmit data buffer is empty */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_TBE));
/* send the EEPROM's internal address to write to : only one byte address */
i2c_data_transmit(I2C0, write_address);
/* wait until BTC bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send the byte to be written */
i2c_data_transmit(I2C0, *p_buffer);
/* wait until BTC bit is set */
delay_time = i2c_delay_time;
while(SET!=i2c_flag_get(I2C0, I2C_FLAG_BTC));
/* send a stop condition to I2C bus */
i2c_stop_on_bus(I2C0);
/* wait until the stop condition is finished */
delay_time = i2c_delay_time;
while((I2C_CTL0(I2C0)&0x0200));
#ifdef I2C_MUTEX
xSemaphoreGive(g_i2c_mutex);
#endif
return 0;
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
/* initialize KEY and LED, configure SysTick */
gd_eval_key_init(KEY_WAKEUP, KEY_MODE_GPIO);
systick_config();
gd_eval_led_init(LED1);
/* flash the LED for test */
led_flash(1);
delay_1ms(500);
gd_eval_led_on(LED1);
i2c_gpio_config();
a=0x5a;
b=0xa5;
c=0x69;
i2c_byte_write(&a, 0xa0, 0, 50000);
i2c_byte_write(&b, 0xa0, 1, 50000);
i2c_byte_write(&c, 0xa0, 2, 50000);
i2c_buffer_read(&a, 0, 0xa0, 1, 1000);
i2c_buffer_read(&b, 1, 0xa0, 1, 1000);
i2c_buffer_read(&c, 2, 0xa0, 1, 1000);
while(1)
{
}
}
/*!
\brief flash the LED for test
\param[in] times: times to flash the LEDs
\param[out] none
\retval none
*/
void led_flash(int times)
{
int i;
for (i = 0;i < times;i++){
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
/* delay 500 ms */
delay_1ms(500);
/* toggle the LEDs */
gd_eval_led_toggle(LED1);
}
}
效果图:
工程:
AT24C08.rar
(612.3 KB)
|
|