I2C0_IE.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*!
  2. \file I2C0_IE.c
  3. \brief I2C0 master receiver interrupt program
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x_i2c.h"
  33. #include "I2C_IE.h"
  34. uint32_t event1;
  35. /*!
  36. \brief handle I2C0 event interrupt request
  37. \param[in] none
  38. \param[out] none
  39. \retval none
  40. */
  41. void I2C0_EventIRQ_Handler(void)
  42. {
  43. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_SBSEND)) {
  44. /* the master sends slave address */
  45. i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_RECEIVER);
  46. } else if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_ADDSEND)) {
  47. if((1 == I2C_nBytes) || (2 == I2C_nBytes)) {
  48. /* clear the ACKEN before the ADDSEND is cleared */
  49. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  50. /* clear the ADDSEND bit */
  51. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_ADDSEND);
  52. } else {
  53. /* clear the ADDSEND bit */
  54. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_ADDSEND);
  55. }
  56. } else if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_RBNE)) {
  57. if(I2C_nBytes > 0) {
  58. if(3 == I2C_nBytes) {
  59. /* wait until the second last data byte is received into the shift register */
  60. while(!i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_BTC));
  61. /* send a NACK for the last data byte */
  62. i2c_ack_config(I2C0, I2C_ACK_DISABLE);
  63. }
  64. /* read a data byte from I2C_DATA*/
  65. *i2c_rxbuffer++ = i2c_data_receive(I2C0);
  66. I2C_nBytes--;
  67. if(0 == I2C_nBytes) {
  68. /* send a stop condition */
  69. i2c_stop_on_bus(I2C0);
  70. status = SUCCESS;
  71. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  72. i2c_ackpos_config(I2C0, I2C_ACKPOS_CURRENT);
  73. /* disable the I2C0 interrupt */
  74. i2c_interrupt_disable(I2C0, I2C_INT_ERR);
  75. i2c_interrupt_disable(I2C0, I2C_INT_BUF);
  76. i2c_interrupt_disable(I2C0, I2C_INT_EV);
  77. }
  78. }
  79. }
  80. }
  81. /*!
  82. \brief handle I2C0 error interrupt request
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void I2C0_ErrorIRQ_Handler(void)
  88. {
  89. /* no acknowledge received */
  90. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_AERR)) {
  91. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_AERR);
  92. }
  93. /* SMBus alert */
  94. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_SMBALT)) {
  95. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_SMBALT);
  96. }
  97. /* bus timeout in SMBus mode */
  98. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_SMBTO)) {
  99. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_SMBTO);
  100. }
  101. /* over-run or under-run when SCL stretch is disabled */
  102. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_OUERR)) {
  103. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_OUERR);
  104. }
  105. /* arbitration lost */
  106. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_LOSTARB)) {
  107. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_LOSTARB);
  108. }
  109. /* bus error */
  110. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_BERR)) {
  111. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_BERR);
  112. }
  113. /* CRC value doesn't match */
  114. if(i2c_interrupt_flag_get(I2C0, I2C_INT_FLAG_PECERR)) {
  115. i2c_interrupt_flag_clear(I2C0, I2C_INT_FLAG_PECERR);
  116. }
  117. /* disable the I2C0 interrupt */
  118. i2c_interrupt_disable(I2C0, I2C_INT_ERR);
  119. i2c_interrupt_disable(I2C0, I2C_INT_BUF);
  120. i2c_interrupt_disable(I2C0, I2C_INT_EV);
  121. }