gblink_pinconf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: BSD-2-Clause
  2. // Copyright (c) 2023 KBEmbedded
  3. #include <furi.h>
  4. #include <storage/storage.h>
  5. #include <lib/toolbox/stream/stream.h>
  6. #include <lib/flipper_format/flipper_format.h>
  7. #include <stdint.h>
  8. #include <gblink/include/gblink.h>
  9. #include "gblink_i.h"
  10. #define PINCONF_FILE_TYPE "Flipper GB Link Pinconf"
  11. #define PINCONF_FILE_VER 1
  12. #define PINCONF_ORIG "Original"
  13. #define PINCONF_MLVK "MLVK2.5"
  14. #define PINCONF_CUST "Custom"
  15. #define PINCONF_SI "SI"
  16. #define PINCONF_SO "SO"
  17. #define PINCONF_CLK "CLK"
  18. struct gblink_pins {
  19. const GpioPin *serin;
  20. const GpioPin *serout;
  21. const GpioPin *clk;
  22. const GpioPin *sd;
  23. };
  24. const struct gblink_pins common_pinouts[PINOUT_COUNT] = {
  25. /* Original */
  26. {
  27. &gpio_ext_pc3,
  28. &gpio_ext_pb3,
  29. &gpio_ext_pb2,
  30. &gpio_ext_pa4,
  31. },
  32. /* MALVEKE EXT1 */
  33. {
  34. &gpio_ext_pa6,
  35. &gpio_ext_pa7,
  36. &gpio_ext_pb3,
  37. &gpio_ext_pa4,
  38. },
  39. };
  40. int gblink_pin_set_by_gpiopin(void *handle, gblink_bus_pins pin, const GpioPin *gpio)
  41. {
  42. furi_assert(handle);
  43. struct gblink *gblink = handle;
  44. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk)
  45. return -1;
  46. switch (pin) {
  47. case PIN_SERIN:
  48. gblink->serin = gpio;
  49. break;
  50. case PIN_SEROUT:
  51. gblink->serout = gpio;
  52. break;
  53. case PIN_CLK:
  54. gblink->clk = gpio;
  55. break;
  56. case PIN_SD:
  57. gblink->sd = gpio;
  58. break;
  59. default:
  60. furi_crash();
  61. break;
  62. }
  63. furi_mutex_release(gblink->start_mutex);
  64. return 0;
  65. }
  66. const GpioPin *gblink_pin_get_by_gpiopin(void *handle, gblink_bus_pins pin)
  67. {
  68. furi_assert(handle);
  69. struct gblink *gblink = handle;
  70. switch (pin) {
  71. case PIN_SERIN:
  72. return gblink->serin;
  73. case PIN_SEROUT:
  74. return gblink->serout;
  75. case PIN_CLK:
  76. return gblink->clk;
  77. case PIN_SD:
  78. return gblink->sd;
  79. default:
  80. furi_crash();
  81. break;
  82. }
  83. return NULL;
  84. }
  85. int gblink_pin_set_default(void *handle, gblink_pinouts pinout)
  86. {
  87. furi_assert(handle);
  88. struct gblink *gblink = handle;
  89. if (pinout == PINOUT_CUSTOM || pinout >= PINOUT_COUNT)
  90. return -1;
  91. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk)
  92. return -1;
  93. gblink->serin = common_pinouts[pinout].serin;
  94. gblink->serout = common_pinouts[pinout].serout;
  95. gblink->clk = common_pinouts[pinout].clk;
  96. gblink->sd = common_pinouts[pinout].sd;
  97. furi_mutex_release(gblink->start_mutex);
  98. return 0;
  99. }
  100. int gblink_pin_get_default(void *handle)
  101. {
  102. furi_assert(handle);
  103. struct gblink *gblink = handle;
  104. int i;
  105. for (i = 0; i < PINOUT_COUNT; i++) {
  106. if (gblink->serin != common_pinouts[i].serin)
  107. continue;
  108. if (gblink->serout != common_pinouts[i].serout)
  109. continue;
  110. if (gblink->clk != common_pinouts[i].clk)
  111. continue;
  112. /* XXX: Currently not checked or used! */
  113. //if (gblink->sd != common_pinouts[pinout].sd;
  114. break;
  115. }
  116. if (i == PINOUT_COUNT)
  117. i = -1;
  118. return i;
  119. }
  120. int gblink_pin_set(void *handle, gblink_bus_pins pin, unsigned int pinnum)
  121. {
  122. furi_assert(handle);
  123. furi_assert(pinnum < gpio_pins_count);
  124. struct gblink *gblink = handle;
  125. if (furi_mutex_acquire(gblink->start_mutex, 0) != FuriStatusOk)
  126. return -1;
  127. switch (pin) {
  128. case PIN_SERIN:
  129. gblink->serin = gpio_pins[pinnum].pin;
  130. break;
  131. case PIN_SEROUT:
  132. gblink->serout = gpio_pins[pinnum].pin;
  133. break;
  134. case PIN_CLK:
  135. gblink->clk = gpio_pins[pinnum].pin;
  136. break;
  137. case PIN_SD:
  138. gblink->sd = gpio_pins[pinnum].pin;
  139. break;
  140. default:
  141. furi_crash();
  142. break;
  143. }
  144. furi_mutex_release(gblink->start_mutex);
  145. return 0;
  146. }
  147. int gblink_pin_get(void *handle, gblink_bus_pins pin)
  148. {
  149. furi_assert(handle);
  150. struct gblink *gblink = handle;
  151. unsigned int i;
  152. for (i = 0; i < gpio_pins_count; i++) {
  153. switch (pin) {
  154. case PIN_SERIN:
  155. if (gpio_pins[i].pin == gblink->serin)
  156. return i;
  157. break;
  158. case PIN_SEROUT:
  159. if (gpio_pins[i].pin == gblink->serout)
  160. return i;
  161. break;
  162. case PIN_CLK:
  163. if (gpio_pins[i].pin == gblink->clk)
  164. return i;
  165. break;
  166. case PIN_SD:
  167. if (gpio_pins[i].pin == gblink->sd)
  168. return i;
  169. break;
  170. default:
  171. furi_crash();
  172. break;
  173. }
  174. }
  175. return -1;
  176. }
  177. int gblink_pin_count_max(void)
  178. {
  179. unsigned int i;
  180. int count = 0;
  181. for (i = 0; i < gpio_pins_count; i++)
  182. if (!gpio_pins[i].debug)
  183. count = i;
  184. return count;
  185. }
  186. int gblink_pin_get_next(unsigned int pinnum)
  187. {
  188. unsigned int i;
  189. /* The check could be eliminated and just rely on the return -1 at the
  190. * end of the function, but this is a shortcut so we don't have to walk
  191. * through the whole table just to find out its an out-of-bounds pin.
  192. */
  193. if (pinnum >= gpio_pins_count)
  194. return -1;
  195. for (i = pinnum; i < gpio_pins_count; i++)
  196. if (!gpio_pins[i].debug)
  197. return i;
  198. return -1;
  199. }
  200. int gblink_pin_get_prev(unsigned int pinnum)
  201. {
  202. int i;
  203. /* The check could be eliminated and just rely on the return -1 at the
  204. * end of the function, but this is a shortcut so we don't have to walk
  205. * through the whole table just to find out its an out-of-bounds pin.
  206. */
  207. if (pinnum >= gpio_pins_count)
  208. return -1;
  209. for (i = pinnum; i >= 0; i--)
  210. if (!gpio_pins[i].debug)
  211. return i;
  212. return -1;
  213. }
  214. bool gblink_pinconf_load(void *gblink)
  215. {
  216. Storage *storage = NULL;;
  217. FlipperFormat *data_file = NULL;
  218. FuriString *string = NULL;
  219. uint32_t val;
  220. bool ret = false;
  221. storage = furi_record_open(RECORD_STORAGE);
  222. string = furi_string_alloc_set(APP_DATA_PATH(""));
  223. storage_common_resolve_path_and_ensure_app_directory(storage, string);
  224. furi_string_cat_str(string, ".gblink_pinconf");
  225. data_file = flipper_format_file_alloc(storage);
  226. if (!flipper_format_file_open_existing(data_file, furi_string_get_cstr(string))) {
  227. FURI_LOG_E("pinconf", "Error opening file %s", furi_string_get_cstr(string));
  228. goto out;
  229. }
  230. if (!flipper_format_read_header(data_file, string, &val)) {
  231. FURI_LOG_E("pinconf", "Missing or incorrect header");
  232. goto out;
  233. }
  234. if (strncmp(furi_string_get_cstr(string), PINCONF_FILE_TYPE, strlen(PINCONF_FILE_TYPE)) ||
  235. val != PINCONF_FILE_VER) {
  236. FURI_LOG_E("pinconf", "Type or version mismatch");
  237. goto out;
  238. }
  239. if (!flipper_format_read_string(data_file, "Mode", string)) {
  240. FURI_LOG_E("pinconf", "Missing Mode");
  241. goto out;
  242. }
  243. if (!strncmp(furi_string_get_cstr(string), PINCONF_ORIG, strlen(PINCONF_ORIG))) {
  244. FURI_LOG_I("pinconf", "Setting Original pinout");
  245. gblink_pin_set_default(gblink, PINOUT_ORIGINAL);
  246. goto out;
  247. }
  248. if (!strncmp(furi_string_get_cstr(string), PINCONF_MLVK, strlen(PINCONF_MLVK))) {
  249. FURI_LOG_I("pinconf", "Setting MALVEKE 2.5 pinout");
  250. gblink_pin_set_default(gblink, PINOUT_MALVEKE_EXT1);
  251. goto out;
  252. }
  253. if (!strncmp(furi_string_get_cstr(string), PINCONF_CUST, strlen(PINCONF_CUST))) {
  254. FURI_LOG_I("pinconf", "Setting Custom pinout");
  255. }
  256. if (!flipper_format_read_uint32(data_file, PINCONF_SI, &val, 1)) {
  257. FURI_LOG_E("pinconf", "Missing SI");
  258. goto out;
  259. } else {
  260. gblink_pin_set(gblink, PIN_SERIN, val);
  261. }
  262. if (!flipper_format_read_uint32(data_file, PINCONF_SO, &val, 1)) {
  263. FURI_LOG_E("pinconf", "Missing SO");
  264. goto out;
  265. } else {
  266. gblink_pin_set(gblink, PIN_SEROUT, val);
  267. }
  268. if (!flipper_format_read_uint32(data_file, PINCONF_CLK, &val, 1)) {
  269. FURI_LOG_E("pinconf", "Missing CLK");
  270. goto out;
  271. } else {
  272. gblink_pin_set(gblink, PIN_CLK, val);
  273. }
  274. ret = true;
  275. out:
  276. flipper_format_file_close(data_file);
  277. furi_string_free(string);
  278. furi_record_close(RECORD_STORAGE);
  279. return ret;
  280. }
  281. /* XXX: TODO: I think there is a way to build this ahead of time and
  282. * write it in one go to be a bit more time efficient.
  283. */
  284. bool gblink_pinconf_save(void *gblink)
  285. {
  286. Storage *storage = NULL;;
  287. FlipperFormat *data_file = NULL;
  288. FuriString *string = NULL;
  289. int rc;
  290. uint32_t pin;
  291. bool ret = false;
  292. storage = furi_record_open(RECORD_STORAGE);
  293. string = furi_string_alloc_set(APP_DATA_PATH(""));
  294. storage_common_resolve_path_and_ensure_app_directory(storage, string);
  295. furi_string_cat_str(string, ".gblink_pinconf");
  296. data_file = flipper_format_file_alloc(storage);
  297. if (!flipper_format_file_open_always(data_file, furi_string_get_cstr(string))) {
  298. FURI_LOG_E("pinconf", "Error opening file %s", furi_string_get_cstr(string));
  299. goto out;
  300. }
  301. if (!flipper_format_write_header_cstr(data_file, PINCONF_FILE_TYPE, PINCONF_FILE_VER)) {
  302. FURI_LOG_E("pinconf", "Error writing header to file");
  303. goto out;
  304. }
  305. rc = gblink_pin_get_default(gblink);
  306. switch (rc) {
  307. case 0:
  308. if (!flipper_format_write_string_cstr(data_file, "Mode", PINCONF_ORIG))
  309. FURI_LOG_E("pinconf", "Error writing mode to file");
  310. goto out;
  311. break;
  312. case 1:
  313. if (!flipper_format_write_string_cstr(data_file, "Mode", PINCONF_MLVK))
  314. FURI_LOG_E("pinconf", "Error writing mode to file");
  315. goto out;
  316. break;
  317. case -1:
  318. if (!flipper_format_write_string_cstr(data_file, "Mode", PINCONF_CUST))
  319. FURI_LOG_E("pinconf", "Error writing mode to file");
  320. break;
  321. default:
  322. FURI_LOG_E("pinconf", "Unknown mode");
  323. goto out;
  324. break;
  325. }
  326. pin = gblink_pin_get(gblink, PIN_SERIN);
  327. if (!flipper_format_write_uint32(data_file, "SI", &pin, 1)) {
  328. FURI_LOG_E("pinconf", "Error writing SI to file");
  329. goto out;
  330. }
  331. pin = gblink_pin_get(gblink, PIN_SEROUT);
  332. if (!flipper_format_write_uint32(data_file, "SO", &pin, 1)) {
  333. FURI_LOG_E("pinconf", "Error writing SO to file");
  334. goto out;
  335. }
  336. pin = gblink_pin_get(gblink, PIN_CLK);
  337. if (!flipper_format_write_uint32(data_file, "CLK", &pin, 1)) {
  338. FURI_LOG_E("pinconf", "Error writing CLK to file");
  339. }
  340. ret = true;
  341. out:
  342. flipper_format_file_close(data_file);
  343. furi_string_free(string);
  344. furi_record_close(RECORD_STORAGE);
  345. return ret;
  346. }