?? i2c_8c-source.html
字號:
00211 <span class="comment">// save data</span>00212 I2cDeviceAddrRW = (deviceAddr & 0xFE); <span class="comment">// RW cleared: write operation</span>00213 <span class="keywordflow">for</span>(i=0; i<length; i++)00214 I2cSendData[i] = *data++;00215 I2cSendDataIndex = 0;00216 I2cSendDataLength = length;00217 <span class="comment">// send start condition</span>00218 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00219 }00220 <a name="l00221"></a><a class="code" href="i2c_8h.html#a23">00221</a> <span class="keywordtype">void</span> <a class="code" href="i2c_8h.html#a23">i2cMasterReceive</a>(u08 deviceAddr, u08 length, u08* data)00222 {00223 u08 i;00224 <span class="comment">// wait for interface to be ready</span>00225 <span class="keywordflow">while</span>(I2cState);00226 <span class="comment">// set state</span>00227 I2cState = I2C_MASTER_RX;00228 <span class="comment">// save data</span>00229 I2cDeviceAddrRW = (deviceAddr|0x01); <span class="comment">// RW set: read operation</span>00230 I2cReceiveDataIndex = 0;00231 I2cReceiveDataLength = length;00232 <span class="comment">// send start condition</span>00233 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00234 <span class="comment">// wait for data</span>00235 <span class="keywordflow">while</span>(I2cState);00236 <span class="comment">// return data</span>00237 <span class="keywordflow">for</span>(i=0; i<length; i++)00238 *data++ = I2cReceiveData[i];00239 }00240 <a name="l00241"></a><a class="code" href="i2c_8h.html#a24">00241</a> u08 <a class="code" href="i2c_8h.html#a24">i2cMasterSendNI</a>(u08 deviceAddr, u08 length, u08* data)00242 {00243 u08 retval = I2C_OK;00244 00245 <span class="comment">// disable TWI interrupt</span>00246 cbi(TWCR, TWIE);00247 00248 <span class="comment">// send start condition</span>00249 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00250 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00251 00252 <span class="comment">// send device address with write</span>00253 <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>( deviceAddr & 0xFE );00254 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00255 00256 <span class="comment">// check if device is present and live</span>00257 <span class="keywordflow">if</span>( inb(TWSR) == TW_MT_SLA_ACK)00258 {00259 <span class="comment">// send data</span>00260 <span class="keywordflow">while</span>(length)00261 {00262 <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>( *data++ );00263 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00264 length--;00265 }00266 }00267 <span class="keywordflow">else</span>00268 {00269 <span class="comment">// device did not ACK it's address,</span>00270 <span class="comment">// data will not be transferred</span>00271 <span class="comment">// return error</span>00272 retval = I2C_ERROR_NODEV;00273 }00274 00275 <span class="comment">// transmit stop condition</span>00276 <span class="comment">// leave with TWEA on for slave receiving</span>00277 <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00278 <span class="keywordflow">while</span>( !(inb(TWCR) & BV(TWSTO)) );00279 00280 <span class="comment">// enable TWI interrupt</span>00281 sbi(TWCR, TWIE);00282 00283 <span class="keywordflow">return</span> retval;00284 }00285 <a name="l00286"></a><a class="code" href="i2c_8h.html#a25">00286</a> u08 <a class="code" href="i2c_8h.html#a25">i2cMasterReceiveNI</a>(u08 deviceAddr, u08 length, u08 *data)00287 {00288 u08 retval = I2C_OK;00289 00290 <span class="comment">// disable TWI interrupt</span>00291 cbi(TWCR, TWIE);00292 00293 <span class="comment">// send start condition</span>00294 <a class="code" href="i2c_8c.html#a15">i2cSendStart</a>();00295 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00296 00297 <span class="comment">// send device address with read</span>00298 <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>( deviceAddr | 0x01 );00299 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00300 00301 <span class="comment">// check if device is present and live</span>00302 <span class="keywordflow">if</span>( inb(TWSR) == TW_MR_SLA_ACK)00303 {00304 <span class="comment">// accept receive data and ack it</span>00305 <span class="keywordflow">while</span>(length > 1)00306 {00307 <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(TRUE);00308 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00309 *data++ = <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>();00310 <span class="comment">// decrement length</span>00311 length--;00312 }00313 00314 <span class="comment">// accept receive data and nack it (last-byte signal)</span>00315 <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(FALSE);00316 <a class="code" href="i2c_8c.html#a17">i2cWaitForComplete</a>();00317 *data++ = <a class="code" href="i2c_8c.html#a20">i2cGetReceivedByte</a>();00318 }00319 <span class="keywordflow">else</span>00320 {00321 <span class="comment">// device did not ACK it's address,</span>00322 <span class="comment">// data will not be transferred</span>00323 <span class="comment">// return error</span>00324 retval = I2C_ERROR_NODEV;00325 }00326 00327 <span class="comment">// transmit stop condition</span>00328 <span class="comment">// leave with TWEA on for slave receiving</span>00329 <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00330 00331 <span class="comment">// enable TWI interrupt</span>00332 sbi(TWCR, TWIE);00333 00334 <span class="keywordflow">return</span> retval;00335 }00336 <span class="comment">/*</span>00337 <span class="comment">void i2cMasterTransferNI(u08 deviceAddr, u08 sendlength, u08* senddata, u08 receivelength, u08* receivedata)</span>00338 <span class="comment">{</span>00339 <span class="comment"> // disable TWI interrupt</span>00340 <span class="comment"> cbi(TWCR, TWIE);</span>00341 <span class="comment"></span>00342 <span class="comment"> // send start condition</span>00343 <span class="comment"> i2cSendStart();</span>00344 <span class="comment"> i2cWaitForComplete();</span>00345 <span class="comment"></span>00346 <span class="comment"> // if there's data to be sent, do it</span>00347 <span class="comment"> if(sendlength)</span>00348 <span class="comment"> {</span>00349 <span class="comment"> // send device address with write</span>00350 <span class="comment"> i2cSendByte( deviceAddr & 0xFE );</span>00351 <span class="comment"> i2cWaitForComplete();</span>00352 <span class="comment"> </span>00353 <span class="comment"> // send data</span>00354 <span class="comment"> while(sendlength)</span>00355 <span class="comment"> {</span>00356 <span class="comment"> i2cSendByte( *senddata++ );</span>00357 <span class="comment"> i2cWaitForComplete();</span>00358 <span class="comment"> sendlength--;</span>00359 <span class="comment"> }</span>00360 <span class="comment"> }</span>00361 <span class="comment"></span>00362 <span class="comment"> // if there's data to be received, do it</span>00363 <span class="comment"> if(receivelength)</span>00364 <span class="comment"> {</span>00365 <span class="comment"> // send repeated start condition</span>00366 <span class="comment"> i2cSendStart();</span>00367 <span class="comment"> i2cWaitForComplete();</span>00368 <span class="comment"></span>00369 <span class="comment"> // send device address with read</span>00370 <span class="comment"> i2cSendByte( deviceAddr | 0x01 );</span>00371 <span class="comment"> i2cWaitForComplete();</span>00372 <span class="comment"></span>00373 <span class="comment"> // accept receive data and ack it</span>00374 <span class="comment"> while(receivelength > 1)</span>00375 <span class="comment"> {</span>00376 <span class="comment"> i2cReceiveByte(TRUE);</span>00377 <span class="comment"> i2cWaitForComplete();</span>00378 <span class="comment"> *receivedata++ = i2cGetReceivedByte();</span>00379 <span class="comment"> // decrement length</span>00380 <span class="comment"> receivelength--;</span>00381 <span class="comment"> }</span>00382 <span class="comment"></span>00383 <span class="comment"> // accept receive data and nack it (last-byte signal)</span>00384 <span class="comment"> i2cReceiveByte(TRUE);</span>00385 <span class="comment"> i2cWaitForComplete();</span>00386 <span class="comment"> *receivedata++ = i2cGetReceivedByte();</span>00387 <span class="comment"> }</span>00388 <span class="comment"> </span>00389 <span class="comment"> // transmit stop condition</span>00390 <span class="comment"> // leave with TWEA on for slave receiving</span>00391 <span class="comment"> i2cSendStop();</span>00392 <span class="comment"> while( !(inb(TWCR) & BV(TWSTO)) );</span>00393 <span class="comment"></span>00394 <span class="comment"> // enable TWI interrupt</span>00395 <span class="comment"> sbi(TWCR, TWIE);</span>00396 <span class="comment">}</span>00397 <span class="comment">*/</span>00398 <span class="comment"></span>00399 <span class="comment">//! I2C (TWI) interrupt service routine</span><a name="l00400"></a><a class="code" href="i2c_8c.html#a26">00400</a> <span class="comment"></span><a class="code" href="a2d_8c.html#a10">SIGNAL</a>(SIG_2WIRE_SERIAL)00401 {00402 <span class="comment">// read status bits</span>00403 u08 status = inb(TWSR) & TWSR_STATUS_MASK;00404 00405 <span class="keywordflow">switch</span>(status)00406 {00407 <span class="comment">// Master General</span>00408 <span class="keywordflow">case</span> TW_START: <span class="comment">// 0x08: Sent start condition</span>00409 <span class="keywordflow">case</span> TW_REP_START: <span class="comment">// 0x10: Sent repeated start condition</span>00410 <span class="preprocessor"> #ifdef I2C_DEBUG</span>00411 <span class="preprocessor"></span> <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00412 rprintf(<span class="stringliteral">"I2C: M->START\r\n"</span>);00413 <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00414 <span class="preprocessor"> #endif</span>00415 <span class="preprocessor"></span> <span class="comment">// send device address</span>00416 <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>(I2cDeviceAddrRW);00417 <span class="keywordflow">break</span>;00418 00419 <span class="comment">// Master Transmitter & Receiver status codes</span>00420 <span class="keywordflow">case</span> TW_MT_SLA_ACK: <span class="comment">// 0x18: Slave address acknowledged</span>00421 <span class="keywordflow">case</span> TW_MT_DATA_ACK: <span class="comment">// 0x28: Data acknowledged</span>00422 <span class="preprocessor"> #ifdef I2C_DEBUG</span>00423 <span class="preprocessor"></span> <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00424 rprintf(<span class="stringliteral">"I2C: MT->SLA_ACK or DATA_ACK\r\n"</span>);00425 <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00426 <span class="preprocessor"> #endif</span>00427 <span class="preprocessor"></span> <span class="keywordflow">if</span>(I2cSendDataIndex < I2cSendDataLength)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -