⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c_8c-source.html

📁 ATMEL的AVR单片机库文件
💻 HTML
📖 第 1 页 / 共 3 页
字号:
00428         {00429             <span class="comment">// send data</span>00430             <a class="code" href="i2c_8h.html#a18">i2cSendByte</a>( I2cSendData[I2cSendDataIndex++] );00431         }00432         <span class="keywordflow">else</span>00433         {00434             <span class="comment">// transmit stop condition, enable SLA ACK</span>00435             <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00436             <span class="comment">// set state</span>00437             I2cState = I2C_IDLE;00438         }00439         <span class="keywordflow">break</span>;00440     <span class="keywordflow">case</span> TW_MR_DATA_NACK:               <span class="comment">// 0x58: Data received, NACK reply issued</span>00441 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00442 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00443         rprintf(<span class="stringliteral">"I2C: MR-&gt;DATA_NACK\r\n"</span>);00444         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00445 <span class="preprocessor">        #endif</span>00446 <span class="preprocessor"></span>        <span class="comment">// store final received data byte</span>00447         I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);00448         <span class="comment">// continue to transmit STOP condition</span>00449     <span class="keywordflow">case</span> TW_MR_SLA_NACK:                <span class="comment">// 0x48: Slave address not acknowledged</span>00450     <span class="keywordflow">case</span> TW_MT_SLA_NACK:                <span class="comment">// 0x20: Slave address not acknowledged</span>00451     <span class="keywordflow">case</span> TW_MT_DATA_NACK:               <span class="comment">// 0x30: Data not acknowledged</span>00452 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00453 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00454         rprintf(<span class="stringliteral">"I2C: MTR-&gt;SLA_NACK or MT-&gt;DATA_NACK\r\n"</span>);00455         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00456 <span class="preprocessor">        #endif</span>00457 <span class="preprocessor"></span>        <span class="comment">// transmit stop condition, enable SLA ACK</span>00458         <a class="code" href="i2c_8c.html#a16">i2cSendStop</a>();00459         <span class="comment">// set state</span>00460         I2cState = I2C_IDLE;00461         <span class="keywordflow">break</span>;00462     <span class="keywordflow">case</span> TW_MT_ARB_LOST:                <span class="comment">// 0x38: Bus arbitration lost</span>00463     <span class="comment">//case TW_MR_ARB_LOST:              // 0x38: Bus arbitration lost</span>00464 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00465 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00466         rprintf(<span class="stringliteral">"I2C: MT-&gt;ARB_LOST\r\n"</span>);00467         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00468 <span class="preprocessor">        #endif</span>00469 <span class="preprocessor"></span>        <span class="comment">// release bus</span>00470         outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT));00471         <span class="comment">// set state</span>00472         I2cState = I2C_IDLE;00473         <span class="comment">// release bus and transmit start when bus is free</span>00474         <span class="comment">//outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTA));</span>00475         <span class="keywordflow">break</span>;00476     <span class="keywordflow">case</span> TW_MR_DATA_ACK:                <span class="comment">// 0x50: Data acknowledged</span>00477 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00478 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00479         rprintf(<span class="stringliteral">"I2C: MR-&gt;DATA_ACK\r\n"</span>);00480         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00481 <span class="preprocessor">        #endif</span>00482 <span class="preprocessor"></span>        <span class="comment">// store received data byte</span>00483         I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);00484         <span class="comment">// fall-through to see if more bytes will be received</span>00485     <span class="keywordflow">case</span> TW_MR_SLA_ACK:                 <span class="comment">// 0x40: Slave address acknowledged</span>00486 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00487 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00488         rprintf(<span class="stringliteral">"I2C: MR-&gt;SLA_ACK\r\n"</span>);00489         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00490 <span class="preprocessor">        #endif</span>00491 <span class="preprocessor"></span>        <span class="keywordflow">if</span>(I2cReceiveDataIndex &lt; (I2cReceiveDataLength-1))00492             <span class="comment">// data byte will be received, reply with ACK (more bytes in transfer)</span>00493             <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(TRUE);00494         <span class="keywordflow">else</span>00495             <span class="comment">// data byte will be received, reply with NACK (final byte in transfer)</span>00496             <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(FALSE);00497         <span class="keywordflow">break</span>;00498 00499     <span class="comment">// Slave Receiver status codes</span>00500     <span class="keywordflow">case</span> TW_SR_SLA_ACK:                 <span class="comment">// 0x60: own SLA+W has been received, ACK has been returned</span>00501     <span class="keywordflow">case</span> TW_SR_ARB_LOST_SLA_ACK:        <span class="comment">// 0x68: own SLA+W has been received, ACK has been returned</span>00502     <span class="keywordflow">case</span> TW_SR_GCALL_ACK:               <span class="comment">// 0x70:     GCA+W has been received, ACK has been returned</span>00503     <span class="keywordflow">case</span> TW_SR_ARB_LOST_GCALL_ACK:      <span class="comment">// 0x78:     GCA+W has been received, ACK has been returned</span>00504 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00505 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00506         rprintf(<span class="stringliteral">"I2C: SR-&gt;SLA_ACK\r\n"</span>);00507         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00508 <span class="preprocessor">        #endif</span>00509 <span class="preprocessor"></span>        <span class="comment">// we are being addressed as slave for writing (data will be received from master)</span>00510         <span class="comment">// set state</span>00511         I2cState = I2C_SLAVE_RX;00512         <span class="comment">// prepare buffer</span>00513         I2cReceiveDataIndex = 0;00514         <span class="comment">// receive data byte and return ACK</span>00515         outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00516         <span class="keywordflow">break</span>;00517     <span class="keywordflow">case</span> TW_SR_DATA_ACK:                <span class="comment">// 0x80: data byte has been received, ACK has been returned</span>00518     <span class="keywordflow">case</span> TW_SR_GCALL_DATA_ACK:          <span class="comment">// 0x90: data byte has been received, ACK has been returned</span>00519 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00520 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00521         rprintf(<span class="stringliteral">"I2C: SR-&gt;DATA_ACK\r\n"</span>);00522         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00523 <span class="preprocessor">        #endif</span>00524 <span class="preprocessor"></span>        <span class="comment">// get previously received data byte</span>00525         I2cReceiveData[I2cReceiveDataIndex++] = inb(TWDR);00526         <span class="comment">// check receive buffer status</span>00527         <span class="keywordflow">if</span>(I2cReceiveDataIndex &lt; I2C_RECEIVE_DATA_BUFFER_SIZE)00528         {00529             <span class="comment">// receive data byte and return ACK</span>00530             <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(TRUE);00531             <span class="comment">//outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));</span>00532         }00533         <span class="keywordflow">else</span>00534         {00535             <span class="comment">// receive data byte and return NACK</span>00536             <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(FALSE);00537             <span class="comment">//outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT));</span>00538         }00539         <span class="keywordflow">break</span>;00540     <span class="keywordflow">case</span> TW_SR_DATA_NACK:               <span class="comment">// 0x88: data byte has been received, NACK has been returned</span>00541     <span class="keywordflow">case</span> TW_SR_GCALL_DATA_NACK:         <span class="comment">// 0x98: data byte has been received, NACK has been returned</span>00542 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00543 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00544         rprintf(<span class="stringliteral">"I2C: SR-&gt;DATA_NACK\r\n"</span>);00545         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00546 <span class="preprocessor">        #endif</span>00547 <span class="preprocessor"></span>        <span class="comment">// receive data byte and return NACK</span>00548         <a class="code" href="i2c_8h.html#a19">i2cReceiveByte</a>(FALSE);00549         <span class="comment">//outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT));</span>00550         <span class="keywordflow">break</span>;00551     <span class="keywordflow">case</span> TW_SR_STOP:                    <span class="comment">// 0xA0: STOP or REPEATED START has been received while addressed as slave</span>00552 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00553 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00554         rprintf(<span class="stringliteral">"I2C: SR-&gt;SR_STOP\r\n"</span>);00555         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00556 <span class="preprocessor">        #endif</span>00557 <span class="preprocessor"></span>        <span class="comment">// switch to SR mode with SLA ACK</span>00558         outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00559         <span class="comment">// i2c receive is complete, call i2cSlaveReceive</span>00560         <span class="keywordflow">if</span>(i2cSlaveReceive) i2cSlaveReceive(I2cReceiveDataIndex, I2cReceiveData);00561         <span class="comment">// set state</span>00562         I2cState = I2C_IDLE;00563         <span class="keywordflow">break</span>;00564 00565     <span class="comment">// Slave Transmitter</span>00566     <span class="keywordflow">case</span> TW_ST_SLA_ACK:                 <span class="comment">// 0xA8: own SLA+R has been received, ACK has been returned</span>00567     <span class="keywordflow">case</span> TW_ST_ARB_LOST_SLA_ACK:        <span class="comment">// 0xB0:     GCA+R has been received, ACK has been returned</span>00568 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00569 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00570         rprintf(<span class="stringliteral">"I2C: ST-&gt;SLA_ACK\r\n"</span>);00571         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00572 <span class="preprocessor">        #endif</span>00573 <span class="preprocessor"></span>        <span class="comment">// we are being addressed as slave for reading (data must be transmitted back to master)</span>00574         <span class="comment">// set state</span>00575         I2cState = I2C_SLAVE_TX;00576         <span class="comment">// request data from application</span>00577         <span class="keywordflow">if</span>(i2cSlaveTransmit) I2cSendDataLength = i2cSlaveTransmit(I2C_SEND_DATA_BUFFER_SIZE, I2cSendData);00578         <span class="comment">// reset data index</span>00579         I2cSendDataIndex = 0;00580         <span class="comment">// fall-through to transmit first data byte</span>00581     <span class="keywordflow">case</span> TW_ST_DATA_ACK:                <span class="comment">// 0xB8: data byte has been transmitted, ACK has been received</span>00582 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00583 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00584         rprintf(<span class="stringliteral">"I2C: ST-&gt;DATA_ACK\r\n"</span>);00585         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00586 <span class="preprocessor">        #endif</span>00587 <span class="preprocessor"></span>        <span class="comment">// transmit data byte</span>00588         outb(TWDR, I2cSendData[I2cSendDataIndex++]);00589         <span class="keywordflow">if</span>(I2cSendDataIndex &lt; I2cSendDataLength)00590             <span class="comment">// expect ACK to data byte</span>00591             outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00592         <span class="keywordflow">else</span>00593             <span class="comment">// expect NACK to data byte</span>00594             outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT));00595         <span class="keywordflow">break</span>;00596     <span class="keywordflow">case</span> TW_ST_DATA_NACK:               <span class="comment">// 0xC0: data byte has been transmitted, NACK has been received</span>00597     <span class="keywordflow">case</span> TW_ST_LAST_DATA:               <span class="comment">// 0xC8:</span>00598 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00599 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00600         rprintf(<span class="stringliteral">"I2C: ST-&gt;DATA_NACK or LAST_DATA\r\n"</span>);00601         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00602 <span class="preprocessor">        #endif</span>00603 <span class="preprocessor"></span>        <span class="comment">// all done</span>00604         <span class="comment">// switch to open slave</span>00605         outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));00606         <span class="comment">// set state</span>00607         I2cState = I2C_IDLE;00608         <span class="keywordflow">break</span>;00609 00610     <span class="comment">// Misc</span>00611     <span class="keywordflow">case</span> TW_NO_INFO:                    <span class="comment">// 0xF8: No relevant state information</span>00612         <span class="comment">// do nothing</span>00613 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00614 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00615         rprintf(<span class="stringliteral">"I2C: NO_INFO\r\n"</span>);00616         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00617 <span class="preprocessor">        #endif</span>00618 <span class="preprocessor"></span>        <span class="keywordflow">break</span>;00619     <span class="keywordflow">case</span> TW_BUS_ERROR:                  <span class="comment">// 0x00: Bus error due to illegal start or stop condition</span>00620 <span class="preprocessor">        #ifdef I2C_DEBUG</span>00621 <span class="preprocessor"></span>        <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1AddToTxBuffer);00622         rprintf(<span class="stringliteral">"I2C: BUS_ERROR\r\n"</span>);00623         <a class="code" href="rprintf_8h.html#a6">rprintfInit</a>(uart1SendByte);00624 <span class="preprocessor">        #endif</span>00625 <span class="preprocessor"></span>        <span class="comment">// reset internal hardware and release bus</span>00626         outb(TWCR, (inb(TWCR)&amp;TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTO)|BV(TWEA));00627         <span class="comment">// set state</span>00628         I2cState = I2C_IDLE;00629         <span class="keywordflow">break</span>;00630     }00631 }00632 <a name="l00633"></a><a class="code" href="i2c_8h.html#a26">00633</a> eI2cStateType <a class="code" href="i2c_8c.html#a27">i2cGetState</a>(<span class="keywordtype">void</span>)00634 {00635     <span class="keywordflow">return</span> I2cState;00636 }</pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Oct 15 03:50:22 2004 for Procyon AVRlib by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.6 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -