summaryrefslogtreecommitdiffhomepage
path: root/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoVirtualUsart.c
blob: 1a909d38a3fcbede9381f8af217959c816480c3c (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
/****************************************************************************//**
  \file usbFifoVirtualUart.c

  \brief Implementation of virtual uart API.

  \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:
    11/09/08 A. Khromykh - Created
*******************************************************************************/
/******************************************************************************
                   Includes section
******************************************************************************/
#include <usbFifoVirtualUsart.h>
#include <usbFifoUsart.h>
#include <atomic.h>
#include <irq.h>

/******************************************************************************
                   External global variables section
******************************************************************************/
extern void (* extHandler)(void);

/******************************************************************************
                   Global variables section
******************************************************************************/
// pointer to application uart descriptor
HAL_UsartDescriptor_t *usbfifoPointDescrip = NULL;

/******************************************************************************
                   Implementations section
******************************************************************************/
/******************************************************************************
Open virtual com port and register uart's event handlers.

Parameters:
  descriptor - pointer to HAL_UartDescriptor_t structure

Returns:
  Returns positive uart descriptor on success or -1 in cases:
    - bad uart channel;
    - there are not enough resources;
    - receiving buffer is less bulk endpoint size;
******************************************************************************/
int USBFIFO_OpenUsart(HAL_UsartDescriptor_t *descriptor)
{
  if (NULL == descriptor)
    return -1;

  if (USART_CHANNEL_USBFIFO != descriptor->tty)
    return -1;

  if (NULL != usbfifoPointDescrip)
    return -1; /* source was opened */

  extHandler = usbfifoHandler;
  usbfifoPointDescrip = descriptor;

  usbfifoPointDescrip->service.rxPointOfRead = 0;
  usbfifoPointDescrip->service.rxPointOfWrite = 0;
  usbfifoPointDescrip->service.txPointOfRead = 0;
  usbfifoPointDescrip->service.txPointOfWrite = 0;
  usbfifoInit();
  // enable receiver
  HAL_EnableIrq(IRQ_7);

  return (int)descriptor->tty;
}

/******************************************************************************
Frees the virtual uart channel.
Parameters:
  descriptor - the uart descriptor.
Returns:
  Returns 0 on success or -1 if bad descriptor.
******************************************************************************/
int USBFIFO_CloseUsart(HAL_UsartDescriptor_t *descriptor)
{
  if (NULL == descriptor)
    return -1;

  if (usbfifoPointDescrip != descriptor)
    return -1;

  usbfifoPointDescrip = NULL;
  extHandler = NULL;
  usbfifoUnInit();

  return 0;
}

/******************************************************************************
Writes a number of bytes to a virtual uart channel.
txCallback function will be used to notify when the transmission is finished.
Parameters:
  descriptor - pointer to HAL_UartDescriptor_t structure;
  buffer - pointer to the application data buffer;
  length - number of bytes to transfer;
Returns:
  -1 - bad descriptor;
   Number of bytes placed to the buffer - success.
******************************************************************************/
int USBFIFO_WriteUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  uint16_t           poW;
  uint16_t           poR;
  uint16_t           old;
  uint16_t           wasWrote = 0;
  HalUsartService_t *control;

  if (NULL == descriptor)
    return -1;

  if (usbfifoPointDescrip != descriptor)
    return -1;

  if (!buffer || !length)
    return -1;

  control = &descriptor->service;
  ATOMIC_SECTION_ENTER
    poW = control->txPointOfWrite;
    poR = control->txPointOfRead;
  ATOMIC_SECTION_LEAVE

  if (0 == descriptor->txBufferLength)
  { // Callback mode
    if (poW != poR)
      return -1; // there is unsent data
    descriptor->txBuffer = buffer;
    control->txPointOfWrite = length;
    control->txPointOfRead = 0;
    wasWrote = length;
  } // Callback mode.
  else
  { // Polling mode.
    while (wasWrote < length)
    {
      old = poW;

      if (++poW == descriptor->txBufferLength)
        poW = 0;

      if (poW == poR)
      { // Buffer full.
        poW = old;
        break;
      } // Buffer full.

      descriptor->txBuffer[old] = buffer[wasWrote++];
    }

    ATOMIC_SECTION_ENTER
      control->txPointOfWrite = poW;
    ATOMIC_SECTION_LEAVE
  } // Polling mode

  HAL_EnableIrq(IRQ_6);

  return wasWrote;
}

/*****************************************************************************
Reads length bytes from uart and places ones to buffer.
Parameters:
  descriptor - uart descriptor;
  buffer - pointer to a application buffer;
  length - the number of bytes which should be placed to buffer
Returns:
  -1 - bad descriptor, bad number to read;
  number of bytes that were placed to buffer.
*****************************************************************************/
int USBFIFO_ReadUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
{
  uint16_t           wasRead = 0;
  uint16_t           poW;
  uint16_t           poR;
  HalUsartService_t *control;

  if (NULL == descriptor)
    return -1;

  if (!buffer || !length)
    return -1;

  if (descriptor != usbfifoPointDescrip)
    return -1; // Channel is not opened.

  control = &usbfifoPointDescrip->service;
  ATOMIC_SECTION_ENTER
    poW = control->rxPointOfWrite;
    poR = control->rxPointOfRead;
  ATOMIC_SECTION_LEAVE

  while ((poR != poW) && (wasRead < length))
  {
    buffer[wasRead] = descriptor->rxBuffer[poR];
    if (++poR == descriptor->rxBufferLength)
      poR = 0;
    wasRead++;
  }

  ATOMIC_SECTION_ENTER
    control->rxPointOfRead = poR;
    control->rxBytesInBuffer -= wasRead;
  ATOMIC_SECTION_LEAVE

  HAL_EnableIrq(IRQ_7);

  return wasRead;
}

/**************************************************************************//**
\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 USBFIFO_IsTxEmpty(HAL_UsartDescriptor_t *descriptor)
{
  HalUsartService_t *control;
  uint16_t           poW;
  uint16_t           poR;

  if (NULL == descriptor)
    return -1;

  if (descriptor != usbfifoPointDescrip)
    return -1; // Channel is not opened.

  control = &usbfifoPointDescrip->service;
  ATOMIC_SECTION_ENTER
    poW = control->txPointOfWrite;
    poR = control->txPointOfRead;
  ATOMIC_SECTION_LEAVE
  if (poW == poR)
    return 1;

  return 0;
}

// eof usbFifoVirtualUart.c