No Description
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.

hw_id.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * hw_id.c
  3. *
  4. * Copyright (c) 2022 - 2024 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * See <http://www.gnu.org/licenses/>.
  17. */
  18. #include "hardware/watchdog.h"
  19. #include "config.h"
  20. #include "log.h"
  21. #include "hw_id.h"
  22. #define SR_WIDTH 8
  23. #define SR_GPIO_LOAD 20
  24. #define SR_GPIO_CLK 21
  25. #define SR_GPIO_DATA 22
  26. static bool states[SR_WIDTH] = {0};
  27. void hw_id_init(void) {
  28. gpio_init(SR_GPIO_LOAD);
  29. gpio_set_dir(SR_GPIO_LOAD, GPIO_OUT);
  30. gpio_put(SR_GPIO_LOAD, true);
  31. gpio_init(SR_GPIO_CLK);
  32. gpio_set_dir(SR_GPIO_CLK, GPIO_OUT);
  33. gpio_put(SR_GPIO_CLK, true);
  34. gpio_init(SR_GPIO_DATA);
  35. gpio_set_dir(SR_GPIO_DATA, GPIO_IN);
  36. }
  37. void hw_id_read(void) {
  38. gpio_put(SR_GPIO_LOAD, false);
  39. watchdog_update();
  40. sleep_us(5);
  41. gpio_put(SR_GPIO_LOAD, true);
  42. watchdog_update();
  43. sleep_us(5);
  44. for (uint i = 0; i < SR_WIDTH; i++) {
  45. gpio_put(SR_GPIO_CLK, false);
  46. watchdog_update();
  47. sleep_us(5);
  48. states[i] = gpio_get(SR_GPIO_DATA);
  49. gpio_put(SR_GPIO_CLK, true);
  50. watchdog_update();
  51. sleep_us(5);
  52. //debug("bit %d is %s", i, states[i] ? "true" : "false");
  53. }
  54. }
  55. enum hw_type hw_type(void) {
  56. uint val = 0;
  57. for (uint i = 4; i < 8; i++) {
  58. val |= states[i] ? (1 << (i - 4)) : 0;
  59. }
  60. return val;
  61. }
  62. uint hw_id(void) {
  63. uint val = 0;
  64. for (uint i = 0; i < 4; i++) {
  65. val |= (!states[i]) ? (1 << (4 - i - 1)) : 0;
  66. }
  67. return val;
  68. }