uart.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "ble_gap.h"
  5. #include "app_uart.h"
  6. #include "app_util_platform.h"
  7. #include "bsp.h"
  8. #include "uart.h"
  9. #define UART_TX_BUF_SIZE 256
  10. #define UART_RX_BUF_SIZE 256
  11. #define BLE_DATA_LEN 32
  12. static uint8_t uart_data[BLE_DATA_LEN];
  13. static uint8_t uart_len = 0;
  14. void uart_process_data(void)
  15. {
  16. }
  17. /**@brief Function for handling app_uart events.
  18. *
  19. * @details This function will receive a single character from the app_uart module and append it to
  20. * a string. The string will be be sent over BLE when the last character received was a
  21. * 'new line' i.e '\n' (hex 0x0D) or if the string has reached a length of
  22. * @ref NUS_MAX_DATA_LENGTH.
  23. */
  24. /**@snippet [Handling the data received over UART] */
  25. void uart_event_handle(app_uart_evt_t * p_event)
  26. {
  27. switch (p_event->evt_type)
  28. {
  29. case APP_UART_DATA_READY:
  30. UNUSED_VARIABLE(app_uart_get(&uart_data[uart_len]));
  31. uart_len++;
  32. if ((uart_data[uart_len - 1] == '\n') || (uart_len >= (BLE_DATA_LEN)))
  33. {
  34. printf("uart:%s", uart_data);
  35. uart_data[uart_len - 1] = '\0';
  36. uart_data[uart_len - 2] = '\0';
  37. uart_process_data();
  38. memset(uart_data, 0, BLE_DATA_LEN);
  39. uart_len = 0;
  40. }
  41. break;
  42. case APP_UART_COMMUNICATION_ERROR:
  43. APP_ERROR_HANDLER(p_event->data.error_communication);
  44. break;
  45. case APP_UART_FIFO_ERROR:
  46. APP_ERROR_HANDLER(p_event->data.error_code);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. void uart_init(void)
  53. {
  54. uint32_t err_code;
  55. const app_uart_comm_params_t comm_params =
  56. {
  57. RX_PIN_NUMBER,
  58. TX_PIN_NUMBER,
  59. RTS_PIN_NUMBER,
  60. CTS_PIN_NUMBER,
  61. //APP_UART_FLOW_CONTROL_ENABLED,
  62. APP_UART_FLOW_CONTROL_DISABLED,
  63. false,
  64. UART_BAUDRATE_BAUDRATE_Baud115200
  65. };
  66. APP_UART_FIFO_INIT( &comm_params,
  67. UART_RX_BUF_SIZE,
  68. UART_TX_BUF_SIZE,
  69. uart_event_handle,
  70. APP_IRQ_PRIORITY_LOW,
  71. err_code);
  72. APP_ERROR_CHECK(err_code);
  73. }