twi2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. void twi2c_clear_after_send(I2CDriver *i2cp)
  70. {
  71. // echoReply.size = 0; // Clear receive message
  72. // i2cSlaveReplyI(i2cp, &initialReply);
  73. }
  74. uint8_t twi2c_start(void) {
  75. i2cStart(&I2C_DRIVER, &i2cconfig);
  76. return 0;
  77. }
  78. void twi2c_init(void) {
  79. palSetGroupMode(GPIOB,6,7, PAL_MODE_INPUT); // Try releasing special pins for a short time
  80. chThdSleepMilliseconds(10);
  81. palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  82. palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  83. // try high drive (from kiibohd)
  84. // I2C_DRIVER.i2c->C2 |= I2Cx_C2_HDRS;
  85. // try glitch fixing (from kiibohd)
  86. // I2C_DRIVER.i2c->FLT = 4;
  87. }
  88. uint8_t twi2c_write(uint8_t data) {
  89. return i2cMasterTransmitTimeout(&I2C_DRIVER, twi2c_address/2, &data, 1, 0, 0, MS2ST(100));
  90. }
  91. uint8_t twi2c_transmit(uint8_t address, uint8_t * data, uint16_t length) {
  92. return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, data, length, 0, 0, MS2ST(100));
  93. }
  94. uint8_t twi2c_receive(uint8_t address, uint8_t * data, uint16_t length) {
  95. return i2cMasterReceiveTimeout(&I2C_DRIVER, address/2, data, length, MS2ST(100));
  96. }
  97. uint8_t twi2c_incoming_body[512];
  98. uint8_t twi2c_outgoing_body[512];
  99. // Response to received messages
  100. I2CSlaveMsg twi2c_incoming_message = {
  101. sizeof(twi2c_incoming_body),
  102. twi2c_incoming_body,
  103. NULL,
  104. twi2c_incoming_message_process,
  105. twi2c_catch_error /* Error hook */
  106. };
  107. void twi2c_incoming_message_process(I2CDriver * i2cp) {
  108. size_t len = i2cSlaveBytes(i2cp);
  109. (*twi2c_message_received_callback)(i2cp, twi2c_incoming_body, len);
  110. }
  111. // Response to received messages
  112. I2CSlaveMsg twi2c_outgoing_message = {
  113. sizeof(twi2c_outgoing_body),
  114. twi2c_outgoing_body,
  115. NULL,
  116. twi2c_clear_after_send,
  117. twi2c_catch_error
  118. };
  119. uint8_t twi2c_reply(I2CDriver * i2cp, uint8_t * data, uint16_t length) {
  120. memcpy(twi2c_outgoing_body, data, length);
  121. twi2c_outgoing_message.size = length;
  122. i2cSlaveReplyI(i2cp, &twi2c_outgoing_message);
  123. return 0;
  124. }
  125. uint8_t twi2c_transmit_receive(uint8_t address, uint8_t * tx_body, uint16_t tx_length, uint8_t * rx_body, uint16_t rx_length) {
  126. return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, tx_body, tx_length, rx_body, rx_length, MS2ST(100));
  127. }
  128. uint8_t twi2c_start_listening(uint8_t address, twi2c_message_received callback) {
  129. twi2c_message_received_callback = callback;
  130. I2C_DRIVER.slaveTimeout = MS2ST(100);
  131. i2cSlaveConfigure(&I2C_DRIVER, &twi2c_incoming_message, &twi2c_outgoing_message);
  132. i2cMatchAddress(&I2C_DRIVER, address/2);
  133. return 0;
  134. }
  135. void twi2c_stop(void) {
  136. i2cUnmatchAll(&I2C_DRIVER);
  137. i2cStop(&I2C_DRIVER);
  138. }