2
0

twi2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Copyright 2018 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "twi2c.h"
  17. #include <string.h>
  18. #include <hal.h>
  19. #include "chprintf.h"
  20. #include "memstreams.h"
  21. #include "printf.h"
  22. #include "hal_i2cslave.h"
  23. /**
  24. * I2C slave test routine.
  25. *
  26. * To use: Add file to a project, call startComms() with the address of a serial stream
  27. *
  28. * There are two different responses:
  29. * a) A read-only transaction - returns the "Initial Reply" message
  30. * b) A write then read transaction - calls a message processor and returns the generated reply.
  31. * Stretches clock until reply available.
  32. */
  33. // static const I2CConfig masterI2CConfig = {
  34. // 400000
  35. // };
  36. I2CSlaveMsgCB twi2c_incoming_message_process, twi2c_catch_error, twi2c_clear_after_send;
  37. twi2c_message_received twi2c_message_received_callback;
  38. static uint8_t twi2c_address;
  39. static const I2CConfig i2cconfig = {
  40. STM32_TIMINGR_PRESC(15U) |
  41. STM32_TIMINGR_SCLDEL(4U) | STM32_TIMINGR_SDADEL(2U) |
  42. STM32_TIMINGR_SCLH(15U) | STM32_TIMINGR_SCLL(21U),
  43. 0,
  44. 0
  45. };
  46. /**
  47. * Track I2C errors
  48. */
  49. uint8_t gotI2cError = 0;
  50. uint32_t lastI2cErrorFlags = 0;
  51. // Called from ISR to log error
  52. void noteI2cError(uint32_t flags)
  53. {
  54. lastI2cErrorFlags = flags;
  55. gotI2cError = 1;
  56. }
  57. /**
  58. * Generic error handler
  59. *
  60. * Called in interrupt context, so need to watch what we do
  61. */
  62. void twi2c_catch_error(I2CDriver *i2cp)
  63. {
  64. noteI2cError(i2cp->errors);
  65. }
  66. /**
  67. * Callback after sending of response complete - restores default reply in case polled
  68. */
  69. uint8_t twi2c_start(void) {
  70. i2cStart(&I2C_DRIVER, &i2cconfig);
  71. return 0;
  72. }
  73. void twi2c_init(void) {
  74. palSetGroupMode(GPIOB,6,7, PAL_MODE_INPUT); // Try releasing special pins for a short time
  75. chThdSleepMilliseconds(10);
  76. palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  77. palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  78. // try high drive (from kiibohd)
  79. // I2C_DRIVER.i2c->C2 |= I2Cx_C2_HDRS;
  80. // try glitch fixing (from kiibohd)
  81. // I2C_DRIVER.i2c->FLT = 4;
  82. }
  83. uint8_t twi2c_write(uint8_t data) {
  84. return i2cMasterTransmitTimeout(&I2C_DRIVER, twi2c_address/2, &data, 1, NULL, 0, MS2ST(100));
  85. }
  86. uint8_t twi2c_transmit(uint8_t address, uint8_t * data, uint16_t length) {
  87. return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, data, length, NULL, 0, MS2ST(100));
  88. }
  89. uint8_t twi2c_receive(uint8_t address, uint8_t * data, uint16_t length) {
  90. return i2cMasterReceiveTimeout(&I2C_DRIVER, address/2, data, length, MS2ST(100));
  91. }
  92. uint8_t twi2c_incoming_body[50] = {0};
  93. uint8_t twi2c_outgoing_body[1024] = {0};
  94. // Response to received messages
  95. I2CSlaveMsg twi2c_incoming_message = {
  96. sizeof(twi2c_incoming_body),
  97. twi2c_incoming_body,
  98. NULL,
  99. twi2c_incoming_message_process,
  100. twi2c_catch_error /* Error hook */
  101. };
  102. void twi2c_incoming_message_process(I2CDriver * i2cp) {
  103. size_t len = i2cSlaveBytes(i2cp);
  104. (*twi2c_message_received_callback)(i2cp, twi2c_incoming_body, len);
  105. }
  106. // Response to received messages
  107. I2CSlaveMsg twi2c_outgoing_message = {
  108. sizeof(twi2c_outgoing_body),
  109. twi2c_outgoing_body,
  110. NULL,
  111. twi2c_clear_after_send,
  112. twi2c_catch_error
  113. };
  114. void twi2c_clear_after_send(I2CDriver *i2cp) {
  115. twi2c_outgoing_message.size = 0; // Clear receive message
  116. //i2cSlaveReplyI(i2cp, &initialReply);
  117. }
  118. uint8_t twi2c_reply(I2CDriver * i2cp, uint8_t * data, uint16_t length) {
  119. memcpy(twi2c_outgoing_body, data, length);
  120. twi2c_outgoing_message.size = length;
  121. i2cSlaveReplyI(i2cp, &twi2c_outgoing_message);
  122. return 0;
  123. }
  124. uint8_t twi2c_transmit_receive(uint8_t address, uint8_t * tx_body, uint16_t tx_length, uint8_t * rx_body, uint16_t rx_length) {
  125. return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, tx_body, tx_length, rx_body, rx_length, MS2ST(100));
  126. }
  127. uint8_t twi2c_start_listening(uint8_t address, twi2c_message_received callback) {
  128. twi2c_message_received_callback = callback;
  129. I2C_DRIVER.slaveTimeout = MS2ST(100);
  130. i2cSlaveConfigure(&I2C_DRIVER, &twi2c_incoming_message, &twi2c_outgoing_message);
  131. i2cMatchAddress(&I2C_DRIVER, address/2);
  132. return 0;
  133. }
  134. uint8_t twi2c_remove_listening(uint8_t address) {
  135. i2cUnmatchAddress(&I2C_DRIVER, address/2);
  136. return 0;
  137. }
  138. uint8_t twi2c_add_listening(uint8_t address) {
  139. i2cMatchAddress(&I2C_DRIVER, address/2);
  140. return 0;
  141. }
  142. uint8_t twi2c_remove_listening_i(uint8_t address) {
  143. i2cUnmatchAddressI(&I2C_DRIVER, address/2);
  144. return 0;
  145. }
  146. uint8_t twi2c_add_listening_i(uint8_t address) {
  147. i2cMatchAddressI(&I2C_DRIVER, address/2);
  148. return 0;
  149. }
  150. void twi2c_stop(void) {
  151. i2cUnmatchAll(&I2C_DRIVER);
  152. i2cStop(&I2C_DRIVER);
  153. }