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 /*----- FetBslI2c.c -----*/
39 #include "hw_compiler_specific.h"
40
41 #include "msp430.h"
42 #include "..\FetCom.h"
43 #include "I2C\I2C.h"
44 #include "USB_API/USB_Common/types.h" // Basic Type declarations
45 #include "FetBsl.h"
46
47 static FET_USB_INFOS_t UsbPointers_;
48 static edt_common_methods_t hilPointers_;
49
50 // initilaize FetBSL_functions
FetBslI2c_Init()51 void FetBslI2c_Init()
52 {
53 com_RtsInterruptDisable();
54 P8SEL &= ~BIT3; // de-assign P3.3 from UCA0TXD to GPIO
55 P8SEL &= ~BIT2; // de-assign P3.4 from UCA0RXD to GPIO
56 I2C_init();
57 }
58
FetBslI2c_SetUsb(FET_USB_INFOS_t * usb_Pointers)59 void FetBslI2c_SetUsb(FET_USB_INFOS_t* usb_Pointers)
60 {
61 UsbPointers_ = *usb_Pointers;
62 }
63
FetBslI2c_SetHil(edt_common_methods_t * hil_Pointers)64 void FetBslI2c_SetHil(edt_common_methods_t* hil_Pointers)
65 {
66 hilPointers_ = *hil_Pointers;
67 }
68
FetBslI2c_Send()69 short FetBslI2c_Send()
70 {
71 _DINT_FET()
72 static short isFirstMessage = 1, isLastMessage = 0;
73 static unsigned short dataLength = 0;
74 // Start USB to I2C bridge communication ----------------------------------
75 BYTE BytesInUsbBuffer = UsbPointers_.FetUSB_bytesInUSBBuffer(CDC0_DATA_INTERFACE);
76 if(BytesInUsbBuffer)
77 {
78 BYTE i2cDataToSend[BSL_MAX_DATA_LENGTH] = {0};
79 BYTE returnData[BSL_MAX_DATA_LENGTH] = {0};
80 unsigned short readLength = 0;
81
82 UsbPointers_.FetUSB_receiveData(i2cDataToSend, BytesInUsbBuffer , 1);
83 if(isFirstMessage)
84 {
85 dataLength = (i2cDataToSend[2] << 8 | i2cDataToSend[1]);
86 isFirstMessage = 0;
87 dataLength = dataLength + OFFSET_BSL_HEADER + OFFSET_BSL_CRC - BytesInUsbBuffer;
88 I2C_writeData(I2C_SLAVE_ADDRESS ,i2cDataToSend, BytesInUsbBuffer, I2C_NO_STOP, I2C_START); // start write
89 }
90 else
91 {
92 dataLength = dataLength - BytesInUsbBuffer;
93 I2C_writeData(I2C_SLAVE_ADDRESS,i2cDataToSend, BytesInUsbBuffer, I2C_NO_STOP, I2C_NO_START);// continue write
94 }
95
96 if(dataLength == 0)
97 {
98 isLastMessage = 1;
99 }
100
101 if(isLastMessage)
102 {
103 unsigned int i = 0;
104 hilPointers_.Delay_1ms(2);
105 I2C_readData(I2C_SLAVE_ADDRESS, returnData, &readLength, I2C_REPEATED_START);
106 for(i = 0; i < readLength; i++)
107 {
108 COM_BASE_Receive(returnData[i]);
109 }
110 isFirstMessage = 1;
111 isLastMessage = 0;
112 }
113 }
114 _EINT_FET()
115 return 0;
116 }
117