summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/i2c.h
blob: 79e6b2e6202c7d8de370151a3fad59c7394f0666 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***************************************************************************//**
  \file i2c.h

  \brief Declarations of i2c interface.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
      5/12/07 A. Khromykh - Created
 ******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

#ifndef _I2C_H
#define _I2C_H

/******************************************************************************
                   Includes section
******************************************************************************/
#include <i2cPacket.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
/** \brief TWI status codes. */
enum
{
  TWS_BUSERROR      = 0x00,
  TWS_START         = 0x08,
  TWS_RSTART        = 0x10,
  TWS_MT_SLA_ACK    = 0x18,
  TWS_MT_SLA_NACK   = 0x20,
  TWS_MT_DATA_ACK   = 0x28,
  TWS_MT_DATA_NACK  = 0x30,
  TWS_M_ARB_LOST    = 0x38,
  TWS_MR_SLA_ACK    = 0x40,
  TWS_MR_SLA_NACK   = 0x48,
  TWS_MR_DATA_ACK   = 0x50,
  TWS_MR_DATA_NACK  = 0x58
};

/******************************************************************************
                   Prototypes section
******************************************************************************/
/**************************************************************************//**
\brief Inits TWI module. Setup the speed of TWI.
\param[in]
  i2cMode - the speed of TWI.
******************************************************************************/
void halInitI2c(HAL_i2cMode_t *i2cMode);

/**************************************************************************//**
\brief Notification about the start condition was sent.
******************************************************************************/
void halSendStartDoneI2c(void);

/**************************************************************************//**
\brief Notification that byte was written to the TWI.
\param[in]
  result - contains result of previous operation.
******************************************************************************/
void halWriteDoneI2c(void);

/**************************************************************************//**
\brief Notification that byte was read from the TWI.
\param[in]
  data - contains byte that was read.
******************************************************************************/
void halReadDoneI2c(uint8_t data);

/**************************************************************************//**
\brief Notification that last byte was read from the TWI. Needs send STOP condition
on bus.
\param[in]
  data - contains byte that was read.
******************************************************************************/
void halReadLastByteDoneI2c(uint8_t data);

/**************************************************************************//**
\brief Notification that address byte was written to the TWI and was read ACK.
Starts reading data.
******************************************************************************/
void halMasterReadWriteAddressAckI2c(void);

/**************************************************************************//**
\brief Resets TWI bus and i2c HAL.
******************************************************************************/
void halI2cBusReset(void);

/******************************************************************************
                   Inline static functions section
******************************************************************************/
/**************************************************************************//**
\brief Loop for waiting for end of stop condition on bus.
******************************************************************************/
INLINE void halWaitEndOfStopStation(void)
{
  loop_until_bit_is_clear(TWCR, TWSTO);
}

/**************************************************************************//**
\brief Enables interrupt on TWI.
******************************************************************************/
INLINE void halInterruptEnableI2c(void)
{
  TWCR |= (1 << TWIE);
}

/**************************************************************************//**
\brief Disables interrupt on TWI.
******************************************************************************/
INLINE void halInterruptDisableI2c(void)
{
  TWCR &= (~(1 << TWIE));
}

/*************************************************************************//**
\brief Returns byte that was read from the TWI.
******************************************************************************/
INLINE uint8_t halReadByteI2c(void)
{
  return TWDR;
}

/*************************************************************************//**
\brief Resets the TWI.
******************************************************************************/
INLINE void halResetI2c(void)
{
  TWCR = ((1 << TWSTO) | (1 << TWINT));  // Reset TWI
}

/**************************************************************************//**
\brief Begins writing an byte to TWI.
\param[in]
  data - an byte for sending.
******************************************************************************/
INLINE void halWriteI2c(uint8_t data)
{
  TWDR =  data;
  TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWIE);
}

/**************************************************************************//**
\brief Begins read an byte from TWI.
\param[in]
  ack - defines the need to send ACK after an byte was recieved.
******************************************************************************/
INLINE void halReadI2c(bool ack)
{
  if (ack)
    TWCR |= (1 << TWEA);
  else
    TWCR &= ~(1 << TWEA);

  TWCR |= ((1 << TWINT) | (1 << TWIE) | (1 << TWEN));  // Trigger the TWI
}

/**************************************************************************//**
\brief Directs TWI to send stop condition.
******************************************************************************/
INLINE void halSendStopI2c(void)
{
  TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
}

/**************************************************************************//**
\brief Directs the TWI to send start condition.
******************************************************************************/
INLINE void halSendStartI2c(void)
{
  TWCR = ((1 << TWSTA) | (1 <<TWINT) | (1 << TWEN) | (1 << TWIE));
}

#endif /* _I2C_H*/

// eof i2c.h