Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

i2c.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2014 Jan Rychter
  3. * Modifications (C) 2015-2016 Jacob Alexander
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #pragma once
  24. // ----- Includes ----
  25. // Compiler Includes
  26. #include <Lib/ScanLib.h>
  27. // ----- Defines -----
  28. // Channel status definitions. These are not enumerated, as I want them to be uint8_t.
  29. #define I2C_AVAILABLE 0
  30. #define I2C_BUSY 1
  31. #define I2C_ERROR 2
  32. #define I2C_RESTART 1<<8
  33. #define I2C_READ 2<<8
  34. // ----- Structs -----
  35. typedef struct {
  36. uint16_t *sequence;
  37. uint16_t *sequence_end;
  38. uint8_t *received_data;
  39. void (*callback_fn)(void*);
  40. void *user_data;
  41. uint8_t reads_ahead;
  42. uint8_t status;
  43. uint8_t txrx;
  44. } I2C_Channel;
  45. // ----- Functions -----
  46. /*
  47. * I2C Module Setup
  48. */
  49. void i2c_setup();
  50. /*
  51. * Sends a command/data sequence that can include restarts, writes and reads. Every transmission begins with a START,
  52. * and ends with a STOP so you do not have to specify that.
  53. *
  54. * sequence is the I2C operation sequence that should be performed. It can include any number of writes, restarts and
  55. * reads. Note that the sequence is composed of uint16_t, not uint8_t. This is because we have to support out-of-band
  56. * signalling of I2C_RESTART and I2C_READ operations, while still passing through 8-bit data.
  57. *
  58. * sequence_length is the number of sequence elements (not bytes). Sequences of arbitrary length are supported. The
  59. * minimum sequence length is (rather obviously) 2.
  60. *
  61. * received_data should point to a buffer that can hold as many bytes as there are I2C_READ operations in the
  62. * sequence. If there are no reads, 0 can be passed, as this parameter will not be used.
  63. *
  64. * callback_fn is a pointer to a function that will get called upon successful completion of the entire sequence. If 0 is
  65. * supplied, no function will be called. Note that the function will be called fron an interrupt handler, so it should do
  66. * the absolute minimum possible (such as enqueue an event to be processed later, set a flag, exit sleep mode, etc.)
  67. *
  68. * user_data is a pointer that will be passed to the callback_fn.
  69. */
  70. int32_t i2c_send_sequence(
  71. uint8_t ch,
  72. uint16_t *sequence,
  73. uint32_t sequence_length,
  74. uint8_t *received_data,
  75. void (*callback_fn)(void*),
  76. void *user_data
  77. );
  78. /*
  79. * Convenience macros
  80. */
  81. #define i2c_send(ch, seq, seq_len) i2c_send_sequence( ch, seq, seq_len, 0, 0, 0 )
  82. #define i2c_read(ch, seq, seq_len, rec) i2c_send_sequence( ch, seq, seq_len, rec, 0, 0 )
  83. /*
  84. * Check if busy
  85. */
  86. uint8_t i2c_busy( uint8_t ch );
  87. uint8_t i2c_any_busy();