1 /*
2 * FetUart.c
3 *
4 * Base class for all memory classes.
5 *
6 * Copyright (C) 2007 - 2011 Texas Instruments Incorporated - http://www.ti.com/
7 *
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the
19 * distribution.
20 *
21 * Neither the name of Texas Instruments Incorporated nor the names of
22 * its contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37 
38 /*----- FetUart.c -----*/
39 #include "hw_compiler_specific.h"
40 
41 #include "msp430.h"
42 #include "../FetCom.h"
43 #include "USB_API/USB_Common/types.h"          // Basic Type declarations
44 
45 static unsigned char RtsReady = 0;
46 static unsigned short HandshakeOn = 0;
47 
48 static FET_USB_INFOS_t UsbPointers_;
49 static unsigned long UartPreviousBaudrate = 0;
50 
51 static unsigned short *Semaphore_Add = (unsigned short*)COM_SEMAPHOREADRESS;
52 
53 static unsigned short parityEven = 0;
54 
Uart_SetCts()55 short Uart_SetCts()
56 {
57     uart_SetCtsBit();
58     *Semaphore_Add = 1;
59     return (1);
60 }
61 
Uart_ClearCts()62 short Uart_ClearCts()
63 {
64     uart_ClearCtsBit();     // clear CTS
65     *Semaphore_Add = 0;
66     return (0);
67 }
68 
69 // init function for UART port pins
70 // reset all portpins
Uart_PortsHds()71 void Uart_PortsHds()         // with handshake
72 {
73     uart_CtsOut();          // CTS output
74     Uart_ClearCts();     // clear CTS
75     uart_RtsIn();           // RTS input3
76 
77     //uart_RtsRen();          // enable pull up resistor on RTS
78     uart_TxdSel();
79     uart_RxdSel();
80 }
81 
Uart_EnableRtsPullUp()82 void Uart_EnableRtsPullUp()
83 {
84   RTS_PULLUP_REN |= RTS_PULLUP_BIT;
85   RTS_PULLUP_OUT |= RTS_PULLUP_BIT;
86   rtsSetPullUpDir();
87 }
88 
UART_DisableHandshake()89 void UART_DisableHandshake()
90 {
91     HandshakeOn = 0;
92     RtsReady = 1;
93     com_RtsInterruptDisable();
94     Uart_EnableRtsPullUp();
95 }
96 
UART_EnableHandshake()97 void UART_EnableHandshake()
98 {
99     HandshakeOn = 1;
100     RtsReady = 0;
101     com_RtsIes();          // RTS Interrupt edge select
102     com_RtsIe();           // RTS Interrupt enable
103     com_RtsIfgClear();     // RTS Interrupt flag clear
104     com_RtsIfgSet();       // RTS Interrupt flag set (required for sending first byte)
105 }
106 
107 // initilaize UART functions
Uart_Init(unsigned short mode,unsigned short parity)108 void Uart_Init(unsigned short mode, unsigned short parity)
109 {
110     if(mode == UART_NO_HANDSHAKE)
111     {
112         UART_DisableHandshake();
113     }
114     if(mode == UART_HANDSHAKE)
115     {
116         UART_EnableHandshake();
117     }
118     // Ports configuration
119     Uart_PortsHds();
120     UCA_IE_REGISTER |= UCRXIE;        // enable USCIAx interrupt
121 
122     parityEven = parity;
123 }
124 
Uart_SetUsb(FET_USB_INFOS_t * usb_Pointers)125 void Uart_SetUsb(FET_USB_INFOS_t* usb_Pointers)
126 {
127     UsbPointers_ = *usb_Pointers;
128 }
129 
Uart_Close()130 void Uart_Close()
131 {
132   TX_RX_PORT_SEL &= ~( TX_PORT_BIT | RX_PORT_BIT ); // deassign IO-Ports from UCAxTXD, UCAxRxD
133   TX_RX_PORT_DIR &= ~( TX_PORT_BIT | RX_PORT_BIT );
134   // set RTS as input
135   RTS_PORT_OUT &= ~RTS_PORT_BIT;
136   RTS_PORT_DIR &= ~RTS_PORT_BIT;
137   // set CTS as input
138   CTS_PORT_OUT &= ~CTS_PORT_BIT;
139   CTS_PORT_DIR &= ~CTS_PORT_BIT;
140   CTS_PORT_REN &= ~CTS_PORT_BIT;
141 
142   com_RtsIfgClear();
143 }
144 
UART_SetRts()145 void UART_SetRts()
146 {
147     RtsReady = 1;
148 }
149 
150 // configuration function for UART settings
151 // UART will be configure automatically about COM channel configuration
Uart_Config(unsigned long UartBaudrate)152 short Uart_Config(unsigned long UartBaudrate)
153 {
154     Uart_ClearCts();
155     if(UartBaudrate != UartPreviousBaudrate)   // baudrate is changed ?
156     {
157         UCA_CTRL_REGISTER |= UCSWRST;         // Put state maschine reset
158         UCA_CTRL_REGISTER |= UCSSEL__SMCLK;   // clock source: SMCLK
159 
160         if(UartBaudrate == 4800)
161         {
162             #ifdef eZ_FET
163                 UCA_BR0 = 0x45;
164                 UCA_BR1 = 0x1;
165                 UCA_MOD_CTRL_REGISTER = 0x80 + UCOS16;
166             #endif
167             #ifdef MSP_FET
168                 UCA_BR0 = 0x1e;
169                 UCA_BR1 = 0x1;
170                 UCA_MOD_CTRL_REGISTER = 0xC0 + UCOS16;
171             #endif
172         }
173         if(UartBaudrate == 9600)
174         {
175             #ifdef eZ_FET
176                 UCA_BR0 = 0xA2;
177                 UCA_BR1 = 0x0;
178                 UCA_MOD_CTRL_REGISTER = 0xC0 + UCOS16;
179             #endif
180             #ifdef MSP_FET
181                 UCA_BR0 = 0x8F;
182                 UCA_BR1 = 0x0;
183                 UCA_MOD_CTRL_REGISTER = 0xE0 + UCOS16;
184             #endif
185         }
186         else if(UartBaudrate == 14400)
187         {
188             #ifdef eZ_FET
189                 UCA_BR0 = 0x6C;
190                 UCA_BR1 = 0x0;
191                 UCA_MOD_CTRL_REGISTER = 0x80 + UCOS16;
192             #endif
193             #ifdef MSP_FET
194                 UCA_BR0 = 0x5F;
195                 UCA_BR1 = 0x0;
196                 UCA_MOD_CTRL_REGISTER =  0x90 + UCOS16;
197             #endif
198         }
199         else if(UartBaudrate == 19200)
200         {
201             #ifdef eZ_FET
202                 UCA_BR0 = 0x51;
203                 UCA_BR1 = 0x0;
204                 UCA_MOD_CTRL_REGISTER = 0x60 + UCOS16;
205             #endif
206             #ifdef MSP_FET
207                 UCA_BR0 = 0x47;
208                 UCA_BR1 = 0x0;
209                 UCA_MOD_CTRL_REGISTER = 0xF0 + UCOS16;
210             #endif
211         }
212         else if(UartBaudrate == 28800)
213         {
214             #ifdef eZ_FET
215                 UCA_BR0 = 0x36;
216                 UCA_BR1 = 0x0;
217                 UCA_MOD_CTRL_REGISTER = 0x40 + UCOS16;
218             #endif
219             #ifdef MSP_FET
220                 UCA_BR0 = 0x2F;
221                 UCA_BR1 = 0x0;
222                 UCA_MOD_CTRL_REGISTER = 0x50 + UCOS16;
223             #endif
224         }
225         else if(UartBaudrate == 38400)
226         {
227             #ifdef eZ_FET
228                 UCA_BR0 = 0x28;
229                 UCA_BR1 = 0x0;
230                 UCA_MOD_CTRL_REGISTER = 0xB0 + UCOS16;
231             #endif
232             #ifdef MSP_FET
233                 UCA_BR0 = 0x23;
234                 UCA_BR1 = 0x0;
235                 UCA_MOD_CTRL_REGISTER = 0x80 + UCOS16;
236             #endif
237         }
238         else if(UartBaudrate == 56000)
239         {
240             #ifdef eZ_FET
241                 UCA_BR0 =0x1B;
242                 UCA_BR1 = 0x0;
243                 UCA_MOD_CTRL_REGISTER = 0xE0 + UCOS16;
244             #endif
245             #ifdef MSP_FET
246                 UCA_BR0 = 0x18;
247                 UCA_BR1 = 0x0;
248                 UCA_MOD_CTRL_REGISTER = 0x40 + UCOS16;
249             #endif
250         }
251         else if(UartBaudrate == 57600)
252         {
253             #ifdef eZ_FET
254                 UCA_BR0 = 0x1B;
255                 UCA_BR1 = 0x0;
256                 UCA_MOD_CTRL_REGISTER = 0x20  + UCOS16;
257             #endif
258             #ifdef MSP_FET
259                 UCA_BR0 = 0x17;
260                 UCA_BR1 = 0x0;
261                 UCA_MOD_CTRL_REGISTER = 0xA0 + UCOS16;
262             #endif
263         }
264         else if(UartBaudrate == 115200)
265         {
266             #ifdef eZ_FET
267                 UCA_BR0 = 0xD;
268                 UCA_BR1 = 0x0;
269                 UCA_MOD_CTRL_REGISTER = 0x90  + UCOS16;
270             #endif
271             #ifdef MSP_FET
272                 UCA_BR0 = 0xB;
273                 UCA_BR1 = 0x0;
274                 UCA_MOD_CTRL_REGISTER = 0xD0 + UCOS16;
275             #endif
276         }
277         else if(UartBaudrate == 256000)
278         {
279            #ifdef eZ_FET
280                 UCA_BR0 = 0x6;
281                 UCA_BR1 = 0x0;
282                 UCA_MOD_CTRL_REGISTER = 0x4 + UCOS16;
283             #endif
284             #ifdef MSP_FET
285                 UCA_BR0 = 0x5;
286                 UCA_BR1 = 0x0;
287                 UCA_MOD_CTRL_REGISTER = 0xE0 + UCOS16;
288             #endif
289         }
290 
291         if(parityEven == PARITY_EVEN)
292         {
293             UCA_CTRL0_REGISTER |= UCPEN | UCPAR;
294         }
295         else
296         {
297             UCA_CTRL0_REGISTER &= ~(UCPEN | UCPAR);
298             UCA_CTRL_REGISTER = 0;
299             UCA_CTRL_REGISTER |= UCSSEL__SMCLK;   // clock source: SMCLK
300         }
301 
302         UCA_CTRL_REGISTER &=~ UCSWRST;    // Initialize USCI state machine
303         UCA_IE_REGISTER |= UCRXIE;        // enable USCIAx interrupt
304 
305         UartPreviousBaudrate = UartBaudrate;
306     }
307     Uart_SetCts();
308     return (0);
309 }
310 
Uart_GetRts()311 short Uart_GetRts()
312 {
313     if(uart_RtsStatus())    // get RTS status from Target
314     {
315         return (1);
316     }
317     else
318     {
319         return (0);
320     }
321 }
322 
Uart_NotBusy()323 short Uart_NotBusy()
324 {
325     if((UCA_STATUS_REGISTER & UCBUSY) == UCBUSY)   // when busy:
326     {
327         return (0);                     // return 0
328     }
329     return (1);                         // else return 1
330 }
331 
Uart_TxEmpty()332 short Uart_TxEmpty()
333 {
334     // UCTXIFG is set when UCAxTXBUF empty
335     if(UCTXIFG == (UCTXIFG & UCA_INTERRUPT_FLAG_REGISTER))
336     {
337         return (1);     // empty
338     }
339     return (0);         // not empty
340 }
341 
Uart_Send()342 short Uart_Send()
343 {
344     BYTE BytesInUsbBuffer = UsbPointers_.FetUSB_bytesInUSBBuffer(CDC0_DATA_INTERFACE);
345     if(BytesInUsbBuffer)
346     {
347         if(Uart_NotBusy() && Uart_TxEmpty() && RtsReady)
348         {
349             if(HandshakeOn == 1)
350             {
351                 // just check for toggle of RTS line if Handshake is used.
352                 RtsReady = 0;
353             }
354             // Start USB to UART bridge communication ----------------------------------
355             BYTE uartDataToSend = 0;
356             UsbPointers_.FetUSB_receiveData(&uartDataToSend, 1 , CDC0_DATA_INTERFACE);
357             UCA_TX_BUF = uartDataToSend;
358 
359         }
360     }
361     return 0;
362 }
363