nrf_dfu_trigger_usb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2017 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_dfu_trigger_usb.h"
  41. #include "app_usbd.h"
  42. #include "app_usbd_nrf_dfu_trigger.h"
  43. #include "nrf_drv_clock.h"
  44. #include "nrf_log_ctrl.h"
  45. #include "nrf_gpio.h"
  46. #include "boards.h"
  47. #include "app_util.h"
  48. #include "app_usbd_serial_num.h"
  49. #define NRF_LOG_MODULE_NAME nrf_dfu_trigger_usb
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. #ifndef BSP_SELF_PINRESET_PIN
  53. #error "This module is intended to be used with boards that have the GP pin shortened with the RESET pin."
  54. #endif
  55. /**
  56. * @brief Enable power USB detection.
  57. *
  58. * Configure if the example supports USB port connection.
  59. */
  60. #ifndef USBD_POWER_DETECTION
  61. #define USBD_POWER_DETECTION true
  62. #endif
  63. #define DFU_FLASH_PAGE_SIZE (NRF_FICR->CODEPAGESIZE)
  64. #define DFU_FLASH_PAGE_COUNT (NRF_FICR->CODESIZE)
  65. // Semantic versioning string.
  66. #define VERSION_STRING STRINGIFY(APP_VERSION_MAJOR) "." STRINGIFY(APP_VERSION_MINOR) "." STRINGIFY(APP_VERSION_PATCH) APP_VERSION_PRERELEASE APP_VERSION_METADATA
  67. static uint8_t m_version_string[] = APP_NAME " " VERSION_STRING; ///< Human-readable version string.
  68. static app_usbd_nrf_dfu_trigger_nordic_info_t m_dfu_info; ///< Struct with various information about the current firmware.
  69. static void dfu_trigger_evt_handler(app_usbd_class_inst_t const * p_inst,
  70. app_usbd_nrf_dfu_trigger_user_event_t event)
  71. {
  72. UNUSED_PARAMETER(p_inst);
  73. switch (event)
  74. {
  75. case APP_USBD_NRF_DFU_TRIGGER_USER_EVT_DETACH:
  76. NRF_LOG_INFO("DFU Detach request received. Triggering a pin reset.");
  77. NRF_LOG_FINAL_FLUSH();
  78. nrf_gpio_cfg_output(BSP_SELF_PINRESET_PIN);
  79. nrf_gpio_pin_clear(BSP_SELF_PINRESET_PIN);
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. APP_USBD_NRF_DFU_TRIGGER_GLOBAL_DEF(m_app_dfu,
  86. NRF_DFU_TRIGGER_USB_INTERFACE_NUM,
  87. &m_dfu_info,
  88. m_version_string,
  89. dfu_trigger_evt_handler);
  90. static void usbd_user_evt_handler(app_usbd_event_type_t event)
  91. {
  92. switch (event)
  93. {
  94. case APP_USBD_EVT_DRV_SUSPEND:
  95. break;
  96. case APP_USBD_EVT_DRV_RESUME:
  97. break;
  98. case APP_USBD_EVT_STARTED:
  99. break;
  100. case APP_USBD_EVT_STOPPED:
  101. app_usbd_disable();
  102. break;
  103. case APP_USBD_EVT_POWER_DETECTED:
  104. NRF_LOG_INFO("USB power detected");
  105. if (!nrf_drv_usbd_is_enabled())
  106. {
  107. app_usbd_enable();
  108. }
  109. break;
  110. case APP_USBD_EVT_POWER_REMOVED:
  111. NRF_LOG_INFO("USB power removed");
  112. app_usbd_stop();
  113. break;
  114. case APP_USBD_EVT_POWER_READY:
  115. NRF_LOG_INFO("USB ready");
  116. app_usbd_start();
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. static void strings_create(void)
  123. {
  124. uint8_t prev_char = 'a'; // Arbitrary valid char, not '-'.
  125. // Remove characters that are not supported in semantic version strings.
  126. for (size_t i = strlen(APP_NAME) + 1; i < strlen((char*)m_version_string); i++)
  127. {
  128. if (((m_version_string[i] >= 'a') && (m_version_string[i] <= 'z'))
  129. || ((m_version_string[i] >= 'A') && (m_version_string[i] <= 'Z'))
  130. || ((m_version_string[i] >= '0') && (m_version_string[i] <= '9'))
  131. || (m_version_string[i] == '+')
  132. || (m_version_string[i] == '.')
  133. || (m_version_string[i] == '-'))
  134. {
  135. // Valid semantic version character.
  136. }
  137. else if (prev_char == '-')
  138. {
  139. m_version_string[i] = '0';
  140. }
  141. else
  142. {
  143. m_version_string[i] = '-';
  144. }
  145. prev_char = m_version_string[i];
  146. }
  147. #if !NRF_DFU_TRIGGER_USB_USB_SHARED
  148. app_usbd_serial_num_generate();
  149. #endif
  150. }
  151. #if !(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)
  152. static void usbd_evt_handler(app_usbd_internal_evt_t const * const p_event)
  153. {
  154. app_usbd_event_execute(p_event);
  155. }
  156. #endif
  157. ret_code_t nrf_dfu_trigger_usb_init(void)
  158. {
  159. ret_code_t ret;
  160. static bool initialized = false;
  161. if (initialized)
  162. {
  163. return NRF_SUCCESS;
  164. }
  165. m_dfu_info.wAddress = CODE_START;
  166. m_dfu_info.wFirmwareSize = CODE_SIZE;
  167. m_dfu_info.wVersionMajor = APP_VERSION_MAJOR;
  168. m_dfu_info.wVersionMinor = APP_VERSION_MINOR;
  169. m_dfu_info.wFirmwareID = APP_ID;
  170. m_dfu_info.wFlashPageSize = DFU_FLASH_PAGE_SIZE;
  171. m_dfu_info.wFlashSize = m_dfu_info.wFlashPageSize * DFU_FLASH_PAGE_COUNT;
  172. strings_create();
  173. if (!NRF_DFU_TRIGGER_USB_USB_SHARED)
  174. {
  175. static const app_usbd_config_t usbd_config = {
  176. #if !(APP_USBD_CONFIG_EVENT_QUEUE_ENABLE)
  177. .ev_handler = usbd_evt_handler,
  178. #endif
  179. .ev_state_proc = usbd_user_evt_handler
  180. };
  181. ret = nrf_drv_clock_init();
  182. if ((ret != NRF_SUCCESS) && (ret != NRF_ERROR_MODULE_ALREADY_INITIALIZED))
  183. {
  184. return ret;
  185. }
  186. ret = app_usbd_init(&usbd_config);
  187. if (ret != NRF_SUCCESS)
  188. {
  189. return ret;
  190. }
  191. }
  192. app_usbd_class_inst_t const * class_dfu = app_usbd_nrf_dfu_trigger_class_inst_get(&m_app_dfu);
  193. ret = app_usbd_class_append(class_dfu);
  194. if (!NRF_DFU_TRIGGER_USB_USB_SHARED)
  195. {
  196. if (USBD_POWER_DETECTION)
  197. {
  198. ret = app_usbd_power_events_enable();
  199. APP_ERROR_CHECK(ret);
  200. }
  201. else
  202. {
  203. NRF_LOG_INFO("No USB power detection enabled\r\nStarting USB now");
  204. app_usbd_enable();
  205. app_usbd_start();
  206. }
  207. }
  208. if (ret == NRF_SUCCESS)
  209. {
  210. initialized = true;
  211. }
  212. return ret;
  213. }