summaryrefslogtreecommitdiff
path: root/bsp/at32/libraries/usbotg_library/src/usb_core.c
blob: f35411bd8982b255e9141fed6ebf254bb552b253 (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
/**
  **************************************************************************
  * @file     usb_core.c
  * @brief    usb driver
  **************************************************************************
  *                       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 "usb_core.h"

/** @defgroup USB_drivers_core
  * @brief usb global drivers core
  * @{
  */

/** @defgroup USB_core_private_functions
  * @{
  */

usb_sts_type usb_core_config(otg_core_type *udev, uint8_t core_id);

/**
  * @brief  usb core config
  * @param  otgdev: to the structure of otg_core_type
  * @param  core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
  * @retval usb_sts_type
  */
usb_sts_type usb_core_config(otg_core_type *otgdev, uint8_t core_id)
{
  /* set usb speed and core id */
  otgdev->cfg.speed = core_id;
  otgdev->cfg.core_id = core_id;

  /* default sof out and vbus ignore */
  otgdev->cfg.sof_out = FALSE;
  otgdev->cfg.vbusig = FALSE;

  /* set max size */
  otgdev->cfg.max_size = 64;

  /* set support number of channel and endpoint */
#ifdef USE_OTG_HOST_MODE
  otgdev->cfg.hc_num = USB_HOST_CHANNEL_NUM;
#endif
#ifdef USE_OTG_DEVICE_MODE
  otgdev->cfg.ept_num = USB_EPT_MAX_NUM;
#endif
  otgdev->cfg.fifo_size = OTG_FIFO_SIZE;
  if(core_id == USB_FULL_SPEED_CORE_ID)
  {
    otgdev->cfg.phy_itface = 2;
  }
#ifdef USB_SOF_OUTPUT_ENABLE
  otgdev->cfg.sof_out = TRUE;
#endif

#ifdef USB_VBUS_IGNORE
  otgdev->cfg.vbusig = TRUE;
#endif
  otgdev->cfg.dma_en = FALSE;

#ifdef OTG_USE_DMA
  otgdev->cfg.dma_en = TRUE;
#endif

  return USB_OK;
}

#ifdef USE_OTG_DEVICE_MODE
/**
  * @brief  usb device initialization
  * @param  otgdev: to the structure of otg_core_type
  * @param  core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
  * @param  usb_id: select use OTG1 or OTG2
  *         this parameter can be one of the following values:
  *         - USB_OTG1_ID
  *         - USB_OTG2_ID
  * @param  dev_handler: usb class callback handler
  * @param  desc_handler: device config callback handler
  * @retval usb_sts_type
  */
usb_sts_type usbd_init(otg_core_type *otgdev,
                  uint8_t core_id, uint8_t usb_id)
{
  usb_sts_type usb_sts = USB_OK;

  /* select use OTG1 or OTG2 */
  otgdev->usb_reg = usb_global_select_core(usb_id);

  /* usb device core config */
  usb_core_config(otgdev, core_id);

  if(otgdev->cfg.sof_out)
  {
    otgdev->usb_reg->gccfg_bit.sofouten = TRUE;
  }

  if(otgdev->cfg.vbusig)
  {
    otgdev->usb_reg->gccfg_bit.vbusig = TRUE;
  }
  if(otgdev->cfg.dma_en)
  {
    otgdev->dev.dma_en = TRUE;
  }
  else
  {
    otgdev->dev.dma_en = FALSE;
  }
  /* usb device core init */
  usbd_core_init(&(otgdev->dev), otgdev->usb_reg,
                core_id);

  return usb_sts;
}
#endif

#ifdef USE_OTG_HOST_MODE

/**
  * @brief  usb host initialization.
  * @param  otgdev: to the structure of otg_core_type
  * @param  core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
  * @param  usb_id: select use OTG1 or OTG2
  *         this parameter can be one of the following values:
  *         - USB_OTG1_ID
  *         - USB_OTG2_ID
  * @param  class_handler: usb class callback handler
  * @param  user_handler: user callback handler
  * @retval usb_sts_type
  */
usb_sts_type usbh_init(otg_core_type *otgdev,
                  uint8_t core_id, uint8_t usb_id)
{
  usb_sts_type status = USB_OK;

  /* select use otg1 or otg2 */
  otgdev->usb_reg = usb_global_select_core(usb_id);

  /* usb core config */
  usb_core_config(otgdev, core_id);

  if(otgdev->cfg.sof_out)
  {
    otgdev->usb_reg->gccfg_bit.sofouten = TRUE;
  }

  if(otgdev->cfg.vbusig)
  {
    otgdev->usb_reg->gccfg_bit.vbusig = TRUE;
  }
  if(otgdev->cfg.dma_en)
  {
    otgdev->host.dma_en = TRUE;
  }
  else
  {
    otgdev->host.dma_en = FALSE;
  }

  /* usb host core init */
  usbh_core_init(&otgdev->host, otgdev->usb_reg,
                core_id);

  return status;
}
#endif

/**
  * @}
  */

/**
  * @}
  */