Skip to content

Commit 3c6c780

Browse files
authored
Fixes in System.IO.Port (#2434)
***NO_CI***
1 parent 464675e commit 3c6c780

File tree

2 files changed

+63
-49
lines changed

2 files changed

+63
-49
lines changed

targets/AzureRTOS/ST/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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] == ',')

targets/AzureRTOS/ST/_nanoCLR/System.IO.Ports/sys_io_ser_native_target.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <target_system_io_ports_config.h>
1010
#include <sys_io_ser_native.h>
1111
#include <hal.h>
12-
#include <nanoHAL.h>
1312

1413
// struct representing the UART
1514
typedef struct
@@ -61,7 +60,7 @@ extern NF_PAL_UART Uart8_PAL;
6160
#endif
6261

6362
// the following macro defines a function that configures the GPIO pins for a STM32 UART/USART
64-
// it gets called in the Windows_Devices_SerialCommunication_SerialDevice::NativeConfig function
63+
// it gets called in the System_IO_Ports_SerialPort::NativeConfig function
6564
// this is required because the UART/USART peripherals can use multiple GPIO configuration combinations
6665
#define UART_CONFIG_PINS(num, gpio_port_tx, gpio_port_rx, tx_pin, rx_pin, alternate_function) \
6766
void ConfigPins_UART##num() \
@@ -72,7 +71,7 @@ extern NF_PAL_UART Uart8_PAL;
7271

7372
///////////////////////////////////////////////////////////////////////////////////////////////////
7473
// when a UART/USART is defined the declarations below will have the real function/configuration
75-
// in the target folder @ target_windows_devices_serialcommunication_config.cpp
74+
// in the target folder @ target_system_io_ports_config.cpp
7675
///////////////////////////////////////////////////////////////////////////////////////////////////
7776
void ConfigPins_UART1();
7877
void ConfigPins_UART2();
@@ -84,7 +83,7 @@ void ConfigPins_UART7();
8483
void ConfigPins_UART8();
8584

8685
// the following macro defines a function that initializes an UART struct
87-
// it gets called in the Windows_Devices_SerialCommunication_SerialDevice::NativeInit function
86+
// it gets called in the system_io_ports_SerialDevice::NativeInit function
8887

8988
#if defined(STM32F7XX) || defined(STM32F0XX)
9089

@@ -111,7 +110,7 @@ void ConfigPins_UART8();
111110

112111
#else
113112

114-
// all other STM32F use UART driver v1 which has a different UARTConfig struct
113+
// all other STM32F series use UART driver v1 which has a different UARTConfig struct
115114
#define UART_INIT(num) \
116115
void Init_UART##num() \
117116
{ \
@@ -133,7 +132,7 @@ void ConfigPins_UART8();
133132
#endif
134133

135134
// when a UART/USART is defined the declarations below will have the real function/configuration
136-
// in the target folder @ target_windows_devices_serialcommunication_config.cpp
135+
// in the target folder @ target_system_io_ports_config.cpp
137136
void Init_UART1();
138137
void Init_UART2();
139138
void Init_UART3();
@@ -144,7 +143,7 @@ void Init_UART7();
144143
void Init_UART8();
145144

146145
// the following macro defines a function that un initializes an UART struct
147-
// it gets called in the Windows_Devices_SerialCommunication_SerialDevice::NativeDispose function
146+
// it gets called in the System_IO_Ports_SerialPort::NativeDispose function
148147
#define UART_UNINIT(num) \
149148
void UnInit_UART##num() \
150149
{ \
@@ -157,7 +156,7 @@ void Init_UART8();
157156
}
158157

159158
// when a UART/USART is defined the declarations below will have the real function/configuration
160-
// in the target folder @ target_windows_devices_serialcommunication_config.cpp
159+
// in the target folder @ target_system_io_ports_config.cpp
161160
void UnInit_UART1();
162161
void UnInit_UART2();
163162
void UnInit_UART3();

0 commit comments

Comments
 (0)