forked from monpetit/nrf52-spi-i2c-master-ssd1306
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart_module.c
95 lines (80 loc) · 2.67 KB
/
uart_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "nrf.h"
#include "app_uart.h"
// #include "boards.h"
#include "app_error.h"
#include "bsp.h"
#include "nordic_common.h"
#include "uart_module.h"
#include "ds3234_rtc.h"
#include <stdio.h>
#include <stdbool.h>
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
uint8_t time_buffer[128] = {0};
extern volatile bool change_rtc;
/**@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' i.e '\n' (hex 0x0D) or if the string has reached a length of
* @ref NUS_MAX_DATA_LENGTH.
*/
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t index = 0;
static uint8_t ch;
switch (p_event->evt_type) {
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&ch));
app_uart_put(ch);
if (ch == '{') {
index = 0;
time_buffer[index++] = ch;
}
else if (ch == '}') {
time_buffer[index] = '\0';
index = 0;
printf("\r\n* received string = [%s]\r\n", time_buffer + 1);
change_rtc = true;
}
else if (index >= 128)
index = 0;
else
time_buffer[index++] = ch;
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
/**@snippet [Handling the data received over UART] */
/**@brief Function for initializing the UART module.
*/
/**@snippet [UART Initialization] */
void uart_init(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params = {
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud38400
};
APP_UART_FIFO_INIT( &comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_event_handle,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */