forked from RT-Thread/rt-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_serial_v2.c
More file actions
2113 lines (1787 loc) · 63.4 KB
/
dev_serial_v2.c
File metadata and controls
2113 lines (1787 loc) · 63.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2006-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-06-01 KyleChan first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#define DBG_TAG "Serial"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#ifdef RT_SERIAL_BUF_STRATEGY_DROP
#define RT_SERIAL_FIFO_LOCK(spinlock) ((rt_base_t)0)
#define RT_SERIAL_FIFO_UNLOCK(spinlock, level) \
do { \
RT_UNUSED(spinlock); \
RT_UNUSED(level); \
} while (0)
#else
#define RT_SERIAL_FIFO_LOCK(spinlock) rt_spin_lock_irqsave(spinlock)
#define RT_SERIAL_FIFO_UNLOCK(spinlock, level) rt_spin_unlock_irqrestore(spinlock, level)
#endif /* RT_SERIAL_BUF_STRATEGY_DROP */
#ifdef RT_USING_POSIX_STDIO
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <dfs_file.h>
#ifdef RT_USING_POSIX_TERMIOS
#include <termios.h>
#endif
#ifdef getc
#undef getc
#endif
#ifdef putc
#undef putc
#endif
RT_OBJECT_HOOKLIST_DEFINE(rt_hw_serial_rxind);
static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
{
rt_wqueue_wakeup(&dev->wait_queue, (void *)POLLIN);
RT_OBJECT_HOOKLIST_CALL(rt_hw_serial_rxind, (dev, size));
return RT_EOK;
}
/* fops for serial */
static int serial_fops_open(struct dfs_file *fd)
{
rt_err_t ret = 0;
rt_uint16_t flags = 0;
rt_device_t device;
device = (rt_device_t)fd->vnode->data;
RT_ASSERT(device != RT_NULL);
switch (fd->flags & O_ACCMODE)
{
case O_RDONLY:
LOG_D("fops open: O_RDONLY!");
flags = RT_DEVICE_FLAG_RDONLY;
break;
case O_WRONLY:
LOG_D("fops open: O_WRONLY!");
flags = RT_DEVICE_FLAG_WRONLY;
break;
case O_RDWR:
LOG_D("fops open: O_RDWR!");
flags = RT_DEVICE_FLAG_RDWR;
break;
default:
LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
break;
}
if ((fd->flags & O_ACCMODE) != O_WRONLY)
rt_device_set_rx_indicate(device, serial_fops_rx_ind);
ret = rt_device_open(device, flags | RT_SERIAL_RX_BLOCKING | RT_SERIAL_TX_BLOCKING);
if (ret == RT_EOK) return 0;
return ret;
}
static int serial_fops_close(struct dfs_file *fd)
{
rt_device_t device;
device = (rt_device_t)fd->vnode->data;
rt_device_set_rx_indicate(device, RT_NULL);
rt_device_close(device);
return 0;
}
static int serial_fops_ioctl(struct dfs_file *fd, int cmd, void *args)
{
rt_device_t device;
int flags = (int)(rt_base_t)args;
int mask = O_NONBLOCK | O_APPEND;
device = (rt_device_t)fd->vnode->data;
switch (cmd)
{
case FIONREAD:
break;
case FIONWRITE:
break;
case F_SETFL:
flags &= mask;
fd->flags &= ~mask;
fd->flags |= flags;
break;
}
return rt_device_control(device, cmd, args);
}
#ifdef RT_USING_DFS_V2
static ssize_t serial_fops_read(struct dfs_file *fd, void *buf, size_t count, off_t *pos)
#else
static ssize_t serial_fops_read(struct dfs_file *fd, void *buf, size_t count)
#endif
{
ssize_t size = 0;
rt_device_t device;
rt_int32_t rx_timout;
if (count == 0) return 0;
RT_ASSERT(fd != RT_NULL && buf != RT_NULL);
device = (rt_device_t)fd->vnode->data;
RT_ASSERT(device != RT_NULL);
/* nonblock mode */
if (fd->flags & O_NONBLOCK)
{
rx_timout = RT_WAITING_NO;
rt_device_control(device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timout);
size = rt_device_read(device, -1, buf, count);
if (size <= 0)
{
size = -1;
rt_set_errno(EAGAIN);
}
}
else
{
rx_timout = RT_WAITING_FOREVER;
rt_device_control(device, RT_SERIAL_CTRL_SET_RX_TIMEOUT, (void *)&rx_timout);
size = rt_device_read(device, -1, buf, count);
}
return size;
}
#ifdef RT_USING_DFS_V2
static ssize_t serial_fops_write(struct dfs_file *fd, const void *buf, size_t count, off_t *pos)
#else
static ssize_t serial_fops_write(struct dfs_file *fd, const void *buf, size_t count)
#endif
{
ssize_t size = 0;
rt_device_t device;
rt_int32_t tx_timeout;
device = (rt_device_t)fd->vnode->data;
if (fd->flags & O_NONBLOCK)
{
tx_timeout = RT_WAITING_NO;
rt_device_control(device, RT_SERIAL_CTRL_SET_TX_TIMEOUT, (void *)&tx_timeout);
size = rt_device_write(device, -1, buf, count);
if (size <= 0)
{
size = -1;
rt_set_errno(EAGAIN);
}
}
else
{
tx_timeout = RT_WAITING_FOREVER;
rt_device_control(device, RT_SERIAL_CTRL_SET_TX_TIMEOUT, (void *)&tx_timeout);
size = rt_device_write(device, -1, buf, count);
}
return size;
}
static int serial_fops_flush(struct dfs_file *fd)
{
rt_device_t device;
struct rt_serial_device *serial;
device = (rt_device_t)fd->vnode->data;
RT_ASSERT(device != RT_NULL);
serial = (struct rt_serial_device *)device;
rt_device_control(device, RT_SERIAL_CTRL_TX_FLUSH, (void *)RT_NULL);
rt_device_control(device, RT_SERIAL_CTRL_RX_FLUSH, (void *)RT_NULL);
return 0;
}
static int serial_fops_poll(struct dfs_file *fd, struct rt_pollreq *req)
{
int mask = 0;
int flags = 0;
rt_device_t device;
struct rt_serial_device *serial;
device = (rt_device_t)fd->vnode->data;
RT_ASSERT(device != RT_NULL);
serial = (struct rt_serial_device *)device;
/* only support POLLIN */
flags = fd->flags & O_ACCMODE;
if (flags == O_RDONLY || flags == O_RDWR)
{
rt_base_t level;
struct rt_serial_rx_fifo *rx_fifo;
rt_poll_add(&device->wait_queue, req);
rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
if (rt_ringbuffer_data_len(&rx_fifo->rb))
mask |= POLLIN;
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
}
/* mask|=POLLOUT; */
return mask;
}
const static struct dfs_file_ops _serial_fops =
{
.open = serial_fops_open,
.close = serial_fops_close,
.ioctl = serial_fops_ioctl,
.read = serial_fops_read,
.write = serial_fops_write,
.flush = serial_fops_flush,
.poll = serial_fops_poll,
};
#endif /* RT_USING_POSIX_STDIO */
static rt_ssize_t rt_serial_get_linear_buffer(struct rt_ringbuffer *rb,
rt_uint8_t **ptr)
{
rt_size_t size;
RT_ASSERT(rb != RT_NULL);
/* whether has enough data */
size = rt_ringbuffer_data_len(rb);
/* no data */
if (size == 0)
return 0;
*ptr = &rb->buffer_ptr[rb->read_index];
if (rb->buffer_size - rb->read_index > size)
{
return size;
}
return rb->buffer_size - rb->read_index;
}
#ifdef RT_SERIAL_USING_DMA
static void rt_serial_update_read_index(struct rt_ringbuffer *rb,
rt_uint16_t length)
{
rt_size_t size;
RT_ASSERT(rb != RT_NULL);
/* whether has enough data */
size = rt_ringbuffer_data_len(rb);
/* no data */
if (size == 0)
return;
/* less data */
if (size < length)
length = size;
if (rb->buffer_size - rb->read_index > length)
{
rb->read_index += length;
return;
}
/* we are going into the other side of the mirror */
rb->read_mirror = ~rb->read_mirror;
rb->read_index = length - (rb->buffer_size - rb->read_index);
return;
}
static void rt_serial_update_write_index(struct rt_ringbuffer *rb,
rt_uint16_t length)
{
rt_uint16_t space_length;
RT_ASSERT(rb != RT_NULL);
/* whether has enough space */
space_length = rt_ringbuffer_space_len(rb);
if (length > rb->buffer_size)
{
length = rb->buffer_size;
#if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
LOG_W("The serial buffer (len %d) is overflow.", rb->buffer_size);
#endif
}
if (rb->buffer_size - rb->write_index > length)
{
/* this should not cause overflow because there is enough space for
* length of data in current mirror */
rb->write_index += length;
if (length > space_length)
rb->read_index = rb->write_index;
return;
}
/* we are going into the other side of the mirror */
rb->write_mirror = ~rb->write_mirror;
rb->write_index = length - (rb->buffer_size - rb->write_index);
if (length > space_length)
{
if (rb->write_index <= rb->read_index)
rb->read_mirror = ~rb->read_mirror;
rb->read_index = rb->write_index;
}
return;
}
#endif /* RT_SERIAL_USING_DMA */
/**
* @brief Serial polling receive data routine, This function will receive data
* in a continuous loop by one by one byte.
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Receive data buffer.
* @param size Receive data buffer length.
* @return Return the final length of data received.
*/
rt_ssize_t _serial_poll_rx(struct rt_device *dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
rt_size_t getc_size;
int getc_element; /* Gets one byte of data received */
rt_uint8_t *getc_buffer; /* Pointer to the receive data buffer */
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
getc_buffer = (rt_uint8_t *)buffer;
getc_size = size;
while (size)
{
getc_element = serial->ops->getc(serial);
if (getc_element == -1) break;
*getc_buffer = getc_element;
++getc_buffer;
--size;
if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
{
/* If open_flag satisfies RT_DEVICE_FLAG_STREAM
* and the received character is '\n', exit the loop directly */
if (getc_element == '\n') break;
}
}
return getc_size - size;
}
/**
* @brief Serial polling transmit data routines, This function will transmit
* data in a continuous loop by one by one byte.
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Transmit data buffer.
* @param size Transmit data buffer length.
* @return Return the final length of data transmit.
*/
rt_ssize_t _serial_poll_tx(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
rt_size_t putc_size;
rt_uint8_t *putc_buffer; /* Pointer to the transmit data buffer */
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
putc_buffer = (rt_uint8_t *)buffer;
putc_size = size;
while (size)
{
if (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
{
/* If open_flag satisfies RT_DEVICE_FLAG_STREAM and the received character is '\n',
* inserts '\r' character before '\n' character for the effect of carriage return newline */
if (*putc_buffer == '\n')
serial->ops->putc(serial, '\r');
}
serial->ops->putc(serial, *putc_buffer);
++putc_buffer;
--size;
}
return putc_size - size;
}
/**
* @brief Serial receive data routines, This function will receive
* data by using fifo
*
* @note In blocking mode, the function will wait until the specified amount of data is received or until a timeout occurs.
* In non-blocking mode, the function will immediately attempt to retrieve as much data as possible from the ring buffer and return.
*
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Receive data buffer.
* @param size Receive data buffer length.
* @return Returns the actual length of data received. If a timeout occurs in blocking mode, it returns -RT_ETIMEOUT.
*/
static rt_ssize_t _serial_fifo_rx(struct rt_device *dev,
rt_off_t pos,
void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_rx_fifo *rx_fifo;
rt_base_t level;
rt_size_t recv_size = 0;
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
if (dev->open_flag & RT_SERIAL_RX_BLOCKING)
{
rt_size_t data_len;
rt_tick_t now_tick;
rt_size_t rx_bufsz_third = serial->config.rx_bufsz / 2;
rt_int32_t base_rx_timeout = rt_atomic_load(&rx_fifo->rx_timeout);
rt_int32_t rx_timeout = base_rx_timeout;
rt_tick_t begin_tick = rt_tick_get();
while (1)
{
if (rx_timeout != RT_WAITING_NO)
{
level = rt_spin_lock_irqsave(&serial->spinlock);
data_len = rt_ringbuffer_data_len(&rx_fifo->rb);
if (data_len < size - recv_size)
{
if (size - (recv_size + data_len) >= rx_bufsz_third)
{
rx_fifo->rx_cpt_index = rx_bufsz_third;
}
else
{
rx_fifo->rx_cpt_index = size - (recv_size + data_len);
}
rt_completion_wait(&rx_fifo->rx_cpt, 0);
}
rt_spin_unlock_irqrestore(&serial->spinlock, level);
}
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
recv_size += rt_ringbuffer_get(&rx_fifo->rb, (rt_uint8_t *)buffer + recv_size, size - recv_size);
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
if (recv_size == size || rx_timeout == RT_WAITING_NO)
{
break;
}
rt_completion_wait(&rx_fifo->rx_cpt, rx_timeout);
if (rx_timeout != RT_WAITING_FOREVER)
{
now_tick = rt_tick_get();
if (now_tick - begin_tick >= base_rx_timeout)
{
return -RT_ETIMEOUT;
}
else
{
if (now_tick > begin_tick)
rx_timeout = base_rx_timeout - (now_tick - begin_tick);
else
rx_timeout = begin_tick + base_rx_timeout - now_tick + 1;
}
}
}
}
else if (dev->open_flag & RT_SERIAL_RX_NON_BLOCKING)
{
/* When open_flag is RT_SERIAL_RX_NON_BLOCKING,
* the data is retrieved directly from the ringbuffer and returned */
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
recv_size = rt_ringbuffer_get(&rx_fifo->rb, buffer, size);
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
}
return recv_size;
}
/**
* @brief Serial transmit data routines, This function will transmit
* data by using blocking_nbuf.
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Transmit data buffer.
* @param size Transmit data buffer length.
* @return Returns the actual length of data transmitted. If a timeout occurs, it returns -RT_ETIMEOUT.
*/
static rt_ssize_t _serial_fifo_tx_blocking_nbuf(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo;
rt_ssize_t send_size;
rt_err_t ret;
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
{
/* using poll tx when the scheduler not startup or in stream mode */
return _serial_poll_tx(dev, pos, buffer, size);
}
/* When serial transmit in tx_blocking mode,
* if the activated mode is RT_TRUE, it will return directly */
if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
{
return 0;
}
/* clear tx_cpt flag */
rt_completion_wait(&tx_fifo->tx_cpt, 0);
/* Call the transmit interface for transmission */
send_size = serial->ops->transmit(serial,
(rt_uint8_t *)buffer,
size,
RT_SERIAL_TX_BLOCKING);
if (rt_atomic_load(&tx_fifo->tx_timeout) == RT_WAITING_NO)
{
return send_size;
}
/* Waiting for the transmission to complete */
ret = rt_completion_wait(&tx_fifo->tx_cpt, rt_atomic_load(&tx_fifo->tx_timeout));
if (ret != RT_EOK)
{
if (ret == -RT_ETIMEOUT)
{
return ret;
}
else
{
return 0;
}
}
/* Inactive tx mode flag */
rt_atomic_flag_clear(&tx_fifo->activated);
return send_size;
}
/**
* @brief Serial transmit data routines, This function will transmit
* data by using blocking_buf.
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Transmit data buffer.
* @param size Transmit data buffer length.
* @return Returns the final length of data transmitted. If not all data can be sent within the timeout period, returns -RT_ETIMEOUT.
*/
static rt_ssize_t _serial_fifo_tx_blocking_buf(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo;
rt_base_t level;
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
if (rt_thread_self() == RT_NULL || (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
{
/* using poll tx when the scheduler not startup or in stream mode */
return _serial_poll_tx(dev, pos, buffer, size);
}
/* When serial transmit in tx_blocking mode,
* if the activated mode is RT_TRUE, it will return directly */
if (rt_atomic_flag_test_and_set(&tx_fifo->activated))
{
return 0;
}
rt_tick_t now_tick;
rt_int32_t base_tx_timeout = rt_atomic_load(&tx_fifo->tx_timeout);
rt_int32_t tx_timeout = base_tx_timeout;
rt_tick_t begin_tick = rt_tick_get();
rt_size_t send_size = 0;
while (send_size != size)
{
/* Copy one piece of data into the ringbuffer at a time
* until the length of the data is equal to size */
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
tx_fifo->put_size = rt_ringbuffer_put(&tx_fifo->rb,
(rt_uint8_t *)buffer + send_size,
size - send_size);
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
/* clear tx_cpt flag */
rt_completion_wait(&tx_fifo->tx_cpt, 0);
/* Call the transmit interface for transmission again */
/* Note that in interrupt mode, buffer and tx_fifo->put_size
* are inactive parameters */
serial->ops->transmit(serial,
(rt_uint8_t *)buffer + send_size,
tx_fifo->put_size,
RT_SERIAL_TX_BLOCKING);
send_size += tx_fifo->put_size;
if (tx_timeout == RT_WAITING_NO)
{
break;
}
/* Waiting for the transmission to complete */
rt_completion_wait(&tx_fifo->tx_cpt, tx_timeout);
if (tx_timeout != RT_WAITING_FOREVER)
{
now_tick = rt_tick_get();
if (now_tick - begin_tick >= base_tx_timeout)
{
return -RT_ETIMEOUT;
}
else
{
if (now_tick > begin_tick)
tx_timeout = base_tx_timeout - (now_tick - begin_tick);
else
tx_timeout = begin_tick + base_tx_timeout - now_tick + 1;
}
}
}
/* Finally Inactivate the tx->fifo */
rt_atomic_flag_clear(&tx_fifo->activated);
return send_size;
}
/**
* @brief Serial transmit data routines, This function will transmit
* data by using nonblocking.
* @param dev The pointer of device driver structure
* @param pos Empty parameter.
* @param buffer Transmit data buffer.
* @param size Transmit data buffer length.
* @return Return the final length of data transmit.
*/
static rt_ssize_t _serial_fifo_tx_nonblocking(struct rt_device *dev,
rt_off_t pos,
const void *buffer,
rt_size_t size)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo;
rt_uint8_t *put_ptr;
rt_base_t level;
rt_size_t send_size = 0;
if (size == 0) return 0;
RT_ASSERT(dev != RT_NULL && buffer != RT_NULL);
serial = (struct rt_serial_device *)dev;
tx_fifo = (struct rt_serial_tx_fifo *)serial->serial_tx;
RT_ASSERT(tx_fifo != RT_NULL);
/* When serial transmit in tx_non_blocking mode, if the activated mode is RT_FALSE,
* start copying data into the ringbuffer */
if (!rt_atomic_flag_test_and_set(&tx_fifo->activated))
{
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
/* Copying data into the ringbuffer */
send_size = rt_ringbuffer_put(&tx_fifo->rb, buffer, size);
/* Get the linear length buffer from ringbuffer */
tx_fifo->put_size = rt_serial_get_linear_buffer(&tx_fifo->rb, &put_ptr);
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
/* clear tx_cpt flag */
rt_completion_wait(&tx_fifo->tx_cpt, 0);
/* Call the transmit interface for transmission again */
/* Note that in interrupt mode, put_ptr and tx_fifo->put_size
* are inactive parameters */
serial->ops->transmit(serial,
put_ptr,
tx_fifo->put_size,
RT_SERIAL_TX_NON_BLOCKING);
/* In tx_nonblocking mode, there is no need to call rt_completion_wait() APIs to wait
* for the rt_current_thread to resume */
return send_size;
}
/* If the activated mode is RT_TRUE, it means that serial device is transmitting,
* where only the data in the ringbuffer and there is no need to call the transmit() API.
* Note that this part of the code requires disable interrupts
* to prevent multi thread reentrant */
level = RT_SERIAL_FIFO_LOCK(&serial->spinlock);
/* Copying data into the ringbuffer */
send_size = rt_ringbuffer_put(&tx_fifo->rb, buffer, size);
RT_SERIAL_FIFO_UNLOCK(&serial->spinlock, level);
return send_size;
}
/**
* @brief Enable serial transmit mode.
* @param dev The pointer of device driver structure
* @param rx_oflag The flag of that the serial port opens.
* @return Return the status of the operation.
*/
static rt_err_t rt_serial_tx_enable(struct rt_device *dev,
rt_uint16_t tx_oflag)
{
struct rt_serial_device *serial;
struct rt_serial_tx_fifo *tx_fifo = RT_NULL;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
if (serial->config.tx_bufsz == 0)
{
/* Cannot use RT_SERIAL_TX_NON_BLOCKING when tx_bufsz is 0 */
if (tx_oflag == RT_SERIAL_TX_NON_BLOCKING)
{
LOG_E("(%s) serial device with misconfigure: tx_bufsz = 0",
dev->parent.name);
return -RT_EINVAL;
}
#ifndef RT_USING_DEVICE_OPS
dev->write = _serial_poll_tx;
#endif
dev->open_flag |= RT_SERIAL_TX_BLOCKING;
return RT_EOK;
}
/* Limits the minimum value of tx_bufsz */
if (serial->config.tx_bufsz < RT_SERIAL_TX_MINBUFSZ)
serial->config.tx_bufsz = RT_SERIAL_TX_MINBUFSZ;
if (tx_oflag == RT_SERIAL_TX_BLOCKING)
{
/* When using RT_SERIAL_TX_BLOCKING, it is necessary to determine
* whether serial device needs to use buffer */
rt_err_t optmode; /* The operating mode used by serial device */
/* Call the Control() API to get the operating mode */
optmode = serial->ops->control(serial,
RT_DEVICE_CHECK_OPTMODE,
(void *)RT_DEVICE_FLAG_TX_BLOCKING);
if (optmode == RT_SERIAL_TX_BLOCKING_BUFFER)
{
/* If use RT_SERIAL_TX_BLOCKING_BUFFER, the ringbuffer is initialized */
tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
RT_ASSERT(tx_fifo != RT_NULL);
rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
rt_ringbuffer_init(&tx_fifo->rb,
(rt_uint8_t *)tx_fifo + sizeof(struct rt_serial_tx_fifo),
serial->config.tx_bufsz);
serial->serial_tx = tx_fifo;
#ifndef RT_USING_DEVICE_OPS
dev->write = _serial_fifo_tx_blocking_buf;
#endif
}
else
{
/* If not use RT_SERIAL_TX_BLOCKING_BUFFER,
* the control() API is called to configure the serial device */
tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo));
RT_ASSERT(tx_fifo != RT_NULL);
rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo));
/* Init rb.buffer_ptr to RT_NULL, in rt_serial_write() need check it
* otherwise buffer_ptr maybe a random value, as rt_malloc not init memory */
serial->serial_tx = tx_fifo;
#ifndef RT_USING_DEVICE_OPS
dev->write = _serial_fifo_tx_blocking_nbuf;
#endif
/* Call the control() API to configure the serial device by RT_SERIAL_TX_BLOCKING*/
serial->ops->control(serial,
RT_DEVICE_CTRL_CONFIG,
(void *)RT_SERIAL_TX_BLOCKING);
}
rt_atomic_flag_clear(&tx_fifo->activated);
tx_fifo->put_size = 0;
rt_atomic_store(&tx_fifo->tx_timeout, RT_WAITING_FOREVER);
rt_completion_init(&tx_fifo->tx_cpt);
dev->open_flag |= RT_SERIAL_TX_BLOCKING;
return RT_EOK;
}
/* When using RT_SERIAL_TX_NON_BLOCKING, ringbuffer needs to be initialized,
* and initialize the tx_fifo->activated value is RT_FALSE.
*/
tx_fifo = (struct rt_serial_tx_fifo *)rt_malloc(sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
RT_ASSERT(tx_fifo != RT_NULL);
rt_memset(tx_fifo, RT_NULL, sizeof(struct rt_serial_tx_fifo) + serial->config.tx_bufsz);
rt_ringbuffer_init(&tx_fifo->rb,
(rt_uint8_t *)tx_fifo + sizeof(struct rt_serial_tx_fifo),
serial->config.tx_bufsz);
serial->serial_tx = tx_fifo;
rt_atomic_flag_clear(&tx_fifo->activated);
tx_fifo->put_size = 0;
#ifndef RT_USING_DEVICE_OPS
dev->write = _serial_fifo_tx_nonblocking;
#endif
rt_completion_init(&tx_fifo->tx_cpt);
dev->open_flag |= RT_SERIAL_TX_NON_BLOCKING;
/* Call the control() API to configure the serial device by RT_SERIAL_TX_NON_BLOCKING*/
serial->ops->control(serial,
RT_DEVICE_CTRL_CONFIG,
(void *)RT_SERIAL_TX_NON_BLOCKING);
return RT_EOK;
}
/**
* @brief Enable serial receive mode.
* @param dev The pointer of device driver structure
* @param rx_oflag The flag of that the serial port opens.
* @return Return the status of the operation.
*/
static rt_err_t rt_serial_rx_enable(struct rt_device *dev,
rt_uint16_t rx_oflag)
{
struct rt_serial_device *serial;
struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
rt_size_t rx_fifo_size = 0;
RT_ASSERT(dev != RT_NULL);
serial = (struct rt_serial_device *)dev;
if (serial->config.rx_bufsz == 0)
{
/* Cannot use RT_SERIAL_RX_NON_BLOCKING when rx_bufsz is 0 */
if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
{
LOG_E("(%s) serial device with misconfigure: rx_bufsz = 0",
dev->parent.name);
return -RT_EINVAL;
}
#ifndef RT_USING_DEVICE_OPS
dev->read = _serial_poll_rx;
#endif
dev->open_flag |= RT_SERIAL_RX_BLOCKING;
return RT_EOK;
}
/* Limits the minimum value of rx_bufsz */
if (serial->config.rx_bufsz < RT_SERIAL_RX_MINBUFSZ)
serial->config.rx_bufsz = RT_SERIAL_RX_MINBUFSZ;
#ifdef RT_SERIAL_USING_DMA
if (serial->config.dma_ping_bufsz > 0 && serial->config.dma_ping_bufsz < RT_SERIAL_RX_MINBUFSZ / 2)
serial->config.dma_ping_bufsz = RT_SERIAL_RX_MINBUFSZ / 2;
rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz + serial->config.dma_ping_bufsz;
#else
rx_fifo_size = sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz;
#endif
rx_fifo = (struct rt_serial_rx_fifo *)rt_malloc(rx_fifo_size);
RT_ASSERT(rx_fifo != RT_NULL);
rt_memset(rx_fifo, RT_NULL, rx_fifo_size);
rt_ringbuffer_init(&rx_fifo->rb,
(rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo),
serial->config.rx_bufsz);
#ifdef RT_SERIAL_USING_DMA
if (serial->config.dma_ping_bufsz > 0)
{
rt_ringbuffer_init(&rx_fifo->dma_ping_rb,
(rt_uint8_t *)rx_fifo + sizeof(struct rt_serial_rx_fifo) + serial->config.rx_bufsz,
serial->config.dma_ping_bufsz);
}
#endif
serial->serial_rx = rx_fifo;
#ifndef RT_USING_DEVICE_OPS
dev->read = _serial_fifo_rx;
#endif
if (rx_oflag == RT_SERIAL_RX_NON_BLOCKING)
{
dev->open_flag |= RT_SERIAL_RX_NON_BLOCKING;
/* Call the control() API to configure the serial device by RT_SERIAL_RX_NON_BLOCKING*/
serial->ops->control(serial,
RT_DEVICE_CTRL_CONFIG,
(void *)RT_SERIAL_RX_NON_BLOCKING);
return RT_EOK;
}
/* When using RT_SERIAL_RX_BLOCKING, rt_completion_init() and rx_cpt_index are initialized */
rx_fifo->rx_cpt_index = 0;
rt_atomic_store(&rx_fifo->rx_timeout, RT_WAITING_FOREVER);
rt_completion_init(&rx_fifo->rx_cpt);
dev->open_flag |= RT_SERIAL_RX_BLOCKING;
/* Call the control() API to configure the serial device by RT_SERIAL_RX_BLOCKING*/
serial->ops->control(serial,
RT_DEVICE_CTRL_CONFIG,
(void *)RT_SERIAL_RX_BLOCKING);
return RT_EOK;
}