24#define mem_debug printf
30#pragma data_alignment=32
31static uint8_t _mem_pool[MEM_POOL_UNIT_NUM][MEM_POOL_UNIT_SIZE];
33static uint8_t _mem_pool[MEM_POOL_UNIT_NUM][MEM_POOL_UNIT_SIZE]
__attribute__((aligned(32)));
35static uint8_t _unit_used[MEM_POOL_UNIT_NUM];
37static volatile int _usbh_mem_used;
38static volatile int _usbh_max_mem_used;
39static volatile int _mem_pool_used;
44uint8_t _dev_addr_pool[128];
45static volatile int _device_addr;
53void usbh_memory_init(
void)
55 if (
sizeof(TD_T) > MEM_POOL_UNIT_SIZE)
57 USB_error(
"TD_T - MEM_POOL_UNIT_SIZE too small!\n");
61 if (
sizeof(ED_T) > MEM_POOL_UNIT_SIZE)
63 USB_error(
"ED_T - MEM_POOL_UNIT_SIZE too small!\n");
68 _usbh_max_mem_used = 0L;
70 memset(_unit_used, 0,
sizeof(_unit_used));
76 memset(_dev_addr_pool, 0,
sizeof(_dev_addr_pool));
80uint32_t usbh_memory_used(
void)
82 printf(
"USB static memory: %d/%d, heap used: %d\n", _mem_pool_used, MEM_POOL_UNIT_NUM, _usbh_mem_used);
83 return _usbh_mem_used;
86static void memory_counter(
int size)
88 _usbh_mem_used += size;
89 if (_usbh_mem_used > _usbh_max_mem_used)
90 _usbh_max_mem_used = _usbh_mem_used;
93void * usbh_alloc_mem(
int size)
100 USB_error(
"usbh_alloc_mem failed! %d\n", size);
105 memory_counter(size);
109void usbh_free_mem(
void *p,
int size)
112 memory_counter(0-size);
120UDEV_T * alloc_device(
void)
124 udev = malloc(
sizeof(*udev));
127 USB_error(
"alloc_device failed!\n");
130 memset(udev, 0,
sizeof(*udev));
131 memory_counter(
sizeof(*udev));
133 udev->next = g_udev_list;
138void free_device(UDEV_T *udev)
145 if (udev->cfd_buff !=
NULL)
146 usbh_free_mem(udev->cfd_buff, MAX_DESC_BUFF_SIZE);
151 if (g_udev_list == udev)
153 g_udev_list = g_udev_list->next;
162 d->next = udev->next;
170 memory_counter(-
sizeof(*udev));
173int alloc_dev_address(
void)
177 if (_device_addr >= 128)
182 if (_dev_addr_pool[_device_addr] == 0)
184 _dev_addr_pool[_device_addr] = 1;
188 if (_device_addr >= 128)
193void free_dev_address(
int dev_addr)
196 _dev_addr_pool[dev_addr] = 0;
203UTR_T * alloc_utr(UDEV_T *udev)
207 utr = malloc(
sizeof(*utr));
210 USB_error(
"alloc_utr failed!\n");
213 memory_counter(
sizeof(*utr));
214 memset(utr, 0,
sizeof(*utr));
216 mem_debug(
"[ALLOC] [UTR] - 0x%x\n", (
int)utr);
220void free_utr(UTR_T *utr)
225 mem_debug(
"[FREE] [UTR] - 0x%x\n", (
int)utr);
227 memory_counter(0-(
int)
sizeof(*utr));
234ED_T * alloc_ohci_ED(
void)
239 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
241 if (_unit_used[i] == 0)
245 ed = (ED_T *)&_mem_pool[i];
246 memset(ed, 0,
sizeof(*ed));
247 mem_debug(
"[ALLOC] [ED] - 0x%x\n", (
int)ed);
251 USB_error(
"alloc_ohci_ED failed!\n");
255void free_ohci_ED(ED_T *ed)
259 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
261 if ((uint32_t)&_mem_pool[i] == (uint32_t)ed)
263 mem_debug(
"[FREE] [ED] - 0x%x\n", (
int)ed);
269 USB_debug(
"free_ohci_ED - not found! (ignored in case of multiple UTR)\n");
275TD_T * alloc_ohci_TD(UTR_T *utr)
280 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
282 if (_unit_used[i] == 0)
286 td = (TD_T *)&_mem_pool[i];
288 memset(td, 0,
sizeof(*td));
290 mem_debug(
"[ALLOC] [TD] - 0x%x\n", (
int)td);
294 USB_error(
"alloc_ohci_TD failed!\n");
298void free_ohci_TD(TD_T *td)
302 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
304 if ((uint32_t)&_mem_pool[i] == (uint32_t)td)
306 mem_debug(
"[FREE] [TD] - 0x%x\n", (
int)td);
312 USB_error(
"free_ohci_TD - not found!\n");
318QH_T * alloc_ehci_QH(
void)
323 for (i = (_sidx+1) % MEM_POOL_UNIT_NUM; i != _sidx; i = (i+1) % MEM_POOL_UNIT_NUM)
325 if (_unit_used[i] == 0)
330 qh = (QH_T *)&_mem_pool[i];
331 memset(qh, 0,
sizeof(*qh));
332 mem_debug(
"[ALLOC] [QH] - 0x%x\n", (
int)qh);
338 USB_error(
"alloc_ehci_QH failed!\n");
341 qh->Curr_qTD = QTD_LIST_END;
342 qh->OL_Next_qTD = QTD_LIST_END;
343 qh->OL_Alt_Next_qTD = QTD_LIST_END;
344 qh->OL_Token = QTD_STS_HALT;
348void free_ehci_QH(QH_T *qh)
352 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
354 if ((uint32_t)&_mem_pool[i] == (uint32_t)qh)
356 mem_debug(
"[FREE] [QH] - 0x%x\n", (
int)qh);
362 USB_debug(
"free_ehci_QH - not found! (ignored in case of multiple UTR)\n");
368qTD_T * alloc_ehci_qTD(UTR_T *utr)
373 for (i = (_sidx+1) % MEM_POOL_UNIT_NUM; i != _sidx; i = (i+1) % MEM_POOL_UNIT_NUM)
375 if (_unit_used[i] == 0)
380 qtd = (qTD_T *)&_mem_pool[i];
382 memset(qtd, 0,
sizeof(*qtd));
383 qtd->Next_qTD = QTD_LIST_END;
384 qtd->Alt_Next_qTD = QTD_LIST_END;
385 qtd->Token = 0x1197B3F;
387 mem_debug(
"[ALLOC] [qTD] - 0x%x\n", (
int)qtd);
391 USB_error(
"alloc_ehci_qTD failed!\n");
395void free_ehci_qTD(qTD_T *qtd)
399 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
401 if ((uint32_t)&_mem_pool[i] == (uint32_t)qtd)
403 mem_debug(
"[FREE] [qTD] - 0x%x\n", (
int)qtd);
409 USB_error(
"free_ehci_qTD 0x%x - not found!\n", (
int)qtd);
415iTD_T * alloc_ehci_iTD(
void)
420 for (i = (_sidx+1) % MEM_POOL_UNIT_NUM; i != _sidx; i = (i+1) % MEM_POOL_UNIT_NUM)
422 if (i+2 >= MEM_POOL_UNIT_NUM)
425 if ((_unit_used[i] == 0) && (_unit_used[i+1] == 0))
427 _unit_used[i] = _unit_used[i+1] = 1;
430 itd = (iTD_T *)&_mem_pool[i];
431 memset(itd, 0,
sizeof(*itd));
432 mem_debug(
"[ALLOC] [iTD] - 0x%x\n", (
int)itd);
436 USB_error(
"alloc_ehci_iTD failed!\n");
440void free_ehci_iTD(iTD_T *itd)
444 for (i = 0; i+1 < MEM_POOL_UNIT_NUM; i++)
446 if ((uint32_t)&_mem_pool[i] == (uint32_t)itd)
448 mem_debug(
"[FREE] [iTD] - 0x%x\n", (
int)itd);
449 _unit_used[i] = _unit_used[i+1] = 0;
454 USB_error(
"free_ehci_iTD 0x%x - not found!\n", (
int)itd);
460siTD_T * alloc_ehci_siTD(
void)
465 for (i = (_sidx+1) % MEM_POOL_UNIT_NUM; i != _sidx; i = (i+1) % MEM_POOL_UNIT_NUM)
467 if (_unit_used[i] == 0)
472 sitd = (siTD_T *)&_mem_pool[i];
473 memset(sitd, 0,
sizeof(*sitd));
474 mem_debug(
"[ALLOC] [siTD] - 0x%x\n", (
int)sitd);
478 USB_error(
"alloc_ehci_siTD failed!\n");
482void free_ehci_siTD(siTD_T *sitd)
486 for (i = 0; i < MEM_POOL_UNIT_NUM; i++)
488 if ((uint32_t)&_mem_pool[i] == (uint32_t)sitd)
490 mem_debug(
"[FREE] [siTD] - 0x%x\n", (
int)sitd);
496 USB_error(
"free_ehci_siTD 0x%x - not found!\n", (
int)sitd);
void *__dso_handle __attribute__((weak))
NuMicro peripheral access layer header file.
#define NULL
NULL pointer.
USB Host library header file.