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.

util.c 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * util.c
  3. *
  4. * Copyright (c) 2022 - 2023 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 <string.h>
  19. #include "pico/stdlib.h"
  20. #include "pico/bootrom.h"
  21. #include "hardware/watchdog.h"
  22. #include "config.h"
  23. #include "log.h"
  24. #include "util.h"
  25. #define HEARTBEAT_INTERVAL_MS 500
  26. #ifdef PICO_DEFAULT_LED_PIN
  27. static uint32_t last_heartbeat = 0;
  28. #endif // PICO_DEFAULT_LED_PIN
  29. void heartbeat_init(void) {
  30. #ifdef PICO_DEFAULT_LED_PIN
  31. gpio_init(PICO_DEFAULT_LED_PIN);
  32. gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
  33. gpio_put(PICO_DEFAULT_LED_PIN, 1);
  34. #endif // PICO_DEFAULT_LED_PIN
  35. }
  36. void heartbeat_run(void) {
  37. #ifdef PICO_DEFAULT_LED_PIN
  38. uint32_t now = to_ms_since_boot(get_absolute_time());
  39. if (now >= (last_heartbeat + HEARTBEAT_INTERVAL_MS)) {
  40. last_heartbeat = now;
  41. gpio_xor_mask(1 << PICO_DEFAULT_LED_PIN);
  42. }
  43. #endif // PICO_DEFAULT_LED_PIN
  44. }
  45. bool str_startswith(const char *str, const char *start) {
  46. size_t l = strlen(start);
  47. if (l > strlen(str)) {
  48. return false;
  49. }
  50. return (strncmp(str, start, l) == 0);
  51. }
  52. int32_t convert_two_complement(int32_t b) {
  53. if (b & 0x8000) {
  54. b = -1 * ((b ^ 0xffff) + 1);
  55. }
  56. return b;
  57. }
  58. void reset_to_bootloader(void) {
  59. #ifdef PICO_DEFAULT_LED_PIN
  60. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  61. #else // ! PICO_DEFAULT_LED_PIN
  62. reset_usb_boot(0, 0);
  63. #endif // PICO_DEFAULT_LED_PIN
  64. }
  65. void reset_to_main(void) {
  66. watchdog_enable(1, 1);
  67. while (1) {
  68. // wait 1ms until watchdog kills us
  69. asm volatile("nop");
  70. }
  71. }
  72. void hexdump(uint8_t *buff, size_t len) {
  73. for (size_t i = 0; i < len; i += 16) {
  74. for (size_t j = 0; (j < 16) && ((i + j) < len); j++) {
  75. print("0x%02X", buff[i + j]);
  76. if ((j < 15) && ((i + j) < (len - 1))) {
  77. print(" ");
  78. }
  79. }
  80. println();
  81. }
  82. }