summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halSpi.h
blob: 59ab72c82c9679cd21ac9d648642e2e45b136e97 (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
/*****************************************************************************//**
\file  halSpi.h

\brief  Declarations of USART SPI mode.

\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:
    29/06/07 E. Ivanov - Created
**********************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/
#ifndef _HALSPI_H
#define _HALSPI_H

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

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define SPI_CHANNEL_0 USART_CHANNEL_0  // USART0 AtMega1281/2561 start addresss
#define SPI_CHANNEL_1 USART_CHANNEL_1  // USART1 AtMega1281/2561 start addresss

/******************************************************************************
                   Types section
******************************************************************************/
// spi channel
typedef UsartChannel_t SpiChannel_t;

// types of the clock mode
typedef enum
{
  // leading edge sample RX bit (rising), trailing edge setup TX bit (falling).
  SPI_CLOCK_MODE0,
  // leading edge setup TX bit (rising), trailing edge sample RX bit (falling).
  SPI_CLOCK_MODE1,
  // leading edge sample RX bit (falling), trailing edge setup TX bit (rising).
  SPI_CLOCK_MODE2,
  // leading edge setup TX bit (falling), trailing edge sample RX bit (rising).
  SPI_CLOCK_MODE3
} SpiClockMode_t;

// clock rate
typedef enum
{
  SPI_CLOCK_RATE_62 =  ((F_CPU / (2 * 62500ul)) - 1),
  SPI_CLOCK_RATE_125 =  ((F_CPU / (2 * 125000ul)) - 1),
  SPI_CLOCK_RATE_250 =  ((F_CPU / (2 * 250000ul)) - 1),
  SPI_CLOCK_RATE_500 =  ((F_CPU / (2 * 500000ul)) - 1),
  SPI_CLOCK_RATE_1000 = ((F_CPU / (2 * 1000000ul)) - 1),
  SPI_CLOCK_RATE_2000 = ((F_CPU / (2 * 2000000ul)) - 1)
} SpiBaudRate_t;

// Data order
typedef enum
{
  SPI_DATA_MSB_FIRST, // data with MSB first
  SPI_DATA_LSB_FIRST  // data with LSB first
} SpiDataOrder_t;

/******************************************************************************
                   Prototypes section
******************************************************************************/
/******************************************************************************
Disables USART channel.
Parameters:
  tty  -  spi channel.
******************************************************************************/
void halClearUsartSpi(SpiChannel_t tty);

/******************************************************************************
Write a length bytes to the SPI.
Parameters:
  tty    -  spi channel
  buffer -  pointer to application data buffer;
  length -  number bytes for transfer;
Returns:
  number of written bytes
******************************************************************************/
uint16_t halSyncUsartSpiWriteData(SpiChannel_t tty, uint8_t *buffer, uint16_t length);

/******************************************************************************
Write & read a length bytes to & from the SPI.
Parameters:
  tty    -  spi channel
  buffer -  pointer to application data buffer;
  length -  number bytes for transfer;
Returns:
  number of written & read bytes
******************************************************************************/
uint16_t halSyncUsartSpiReadData(SpiChannel_t tty, uint8_t *buffer, uint16_t length);

/******************************************************************************
                   Inline static functions section
******************************************************************************/
/******************************************************************************
Enables data register empty interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halEnableUsartSpiDremInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) |= (1 << UDRIE0);
}

/******************************************************************************
Disables data register empty interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halDisableUsartSpiDremInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) &= ~(1 << UDRIE0);
}

/******************************************************************************
Enables transmit complete interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halEnableUsartSpiTxcInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) |=  (1 << TXCIE0);
}

/******************************************************************************
Disables transmit complete interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halDisableUsartSpiTxcInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) &=  ~(1 << TXCIE0);
}

/*****************************************************************************
Enables receive complete interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halEnableUsartSpiRxcInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) |= (1 << RXCIE0);
}

/*****************************************************************************
Disables receive complete interrupt.
Parameters:
  tty  -  spi channel.
Returns:
  none.
******************************************************************************/
INLINE void halDisableUsartSpiRxcInterrupt(SpiChannel_t tty)
{
  UCSRnB(tty) &= ~(1 << RXCIE0);
}
#endif
//eof halSpi.h