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
51 }
52 //*********************************************************************************************
53 // STOP CONDITION ON SMBus
54 //*********************************************************************************************
55 //Name: STOPbit
56 //Function: Generate STOP condition on SMBus
57 //Parameters: No
58 //Return: No
59 //Comments: Refer to "System Managment BUS(SMBus) specification Version 2.0"
60 // or AN"SMBus communication with MLX90614" on the website www.melexis.com
61 //*********************************************************************************************
62 void SMB_STOP_bit(void)
63 {
1275 kakl 64 // disable_interrupts(GLOBAL);
1274 kakl 65 mSDA_HIGH();
66 mSCL_LOW(); // Clear SCL line
67 delay_us( TBUF ); // Wait a few microseconds
68 mSDA_LOW(); // Clear SDA line
69 delay_us( TBUF ); // Wait a few microseconds
70 mSCL_HIGH(); // Set SCL line
71 delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min)
72 mSDA_HIGH(); // Set SDA line
1275 kakl 73 // enable_interrupts(GLOBAL);
1274 kakl 74 }
75  
76  
77 void SMB_send_bit(unsigned char bit_out)
78 {
1275 kakl 79 // disable_interrupts(GLOBAL);
1274 kakl 80 if(bit_out==0) {mSDA_LOW();}
81 else {mSDA_HIGH();}
82 delay_us(3);
83 mSCL_HIGH(); // Set SCL line
84 delay_us( HIGHLEV ); // High Level of Clock Pulse
85 mSCL_LOW(); // Clear SCL line
1283 kakl 86  
87 toggle_dome();
88  
1274 kakl 89 delay_us( LOWLEV ); // Low Level of Clock Pulse
90 // mSDA_HIGH(); // Master release SDA line ,
1275 kakl 91 // enable_interrupts(GLOBAL);
92  
1283 kakl 93 toggle_dome();
1274 kakl 94 return;
95 }
96  
97 unsigned char SMB_Receive_bit(void)
98 {
99 unsigned char Ack_bit;
100  
1275 kakl 101 // disable_interrupts(GLOBAL);
1274 kakl 102 mSDA_HIGH(); //_SDA_IO=1; // SDA-input
103 mSCL_HIGH(); // Set SCL line
104 delay_us( HIGHLEV ); // High Level of Clock Pulse
105 if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit
106 else Ack_bit=0; // /
107 mSCL_LOW(); // Clear SCL line
1283 kakl 108  
109 toggle_dome();
110  
1274 kakl 111 delay_us( LOWLEV ); // Low Level of Clock Pulse
1275 kakl 112 // enable_interrupts(GLOBAL);
1274 kakl 113  
1283 kakl 114 toggle_dome();
1274 kakl 115 return Ack_bit;
116 }
117  
118  
119 //*********************************************************************************************
120 // TRANSMIT DATA ON SMBus
121 //*********************************************************************************************
122 //Name: TX_byte
123 //Function: Send a byte on SMBus
124 //Parameters: TX_buffer ( the byte which will be send on the SMBus )
125 //Return: Ack_bit ( acknowledgment bit )
126 //Comments: Sends MSbit first
127 //*********************************************************************************************
128 unsigned char SMB_TX_byte(unsigned char Tx_buffer)
129 {
130 unsigned char Bit_counter;
131 unsigned char Ack_bit;
132 unsigned char bit_out;
133  
134 for(Bit_counter=8; Bit_counter; Bit_counter--)
135 {
136 if(Tx_buffer&0x80) bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
137 else bit_out=0; // else clear bit_out
138  
139 SMB_send_bit(bit_out); // Send the current bit on SDA
140 Tx_buffer<<=1; // Get next bit for checking
141 }
142  
143 Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit
144  
145 return Ack_bit;
1275 kakl 146 }
1274 kakl 147  
148 //*********************************************************************************************
149 // RECEIVE DATA ON SMBus
150 //*********************************************************************************************
151 //Name: RX_byte
152 //Function: Receive a byte on SMBus
153 //Parameters: ack_nack (ackowlegment bit)
154 //Return: RX_buffer(Received byte)
155 //Comments: MSbit is received first
156 //*********************************************************************************************
157 unsigned char SMB_RX_byte(unsigned char ack_nack)
158 {
159 unsigned char RX_buffer;
160 unsigned char Bit_Counter;
161  
162 for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
163 {
164 if(SMB_Receive_bit()) // Get a bit from the SDA line
165 {
166 RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
167 RX_buffer |=0b00000001;
168 }
169 else
170 {
171 RX_buffer <<= 1; // If the bit is LOW save 0 in RX_buffer
172 RX_buffer &=0b11111110;
173 }
174 }
175  
176 SMB_send_bit(ack_nack); // Sends acknowledgment bit
177  
178 return RX_buffer;
179 }
180  
181  
182 unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation
183 {
184 unsigned char crc[6];
185 unsigned char BitPosition=47;
186 unsigned char shift;
187 unsigned char i;
188 unsigned char j;
189 unsigned char temp;
190  
191 do
192 {
193 crc[5]=0; /* Load CRC value 0x000000000107 */
194 crc[4]=0;
195 crc[3]=0;
196 crc[2]=0;
197 crc[1]=0x01;
198 crc[0]=0x07;
199 BitPosition=47; /* Set maximum bit position at 47 */
200 shift=0;
201  
202 //Find first 1 in the transmited message
203 i=5; /* Set highest index */
204 j=0;
205 while((pec[i]&(0x80>>j))==0 && i>0)
206 {
207 BitPosition--;
208 if(j<7)
209 {
210 j++;
211 }
212 else
213 {
214 j=0x00;
215 i--;
216 }
1283 kakl 217 toggle_dome();
1275 kakl 218 }
1274 kakl 219  
220 shift=BitPosition-8; /*Get shift value for crc value*/
221  
222  
223 //Shift crc value
224 while(shift)
225 {
226 for(i=5; i<0xFF; i--)
227 {
228 if((crc[i-1]&0x80) && (i>0))
229 {
230 temp=1;
231 }
232 else
233 {
234 temp=0;
235 }
236 crc[i]<<=1;
237 crc[i]+=temp;
1275 kakl 238 }
1274 kakl 239 shift--;
1283 kakl 240 toggle_dome();
1275 kakl 241 }
1274 kakl 242  
243 //Exclusive OR between pec and crc
244 for(i=0; i<=5; i++)
245 {
246 pec[i] ^=crc[i];
1275 kakl 247 }
1283 kakl 248 } while(BitPosition>8);
1274 kakl 249  
250 return pec[0];
251 }