Skip to content

Commit

Permalink
Add managed event on serial communication
Browse files Browse the repository at this point in the history
- Add new PAL event EVENT_SERIAL
- Update rx callback to fire managed event or imidiate return on detection of watch char
- Update declaration of native assembly supporting nanoframework/Windows.Devices.SerialCommunication#25

** WIP for ESP32 implementation **

Signed-off-by: José Simões <[email protected]>
  • Loading branch information
josesimoes committed Jun 26, 2018
1 parent 6590c3e commit fe0bbc1
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/PAL/Include/nanoPAL_Events.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define EVENT_UNKNOWN 0
#define EVENT_CUSTOM 10
#define EVENT_GPIO 20
#define EVENT_SERIAL 30

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ static const CLR_RT_MethodHandler method_lookup[] =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeDispose___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeInit___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeConfig___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeWrite___VOID__SZARRAY_U1,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeStore___U4,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeRead___U4__SZARRAY_U1__I4__I4,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeSetWatchChar___VOID,
NULL,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::GetDeviceSelector___STATIC__STRING,
NULL,
Expand All @@ -53,12 +65,19 @@ static const CLR_RT_MethodHandler method_lookup[] =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};

const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_Windows_Devices_SerialCommunication =
{
"Windows.Devices.SerialCommunication",
0x5E530ECD,
0x2B84D4FF,
method_lookup,
{ 1, 0, 0, 0 }
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,29 @@ struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_PinChan

};

struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDataEvent
{
static const int FIELD__SerialDeviceIndex = 3;
static const int FIELD__Event = 4;


//--//

};

struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDataReceivedEventArgs
{
static const int FIELD___data = 1;


//--//

};

struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice
{
static const int FIELD_STATIC__s_eventListener = 0;

static const int FIELD___disposed = 1;
static const int FIELD___syncLock = 2;
static const int FIELD___opened = 3;
Expand All @@ -48,13 +69,16 @@ struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialD
static const int FIELD___parity = 13;
static const int FIELD___stopBits = 14;
static const int FIELD___bytesReceived = 15;
static const int FIELD___watchChar = 16;
static const int FIELD___callbacksDataReceivedEvent = 17;

NANOCLR_NATIVE_DECLARE(NativeDispose___VOID);
NANOCLR_NATIVE_DECLARE(NativeInit___VOID);
NANOCLR_NATIVE_DECLARE(NativeConfig___VOID);
NANOCLR_NATIVE_DECLARE(NativeWrite___VOID__SZARRAY_U1);
NANOCLR_NATIVE_DECLARE(NativeStore___U4);
NANOCLR_NATIVE_DECLARE(NativeRead___U4__SZARRAY_U1__I4__I4);
NANOCLR_NATIVE_DECLARE(NativeSetWatchChar___VOID);
NANOCLR_NATIVE_DECLARE(GetDeviceSelector___STATIC__STRING);

//--//
Expand All @@ -63,8 +87,17 @@ struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialD

struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDeviceController
{
static const int FIELD_STATIC__s_instance = 0;
static const int FIELD_STATIC__s_deviceCollection = 1;
static const int FIELD_STATIC__s_instance = 1;
static const int FIELD_STATIC__s_deviceCollection = 2;


//--//

};

struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDeviceEventListener
{
static const int FIELD___serialDevicesMap = 1;


//--//
Expand All @@ -90,7 +123,6 @@ struct Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialD

};


extern const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_Windows_Devices_SerialCommunication;


Expand All @@ -107,6 +139,8 @@ struct NF_PAL_UART
HAL_RingBuffer<uint8_t> RxRingBuffer;
uint8_t* RxBuffer;
uint16_t RxBytesToRead;

uint8_t WatchChar;
};


Expand Down Expand Up @@ -209,6 +243,7 @@ extern uint8_t Uart8_RxBuffer[];
Uart##num##_PAL.TxOngoingCount = 0; \
Uart##num##_PAL.RxBuffer = Uart##num##_RxBuffer; \
Uart##num##_PAL.RxRingBuffer.Initialize( Uart##num##_PAL.RxBuffer, rx_buffer_size); \
Uart##num##_PAL.WatchChar = 0; \
}

#else
Expand All @@ -227,6 +262,7 @@ extern uint8_t Uart8_RxBuffer[];
Uart##num##_PAL.TxOngoingCount = 0; \
Uart##num##_PAL.RxBuffer = Uart##num##_RxBuffer; \
Uart##num##_PAL.RxRingBuffer.Initialize( Uart##num##_PAL.RxBuffer, rx_buffer_size); \
Uart##num##_PAL.WatchChar = 0; \
}

#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ enum InputStreamOptions
InputStreamOptions_ReadAhead
};

////////////////////////////////////////////////////////////////////////////////////////////////
// !!! KEEP IN SYNC WITH Windows.Devices.SerialCommunication.SerialData (in managed code) !!! //
////////////////////////////////////////////////////////////////////////////////////////////////

enum SerialData
{
SerialData_Chars = 0,
SerialData_WatchChar,
};

/////////////////////////////////////////////////////////
// UART PAL strucs delcared in win_dev_serial_native.h //
/////////////////////////////////////////////////////////
Expand Down Expand Up @@ -159,59 +169,65 @@ static void TxEnd1(UARTDriver *uartp)
// This callback is invoked when a character is received but the application was not ready to receive it, the character is passed as parameter.
static void RxChar(UARTDriver *uartp, uint16_t c)
{
(void)uartp;
(void)c;

NATIVE_INTERRUPT_START

NF_PAL_UART* palUart;
uint8_t portIndex = 0;

#if STM32_UART_USE_USART1
if (uartp == &UARTD1)
{
palUart = &Uart1_PAL;
portIndex = 1;
}
#endif
#if STM32_UART_USE_USART2
if (uartp == &UARTD2)
{
palUart = &Uart2_PAL;
portIndex = 2;
}
#endif
#if STM32_UART_USE_USART3
if (uartp == &UARTD3)
{
palUart = &Uart3_PAL;
portIndex = 3;
}
#endif
#if STM32_UART_USE_UART4
if (uartp == &UARTD4)
{
palUart = &Uart4_PAL;
portIndex = 4;
}
#endif
#if STM32_UART_USE_UART5
if (uartp == &UARTD5)
{
palUart = &Uart5_PAL;
portIndex = 5;
}
#endif
#if STM32_UART_USE_USART6
if (uartp == &UARTD6)
{
palUart = &Uart6_PAL;
portIndex = 6;
}
#endif
#if STM32_UART_USE_UART7
if (uartp == &UARTD7)
{
palUart = &Uart7_PAL;
portIndex = 7;
}
#endif
#if STM32_UART_USE_UART8
if (uartp == &UARTD8)
{
palUart = &Uart8_PAL;
portIndex = 8;
}
#endif

Expand All @@ -221,14 +237,27 @@ static void RxChar(UARTDriver *uartp, uint16_t c)
// don't care about the success of the operation, if it's full we are droping the char anyway
palUart->RxRingBuffer.Push((uint8_t)c);

// check if the requested bytes are available in the buffer
if(palUart->RxBytesToRead > 0 && palUart->RxRingBuffer.Length() >= palUart->RxBytesToRead)
// is there a read operation going on?
if(palUart->RxBytesToRead > 0)
{
// reset Rx bytes to read count
palUart->RxBytesToRead = 0;
// yes
// check if the requested bytes are available in the buffer...
//... or if the watch char was received
if((palUart->RxRingBuffer.Length() >= palUart->RxBytesToRead) ||
(c == palUart->WatchChar))
{
// reset Rx bytes to read count
palUart->RxBytesToRead = 0;

// fire event for Rx buffer complete
Events_Set(SYSTEM_EVENT_FLAG_COM_IN);
// fire event for Rx buffer complete
Events_Set(SYSTEM_EVENT_FLAG_COM_IN);
}
}
else
{
// no read operation ongoing, so fire an event
// post a managed event with the port index and event code (check if this is the watch char or just another another)
PostManagedEvent( EVENT_SERIAL, 0, portIndex, (c == palUart->WatchChar) ? SerialData_WatchChar : SerialData_Chars );
}

NATIVE_INTERRUPT_END
Expand Down Expand Up @@ -962,6 +991,71 @@ HRESULT Library_win_dev_serial_native_Windows_Devices_SerialCommunication_Serial
NANOCLR_NOCLEANUP();
}

HRESULT Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeSetWatchChar___VOID( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
{
NF_PAL_UART* palUart;

// get a pointer to the managed object instance and check that it's not NULL
CLR_RT_HeapBlock* pThis = stack.This(); FAULT_ON_NULL(pThis);

// Choose the driver for this SerialDevice
switch ((int)pThis[ FIELD___portIndex ].NumericByRef().s4)
{
#if STM32_UART_USE_USART1
case 1 :
palUart = &Uart1_PAL;
break;
#endif
#if STM32_UART_USE_USART2
case 2 :
palUart = &Uart2_PAL;
break;
#endif
#if STM32_UART_USE_USART3
case 3 :
palUart = &Uart3_PAL;
break;
#endif
#if STM32_UART_USE_UART4
case 4 :
palUart = &Uart4_PAL;
break;
#endif
#if STM32_UART_USE_UART5
case 5 :
palUart = &Uart5_PAL;
break;
#endif
#if STM32_UART_USE_USART6
case 6 :
palUart = &Uart6_PAL;
break;
#endif
#if STM32_UART_USE_UART7
case 7 :
palUart = &Uart7_PAL;
break;
#endif
#if STM32_UART_USE_UART8
case 8 :
palUart = &Uart8_PAL;
break;
#endif
default:
// this COM port is not valid
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
break;
}

// set watch char
palUart->WatchChar = (uint8_t)pThis[ FIELD___watchChar ].NumericByRef().u1;

}
NANOCLR_NOCLEANUP();
}

HRESULT Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::GetDeviceSelector___STATIC__STRING( CLR_RT_StackFrame& stack )
{
NANOCLR_HEADER();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,24 @@ static const CLR_RT_MethodHandler method_lookup[] =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeDispose___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeInit___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeConfig___VOID,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeWrite___VOID__SZARRAY_U1,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeStore___U4,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeRead___U4__SZARRAY_U1__I4__I4,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::NativeSetWatchChar___VOID,
NULL,
Library_win_dev_serial_native_Windows_Devices_SerialCommunication_SerialDevice::GetDeviceSelector___STATIC__STRING,
NULL,
Expand All @@ -53,12 +65,19 @@ static const CLR_RT_MethodHandler method_lookup[] =
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
};

const CLR_RT_NativeAssemblyData g_CLR_AssemblyNative_Windows_Devices_SerialCommunication =
{
"Windows.Devices.SerialCommunication",
0x5E530ECD,
0x2B84D4FF,
method_lookup,
{ 1, 0, 0, 0 }
};
Loading

0 comments on commit fe0bbc1

Please sign in to comment.