You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
6.7 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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 "hal_i2cslave.h"
  20. #include "chprintf.h"
  21. #include "memstreams.h"
  22. #include "printf.h"
  23. #include "matrix.h"
  24. /**
  25. * I2C slave test routine.
  26. *
  27. * To use: Add file to a project, call startComms() with the address of a serial stream
  28. *
  29. * There are two different responses:
  30. * a) A read-only transaction - returns the "Initial Reply" message
  31. * b) A write then read transaction - calls a message processor and returns the generated reply.
  32. * Stretches clock until reply available.
  33. */
  34. // static const I2CConfig masterI2CConfig = {
  35. // 400000
  36. // };
  37. I2CSlaveMsgCB twi2c_slave_message_process, catchError, clearAfterSend;
  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. NULL
  46. };
  47. char initialReplyBody[50] = "Initial reply"; // 'Status' response if read without preceding write
  48. uint32_t messageCounter = 0; /* Counts number of messages received to return as part of response */
  49. uint8_t rxBody[2]; /* stores last message master sent us (intentionally a few bytes smaller than txBody) */
  50. uint8_t txBody[MATRIX_ROWS/2]; /* Return message buffer for computed replies */
  51. BaseSequentialStream *chp = NULL; // Used for serial logging
  52. // Handler when something sent to us
  53. const I2CSlaveMsg echoRx =
  54. {
  55. sizeof(rxBody), /* max sizeof received msg body */
  56. rxBody, /* body of received msg */
  57. NULL, /* do nothing on address match */
  58. twi2c_slave_message_process, /* Routine to process received messages */
  59. catchError /* Error hook */
  60. };
  61. // // 'Empty' reply when nothing to say, and no message received. In RAM, to allow update
  62. I2CSlaveMsg initialReply =
  63. {
  64. sizeof(initialReplyBody), /* trailing zero byte will be repeated as needed */
  65. (uint8_t *)initialReplyBody,
  66. NULL, /* do nothing on address match */
  67. NULL, /* do nothing after reply sent */
  68. catchError /* Error hook */
  69. };
  70. // // 'Empty' reply when nothing to say, and no message received. In RAM, to allow update
  71. // I2CSlaveMsg initialReply =
  72. // {
  73. // 0, /* trailing zero byte will be repeated as needed */
  74. // NULL,
  75. // NULL, /* do nothing on address match */
  76. // NULL, /* do nothing after reply sent */
  77. // catchError /* Error hook */
  78. // };
  79. // Response to received messages
  80. I2CSlaveMsg echoReply = { /* this is in RAM so size may be updated */
  81. MATRIX_ROWS / 2, /* filled in with the length of the message to send */
  82. txBody, /* Response message */
  83. NULL, /* do nothing special on address match */
  84. clearAfterSend, /* Clear receive buffer once replied */
  85. catchError /* Error hook */
  86. };
  87. /**
  88. * Track I2C errors
  89. */
  90. uint8_t gotI2cError = 0;
  91. uint32_t lastI2cErrorFlags = 0;
  92. // Called from ISR to log error
  93. void noteI2cError(uint32_t flags)
  94. {
  95. lastI2cErrorFlags = flags;
  96. gotI2cError = 1;
  97. }
  98. /**
  99. * Generic error handler
  100. *
  101. * Called in interrupt context, so need to watch what we do
  102. */
  103. void catchError(I2CDriver *i2cp)
  104. {
  105. noteI2cError(i2cp->errors);
  106. }
  107. extern void matrix_copy(matrix_row_t * copy);
  108. const char hexString[16] = "0123456789abcdef";
  109. /**
  110. * Message processor - looks at received message, determines reply as quickly as possible
  111. *
  112. * Responds with the value of the messageCounter (in hex), followed by the received message in [..]
  113. *
  114. * Note: Called in interrupt context, so need to be quick!
  115. */
  116. void twi2c_slave_message_process(I2CDriver *i2cp) {
  117. // size_t len = i2cSlaveBytes(i2cp); // Number of bytes received
  118. // memset(txBody, 0, MATRIX_ROWS / 2 * sizeof(matrix_row_t));
  119. matrix_copy(txBody);
  120. echoReply.size = MATRIX_ROWS / 2;
  121. i2cSlaveReplyI(i2cp, &echoReply);
  122. }
  123. /**
  124. * Callback after sending of response complete - restores default reply in case polled
  125. */
  126. void clearAfterSend(I2CDriver *i2cp)
  127. {
  128. // echoReply.size = 0; // Clear receive message
  129. // i2cSlaveReplyI(i2cp, &initialReply);
  130. }
  131. /**
  132. * Start the I2C Slave port to accept comms from master CPU
  133. *
  134. * We then go into a loop checking for errors, and never return
  135. */
  136. void twi2c_slave_init(void) {
  137. twi2c_init();
  138. i2cStart(&I2C_DRIVER, &I2CConfig);
  139. #if HAL_USE_I2C_SLAVE
  140. I2C_DRIVER.slaveTimeout = MS2ST(100); // Time for complete message
  141. #endif
  142. // i2cSlaveConfigure(&I2C_DRIVER, &echoRx, &initialReply);
  143. memset(txBody, 0, MATRIX_ROWS / 2 * sizeof(matrix_row_t));
  144. i2cSlaveConfigure(&I2C_DRIVER, &echoRx, &echoReply);
  145. // Enable match address after everything else set up
  146. i2cMatchAddress(&I2C_DRIVER, slaveI2Caddress/2);
  147. // i2cMatchAddress(&I2C_DRIVER, myOtherI2Caddress/2);
  148. // i2cMatchAddress(&I2C_DRIVER, 0); /* "all call" */
  149. printf("Slave I2C started\n\r");
  150. }
  151. void twi2c_slave_task(void) {
  152. if (gotI2cError) {
  153. gotI2cError = 0;
  154. printf("I2cError: %04x\r\n", lastI2cErrorFlags);
  155. }
  156. }
  157. uint8_t twi2c_start(uint8_t address) {
  158. twi2c_address = address;
  159. i2cStart(&I2C_DRIVER, &I2CConfig);
  160. }
  161. void twi2c_init(void) {
  162. palSetGroupMode(GPIOB,8,9, PAL_MODE_INPUT); // Try releasing special pins for a short time
  163. chThdSleepMilliseconds(10);
  164. palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  165. palSetPadMode(GPIOB, 8, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP);
  166. // try high drive (from kiibohd)
  167. // I2C_DRIVER.i2c->C2 |= I2Cx_C2_HDRS;
  168. // try glitch fixing (from kiibohd)
  169. // I2C_DRIVER.i2c->FLT = 4;
  170. }
  171. uint8_t twi2c_write(uint8_t data) {
  172. uint8_t buffer[1] = {0};
  173. return i2cMasterTransmitTimeout(&I2C_DRIVER, twi2c_address/2, &data, 1, buffer, 1, MS2ST(100));
  174. }
  175. uint8_t twi2c_transmit(uint8_t address, uint8_t* data, uint16_t length) {
  176. twi2c_address = address;
  177. i2cStart(&I2C_DRIVER, &I2CConfig);
  178. return i2cMasterTransmitTimeout(&I2C_DRIVER, twi2c_address/2, data, length, buffer, 1, MS2ST(100));
  179. }