summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/include/usart.h
blob: f81a5de73c48b2f84100036cb654f78eeba40337 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/****************************************************************************//**
  \file  usart.h

  \brief The header file describes the usart 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:
    22/08/07 A. Khromykh - Created
*******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/
#ifndef _USART_H
#define _USART_H

/******************************************************************************
                   Includes section
******************************************************************************/
// \cond
#include <halUsart.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
#define USART_FLOW_CONTROL_NONE 0
#define USART_FLOW_CONTROL_HARDWARE (1 << 0)
#define USART_DTR_CONTROL (1 << 1)

#if defined(ATMEGA1281) || defined(ATMEGA2561) || defined(ATMEGA1284) || \
    defined(AT90USB1287) || defined(ATMEGA128RFA1) || defined(ATXMEGA128A1) || \
    defined(ATXMEGA256A3) || defined(ATXMEGA256D3)
  // this define is used only by HAL.
  #define USART_SPI_READ_MODE  (1 << 4)
  #define USART_SPI_WRITE_MODE (1 << 3)
#endif
// \endcond

/******************************************************************************
                   Types section
******************************************************************************/
/** \brief Usart descriptor*/
typedef struct
{
  /** \brief HAL USART service field - contains variables for HAL USART module
  internal needs */
  HalUsartService_t service;
  /** \brief tty - USART_CHANNEL_n to be used. "n" range depends on the platform.
  Take a look into halUsart.h platform specific file fore more details. */
  UsartChannel_t tty;
  /** \brief Sets synchronous or asynchronous routine. \n
  Must be chosen from: \n
    USART_MODE_ASYNC \n
    USART_MODE_RS485 (only for arm)\n
    USART_MODE_SYNC  \n */
  UsartMode_t mode;
  /** \brief baudrate - USART baud rate. Must be chosen from: \n
    USART_BAUDRATE_1200  \n
    USART_BAUDRATE_2400  \n
    USART_BAUDRATE_4800  \n
    USART_BAUDRATE_9600 \n
    USART_BAUDRATE_19200 \n
    USART_BAUDRATE_38400 \n
    USART_SYNC_BAUDRATE_1200 \n
    USART_SYNC_BAUDRATE_2400 \n
    USART_SYNC_BAUDRATE_4800 \n
    USART_SYNC_BAUDRATE_9600 \n
    USART_SYNC_BAUDRATE_38400 \n
    USART_SYNC_BAUDRATE_57600 \n
    USART_SYNC_BAUDRATE_115200  \n */
  UsartBaudRate_t baudrate;
  /** \brief data -  USART data length. Must be chosen from: \n
    USART_DATA5 \n
    USART_DATA6 \n
    USART_DATA7 \n
    USART_DATA8 \n */
  UsartData_t dataLength;
  /** \brief parity -  USART parity mode. Must be chosen from: \n
    USART_PARITY_NONE \n
    USART_PARITY_EVEN \n
    USART_PARITY_ODD \n  */
  UsartParity_t parity;
  /** \brief stopbits - USART stop bits number. Must be chosen from: \n
    USART_STOPBIT_1 \n
    USART_STOPBIT_2 \n */
  UsartStopBits_t stopbits;
  /** \brief edge - data received edge (only for usart). Must be chosen from: \n
    USART_EDGE_MODE_FALLING \n
    USART_EDGE_MODE_RISING  \n */
  UsartEdgeMode_t edge;
  /** \brief master or slave on usart (only for usart). Must be chosen from:
    USART_CLK_MODE_MASTER \n
    USART_CLK_MODE_SLAVE  \n */
  UsartClkMode_t syncMode;
  /** \brief It's pointer to receive buffer. \n
   If rxBuffer is NULL then transactions are discarded. \n
   Size of buffer depends on user application. */
  uint8_t *rxBuffer;
  /** \brief length of receive buffer */
  uint16_t rxBufferLength;
  /** \brief It's pointer to transmit buffer. \n
   If txBuffer is NULL then callback method is used. \n
   If txBuffer isn't NULL then polling method is used. */
  uint8_t *txBuffer;
  /** \brief length of transmit buffer */
  uint16_t txBufferLength;
  /** \brief It's receive usart callback. \n
   If rxCallback is NULL then polling method is used. \n
   If rxCallback isn't NULL then callback method is used.*/
  void (*rxCallback)(uint16_t);
  /** \brief It's transmitting was completed usart callback. \n
   If txBuffer isn't NULL then txCallback notify about end of bytes sending.  */
  void (*txCallback)(void);
  #if defined(_USE_USART_ERROR_EVENT_)
    /** \brief It's error was occurred usart callback. \n
    If receiver error is issued then errCallback notify about issue with reason. \n
    Reason must be: \n
    FRAME_ERROR  \n
    DATA_OVERRUN \n
    PARITY_ERROR */
    void (*errCallback)(UsartErrorReason_t);
  #endif
  /** \brief flow control of usart. One of the defines \n
   USART_FLOW_CONTROL_NONE, \n
   USART_FLOW_CONTROL_HARDWARE, USART_DTR_CONTROL, it is possible to combine by logical OR. \n
   RS485 mode needs for USART_FLOW_CONTROL_NONE. \n
   The RTS pin is driven high when the transmitter is operating. */
  uint8_t flowControl;
} HAL_UsartDescriptor_t;

/******************************************************************************
                   Prototypes section
******************************************************************************/
/**************************************************************************//**
\brief Registers usart's event handlers. Performs configuration
of usart registers. Performs configuration of RTS, CTS and DTR pins.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure

\return
  Returns positive usart descriptor on success or -1 in cases: \n
    - bad usart channel; \n
    - there are not enough resources; \n
******************************************************************************/
int HAL_OpenUsart(HAL_UsartDescriptor_t *descriptor);

/*************************************************************************//**
\brief Releases the usart channel and pins, if hardware flow control was used.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure

\return
 -1 - bad descriptor or channel is already closed; \n
  0 - success.
*****************************************************************************/
int HAL_CloseUsart(HAL_UsartDescriptor_t *descriptor);

/**************************************************************************//**
\brief Writes a number of bytes to usart channel.
txCallback function will be used to notify when the transmission is finished.
If hardware flow control is used for transmitting then RTS and DTR pins will
be tested during transmission.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\param[in]
  buffer - pointer to the application data buffer;

\param[in]
  length - number of bytes to transfer;

\return
  -1 - bad descriptor; \n
   Number of bytes placed to the buffer - success.
******************************************************************************/
int HAL_WriteUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);

/*************************************************************************//**
\brief Reads a number of bytes from usart and places them to the buffer.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\param[in]
  buffer - pointer to the application buffer;

\param[in]
  length - number of bytes to be placed to the buffer;

\return
  -1 - bad descriptor, or bad number of bytes to read; \n
  Number of bytes placed to the buffer - success.
*****************************************************************************/
int HAL_ReadUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length);

/**************************************************************************//**
\brief Forbids the host to transmit data.
Only USART_CHANNEL_1 can be used for hardware flow control for avr.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\return
  -1 - bad descriptor, bad usart, or unsupported mode; \n
   0 - success.
******************************************************************************/
int HAL_OnUsartCts(HAL_UsartDescriptor_t *descriptor);

/**************************************************************************//**
\brief Allows the host to transmit data.
Only USART_CHANNEL_1 can be used for hardware flow control for avr.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\return
  -1 - bad descriptor, bad usart, or unsupported mode; \n
   0 - success.
******************************************************************************/
int HAL_OffUsartCts(HAL_UsartDescriptor_t *descriptor);

/**************************************************************************//**
\brief Reads RTS pin state.
Only USART_CHANNEL_1 can be used for hardware flow control for avr.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\return
  -1 - bad descriptor, bad usart, or unsupported mode; \n
   0 - RTS is low level; \n
   1 - RTS is high level;
******************************************************************************/
int HAL_ReadUsartRts(HAL_UsartDescriptor_t *descriptor);

/**************************************************************************//**
\brief Reads DTR pin state.
Only USART_CHANNEL_1 can be used for hardware flow control for avr.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\return
  -1 - bad descriptor, bad usart, or unsupported mode; \n
   0 - DTR is low level; \n
   1 - DTR is high level;
******************************************************************************/
int HAL_ReadUsartDtr(HAL_UsartDescriptor_t *descriptor);

/**************************************************************************//**
\brief Checks the status of tx buffer.

\param[in]
  descriptor - pointer to HAL_UsartDescriptor_t structure;

\return
  -1 - bad descriptor, no tx buffer; \n
   1 - tx buffer is empty; \n
   0 - tx buffer is not empty;
******************************************************************************/
int HAL_IsTxEmpty(HAL_UsartDescriptor_t *descriptor);

#if defined(ATMEGA1281) || defined(ATMEGA2561) || defined(ATMEGA1284) || defined(AT90USB1287) || defined(ATMEGA128RFA1)
/**************************************************************************//**
\brief Enables DTR wake up.

\param[in]
  callback - pointer to callback method;
******************************************************************************/
void HAL_EnableDtrWakeUp(void (* callback)(void));

/**************************************************************************//**
\brief Disables DTR wake up.
******************************************************************************/
void HAL_DisableDtrWakeUp(void);
#endif

#endif /* _USART_H */
// eof usart.h