A31G11x F/W Packages  2.5.0
ABOV Cortex-M0+ Core based MCUs Integrated Driver
A31G11x_hal_debug_frmwrk.c
Go to the documentation of this file.
1 /***************************************************************************//****************************************************************************/
34 
35 /* Includes ----------------------------------------------------------------- */
36 //******************************************************************************
37 // Include
38 //******************************************************************************
39 
40 #include <stdarg.h>
41 #include <stdio.h>
43 #include "A31G11x_hal_pcu.h"
44 
45 //******************************************************************************
46 // Constant
47 //******************************************************************************
48 
49 #define ASCII_BACKSPACE (0x08)
50 #define ASCII_LINEFEED (0x0A)
51 #define ASCII_CARRIAGE_RETURN (0x0D)
52 
53 #ifdef _DEBUG_MSG
54 
55 //******************************************************************************
56 // Variable
57 //******************************************************************************
58 
59 void ( *_db_msg )( UARTn_Type* UARTx, const void* s );
60 void ( *_db_msg_ )( UARTn_Type* UARTx, const void* s );
61 void ( *_db_char )( UARTn_Type* UARTx, uint8_t ch );
62 void ( *_db_dec )( UARTn_Type* UARTx, uint8_t decn );
63 void ( *_db_dec_16 )( UARTn_Type* UARTx, uint16_t decn );
64 void ( *_db_dec_32 )( UARTn_Type* UARTx, uint32_t decn );
65 void ( *_db_hex )( UARTn_Type* UARTx, uint8_t hexn );
66 void ( *_db_hex_16 )( UARTn_Type* UARTx, uint16_t hexn );
67 void ( *_db_hex_32 )( UARTn_Type* UARTx, uint32_t hexn );
68 uint8_t ( *_db_get_char )( UARTn_Type* UARTx );
69 uint8_t ( *_db_get_ch )( UARTn_Type* UARTx, uint8_t* ch );
70 
71 /* Public Functions --------------------------------------------------------- */
72 //******************************************************************************
73 // Function
74 //******************************************************************************
75 
76 #if 0
77 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
86 int fputc( int ch, FILE* f )
87 {
88  while( HAL_UART_CheckBusy( ( UARTn_Type* )DEBUG_UART_PORT ) );
89  HAL_UART_TransmitByte( ( UARTn_Type* )DEBUG_UART_PORT, ch );
90 
91  return( ch );
92 }
93 #endif
94 
95 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
104 void UARTPutChar( UARTn_Type* UARTx, uint8_t ch )
105 {
106  HAL_UART_Transmit( UARTx, &ch, 1, BLOCKING );
107 }
108 
109 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
116 uint8_t UARTGetChar( UARTn_Type* UARTx )
117 {
118  uint8_t tmp = 0;
119 
120  HAL_UART_Receive( UARTx, &tmp, 1, BLOCKING );
121 
122  return( tmp );
123 }
124 
125 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
134 uint8_t UARTGetCh( UARTn_Type* UARTx, uint8_t* ch )
135 {
136  if( !( UARTx->LSR & UARTn_LSR_RDR ) )
137  {
138  *ch = 0;
139  return( 0 );
140  }
141  else
142  {
143  *ch = HAL_UART_ReceiveByte( UARTx );
144  return( 1 );
145  }
146 }
147 
148 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
157 void UARTPuts( UARTn_Type* UARTx, const void* str )
158 {
159  uint8_t* s = ( uint8_t* )str;
160 
161  while( *s )
162  {
163  UARTPutChar( UARTx, *s++ );
164  }
165 }
166 
167 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
176 void UARTPuts_( UARTn_Type* UARTx, const void* str )
177 {
178  UARTPuts( UARTx, str );
179  UARTPuts( UARTx, "\n\r" );
180 }
181 
182 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
191 void UARTPutDec( UARTn_Type* UARTx, uint8_t decnum )
192 {
193  uint8_t c1 = decnum % 10;
194  uint8_t c2 = ( decnum / 10 ) % 10;
195  uint8_t c3 = ( decnum / 100 ) % 10;
196 
197  UARTPutChar( UARTx, '0' + c3 );
198  UARTPutChar( UARTx, '0' + c2 );
199  UARTPutChar( UARTx, '0' + c1 );
200 }
201 
202 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
211 void UARTPutDec16( UARTn_Type* UARTx, uint16_t decnum )
212 {
213  uint8_t c1 = decnum % 10;
214  uint8_t c2 = ( decnum / 10 ) % 10;
215  uint8_t c3 = ( decnum / 100 ) % 10;
216  uint8_t c4 = ( decnum / 1000 ) % 10;
217  uint8_t c5 = ( decnum / 10000 ) % 10;
218 
219  UARTPutChar( UARTx, '0' + c5 );
220  UARTPutChar( UARTx, '0' + c4 );
221  UARTPutChar( UARTx, '0' + c3 );
222  UARTPutChar( UARTx, '0' + c2 );
223  UARTPutChar( UARTx, '0' + c1 );
224 }
225 
226 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
235 void UARTPutDec32( UARTn_Type* UARTx, uint32_t decnum )
236 {
237  uint8_t c1 = decnum % 10;
238  uint8_t c2 = ( decnum / 10 ) % 10;
239  uint8_t c3 = ( decnum / 100 ) % 10;
240  uint8_t c4 = ( decnum / 1000 ) % 10;
241  uint8_t c5 = ( decnum / 10000 ) % 10;
242  uint8_t c6 = ( decnum / 100000 ) % 10;
243  uint8_t c7 = ( decnum / 1000000 ) % 10;
244  uint8_t c8 = ( decnum / 10000000 ) % 10;
245  uint8_t c9 = ( decnum / 100000000 ) % 10;
246  uint8_t c10 = ( decnum / 1000000000 ) % 10;
247 
248  UARTPutChar( UARTx, '0' + c10 );
249  UARTPutChar( UARTx, '0' + c9 );
250  UARTPutChar( UARTx, '0' + c8 );
251  UARTPutChar( UARTx, '0' + c7 );
252  UARTPutChar( UARTx, '0' + c6 );
253  UARTPutChar( UARTx, '0' + c5 );
254  UARTPutChar( UARTx, '0' + c4 );
255  UARTPutChar( UARTx, '0' + c3 );
256  UARTPutChar( UARTx, '0' + c2 );
257  UARTPutChar( UARTx, '0' + c1 );
258 }
259 
260 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
269 void UARTPutHex( UARTn_Type* UARTx, uint8_t hexnum )
270 {
271  uint8_t nibble, i;
272 
273  i = 1;
274  do
275  {
276  nibble = ( hexnum >> ( 4 * i ) ) & 0x0F;
277  UARTPutChar( UARTx, ( nibble > 9 ) ? ( 'A' + nibble - 10 ) : ( '0' + nibble ) );
278  } while( i-- );
279 }
280 
281 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
290 void UARTPutHex16( UARTn_Type* UARTx, uint16_t hexnum )
291 {
292  uint8_t nibble, i;
293 
294  i = 3;
295  do
296  {
297  nibble = ( hexnum >> ( 4 * i ) ) & 0x0F;
298  UARTPutChar( UARTx, ( nibble > 9 ) ? ( 'A' + nibble - 10 ) : ( '0' + nibble ) );
299  } while( i-- );
300 }
301 
302 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
311 void UARTPutHex32( UARTn_Type* UARTx, uint32_t hexnum )
312 {
313  uint8_t nibble, i;
314 
315  i = 7;
316  do
317  {
318  nibble = ( hexnum >> ( 4 * i ) ) & 0x0F;
319  UARTPutChar( UARTx, ( nibble > 9 ) ? ( 'A' + nibble - 10 ) : ( '0' + nibble ) );
320  } while( i-- );
321 }
322 
323 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
329 void cprintf( const char* format, ... )
330 {
331  char buffer[512 + 1];
332  va_list vArgs;
333 
334  va_start( vArgs, format );
335  vsprintf( ( char* )buffer, ( char const* )format, vArgs );
336  va_end( vArgs );
337 
338  _DBG( buffer );
339 }
340 
341 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
346 void debug_frmwrk_init( void )
347 {
348  UARTn_CFG_Type UARTn_Config;
349 
350 #if (USED_UART_DEBUG_PORT == 0)
351 
352  // Initialize UART0 pin connect
353  HAL_GPIO_ConfigOutput( ( Pn_Type* )PB, 5, ALTERN_FUNC ); // PB5 SWDIO 0: SEG20 1: RXD0 2: SWDIO 3: ---- 4: ----
354  HAL_GPIO_ConfigFunction( ( Pn_Type* )PB, 5, AFSRx_AF1 );
355 
356  HAL_GPIO_ConfigOutput( ( Pn_Type* )PB, 4, ALTERN_FUNC ); // PB4 SWCLK 0: SEG21 1: TXD0 2: SWCLK 3: ---- 4: ----
357  HAL_GPIO_ConfigFunction( ( Pn_Type* )PB, 4, AFSRx_AF1 );
358 
359 #elif (USED_UART_DEBUG_PORT == 1)
360 
361  // Initialize UART1 pin connect
362  HAL_GPIO_ConfigOutput( ( Pn_Type* )PB, 7, ALTERN_FUNC ); // PB7 0: SEG18 1: RXD1 2: ---- 3: ---- 4: ----
363  HAL_GPIO_ConfigFunction( ( Pn_Type* )PB, 7, AFSRx_AF1 );
364 
365  HAL_GPIO_ConfigOutput( ( Pn_Type* )PB, 6, ALTERN_FUNC ); // PB6 0: SEG19 1: TXD1 2: ---- 3: ---- 4: ----
366  HAL_GPIO_ConfigFunction( ( Pn_Type* )PB, 6, AFSRx_AF1 );
367 
368 #endif
369 
370  /* Initialize UART Configuration parameter structure to default state:
371  * Baudrate = 38400bps
372  * 8 data bit
373  * no parity
374  * 1 stop bit
375  */
376  HAL_UART_ConfigStructInit( &UARTn_Config );
377  UARTn_Config.Baudrate = 38400;
378 
379  // Initialize DEBUG_UART_PORT peripheral with given to corresponding parameter
380  HAL_UART_Init( ( UARTn_Type* )DEBUG_UART_PORT, &UARTn_Config );
381 
382  _db_msg = UARTPuts;
393 }
394 
395 /*-------------------------------------------------------------------------*//*-------------------------------------------------------------------------*/
400 uint8_t getstring( void )
401 {
402  uint8_t ch;
403 
404  ch = UARTGetChar( ( UARTn_Type* )UART1 );
405 
406  if( ch > 0 )
407  {
408  if( InCount < 80 )
409  {
410  if( InCount == 0 && ch < 0x20 )
411  {
412  InData[0] = 0;
413  return ch;
414  }
415 
416  UARTPutChar( ( UARTn_Type* )UART1, ch );
417  if( ch == ASCII_BACKSPACE )
418  {
419  InCount--;
420  return ch;
421  }
422 
423  if( ch == ASCII_CARRIAGE_RETURN )
424  {
425  InData[InCount] = 0;
426  InFlag = 1;
427  return ch;
428  }
429 
430  InData[InCount++] = ch;
431  }
432  }
433 
434  return 0;
435 }
436 
437 #endif /* _DEBUG_MSG */
438 
void(* _db_hex_16)(UARTn_Type *UARTx, uint16_t hexn)
Contains all macro definitions and function prototypes support for pcu firmware library on A31G11x.
void(* _db_dec_32)(UARTn_Type *UARTx, uint32_t decn)
void UARTPutDec16(UARTn_Type *UARTx, uint16_t decnum)
Puts a decimal number to UART port.
void UARTPutDec(UARTn_Type *UARTx, uint8_t decnum)
Puts a decimal number to UART port.
Contains all macro definitions and function prototypes support for debug_frmwrk firmware library on A...
void(* _db_dec_16)(UARTn_Type *UARTx, uint16_t decn)
HAL_Status_Type HAL_UART_ConfigStructInit(UARTn_CFG_Type *UARTn_Config)
Fills each UARTn_Config member with its default value:
uint8_t UARTGetCh(UARTn_Type *UARTx, uint8_t *ch)
Get a character to UART port.
char c8
void(* _db_msg_)(UARTn_Type *UARTx, const void *s)
void(* _db_hex)(UARTn_Type *UARTx, uint8_t hexn)
void HAL_GPIO_ConfigOutput(Pn_Type *Px, uint8_t pin_no, PCU_PORT_MODE dir_type)
Configure pin mode.
int InFlag
void(* _db_msg)(UARTn_Type *UARTx, const void *s)
void HAL_GPIO_ConfigFunction(Pn_Type *Px, uint8_t pin_no, uint32_t func)
Configure pin function.
char InData[80]
void UARTPutHex(UARTn_Type *UARTx, uint8_t hexnum)
Puts a hex number to UART port.
void UARTPutHex32(UARTn_Type *UARTx, uint32_t hexnum)
Puts a hex number to UART port.
void debug_frmwrk_init(void)
Initializes Debug Framework through initializing UARTn.
void cprintf(const char *format,...)
print function that supports format as same as printf() function of <stdio.h> library
void UARTPutDec32(UARTn_Type *UARTx, uint32_t decnum)
Puts a decimal number to UART port.
FlagStatus HAL_UART_CheckBusy(UARTn_Type *UARTx)
Check whether if UART is busy or not.
uint8_t HAL_UART_ReceiveByte(UARTn_Type *UARTx)
Receive a single data from UART peripheral.
uint32_t HAL_UART_Transmit(UARTn_Type *UARTx, uint8_t *txbuf, uint32_t buflen, TRANSFER_BLOCK_Type flag)
Send a block of data via UART peripheral.
void UARTPuts(UARTn_Type *UARTx, const void *str)
Puts a string to UART port.
uint32_t HAL_UART_Receive(UARTn_Type *UARTx, uint8_t *rxbuf, uint32_t buflen, TRANSFER_BLOCK_Type flag)
Receive a block of data via UART peripheral.
void(* _db_dec)(UARTn_Type *UARTx, uint8_t decn)
uint8_t(* _db_get_ch)(UARTn_Type *UARTx, uint8_t *ch)
uint8_t getstring(void)
Get a character to UART port.
void UARTPuts_(UARTn_Type *UARTx, const void *str)
Puts a string to UART port and print new line.
uint8_t UARTGetChar(UARTn_Type *UARTx)
Get a character to UART port.
int InCount
void(* _db_char)(UARTn_Type *UARTx, uint8_t ch)
HAL_Status_Type HAL_UART_TransmitByte(UARTn_Type *UARTx, uint8_t Data)
Transmit a single data through UART peripheral.
int fputc(int ch, FILE *f)
Puts a character to file.
void UARTPutHex16(UARTn_Type *UARTx, uint16_t hexnum)
Puts a hex number to UART port.
void UARTPutChar(UARTn_Type *UARTx, uint8_t ch)
Puts a character to UART port.
HAL_Status_Type HAL_UART_Init(UARTn_Type *UARTx, UARTn_CFG_Type *UARTn_Config)
Initialize the UARTn peripheral with the specified parameters.
void(* _db_hex_32)(UARTn_Type *UARTx, uint32_t hexn)
uint8_t(* _db_get_char)(UARTn_Type *UARTx)