Sin descripción
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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 "util.h"
  10. #define HEARTBEAT_INTERVAL_MS 500
  11. #ifdef PICO_DEFAULT_LED_PIN
  12. static uint32_t last_heartbeat = 0;
  13. #endif // PICO_DEFAULT_LED_PIN
  14. void heartbeat_init(void) {
  15. #ifdef PICO_DEFAULT_LED_PIN
  16. gpio_init(PICO_DEFAULT_LED_PIN);
  17. gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
  18. gpio_put(PICO_DEFAULT_LED_PIN, 1);
  19. #endif // PICO_DEFAULT_LED_PIN
  20. }
  21. void heartbeat_run(void) {
  22. #ifdef PICO_DEFAULT_LED_PIN
  23. uint32_t now = to_ms_since_boot(get_absolute_time());
  24. if (now >= (last_heartbeat + HEARTBEAT_INTERVAL_MS)) {
  25. last_heartbeat = now;
  26. gpio_xor_mask(1 << PICO_DEFAULT_LED_PIN);
  27. }
  28. #endif // PICO_DEFAULT_LED_PIN
  29. }
  30. bool str_startswith(const char *str, const char *start) {
  31. size_t l = strlen(start);
  32. if (l > strlen(str)) {
  33. return false;
  34. }
  35. return (strncmp(str, start, l) == 0);
  36. }
  37. int32_t convert_two_complement(int32_t b) {
  38. if (b & 0x8000) {
  39. b = -1 * ((b ^ 0xffff) + 1);
  40. }
  41. return b;
  42. }
  43. void reset_to_bootloader(void) {
  44. #ifdef PICO_DEFAULT_LED_PIN
  45. reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0);
  46. #else // ! PICO_DEFAULT_LED_PIN
  47. reset_usb_boot(0, 0);
  48. #endif // PICO_DEFAULT_LED_PIN
  49. }
  50. void reset_to_main(void) {
  51. watchdog_enable(1, 1);
  52. while (1) {
  53. // wait 1ms until watchdog kills us
  54. asm volatile("nop");
  55. }
  56. }