summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/usbd_library/src/usbd_int.c
blob: 4684d576ada88930ce7f870b6008a9f3f1f9dc6b (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
  **************************************************************************
  * @file     usbd_int.c
  * @brief    usb interrupt request
  **************************************************************************
  *                       Copyright notice & Disclaimer
  *
  * The software Board Support Package (BSP) that is made available to
  * download from Artery official website is the copyrighted work of Artery.
  * Artery authorizes customers to use, copy, and distribute the BSP
  * software and its related documentation for the purpose of design and
  * development in conjunction with Artery microcontrollers. Use of the
  * software is governed by this copyright notice and the following disclaimer.
  *
  * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  *
  **************************************************************************
  */

#include "usbd_int.h"

/** @defgroup USBD_drivers_interrupt
  * @brief usb device interrupt
  * @{
  */

/** @defgroup USBD_int_private_functions
  * @{
  */

/**
  * @brief  usb device interrput request handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_irq_handler(usbd_core_type *udev)
{
  usbd_type *usbx = udev->usb_reg;
  uint32_t sts_val = usbx->intsts;
  uint32_t sts_ien = usbx->ctrl;

  if(sts_val & USB_TC_FLAG)
  {
    /* endpoint tc interrupt handler */
    usbd_ept_loop_handler(udev);
  }

  if(sts_val & USB_RST_FLAG)
  {
    /* clear reset flag */
    usb_flag_clear(usbx, USB_RST_FLAG);

    /* reset interrupt handler */
    usbd_reset_handler(udev);
  }

  if((sts_val & USB_SOF_FLAG) &&
    (sts_ien & USB_SOF_INT))
  {
    /* clear sof flag */
    usb_flag_clear(usbx, USB_SOF_FLAG);

    /* sof interrupt handler */
    usbd_sof_callback(udev);
  }

  if((sts_val & USB_LSOF_FLAG) &&
    (sts_ien & USB_LSOF_INT))
  {
    /* clear lsof flag */
    usb_flag_clear(usbx, USB_LSOF_FLAG);
  }

  if((sts_val & USB_SP_FLAG) &&
    (sts_ien & USB_SP_INT))
  {
    /* clear suspend flag */
    usb_flag_clear(usbx, USB_SP_FLAG);

    /* usb suspend interrupt handler */
    usbd_suspend_handler(udev);
  }

  if((sts_val & USB_WK_FLAG) &&
    (sts_ien & USB_WK_INT))
  {
    /* usb wakeup interrupt handler */
    usbd_wakeup_handler(udev);

    /* clear wakeup flag */
    usb_flag_clear(usbx, USB_WK_FLAG);
  }
}

/**
  * @brief  usb device endpoint request handler.
  * @param  udev: to the structure of usbd_core_type
  * @param  ept_num: endpoint number
  * @retval none
  */
void usbd_eptn_handler(usbd_core_type *udev, usb_ept_number_type ept_num)
{
  usbd_type *usbx = udev->usb_reg;
  usb_ept_info *ept_info;
  uint32_t ept_val = usbx->ept[ept_num];
  uint16_t length;

  /* in interrupt request  */
  if(ept_val & USB_TXTC)
  {
    /* get endpoint register and in transfer info struct */
    ept_info = &udev->ept_in[ept_num];

    /* clear endpoint tc flag */
    USB_CLEAR_TXTC(ept_num);

    /* get endpoint tx length */
    ept_info->trans_len = USB_GET_TX_LEN(ept_num);

    /* offset the trans buffer */
    ept_info->trans_buf += ept_info->trans_len;

    if(ept_info->total_len == 0 || ept_num == USB_EPT0)
    {
      /* in transfer complete */
      usbd_data_in_stage_callback(udev,ept_num);
    }
    else
    {
      /* endpoint continue send data */
      usbd_ept_send(udev, ept_num, ept_info->trans_buf, ept_info->total_len);
    }
    /* set the host assignment address */
    if(udev->conn_state == USB_CONN_STATE_ADDRESSED && udev->device_addr > 0)
    {
      usb_set_address(udev->usb_reg, udev->device_addr);
      udev->device_addr = 0;
    }
  }
  else
  {
    /* setup and out interrupt request */

    /* get endpoint register and out transfer info struct */
    ept_info = &udev->ept_out[ept_num];

    if((ept_val & USB_SETUPTC) != 0)
    {
      /* endpoint setup interrupt request */

      /* get endpoint received data length */
      ept_info->trans_len = USB_GET_RX_LEN(ept_num);

      /* read endpoint received data */
      usb_read_packet(udev->setup_buffer, ept_info->rx_addr, ept_info->trans_len);

      /* clear endpoint rx tc flag */
      USB_CLEAR_RXTC(USB_EPT0);

      /* endpoint setup complete handler */
      usbd_core_setup_handler(udev, ept_num);
      usbd_setup_phase_done_callback(udev);
    }
    else if(ept_val & USB_RXTC )
    {
      /* endpoint out interrupt request */
      USB_CLEAR_RXTC(ept_num);

      if(ept_info->is_double_buffer == 0)
      {
        /* get endpoint received data length */
        length = USB_GET_RX_LEN(ept_num);

        /* read endpoint received data */
        usb_read_packet(ept_info->trans_buf, ept_info->rx_addr, length);
      }
      else
      {
        if( ept_val & USB_RXDTS)
        {
          length = USB_DBUF0_GET_LEN(ept_num);
          usb_read_packet(ept_info->trans_buf, ept_info->tx_addr, length);
        }
        else
        {
          length = USB_DBUF1_GET_LEN(ept_num);
          usb_read_packet(ept_info->trans_buf, ept_info->rx_addr, length);
        }
        USB_FREE_DB_USER_BUFFER(ept_num, DATA_TRANS_OUT);
      }

      /* set received data length */
      ept_info->trans_len += length;
      ept_info->trans_buf += length;

      if(ept_info->total_len == 0 || length < ept_info->maxpacket || ept_num == USB_EPT0)
      {
        /* out transfer complete */
        usbd_data_out_stage_callback(udev, ept_num);
      }
      else
      {
        /* endpoint continue receive data  */
        usbd_ept_recv(udev, ept_num, ept_info->trans_buf, ept_info->total_len);
      }
    }
  }
}

/**
  * @brief  usb device endpoint loop handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_ept_loop_handler(usbd_core_type *udev)
{
  usbd_type *usbx = udev->usb_reg;
  usb_ept_number_type ept_num = USB_EPT0;
  uint32_t sts_val;

  while((sts_val = usbx->intsts) & USB_TC_FLAG)
  {
    /* get the interrupt endpoint number */
    ept_num = (usb_ept_number_type)(sts_val & USB_EPT_NUM_FLAG);

    usbd_eptn_handler(udev, ept_num);
  }
}

/**
  * @brief  usb device reset interrupt request handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_reset_handler(usbd_core_type *udev)
{
  /* free usb buffer */
  usb_buffer_free();

  usbd_ept_defaut_init(udev);
#ifndef USB_EPT_AUTO_MALLOC_BUFFER
  usbd_ept_buf_custom_define(udev, 0x80, EPT0_TX_ADDR);
  usbd_ept_buf_custom_define(udev, 0x00, EPT0_RX_ADDR);
#endif

  /* open endpoint 0 out */
  usbd_ept_open(udev, 0x00, EPT_CONTROL_TYPE, 0x40);

  /* open endpoint 0 in */
  usbd_ept_open(udev, 0x80, EPT_CONTROL_TYPE, 0x40);

  /* set device address to 0 */
  usb_set_address(udev->usb_reg, 0);

  /* usb connect state set to default */
  udev->conn_state = USB_CONN_STATE_DEFAULT;

  /* user define reset event */
  usbd_reset_callback(udev);
}

/**
  * @brief  usb device sof interrupt request handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_sof_handler(usbd_core_type *udev)
{
  /* user sof handler in class define*/
  if(udev->class_handler->sof_handler)
    udev->class_handler->sof_handler(udev);
}

/**
  * @brief  usb device suspend interrupt request handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_suspend_handler(usbd_core_type *udev)
{
  /* save connect state */
  udev->old_conn_state = udev->conn_state;

  /* set current state to suspend */
  udev->conn_state = USB_CONN_STATE_SUSPENDED;

  /* enter suspend mode */
  usbd_enter_suspend(udev);
}

/**
  * @brief  usb device wakup interrupt request handler.
  * @param  udev: to the structure of usbd_core_type
  * @retval none
  */
void usbd_wakeup_handler(usbd_core_type *udev)
{
  /* exit suspend mode */
  usb_exit_suspend(udev->usb_reg);

  /* restore connect state */
  udev->conn_state = udev->old_conn_state;

  /* user define wakeup event */
}

__weak void usbd_reset_callback(usbd_core_type *udev)
{
}

__weak void usbd_setup_phase_done_callback(usbd_core_type *udev)
{
}

__weak void usbd_data_in_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
}

__weak void usbd_sof_callback(usbd_core_type *udev)
{
}

__weak void usbd_data_out_stage_callback(usbd_core_type *udev, uint32_t ept_num)
{
}

__weak void usbd_connectCallback(usbd_core_type *udev)
{
}

__weak void usbd_disconnectCallback(usbd_core_type *udev)
{
}

/**
  * @}
  */

/**
  * @}
  */