summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/include/i2cPacket.h
blob: 102f2cd6f5951ba4483051997c29f8fc5b91f83d (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
/**************************************************************************//**
  \file  i2cPacket.h

  \brief The header file describes the i2cPacket 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 _I2CPACKET_H
#define _I2CPACKET_H

/******************************************************************************
                   Includes section
******************************************************************************/
// \cond
#include <halClkCtrl.h>
#include <types.h>
// \endcond

/******************************************************************************
                   Define(s) section
******************************************************************************/
/** \brief i2c bus prescaler */
#define HAL_I2C_PRESCALER 0ul
// \cond
/** \brief internal address size */
#if defined(AT91SAM7X256)
  /* AT91C_TWI_IADRSZ_NO */
  #define  HAL_NO_INTERNAL_ADDRESS   (0x0 << 8)
  /* AT91C_TWI_IADRSZ_1_BYTE */
  #define  HAL_ONE_BYTE_SIZE         (0x1 << 8)
  /* AT91C_TWI_IADRSZ_2_BYTE */
  #define  HAL_TWO_BYTE_SIZE         (0x2 << 8)
  /* AT91C_TWI_IADRSZ_3_BYTE */
  #define  HAL_THREE_BYTE_SIZE       (0x3 << 8)
#elif defined(ATMEGA1281) || defined(ATMEGA2561) || defined(ATMEGA1284) || defined(AT90USB1287) || defined(ATMEGA128RFA1)
  #define  HAL_NO_INTERNAL_ADDRESS   0x0
  #define  HAL_ONE_BYTE_SIZE         0x1
  #define  HAL_TWO_BYTE_SIZE         0x2
  #define  HAL_THREE_BYTE_SIZE       0x3
#endif
// \endcond

/******************************************************************************
                   Types section
******************************************************************************/
/** \brief i2c baud rate */
#if defined(AT91SAM7X256)
  typedef enum
  {
    I2C_CLOCK_RATE_250 = F_CPU/(2*250000) - 3, // 200 Kb/s clock rate
    I2C_CLOCK_RATE_125 = F_CPU/(2*125000) - 3, // 125 Kb/s clock rate
    I2C_CLOCK_RATE_100 = F_CPU/(2*100000) - 3, // 100 Kb/s clock rate
    I2C_CLOCK_RATE_62  = F_CPU/(2*62500) - 3   // 62.5 Kb/s clock rate
  } I2cClockRate_t;
#elif defined(ATMEGA1281) || defined(ATMEGA2561) || defined(ATMEGA1284) || defined(AT90USB1287) || defined(ATMEGA128RFA1)
  typedef enum
  {
    I2C_CLOCK_RATE_250 = ((F_CPU/250000ul) - 16ul)/(2ul * (1ul << HAL_I2C_PRESCALER) * (1ul << HAL_I2C_PRESCALER)), // 250 Kb/s clock rate
    I2C_CLOCK_RATE_125 = ((F_CPU/125000ul) - 16ul)/(2ul * (1ul << HAL_I2C_PRESCALER) * (1ul << HAL_I2C_PRESCALER)), // 125 Kb/s clock rate
    I2C_CLOCK_RATE_62  = ((F_CPU/62500ul) - 16ul)/(2ul * (1ul << HAL_I2C_PRESCALER) * (1ul << HAL_I2C_PRESCALER))   // 62.5 Kb/s clock rate
  } I2cClockRate_t;
#endif

/** \brief TWI clock rate */
typedef struct
{
  I2cClockRate_t clockrate; // clock rate
} HAL_i2cMode_t;

/**************************************************************************//**
\brief i2c access control structure
******************************************************************************/
typedef struct
{
  /** \brief slave address */
  uint8_t id;
  /** \brief number of bytes to be written to the bus */
  uint16_t length;
  /** \brief pointer to the data */
  uint8_t *data;
  /** \brief internal device address size conditions:
                  HAL_NO_INTERNAL_ADDRESS - byte command condition  \n
                  HAL_ONE_BYTE_SIZE       \n
                  HAL_TWO_BYTE_SIZE       \n
                  HAL_THREE_BYTE_SIZE */
#if defined(AT91SAM7X256)
  uint32_t lengthAddr;
#elif defined(ATMEGA1281) || defined(ATMEGA2561) || defined(ATMEGA1284) || defined(AT90USB1287) || defined(ATMEGA128RFA1)
  uint8_t lengthAddr;
#endif
  /** \brief internal device address */
  uint32_t internalAddr;
  /** \brief callback function */
  void (*f)(bool result);
} HAL_I2cParams_t;

/******************************************************************************
                   Prototypes section
******************************************************************************/
/**************************************************************************//**
\brief Opens twi resource.\n
field of i2cMode structure \n
i2cClockRate_t clockrate (set by user). Must be chosen from:\n
  I2C_CLOCK_RATE_250 - 250 Kb/s clock rate \n
  I2C_CLOCK_RATE_125 - 125 Kb/s clock rate \n
  I2C_CLOCK_RATE_62  - 62.5 Kb/s clock rate \n

\param[in]
  i2cMode - pointer to the mode structure.

\return
  -1 - resource was already open or pointer is NULL. \n
   0 - success.
******************************************************************************/
int HAL_OpenI2cPacket(HAL_i2cMode_t *i2cMode);

/**************************************************************************//**
\brief Closes resource.

\return
  -1 - resource was not open. \n
   0 - success.
******************************************************************************/
int HAL_CloseI2cPacket(void);

/**************************************************************************//**
\brief Writes a series of bytes out to the TWI bus. Operation result will be
sent to the callback function of the HAL_I2cParams_t structure.

\param[in]
  param - pointer to HAL_I2cParams_t structure
\return
   0 - the bus is free and the request is accepted. \n
  -1 - otherwise.
******************************************************************************/
int HAL_WriteI2cPacket(HAL_I2cParams_t *param);

/**************************************************************************//**
\brief Reads the series of bytes from the TWI bus. Operation result will be
sent to the callback function of the HAL_I2cParams_t structure.

\param[in]
  param - pointer to HAL_I2cParams_t structure
\return
  0 - the bus is free and the request is accepted. \n
 -1 - otherwise.
******************************************************************************/
int HAL_ReadI2cPacket(HAL_I2cParams_t *param);

#endif /* _I2CPACKET_H */
// eof i2cPacket.h