Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2194 - 1
/*****************************************************************************
2
*
3
* Atmel Corporation
4
*
5
* File              : TWI_Slave.h
6
* Compiler          : IAR EWAAVR 2.28a/3.10c
7
* Revision          : $Revision: 2475 $
8
* Date              : $Date: 2007-09-20 12:00:43 +0200 (to, 20 sep 2007) $
9
* Updated by        : $Author: mlarsson $
10
*
11
* Support mail      : avr@atmel.com
12
*
13
* Supported devices : All devices with a TWI module can be used.
14
*                     The example is written for the ATmega16
15
*
16
* AppNote           : AVR311 - TWI Slave Implementation
17
*
18
* Description       : Header file for TWI_slave.c
19
*                     Include this file in the application.
20
*
21
****************************************************************************/
22
/*! \page MISRA
23
 *
24
 * General disabling of MISRA rules:
25
 * * (MISRA C rule 1) compiler is configured to allow extensions
26
 * * (MISRA C rule 111) bit fields shall only be defined to be of type unsigned int or signed int
27
 * * (MISRA C rule 37) bitwise operations shall not be performed on signed integer types
28
 * As it does not work well with 8bit architecture and/or IAR
29
 
30
 * Other disabled MISRA rules
31
 * * (MISRA C rule 109) use of union - overlapping storage shall not be used
32
 * * (MISRA C rule 61) every non-empty case clause in a switch statement shall be terminated with a break statement
33
*/
34
 
35
/****************************************************************************
36
  TWI Status/Control register definitions
37
****************************************************************************/
38
 
39
#define TWI_BUFFER_SIZE 4      // Reserves memory for the drivers transceiver buffer. 
40
                               // Set this to the largest message size that will be sent including address byte.
41
 
42
/****************************************************************************
43
  Global definitions
44
****************************************************************************/
45
 
46
union TWI_statusReg_t                       // Status byte holding flags.
47
{
48
    unsigned char all;
49
    struct
50
    {
51
        unsigned char lastTransOK:1;      
52
        unsigned char RxDataInBuf:1;
53
        unsigned char genAddressCall:1;                        // TRUE = General call, FALSE = TWI Address;
54
        unsigned char unusedBits:5;
55
    };
56
};
57
 
58
extern union TWI_statusReg_t TWI_statusReg;
59
 
60
 
61
/****************************************************************************
62
  Function definitions
63
****************************************************************************/
64
void twi_slave_init( unsigned char );
65
unsigned char twi_slave_busy( void );
66
unsigned char twi_slave_get_status( void );
67
void twi_slave_start_with_data( unsigned char * , unsigned char );
68
void twi_slave_start( void );
69
unsigned char twi_slave_get_data( unsigned char *, unsigned char );
70
 
71
/****************************************************************************
72
  Bit and byte definitions
73
****************************************************************************/
74
#define TWI_READ_BIT  0   // Bit position for R/W bit in "address byte".
75
#define TWI_ADR_BITS  1   // Bit position for LSB of the slave address bits in the init byte.
76
#define TWI_GEN_BIT   0   // Bit position for LSB of the general call bit in the init byte.
77
 
78
#define TRUE          1
79
#define FALSE         0
80
 
81
/****************************************************************************
82
  TWI State codes
83
****************************************************************************/
84
// General TWI Master staus codes                      
85
#define TWI_START                  0x08  // START has been transmitted  
86
#define TWI_REP_START              0x10  // Repeated START has been transmitted
87
#define TWI_ARB_LOST               0x38  // Arbitration lost
88
 
89
// TWI Master Transmitter staus codes                      
90
#define TWI_MTX_ADR_ACK            0x18  // SLA+W has been tramsmitted and ACK received
91
#define TWI_MTX_ADR_NACK           0x20  // SLA+W has been tramsmitted and NACK received 
92
#define TWI_MTX_DATA_ACK           0x28  // Data byte has been tramsmitted and ACK received
93
#define TWI_MTX_DATA_NACK          0x30  // Data byte has been tramsmitted and NACK received 
94
 
95
// TWI Master Receiver staus codes  
96
#define TWI_MRX_ADR_ACK            0x40  // SLA+R has been tramsmitted and ACK received
97
#define TWI_MRX_ADR_NACK           0x48  // SLA+R has been tramsmitted and NACK received
98
#define TWI_MRX_DATA_ACK           0x50  // Data byte has been received and ACK tramsmitted
99
#define TWI_MRX_DATA_NACK          0x58  // Data byte has been received and NACK tramsmitted
100
 
101
// TWI Slave Transmitter staus codes
102
#define TWI_STX_ADR_ACK            0xA8  // Own SLA+R has been received; ACK has been returned
103
#define TWI_STX_ADR_ACK_M_ARB_LOST 0xB0  // Arbitration lost in SLA+R/W as Master; own SLA+R has been received; ACK has been returned
104
#define TWI_STX_DATA_ACK           0xB8  // Data byte in TWDR has been transmitted; ACK has been received
105
#define TWI_STX_DATA_NACK          0xC0  // Data byte in TWDR has been transmitted; NOT ACK has been received
106
#define TWI_STX_DATA_ACK_LAST_BYTE 0xC8  // Last data byte in TWDR has been transmitted (TWEA = �0�); ACK has been received
107
 
108
// TWI Slave Receiver staus codes
109
#define TWI_SRX_ADR_ACK            0x60  // Own SLA+W has been received ACK has been returned
110
#define TWI_SRX_ADR_ACK_M_ARB_LOST 0x68  // Arbitration lost in SLA+R/W as Master; own SLA+W has been received; ACK has been returned
111
#define TWI_SRX_GEN_ACK            0x70  // General call address has been received; ACK has been returned
112
#define TWI_SRX_GEN_ACK_M_ARB_LOST 0x78  // Arbitration lost in SLA+R/W as Master; General call address has been received; ACK has been returned
113
#define TWI_SRX_ADR_DATA_ACK       0x80  // Previously addressed with own SLA+W; data has been received; ACK has been returned
114
#define TWI_SRX_ADR_DATA_NACK      0x88  // Previously addressed with own SLA+W; data has been received; NOT ACK has been returned
115
#define TWI_SRX_GEN_DATA_ACK       0x90  // Previously addressed with general call; data has been received; ACK has been returned
116
#define TWI_SRX_GEN_DATA_NACK      0x98  // Previously addressed with general call; data has been received; NOT ACK has been returned
117
#define TWI_SRX_STOP_RESTART       0xA0  // A STOP condition or repeated START condition has been received while still addressed as Slave
118
 
119
// TWI Miscellaneous status codes
120
#define TWI_NO_STATE               0xF8  // No relevant state information available; TWINT = �0�
121
#define TWI_BUS_ERROR              0x00  // Bus error due to an illegal START or STOP condition