]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/lufa/LUFA-git/LUFA/Drivers/Peripheral/AVR8/Serial_AVR8.c
Merge commit '657d9f23fe47fb88cf221adb23095082f191ba6a'
[max/tmk_keyboard.git] / tmk_core / protocol / lufa / LUFA-git / LUFA / Drivers / Peripheral / AVR8 / Serial_AVR8.c
1 /*
2              LUFA Library
3      Copyright (C) Dean Camera, 2014.
4
5   dean [at] fourwalledcubicle [dot] com
6            www.lufa-lib.org
7 */
8
9 /*
10   Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12   Permission to use, copy, modify, distribute, and sell this
13   software and its documentation for any purpose is hereby granted
14   without fee, provided that the above copyright notice appear in
15   all copies and that both that the copyright notice and this
16   permission notice and warranty disclaimer appear in supporting
17   documentation, and that the name of the author not be used in
18   advertising or publicity pertaining to distribution of the
19   software without specific, written prior permission.
20
21   The author disclaims all warranties with regard to this
22   software, including all implied warranties of merchantability
23   and fitness.  In no event shall the author be liable for any
24   special, indirect or consequential damages or any damages
25   whatsoever resulting from loss of use, data or profits, whether
26   in an action of contract, negligence or other tortious action,
27   arising out of or in connection with the use or performance of
28   this software.
29 */
30
31 #include "../../../Common/Common.h"
32 #if (ARCH == ARCH_AVR8)
33
34 #define  __INCLUDE_FROM_SERIAL_C
35 #include "../Serial.h"
36
37 FILE USARTSerialStream;
38
39 int Serial_putchar(char DataByte,
40                    FILE *Stream)
41 {
42         (void)Stream;
43
44         Serial_SendByte(DataByte);
45         return 0;
46 }
47
48 int Serial_getchar(FILE *Stream)
49 {
50         (void)Stream;
51
52         if (!(Serial_IsCharReceived()))
53           return _FDEV_EOF;
54
55         return Serial_ReceiveByte();
56 }
57
58 int Serial_getchar_Blocking(FILE *Stream)
59 {
60         (void)Stream;
61
62         while (!(Serial_IsCharReceived()));
63         return Serial_ReceiveByte();
64 }
65
66 void Serial_SendString_P(const char* FlashStringPtr)
67 {
68         uint8_t CurrByte;
69
70         while ((CurrByte = pgm_read_byte(FlashStringPtr)) != 0x00)
71         {
72                 Serial_SendByte(CurrByte);
73                 FlashStringPtr++;
74         }
75 }
76
77 void Serial_SendString(const char* StringPtr)
78 {
79         uint8_t CurrByte;
80
81         while ((CurrByte = *StringPtr) != 0x00)
82         {
83                 Serial_SendByte(CurrByte);
84                 StringPtr++;
85         }
86 }
87
88 void Serial_SendData(const void* Buffer,
89                      uint16_t Length)
90 {
91         while (Length--)
92           Serial_SendByte(*((uint8_t*)Buffer++));
93 }
94
95 void Serial_CreateStream(FILE* Stream)
96 {
97         if (!(Stream))
98         {
99                 Stream = &USARTSerialStream;
100                 stdin  = Stream;
101                 stdout = Stream;
102         }
103
104         *Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar, _FDEV_SETUP_RW);
105 }
106
107 void Serial_CreateBlockingStream(FILE* Stream)
108 {
109         if (!(Stream))
110         {
111                 Stream = &USARTSerialStream;
112                 stdin  = Stream;
113                 stdout = Stream;
114         }
115
116         *Stream = (FILE)FDEV_SETUP_STREAM(Serial_putchar, Serial_getchar_Blocking, _FDEV_SETUP_RW);
117 }
118
119 #endif