MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / rt_serial_open

Function rt_serial_open

components/drivers/serial/dev_serial_v2.c:1182–1232  ·  view source on GitHub ↗

* @brief Open the serial device. * @param dev The pointer of device driver structure * @param oflag The flag of that the serial port opens. * @return Return the status of the operation. */

Source from the content-addressed store, hash-verified

1180 * @return Return the status of the operation.
1181 */
1182static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
1183{
1184 struct rt_serial_device *serial;
1185 rt_err_t result = RT_EOK;
1186
1187 RT_ASSERT(dev != RT_NULL);
1188 serial = (struct rt_serial_device *)dev;
1189
1190 LOG_D("open serial device: 0x%08x with open flag: 0x%04x", dev, oflag);
1191
1192 /* By default, the receive mode of a serial devide is RT_SERIAL_RX_NON_BLOCKING */
1193 if ((oflag & RT_SERIAL_RX_BLOCKING) == RT_SERIAL_RX_BLOCKING)
1194 dev->open_flag |= RT_SERIAL_RX_BLOCKING;
1195 else
1196 dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
1197
1198 /* By default, the transmit mode of a serial devide is RT_SERIAL_TX_BLOCKING */
1199 if ((oflag & RT_SERIAL_TX_NON_BLOCKING) == RT_SERIAL_TX_NON_BLOCKING)
1200 dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
1201 else
1202 dev->open_flag |= RT_SERIAL_TX_BLOCKING;
1203
1204 /* set steam flag */
1205 if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
1206 dev->open_flag |= RT_DEVICE_FLAG_STREAM;
1207
1208 /* initialize the Rx structure according to open flag */
1209 if (serial->serial_rx == RT_NULL)
1210 {
1211 result = rt_serial_rx_enable(dev, dev->open_flag & (RT_SERIAL_RX_BLOCKING | RT_SERIAL_RX_NON_BLOCKING));
1212 if (result != RT_EOK)
1213 {
1214 rt_serial_close(dev);
1215 goto __exit;
1216 }
1217 }
1218
1219 /* initialize the Tx structure according to open flag */
1220 if (serial->serial_tx == RT_NULL)
1221 {
1222 result = rt_serial_tx_enable(dev, dev->open_flag & (RT_SERIAL_TX_BLOCKING | RT_SERIAL_TX_NON_BLOCKING));
1223 if (result != RT_EOK)
1224 {
1225 rt_serial_close(dev);
1226 goto __exit;
1227 }
1228 }
1229
1230__exit:
1231 return result;
1232}
1233
1234
1235static void _serial_rx_flush(struct rt_serial_device *serial)

Callers

nothing calls this directly

Calls 3

rt_serial_rx_enableFunction · 0.85
rt_serial_tx_enableFunction · 0.85
rt_serial_closeFunction · 0.70

Tested by

no test coverage detected