暫無描述
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 1.8KB

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