]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - i3-mote/i3-mote.git/blob - Basic-Test-Package/MSP432/Test_MSP432_DebugUART_EchoPC/uart_printf.c
MSP432 Test TI-RTOS
[i3-mote/i3-mote.git] / Basic-Test-Package / MSP432 / Test_MSP432_DebugUART_EchoPC / uart_printf.c
1 /**************************************************************************************************\r
2   Filename:       uart_printf.c\r
3 \r
4   Description:    This file contains the TI-RTOS hooks for printing to UART via\r
5                   System_printf(..).\r
6 \r
7                   This is a very basic implementation made for the purposes of\r
8                   terminal feedback in workshops, trainings and debug.\r
9 \r
10   Copyright 2015 Texas Instruments Incorporated. All rights reserved.\r
11 \r
12   IMPORTANT: Your use of this Software is limited to those specific rights\r
13   granted under the terms of a software license agreement between the user\r
14   who downloaded the software, his/her employer (which must be your employer)\r
15   and Texas Instruments Incorporated (the "License").  You may not use this\r
16   Software unless you agree to abide by the terms of the License. The License\r
17   limits your use, and you acknowledge, that the Software may not be modified,\r
18   copied or distributed unless embedded on a Texas Instruments microcontroller\r
19   or used solely and exclusively in conjunction with a Texas Instruments radio\r
20   frequency transceiver, which is integrated into your product.  Other than for\r
21   the foregoing purpose, you may not use, reproduce, copy, prepare derivative\r
22   works of, modify, distribute, perform, display or sell this Software and/or\r
23   its documentation for any purpose.\r
24 \r
25   YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE\r
26   PROVIDED �AS IS� WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,\r
27   INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,\r
28   NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL\r
29   TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,\r
30   NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER\r
31   LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES\r
32   INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE\r
33   OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT\r
34   OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES\r
35   (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.\r
36 \r
37   Should you have any questions regarding your right to use this Software,\r
38   contact Texas Instruments Incorporated at www.TI.com.\r
39 **************************************************************************************************/\r
40 \r
41 /*********************************************************************\r
42  * INCLUDES\r
43  */\r
44 #include <Board.h>\r
45 #include <ti/drivers/UART.h>\r
46 //#include <ti/drivers/uart/UARTCC26XX.h>\r
47 #include <stdint.h>\r
48 \r
49 /*********************************************************************\r
50  * CONSTANTS\r
51  */\r
52 #define UART_PRINTF_BUF_LEN      1024\r
53 \r
54 /*********************************************************************\r
55  * TYPEDEFS\r
56  */\r
57 \r
58 /*********************************************************************\r
59  * LOCAL VARIABLES\r
60  */\r
61 static uint8_t  uartPrintf_outArray[UART_PRINTF_BUF_LEN];\r
62 static uint16_t uartPrintf_head = 0;\r
63 static uint16_t uartPrintf_tail = 0;\r
64 static UART_Handle hUart = NULL;\r
65 \r
66 \r
67 /*********************************************************************\r
68  * PUBLIC FUNCTIONS\r
69  */\r
70 \r
71 /*********************************************************************\r
72  * @fn      UartPrintf_init\r
73  *\r
74  * @brief   Initializes the putchar hooks with the handle to the UART.\r
75  *\r
76  * @param   handle - UART driver handle to an initialized and opened UART.\r
77  *\r
78  * @return  None.\r
79  */\r
80 void UartPrintf_init(UART_Handle handle)\r
81 {\r
82         hUart = handle;\r
83 }\r
84 \r
85 /*********************************************************************\r
86  * SYSTEM HOOK FUNCTIONS\r
87  */\r
88 \r
89 /*********************************************************************\r
90  * @fn      uartPrintf_putch\r
91  *\r
92  * @brief   User supplied PutChar function.\r
93  *          typedef Void (*SysCallback_PutchFxn)(Char);\r
94  *\r
95  *          This function is called whenever the System module needs\r
96  *          to output a character.\r
97  *\r
98  *          This implementation fills a very basic ring-buffer, and relies\r
99  *          on another function to flush this buffer out to UART.\r
100  *\r
101  *          Requires SysCallback to be the system provider module.\r
102  *          Initialized via SysCallback.putchFxn = "&uartPrintf_putch"; in the\r
103  *          TI-RTOS configuration script.\r
104  *\r
105  * @param   ch - Character\r
106  *\r
107  * @return  None.\r
108  *\r
109  * @post    ::uartPrintf_head is incremented by one with wrap at UART_PRINTF_BUF_LEN\r
110  *          if there is room.\r
111  */\r
112 void uartPrintf_putch(char ch)\r
113 {\r
114     // uartPrintf_tail should never catch up with uartPrintf_head. Discard in-between bytes.\r
115         if ( (uartPrintf_head + 1) % UART_PRINTF_BUF_LEN == uartPrintf_tail )\r
116                 return;\r
117 \r
118         uartPrintf_outArray[uartPrintf_head] = ch;\r
119         uartPrintf_head++;\r
120 \r
121         if (uartPrintf_head >= UART_PRINTF_BUF_LEN)\r
122                 uartPrintf_head = 0;\r
123 }\r
124 \r
125 /*********************************************************************\r
126  * @fn      uartPrintf_flush\r
127  *\r
128  * @brief   Printf-buffer flush function\r
129  *\r
130  *          In this implementation it is intended to be called by the\r
131  *          Idle task when nothing else is running.\r
132  *\r
133  *          This is achieved by setting up the Idle task in the TI-RTOS\r
134  *          configuration script like so:\r
135  *\r
136  *          var Idle = xdc.useModule('ti.sysbios.knl.Idle');\r
137  *          Idle.addFunc('&uartPrintf_flush');\r
138  *\r
139  * @param   None. Relies on global state.\r
140  *\r
141  * @return  None.\r
142  *\r
143  * @post    ::uartPrintf_tail is incremented to where uartPrintf_head\r
144  *          was at the time the function was called.\r
145   */\r
146 void uartPrintf_flush()\r
147 {\r
148         // Abort in case UART hasn't been initialized.\r
149         if (NULL == hUart)\r
150                 return;\r
151 \r
152   // Lock head position to avoid race conditions\r
153   uint16_t curHead = uartPrintf_head;\r
154 \r
155   // Find out how much data must be output, and how to output it.\r
156   int needWrap = curHead < uartPrintf_tail;\r
157 \r
158   uint16_t outLen = needWrap?(UART_PRINTF_BUF_LEN-uartPrintf_tail+curHead):(curHead-uartPrintf_tail);\r
159 \r
160         if (outLen)\r
161         {\r
162                 if (needWrap)\r
163                 {\r
164                         UART_write(hUart, &uartPrintf_outArray[uartPrintf_tail], UART_PRINTF_BUF_LEN - uartPrintf_tail);\r
165                         UART_write(hUart, uartPrintf_outArray, curHead);\r
166                 }\r
167                 else\r
168                 {\r
169                         UART_write(hUart, &uartPrintf_outArray[uartPrintf_tail], outLen);\r
170                 }\r
171         }\r
172 \r
173         uartPrintf_tail = curHead;\r
174 }\r
175 \r
176 \r
177 char uart_getch()\r
178 {\r
179         char input;\r
180 \r
181         if (NULL == hUart)\r
182                 return 0;\r
183 \r
184         // Blocking Read\r
185         UART_read(hUart,&input,1);\r
186         return input;\r
187 }\r
188 \r
189 int uart_putch(char input)\r
190 {\r
191         if (NULL == hUart)\r
192                 return 0;\r
193 \r
194         UART_write(hUart,&input,1);\r
195         return 1;\r
196 }\r
197 \r
198 \r