i2ca.c 26.7 KB
Newer Older
时昊's avatar
时昊 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
/**
  ******************************************************************************
  * @file    i2ca.c
  * @author  CMS Application Team
  * @version Vx.x.x
  * @date    24-April-2022
  * @brief   This file provides firmware functions to manage the following 
  *          functionalities of the Universal Inter Integrated Circuit (I2CA):           
  @verbatim       
 ===============================================================================
                        ##### How to use this driver #####
 ===============================================================================
    [..]
            
    @endverbatim        
  ******************************************************************************
  * @attention
  *
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "i2ca.h"
#include "cgc.h"

/** @addtogroup bat32g135_StdPeriph_Driver
  * @{
  */

/** @defgroup I2CA 
  * @brief I2CA driver modules
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define I2CA_MAX_CLOCK  (20000000U)
#define I2CA_DIR_TRANSMIT   0x01
#define I2CA_DIR_RECEIVE    0x02

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
IRQn_Type I2CA_IRQTable[I2CA_BUS_MAX_NUM] = 
{IICA0_IRQn, IICA1_IRQn};

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  I2CA module Deinit.
  * @param  None
  * @retval None
  */
void I2CA_DeInit(I2CASelect_TypeDef I2CAx)
{
	if(I2CAx == I2CA0)
	{
		CGC_PER0PeriphClockCmd(CGC_PER0Periph_I2CA0, DISABLE);
	}
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)	
	else if(I2CAx == I2CA1)
	{
		CGC_PER2PeriphClockCmd(CGC_PER2Periph_I2CA1, DISABLE);
	}
#endif
}

/**
  * @brief  The specified UART1 peripheral initialization and set baud rate by UART_BAUD_t.
  * @param  UART_Instance: UART peripheral register base address.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure that contains
  *         the configuration information for the specified UART peripheral.
  * @param  pValue: UART baud rate setting option data structure.
  * @retval None
  */
static void IICA_Init(IICA_TypeDef *IICA_Instance, I2CA_InitTypeDef* I2CA_InitStruct)
{
    uint8_t tmpreg = 0;
    uint8_t tmp_wl, tmp_wh;
    uint32_t fmck;
	
	tmpreg = IICA_Instance->IICCTL1;
    /* I2CA register setting for control register n1 */
    tmpreg = IICA_Instance->IICCTL1;
    if (I2CA_InitStruct->I2C_ClockSpeed <= 100000)
    {
        /* operates in standard mode */
        tmpreg &= ~(1 << I2CA_OPERATE_BIT);
        tmpreg &= ~(1 << I2CA_DIGITAL_FILTER_BIT);
    }
    else
    {
        /* operates in fast mode and open filter */
        tmpreg |=  (1 << I2CA_OPERATE_BIT);
        tmpreg |=  (1 << I2CA_DIGITAL_FILTER_BIT);
    }

    /* clock for I2CA division select */
    if (SystemCoreClock >= 1000000 && SystemCoreClock <= I2CA_MAX_CLOCK)
    {
        tmpreg &= ~(1 << I2CA_FCLK_DIV_BIT);
        fmck = SystemCoreClock;
    }
    else if (SystemCoreClock > I2CA_MAX_CLOCK)
    {
        tmpreg |=  (1 << I2CA_FCLK_DIV_BIT);
        fmck = (SystemCoreClock / 2) ;
    }

    IICA_Instance->IICCTL1 = tmpreg;

    /* Own address setting for I2C bus */
    IICA_Instance->SVA = I2CA_InitStruct->I2C_OwnAddress;

    /* I2CA register setting for flag register n */
    tmpreg = IICA_Instance->IICF0;

    /* enable generation of a start condition without detecting a stop condition */
    tmpreg |=  (1 << I2CA_STOP_DETECTION_BIT);

    /* enable  Communication reservation */
    tmpreg |=  (1 << I2CA_RESERVATION_BIT);

    IICA_Instance->IICF0 = tmpreg;

    /* I2CA bus clock option setting */
    if (I2CA_InitStruct->I2C_ClockSpeed <= 100000)
    {
        if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusSlave)
        {
            tmp_wl = 4.7 * fmck / 1000000;
            tmp_wh = 5.3 * fmck / 1000000;
        }
        else if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusMaster)
        {
            tmp_wl = (fmck) * 0.47 / (I2CA_InitStruct->I2C_ClockSpeed);
            tmp_wh = (fmck) * 0.53 / (I2CA_InitStruct->I2C_ClockSpeed);
        }
    }
    else if (I2CA_InitStruct->I2C_ClockSpeed <= 400000)
    {
        if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusSlave)
        {
            tmp_wl = 1.3 * fmck / 1000000;
            tmp_wh = 1.2 * fmck / 1000000;
        }
        else if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusMaster)
        {
            tmp_wl = (fmck) * 0.52 / (I2CA_InitStruct->I2C_ClockSpeed);
            tmp_wh = (fmck) * 0.48 / (I2CA_InitStruct->I2C_ClockSpeed);
        }
    }
    else
    {
        if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusSlave)
        {
            tmp_wl = 0.5 * fmck / 1000000;
            tmp_wh = 0.5 * fmck / 1000000;
        }
        else if (I2CA_InitStruct->I2C_Mode == I2CA_Mode_SMBusMaster)
        {
            tmp_wl = (fmck) * 0.5 / (I2CA_InitStruct->I2C_ClockSpeed);
            tmp_wh = (fmck) * 0.5 / (I2CA_InitStruct->I2C_ClockSpeed);
        }
    }

    /* IICWL0 and IICWH0 register setting to config clock square option */
    IICA_Instance->IICWL = tmp_wl;
    IICA_Instance->IICWH = tmp_wh;

    /* I2CA bus option setting for control register n0 */
    /* interrupt request is generated at the ninth clock's falling edge */
    IICA_Instance->IICCTL0 |=  (1 << I2CA_WTIM_BIT);

    /* acknowledgment config */
    if (I2CA_InitStruct->I2C_Ack)
        IICA_Instance->IICCTL0 |=  (1 << I2CA_ACK_BIT);
    else
        IICA_Instance->IICCTL0 &= ~(1 << I2CA_ACK_BIT);

    /* enable operation */
    IICA_Instance->IICCTL0 |=  (1 << I2CA_EN_BIT);

    /* this exits from the current communications and sets standby mode */
    IICA_Instance->IICCTL0 |=  (1 << I2CA_LREL_BIT);
	
}
/**
  * @brief  Initializes the I2CA peripheral according to the specified
  *         parameters in the I2CA_InitStruct .
  * @param  I2CA_InitStruct: pointer to a I2CA_InitTypeDef structure that contains
  *         the configuration information for the specified I2CA peripheral.
  * @retval None
  */
void I2CA_Init(I2CASelect_TypeDef I2CAx, I2CA_InitTypeDef* I2CA_InitStruct)
{
    /* Check the parameters */
    assert_param(IS_I2CA_CLOCK_SPEED(I2CA_InitStruct->I2C_ClockSpeed));
    assert_param(IS_I2CA_MODE(I2CA_InitStruct->I2C_Mode));
    assert_param(IS_I2CA_DUTY_CYCLE(I2CA_InitStruct->I2C_DutyCycle));
    assert_param(IS_I2CA_OWN_ADDRESS(I2CA_InitStruct->I2C_OwnAddress));
    assert_param(IS_I2CA_ACK_STATE(I2CA_InitStruct->I2C_Ack));

	switch(I2CAx)
	{
		case I2CA0:
		{
			CGC_PER0PeriphClockCmd(CGC_PER0Periph_I2CA0, ENABLE);			
			IICA_TypeDef *I2CA_Instance = IICA0;	
			IICA_Init(I2CA_Instance,I2CA_InitStruct);			
		}
		break;
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)	
		case I2CA1:
		{
			CGC_PER2PeriphClockCmd(CGC_PER2Periph_I2CA1, ENABLE);			
			IICA_TypeDef *I2CA_Instance = IICA1;
			IICA_Init(I2CA_Instance,I2CA_InitStruct);	
		}
		break;
#endif
		default:
			break;
	}
}
/**
  * @brief  Enables or disables the specified I2CA peripheral.
  * @param  NewState: new state of the I2CA peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2CA_Cmd(I2CASelect_TypeDef I2CAx,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));
	

	if (NewState != DISABLE)
	{
		/* Enable the I2CA periphal by setting the SPIE register */
		I2CA_SET_REG(I2CAx,IICCTL00,IICA0_IICCTL00_SPIE_Pos);
	}
	else
	{
		/* Disable the I2CA periphal by setting the SPIE register */
		I2CA_CLEAR_REG(I2CAx,IICCTL00,IICA0_IICCTL00_SPIE_Pos);
	}

}

/**
  * @brief  Enables or disables the specified I2CA acknowledgement.
  * @param  NewState: new state of the I2CA acknowledgement.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
void I2CA_Acknowledge_Cmd(I2CASelect_TypeDef I2CAx,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));

	if (NewState != DISABLE)
	{
		/* Enable the I2CA acknowledgement by setting the ACKE register */
		I2CA_SET_REG(I2CAx,IICCTL00,IICA0_IICCTL00_ACKE_Pos);
	}
	else
	{
		/* Disable the I2CA acknowledgement by setting the ACKE register */
		I2CA_CLEAR_REG(I2CAx,IICCTL00,IICA0_IICCTL00_ACKE_Pos);
	}

}

/**
  * @brief  Configurate the Interrupt positon depend on the register WTIM bit in IICCTL00.
  * @param  Intpos: Interrupt position config.
  *          This parameter can be one of the following values:
  *            @arg I2CA_INTPOS_8CLK	: In the 8th clock to generate intterupt signal.
  *            @arg I2CA_INTPOS_9CLK	: In the 9th clock to generate intterupt signal.
  * @retval None
  */
void I2CA_InterruptPosition_Config(I2CASelect_TypeDef I2CAx,uint8_t Intpos)
{
    /* Check the parameters */
    assert_param(IS_I2CA_INTPOS(Intpos));

    /* 
    Generate the interrupt signal int the 8th clock falling edge.
    Master role : when the Nth clock is completed, set the clock is low to wait.
    Slave role  : when the Nth clock is completed, set the clock is low to wait master.
    Note : N is depend on param intpos.
    */	
	if (Intpos == I2CA_INTPOS_8CLK)
	{
		I2CA_CLEAR_REG(I2CAx,IICCTL00,IICA0_IICCTL00_WTIM_Pos);			
	}
	else if (Intpos == I2CA_INTPOS_9CLK)
	{
		I2CA_SET_REG(I2CAx,IICCTL00,IICA0_IICCTL00_WTIM_Pos);						
	}
}

/**
  * @brief  To relieve the wait for I2CA bus state
  * @param  NewState: new state of the I2CA waiting relieve.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  * @Note   the WREL bit will auto reset when the relieve is completed.
  */
void I2CA_WaitRelieve_Cmd(I2CASelect_TypeDef I2CAx,FunctionalState NewState)
{
	/* Check the parameters */
	assert_param(IS_FUNCTIONAL_STATE(NewState));


	if (NewState != DISABLE)
	{
		/* Relieve the wait for bus state */
		I2CA_SET_REG(I2CAx,IICCTL00,IICA0_IICCTL00_WREL_Pos);						
	}
	else
	{
		I2CA_CLEAR_REG(I2CAx,IICCTL00,IICA0_IICCTL00_WREL_Pos);			
	}

}

/**
  * @brief  Configurate the I2CA own address to SVA0 register.
  * @param  Address: Set the device address to register.
  * @retval None
  */
void I2CA_OwnAddress_Config(I2CASelect_TypeDef I2CAx,uint8_t Address)
{
	/* Check the parameters */
	assert_param(IS_I2CA_OWN_ADDRESS(Address));
	
	IICA_TypeDef *I2CA_Instance;
	
	if(I2CAx == I2CA0)	
	{
		I2CA_Instance = (IICA_TypeDef *)IICA0;
	}	
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)
	else if(I2CAx == I2CA1)
	{
		I2CA_Instance = (IICA_TypeDef *)IICA1;
	}	
#endif
	I2CA_Instance->SVA = Address;	
}

/**
  * @}
  */

/** @defgroup I2CA Data transfers functions
 *  @brief   Data transfers functions
 *
@verbatim   
 ===============================================================================
                      ##### Data transfers functions #####
 ===============================================================================  

 [..] This section provides a set of functions allowing to manage the I2CA data 
      transfers. In reception, data are received and then stored into an internal 
      buffer while. In transmission, data are first stored into an internal send
      buffer before being transmitted.

 [..] The read access of the IICA0 register can be done using the I2CA_ReadByte()
      function and returns the Rx register value. Whereas a write access to the IICA0 
      can be done using I2CA_WriteByte() function and stores the written data into 
      Tx register.

@endverbatim
  * @{
  */

/**
  * @brief  Read the register data for IICA0.
  * @param  None.
  * @retval The data value for IICA0.
  */
uint8_t I2CA_ReadByte(I2CASelect_TypeDef I2CAx)
{
	IICA_TypeDef *I2CA_Instance;
	
	if(I2CAx == I2CA0)	
	{
		I2CA_Instance = (IICA_TypeDef *)IICA0;
	}
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)
	else if(I2CAx == I2CA1)
	{
		I2CA_Instance = (IICA_TypeDef *)IICA1;
	}
#endif
	return (I2CA_Instance->IICA);
}

/**
  * @brief  Write the data to IICA0 register.
  * @param  The data which to be write.
  * @retval None.
  */
void I2CA_WriteByte(I2CASelect_TypeDef I2CAx,uint8_t Data)
{
	IICA_TypeDef *I2CA_Instance;
	if(I2CAx == I2CA0)	
	{
		I2CA_Instance = (IICA_TypeDef *)IICA0;
	}
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)
	else if(I2CAx == I2CA1)
	{
		I2CA_Instance = (IICA_TypeDef *)IICA1;
	}
#endif
	I2CA_Instance->IICA = Data;		

}

/**
  * @brief  Generates I2CA communication START condition.
  * @param  None
  * @retval None.
  */
void I2CA_GenerateSTART(I2CASelect_TypeDef I2CAx)
{
    /* Generate a START condition */
	I2CA_SET_REG(I2CAx,IICCTL00,I2CA_START_BIT);						
}

/**
  * @brief  Generates I2CA communication STOP condition.
  * @param  None
  * @retval None.
  */
void I2CA_GenerateSTOP(I2CASelect_TypeDef I2CAx)
{
    /* Generate a STOP condition */
	I2CA_SET_REG(I2CAx,IICCTL00,I2CA_STOP_BIT);						
}

/**
  * @brief  Checks whether the specified I2CA flag is set or not.
  * @param  I2CA_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *            @arg I2CA_STATUS_SPD	: the STOP condition check status.
  *            @arg I2CA_STATUS_STD	: the START condition check status.
  *            @arg I2CA_STATUS_ACK	: the ACK condition check status.
  *            @arg I2CA_STATUS_TRC	: bus status in transmit(is 1) or receive(is 0) mode.
  *            @arg I2CA_STATUS_COI	: check the address is matched.
  *            @arg I2CA_STATUS_EXC	: the extend code received or not status.
  *            @arg I2CA_STATUS_ALD	: the arbitrate success(is 0) or fail(is 1).
  *            @arg I2CA_STATUS_MSTS: to display the communication role is master(is 1) or slave(is 0).
  *            @arg I2CA_STATUS_BUSBSY: I2C bus is busy or idle.
  *            @arg I2CA_STATUS_STCF: Generate START condition or not.
  * @retval The new state of I2CA_FLAG (SET or RESET).
  */
FlagStatus I2CA_GetFlagStaus(I2CASelect_TypeDef I2CAx,uint16_t I2CA_FLAG)
{
    FlagStatus bitstatus;
	uint8_t tmp_reg = 0;
    uint8_t tmp_flag = 0;

    /* Check the parameters */
    assert_param(IS_I2CA_STATUS(I2CA_FLAG));

	if(I2CAx == I2CA0)	
	{
		if (I2CA_FLAG == I2CA_STATUS_BUSBSY || I2CA_FLAG == I2CA_STATUS_STCF)
		{
			tmp_reg = IICA0->IICF0;
			tmp_flag = (uint8_t)(I2CA_FLAG >> 8);
		}
		else
		{
			tmp_reg = IICA0->IICS0;
			tmp_flag = (uint8_t)(I2CA_FLAG & 0xFF);
		}		
	}
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)
	else if(I2CAx == I2CA1)
	{
		if (I2CA_FLAG == I2CA_STATUS_BUSBSY || I2CA_FLAG == I2CA_STATUS_STCF)
		{
			tmp_reg = IICA1->IICF0;
			tmp_flag = (uint8_t)(I2CA_FLAG >> 8);
		}
		else
		{
			tmp_reg = IICA1->IICS0;
			tmp_flag = (uint8_t)(I2CA_FLAG & 0xFF);
		}		
	}
#endif
    if ((tmp_reg & tmp_flag) != (uint16_t)RESET)
        bitstatus = SET;
    else
        bitstatus = RESET;

    return bitstatus;
}

/**
  * @brief  Send a data buffer through the I2CA peripheral to write.
  * @param  Address: Device address in the I2C bus.
  * @param  Reg: Register address in the Device.
  * @param  Data: Data buffer address.
  * @param  Len: Data buffer length need to transmit.
  * @retval None
  */
I2CA_Status I2CA_Master_WriteData(I2CASelect_TypeDef I2CAx,uint8_t Address, uint8_t Reg, uint8_t *Data, uint16_t Len)
{
	I2CA_Status status = OK;
    IRQn_Type irq;

    /* Get the IQR number depend on the I2Cx in SCI unit */
    irq = I2CA_IRQTable[I2CAx];
	
	if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_BUSBSY) != RESET)
	{
		status = BUSY;
	}
	else
	{
		I2CA_GenerateSTART(I2CAx);

		I2CA_WriteByte(I2CAx,Address & 0xFE);
		
		while(!INTC_GetPendingIRQ(irq));
		INTC_ClearPendingIRQ(irq);
		
		I2CA_WriteByte(I2CAx,Reg);
		while(!INTC_GetPendingIRQ(irq));
		INTC_ClearPendingIRQ(irq);
		
		do
		{
			I2CA_WriteByte(I2CAx,*Data++);
			while(!INTC_GetPendingIRQ(irq));
			INTC_ClearPendingIRQ(irq);
			
		} while(--Len);
		
        /* stop condition is generated */
        I2CA_GenerateSTOP(I2CAx);
	}
	
	return status;
}

/**
  * @brief  Read a data buffer through the I2CA peripheral to device register address.
  * @param  Address: Device address in the I2C bus.
  * @param  Reg: Register address in the Device.
  * @param  Data: Data buffer address.
  * @param  Len: Data buffer length need to transmit read.
  * @retval None
  */
I2CA_Status I2CA_Master_ReadData(I2CASelect_TypeDef I2CAx,uint8_t Address, uint8_t Reg, uint8_t *Data, uint16_t Len)
{
	I2CA_Status status = OK;
    IRQn_Type irq;

    irq = I2CA_IRQTable[I2CAx];
	
	if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_BUSBSY) != RESET)
	{
		status = BUSY;
	}
	else
	{
        /* Generate the START condition */
		I2CA_GenerateSTART(I2CAx);
		
		I2CA_WriteByte(I2CAx,Address & 0xFE);		
		
        /* Wait device address send succes */
        while(!INTC_GetPendingIRQ(irq));
        INTC_ClearPendingIRQ(irq);
		
		I2CA_WriteByte(I2CAx,Reg);
        /* Wait device register send succes */
        while(!INTC_GetPendingIRQ(irq));
        INTC_ClearPendingIRQ(irq);

        /* Generate the RESTART condition */
        I2CA_GenerateSTART(I2CAx);
		
		I2CA_WriteByte(I2CAx,Address | 0x01);		
		
        /* Wait address send succes */
        while(!INTC_GetPendingIRQ(irq));
        INTC_ClearPendingIRQ(irq);
		
		do {
            /* When the last byte to receive, we should not output ACK */
            if(Len == 1U)
            {
                /* acknowledgment disable in the last byte to receive */
				I2CA_CLEAR_REG(I2CAx,IICCTL00,I2CA_ACK_BIT);
            }

            /* Release the bus status for receive data */
			I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WREL_BIT);
			
            /* Wait data receive succes */
			while(!INTC_GetPendingIRQ(irq));
			INTC_ClearPendingIRQ(irq);
			
            /* copy the receive data to target memory */
			*Data++ = I2CA_ReadByte(I2CAx);
		} while(--Len);
		
        /* STOP condition is generated */
        I2CA_GenerateSTOP(I2CAx);
	}
	
	return status;
}

/**
  * @brief  Receive a data buffer through the I2CA peripheral to device register address by slave mode.
  * @param  Data: Data buffer address.
  * @param  Size: Data size when the slave hope to recceive.
  * @param  Len: Data length in buffer received.
  * @retval status: This parameter can be a value of @ref I2CA_Status.
  */
I2CA_Status I2CA_Slave_ReceiveData(I2CASelect_TypeDef I2CAx,uint8_t *Data, uint16_t Size, uint16_t *Len)
{
	I2CA_Status status = OK;
    uint8_t Address_Match = 0;
    uint16_t Recv_Length = 0;
    IRQn_Type irq;

    irq = I2CA_IRQTable[I2CAx];

    while(1)
    {
        while(!INTC_GetPendingIRQ(irq));
        INTC_ClearPendingIRQ(irq);

        /* Check stop condition */
        if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_SPD)) 
        {
            /* Disable I2CA stop condition interrupt*/
			I2CA_CLEAR_REG(I2CAx,IICCTL00,I2CA_SPIE_BIT);
            break;
        }
        else
        {
			/* When the address match is success */
            if (!Address_Match && I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_COI))
            {
                Address_Match = 1;
				
				/* SPIE0 = 1: enable I2CA stop condition interrupt */
				I2CA_SET_REG(I2CAx,IICCTL00,I2CA_SPIE_BIT);
				
				/* interrupt request is generated at the eighth clock's falling edge */
				I2CA_CLEAR_REG(I2CAx,IICCTL00,I2CA_WTIM_BIT);
				/* cancel wait */
				I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WREL_BIT);
            }
            else
            {
				if (Recv_Length < Size)
				{
					*Data = I2CA_ReadByte(I2CAx);
					Data++;
					Recv_Length++;

					/* WREL0 = 1U: cancel wait */
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WREL_BIT);

					if(Size == Recv_Length)
					{
						/* WTIM0 = 1:  interrupt request is generated at the ninth clock's falling edge */
						I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WTIM_BIT);

                        break;
					}
				}
				else
				{
					/* When the receive data length exceed the hope length,we shoule not write data to the receive buffer */
					/* WTIM0 = 1:  interrupt request is generated at the ninth clock's falling edge */
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WTIM_BIT);
					/* WREL0 = 1U: cancel wait */
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WREL_BIT);
				}
            }
        }
    }
	
	/* Set the receive data length */
	*Len = Recv_Length;
	
	return status;
}

/**
  * @brief  Transmit a data buffer through the I2CA peripheral to device register address by slave mode.
  * @param  Data: Data buffer address.
  * @param  Size: Data size when the slave hope to recceive.
  * @retval status: This parameter can be a value of @ref I2CA_Status.
  */
I2CA_Status I2CA_Slave_TransmitData(I2CASelect_TypeDef I2CAx,uint8_t *Reg, uint8_t *Data, uint16_t Size)
{
	I2CA_Status status = OK;
	uint8_t Stop_count = 0;
    uint8_t Address_Match = 0;
    uint16_t Send_Length = 0;
    IRQn_Type irq;
	IICA_TypeDef *I2CA_Instance;
	
    irq = I2CA_IRQTable[I2CAx];	
	if(I2CAx == I2CA0)	
	{
		I2CA_Instance = (IICA_TypeDef *)IICA0;
	}
#if defined(BAT32G1XX_80PIN) || defined(BAT32G1XX_100PIN)
	else if(I2CAx == I2CA1)
	{
		I2CA_Instance = (IICA_TypeDef *)IICA1;
	}
#endif
    while(1)
    {
        while(!INTC_GetPendingIRQ(irq));
        INTC_ClearPendingIRQ(irq);

        /* Check stop condition */
        if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_SPD))
        {
			Stop_count++;
			
			/* To avoid the repeated stop condition */
			if (Stop_count == 2)
			{
				/* Disable I2CA */
				I2CA_CLEAR_REG(I2CAx,IICCTL00,I2CA_SPIE_BIT);				
				break;
			}
        }
        else
        {
			/* When the address match is success */
            if ((Address_Match < 2) && I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_COI))  //接收地址和自身从机地址一致
            {
                Address_Match ++;
				
				if (Address_Match == 1)
				{
					I2CA_Instance->SVA|= 0x01;    //第一次先读		
				} 
				else if (Address_Match == 2)
				{
					*Reg = I2CA_ReadByte(I2CAx);
					I2CA_Instance->SVA &= ~ 0x01;
				}
				
				if (!(I2CA_Instance->IICCTL0 & I2CA_SPIE_ENABLE))
				{
					/* SPIE0 = 1: enable I2CA */
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_SPIE_BIT);
				}
				
				/* Sending or receiving */
				if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_TRC))    //send status
				{
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WTIM_BIT);//在输入9个时钟,将时钟置为低电平
					I2CA_WriteByte(I2CAx,*Data);
					Data++;
					Send_Length++;
				}
				else
				{
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_ACK_BIT); /* ACKE0 = 1U: enable acknowledgment */
					I2CA_CLEAR_REG(I2CAx,IICCTL00,I2CA_WTIM_BIT);// interrupt request is generated at the eighth clock's falling edge
					I2CA_SET_REG(I2CAx,IICCTL00,I2CA_WREL_BIT); // cancel wait 
				}
            }
            else
            {
				/* Sending status */
				if (I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_TRC))
				{
					if (Send_Length == Size)
					{
						I2CA_WriteByte(I2CAx,0xFF);
					}
					else
					{
						/* Write data to IICA0 register to send it */
						I2CA_WriteByte(I2CAx,*Data);	
						Data++;
						Send_Length++;
					}
				}
            }
        }
	}
	
	return status;
}

/**
  * @brief  Transmit or receive the data which can be transmitted. This function only used by ISR.
  * @param  TxBuffer:   The transmit buffer pointer for application memory.
  * @param  TxLength:   The value pointer for transmit data length.
  * @param  RxBuffer:   The receive buffer pointer for application memory.
  * @param  RxLength:   The value pointer for received data length.
  * @param  RxTotLength:The total length of the receive buffer. 
  * @retval Event: This parameter can be a value of @ref I2CA_Slave_Event.
  */
I2CA_Slave_Event I2CA_Slave_TransmitReceive_IT(I2CASelect_TypeDef I2CAx, uint8_t *TxBuffer, uint16_t *TxLength, 
                                                uint8_t *RxBuffer, uint16_t *RxLength, uint16_t RxTotLength)
{
    I2CA_Slave_Event Event = I2C_SLAVE_EVENT_NONE;
    static uint8_t is_AddrMatch;
	static uint8_t TransmitDir;
    static uint8_t *TxData, *RxData;
    static uint16_t Length;

    /* Control for stop condition */
    if (is_AddrMatch && I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_SPD))
    {
        is_AddrMatch = 0;
        if (TransmitDir == I2CA_DIR_TRANSMIT)
        {
            TransmitDir = 0;
            Event = I2C_SLAVE_EVENT_TRANSMIT_COMPLETED;
        }
        else if (TransmitDir == I2CA_DIR_RECEIVE)
        {
            TransmitDir = 0;
            *RxLength = Length;
            Event = I2C_SLAVE_EVENT_RECEIVE_COMPLETED;
        }
        
        /* I2CA periphal disable and ACK disable for the next transmission */
        I2CA_Acknowledge_Cmd(I2CAx,DISABLE);
        I2CA_Cmd(I2CAx,DISABLE);
    }
    else
    {
        /* Address to match and get the operate for master */
		if (!is_AddrMatch && I2CA_GetFlagStaus(I2CAx,I2CA_STATUS_COI))
		{
			is_AddrMatch = 1;
			
			I2CA_Cmd(I2CAx,ENABLE);
			
			if (I2CA_ReadByte(I2CAx) & 0x01)
			{
                /* Assign the TxBuffer pointer and the data length which to be transmit. */
				TxData = TxBuffer;
                Length = *TxLength;
				
                /* Config the interrupt position and write the first byte to register */
				I2CA_InterruptPosition_Config(I2CAx,I2CA_INTPOS_9CLK);
				I2CA_WriteByte(I2CAx,*TxData);
				TxData++;
				Length--;

				/* Slave transmit operate */
				TransmitDir = I2CA_DIR_TRANSMIT;
                Event = I2C_SLAVE_EVENT_READ_ADDRESS_MATCHED;
			}
			else
			{
                /* Assign the RxBuffer pointer. */
				RxData = RxBuffer;
				Length = 0;
				
                /* Config the interrupt position and enable ACK and I2CA periphal */
				I2CA_InterruptPosition_Config(I2CAx,I2CA_INTPOS_8CLK);
                I2CA_Acknowledge_Cmd(I2CAx,ENABLE);
				I2CA_WaitRelieve_Cmd(I2CAx,ENABLE);

				/* Slave receive operate */
				TransmitDir = I2CA_DIR_RECEIVE;
                Event = I2C_SLAVE_EVENT_WRITE_ADDRESS_MATCHED;
			}
		}
		else if (is_AddrMatch)
		{
			if (TransmitDir == I2CA_DIR_TRANSMIT)
			{
                /* The transmission direction is transmit. */
				if (Length > 0U)
				{
                    /* Write send data one by one byte to the register. */
					I2CA_WriteByte(I2CAx,*TxData);
					TxData++;
					Length--;
				}
				else
				{
                    /* When the data transmit is completed, we shoule write 0xFF to pull-up the bus. */
                    /* To wait the stop condition */
					I2CA_WriteByte(I2CAx,0xFF);
				}
                Event = I2C_SLAVE_EVENT_TRANSMIT_RUNNING;
			}
			else if (TransmitDir == I2CA_DIR_RECEIVE)
			{
                /* The transmission direction is receive. */
				I2CA_WaitRelieve_Cmd(I2CAx,ENABLE);
				if (Length < RxTotLength)
				{
					*RxData = I2CA_ReadByte(I2CAx);
					RxData++;
					Length++;
				}
				else
				{
                    /* Receive data operate finished and config the interrupt position to wait the stop condition. */
					I2CA_InterruptPosition_Config(I2CAx,I2CA_INTPOS_9CLK);
				}
                Event = I2C_SLAVE_EVENT_RECEIVE_RUNNING;
			}
		}
		else
		{
			I2CA_WaitRelieve_Cmd(I2CAx,ENABLE);
            Event = I2C_SLAVE_EVENT_ERROR;
		}
    }

    return Event;
}