/*! \file main.c \brief slave transmitter \version 2017-02-10, V1.0.0, firmware for GD32F30x \version 2018-10-10, V1.1.0, firmware for GD32F30x \version 2018-12-25, V2.0.0, firmware for GD32F30x \version 2020-09-30, V2.1.0, firmware for GD32F30x */ /* Copyright (c) 2020, GigaDevice Semiconductor Inc. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "gd32f30x.h" #define I2C_10BIT_ADDRESS 0 #define I2C0_OWN_ADDRESS7 0x82 #define I2C0_SLAVE_ADDRESS7 0x72 #define I2C0_OWN_ADDRESS10 0x0322 uint8_t i2c_transmitter[16]; void rcu_config(void); void gpio_config(void); void i2c_config(void); /*! \brief main function \param[in] none \param[out] none \retval none */ int main(void) { uint8_t i; /* cofigure RCU */ rcu_config(); /* cofigure GPIO */ gpio_config(); /* cofigure I2C */ i2c_config(); for(i = 0; i < 16; i++) { i2c_transmitter[i] = i + 0x80; } #if I2C_10BIT_ADDRESS /* 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 ADDSEND bit is set */ while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)); /* clear ADDSEND bit */ i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); #else /* wait until ADDSEND bit is set */ while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND)); /* clear ADDSEND bit */ i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND); #endif /* wait until the transmission data register is empty */ while(!i2c_flag_get(I2C0, I2C_FLAG_TBE)); for(i = 0; i < 16; i++) { /* send a data byte */ i2c_data_transmit(I2C0, i2c_transmitter[i]); /* wait until the transmission data register is empty */ while(!i2c_flag_get(I2C0, I2C_FLAG_TBE)); } /* the master doesn't acknowledge for the last byte */ while(!i2c_flag_get(I2C0, I2C_FLAG_AERR)); /* clear the bit of AERR */ i2c_flag_clear(I2C0, I2C_FLAG_AERR); while(1) { } } /*! \brief enable the peripheral clock \param[in] none \param[out] none \retval none */ void rcu_config(void) { /* enable GPIOB clock */ rcu_periph_clock_enable(RCU_GPIOB); /* enable I2C0 clock */ rcu_periph_clock_enable(RCU_I2C0); } /*! \brief cofigure the GPIO ports \param[in] none \param[out] none \retval none */ void gpio_config(void) { /* connect PB6 to I2C0_SCL */ /* connect PB7 to I2C0_SDA */ gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7); } /*! \brief cofigure the I2C0 interfaces \param[in] none \param[out] none \retval none */ void i2c_config(void) { /* cofigure I2C clock */ i2c_clock_config(I2C0, 400000, I2C_DTCY_2); /* cofigure I2C address */ #if I2C_10BIT_ADDRESS i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_10BITS, I2C0_OWN_ADDRESS10); #else i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_OWN_ADDRESS7); #endif /* enable I2C0 */ i2c_enable(I2C0); /* enable acknowledge */ i2c_ack_config(I2C0, I2C_ACK_ENABLE); }