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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2014 Jan Rychter
  3. * Modifications (C) 2015 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. // ----- Variables -----
  46. extern volatile I2C_Channel i2c_channels[1];
  47. // ----- Functions -----
  48. /*
  49. * I2C Module Setup - Channel 0 only
  50. */
  51. void i2c_setup();
  52. /*
  53. * Sends a command/data sequence that can include restarts, writes and reads. Every transmission begins with a START,
  54. * and ends with a STOP so you do not have to specify that.
  55. *
  56. * sequence is the I2C operation sequence that should be performed. It can include any number of writes, restarts and
  57. * reads. Note that the sequence is composed of uint16_t, not uint8_t. This is because we have to support out-of-band
  58. * signalling of I2C_RESTART and I2C_READ operations, while still passing through 8-bit data.
  59. *
  60. * sequence_length is the number of sequence elements (not bytes). Sequences of arbitrary length are supported. The
  61. * minimum sequence length is (rather obviously) 2.
  62. *
  63. * received_data should point to a buffer that can hold as many bytes as there are I2C_READ operations in the
  64. * sequence. If there are no reads, 0 can be passed, as this parameter will not be used.
  65. *
  66. * callback_fn is a pointer to a function that will get called upon successful completion of the entire sequence. If 0 is
  67. * supplied, no function will be called. Note that the function will be called fron an interrupt handler, so it should do
  68. * the absolute minimum possible (such as enqueue an event to be processed later, set a flag, exit sleep mode, etc.)
  69. *
  70. * user_data is a pointer that will be passed to the callback_fn.
  71. */
  72. int32_t i2c_send_sequence(
  73. uint16_t *sequence,
  74. uint32_t sequence_length,
  75. uint8_t *received_data,
  76. void (*callback_fn)(void*),
  77. void *user_data
  78. );
  79. /*
  80. * Convenience macros
  81. */
  82. #define i2c_send(seq, seq_len) i2c_send_sequence( seq, seq_len, 0, 0, 0 )
  83. #define i2c_read(seq, seq_len, rec) i2c_send_sequence( seq, seq_len, rec, 0, 0 )
  84. /*
  85. * Check if busy
  86. */
  87. uint8_t i2c_busy();