Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / Peripheral / AVR8 / TWI_AVR8.c @ 978b99e5

History | View | Annotate | Download (5.02 KB)

1
/*
2
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7
*/
8

    
9
/*
10
  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11

12
  Permission to use, copy, modify, distribute, and sell this
13
  software and its documentation for any purpose is hereby granted
14
  without fee, provided that the above copyright notice appear in
15
  all copies and that both that the copyright notice and this
16
  permission notice and warranty disclaimer appear in supporting
17
  documentation, and that the name of the author not be used in
18
  advertising or publicity pertaining to distribution of the
19
  software without specific, written prior permission.
20

21
  The author disclaim all warranties with regard to this
22
  software, including all implied warranties of merchantability
23
  and fitness.  In no event shall the author be liable for any
24
  special, indirect or consequential damages or any damages
25
  whatsoever resulting from loss of use, data or profits, whether
26
  in an action of contract, negligence or other tortious action,
27
  arising out of or in connection with the use or performance of
28
  this software.
29
*/
30

    
31
#define  __INCLUDE_FROM_TWI_C
32
#include "../TWI.h"
33

    
34
uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
35
                              const uint8_t TimeoutMS)
36
{
37
        for (;;)
38
        {
39
                bool     BusCaptured = false;
40
                uint16_t TimeoutRemaining;
41

    
42
                TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
43

    
44
                TimeoutRemaining = (TimeoutMS * 100);
45
                while (TimeoutRemaining-- && !(BusCaptured))
46
                {
47
                        if (TWCR & (1 << TWINT))
48
                        {
49
                                switch (TWSR & TW_STATUS_MASK)
50
                                {
51
                                        case TW_START:
52
                                        case TW_REP_START:
53
                                                BusCaptured = true;
54
                                                break;
55
                                        case TW_MT_ARB_LOST:
56
                                                TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
57
                                                continue;
58
                                        default:
59
                                                TWCR = (1 << TWEN);
60
                                                return TWI_ERROR_BusFault;
61
                                }
62
                        }
63

    
64
                        _delay_us(10);
65
                }
66

    
67
                if (!(TimeoutRemaining))
68
                {
69
                        TWCR = (1 << TWEN);
70
                        return TWI_ERROR_BusCaptureTimeout;
71
                }
72

    
73
                TWDR = SlaveAddress;
74
                TWCR = ((1 << TWINT) | (1 << TWEN));
75

    
76
                TimeoutRemaining = (TimeoutMS * 100);
77
                while (TimeoutRemaining--)
78
                {
79
                        if (TWCR & (1 << TWINT))
80
                          break;
81

    
82
                        _delay_us(10);
83
                }
84

    
85
                if (!(TimeoutRemaining))
86
                  return TWI_ERROR_SlaveResponseTimeout;
87

    
88
                switch (TWSR & TW_STATUS_MASK)
89
                {
90
                        case TW_MT_SLA_ACK:
91
                        case TW_MR_SLA_ACK:
92
                                return TWI_ERROR_NoError;
93
                        default:
94
                                TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
95
                                return TWI_ERROR_SlaveNotReady;
96
                }
97
        }
98
}
99

    
100
bool TWI_SendByte(const uint8_t Byte)
101
{
102
        TWDR = Byte;
103
        TWCR = ((1 << TWINT) | (1 << TWEN));
104
        while (!(TWCR & (1 << TWINT)));
105

    
106
        return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
107
}
108

    
109
bool TWI_ReceiveByte(uint8_t* const Byte,
110
                                         const bool LastByte)
111
{
112
        uint8_t TWCRMask;
113

    
114
        if (LastByte)
115
          TWCRMask = ((1 << TWINT) | (1 << TWEN));
116
        else
117
          TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
118

    
119
        TWCR = TWCRMask;
120
        while (!(TWCR & (1 << TWINT)));
121
        *Byte = TWDR;
122
        
123
        uint8_t Status = (TWSR & TW_STATUS_MASK);
124
        
125
        return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));
126
}
127

    
128
uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
129
                       const uint8_t TimeoutMS,
130
                       const uint8_t* InternalAddress,
131
                       uint8_t InternalAddressLen,
132
                       uint8_t* Buffer,
133
                       uint8_t Length)
134
{
135
        uint8_t ErrorCode;
136
        
137
        if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
138
                                               TimeoutMS)) == TWI_ERROR_NoError)
139
        {
140
                while (InternalAddressLen--)
141
                {
142
                        if (!(TWI_SendByte(*(InternalAddress++))))
143
                        {                                
144
                                ErrorCode = TWI_ERROR_SlaveNAK;
145
                                break;
146
                        }
147
                }
148
                
149
                if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
150
                                                                                           TimeoutMS)) == TWI_ERROR_NoError)
151
                {
152
                        while (Length--)
153
                        {
154
                                if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
155
                                {
156
                                        ErrorCode = TWI_ERROR_SlaveNAK;
157
                                        break;
158
                                }
159
                        }
160
                        
161
                        TWI_StopTransmission();
162
                }
163
        }
164
        
165
        return ErrorCode;
166
}
167

    
168
uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
169
                        const uint8_t TimeoutMS,
170
                        const uint8_t* InternalAddress,
171
                        uint8_t InternalAddressLen,
172
                        const uint8_t* Buffer,
173
                        uint8_t Length)
174
{
175
        uint8_t ErrorCode;
176

    
177
        if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
178
                                               TimeoutMS)) == TWI_ERROR_NoError)
179
        {
180
                while (InternalAddressLen--)
181
                {
182
                        if (!(TWI_SendByte(*(InternalAddress++))))
183
                        {                                
184
                                ErrorCode = TWI_ERROR_SlaveNAK;
185
                                break;
186
                        }
187
                }
188

    
189
                while (Length--)
190
                {
191
                        if (!(TWI_SendByte(*(Buffer++))))
192
                        {
193
                                ErrorCode = TWI_ERROR_SlaveNAK;
194
                                break;
195
                        }
196
                }
197
                
198
                TWI_StopTransmission();
199
        }
200
        
201
        return ErrorCode;
202
}