gblink.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. // Copyright (c) 2023 KBEmbedded
  3. #ifndef __GBLINK_H__
  4. #define __GBLINK_H__
  5. #pragma once
  6. #include <furi.h>
  7. #include <furi_hal.h>
  8. #include <stdint.h>
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. typedef enum {
  13. /* Flipper drives the clock line */
  14. /* Unsupported at this time */
  15. GBLINK_CLK_INT,
  16. /* Game Boy drives the clock line */
  17. GBLINK_CLK_EXT,
  18. } gblink_clk_source;
  19. /* Currently unused */
  20. typedef enum {
  21. GBLINK_MODE_GBC,
  22. GBLINK_MODE_GBA,
  23. } gblink_mode;
  24. /* Should this just be a macro? */
  25. /* This pretty much only applies to GBC, OG GB is 8192 Hz only */
  26. /* This is only for TX */
  27. typedef enum {
  28. GBLINK_SPD_8192HZ = 4096,
  29. GBLINK_SPD_16384HZ = 8192,
  30. GBLINK_SPD_262144HZ = 16384,
  31. GBLINK_SPD_524288HZ = 262144,
  32. } gblink_speed;
  33. struct gblink_pins {
  34. const GpioPin *serin;
  35. const GpioPin *serout;
  36. const GpioPin *clk;
  37. const GpioPin *sd;
  38. };
  39. typedef enum {
  40. PINOUT_ORIGINAL,
  41. PINOUT_MALVEKE_EXT1,
  42. PINOUT_COUNT,
  43. } gblink_pinouts;
  44. extern const struct gblink_pins common_pinouts[PINOUT_COUNT];
  45. /*
  46. * All of these are things that are basically set once and would never need
  47. * to change during the lifetime of the instance.
  48. *
  49. * Callback can indeed be NULL if unneeded, however a call to the blocking
  50. * gblink_transfer_tx_wait_complete() must be used in order to get a notification
  51. * of the transfer being complete.
  52. */
  53. struct gblink_spec {
  54. struct gblink_pins *pins;
  55. gblink_mode mode;
  56. void (*callback)(void* cb_context, uint8_t in);
  57. void *cb_context;
  58. };
  59. /*
  60. * NOTE:
  61. * This can be called at any time, it resets the current byte transfer information
  62. */
  63. void gblink_clk_source_set(void *handle, gblink_clk_source clk_source);
  64. void gblink_speed_set(void *handle, gblink_speed speed);
  65. void gblink_timeout_set(void *handle, uint32_t us);
  66. bool gblink_transfer(void *handle, uint8_t val);
  67. /*
  68. * This can be used for INT or EXT clock modes. After a call to
  69. * gblink_transfer this can be called at any time and will return only after
  70. * a full byte is transferred and it will return the byte that was last shifted
  71. * in from the link partner.
  72. */
  73. uint8_t gblink_transfer_tx_wait_complete(void *handle);
  74. void gblink_nobyte_set(void *handle, uint8_t val);
  75. void gblink_int_enable(void *handle);
  76. void gblink_int_disable(void *handle);
  77. void *gblink_alloc(struct gblink_spec *gblink_spec);
  78. void gblink_free(void *handle);
  79. #ifdef __cplusplus
  80. }
  81. #endif
  82. #endif // __GBLINK_H__