@@ -30,10 +30,14 @@ LOG_MODULE_REGISTER(uart_mcux_lpuart, LOG_LEVEL_ERR);
30
30
31
31
#define PINCTRL_STATE_FLOWCONTROL PINCTRL_STATE_PRIV_START
32
32
33
- #if defined(CONFIG_UART_LINE_CTRL ) && \
34
- defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT ) && \
35
- (FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT )
36
- #define UART_LINE_CTRL_ENABLE
33
+ /* Helper macros */
34
+ #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT ) && \
35
+ FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
36
+ #define LPUART_HAS_MODEM 1
37
+ #endif
38
+
39
+ #if defined(FSL_FEATURE_LPUART_HAS_MCR ) && FSL_FEATURE_LPUART_HAS_MCR
40
+ #define LPUART_HAS_MCR 1
37
41
#endif
38
42
39
43
#if defined(CONFIG_UART_ASYNC_API ) && defined(CONFIG_UART_INTERRUPT_DRIVEN )
@@ -970,9 +974,47 @@ static void mcux_lpuart_isr(const struct device *dev)
970
974
}
971
975
#endif /* CONFIG_UART_MCUX_LPUART_ISR_SUPPORT */
972
976
977
+ #if LPUART_HAS_MODEM
978
+ static int mcux_lpuart_config_flowctrl (uint8_t flow_ctrl , lpuart_config_t * uart_config )
979
+ {
980
+ switch (flow_ctrl ) {
981
+ case UART_CFG_FLOW_CTRL_NONE :
982
+ uart_config -> enableTxCTS = false;
983
+ uart_config -> enableRxRTS = false;
984
+ break ;
985
+
986
+ case UART_CFG_FLOW_CTRL_RS485 :
987
+ uart_config -> enableTxCTS = false;
988
+ uart_config -> enableRxRTS = false;
989
+ break ;
990
+
991
+ case UART_CFG_FLOW_CTRL_RTS_CTS :
992
+ uart_config -> enableTxCTS = true;
993
+ uart_config -> enableRxRTS = true;
994
+ break ;
995
+
996
+ default :
997
+ return - ENOTSUP ;
998
+ }
999
+
1000
+ return 0 ;
1001
+ }
1002
+ #else
1003
+ static int mcux_lpuart_config_flowctrl (uint8_t flow_ctrl , lpuart_config_t * uart_config )
1004
+ {
1005
+ if (flow_ctrl == UART_CFG_FLOW_CTRL_NONE ) {
1006
+ return 0 ;
1007
+ }
1008
+
1009
+ return - ENOTSUP ;
1010
+ }
1011
+ #endif /* LPUART_HAS_MODEM */
1012
+
973
1013
static int mcux_lpuart_configure_basic (const struct device * dev , const struct uart_config * cfg ,
974
1014
lpuart_config_t * uart_config )
975
1015
{
1016
+ int ret ;
1017
+
976
1018
/* Translate UART API enum to LPUART enum from HAL */
977
1019
switch (cfg -> parity ) {
978
1020
case UART_CFG_PARITY_NONE :
@@ -1016,22 +1058,15 @@ static int mcux_lpuart_configure_basic(const struct device *dev, const struct ua
1016
1058
}
1017
1059
#endif
1018
1060
1019
- #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT ) && \
1020
- FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
1021
- switch (cfg -> flow_ctrl ) {
1022
- case UART_CFG_FLOW_CTRL_NONE :
1023
- case UART_CFG_FLOW_CTRL_RS485 :
1024
- uart_config -> enableTxCTS = false;
1025
- uart_config -> enableRxRTS = false;
1026
- break ;
1027
- case UART_CFG_FLOW_CTRL_RTS_CTS :
1028
- uart_config -> enableTxCTS = true;
1029
- uart_config -> enableRxRTS = true;
1030
- break ;
1031
- default :
1061
+ /* Configure for Flow Control option */
1062
+ if (!IS_ENABLED (LPUART_HAS_MCR ) && cfg -> flow_ctrl == UART_CFG_FLOW_CTRL_DTR_DSR ) {
1032
1063
return - ENOTSUP ;
1033
1064
}
1034
- #endif
1065
+
1066
+ ret = mcux_lpuart_config_flowctrl (cfg -> flow_ctrl , uart_config );
1067
+ if (ret ) {
1068
+ return ret ;
1069
+ }
1035
1070
1036
1071
uart_config -> baudRate_Bps = cfg -> baudrate ;
1037
1072
uart_config -> enableRx = true;
@@ -1105,8 +1140,7 @@ static int mcux_lpuart_configure_init(const struct device *dev, const struct uar
1105
1140
1106
1141
LPUART_Init (config -> base , & uart_config , clock_freq );
1107
1142
1108
- #if defined(FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT ) && \
1109
- FSL_FEATURE_LPUART_HAS_MODEM_SUPPORT
1143
+ #ifdef LPUART_HAS_MODEM
1110
1144
if (cfg -> flow_ctrl == UART_CFG_FLOW_CTRL_RS485 ) {
1111
1145
/* Set the LPUART into RS485 mode (tx driver enable using RTS) */
1112
1146
config -> base -> MODIR |= LPUART_MODIR_TXRTSE (true);
@@ -1185,10 +1219,15 @@ static int mcux_lpuart_configure(const struct device *dev,
1185
1219
}
1186
1220
#endif /* CONFIG_UART_USE_RUNTIME_CONFIGURE */
1187
1221
1188
- #ifdef UART_LINE_CTRL_ENABLE
1189
- static void mcux_lpuart_line_ctrl_set_rts ( const struct mcux_lpuart_config * config ,
1190
- uint32_t val )
1222
+ #ifdef CONFIG_UART_LINE_CTRL
1223
+ #if LPUART_HAS_MODEM
1224
+ static void mcux_lpuart_line_ctrl_set_rts ( const struct device * dev , uint32_t val )
1191
1225
{
1226
+ const struct mcux_lpuart_config * config = dev -> config ;
1227
+
1228
+ /* Disable Transmitter and Receiver */
1229
+ config -> base -> CTRL &= ~(LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK );
1230
+
1192
1231
if (val >= 1U ) {
1193
1232
/* Reset TXRTS to set RXRTSE bit, this provides high-level on RTS line */
1194
1233
config -> base -> MODIR &= ~(LPUART_MODIR_TXRTSPOL_MASK | LPUART_MODIR_TXRTSE_MASK );
@@ -1199,20 +1238,62 @@ static void mcux_lpuart_line_ctrl_set_rts(const struct mcux_lpuart_config *confi
1199
1238
config -> base -> MODIR |= (LPUART_MODIR_TXRTSPOL_MASK | LPUART_MODIR_TXRTSE_MASK );
1200
1239
}
1201
1240
}
1241
+ #else
1242
+ #define mcux_lpuart_line_ctrl_set_rts (dev , val ) ret = -ENOTSUP
1243
+ #endif /* LPUART_HAS_MODEM */
1244
+
1245
+ #if LPUART_HAS_MCR
1246
+ static void mcux_lpuart_set_dtr (const struct device * dev , uint32_t val )
1247
+ {
1248
+ const struct mcux_lpuart_config * config = dev -> config ;
1249
+
1250
+ if (val >= 1U ) {
1251
+ /* assert DTR_b */
1252
+ config -> base -> MCR &= ~LPUART_MCR_DTR_MASK ;
1253
+ } else {
1254
+ /* deassert DTR_b */
1255
+ config -> base -> MCR |= LPUART_MCR_DTR_MASK ;
1256
+ }
1257
+ }
1258
+ #else
1259
+ #define mcux_lpuart_set_dtr (dev , val ) ret = -ENOTSUP
1260
+ #endif /* LPUART_HAS_MCR */
1202
1261
1203
1262
static int mcux_lpuart_line_ctrl_set (const struct device * dev ,
1204
1263
uint32_t ctrl , uint32_t val )
1205
1264
{
1206
- const struct mcux_lpuart_config * config = dev -> config ;
1207
1265
int ret = 0 ;
1208
1266
1209
1267
switch (ctrl ) {
1210
1268
case UART_LINE_CTRL_RTS :
1211
- /* Disable Transmitter and Receiver */
1212
- config -> base -> CTRL &= ~( LPUART_CTRL_TE_MASK | LPUART_CTRL_RE_MASK ) ;
1269
+ mcux_lpuart_line_ctrl_set_rts ( dev , val );
1270
+ break ;
1213
1271
1214
- mcux_lpuart_line_ctrl_set_rts (config , val );
1272
+ case UART_LINE_CTRL_DTR :
1273
+ mcux_lpuart_set_dtr (dev , val );
1274
+ break ;
1275
+
1276
+ default :
1277
+ ret = - ENOTSUP ;
1278
+ }
1279
+
1280
+ return ret ;
1281
+ }
1215
1282
1283
+ #if LPUART_HAS_MCR
1284
+ static int mcux_lpuart_line_ctrl_get (const struct device * dev ,
1285
+ uint32_t ctrl , uint32_t * val )
1286
+ {
1287
+ const struct mcux_lpuart_config * config = dev -> config ;
1288
+ int ret = 0 ;
1289
+
1290
+ switch (ctrl ) {
1291
+ case UART_LINE_CTRL_DSR :
1292
+ * val = (config -> base -> MSR & LPUART_MSR_DSR_MASK ) >> LPUART_MSR_DSR_SHIFT ;
1293
+ break ;
1294
+
1295
+ case UART_LINE_CTRL_DCD :
1296
+ * val = (config -> base -> MSR & LPUART_MSR_DCD_MASK ) >> LPUART_MSR_DCD_SHIFT ;
1216
1297
break ;
1217
1298
1218
1299
default :
@@ -1221,7 +1302,14 @@ static int mcux_lpuart_line_ctrl_set(const struct device *dev,
1221
1302
1222
1303
return ret ;
1223
1304
}
1224
- #endif /* UART_LINE_CTRL_ENABLE */
1305
+ #else
1306
+ static int mcux_lpuart_line_ctrl_get (const struct device * dev ,
1307
+ uint32_t ctrl , uint32_t * val )
1308
+ {
1309
+ return - ENOTSUP ;
1310
+ }
1311
+ #endif /* LPUART_HAS_MCR */
1312
+ #endif /* CONFIG_UART_LINE_CTRL */
1225
1313
1226
1314
static int mcux_lpuart_init (const struct device * dev )
1227
1315
{
@@ -1301,9 +1389,10 @@ static DEVICE_API(uart, mcux_lpuart_driver_api) = {
1301
1389
.rx_buf_rsp = mcux_lpuart_rx_buf_rsp ,
1302
1390
.rx_disable = mcux_lpuart_rx_disable ,
1303
1391
#endif /* CONFIG_UART_ASYNC_API */
1304
- #ifdef UART_LINE_CTRL_ENABLE
1392
+ #ifdef CONFIG_UART_LINE_CTRL
1305
1393
.line_ctrl_set = mcux_lpuart_line_ctrl_set ,
1306
- #endif /* UART_LINE_CTRL_ENABLE */
1394
+ .line_ctrl_get = mcux_lpuart_line_ctrl_get ,
1395
+ #endif /* CONFIG_UART_LINE_CTRL */
1307
1396
};
1308
1397
1309
1398
0 commit comments