Skip to content

Commit 0edc862

Browse files
Fix serial parity and databits for STM32 (#2833)
1 parent a55a007 commit 0edc862

File tree

2 files changed

+104
-6
lines changed

2 files changed

+104
-6
lines changed

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

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,10 +980,59 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeConfig___VOI
980980
// setup configuration
981981

982982
// data bits @ CR1:M1&M0
983-
// TODO
983+
switch ((uint16_t)pThis[FIELD___dataBits].NumericByRef().s4)
984+
{
985+
// case 5:
986+
// // palUart->Uart_cfg.cr1 |= UART_DATA_5_BITS;
987+
// break;
988+
989+
// case 6:
990+
// // palUart->Uart_cfg.cr1 |= UART_DATA_6_BITS;
991+
// break;
992+
993+
case 7:
994+
// FIXME
995+
// palUart->Uart_cfg.cr1 |= USART_CR1_DATA7;
996+
break;
997+
998+
case 8:
999+
// FIXME
1000+
// palUart->Uart_cfg.cr1 |= USART_CR1_DATA8;
1001+
break;
1002+
1003+
default:
1004+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
1005+
}
9841006

985-
// parity @ CR1:PS
986-
// TODO
1007+
// parity
1008+
switch ((Parity)pThis[FIELD___parity].NumericByRef().s4)
1009+
{
1010+
case Parity_None:
1011+
palUart->Uart_cfg.cr1 &= ~(USART_CR1_PCE | USART_CR1_PS | USART_CR1_M);
1012+
break;
1013+
case Parity_Even:
1014+
#ifdef USART_CR1_M0
1015+
// cope with F3 and F7 where there are 2 bits in CR1_M
1016+
palUart->Uart_cfg.cr1 |= USART_CR1_M0 | USART_CR1_PCE;
1017+
#else
1018+
palUart->Uart_cfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
1019+
#endif
1020+
palUart->Uart_cfg.cr1 &= ~USART_CR1_PS;
1021+
break;
1022+
case Parity_Odd:
1023+
// setting USART_CR1_M ensures extra bit is used as parity
1024+
// not last bit of data
1025+
#ifdef USART_CR1_M0
1026+
// cope with F3 and F7 where there are 2 bits in CR1_M
1027+
palUart->Uart_cfg.cr1 |= USART_CR1_M0 | USART_CR1_PCE;
1028+
#else
1029+
palUart->Uart_cfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
1030+
#endif
1031+
palUart->Uart_cfg.cr1 |= USART_CR1_PS;
1032+
break;
1033+
default:
1034+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
1035+
}
9871036

9881037
// Check RS485 mode is not selected as currently not supported
9891038
if ((SerialMode)pThis[FIELD___mode].NumericByRef().s4 != SerialMode_Normal)

targets/ChibiOS/_nanoCLR/System.IO.Ports/sys_io_ser_native_System_IO_Ports_SerialPort.cpp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,59 @@ HRESULT Library_sys_io_ser_native_System_IO_Ports_SerialPort::NativeConfig___VOI
983983
// setup configuration
984984

985985
// data bits @ CR1:M1&M0
986-
// TODO
986+
switch ((uint16_t)pThis[FIELD___dataBits].NumericByRef().s4)
987+
{
988+
// case 5:
989+
// // palUart->Uart_cfg.cr1 |= UART_DATA_5_BITS;
990+
// break;
991+
992+
// case 6:
993+
// // palUart->Uart_cfg.cr1 |= UART_DATA_6_BITS;
994+
// break;
995+
996+
case 7:
997+
// FIXME
998+
// palUart->Uart_cfg.cr1 |= USART_CR1_DATA7;
999+
break;
1000+
1001+
case 8:
1002+
// FIXME
1003+
// palUart->Uart_cfg.cr1 |= USART_CR1_DATA8;
1004+
break;
1005+
1006+
default:
1007+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
1008+
}
9871009

988-
// parity @ CR1:PS
989-
// TODO
1010+
// parity
1011+
switch ((Parity)pThis[FIELD___parity].NumericByRef().s4)
1012+
{
1013+
case Parity_None:
1014+
palUart->Uart_cfg.cr1 &= ~(USART_CR1_PCE | USART_CR1_PS | USART_CR1_M);
1015+
break;
1016+
case Parity_Even:
1017+
#ifdef USART_CR1_M0
1018+
// cope with F3 and F7 where there are 2 bits in CR1_M
1019+
palUart->Uart_cfg.cr1 |= USART_CR1_M0 | USART_CR1_PCE;
1020+
#else
1021+
palUart->Uart_cfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
1022+
#endif
1023+
palUart->Uart_cfg.cr1 &= ~USART_CR1_PS;
1024+
break;
1025+
case Parity_Odd:
1026+
// setting USART_CR1_M ensures extra bit is used as parity
1027+
// not last bit of data
1028+
#ifdef USART_CR1_M0
1029+
// cope with F3 and F7 where there are 2 bits in CR1_M
1030+
palUart->Uart_cfg.cr1 |= USART_CR1_M0 | USART_CR1_PCE;
1031+
#else
1032+
palUart->Uart_cfg.cr1 |= USART_CR1_M | USART_CR1_PCE;
1033+
#endif
1034+
palUart->Uart_cfg.cr1 |= USART_CR1_PS;
1035+
break;
1036+
default:
1037+
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_PARAMETER);
1038+
}
9901039

9911040
// Check RS485 mode is not selected as currently not supported
9921041
if ((SerialMode)pThis[FIELD___mode].NumericByRef().s4 != SerialMode_Normal)

0 commit comments

Comments
 (0)