| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- #include "gd32f3x0.h"
- #include "gd32f3x0_i2c.h"
- #include "gpio.h"
- #include "i2c.h"
- static int _gd32_i2c_rw_bytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write);
- #define iic_device(id) ((id == 0)?I2C0:I2C1)
- static void _delay_1us(uint32_t n)
- {
- while(n-->0)
- {
- int N=1000;
- while(N-->0);
- }
- }
- static void _i2c_deinit(uint32_t i2c_periph)
- {
- switch(i2c_periph){
- case I2C0:
- /* reset I2C0 */
- rcu_periph_reset_enable(RCU_I2C0RST);
- _delay_1us(10);
- rcu_periph_reset_disable(RCU_I2C0RST);
- break;
- case I2C1:
- /* reset I2C1 */
- rcu_periph_reset_enable(RCU_I2C1RST);
- _delay_1us(10);
- rcu_periph_reset_disable(RCU_I2C1RST);
- break;
- default:
- break;
- }
- }
- void gd32_i2c_init(uint32_t i2c_device, uint32_t rate){
- uint32_t device = iic_device(i2c_device);
- rcu_periph_clock_enable(RCU_GPIOB);
- if (device == I2C0) {
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_8|GPIO_PIN_9);
- gpio_mode_af(GPIOB ,GPIO_PUPD_NONE, GPIO_PIN_8|GPIO_PIN_9);
- rcu_periph_clock_enable(RCU_I2C0);
- _i2c_deinit(I2C0);
-
- } else {
- gpio_af_set(GPIOB, GPIO_AF_1, GPIO_PIN_10|GPIO_PIN_11);
- gpio_mode_af(GPIOB ,GPIO_PUPD_NONE, GPIO_PIN_10|GPIO_PIN_11);
- rcu_periph_clock_enable(RCU_I2C1);
- _i2c_deinit(I2C1);
- }
- i2c_clock_config(device, rate, I2C_DTCY_2);
- i2c_enable(device);
- }
- static int gd32_i2c_write_address(uint32_t deivce, uint8_t address, uint8_t write){
- uint32_t times;
- if (!write){
- address |= 1;
- }
-
- I2C_DATA((deivce)) = address;
- for (times = 10000; times > 0; times--) {
- if (i2c_flag_get((deivce), I2C_FLAG_ADDSEND)) {
- return 0;
- }
- }
- return -1;
- }
- int gd32_i2c_read_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value){
- return _gd32_i2c_rw_bytes(index, address, reg, value, 1, 0);
- }
- int gd32_i2c_read_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length){
- return _gd32_i2c_rw_bytes(index, address, reg, buffer, length, 0);
- }
- int gd32_i2c_write_byte(uint32_t index, uint8_t address, uint8_t reg, uint8_t value){
- return _gd32_i2c_rw_bytes(index, address, reg, &value, 1, 1);
- }
- int gd32_i2c_write_nbytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *value, int length){
- return _gd32_i2c_rw_bytes(index, address, reg, value, length, 1);
- }
- static int gd32_i2c_wait_flags(uint32_t i2c_periph, i2c_flag_enum flag, FlagStatus status){
- int remain = 100000;
- while (i2c_flag_get(i2c_periph, flag) != status) {
- if (remain-- <= 0){
- return -1;
- }
- }
- return 0;
- }
- static void gd32_i2c_wait_stop(uint32_t i2c_periph){
- int remain = 100000;
- while(I2C_CTL0(i2c_periph) & 0x0200){
- if (remain-- <= 0){
- break;
- }
- };
- }
- static int _gd32_i2c_rw_bytes(uint32_t index, uint8_t address, uint8_t reg, uint8_t *buffer, int length, int write){
- int ret = length;
- uint32_t device = iic_device(index);
- i2c_ackpos_config(device, length == 2?I2C_ACKPOS_NEXT:I2C_ACKPOS_CURRENT);
- i2c_ack_config(device, I2C_ACK_ENABLE);
- if (gd32_i2c_wait_flags(device, I2C_FLAG_I2CBSY, RESET) < 0){
- ret = -1;
- goto out_i2c_stop_on_bus;
- }
-
- //send reg
- i2c_start_on_bus(device);
- if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
- ret = -2;
- goto out_i2c_stop_on_bus;
- }
-
- if (gd32_i2c_write_address(device, address, 1) < 0){
- ret = -3;
- goto out_i2c_stop_on_bus;
- }
- i2c_flag_clear(device, I2C_FLAG_ADDSEND);
- /* wait until the transmit data buffer is empty */
- if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
- ret = -4;
- goto out_i2c_stop_on_bus;
- }
- i2c_data_transmit(device, reg);
- if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
- ret = -5;
- goto out_i2c_stop_on_bus;
- }
- if (write){
- int index = 0;
- for (; index < length; index++){
- if (gd32_i2c_wait_flags(device, I2C_FLAG_TBE, SET) < 0){
- ret = -6;
- goto out_i2c_stop_on_bus;
- }
- i2c_data_transmit(device, buffer[index]);
- }
- if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
- ret = -7;
- goto out_i2c_stop_on_bus;
- }
- ret = length;
- goto out_i2c_stop_on_bus;
- }
-
- //begin read
- i2c_start_on_bus(device);
- if (gd32_i2c_wait_flags(device, I2C_FLAG_SBSEND, SET) < 0){
- ret = -8;
- goto out_i2c_stop_on_bus;
- }
- if (gd32_i2c_write_address(device, address, 0) < 0){
- ret = -9;
- goto out_i2c_stop_on_bus;
- }
- i2c_flag_clear(device, I2C_FLAG_ADDSEND);
- if (length< 3) {
- i2c_ack_config(device, I2C_ACK_DISABLE);
-
- } else {
- while (length > 3) {
- if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
- ret = -10;
- goto out_i2c_stop_on_bus;
- }
- *buffer++ = i2c_data_receive(device);
- length--;
- }
- if (gd32_i2c_wait_flags(device, I2C_FLAG_BTC, SET) < 0){
- ret = -11;
- goto out_i2c_stop_on_bus;
- }
- i2c_ack_config(device, I2C_ACK_DISABLE);
- }
-
- while (length > 0) {
- if (gd32_i2c_wait_flags(device, I2C_FLAG_RBNE, SET) < 0){
- ret = -12;
- goto out_i2c_stop_on_bus;
- }
- *buffer++ = i2c_data_receive(device);
- length--;
- }
- if (length != 0){
- ret = -13;
- }
- out_i2c_stop_on_bus:
- i2c_stop_on_bus(device);
- gd32_i2c_wait_stop(device);
- return ret;
- }
|