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

📄 i2c_8c-source.html

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

⌨️ 快捷键说明

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