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

📄 i2c_8c-source.html

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