gblink.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. // Copyright (c) 2023 KBEmbedded
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <stm32wbxx_ll_exti.h>
  6. #include <stm32wbxx_ll_system.h>
  7. #include <stdint.h>
  8. #include <gblink/include/gblink.h>
  9. #include <gblink/include/gblink_pinconf.h>
  10. #include "gblink_i.h"
  11. #include "exti_workaround_i.h"
  12. #include "clock_timer_i.h"
  13. static inline bool gblink_transfer_in_progress(struct gblink *gblink)
  14. {
  15. return !(furi_semaphore_get_count(gblink->out_byte_sem));
  16. }
  17. /* XXX: TODO: Investigate how exceeding the timeout would work if in INT clock
  18. * mode. I think this would reset the state machine, but, I don't think the
  19. * transfer would be restarted with the correct data.
  20. */
  21. static void gblink_shift_in_isr(struct gblink *gblink)
  22. {
  23. const uint32_t time_ticks = furi_hal_cortex_instructions_per_microsecond() * gblink->bitclk_timeout_us;
  24. if (gblink->source == GBLINK_CLK_INT)
  25. furi_hal_gpio_write(gblink->clk, 1);
  26. /* If we exceeded the bit clock timeout, reset all counters */
  27. if ((DWT->CYCCNT - gblink->time) > time_ticks) {
  28. gblink->in = 0;
  29. gblink->shift = 0;
  30. }
  31. gblink->time = DWT->CYCCNT;
  32. gblink->in <<= 1;
  33. gblink->in |= furi_hal_gpio_read(gblink->serin);
  34. gblink->shift++;
  35. /* If 8 bits transfered, reset shift counter, call registered
  36. * callback, re-set nobyte in output buffer.
  37. */
  38. if (gblink->shift == 8) {
  39. if (gblink->source == GBLINK_CLK_INT)
  40. clock_timer_stop();
  41. gblink->shift = 0;
  42. /*
  43. * Set up next out byte before calling the callback.
  44. * This is in case the callback itself sets a new out
  45. * byte which it will in most cases.
  46. *
  47. * The nobyte value is set in place as the next output byte,
  48. * in case the flipper does not set a real byte before the next
  49. * transfer starts.
  50. */
  51. gblink->out = gblink->nobyte;
  52. furi_semaphore_release(gblink->out_byte_sem);
  53. /*
  54. * Call the callback, if set, and then release the semaphore
  55. * in case a thread is waiting on TX to complete.
  56. */
  57. if (gblink->callback)
  58. gblink->callback(gblink->cb_context, gblink->in);
  59. furi_semaphore_release(gblink->transfer_sem);
  60. }
  61. }
  62. static void gblink_shift_out_isr(struct gblink *gblink)
  63. {
  64. furi_semaphore_acquire(gblink->out_byte_sem, 0);
  65. furi_hal_gpio_write(gblink->serout, !!(gblink->out & 0x80));
  66. gblink->out <<= 1;
  67. /* XXX: TODO: Check that this is the correct thing with open drain.
  68. * does 0 value actually drive the line low, or high?
  69. */
  70. if (gblink->source == GBLINK_CLK_INT)
  71. furi_hal_gpio_write(gblink->clk, 0);
  72. }
  73. static void gblink_clk_isr(void *context)
  74. {
  75. furi_assert(context);
  76. struct gblink *gblink = context;
  77. bool out = false;
  78. /*
  79. * Whether we're shifting in or out is dependent on the clock source.
  80. * If external, and the clock line is high, that means a posedge just
  81. * occurred and we need to shift data in.
  82. *
  83. * If internal, and the clock line is high, that means we're about
  84. * to drive a negedge and need to shift data out.
  85. *
  86. * The actual in/out functions drive the clock state at the right times
  87. * if the clock is internal source.
  88. */
  89. out = (furi_hal_gpio_read(gblink->clk) ==
  90. (gblink->source == GBLINK_CLK_INT));
  91. if (out)
  92. gblink_shift_out_isr(gblink);
  93. else
  94. gblink_shift_in_isr(gblink);
  95. }
  96. /*
  97. * Call to set up the clk pin modes to do the right thing based on if INT or
  98. * EXT clock source is configured.
  99. */
  100. static void gblink_clk_configure(struct gblink *gblink)
  101. {
  102. if (gblink->source == GBLINK_CLK_EXT) {
  103. furi_hal_gpio_init(gblink->clk, GpioModeInterruptRiseFall, GpioPullUp, GpioSpeedVeryHigh);
  104. /* furi_hal_gpio_init, while it sets interrupt settings on the GPIO,
  105. * does not actually enable the EXTI interrupt.
  106. */
  107. gblink_int_enable(gblink);
  108. } else {
  109. /* This will disable the EXTI interrupt for us */
  110. furi_hal_gpio_init(gblink->clk, GpioModeOutputOpenDrain, GpioPullUp, GpioSpeedVeryHigh);
  111. }
  112. }
  113. void gblink_clk_source_set(void *handle, gblink_clk_source source)
  114. {
  115. furi_assert(handle);
  116. struct gblink *gblink = handle;
  117. if (source == gblink->source)
  118. return;
  119. /*
  120. * NOTE:
  121. * I'm not sure the best way to handle this at the moment. In theory,
  122. * it should be safe to check that we're just not in the middle of a
  123. * transfer and not worry about getting stuck.
  124. * However, I'm not really sure how true that is, so for now this will
  125. * always change the source and reset the current byte transfer.
  126. * It is up to the callee to ensure that they are between bytes.
  127. *
  128. * One idea would be to get the semaphore, but wait the set timeout.
  129. * if that is exceeded or the semaphore is acquired, then its probably
  130. * safe to change the source and reset shift register.
  131. */
  132. gblink->source = source;
  133. gblink->shift = 0;
  134. gblink_clk_configure(gblink);
  135. }
  136. void gblink_speed_set(void *handle, gblink_speed speed)
  137. {
  138. furi_assert(handle);
  139. struct gblink *gblink = handle;
  140. /*
  141. * This does not need any protection, it will take effect at the start
  142. * of the next byte.
  143. */
  144. gblink->speed = speed;
  145. }
  146. /* default is set to 500 us */
  147. void gblink_timeout_set(void *handle, uint32_t us)
  148. {
  149. furi_assert(handle);
  150. struct gblink *gblink = handle;
  151. gblink->bitclk_timeout_us = us;
  152. }
  153. int gblink_callback_set(void *handle, void (*callback)(void* cb_context, uint8_t in), void *cb_context)
  154. {
  155. furi_assert(handle);
  156. struct gblink *gblink = handle;
  157. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk)
  158. return 1;
  159. gblink->callback = callback;
  160. gblink->cb_context = cb_context;
  161. furi_mutex_release(gblink->start_mutex);
  162. return 0;
  163. }
  164. int gblink_mode_set(void *handle, gblink_mode mode)
  165. {
  166. furi_assert(handle);
  167. struct gblink *gblink = handle;
  168. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk)
  169. return 1;
  170. gblink->mode = mode;
  171. furi_mutex_release(gblink->start_mutex);
  172. return 0;
  173. }
  174. /* XXX: TODO: This doesn't check for start! */
  175. bool gblink_transfer(void *handle, uint8_t val)
  176. {
  177. furi_assert(handle);
  178. struct gblink *gblink = handle;
  179. bool ret = false;
  180. /* Stop the world, this is to ensure we can safely set the next out byte */
  181. /*
  182. * The reason for and therefore issue of setting the next byte has a few
  183. * points to keep in mind.
  184. *
  185. * First, with EXT clock source, the first hint of the external device
  186. * clocking in data is a negative edge where it would set data. This
  187. * means that the next out byte needs to be set before that.
  188. *
  189. * Second, since the interrupt on the neg clock edge loads the next
  190. * byte in to serout after grabbing the semaphore; we can stop the
  191. * world right now, and set the byte if there is no transfer in
  192. * progress. As soon as the world is resumed, the IRQ will fire, and
  193. * the correct, new, data byte will start to be shifted out.
  194. */
  195. FURI_CRITICAL_ENTER();
  196. /* If we're in the middle of a tranfer, don't let the byte be set. */
  197. if (!gblink_transfer_in_progress(gblink)) {
  198. gblink->out = val;
  199. ret = true;
  200. /*
  201. * Now that we're this far, this means the byte we set will be
  202. * transferred one way or another. Because of that, take the
  203. * transfer semaphore. This gets released once a full byte has
  204. * been transferred. This is for the TX wait function. We cannot
  205. * use the out_byte_sem as if the wait is called immediately
  206. * after the transfer, and no data has yet been shifted out,
  207. * the TX wait function would incorrectly return immediately.
  208. */
  209. furi_semaphore_acquire(gblink->transfer_sem, 0);
  210. }
  211. FURI_CRITICAL_EXIT();
  212. /*
  213. * If the out byte was successfully set, and we're driving the clock,
  214. * turn on our timer for byte transfer.
  215. */
  216. if (ret && gblink->source == GBLINK_CLK_INT)
  217. clock_timer_start(gblink_clk_isr, gblink, gblink->speed);
  218. return ret;
  219. }
  220. uint8_t gblink_transfer_tx_wait_complete(void *handle)
  221. {
  222. struct gblink *gblink = handle;
  223. /* XXX: TODO: Think about how to implement this in a way that we can
  224. * use the semaphore to see if there is a transfer waiting to happen,
  225. * but not in a way that would incorrectly show a transfer waiting. e.g.
  226. * if this takes the semaphore, then the semaphore is in the same state
  227. * as if a transfer was in progress. Should this put back the semaphore
  228. * after acquiring it? Is there a better way of handling it?
  229. */
  230. furi_semaphore_acquire(gblink->transfer_sem, FuriWaitForever);
  231. return gblink->in;
  232. }
  233. void gblink_nobyte_set(void *handle, uint8_t val)
  234. {
  235. struct gblink *gblink = handle;
  236. /*
  237. * This is safe to run at any time. It is only copied in after a byte
  238. * transfer is completed.
  239. */
  240. gblink->nobyte = val;
  241. }
  242. void gblink_int_enable(void *handle)
  243. {
  244. furi_assert(handle);
  245. struct gblink *gblink = handle;
  246. /*
  247. * NOTE: This is currently safe to run even with the exti workaround
  248. * in effect. It just enables the root EXTI interrupt source of the
  249. * given pin.
  250. */
  251. furi_hal_gpio_enable_int_callback(gblink->clk);
  252. }
  253. void gblink_int_disable(void *handle)
  254. {
  255. furi_assert(handle);
  256. struct gblink *gblink = handle;
  257. /*
  258. * NOTE: This is currently safe to run even with the exti workaround
  259. * in effect. It just disables the root EXTI interrupt source of the
  260. * given pin.
  261. */
  262. furi_hal_gpio_disable_int_callback(gblink->clk);
  263. }
  264. void *gblink_alloc(void)
  265. {
  266. struct gblink *gblink;
  267. /* Allocate and zero struct */
  268. gblink = malloc(sizeof(struct gblink));
  269. //gblink->spec = malloc(sizeof(struct gblink_spec));
  270. gblink->transfer_sem = furi_semaphore_alloc(1, 1);
  271. gblink->out_byte_sem = furi_semaphore_alloc(1, 1);
  272. gblink->start_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  273. /* Set defaults */
  274. gblink_pin_set_default(gblink, PINOUT_ORIGINAL);
  275. gblink_mode_set(gblink, GBLINK_MODE_GBC);
  276. gblink_clk_source_set(gblink, GBLINK_CLK_EXT);
  277. gblink_speed_set(gblink, GBLINK_SPD_8192HZ);
  278. gblink_timeout_set(gblink, 500);
  279. /* Set current time to start timeout calculations */
  280. gblink->time = DWT->CYCCNT;
  281. return gblink;
  282. }
  283. void gblink_start(void *handle)
  284. {
  285. furi_assert(handle);
  286. struct gblink *gblink = handle;
  287. /* XXX: Check callback is valid */
  288. furi_mutex_acquire(gblink->start_mutex, FuriWaitForever);
  289. /* Set up pins */
  290. /* TODO: Set up a list of pins that are not safe to use with interrupts.
  291. * I do believe the main FURI GPIO struct has this data baked in so that
  292. * could be used. For now though, we're only checking for the MALVEKE
  293. * pinout which uses a clk pin that has its IRQ shared with the Okay
  294. * button.
  295. * See the work done in pokemon trade tool custom pinout selection for
  296. * an idea of how to check all that.
  297. */
  298. furi_hal_gpio_write(gblink->serout, false);
  299. furi_hal_gpio_init(gblink->serout, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh);
  300. furi_hal_gpio_write(gblink->serin, false);
  301. furi_hal_gpio_init(gblink->serin, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  302. /* Set up interrupt on clock pin */
  303. if (gblink->clk == &gpio_ext_pb3) {
  304. /* The clock pin is on a pin that is not safe to set an interrupt
  305. * on, so we do a gross workaround to get an interrupt enabled
  306. * on that pin in a way that can be undone safely later with
  307. * no impact to the shared IRQ.
  308. */
  309. gblink->exti_workaround_handle = exti_workaround(gblink->clk, gblink_clk_isr, gblink);
  310. } else {
  311. /* This may not be needed after NFC refactor */
  312. furi_hal_gpio_remove_int_callback(gblink->clk);
  313. furi_hal_gpio_add_int_callback(gblink->clk, gblink_clk_isr, gblink);
  314. }
  315. /* The above immediately enables the interrupt, we don't want
  316. * that just yet and we want configure to handle it.
  317. */
  318. gblink_int_disable(gblink);
  319. gblink_clk_configure(gblink);
  320. }
  321. void gblink_stop(void *handle)
  322. {
  323. furi_assert(handle);
  324. struct gblink *gblink = handle;
  325. /* If we can acquire the mutex, that means start was never actually
  326. * called. Crash.
  327. * XXX: Probably a bit harsh to just crash, can it gracefully recover
  328. * without too much effort?
  329. */
  330. if (furi_mutex_acquire(gblink->start_mutex, 0) == FuriStatusOk) {
  331. furi_crash();
  332. return;
  333. }
  334. if (gblink->clk == &gpio_ext_pb3) {
  335. /* This handles switching the IVT back and putting the EXTI
  336. * regs and pin regs in a valid state for normal use.
  337. */
  338. exti_workaround_undo(gblink->exti_workaround_handle);
  339. } else {
  340. /* Remove interrupt, set IO to sane state */
  341. furi_hal_gpio_remove_int_callback(gblink->clk);
  342. }
  343. furi_hal_gpio_init_simple(gblink->serin, GpioModeAnalog);
  344. furi_hal_gpio_init_simple(gblink->serout, GpioModeAnalog);
  345. furi_hal_gpio_init_simple(gblink->clk, GpioModeAnalog);
  346. furi_mutex_release(gblink->start_mutex);
  347. }
  348. void gblink_free(void *handle)
  349. {
  350. furi_assert(handle);
  351. struct gblink *gblink = handle;
  352. /* If we cannot acquire the mutex, that means the link was never properly
  353. * stopped. Crash.
  354. * XXX: Can this be gracefully handled?
  355. */
  356. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk) {
  357. furi_crash();
  358. return;
  359. }
  360. furi_mutex_release(gblink->start_mutex);
  361. furi_mutex_free(gblink->start_mutex);
  362. furi_semaphore_free(gblink->transfer_sem);
  363. furi_semaphore_free(gblink->out_byte_sem);
  364. free(gblink);
  365. }