Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dbg_trace.c « utilities « STM32_WPAN « ST « Middlewares - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58266c6ba9fba343136adc732ff9ffd1e3c96871 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
  ******************************************************************************
  * @file    dbg_trace.c
  * @author  MCD Application Team
  * @brief   This file contains the Interface with BLE Drivers functions.
  ******************************************************************************
   * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics. 
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the 
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
 */


/* Includes ------------------------------------------------------------------*/
#include "utilities_common.h"
#include "stm_queue.h"
#include "dbg_trace.h"

/* Definition of the function */
#if !defined(__GNUC__)  /* SW4STM32 */
size_t __write(int handle, const unsigned char * buf, size_t bufSize);
#endif

/** @addtogroup TRACE
 * @{
 */


/** @defgroup TRACE_LOG
 * @brief TRACE Logging functions
 * @{
 */

/* Private typedef -----------------------------------------------------------*/
/** @defgroup TRACE Log private typedef 
 * @{
 */

/**
 * @}
 */

/* Private defines -----------------------------------------------------------*/
/** @defgroup TRACE Log private defines 
 * @{
 */

/**
 * @}
 */

/* Private macros ------------------------------------------------------------*/
/** @defgroup TRACE Log private macros 
 * @{
 */
/**
 * @}
 */

/* Private variables ---------------------------------------------------------*/
/** @defgroup TRACE Log private variables 
 * @{
 */
#if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 ))
#if (DBG_TRACE_USE_CIRCULAR_QUEUE != 0)
static queue_t MsgDbgTraceQueue;
static uint8_t MsgDbgTraceQueueBuff[DBG_TRACE_MSG_QUEUE_SIZE];
#endif
__IO ITStatus DbgTracePeripheralReady = SET;
#endif
/**
 * @}
 */

/* Global variables ----------------------------------------------------------*/
/** @defgroup TRACE Log Global variable
 * @{
 */
/**
 * @}
 */

/* Private function prototypes -----------------------------------------------*/
/** @defgroup TRACE Log private function prototypes 
 * @{
 */
#if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 ))
static void DbgTrace_TxCpltCallback(void);
#endif


/**
 * @}
 */


/* Private Functions Definition ------------------------------------------------------*/
/** @defgroup TRACE Log Private function 
 * @{
 */


/* Functions Definition ------------------------------------------------------*/
/** @defgroup TRACE Log APIs 
 * @{
 */

/**
 * @brief  DbgTraceGetFileName: Return filename string extracted from full path information
 * @param  *fullPath Fullpath string (path + filename)
 * @retval char* Pointer on filename string
 */

const char *DbgTraceGetFileName(const char *fullpath)
{
  const char *ret = fullpath;

  if (strrchr(fullpath, '\\') != NULL)
  {
    ret = strrchr(fullpath, '\\') + 1;
  }
  else if (strrchr(fullpath, '/') != NULL)
  {
    ret = strrchr(fullpath, '/') + 1;
  }

  return ret;
}

/**
 * @brief  DbgTraceBuffer: Output buffer content information to output Stream
 * @param  *pBuffer  Pointer on buffer to be output
 * @param  u32Length buffer Size
 * @paramt strFormat string as expected by "printf" function. Used to desrcibe buffer content information.
 * @param  ...       Paremeters to be "formatted" in strFormat string (if any)
 * @retval None
 */

void DbgTraceBuffer(const void *pBuffer, uint32_t u32Length, const char *strFormat, ...)
{
  va_list vaArgs;
  uint32_t u32Index;
  va_start(vaArgs, strFormat);
  vprintf(strFormat, vaArgs);
  va_end(vaArgs);
  for (u32Index = 0; u32Index < u32Length; u32Index ++)
  {
    printf(" %02X", ((const uint8_t *) pBuffer)[u32Index]);
  }
}

#if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 ))
/**
 * @brief  DBG_TRACE USART Tx Transfer completed callback
 * @param  UartHandle: UART handle.
 * @note   Indicate the end of the transmission of a DBG_TRACE trace buffer to DBG_TRACE USART. If queue
 *         contains new trace data to transmit, start a new transmission.
 * @retval None
 */
static void DbgTrace_TxCpltCallback(void)
{
#if (DBG_TRACE_USE_CIRCULAR_QUEUE != 0)
  uint8_t* buf;
  uint16_t bufSize;

  BACKUP_PRIMASK();

  DISABLE_IRQ();			/**< Disable all interrupts by setting PRIMASK bit on Cortex*/
  /* Remove element just sent to UART */
  CircularQueue_Remove(&MsgDbgTraceQueue,&bufSize);

  /* Sense if new data to be sent */
  buf=CircularQueue_Sense(&MsgDbgTraceQueue,&bufSize);


  if ( buf != NULL) 
  {
    RESTORE_PRIMASK();
    DbgOutputTraces((uint8_t*)buf, bufSize, DbgTrace_TxCpltCallback);
  } 
  else
  {
    DbgTracePeripheralReady = SET;
    RESTORE_PRIMASK();
  }

#else
  BACKUP_PRIMASK();

  DISABLE_IRQ();      /**< Disable all interrupts by setting PRIMASK bit on Cortex*/

  DbgTracePeripheralReady = SET;

  RESTORE_PRIMASK();
#endif
}
#endif

void DbgTraceInit( void )
{
#if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 ))
  DbgOutputInit();
#if (DBG_TRACE_USE_CIRCULAR_QUEUE != 0)
  CircularQueue_Init(&MsgDbgTraceQueue, MsgDbgTraceQueueBuff, DBG_TRACE_MSG_QUEUE_SIZE, 0, CIRCULAR_QUEUE_SPLIT_IF_WRAPPING_FLAG);
#endif 
#endif
  return;
}


#if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 ))
#if defined(__GNUC__)  /* SW4STM32 (GCC) */
/**
 * @brief	_write: override the __write standard lib function to redirect printf to USART.
 * @param	handle output handle (STDIO, STDERR...)
 * @param	buf buffer to write
 * @param	bufsize buffer size
 * @param	...: arguments to be formatted in format string
 * @retval none
 */
size_t _write(int handle, const unsigned char * buf, size_t bufSize)
{
  return ( DbgTraceWrite(handle, buf, bufSize) );
}

#else
/**
 * @brief __write: override the _write standard lib function to redirect printf to USART.
 * @param handle output handle (STDIO, STDERR...)
 * @param buf buffer to write
 * @param bufsize buffer size
 * @param ...: arguments to be formatted in format string
 * @retval none
 */
size_t __write(int handle, const unsigned char * buf, size_t bufSize)
{
  return ( DbgTraceWrite(handle, buf, bufSize) );
}
#endif /* #if defined(__GNUC__)  */

/**
 * @brief Override the standard lib function to redirect printf to USART.
 * @param handle output handle (STDIO, STDERR...)
 * @param buf buffer to write
 * @param bufsize buffer size
 * @retval Number of elements written
 */
size_t DbgTraceWrite(int handle, const unsigned char * buf, size_t bufSize)
{
  size_t chars_written = 0;
  uint8_t* buffer;

  BACKUP_PRIMASK();

  /* Ignore flushes */
  if ( handle == -1 )
  {
    chars_written = ( size_t ) 0;
  }
  /* Only allow stdout/stderr output */
  else if ( ( handle != 1 ) && ( handle != 2 ) )
  {
    chars_written = ( size_t ) - 1;
  }
  /* Parameters OK, call the low-level character output routine */
  else if (bufSize != 0)
  {
    chars_written = bufSize;
    /* If queue emepty and TX free, send directly */
    /* CS Start */

#if (DBG_TRACE_USE_CIRCULAR_QUEUE != 0)
    DISABLE_IRQ();      /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
    buffer=CircularQueue_Add(&MsgDbgTraceQueue,(uint8_t*)buf, bufSize,1);
    if (buffer && DbgTracePeripheralReady)
    {
      DbgTracePeripheralReady = RESET;
      RESTORE_PRIMASK();
      DbgOutputTraces((uint8_t*)buffer, bufSize, DbgTrace_TxCpltCallback);
    }
    else
    {
      RESTORE_PRIMASK();
    }
#else
    DISABLE_IRQ();      /**< Disable all interrupts by setting PRIMASK bit on Cortex*/
    DbgTracePeripheralReady = RESET;
    RESTORE_PRIMASK();

    DbgOutputTraces((uint8_t*)buf, bufSize, DbgTrace_TxCpltCallback);
    while (!DbgTracePeripheralReady);
#endif
    /* CS END */
  }
  return ( chars_written );
}

#if   defined ( __CC_ARM )     /* Keil */

/* For KEIL re-implement our own version of fputc */
int fputc(int ch, FILE *f)
{
  /* temp char avoids endianness issue */
  char tempch = ch;
  /* Write one character to Debug Circular Queue */
  DbgTraceWrite(1U, (const unsigned char *) &tempch, 1);
  return ch;
}

#endif /* #if   defined ( __CC_ARM )  */

#endif /* #if (( CFG_DEBUG_TRACE_FULL != 0 ) || ( CFG_DEBUG_TRACE_LIGHT != 0 )) */

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/