gblink_i.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. // Copyright (c) 2024 KBEmbedded
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <stdint.h>
  6. #include <gblink/include/gblink.h>
  7. struct gblink {
  8. const GpioPin *serin;
  9. const GpioPin *serout;
  10. const GpioPin *clk;
  11. const GpioPin *sd;
  12. gblink_mode mode;
  13. void (*callback)(void* cb_context, uint8_t in);
  14. void *cb_context;
  15. /* These two semaphores serve similar but distinct purposes. */
  16. /* The transfer semaphore is taken as soon as a transfer() request
  17. * has been started. This is used in the function to wait until the
  18. * transfer has been completed.
  19. */
  20. FuriSemaphore *transfer_sem;
  21. /* The out byte semaphore is used to indicate that a byte transfer
  22. * is in progress. This is used in the transfer function to not allow
  23. * a transfer request if we're in the middle of sending a byte.
  24. * The transfer semaphore is not used for that purpose since if the
  25. * Flipper is in EXT clk mode, once a transfer() is started, there
  26. * would be no way to both prevent transfer() from being called again
  27. * as well as cancelling/changing what we're wanting to send. Using
  28. * out byte semaphore means a transfer() can be called at any time,
  29. * waited on synchronously for a timeout, and then re-called at a
  30. * later time; while blocking that update if a byte is actually
  31. * in the middle of being transmitted.
  32. */
  33. FuriSemaphore *out_byte_sem;
  34. /* Used to lock out changing things after a certain point. Pinout,
  35. * mode, etc.
  36. * XXX: Might make more sense to use the mutex to protect a flag?
  37. * Maybe a semaphore? Though I think that is the wrong use.
  38. */
  39. FuriMutex *start_mutex;
  40. /*
  41. * The following should probably have the world stopped around them
  42. * if not modified in an interrupt context.
  43. */
  44. uint8_t in;
  45. uint8_t out;
  46. uint8_t shift;
  47. uint8_t nobyte;
  48. /* Should only be changed when not in middle of tx, will affect a lot */
  49. gblink_clk_source source;
  50. /* Can be changed at any time, will only take effect on the next
  51. * transfer.
  52. */
  53. gblink_speed speed;
  54. /*
  55. * The following is based on observing Pokemon trade data
  56. *
  57. * Clocks idle between bytes is nominally 430 us long for burst data,
  58. * 15 ms for idle polling (e.g. waiting for menu selection), some oddball
  59. * 2 ms gaps that appears between one 0xFE byte from the Game Boy every trade;
  60. * clock period is nominally 122 us.
  61. *
  62. * Therefore, if we haven't seen a clock in 500 us, reset our bit counter.
  63. * Note that, this should never actually be a concern, but it is an additional
  64. * safeguard against desyncing.
  65. */
  66. uint32_t time;
  67. uint32_t bitclk_timeout_us;
  68. void *exti_workaround_handle;
  69. };