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 /*----- FetBSL.c -----*/
39 #include "hw_compiler_specific.h"
40 
41 #include "msp430.h"
42 #include "..\FetCom.h"
43 #include "FetBsl.h"
44 #include "USB_API/USB_Common/types.h"          // Basic Type declarations
45 
46 static FET_USB_INFOS_t UsbPointers_;
47 
48 static unsigned long UartPreviousBaudrate = 0;
49 
50 // init function for UART port pins
51 // reset all portpins
FetBsl_PortsHds()52 void FetBsl_PortsHds()         // with handshake
53 {
54     uart_CtsOut();          // CTS output
55     uart_ClearCtsBit();     // clear CTS
56     uart_RtsIn();           // RTS input
57     uart_TxdSel();
58     uart_RxdSel();
59 }
60 
61 // initilaize FetBSL_functions
FetBsl_Init()62 void FetBsl_Init()
63 {
64     // Ports configuration
65     FetBsl_PortsHds();
66     UCA_IE_REGISTER |= UCRXIE;        // enable USCIAx interrupt
67 }
68 
FetBsl_SetUsb(FET_USB_INFOS_t * usb_Pointers)69 void FetBsl_SetUsb(FET_USB_INFOS_t* usb_Pointers)
70 {
71     UsbPointers_ = *usb_Pointers;
72 }
73 
FetBsl_Close()74 void FetBsl_Close()
75 {
76   TX_RX_PORT_SEL &= ~( TX_PORT_BIT | RX_PORT_BIT ); // deassign IO-Ports from UCAxTXD, UCAxRxD
77   TX_RX_PORT_DIR &= ~( TX_PORT_BIT | RX_PORT_BIT );
78   // set RTS as input
79   RTS_PORT_OUT &= ~RTS_PORT_BIT;
80   RTS_PORT_DIR &= ~RTS_PORT_BIT;
81   // set CTS as input
82   CTS_PORT_OUT &= ~CTS_PORT_BIT;
83   CTS_PORT_DIR &= ~CTS_PORT_BIT;
84   CTS_PORT_REN &= ~CTS_PORT_BIT;
85   com_RtsIfgClear();
86 }
87 
88 // configuration function for UART settings
89 // UART will be configure automatically about COM channel configuration
FetBsl_Config(unsigned long UartBaudrate)90 short FetBsl_Config(unsigned long UartBaudrate)
91 {
92     uart_ClearCtsBit();
93     if(UartBaudrate != UartPreviousBaudrate)    // baudrate is changed ?
94     {
95         UCA_CTRL_REGISTER |= UCSWRST;         // Put state maschine reset
96         UCA_CTRL_REGISTER |= UCSSEL__SMCLK;   // clock source: SMCLK
97 
98         if(UartBaudrate == 9600)
99         {
100             UCA1BR0 = 0x8F;
101             UCA1BR1 = 0x0;
102             UCA1MCTL = 0xE0 + UCOS16;
103         }
104         else if(UartBaudrate == 19200)
105         {
106             UCA1BR0 = 0x47;
107             UCA1BR1 = 0x0;
108             UCA1MCTL = 0xF0 + UCOS16;
109         }
110         else if(UartBaudrate == 28800)
111         {
112             UCA1BR0 = 0x2B;
113             UCA1BR1 = 0x0;
114             UCA1MCTL = 0x50 + UCOS16;
115         }
116         else if(UartBaudrate == 38400)
117         {
118             UCA1BR0 = 0x23;
119             UCA1BR1 = 0x0;
120             UCA1MCTL = 0x80 + UCOS16;
121         }
122         else if(UartBaudrate == 56000)
123         {
124             UCA1BR0 = 0x18;
125             UCA1BR1 = 0x0;
126             UCA1MCTL = 0x40 + UCOS16;
127         }
128         else if(UartBaudrate == 57600)
129         {
130             UCA1BR0 = 0x17;
131             UCA1BR1 = 0x0;
132             UCA1MCTL = 0xA0 + UCOS16;
133         }
134         else if(UartBaudrate == 115200)
135         {
136             UCA1BR0 = 0xB;
137             UCA1BR1 = 0x0;
138             UCA1MCTL = 0xD0 + UCOS16;
139         }
140         else if(UartBaudrate == 256000)
141         {
142             UCA1BR0 = 0x5;
143             UCA1BR1 = 0x0;
144             UCA1MCTL = 0xE0 + UCOS16;
145         }
146 
147         UCA1CTL0 =  UCPEN +  UCPAR;       //  parety even
148         UCA_CTRL_REGISTER &=~ UCSWRST;    // Initialize USCI state machine
149         UCA_IE_REGISTER |= UCRXIE;        // enable USCIAx interrupt
150 
151         UartPreviousBaudrate = UartBaudrate;
152 
153     }
154     return (0);
155 }
156 
FetBsl_NotBusy()157 short FetBsl_NotBusy()
158 {
159     if((UCA_STATUS_REGISTER & UCBUSY) == UCBUSY)   // when busy:
160     {
161         return (0);                     // return 0
162     }
163     return (1);                         // else return 1
164 }
165 
FetBsl_TxEmpty()166 short FetBsl_TxEmpty()
167 {
168     // UCTXIFG is set when UCAxTXBUF empty
169     if(UCTXIFG == (UCTXIFG & UCA_INTERRUPT_FLAG_REGISTER))
170     {
171         return (1);     // empty
172     }
173     return (0);         // not empty
174 }
175 
176 // functions copies byte by byte from USB buffer to UART buffer
FetBsl_Transmit(unsigned char uartDataToTxBuffer)177 short FetBsl_Transmit(unsigned char uartDataToTxBuffer)
178 {
179     UCA_TX_BUF = uartDataToTxBuffer;
180     return (0);
181 }
182 
FetBsl_Send()183 short FetBsl_Send()
184 {
185       // Start USB to UART bridge communication ----------------------------------
186     BYTE BytesInUsbBuffer = UsbPointers_.FetUSB_bytesInUSBBuffer(CDC0_DATA_INTERFACE);
187     if(BytesInUsbBuffer)
188     {
189         unsigned char i = 0;
190         BYTE uartDataToSend[260] = {0};
191         UsbPointers_.FetUSB_receiveData(uartDataToSend, BytesInUsbBuffer , CDC0_DATA_INTERFACE);
192         while(BytesInUsbBuffer)
193         {
194             if(FetBsl_NotBusy() && FetBsl_TxEmpty())
195             {
196                 FetBsl_Transmit(uartDataToSend[i++]);
197                 BytesInUsbBuffer--;
198             }
199         }
200     }
201     return 0;
202 }
203