Rev Author Line No. Line
1274 kakl 1 /************ SMB driver ************/
2  
3 #define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default)
4 #define RAM_Access 0x00 // RAM access command
5 #define RAM_Tobj1 0x07 // To1 address in the RAM
6 #define RAM_Tamb 0x06 // Ta address in the RAM
7  
1275 kakl 8  
1274 kakl 9 //*High and Low level of clock
10 #define HIGHLEV 40 // max. 50us
11 #define LOWLEV 100 // max. 30ms
12 #define TBUF 20
13  
14 //SMBus control signals
15 #define SCL PIN_B0
16 #define SDA PIN_B1
17  
18 #define mSDA_HIGH() output_float(SDA); // SDA float
19 #define mSDA_LOW() output_low(SDA); // SDA low
20 #define mSCL_HIGH() output_float(SCL); //output_high(SCL); // SCL high
21 #define mSCL_LOW() output_low(SCL); // SCL low
22  
23 #define ACK 0
24 #define NACK 1
25  
26 //**********************************************************************************************
27 // START CONDITION ON SMBus
28 //**********************************************************************************************
29 //Name: START_bit
30 //Function: Generate START condition on SMBus
31 //Parameters: No
32 //Return: No
33 //Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0"
34 // or AN"SMBus communication with MLX90614" on the website www.melexis.com
35 //**********************************************************************************************
36 void SMB_START_bit(void)
37 {
1275 kakl 38 // disable_interrupts(GLOBAL);
1274 kakl 39 mSDA_HIGH(); // Set SDA line
40 delay_us( TBUF ); // Wait a few microseconds
41 mSCL_HIGH(); // Set SCL line
42 delay_us( TBUF ); // Generate bus free time between Stop
43 // and Start condition (Tbuf=4.7us min)
44 mSDA_LOW(); // Clear SDA line
45 delay_us( TBUF ); // Hold time after (Repeated) Start
46 // Condition. After this period, the first clock is generated.
47 //(Thd:sta=4.0us min)
48 mSCL_LOW(); // Clear SCL line
1275 kakl 49 // enable_interrupts(GLOBAL);
1274 kakl 50 delay_us( TBUF ); // Wait a few microseconds
1275 kakl 51  
52 toggle_dome();
1274 kakl 53 }
54 //*********************************************************************************************
55 // STOP CONDITION ON SMBus
56 //*********************************************************************************************
57 //Name: STOPbit
58 //Function: Generate STOP condition on SMBus
59 //Parameters: No
60 //Return: No
61 //Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0"
62 // or AN"SMBus communication with MLX90614" on the website www.melexis.com
63 //*********************************************************************************************
64 void SMB_STOP_bit(void)
65 {
1275 kakl 66 // disable_interrupts(GLOBAL);
1274 kakl 67 mSDA_HIGH();
68 mSCL_LOW(); // Clear SCL line
69 delay_us( TBUF ); // Wait a few microseconds
70 mSDA_LOW(); // Clear SDA line
71 delay_us( TBUF ); // Wait a few microseconds
72 mSCL_HIGH(); // Set SCL line
73 delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min)
74 mSDA_HIGH(); // Set SDA line
1275 kakl 75 // enable_interrupts(GLOBAL);
76  
77 toggle_dome();
1274 kakl 78 }
79  
80  
81 void SMB_send_bit(unsigned char bit_out)
82 {
1275 kakl 83 // disable_interrupts(GLOBAL);
1274 kakl 84 if(bit_out==0) {mSDA_LOW();}
85 else {mSDA_HIGH();}
86 delay_us(3);
87 mSCL_HIGH(); // Set SCL line
88 delay_us( HIGHLEV ); // High Level of Clock Pulse
89 mSCL_LOW(); // Clear SCL line
90 delay_us( LOWLEV ); // Low Level of Clock Pulse
91 // mSDA_HIGH(); // Master release SDA line ,
1275 kakl 92 // enable_interrupts(GLOBAL);
93  
94 toggle_dome();
1274 kakl 95 return;
96 }
97  
98 unsigned char SMB_Receive_bit(void)
99 {
100 unsigned char Ack_bit;
101  
1275 kakl 102 // disable_interrupts(GLOBAL);
1274 kakl 103 mSDA_HIGH(); //_SDA_IO=1; // SDA-input
104 mSCL_HIGH(); // Set SCL line
105 delay_us( HIGHLEV ); // High Level of Clock Pulse
106 if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit
107 else Ack_bit=0; // /
108 mSCL_LOW(); // Clear SCL line
109 delay_us( LOWLEV ); // Low Level of Clock Pulse
1275 kakl 110 // enable_interrupts(GLOBAL);
1274 kakl 111  
1275 kakl 112 toggle_dome();
1274 kakl 113 return Ack_bit;
114 }
115  
116  
117 //*********************************************************************************************
118 // TRANSMIT DATA ON SMBus
119 //*********************************************************************************************
120 //Name: TX_byte
121 //Function: Send a byte on SMBus
122 //Parameters: TX_buffer ( the byte which will be send on the SMBus )
123 //Return: Ack_bit ( acknowledgment bit )
124 //Comments: Sends MSbit first
125 //*********************************************************************************************
126 unsigned char SMB_TX_byte(unsigned char Tx_buffer)
127 {
128 unsigned char Bit_counter;
129 unsigned char Ack_bit;
130 unsigned char bit_out;
131  
132 for(Bit_counter=8; Bit_counter; Bit_counter--)
133 {
134 if(Tx_buffer&0x80) bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
135 else bit_out=0; // else clear bit_out
136  
137 SMB_send_bit(bit_out); // Send the current bit on SDA
138 Tx_buffer<<=1; // Get next bit for checking
139 }
140  
141 Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit
142  
143 return Ack_bit;
1275 kakl 144 }
1274 kakl 145  
146 //*********************************************************************************************
147 // RECEIVE DATA ON SMBus
148 //*********************************************************************************************
149 //Name: RX_byte
150 //Function: Receive a byte on SMBus
151 //Parameters: ack_nack (ackowlegment bit)
152 //Return: RX_buffer(Received byte)
153 //Comments: MSbit is received first
154 //*********************************************************************************************
155 unsigned char SMB_RX_byte(unsigned char ack_nack)
156 {
157 unsigned char RX_buffer;
158 unsigned char Bit_Counter;
159  
160 for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
161 {
162 if(SMB_Receive_bit()) // Get a bit from the SDA line
163 {
164 RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
165 RX_buffer |=0b00000001;
166 }
167 else
168 {
169 RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
170 RX_buffer &=0b11111110;
171 }
172 }
173  
174 SMB_send_bit(ack_nack); // Sends acknowledgment bit
175  
176 return RX_buffer;
177 }
178  
179  
180 unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation
181 {
182 unsigned char crc[6];
183 unsigned char BitPosition=47;
184 unsigned char shift;
185 unsigned char i;
186 unsigned char j;
187 unsigned char temp;
188  
189 do
190 {
191 crc[5]=0; /* Load CRC value 0x000000000107 */
192 crc[4]=0;
193 crc[3]=0;
194 crc[2]=0;
195 crc[1]=0x01;
196 crc[0]=0x07;
197 BitPosition=47; /* Set maximum bit position at 47 */
198 shift=0;
199  
200 //Find first 1 in the transmited message
201 i=5; /* Set highest index */
202 j=0;
203 while((pec[i]&(0x80>>j))==0 && i>0)
204 {
205 BitPosition--;
206 if(j<7)
207 {
208 j++;
209 }
210 else
211 {
212 j=0x00;
213 i--;
214 }
1275 kakl 215 }
1274 kakl 216  
217 shift=BitPosition-8; /*Get shift value for crc value*/
218  
219  
220 //Shift crc value
221 while(shift)
222 {
223 for(i=5; i<0xFF; i--)
224 {
225 if((crc[i-1]&0x80) && (i>0))
226 {
227 temp=1;
228 }
229 else
230 {
231 temp=0;
232 }
233 crc[i]<<=1;
234 crc[i]+=temp;
1275 kakl 235 }
1274 kakl 236 shift--;
1275 kakl 237 }
1274 kakl 238  
239 //Exclusive OR between pec and crc
240 for(i=0; i<=5; i++)
241 {
242 pec[i] ^=crc[i];
1275 kakl 243 }
1274 kakl 244 } while(BitPosition>8);/*End of do-while*/
245  
246 return pec[0];
247 }