nrf_log.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "nrf.h"
  2. #include "nrf_log.h"
  3. #include "nrf_error.h"
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "proc.h"
  8. #include "type.h"
  9. #if defined(NRF_LOG_USES_RTT) && NRF_LOG_USES_RTT == 1
  10. #include <SEGGER_RTT_Conf.h>
  11. #include <SEGGER_RTT.h>
  12. static char buf_normal_up[BUFFER_SIZE_UP];
  13. static char buf_down[BUFFER_SIZE_DOWN];
  14. uint32_t log_rtt_init(void)
  15. {
  16. static bool initialized = false;
  17. if (initialized)
  18. {
  19. return NRF_SUCCESS;
  20. }
  21. if (SEGGER_RTT_ConfigUpBuffer(LOG_TERMINAL_NORMAL,
  22. "Normal",
  23. buf_normal_up,
  24. BUFFER_SIZE_UP,
  25. SEGGER_RTT_MODE_NO_BLOCK_TRIM
  26. )
  27. != 0)
  28. {
  29. return NRF_ERROR_INVALID_STATE;
  30. }
  31. if (SEGGER_RTT_ConfigDownBuffer(LOG_TERMINAL_INPUT,
  32. "Input",
  33. buf_down,
  34. BUFFER_SIZE_DOWN,
  35. SEGGER_RTT_MODE_NO_BLOCK_SKIP
  36. )
  37. != 0)
  38. {
  39. return NRF_ERROR_INVALID_STATE;
  40. }
  41. initialized = true;
  42. return NRF_SUCCESS;
  43. }
  44. // Forward declaration of SEGGER RTT vprintf function
  45. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList);
  46. void log_rtt_printf(int terminal_index, char * format_msg, ...)
  47. {
  48. //lint -save -e526 -e628 -e530
  49. va_list p_args;
  50. va_start(p_args, format_msg);
  51. (void)SEGGER_RTT_vprintf(terminal_index, format_msg, &p_args);
  52. va_end(p_args);
  53. //lint -restore
  54. }
  55. __INLINE void log_rtt_write_string(int terminal_index, int num_args, ...)
  56. {
  57. const char* msg;
  58. //lint -save -e516 -e530
  59. va_list p_args;
  60. va_start(p_args, num_args);
  61. //lint -restore
  62. for (int i = 0; i < num_args; i++)
  63. {
  64. //lint -save -e26 -e10 -e64 -e526 -e628 -e530
  65. msg = va_arg(p_args, const char*);
  66. //lint -restore
  67. (void)SEGGER_RTT_WriteString(terminal_index, msg);
  68. }
  69. va_end(p_args);
  70. }
  71. void log_rtt_write_hex(int terminal_index, uint32_t value)
  72. {
  73. char temp[11];
  74. temp[0] = '0';
  75. temp[1] = 'x';
  76. temp[10] = 0; // Null termination
  77. uint8_t nibble;
  78. uint8_t i = 8;
  79. while(i-- != 0)
  80. {
  81. nibble = (value >> (4 * i)) & 0x0F;
  82. temp[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
  83. }
  84. (void)SEGGER_RTT_WriteString(terminal_index, temp);
  85. }
  86. void log_rtt_write_hex_char(int terminal_index, uint8_t value)
  87. {
  88. char temp[3];
  89. temp[2] = 0; // Null termination
  90. uint8_t nibble;
  91. uint8_t i = 2;
  92. while(i-- != 0)
  93. {
  94. nibble = (value >> (4 * i)) & 0x0F;
  95. temp[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
  96. }
  97. (void)SEGGER_RTT_WriteString(terminal_index, temp);
  98. }
  99. __INLINE int log_rtt_has_input()
  100. {
  101. return SEGGER_RTT_HasKey();
  102. }
  103. uint32_t log_rtt_read_input(char * c)
  104. {
  105. int r;
  106. r = SEGGER_RTT_Read(LOG_TERMINAL_INPUT, c, 1);
  107. if (r == 1)
  108. return NRF_SUCCESS;
  109. else
  110. return NRF_ERROR_NULL;
  111. }
  112. #elif defined(NRF_LOG_USES_UART) && NRF_LOG_USES_UART == 1
  113. #include "app_uart.h"
  114. #include "app_error.h"
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include "nrf.h"
  118. #include "bsp.h"
  119. #define MAX_TEST_DATA_BYTES (15U) /**< max number of test bytes to be used for tx and rx. */
  120. #define UART_TX_BUF_SIZE 128 /**< UART TX buffer size. */
  121. #define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
  122. static uint8_t m_uart_data;
  123. static bool m_uart_has_input;
  124. void uart_error_cb(app_uart_evt_t * p_event)
  125. {
  126. if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  127. {
  128. APP_ERROR_HANDLER(p_event->data.error_communication);
  129. }
  130. else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  131. {
  132. APP_ERROR_HANDLER(p_event->data.error_code);
  133. }
  134. }
  135. uint32_t log_uart_init()
  136. {
  137. static bool initialized = false;
  138. if (initialized)
  139. {
  140. return NRF_SUCCESS;
  141. }
  142. uint32_t err_code;
  143. const app_uart_comm_params_t comm_params =
  144. {
  145. RX_PIN_NUMBER,
  146. TX_PIN_NUMBER,
  147. RTS_PIN_NUMBER,
  148. CTS_PIN_NUMBER,
  149. //APP_UART_FLOW_CONTROL_ENABLED,
  150. APP_UART_FLOW_CONTROL_DISABLED,
  151. false,
  152. UART_BAUDRATE_BAUDRATE_Baud115200 //UART_BAUDRATE_BAUDRATE_Baud115200
  153. };
  154. APP_UART_FIFO_INIT(&comm_params,
  155. UART_RX_BUF_SIZE,
  156. UART_TX_BUF_SIZE,
  157. uart_error_cb,
  158. APP_IRQ_PRIORITY_LOW,
  159. err_code);
  160. initialized = true;
  161. return err_code;
  162. }
  163. //lint -save -e530 -e64
  164. void log_uart_printf(const char * format_msg, ...)
  165. {
  166. va_list p_args;
  167. va_start(p_args, format_msg);
  168. (void)vprintf(format_msg, p_args);
  169. va_end(p_args);
  170. }
  171. __INLINE void log_uart_write_string_many(int num_args, ...)
  172. {
  173. const char* msg;
  174. va_list p_args;
  175. va_start(p_args, num_args);
  176. for (int i = 0; i < num_args; i++)
  177. {
  178. msg = va_arg(p_args, const char*);
  179. log_uart_write_string(msg);
  180. }
  181. va_end(p_args);
  182. }
  183. __INLINE void log_uart_write_string(const char* msg)
  184. {
  185. while( *msg )
  186. {
  187. (void)app_uart_put(*msg++);
  188. }
  189. }
  190. //lint -restore
  191. void log_uart_write_hex(uint32_t value)
  192. {
  193. uint8_t nibble;
  194. uint8_t i = 8;
  195. (void)app_uart_put('0');
  196. (void)app_uart_put('x');
  197. while( i-- != 0 )
  198. {
  199. nibble = (value >> (4 * i)) & 0x0F;
  200. (void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
  201. }
  202. }
  203. void log_uart_write_hex_char(uint8_t c)
  204. {
  205. uint8_t nibble;
  206. uint8_t i = 2;
  207. while( i-- != 0 )
  208. {
  209. nibble = (c >> (4 * i)) & 0x0F;
  210. (void)app_uart_put( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
  211. }
  212. }
  213. __INLINE int log_uart_has_input()
  214. {
  215. if (m_uart_has_input) return 1;
  216. if (app_uart_get(&m_uart_data) == NRF_SUCCESS)
  217. {
  218. m_uart_has_input = true;
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. uint32_t log_uart_read_input(char * c)
  224. {
  225. if (m_uart_has_input)
  226. {
  227. *c = (char)m_uart_data;
  228. m_uart_has_input = false;
  229. return NRF_SUCCESS;
  230. }
  231. if (app_uart_get((uint8_t *)c) == NRF_SUCCESS)
  232. {
  233. return NRF_SUCCESS;
  234. }
  235. return NRF_ERROR_NULL;
  236. }
  237. void log_uart_write_hexs(const char* msg, uint8_t* data, uint32_t length)
  238. {
  239. uint32_t i;
  240. log_uart_write_string(msg);
  241. log_uart_printf(":");
  242. for(i=0; i<length; i++)
  243. {
  244. log_uart_printf("%02X", data[i]);
  245. }
  246. log_uart_printf("\n");
  247. }
  248. #elif defined(NRF_LOG_USES_RAW_UART) && NRF_LOG_USES_RAW_UART == 1
  249. #include "app_uart.h"
  250. #include <stdio.h>
  251. #include <string.h>
  252. #include "bsp.h"
  253. uint32_t log_raw_uart_init()
  254. {
  255. // Disable UART
  256. NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Disabled;
  257. // Configure RX/TX pins
  258. nrf_gpio_cfg_output( TX_PIN_NUMBER );
  259. nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
  260. // Set a default baud rate of UART0_CONFIG_BAUDRATE
  261. NRF_UART0->PSELTXD = TX_PIN_NUMBER;
  262. NRF_UART0->BAUDRATE = UART0_CONFIG_BAUDRATE;
  263. NRF_UART0->PSELRTS = 0xFFFFFFFF;
  264. NRF_UART0->PSELCTS = 0xFFFFFFFF;
  265. // Disable parity and interrupt
  266. NRF_UART0->CONFIG = (UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos );
  267. NRF_UART0->CONFIG |= (UART_CONFIG_HWFC_Disabled << UART_CONFIG_HWFC_Pos );
  268. // Re-enable the UART
  269. NRF_UART0->ENABLE = UART_ENABLE_ENABLE_Enabled;
  270. NRF_UART0->INTENSET = 0;
  271. NRF_UART0->TASKS_STARTTX = 1;
  272. NRF_UART0->TASKS_STARTRX = 1;
  273. return NRF_SUCCESS;
  274. }
  275. void log_raw_uart_printf(const char * format_msg, ...)
  276. {
  277. static char buffer[256];
  278. va_list p_args;
  279. va_start(p_args, format_msg);
  280. sprintf(buffer, format_msg, p_args);
  281. va_end(p_args);
  282. log_raw_uart_write_string(buffer);
  283. }
  284. __INLINE void log_raw_uart_write_char(const char c)
  285. {
  286. NRF_UART0->TXD = c;
  287. while( NRF_UART0->EVENTS_TXDRDY != 1 );
  288. NRF_UART0->EVENTS_TXDRDY = 0;
  289. }
  290. __INLINE void log_raw_uart_write_string_many(int num_args, ...)
  291. {
  292. const char* msg;
  293. va_list p_args;
  294. va_start(p_args, num_args);
  295. for (int i = 0; i < num_args; i++)
  296. {
  297. msg = va_arg(p_args, const char*);
  298. log_raw_uart_write_string(msg);
  299. }
  300. va_end(p_args);
  301. }
  302. __INLINE void log_raw_uart_write_string(const char* msg)
  303. {
  304. while( *msg )
  305. {
  306. NRF_UART0->TXD = *msg++;
  307. while( NRF_UART0->EVENTS_TXDRDY != 1 );
  308. NRF_UART0->EVENTS_TXDRDY = 0;
  309. }
  310. }
  311. void log_raw_uart_write_hex(uint32_t value)
  312. {
  313. uint8_t nibble;
  314. uint8_t i = 8;
  315. log_raw_uart_write_string( "0x" );
  316. while( i-- != 0 )
  317. {
  318. nibble = (value >> (4 * i)) & 0x0F;
  319. log_raw_uart_write_char( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
  320. }
  321. }
  322. void log_raw_uart_write_hex_char(uint8_t c)
  323. {
  324. uint8_t nibble;
  325. uint8_t i = 2;
  326. while( i-- != 0 )
  327. {
  328. nibble = (c >> (4 * i)) & 0x0F;
  329. log_raw_uart_write_hex( (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble) );
  330. }
  331. }
  332. __INLINE int log_raw_uart_has_input()
  333. {
  334. return 0;
  335. }
  336. uint32_t log_raw_uart_read_input(char * c)
  337. {
  338. return NRF_ERROR_NULL;
  339. }
  340. #endif // NRF_LOG_USES_RAW_UART == 1
  341. void log_spi_printf(const char * format_msg, ...)
  342. {
  343. uint8_t buffer[128];
  344. va_list p_args;
  345. va_start(p_args, format_msg);
  346. sprintf((char *restrict) buffer, (uint8_t*)format_msg, p_args);
  347. va_end(p_args);
  348. // spis_transfer_data(BLE_CMD_LOG, 0x01, buffer, strlen((const char*)buffer));
  349. }
  350. const char* log_hex_char(const char c)
  351. {
  352. static volatile char hex_string[3];
  353. hex_string[2] = 0; // Null termination
  354. uint8_t nibble;
  355. uint8_t i = 2;
  356. while(i-- != 0)
  357. {
  358. nibble = (c >> (4 * i)) & 0x0F;
  359. hex_string[1-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
  360. }
  361. return (const char*) hex_string;
  362. }
  363. const char* log_hex(uint32_t value)
  364. {
  365. static volatile char hex_string[11];
  366. hex_string[0] = '0';
  367. hex_string[1] = 'x';
  368. hex_string[10] = 0;
  369. uint8_t nibble;
  370. uint8_t i = 8;
  371. while(i-- != 0)
  372. {
  373. nibble = (value >> (4 * i)) & 0x0F;
  374. hex_string[9-i] = (nibble > 9) ? ('A' + nibble - 10) : ('0' + nibble);
  375. }
  376. return (const char*)hex_string;
  377. }