1 /*
2 * Com_Base.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 /*----- FetBslI2c.c -----*/
39 #include "hw_compiler_specific.h"
40 #include "msp430.h"
41 #include "../FetCom.h"
42 #include "USB_API/USB_Common/types.h"          // Basic Type declarations
43 #include "../BSL/FetBsl.h"
44 #include "../Uart/Uart.h"
45 #include "../../fw/fet/FetVersion.h"
46 
47 
48 #define USB_TIMEOUT 10
49 #define EJECT 1500
50 
51 static unsigned short RxFifoPosition;
52 static unsigned char *RxActiveBuffer;
53 static unsigned char RxBufferFront[COM_FIFOSIZE]={0};
54 static unsigned char RxBufferBack[COM_FIFOSIZE]={0};
55 static unsigned char FrontBufferUsed = 1;
56 static unsigned long PreviousBaudrate = 0;
57 
58 static unsigned short *Semaphore_Add = (unsigned short*)COM_SEMAPHOREADRESS;
59 
60 static const unsigned long com_Signature_ @ "UARTSIGNATURE" = COM_SIGNATURE;
61 #pragma required = com_Signature_
62 
63 static const unsigned int com_LayerVersion_ @ "UARTLAYERVERSION" = COM_LAYER_VERSION;
64 static const unsigned int com_LayerVersionCmp_ @ "UARTLAYERVERSIONCMP" = COM_LAYER_VERSION_CMP;
65 
66 
67 #pragma required = com_LayerVersion_
68 #pragma required = com_LayerVersionCmp_
69 
70 static COM_INFOS_t comInfos_;
71 static edt_common_methods_t hilPointers_;
72 static DCDC_INFOS_t dcdcPointers_;
73 
74 static FET_USB_INFOS_t UsbPointers_;
75 
76 static unsigned short iBytesReceived = 0;
77 static unsigned short EjectCounter = 0;
78 
79 // Dummy functions loaded by default
80 #ifdef MSP_FET
COM_BASE_ConfigModeDummy(unsigned long UartBaudrate)81     static short COM_BASE_ConfigModeDummy(unsigned long UartBaudrate){return 0;}
COM_BASE_SetRtsDummy()82     static void COM_BASE_SetRtsDummy(){return;}
83 #endif
COM_BASE_SetCtsDummy()84 static short COM_BASE_SetCtsDummy(){return 0;}
85 
86 
87 // initilaize UART functions
COM_BASE_Init(COM_INFOS_t * comInfos_Pointer)88 void COM_BASE_Init(COM_INFOS_t* comInfos_Pointer)
89 {
90     // map function pointers in FET UART layer
91     // get Infos about UART-module
92     comInfos_.comGetLayerVersion      = COM_BASE_GetLayerVersion;
93     comInfos_.comConfig               = COM_BASE_Config;
94     comInfos_.comReceive              = COM_BASE_Receive;
95     comInfos_.comSetHil               = COM_BASE_SetHil;
96     comInfos_.comSetDcdc              = COM_BASE_SetDcdc;
97     comInfos_.comSetUSB               = COM_BASE_SetUsb;
98     comInfos_.comLoop                 = COM_BASE_Loop;
99     comInfos_.comClose                = COM_BASE_Close;
100 
101     // set CTS to dummy - only needed for UART mode with handshake
102     comInfos_.comSetCts               = COM_BASE_SetCtsDummy;
103     comInfos_.comClearCts             = COM_BASE_SetCtsDummy;
104     comInfos_.comSetRts               = UART_SetRts;
105 
106     // UART without handshake is configured  by default
107     comInfos_.comTransmit             = Uart_Send;
108     comInfos_.comConfigMode           = Uart_Config;
109 
110     comInfos_.comGetLayerVersionCmp   = COM_BASE_GetLayerVersionCmp;
111 
112     Uart_Init(UART_NO_HANDSHAKE, PARITY_NONE);
113 
114      // init Rx FIFO pointer
115     RxFifoPosition = 0;
116     RxActiveBuffer = RxBufferFront;
117     FrontBufferUsed = 1;
118 
119     // now copy getLayerVersion and return it to uper core layer
120     *comInfos_Pointer =  comInfos_;
121 }
122 
COM_BASE_SetHil(edt_common_methods_t * hil_Pointers)123 void COM_BASE_SetHil(edt_common_methods_t* hil_Pointers)
124 {
125     hilPointers_ = *hil_Pointers;
126 
127 #ifdef MSP_FET
128     FetBslI2c_SetHil(hil_Pointers);
129 #endif
130 }
131 
COM_BASE_SetDcdc(DCDC_INFOS_t * dcdc_Pointers)132 void COM_BASE_SetDcdc(DCDC_INFOS_t* dcdc_Pointers)
133 {
134     dcdcPointers_ = *dcdc_Pointers;
135 }
136 
COM_BASE_SetUsb(FET_USB_INFOS_t * usb_Pointers)137 void COM_BASE_SetUsb(FET_USB_INFOS_t* usb_Pointers)
138 {
139     UsbPointers_ = *usb_Pointers;
140     Uart_SetUsb(usb_Pointers);
141 
142 #ifdef MSP_FET
143     FetBslI2c_SetUsb(usb_Pointers);
144     FetBsl_SetUsb(usb_Pointers);
145 #endif
146 }
147 
COM_BASE_Close()148 void COM_BASE_Close()
149 {
150     RxFifoPosition = 0;
151     RxActiveBuffer = RxBufferFront;
152     FrontBufferUsed = 1;
153     Uart_Close();
154 
155 #ifdef MSP_FET
156     FetBsl_Close();
157 #endif
158 }
159 
160 // return UART layer version
COM_BASE_GetLayerVersion()161 short COM_BASE_GetLayerVersion()
162 {
163     return (COM_LAYER_VERSION);
164 }
165 
COM_BASE_GetLayerVersionCmp()166 short COM_BASE_GetLayerVersionCmp()
167 {
168     return (COM_LAYER_VERSION_CMP);
169 }
170 unsigned short fetTypeInt = 0;
171 
172 #pragma optimize = low
COM_BASE_EvaluateBaudRateCommand(unsigned long Baudrate,unsigned short fetType)173 void COM_BASE_EvaluateBaudRateCommand(unsigned long Baudrate, unsigned short fetType)
174 {
175     *Semaphore_Add = 0;
176     fetTypeInt = fetType;
177     switch(Baudrate)
178     {
179         case COM_CLOSE:
180         case BSL_DISABLE:
181         {
182 #ifdef MSP_FET
183             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_HIGH_Z_UART);
184 #endif
185             COM_BASE_Close();
186             break;
187         }
188         case COM_POWER_DOWN:
189         {
190             hilPointers_.SetVcc(0);
191 #ifdef MSP_FET
192             hilPointers_.SwitchVccFET(0);
193 #endif
194             break;
195         }
196         case COM_POWER_UP:
197         {
198             dcdcPointers_.dcdcSetVcc(3300);
199             hilPointers_.SetVcc(3300);
200 #ifdef MSP_FET
201             hilPointers_.SwitchVccFET(1);
202 #endif
203             break;
204         }
205         case UART_NO_HANDSHAKE:
206         case UART_NO_HANDSHAKE_PARITY_EVEN:
207         {
208             // UART
209 #ifdef MSP_FET
210             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_UART);
211 #endif
212             comInfos_.comTransmit = Uart_Send;
213             comInfos_.comConfigMode = Uart_Config;
214             //configure FET  to work without handshake - reset to defaults
215             comInfos_.comSetCts = COM_BASE_SetCtsDummy;
216             comInfos_.comClearCts = COM_BASE_SetCtsDummy;
217             if(Baudrate == UART_NO_HANDSHAKE_PARITY_EVEN)
218             {
219                 Uart_Init(UART_NO_HANDSHAKE, PARITY_EVEN);
220             }
221             else
222             {
223                 Uart_Init(UART_NO_HANDSHAKE, PARITY_NONE);
224             }
225             break;
226         }
227         case UART_HANDSHAKE:
228         {
229 #ifdef MSP_FET
230             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_UART);
231 #endif
232             comInfos_.comTransmit = Uart_Send;
233             comInfos_.comConfigMode = Uart_Config;
234 
235             if(fetTypeInt == eZ_FET_WITH_DCDC_NO_FLOWCTL || fetTypeInt == eZ_FET_WITH_DCDC_V2x)
236             {
237                 //configure FET  to work without handshake - reset to defaults
238                 comInfos_.comSetCts = COM_BASE_SetCtsDummy;
239                 comInfos_.comClearCts = COM_BASE_SetCtsDummy;
240                 Uart_Init(UART_NO_HANDSHAKE, PARITY_NONE);
241             }
242             else
243             {
244                 //configure FET UART to work with handshake
245                 comInfos_.comSetCts = Uart_SetCts;
246                 comInfos_.comClearCts  = Uart_ClearCts;
247                 Uart_Init(UART_HANDSHAKE, PARITY_NONE);
248                 *Semaphore_Add = comInfos_.comSetCts();
249             }
250             break;
251         }
252 
253 #ifdef MSP_FET
254         case BSL_UART_INVOKE_SEQUENCE:
255         case BSL_UART_SEQUENCE:
256         {
257             //configure FET  to work without handshake - reset to defaults
258             comInfos_.comSetCts = COM_BASE_SetCtsDummy;
259             comInfos_.comClearCts = COM_BASE_SetCtsDummy;
260             comInfos_.comSetRts = COM_BASE_SetRtsDummy;
261 
262             FetBsl_Init();
263             // Ports configuration
264             dcdcPointers_.dcdcSetVcc(3300);
265             hilPointers_.SetVcc(3300);
266             hilPointers_.SwitchVccFET(1);
267             hilPointers_.SetProtocol(2);
268             // UART
269             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_UART);
270             if(BSL_UART_SEQUENCE != Baudrate)
271             {
272               hilPointers_.BSL_EntrySequence(0);
273             }
274             // prtocol specific functions
275             comInfos_.comTransmit = FetBsl_Send;
276             comInfos_.comConfigMode = FetBsl_Config;
277             break;
278         }
279         case BSL_I2C_INVOKE_SEQUENCE1:
280         case BSL_I2C_INVOKE_SEQUENCE2:
281         case BSL_I2C_INVOKE_SEQUENCE3:
282         case BSL_I2C_INVOKE_SEQUENCE4:
283         case BSL_I2C_SEQUENCE1:
284         case BSL_I2C_SEQUENCE2:
285         {
286             //configure FET  to work without handshake - reset to defaults
287             comInfos_.comSetCts = COM_BASE_SetCtsDummy;
288             comInfos_.comClearCts = COM_BASE_SetCtsDummy;
289             comInfos_.comSetRts = COM_BASE_SetRtsDummy;
290 
291             // Ports configuration
292             dcdcPointers_.dcdcSetVcc(3300);
293             hilPointers_.SetVcc(3300);
294             hilPointers_.SwitchVccFET(1);
295             hilPointers_.SetProtocol(2);
296             //I2C config
297             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_HIGH_Z_UART);
298             hilPointers_.ConfigFpgaIoMode(IO_CONFIG_I2C);
299             if(BSL_I2C_SEQUENCE1 != Baudrate && BSL_I2C_SEQUENCE2 != Baudrate)
300             {
301               hilPointers_.BSL_EntrySequence(0);
302             }
303             FetBslI2c_Init();
304             // prtocol specific functions
305             comInfos_.comTransmit = FetBslI2c_Send;
306             comInfos_.comConfigMode = COM_BASE_ConfigModeDummy;
307             break;
308         }
309 #endif
310         default:
311         {
312             if(Baudrate != BSL_I2C_INVOKE_SEQUENCE2 && Baudrate != BSL_I2C_INVOKE_SEQUENCE1 &&
313                Baudrate != BSL_UART_INVOKE_SEQUENCE && Baudrate != UART_HANDSHAKE &&
314                Baudrate != UART_NO_HANDSHAKE && Baudrate !=  COM_POWER_UP &&
315                Baudrate != COM_CLOSE && Baudrate != COM_POWER_DOWN &&
316                Baudrate != BSL_I2C_INVOKE_SEQUENCE3 &&
317                Baudrate != BSL_I2C_INVOKE_SEQUENCE4 && Baudrate != BSL_I2C_SEQUENCE1 &&
318                Baudrate != BSL_I2C_SEQUENCE2 && Baudrate != BSL_UART_SEQUENCE )
319             {
320                 // Uart without handshake is set as default
321             #ifdef MSP_FET
322                 hilPointers_.ConfigFpgaIoMode(IO_CONFIG_UART);
323             #endif
324             }
325         }
326     }
327 }
328 
329 // configuration function for UART settings
330 // UART will be configure automatically about COM channel configuration
COM_BASE_Config(unsigned long Baudrate,unsigned long MCLK_Frequency,unsigned short fetType)331 short COM_BASE_Config(unsigned long Baudrate, unsigned long MCLK_Frequency,
332                       unsigned short fetType)
333 {
334     if(Baudrate != PreviousBaudrate)    // baudrate changed ?
335     {
336        COM_BASE_EvaluateBaudRateCommand(Baudrate, fetType);
337        comInfos_.comConfigMode(Baudrate);
338        PreviousBaudrate = Baudrate;
339     }
340     return (0);
341 }
342 
COM_BASE_SwitchBuffer()343 void COM_BASE_SwitchBuffer()
344 {
345     RxFifoPosition = 0;
346     if(FrontBufferUsed == 1)
347     {
348           RxActiveBuffer = RxBufferBack;
349           FrontBufferUsed = 0;
350     }
351     else
352     {
353           RxActiveBuffer = RxBufferFront;
354           FrontBufferUsed = 1;
355     }
356 }
357 
COM_BASE_GetBuffer()358 unsigned char *COM_BASE_GetBuffer()
359 {
360     unsigned char *activePtr = 0;
361     if(FrontBufferUsed == 0)
362     {
363           activePtr = RxBufferFront;
364     }
365     else
366     {
367           activePtr = RxBufferBack;
368     }
369     return activePtr;
370 }
371 
COM_BASE_Receive(unsigned char character)372 short COM_BASE_Receive(unsigned char character)
373 {
374     *Semaphore_Add = comInfos_.comClearCts();
375     if(RxFifoPosition != COM_FIFOSIZE)
376     {
377         RxActiveBuffer[RxFifoPosition++] = character;
378         iBytesReceived++;
379     }
380     else
381     {
382         COM_BASE_SwitchBuffer();
383         RxActiveBuffer[RxFifoPosition++] = character;
384         iBytesReceived = 1;
385         EjectCounter = 0;
386         UsbPointers_.FetUsb_CdcSendDataInBackground(COM_BASE_GetBuffer(), COM_FIFOSIZE, CDC0_DATA_INTERFACE, USB_TIMEOUT);
387     }
388     *Semaphore_Add = comInfos_.comSetCts();
389     return 0;
390 }
391 
COM_BASE_Loop()392 void COM_BASE_Loop()
393 {
394     comInfos_.comTransmit();
395     // Start USB to Host communication (Uart)-----------------------------------
396     if(iBytesReceived > 0)
397     {
398         EjectCounter++;
399         if(EjectCounter >= EJECT)
400         {
401             *Semaphore_Add = comInfos_.comClearCts();
402             UCA_IE_REGISTER &= ~UCRXIE;        // disable USCIAx interrupt
403             unsigned short tmpbyte = iBytesReceived;
404             iBytesReceived = 0;
405             COM_BASE_SwitchBuffer();
406             BYTE sendResult = UsbPointers_.FetUsb_CdcSendDataInBackground(COM_BASE_GetBuffer(), tmpbyte, CDC0_DATA_INTERFACE, 10);
407             if( sendResult == 0)
408             {
409                 EjectCounter = 0;
410             }
411             UCA_IE_REGISTER |= UCRXIE;        // enable USCIAx interrupt
412             *Semaphore_Add = comInfos_.comSetCts();
413         }
414     }
415     // End USB to Host communication (Uart)-------------------------------------
416 }
417