@@ -240,18 +240,20 @@ static void RxChar(UARTDriver *uartp, uint16_t c)
240240 }
241241 else
242242 {
243- // no read operation ongoing, so fire an event
244-
245- // post a managed event with the port index and event code (check if this is the watch char or just another
246- // another)
247- // TODO: check if callbacks are registered so this is called only if there is anyone listening otherwise don't
248- // bother for that to happen ChibiOS callback has to accept arg which we would passing the GpioPin
249- // CLR_RT_HeapBlock (see Gpio handler) check http://www.chibios.com/forum/viewtopic.php?f=36&t=4798
250- PostManagedEvent (
251- EVENT_SERIAL,
252- 0 ,
253- portIndex,
254- (c == palUart->WatchChar ) ? SerialData_WatchChar : SerialData_Chars);
243+ // no read operation ongoing, so fire an event, if the available bytes are above the threshold
244+ if (palUart->RxRingBuffer .Length () >= palUart->ReceivedBytesThreshold )
245+ {
246+ // post a managed event with the port index and event code (check if this is the watch char or just another
247+ // another)
248+ // TODO: check if callbacks are registered so this is called only if there is anyone listening otherwise
249+ // don't bother for that to happen ChibiOS callback has to accept arg which we would passing the GpioPin
250+ // CLR_RT_HeapBlock (see Gpio handler) check http://www.chibios.com/forum/viewtopic.php?f=36&t=4798
251+ PostManagedEvent (
252+ EVENT_SERIAL,
253+ 0 ,
254+ portIndex,
255+ (c == palUart->WatchChar ) ? SerialData_WatchChar : SerialData_Chars);
256+ }
255257 }
256258
257259 NATIVE_INTERRUPT_END
@@ -414,7 +416,7 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::Read___I4__SZARRAY
414416 }
415417 }
416418
417- // get a the pointer to the array by using the first element of the array
419+ // get a the pointer to the array by using the offset
418420 data = dataBuffer->GetElement (offset);
419421
420422 // Choose the driver for this SerialDevice
@@ -601,18 +603,20 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::ReadLine___STRING(
601603 // got one!
602604 eventResult = false ;
603605 }
606+ else
607+ {
608+ // get new line from field
609+ newLine = pThis[FIELD___newLine].RecoverString ();
610+ newLineLength = hal_strlen_s (newLine);
611+ // need to subtract one because we are 0 indexed
612+ newLineLength--;
604613
605- // get new line from field
606- newLine = pThis[FIELD___newLine].RecoverString ();
607- newLineLength = hal_strlen_s (newLine);
608- // need to subtract one because we are 0 indexed
609- newLineLength--;
610-
611- // set new line char as the last one in the string
612- // only if this one is found it will have a chance of the others being there
613- palUart->NewLineChar = newLine[newLineLength];
614+ // set new line char as the last one in the string
615+ // only if this one is found it will have a chance of the others being there
616+ palUart->NewLineChar = newLine[newLineLength];
614617
615- stack.m_customState = 2 ;
618+ stack.m_customState = 2 ;
619+ }
616620 }
617621
618622 while (eventResult)
@@ -713,27 +717,27 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::Write___VOID__SZAR
713717 NANOCLR_SET_AND_LEAVE (CLR_E_INVALID_PARAMETER);
714718 }
715719
716- // get a the pointer to the array by using the first element of the array
720+ // get a the pointer to the array by using the offset
717721 data = dataBuffer->GetElement (offset);
718722
719723 // push onto the eval stack how many bytes are being pushed to the UART
720- stack.PushValueI4 (length - offset );
724+ stack.PushValueI4 (count );
721725
722726 // flush DMA buffer to ensure cache coherency
723727 // (only required for Cortex-M7)
724- cacheBufferFlush (data, length - offset );
728+ cacheBufferFlush (data, count );
725729
726730 // store pointer
727731 palUart->TxBuffer = data;
728732
729733 // set TX ongoing count
730- palUart->TxOngoingCount = length - offset ;
734+ palUart->TxOngoingCount = count ;
731735
732736 // because the UART can be accessed from several threads need to get exclusive access to it
733737 uartAcquireBus (palUart->UartDriver );
734738
735739 // start sending data (DMA will read from the ring buffer)
736- uartStartSend (palUart->UartDriver , length - offset , (uint8_t *)data);
740+ uartStartSend (palUart->UartDriver , count , (uint8_t *)data);
737741
738742 // bump custom state
739743 stack.m_customState = 2 ;
@@ -752,7 +756,7 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::Write___VOID__SZAR
752756 {
753757 // event occurred
754758 // get from the eval stack how many bytes were buffered to Tx
755- length = stack.m_evalStack [1 ].NumericByRef ().s4 ;
759+ count = stack.m_evalStack [1 ].NumericByRef ().s4 ;
756760
757761 // done here
758762 break ;
@@ -763,13 +767,13 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::Write___VOID__SZAR
763767 }
764768 }
765769
766- // pop "length " heap block from stack
770+ // pop "count " heap block from stack
767771 stack.PopValue ();
768772
769773 // pop "hbTimeout" heap block from stack
770774 stack.PopValue ();
771775
772- stack.SetResult_U4 (length );
776+ stack.SetResult_U4 (count );
773777
774778 // null pointers and vars
775779 pThis = NULL ;
@@ -852,6 +856,7 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeInit___VOID(
852856
853857 NF_PAL_UART *palUart;
854858 int32_t bufferSize;
859+ uint8_t watchChar;
855860
856861 // get a pointer to the managed object instance and check that it's not NULL
857862 CLR_RT_HeapBlock *pThis = stack.This ();
@@ -940,6 +945,15 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeInit___VOID(
940945 palUart->Uart_cfg .rxchar_cb = RxChar;
941946 // palUart->Uart_cfg.rxend_cb = RxEnd;
942947
948+ // get watch character
949+ watchChar = pThis[FIELD___watchChar].NumericByRef ().u1 ;
950+
951+ // set watch char, if set
952+ if (watchChar != 0 )
953+ {
954+ palUart->WatchChar = watchChar;
955+ }
956+
943957 // call the configure
944958 return NativeConfig___VOID (stack);
945959
@@ -1105,7 +1119,7 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeWriteString_
11051119
11061120 bool isNewAllocation = false ;
11071121 char *buffer = NULL ;
1108- uint32_t bufferLength = 0 ;
1122+ uint32_t bufferLength;
11091123 int32_t length = 0 ;
11101124
11111125 // get a pointer to the managed object instance and check that it's not NULL
@@ -1261,32 +1275,33 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::GetDeviceSelector_
12611275
12621276 // declare the device selector string whose max size is "COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8," + terminator
12631277 // and init with the terminator
1264- char deviceSelectorString[40 + 1 ] = { 0 };
1278+ static char deviceSelectorString[] =
12651279
12661280#if defined(NF_SERIAL_COMM_STM32_UART_USE_USART1) && (NF_SERIAL_COMM_STM32_UART_USE_USART1 == TRUE)
1267- strcat (deviceSelectorString, " COM1," );
1281+ " COM1,"
12681282#endif
12691283#if defined(NF_SERIAL_COMM_STM32_UART_USE_USART2) && (NF_SERIAL_COMM_STM32_UART_USE_USART2 == TRUE)
1270- strcat (deviceSelectorString, " COM2," );
1284+ " COM2,"
12711285#endif
12721286#if defined(NF_SERIAL_COMM_STM32_UART_USE_USART3) && (NF_SERIAL_COMM_STM32_UART_USE_USART3 == TRUE)
1273- strcat (deviceSelectorString, " COM3," );
1287+ " COM3,"
12741288#endif
12751289#if defined(NF_SERIAL_COMM_STM32_UART_USE_UART4) && (NF_SERIAL_COMM_STM32_UART_USE_UART4 == TRUE)
1276- strcat (deviceSelectorString, " COM4," );
1290+ " COM4,"
12771291#endif
12781292#if defined(NF_SERIAL_COMM_STM32_UART_USE_UART5) && (NF_SERIAL_COMM_STM32_UART_USE_UART5 == TRUE)
1279- strcat (deviceSelectorString, " COM5," );
1293+ " COM5,"
12801294#endif
12811295#if defined(NF_SERIAL_COMM_STM32_UART_USE_USART6) && (NF_SERIAL_COMM_STM32_UART_USE_USART6 == TRUE)
1282- strcat (deviceSelectorString, " COM6," );
1296+ " COM6,"
12831297#endif
12841298#if defined(NF_SERIAL_COMM_STM32_UART_USE_UART7) && (NF_SERIAL_COMM_STM32_UART_USE_UART7 == TRUE)
1285- strcat (deviceSelectorString, " COM7," );
1299+ " COM7,"
12861300#endif
12871301#if defined(NF_SERIAL_COMM_STM32_UART_USE_UART8) && (NF_SERIAL_COMM_STM32_UART_USE_UART8 == TRUE)
1288- strcat (deviceSelectorString, " COM8," );
1302+ " COM8,"
12891303#endif
1304+ ;
12901305
12911306 // replace the last comma with a terminator
12921307 if (deviceSelectorString[hal_strlen_s (deviceSelectorString) - 1 ] == ' ,' )
0 commit comments