DFUTrigger.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef CONFIG_BOARD_NRF52840DONGLE_NRF52840
  19. #error "The command for triggerring the DFU is available for nRF52840 Dongle only"
  20. #endif
  21. #include <zephyr/drivers/gpio.h>
  22. #include <zephyr/shell/shell.h>
  23. namespace {
  24. constexpr const char * kGPIOController = "GPIO_0";
  25. constexpr gpio_pin_t kGPIOResetPin = 19;
  26. int cmd_dfu(const struct shell * shell, size_t argc, char ** argv)
  27. {
  28. // nRF52840 Dongle contains immutable bootloader which supports the DFU over a serial port.
  29. // Normally, a user must press the Reset button to reboot into the bootloader, but it can
  30. // also be done programatically by setting an appropriate GPIO pin.
  31. const device * gpioController = device_get_binding(kGPIOController);
  32. if (!gpioController)
  33. {
  34. shell_fprintf(shell, SHELL_NORMAL, "Cannot find GPIO controller");
  35. return -ENOEXEC;
  36. }
  37. if (gpio_pin_configure(gpioController, kGPIOResetPin, GPIO_OUTPUT))
  38. {
  39. shell_fprintf(shell, SHELL_NORMAL, "Cannot configure GPIO reset pin");
  40. return -ENOEXEC;
  41. }
  42. if (gpio_pin_set_raw(gpioController, kGPIOResetPin, 0))
  43. {
  44. shell_fprintf(shell, SHELL_NORMAL, "Cannot set GPIO reset pin");
  45. return -ENOEXEC;
  46. }
  47. return 0;
  48. }
  49. } // namespace
  50. SHELL_CMD_ARG_REGISTER(dfu, NULL, "Trigger DFU over serial port", cmd_dfu, 0, 0);