Contiki-NG
Loading...
Searching...
No Matches
dbg-arch.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup nrf
33 * @{
34 *
35 * \addtogroup nrf-os OS drivers
36 * @{
37 *
38 * \addtogroup nrf-dbg Debug driver
39 * @{
40 *
41 * \file
42 * Debug driver for the nRF.
43 * \author
44 * Yago Fontoura do Rosario <yago.rosario@hotmail.com.br>
45 *
46 */
47/*---------------------------------------------------------------------------*/
48#include "contiki.h"
49
50#include "uarte-arch.h"
51#include "usb.h"
52/*---------------------------------------------------------------------------*/
53#if PLATFORM_DBG_CONF_USB
54#define write_byte(b) usb_write((uint8_t *)&b, sizeof(uint8_t))
55#define flush() usb_flush()
56#else /* PLATFORM_DBG_CONF_USB */
57#define write_byte(b) uarte_write(b)
58#define flush()
59#endif /* PLATFORM_DBG_CONF_USB */
60/*---------------------------------------------------------------------------*/
61#if defined(NRF5340_XXAA_NETWORK)
62/*
63 * On the nRF5340 network core, redirect all debug output to a shared
64 * memory ring buffer. The application core drains it and prints with
65 * a [NET] prefix, avoiding UART pin contention between the two cores.
66 */
67#include "nrf-ipc.h"
68/*---------------------------------------------------------------------------*/
69int
70dbg_putchar(int c)
71{
72 volatile struct nrf_ipc_shared_mem *shm = NRF_IPC_SHARED_MEM;
73 uint16_t head = shm->log.head;
74 uint16_t next = (head + 1) % NRF_IPC_LOG_BUF_SIZE;
75
76 /* Drop the character if the buffer is full. */
77 if(next == shm->log.tail) {
78 shm->log.overflow++;
79 return c;
80 }
81
82 shm->log.data[head] = (char)c;
83 __DMB();
84 shm->log.head = next;
85
86 return c;
87}
88/*---------------------------------------------------------------------------*/
89#elif NRF_TRUSTZONE_NONSECURE
90#include "trustzone/tz-api.h"
91
92#define DBG_BUF_SIZE 256
93static char dbg_buf[DBG_BUF_SIZE];
94static uint16_t dbg_pos;
95/*---------------------------------------------------------------------------*/
96int
97dbg_putchar(int c)
98{
99 if(dbg_pos < DBG_BUF_SIZE - 1) {
100 dbg_buf[dbg_pos++] = c;
101 }
102
103 if(c == '\n' || dbg_pos >= DBG_BUF_SIZE - 1) {
104 /* Strip the trailing newline; tz_api_println adds one. */
105 uint16_t len = (dbg_pos > 0 && dbg_buf[dbg_pos - 1] == '\n')
106 ? dbg_pos - 1 : dbg_pos;
107 dbg_buf[len] = '\0';
108 tz_api_println(dbg_buf, len);
109 dbg_pos = 0;
110 }
111
112 return c;
113}
114#else
115int
117{
118 write_byte(c);
119
120 if(c == '\n') {
121 flush();
122 }
123
124 return c;
125}
126#endif /* NRF_TRUSTZONE_NONSECURE */
127/*---------------------------------------------------------------------------*/
128unsigned int
129dbg_send_bytes(const unsigned char *s, unsigned int len)
130{
131 unsigned int i;
132
133 if(s == NULL) {
134 return 0;
135 }
136
137 for(i = 0; i < len; i++) {
138 dbg_putchar(s[i]);
139 }
140
141 flush();
142
143 return i;
144}
145/*---------------------------------------------------------------------------*/
146/**
147 * @}
148 * @}
149 * @}
150 */
unsigned int dbg_send_bytes(const unsigned char *s, unsigned int len)
Print a stream of bytes.
Definition dbg-arch.c:53
int dbg_putchar(int c)
Print a character to debug output.
Definition dbg-arch.c:61
#define NRF_IPC_LOG_BUF_SIZE
Size of the log ring buffer for forwarding net core output to the app core.
Definition nrf-ipc.h:102
#define NRF_IPC_SHARED_MEM
Get a pointer to the shared memory structure.
Definition nrf-ipc.h:212
void tz_api_println(const char *text, size_t len)
Print the specified message via the secure world.
Definition tz-api.c:136
IPC protocol definitions for nRF5340 dual-core communication.
Shared memory layout between the application core and the network core.
Definition nrf-ipc.h:154
struct nrf_ipc_shared_mem::@9 log
Log ring buffer (net -> app).
volatile uint32_t overflow
Characters dropped due to full buffer.
Definition nrf-ipc.h:196
volatile uint16_t tail
Written by app core.
Definition nrf-ipc.h:195
volatile uint16_t head
Written by net core.
Definition nrf-ipc.h:194
UARTE header file for the nRF.