nrf_dfu_serial_uart.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright (c) 2016 - 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_serial.h"
  41. #include <string.h>
  42. #include "boards.h"
  43. #include "app_util_platform.h"
  44. #include "nrf_dfu_transport.h"
  45. #include "nrf_dfu_req_handler.h"
  46. #include "slip.h"
  47. #include "nrf_balloc.h"
  48. #include "nrf_drv_uart.h"
  49. #define NRF_LOG_MODULE_NAME nrf_dfu_serial_uart
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. /**@file
  53. *
  54. * @defgroup nrf_dfu_serial_uart DFU Serial UART transport
  55. * @ingroup nrf_dfu
  56. * @brief Device Firmware Update (DFU) transport layer using UART.
  57. */
  58. #define NRF_SERIAL_OPCODE_SIZE (sizeof(uint8_t))
  59. #define NRF_UART_MAX_RESPONSE_SIZE_SLIP (2 * NRF_SERIAL_MAX_RESPONSE_SIZE + 1)
  60. #define RX_BUF_SIZE (64) //to get 64bytes payload
  61. #define OPCODE_OFFSET (sizeof(uint32_t) - NRF_SERIAL_OPCODE_SIZE)
  62. #define DATA_OFFSET (OPCODE_OFFSET + NRF_SERIAL_OPCODE_SIZE)
  63. #define UART_SLIP_MTU (2 * (RX_BUF_SIZE + 1) + 1)
  64. #define BALLOC_BUF_SIZE ((CEIL_DIV((RX_BUF_SIZE+OPCODE_SIZE),sizeof(uint32_t))*sizeof(uint32_t)))
  65. NRF_BALLOC_DEF(m_payload_pool, (UART_SLIP_MTU + 1), NRF_DFU_SERIAL_UART_RX_BUFFERS);
  66. static nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0);
  67. static uint8_t m_rx_byte;
  68. static nrf_dfu_serial_t m_serial;
  69. static slip_t m_slip;
  70. static uint8_t m_rsp_buf[NRF_UART_MAX_RESPONSE_SIZE_SLIP];
  71. static bool m_active;
  72. static nrf_dfu_observer_t m_observer;
  73. static uint32_t uart_dfu_transport_init(nrf_dfu_observer_t observer);
  74. static uint32_t uart_dfu_transport_close(nrf_dfu_transport_t const * p_exception);
  75. DFU_TRANSPORT_REGISTER(nrf_dfu_transport_t const uart_dfu_transport) =
  76. {
  77. .init_func = uart_dfu_transport_init,
  78. .close_func = uart_dfu_transport_close,
  79. };
  80. static void payload_free(void * p_buf)
  81. {
  82. uint8_t * p_buf_root = (uint8_t *)p_buf - DATA_OFFSET; //pointer is shifted to point to data
  83. nrf_balloc_free(&m_payload_pool, p_buf_root);
  84. }
  85. static ret_code_t rsp_send(uint8_t const * p_data, uint32_t length)
  86. {
  87. uint32_t slip_len;
  88. (void) slip_encode(m_rsp_buf, (uint8_t *)p_data, length, &slip_len);
  89. return nrf_drv_uart_tx(&m_uart, m_rsp_buf, slip_len);
  90. }
  91. static __INLINE void on_rx_complete(nrf_dfu_serial_t * p_transport, uint8_t * p_data, uint8_t len)
  92. {
  93. ret_code_t ret_code = NRF_ERROR_TIMEOUT;
  94. // Check if there is byte to process. Zero length transfer means that RXTO occured.
  95. if (len)
  96. {
  97. ret_code = slip_decode_add_byte(&m_slip, p_data[0]);
  98. }
  99. (void) nrf_drv_uart_rx(&m_uart, &m_rx_byte, 1);
  100. if (ret_code == NRF_SUCCESS)
  101. {
  102. nrf_dfu_serial_on_packet_received(p_transport,
  103. (uint8_t const *)m_slip.p_buffer,
  104. m_slip.current_index);
  105. uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool);
  106. if (p_rx_buf == NULL)
  107. {
  108. NRF_LOG_ERROR("Failed to allocate buffer");
  109. return;
  110. }
  111. NRF_LOG_INFO("Allocated buffer %x", p_rx_buf);
  112. // reset the slip decoding
  113. m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET];
  114. m_slip.current_index = 0;
  115. m_slip.state = SLIP_STATE_DECODING;
  116. }
  117. }
  118. static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
  119. {
  120. switch (p_event->type)
  121. {
  122. case NRF_DRV_UART_EVT_RX_DONE:
  123. on_rx_complete((nrf_dfu_serial_t*)p_context,
  124. p_event->data.rxtx.p_data,
  125. p_event->data.rxtx.bytes);
  126. break;
  127. case NRF_DRV_UART_EVT_ERROR:
  128. APP_ERROR_HANDLER(p_event->data.error.error_mask);
  129. break;
  130. default:
  131. // No action.
  132. break;
  133. }
  134. }
  135. static uint32_t uart_dfu_transport_init(nrf_dfu_observer_t observer)
  136. {
  137. uint32_t err_code = NRF_SUCCESS;
  138. if (m_active)
  139. {
  140. return err_code;
  141. }
  142. NRF_LOG_DEBUG("serial_dfu_transport_init()");
  143. m_observer = observer;
  144. err_code = nrf_balloc_init(&m_payload_pool);
  145. if (err_code != NRF_SUCCESS)
  146. {
  147. return err_code;
  148. }
  149. uint8_t * p_rx_buf = nrf_balloc_alloc(&m_payload_pool);
  150. m_slip.p_buffer = &p_rx_buf[OPCODE_OFFSET];
  151. m_slip.current_index = 0;
  152. m_slip.buffer_len = UART_SLIP_MTU;
  153. m_slip.state = SLIP_STATE_DECODING;
  154. m_serial.rsp_func = rsp_send;
  155. m_serial.payload_free_func = payload_free;
  156. m_serial.mtu = UART_SLIP_MTU;
  157. m_serial.p_rsp_buf = &m_rsp_buf[NRF_UART_MAX_RESPONSE_SIZE_SLIP -
  158. NRF_SERIAL_MAX_RESPONSE_SIZE];
  159. m_serial.p_low_level_transport = &uart_dfu_transport;
  160. nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;
  161. uart_config.pseltxd = TX_PIN_NUMBER;
  162. uart_config.pselrxd = RX_PIN_NUMBER;
  163. uart_config.pselcts = CTS_PIN_NUMBER;
  164. uart_config.pselrts = RTS_PIN_NUMBER;
  165. uart_config.hwfc = NRF_DFU_SERIAL_UART_USES_HWFC ?
  166. NRF_UART_HWFC_ENABLED : NRF_UART_HWFC_DISABLED;
  167. uart_config.p_context = &m_serial;
  168. err_code = nrf_drv_uart_init(&m_uart, &uart_config, uart_event_handler);
  169. if (err_code != NRF_SUCCESS)
  170. {
  171. NRF_LOG_ERROR("Failed initializing uart");
  172. return err_code;
  173. }
  174. err_code = nrf_drv_uart_rx(&m_uart, &m_rx_byte, 1);
  175. if (err_code != NRF_SUCCESS)
  176. {
  177. NRF_LOG_ERROR("Failed initializing rx");
  178. }
  179. NRF_LOG_DEBUG("serial_dfu_transport_init() completed");
  180. m_active = true;
  181. if (m_observer)
  182. {
  183. m_observer(NRF_DFU_EVT_TRANSPORT_ACTIVATED);
  184. }
  185. return err_code;
  186. }
  187. static uint32_t uart_dfu_transport_close(nrf_dfu_transport_t const * p_exception)
  188. {
  189. if ((m_active == true) && (p_exception != &uart_dfu_transport))
  190. {
  191. nrf_drv_uart_uninit(&m_uart);
  192. m_active = false;
  193. }
  194. return NRF_SUCCESS;
  195. }