Change in simtrace2[master]: add synchronous UART transmission and use it in exceptions

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Kévin Redon gerrit-no-reply at lists.osmocom.org
Thu Aug 2 15:54:45 UTC 2018


Kévin Redon has uploaded this change for review. ( https://gerrit.osmocom.org/10313


Change subject: add synchronous UART transmission and use it in exceptions
......................................................................

add synchronous UART transmission and use it in exceptions

The default ISR (particularly the HardFault handler) print information,
but this information was not displayed on the console because the UART
IRQ is lower than some default blocking IRQ.
Allowing to set synchronous transfer corrects this.

The underlying Atmel exception library had to be modified to use the
synchronous output, and is now specific to this project since it
includes the uart_console library.
Making UART_PutChar always synchronous when called from an ISR is not
desired because we use TRACE_ macros is some ISR. The synchronous
output must be set explicitly.

Change-Id: I1b4ace5185cf2dc32684934ed12bf6a8682e9bad
---
M firmware/atmel_softpack_libraries/libchip_sam3s/source/exceptions.c
M firmware/libboard/common/include/uart_console.h
M firmware/libboard/common/source/uart_console.c
3 files changed, 74 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/13/10313/1

diff --git a/firmware/atmel_softpack_libraries/libchip_sam3s/source/exceptions.c b/firmware/atmel_softpack_libraries/libchip_sam3s/source/exceptions.c
index 74e0fbd..e1e8306 100644
--- a/firmware/atmel_softpack_libraries/libchip_sam3s/source/exceptions.c
+++ b/firmware/atmel_softpack_libraries/libchip_sam3s/source/exceptions.c
@@ -2,6 +2,7 @@
  *         ATMEL Microcontroller Software Support
  * ----------------------------------------------------------------------------
  * Copyright (c) 2009, Atmel Corporation
+ * Copyright (c) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon at sysmocom.de>
  *
  * All rights reserved.
  *
@@ -42,6 +43,7 @@
  *----------------------------------------------------------------------------*/
 
 #include "chip.h"
+#include "uart_console.h"
 
 /*----------------------------------------------------------------------------
  *        Exported functions
@@ -52,6 +54,7 @@
  */
 void IrqHandlerNotUsed( void )
 {
+    UART_SetSync( true );
     printf("NotUsed\r\n");
     while ( 1 ) ;
 }
@@ -61,6 +64,7 @@
  */
 WEAK void NMI_Handler( void )
 {
+    UART_SetSync( true );
     printf("NMI\r\n");
     while ( 1 ) ;
 }
@@ -81,6 +85,7 @@
 
 void hard_fault_handler_c(struct hardfault_args *args)
 {
+    UART_SetSync( true );
     printf("\r\nHardFault\r\n");
     printf("R0=%08x, R1=%08x, R2=%08x, R3=%08x, R12=%08x\r\n",
 	   args->r0, args->r1, args->r2, args->r3, args->r12);
@@ -158,6 +163,7 @@
  */
 WEAK void MemManage_Handler( void )
 {
+    UART_SetSync( true );
     printf("MemManage\r\n");
     while ( 1 ) ;
 }
@@ -167,6 +173,7 @@
  */
 WEAK void BusFault_Handler( void )
 {
+    UART_SetSync( true );
     printf("BusFault\r\n");
     while ( 1 ) ;
 }
@@ -176,6 +183,7 @@
  */
 WEAK void UsageFault_Handler( void )
 {
+    UART_SetSync( true );
     printf("UsageFault\r\n");
     while ( 1 ) ;
 }
@@ -185,6 +193,7 @@
  */
 WEAK void SVC_Handler( void )
 {
+    UART_SetSync( true );
     printf("SVC\r\n");
     while ( 1 ) ;
 }
@@ -194,6 +203,7 @@
  */
 WEAK void DebugMon_Handler( void )
 {
+    UART_SetSync( true );
     printf("DebugMon\r\n");
     while ( 1 ) ;
 }
@@ -203,6 +213,7 @@
  */
 WEAK void PendSV_Handler( void )
 {
+    UART_SetSync( true );
     printf("PendSV\r\n");
     while ( 1 ) ;
 }
@@ -212,6 +223,7 @@
  */
 WEAK void SysTick_Handler( void )
 {
+    UART_SetSync( true );
     printf("SysTick\r\n");
     while ( 1 ) ;
 }
@@ -221,6 +233,7 @@
  */
 WEAK void SUPC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("SUPC\r\n");
     while ( 1 ) ;
 }
@@ -230,6 +243,7 @@
  */
 WEAK void RSTC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("RSTC\r\n");
     while ( 1 ) ;
 }
@@ -239,6 +253,7 @@
  */
 WEAK void RTC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("RTC\r\n");
     while ( 1 ) ;
 }
@@ -248,6 +263,7 @@
  */
 WEAK void RTT_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("RTT\r\n");
     while ( 1 ) ;
 }
@@ -257,6 +273,7 @@
  */
 WEAK void WDT_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("WDT\r\n");
     while ( 1 ) ;
 }
@@ -266,6 +283,7 @@
  */
 WEAK void PMC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("PMC\r\n");
     while ( 1 ) ;
 }
@@ -275,6 +293,7 @@
  */
 WEAK void EEFC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("EEFC\r\n");
     while ( 1 ) ;
 }
@@ -284,6 +303,7 @@
  */
 WEAK void UART0_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("UART0\r\n");
     while ( 1 ) ;
 }
@@ -293,6 +313,7 @@
  */
 WEAK void UART1_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("UART1\r\n");
     while ( 1 ) ;
 }
@@ -302,6 +323,7 @@
  */
 WEAK void SMC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("SMC\r\n");
     while ( 1 ) ;
 }
@@ -311,6 +333,7 @@
  */
 WEAK void PIOA_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("PIOA\r\n");
     while ( 1 ) ;
 }
@@ -320,6 +343,7 @@
  */
 WEAK void PIOB_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("PIOB\r\n");
     while ( 1 ) ;
 }
@@ -329,6 +353,7 @@
  */
 WEAK void PIOC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("PIOC\r\n");
     while ( 1 ) ;
 }
@@ -338,6 +363,7 @@
  */
 WEAK void USART0_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("USART0\r\n");
     while ( 1 ) ;
 }
@@ -347,6 +373,7 @@
  */
 WEAK void USART1_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("USART1\r\n");
     while ( 1 ) ;
 }
@@ -356,6 +383,7 @@
  */
 WEAK void MCI_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("MCI\r\n");
     while ( 1 ) ;
 }
@@ -365,6 +393,7 @@
  */
 WEAK void TWI0_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TWI0\r\n");
     while ( 1 ) ;
 }
@@ -374,6 +403,7 @@
  */
 WEAK void TWI1_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TWI1\r\n");
     while ( 1 ) ;
 }
@@ -383,6 +413,7 @@
  */
 WEAK void SPI_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("SPI\r\n");
     while ( 1 ) ;
 }
@@ -392,6 +423,7 @@
  */
 WEAK void SSC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("SSC\r\n");
     while ( 1 ) ;
 }
@@ -401,6 +433,7 @@
  */
 WEAK void TC0_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC0\r\n");
     while ( 1 ) ;
 }
@@ -410,6 +443,7 @@
  */
 WEAK void TC1_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC1\r\n");
     while ( 1 ) ;
 }
@@ -419,6 +453,7 @@
  */
 WEAK void TC2_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC2\r\n");
     while ( 1 ) ;
 }
@@ -428,6 +463,7 @@
  */
 WEAK void TC3_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC3\r\n");
     while ( 1 ) ;
 }
@@ -437,6 +473,7 @@
  */
 WEAK void TC4_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC4\r\n");
     while ( 1 ) ;
 }
@@ -446,6 +483,7 @@
  */
 WEAK void TC5_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("TC5\r\n");
     while ( 1 ) ;
 }
@@ -455,6 +493,7 @@
  */
 WEAK void ADC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("ADC\r\n");
     while ( 1 ) ;
 }
@@ -464,6 +503,7 @@
  */
 WEAK void DAC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("DAC\r\n");
     while ( 1 ) ;
 }
@@ -473,6 +513,7 @@
  */
 WEAK void PWM_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("PWM\r\n");
     while ( 1 ) ;
 }
@@ -482,6 +523,7 @@
  */
 WEAK void CRCCU_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("CRCCU\r\n");
     while ( 1 ) ;
 }
@@ -491,6 +533,7 @@
  */
 WEAK void ACC_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("ACC\r\n");
     while ( 1 ) ;
 }
@@ -500,6 +543,7 @@
  */
 WEAK void USBD_IrqHandler( void )
 {
+    UART_SetSync( true );
     printf("USBD\r\n");
     while ( 1 ) ;
 }
diff --git a/firmware/libboard/common/include/uart_console.h b/firmware/libboard/common/include/uart_console.h
index c0ee3a7..40c9141 100644
--- a/firmware/libboard/common/include/uart_console.h
+++ b/firmware/libboard/common/include/uart_console.h
@@ -33,12 +33,12 @@
 #include <stdint.h>
 
 extern void UART_Configure( uint32_t dwBaudrate, uint32_t dwMasterClock ) ;
-extern void UART_Exit(void) ;
+extern void UART_Exit( void ) ;
 extern void UART_PutChar( uint8_t uc ) ;
+extern void UART_SetSync( _Bool sync ) ;
 extern uint32_t UART_GetChar( void ) ;
 extern uint32_t UART_IsRxReady( void ) ;
 
-
 extern void UART_DumpFrame( uint8_t* pucFrame, uint32_t dwSize ) ;
 extern void UART_DumpMemory( uint8_t* pucBuffer, uint32_t dwSize, uint32_t dwAddress ) ;
 extern uint32_t UART_GetInteger( uint32_t* pdwValue ) ;
diff --git a/firmware/libboard/common/source/uart_console.c b/firmware/libboard/common/source/uart_console.c
index 3ce7331..e936378 100644
--- a/firmware/libboard/common/source/uart_console.c
+++ b/firmware/libboard/common/source/uart_console.c
@@ -2,6 +2,7 @@
  *         ATMEL Microcontroller Software Support
  * ----------------------------------------------------------------------------
  * Copyright (c) 2009, Atmel Corporation
+ * Copyright (c) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon at sysmocom.de>
  *
  * All rights reserved.
  *
@@ -57,6 +58,8 @@
 static uint8_t _ucIsConsoleInitialized=0;
 /** Ring buffer to queue data to be sent */
 static ringbuf uart_tx_buffer;
+/** If UART_PutChar is synchronous (i.e. using blocking polling until character transfer is complete) or asynchronous (i.e. using a buffer and interrupts to complete the transfer). */
+static bool uart_tx_sync = false;
 
 /**
  * \brief Configures an USART peripheral with the specified parameters.
@@ -119,6 +122,17 @@
 	NVIC_DisableIRQ(CONSOLE_IRQ);
 }
 
+/**
+ * \brief Set if UART_PutChar is synchronous (i.e. using blocking polling until character transfer is complete) or asynchronous (i.e. using a buffer and interrupts to complete the transfer).
+ *
+ * \note Per default the transfer is asynchronous.
+ * \param sync True for synchronous, false for asynchronous.
+ */
+void UART_SetSync( _Bool sync )
+{
+	uart_tx_sync = sync;
+}
+
 /** Interrupt Service routine to transmit queued data */
 void CONSOLE_ISR(void)
 {
@@ -148,12 +162,20 @@
 		UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
 	}
 
-	/* Only store input if buffer is not full, else drop it */
-	if (!rbuf_is_full(&uart_tx_buffer)) {
-		rbuf_write(&uart_tx_buffer, c);
-		if (!(pUart->UART_IMR & UART_IMR_TXRDY)) {
-			pUart->UART_IER = UART_IER_TXRDY;
-			CONSOLE_ISR();
+	/* Send depending on synchronous mode set */
+	if (uart_tx_sync) { /* Send the data synchronously (using blocking polling and waiting for the transfer to complete) */
+		while (!(pUart->UART_SR & UART_SR_TXRDY)); /* Wait for transfer buffer to be empty */
+		pUart->UART_THR = c; /* Send data to UART peripheral */
+		while (!(pUart->UART_SR & UART_SR_TXRDY)); /* Wait for transfer buffer to transferred to shift register */
+		while (!(pUart->UART_SR & UART_SR_TXEMPTY)); /* Wait for transfer shift register to be empty (i.e. transfer is complete) */
+	} else { /* Send the data asynchronously (using a buffer and interrupts) */
+		/* Only store input if buffer is not full, else drop it */
+		if (!rbuf_is_full(&uart_tx_buffer)) {
+			rbuf_write(&uart_tx_buffer, c);
+			if (!(pUart->UART_IMR & UART_IMR_TXRDY)) {
+				pUart->UART_IER = UART_IER_TXRDY;
+				CONSOLE_ISR();
+			}
 		}
 	}
 }

-- 
To view, visit https://gerrit.osmocom.org/10313
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I1b4ace5185cf2dc32684934ed12bf6a8682e9bad
Gerrit-Change-Number: 10313
Gerrit-PatchSet: 1
Gerrit-Owner: Kévin Redon <kredon at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180802/87c69ea8/attachment.htm>


More information about the gerrit-log mailing list