ser_phy_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * Copyright (c) 2014 - 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 "ser_phy.h"
  41. #include "ser_config.h"
  42. #ifdef SER_CONNECTIVITY
  43. #include "ser_phy_config_conn.h"
  44. #else
  45. #include "ser_phy_config_app.h"
  46. #endif
  47. #include "nrf_drv_uart.h"
  48. #include "app_error.h"
  49. #include "app_util.h"
  50. #include "app_util_platform.h"
  51. #define UART_TRANSFER_MAX 255
  52. static const nrf_drv_uart_t m_uart = NRF_DRV_UART_INSTANCE(0);
  53. static const nrf_drv_uart_config_t m_uart_config = {
  54. .pseltxd = SER_PHY_UART_TX,
  55. .pselrxd = SER_PHY_UART_RX,
  56. .pselrts = SER_PHY_UART_RTS,
  57. .pselcts = SER_PHY_UART_CTS,
  58. .p_context = NULL,
  59. .interrupt_priority = UART_IRQ_PRIORITY,
  60. #if defined(NRF_DRV_UART_WITH_UARTE) && defined(NRF_DRV_UART_WITH_UART)
  61. .use_easy_dma = true,
  62. #endif
  63. // These values are common for application and connectivity, they are
  64. // defined in "ser_config.h".
  65. .hwfc = SER_PHY_UART_FLOW_CTRL,
  66. .parity = SER_PHY_UART_PARITY,
  67. .baudrate = (nrf_uart_baudrate_t)SER_PHY_UART_BAUDRATE
  68. };
  69. static bool volatile m_tx_in_progress;
  70. static uint8_t m_tx_header_buf[SER_PHY_HEADER_SIZE];
  71. static uint16_t m_bytes_to_transmit;
  72. static uint8_t const * mp_tx_buffer;
  73. static uint8_t m_rx_header_buf[SER_PHY_HEADER_SIZE];
  74. static uint16_t m_bytes_to_receive;
  75. static uint8_t m_rx_drop_buf[1];
  76. static ser_phy_events_handler_t m_ser_phy_event_handler;
  77. static ser_phy_evt_t m_ser_phy_rx_event;
  78. static void packet_sent_callback(void)
  79. {
  80. static ser_phy_evt_t const event = {
  81. .evt_type = SER_PHY_EVT_TX_PKT_SENT,
  82. };
  83. m_ser_phy_event_handler(event);
  84. }
  85. static void buffer_request_callback(uint16_t num_of_bytes)
  86. {
  87. m_ser_phy_rx_event.evt_type = SER_PHY_EVT_RX_BUF_REQUEST;
  88. m_ser_phy_rx_event.evt_params.rx_buf_request.num_of_bytes = num_of_bytes;
  89. m_ser_phy_event_handler(m_ser_phy_rx_event);
  90. }
  91. static void packet_received_callback(void)
  92. {
  93. m_ser_phy_event_handler(m_ser_phy_rx_event);
  94. }
  95. static void packet_dropped_callback(void)
  96. {
  97. static ser_phy_evt_t const event = {
  98. .evt_type = SER_PHY_EVT_RX_PKT_DROPPED,
  99. };
  100. m_ser_phy_event_handler(event);
  101. }
  102. static void hardware_error_callback(uint32_t hw_error)
  103. {
  104. ser_phy_evt_t event = {
  105. .evt_type = SER_PHY_EVT_HW_ERROR,
  106. .evt_params.hw_error.error_code = hw_error,
  107. };
  108. m_ser_phy_event_handler(event);
  109. }
  110. static void packet_rx_start(void)
  111. {
  112. APP_ERROR_CHECK(nrf_drv_uart_rx(&m_uart, m_rx_header_buf,
  113. SER_PHY_HEADER_SIZE));
  114. }
  115. static void packet_byte_drop(void)
  116. {
  117. APP_ERROR_CHECK(nrf_drv_uart_rx(&m_uart, m_rx_drop_buf, 1));
  118. }
  119. static void uart_event_handler(nrf_drv_uart_event_t * p_event,
  120. void * p_context)
  121. {
  122. (void)p_context;
  123. switch (p_event->type)
  124. {
  125. case NRF_DRV_UART_EVT_ERROR:
  126. // Process the error only if this is a parity or overrun error.
  127. // Break and framing errors will always occur before the other
  128. // side becomes active.
  129. if (p_event->data.error.error_mask &
  130. (NRF_UART_ERROR_PARITY_MASK | NRF_UART_ERROR_OVERRUN_MASK))
  131. {
  132. // Pass error source to upper layer.
  133. hardware_error_callback(p_event->data.error.error_mask);
  134. }
  135. packet_rx_start();
  136. break;
  137. case NRF_DRV_UART_EVT_TX_DONE:
  138. if (p_event->data.rxtx.p_data == m_tx_header_buf)
  139. {
  140. #if (SER_HAL_TRANSPORT_TX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  141. if (m_bytes_to_transmit > UART_TRANSFER_MAX)
  142. {
  143. APP_ERROR_CHECK(nrf_drv_uart_tx(&m_uart, mp_tx_buffer,
  144. UART_TRANSFER_MAX));
  145. }
  146. else
  147. #endif // (SER_HAL_TRANSPORT_TX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  148. {
  149. APP_ERROR_CHECK(nrf_drv_uart_tx(&m_uart, mp_tx_buffer,
  150. m_bytes_to_transmit));
  151. }
  152. }
  153. else
  154. {
  155. #if (SER_HAL_TRANSPORT_TX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  156. ASSERT(p_event->data.rxtx.bytes <= m_bytes_to_transmit);
  157. m_bytes_to_transmit -= p_event->data.rxtx.bytes;
  158. if (m_bytes_to_transmit != 0)
  159. {
  160. APP_ERROR_CHECK(nrf_drv_uart_tx(&m_uart,
  161. p_event->data.rxtx.p_data + p_event->data.rxtx.bytes,
  162. m_bytes_to_transmit < UART_TRANSFER_MAX ?
  163. m_bytes_to_transmit : UART_TRANSFER_MAX));
  164. }
  165. else
  166. #endif // (SER_HAL_TRANSPORT_TX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  167. {
  168. m_tx_in_progress = false;
  169. packet_sent_callback();
  170. }
  171. }
  172. break;
  173. case NRF_DRV_UART_EVT_RX_DONE:
  174. if (p_event->data.rxtx.p_data == m_rx_header_buf)
  175. {
  176. m_bytes_to_receive = uint16_decode(m_rx_header_buf);
  177. buffer_request_callback(m_bytes_to_receive);
  178. }
  179. else if (p_event->data.rxtx.p_data == m_rx_drop_buf)
  180. {
  181. --m_bytes_to_receive;
  182. if (m_bytes_to_receive != 0)
  183. {
  184. packet_byte_drop();
  185. }
  186. else
  187. {
  188. packet_dropped_callback();
  189. packet_rx_start();
  190. }
  191. }
  192. else
  193. {
  194. #if (SER_HAL_TRANSPORT_RX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  195. ASSERT(p_event->data.rxtx.bytes <= m_bytes_to_receive);
  196. m_bytes_to_receive -= p_event->data.rxtx.bytes;
  197. if (m_bytes_to_receive != 0)
  198. {
  199. APP_ERROR_CHECK(nrf_drv_uart_rx(&m_uart,
  200. p_event->data.rxtx.p_data + p_event->data.rxtx.bytes,
  201. m_bytes_to_receive < UART_TRANSFER_MAX ?
  202. m_bytes_to_receive : UART_TRANSFER_MAX));
  203. }
  204. else
  205. #endif // (SER_HAL_TRANSPORT_RX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  206. {
  207. packet_received_callback();
  208. packet_rx_start();
  209. }
  210. }
  211. break;
  212. default:
  213. APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
  214. }
  215. }
  216. /** API FUNCTIONS */
  217. uint32_t ser_phy_open(ser_phy_events_handler_t events_handler)
  218. {
  219. uint32_t err_code;
  220. if (events_handler == NULL)
  221. {
  222. return NRF_ERROR_NULL;
  223. }
  224. // Check if function was not called before.
  225. if (m_ser_phy_event_handler != NULL)
  226. {
  227. return NRF_ERROR_INVALID_STATE;
  228. }
  229. err_code = nrf_drv_uart_init(&m_uart, &m_uart_config, uart_event_handler);
  230. if (err_code != NRF_SUCCESS)
  231. {
  232. return NRF_ERROR_INVALID_PARAM;
  233. }
  234. m_ser_phy_event_handler = events_handler;
  235. packet_rx_start();
  236. return err_code;
  237. }
  238. uint32_t ser_phy_tx_pkt_send(const uint8_t * p_buffer, uint16_t num_of_bytes)
  239. {
  240. if (p_buffer == NULL)
  241. {
  242. return NRF_ERROR_NULL;
  243. }
  244. else if (num_of_bytes == 0)
  245. {
  246. return NRF_ERROR_INVALID_PARAM;
  247. }
  248. bool busy;
  249. CRITICAL_REGION_ENTER();
  250. busy = m_tx_in_progress;
  251. m_tx_in_progress = true;
  252. CRITICAL_REGION_EXIT();
  253. if (busy)
  254. {
  255. return NRF_ERROR_BUSY;
  256. }
  257. (void)uint16_encode(num_of_bytes, m_tx_header_buf);
  258. mp_tx_buffer = p_buffer;
  259. m_bytes_to_transmit = num_of_bytes;
  260. APP_ERROR_CHECK(nrf_drv_uart_tx(&m_uart, m_tx_header_buf,
  261. SER_PHY_HEADER_SIZE));
  262. return NRF_SUCCESS;
  263. }
  264. uint32_t ser_phy_rx_buf_set(uint8_t * p_buffer)
  265. {
  266. if (m_ser_phy_rx_event.evt_type != SER_PHY_EVT_RX_BUF_REQUEST)
  267. {
  268. return NRF_ERROR_INVALID_STATE;
  269. }
  270. m_ser_phy_rx_event.evt_type = SER_PHY_EVT_RX_PKT_RECEIVED;
  271. m_ser_phy_rx_event.evt_params.rx_pkt_received.p_buffer = p_buffer;
  272. m_ser_phy_rx_event.evt_params.rx_pkt_received.num_of_bytes =
  273. m_bytes_to_receive;
  274. // If there is not enough memory to receive the packet (no buffer was
  275. // provided), drop its data byte by byte (using an internal 1-byte buffer).
  276. if (p_buffer == NULL)
  277. {
  278. packet_byte_drop();
  279. }
  280. #if (SER_HAL_TRANSPORT_RX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  281. else if (m_bytes_to_receive > UART_TRANSFER_MAX)
  282. {
  283. APP_ERROR_CHECK(nrf_drv_uart_rx(&m_uart, p_buffer, UART_TRANSFER_MAX));
  284. }
  285. #endif // (SER_HAL_TRANSPORT_RX_MAX_PKT_SIZE > UART_TRANSFER_MAX)
  286. else
  287. {
  288. APP_ERROR_CHECK(nrf_drv_uart_rx(&m_uart, p_buffer, m_bytes_to_receive));
  289. }
  290. return NRF_SUCCESS;
  291. }
  292. void ser_phy_close(void)
  293. {
  294. nrf_drv_uart_uninit(&m_uart);
  295. m_ser_phy_event_handler = NULL;
  296. }
  297. void ser_phy_interrupts_enable(void)
  298. {
  299. IRQn_Type irqn;
  300. #if defined(NRF_DRV_UART_WITH_UARTE)
  301. irqn = nrfx_get_irq_number(m_uart.uarte.p_reg);
  302. #else
  303. irqn = nrfx_get_irq_number(m_uart.uart.p_reg);
  304. #endif
  305. NVIC_EnableIRQ(irqn);
  306. }
  307. void ser_phy_interrupts_disable(void)
  308. {
  309. IRQn_Type irqn;
  310. #if defined(NRF_DRV_UART_WITH_UARTE)
  311. irqn = nrfx_get_irq_number(m_uart.uarte.p_reg);
  312. #else
  313. irqn = nrfx_get_irq_number(m_uart.uart.p_reg);
  314. #endif
  315. NVIC_DisableIRQ(irqn);
  316. }