123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- /*!
- \file at24cxx.c
- \brief the read and write function file
- \version 2022-05-30, V1.0.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2022, 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 "at24cxx.h"
- #include "i2c.h"
- #include <stdio.h>
- #define EEPROM_BLOCK0_ADDRESS 0xA0
- #define BUFFER_SIZE 256
- uint16_t eeprom_address;
- /*!
- \brief I2C read and write functions
- \param[in] none
- \param[out] none
- \retval I2C_OK or I2C_FAIL
- */
- uint8_t i2c_24c02_test(void)
- {
- uint16_t i;
- uint8_t i2c_buffer_write[BUFFER_SIZE];
- uint8_t i2c_buffer_read[BUFFER_SIZE];
- printf("\r\nAT24C02 writing...\r\n");
- /* initialize i2c_buffer_write */
- for(i = 0; i < BUFFER_SIZE; i++) {
- i2c_buffer_write[i] = i;
- printf("0x%02X ", i2c_buffer_write[i]);
- if(15 == i % 16) {
- printf("\r\n");
- }
- }
- /* EEPROM data write */
- eeprom_buffer_write_timeout(i2c_buffer_write, EEP_FIRST_PAGE, BUFFER_SIZE);
- printf("AT24C02 reading...\r\n");
- /* EEPROM data read */
- eeprom_buffer_read_timeout(i2c_buffer_read, EEP_FIRST_PAGE, BUFFER_SIZE);
- /* compare the read buffer and write buffer */
- for(i = 0; i < BUFFER_SIZE; i++) {
- if(i2c_buffer_read[i] != i2c_buffer_write[i]) {
- printf("0x%02X ", i2c_buffer_read[i]);
- printf("Err:data read and write aren't matching.\n\r");
- return I2C_FAIL;
- }
- printf("0x%02X ", i2c_buffer_read[i]);
- if(15 == i % 16) {
- printf("\r\n");
- }
- }
- printf("I2C-AT24C02 test passed!\n\r");
- return I2C_OK;
- }
- /*!
- \brief initialize peripherals used by the I2C EEPROM driver
- \param[in] none
- \param[out] none
- \retval none
- */
- void i2c_eeprom_init(void)
- {
- eeprom_address = EEPROM_BLOCK0_ADDRESS;
- }
- /*!
- \brief write one byte to the EEPROM and use timeout function
- \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
- \param[in] write_address: EEPROM's internal address to write to
- \param[out] none
- \retval none
- */
- uint8_t eeprom_byte_write_timeout(uint8_t *p_buffer, uint8_t write_address)
- {
- uint8_t state = I2C_START;
- uint16_t timeout = 0;
- uint8_t i2c_timeout_flag = 0;
- /* enable acknowledge */
- i2c_ack_config(I2CX, I2C_ACK_ENABLE);
- while(!(i2c_timeout_flag)) {
- switch(state) {
- case I2C_START:
- /* i2c master sends start signal only when the bus is idle */
- while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_start_on_bus(I2CX);
- timeout = 0;
- state = I2C_SEND_ADDRESS;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c bus is busy in WRITE BYTE!\n");
- }
- break;
- case I2C_SEND_ADDRESS:
- /* i2c master sends START signal successfully */
- while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
- timeout = 0;
- state = I2C_CLEAR_ADDRESS_FLAG;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends start signal timeout in WRITE BYTE!\n");
- }
- break;
- case I2C_CLEAR_ADDRESS_FLAG:
- /* address flag set means i2c slave sends ACK */
- while((! i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
- timeout = 0;
- state = I2C_TRANSMIT_DATA;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master clears address flag timeout in WRITE BYTE!\n");
- }
- break;
- case I2C_TRANSMIT_DATA:
- /* wait until the transmit data buffer is empty */
- while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- /* send the EEPROM's internal address to write to : only one byte address */
- i2c_data_transmit(I2CX, write_address);
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends data timeout in WRITE BYTE!\n");
- }
- /* wait until BTC bit is set */
- while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- /* send the EEPROM's internal address to write to : only one byte address */
- i2c_data_transmit(I2CX, *p_buffer);
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends data timeout in WRITE BYTE!\n");
- }
- /* wait until BTC bit is set */
- while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- state = I2C_STOP;
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends data timeout in WRITE BYTE!\n");
- }
- break;
- case I2C_STOP:
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- /* i2c master sends STOP signal successfully */
- while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_END;
- i2c_timeout_flag = I2C_OK;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends stop signal timeout in WRITE BYTE!\n");
- }
- break;
- default:
- state = I2C_START;
- i2c_timeout_flag = I2C_OK;
- timeout = 0;
- printf("i2c master sends start signal in WRITE BYTE.\n");
- break;
- }
- }
- return I2C_END;
- }
- /*!
- \brief write more than one byte to the EEPROM with a single write cycle
- \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
- \param[in] write_address: EEPROM's internal address to write to
- \param[in] number_of_byte: number of bytes to write to the EEPROM
- \param[out] none
- \retval none
- */
- uint8_t eeprom_page_write_timeout(uint8_t *p_buffer, uint8_t write_address, uint8_t number_of_byte)
- {
- uint8_t state = I2C_START;
- uint16_t timeout = 0;
- uint8_t i2c_timeout_flag = 0;
- /* enable acknowledge */
- i2c_ack_config(I2CX, I2C_ACK_ENABLE);
- while(!(i2c_timeout_flag)) {
- switch(state) {
- case I2C_START:
- /* i2c master sends start signal only when the bus is idle */
- while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_start_on_bus(I2CX);
- timeout = 0;
- state = I2C_SEND_ADDRESS;
- } else {
- i2c_bus_reset();
- timeout = 0;
- state = I2C_START;
- printf("i2c bus is busy in WRITE!\n");
- }
- break;
- case I2C_SEND_ADDRESS:
- /* i2c master sends START signal successfully */
- while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
- timeout = 0;
- state = I2C_CLEAR_ADDRESS_FLAG;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends start signal timeout in WRITE!\n");
- }
- break;
- case I2C_CLEAR_ADDRESS_FLAG:
- /* address flag set means i2c slave sends ACK */
- while((! i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
- timeout = 0;
- state = I2C_TRANSMIT_DATA;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master clears address flag timeout in WRITE!\n");
- }
- break;
- case I2C_TRANSMIT_DATA:
- /* wait until the transmit data buffer is empty */
- while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- /* send the EEPROM's internal address to write to : only one byte address */
- i2c_data_transmit(I2CX, write_address);
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends EEPROM's internal address timeout in WRITE!\n");
- }
- /* wait until BTC bit is set */
- while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends data timeout in WRITE!\n");
- }
- while(number_of_byte--) {
- i2c_data_transmit(I2CX, *p_buffer);
- /* point to the next byte to be written */
- p_buffer++;
- /* wait until BTC bit is set */
- while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends data timeout in WRITE!\n");
- }
- }
- timeout = 0;
- state = I2C_STOP;
- break;
- case I2C_STOP:
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- /* i2c master sends STOP signal successfully */
- while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_END;
- i2c_timeout_flag = I2C_OK;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends stop signal timeout in WRITE!\n");
- }
- break;
- default:
- state = I2C_START;
- i2c_timeout_flag = I2C_OK;
- timeout = 0;
- printf("i2c master sends start signal in WRITE.\n");
- break;
- }
- }
- return I2C_END;
- }
- /*!
- \brief write buffer of data to the EEPROM use timeout function
- \param[in] p_buffer: pointer to the buffer containing the data to be written to the EEPROM
- \param[in] write_address: EEPROM's internal address to write to
- \param[in] number_of_byte: number of bytes to write to the EEPROM
- \param[out] none
- \retval none
- */
- void eeprom_buffer_write_timeout(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
- {
- uint8_t number_of_page = 0, number_of_single = 0, address = 0, count = 0;
- address = write_address % I2C_PAGE_SIZE;
- count = I2C_PAGE_SIZE - address;
- number_of_page = number_of_byte / I2C_PAGE_SIZE;
- number_of_single = number_of_byte % I2C_PAGE_SIZE;
- /* if write_address is I2C_PAGE_SIZE aligned */
- if(0 == address) {
- while(number_of_page--) {
- eeprom_page_write_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
- eeprom_wait_standby_state_timeout();
- write_address += I2C_PAGE_SIZE;
- p_buffer += I2C_PAGE_SIZE;
- }
- if(0 != number_of_single) {
- eeprom_page_write_timeout(p_buffer, write_address, number_of_single);
- eeprom_wait_standby_state_timeout();
- }
- } else {
- /* if write_address is not I2C_PAGE_SIZE aligned */
- if(number_of_byte < count) {
- eeprom_page_write_timeout(p_buffer, write_address, number_of_byte);
- eeprom_wait_standby_state_timeout();
- } else {
- number_of_byte -= count;
- number_of_page = number_of_byte / I2C_PAGE_SIZE;
- number_of_single = number_of_byte % I2C_PAGE_SIZE;
- if(0 != count) {
- eeprom_page_write_timeout(p_buffer, write_address, count);
- eeprom_wait_standby_state_timeout();
- write_address += count;
- p_buffer += count;
- }
- /* write page */
- while(number_of_page--) {
- eeprom_page_write_timeout(p_buffer, write_address, I2C_PAGE_SIZE);
- eeprom_wait_standby_state_timeout();
- write_address += I2C_PAGE_SIZE;
- p_buffer += I2C_PAGE_SIZE;
- }
- /* write single */
- if(0 != number_of_single) {
- eeprom_page_write_timeout(p_buffer, write_address, number_of_single);
- eeprom_wait_standby_state_timeout();
- }
- }
- }
- }
- /*!
- \brief read data from the EEPROM
- \param[in] p_buffer: pointer to the buffer that receives the data read from the EEPROM
- \param[in] read_address: EEPROM's internal address to start reading from
- \param[in] number_of_byte: number of bytes to reads from the EEPROM
- \param[out] none
- \retval none
- */
- uint8_t eeprom_buffer_read_timeout(uint8_t *p_buffer, uint8_t read_address, uint16_t number_of_byte)
- {
- uint8_t state = I2C_START;
- uint8_t read_cycle = 0;
- uint16_t timeout = 0;
- uint8_t i2c_timeout_flag = 0;
- /* enable acknowledge */
- i2c_ack_config(I2CX, I2C_ACK_ENABLE);
- while(!(i2c_timeout_flag)) {
- switch(state) {
- case I2C_START:
- if(RESET == read_cycle) {
- /* i2c master sends start signal only when the bus is idle */
- while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- /* whether to send ACK or not for the next byte */
- if(2 == number_of_byte) {
- i2c_ackpos_config(I2CX, I2C_ACKPOS_NEXT);
- }
- } else {
- i2c_bus_reset();
- timeout = 0;
- state = I2C_START;
- printf("i2c bus is busy in READ!\n");
- }
- }
- /* send the start signal */
- i2c_start_on_bus(I2CX);
- timeout = 0;
- state = I2C_SEND_ADDRESS;
- break;
- case I2C_SEND_ADDRESS:
- /* i2c master sends START signal successfully */
- while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- if(RESET == read_cycle) {
- i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
- state = I2C_CLEAR_ADDRESS_FLAG;
- } else {
- i2c_master_addressing(I2CX, eeprom_address, I2C_RECEIVER);
- if(number_of_byte < 3) {
- /* disable acknowledge */
- i2c_ack_config(I2CX, I2C_ACK_DISABLE);
- }
- state = I2C_CLEAR_ADDRESS_FLAG;
- }
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master sends start signal timeout in READ!\n");
- }
- break;
- case I2C_CLEAR_ADDRESS_FLAG:
- /* address flag set means i2c slave sends ACK */
- while((!i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
- if((SET == read_cycle) && (1 == number_of_byte)) {
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- }
- timeout = 0;
- state = I2C_TRANSMIT_DATA;
- } else {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master clears address flag timeout in READ!\n");
- }
- break;
- case I2C_TRANSMIT_DATA:
- if(RESET == read_cycle) {
- /* wait until the transmit data buffer is empty */
- while((! i2c_flag_get(I2CX, I2C_FLAG_TBE)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- /* send the EEPROM's internal address to write to : only one byte address */
- i2c_data_transmit(I2CX, read_address);
- timeout = 0;
- } else {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master wait data buffer is empty timeout in READ!\n");
- }
- /* wait until BTC bit is set */
- while((!i2c_flag_get(I2CX, I2C_FLAG_BTC)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_START;
- read_cycle++;
- } else {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master sends EEPROM's internal address timeout in READ!\n");
- }
- } else {
- while(number_of_byte) {
- timeout++;
- if(3 == number_of_byte) {
- /* wait until BTC bit is set */
- while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
- /* disable acknowledge */
- i2c_ack_config(I2CX, I2C_ACK_DISABLE);
- }
- if(2 == number_of_byte) {
- /* wait until BTC bit is set */
- while(!i2c_flag_get(I2CX, I2C_FLAG_BTC));
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- }
- /* wait until RBNE bit is set */
- if(i2c_flag_get(I2CX, I2C_FLAG_RBNE)) {
- /* read a byte from the EEPROM */
- *p_buffer = i2c_data_receive(I2CX);
- /* point to the next location where the byte read will be saved */
- p_buffer++;
- /* decrement the read bytes counter */
- number_of_byte--;
- timeout = 0;
- }
- if(timeout > I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master sends data timeout in READ!\n");
- }
- }
- timeout = 0;
- state = I2C_STOP;
- }
- break;
- case I2C_STOP:
- /* i2c master sends STOP signal successfully */
- while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_END;
- i2c_timeout_flag = I2C_OK;
- } else {
- timeout = 0;
- state = I2C_START;
- read_cycle = 0;
- printf("i2c master sends stop signal timeout in READ!\n");
- }
- break;
- default:
- state = I2C_START;
- read_cycle = 0;
- i2c_timeout_flag = I2C_OK;
- timeout = 0;
- printf("i2c master sends start signal in READ.\n");
- break;
- }
- }
- return I2C_END;
- }
- /*!
- \brief wait for EEPROM standby state use timeout function
- \param[in] none
- \param[out] none
- \retval none
- */
- uint8_t eeprom_wait_standby_state_timeout()
- {
- uint8_t state = I2C_START;
- uint16_t timeout = 0;
- uint8_t i2c_timeout_flag = 0;
- while(!(i2c_timeout_flag)) {
- switch(state) {
- case I2C_START:
- /* i2c master sends start signal only when the bus is idle */
- while(i2c_flag_get(I2CX, I2C_FLAG_I2CBSY) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_start_on_bus(I2CX);
- timeout = 0;
- state = I2C_SEND_ADDRESS;
- } else {
- i2c_bus_reset();
- timeout = 0;
- state = I2C_START;
- printf("i2c bus is busy in EEPROM standby!\n");
- }
- break;
- case I2C_SEND_ADDRESS:
- /* i2c master sends START signal successfully */
- while((! i2c_flag_get(I2CX, I2C_FLAG_SBSEND)) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- i2c_master_addressing(I2CX, eeprom_address, I2C_TRANSMITTER);
- timeout = 0;
- state = I2C_CLEAR_ADDRESS_FLAG;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends start signal timeout in EEPROM standby!\n");
- }
- break;
- case I2C_CLEAR_ADDRESS_FLAG:
- while((!((i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) || (i2c_flag_get(I2CX, I2C_FLAG_AERR)))) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- if(i2c_flag_get(I2CX, I2C_FLAG_ADDSEND)) {
- i2c_flag_clear(I2CX, I2C_FLAG_ADDSEND);
- timeout = 0;
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- i2c_timeout_flag = I2C_OK;
- /* exit the function */
- return I2C_END;
- } else {
- /* clear the bit of AE */
- i2c_flag_clear(I2CX, I2C_FLAG_AERR);
- timeout = 0;
- state = I2C_STOP;
- }
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master clears address flag timeout in EEPROM standby!\n");
- }
- break;
- case I2C_STOP:
- /* send a stop condition to I2C bus */
- i2c_stop_on_bus(I2CX);
- /* i2c master sends STOP signal successfully */
- while((I2C_CTL0(I2CX) & I2C_CTL0_STOP) && (timeout < I2C_TIME_OUT)) {
- timeout++;
- }
- if(timeout < I2C_TIME_OUT) {
- timeout = 0;
- state = I2C_START;
- } else {
- timeout = 0;
- state = I2C_START;
- printf("i2c master sends stop signal timeout in EEPROM standby!\n");
- }
- break;
- default:
- state = I2C_START;
- timeout = 0;
- printf("i2c master sends start signal end in EEPROM standby!.\n");
- break;
- }
- }
- return I2C_END;
- }
|