Commit Graph

95 Commits

Author SHA1 Message Date
hathach 43b255f413 more typos 2022-12-04 19:44:01 +07:00
Jerzy Kasenberg 3133cacc6a nrf5x: Fix reception of large ISO packets
ISO packet size is up to 1023 for full speed device.
Upon completion of ISO reception, reported length of incoming packet
was truncated to one byte only.
This results in incorrect data stream for higher bit rates
48 samples * 4 bytes per sample * 2 channels = 384 bytes of data
and 128 was reported.

There is no change in logic extending xact_len to uint16_t fixes the issue.
2022-09-13 08:11:56 +02:00
Jerzy Kasenberg 5e3cfe7b57 nrf5x: Fix endpoint internal state when closed
Field started (regardind transfer) was only cleaed when transfer
was finished.
For audio devices set interface is called many times.
When there is no audio (silence) set interface requests zero
lenght bandwithd that in turn calls dcd_edpt_close().

When endpoint is closed due to set interface request transfer
should not longer be started since it will block next start transfer
with assert.

This just sets 'started' to false when endpoint is closed.
2022-08-26 08:10:15 +02:00
Jerzy Kasenberg 8b37aa1579 nrf5x: Fix DMA access
There were two problems:
- dma_running flag could be checked in USB interrupt (not set yet) then higher priority
  interrupt could start transfer, check dma_running (not set yet) set it to true start
  DMA; then when USB interrupt continues it starts another DMA that is not allowed
- when DMA is started some registers can't be safely accessed, read can yield invalid
  values (SIZE.EPOUT, SIZE.EPISO)
  current implementation could start DMA for one OUT endpoint then check that another
  endpoint also has data and while DMA was not started right away, SIZE.EPOUT was copied
  already to MAXCNT register. Later on when DMA was started not all data was read from
  endpoint due to incorrect DMA size previously set.

To prevent both cases dma_running is changed in atomic way.
Only code that actually set this value to true starts DMA, code that tried and
had dma_running flag already set simply defers DMA start to USB task.
This eliminates also need to have mutex that was there to limit access to dma_running flag
to one task only.
transfer also now has started flag that is set only after dcd_edpt_xfer() sets up total_len
and actua_len. Previously USB interrupt was disabled when total_len and actual_len were
setup to prevent race condition when data arrived to ednpoint before transfer was setup
was finished.
2022-06-02 17:23:35 +02:00
hathach 606f932d92 added dcd_sof_enable() stubs for all other ports 2022-03-07 23:05:05 +07:00
hathach d10326cb4e rename TUSB_OPT_DEVICE_ENABLED to CFG_TUD_ENABLED
TUSB_OPT_DEVICE_ENABLED still usable for backward compatible
2022-02-25 18:35:21 +07:00
Jerzy Kasenberg 36b6ed8ff9 nrf5x: Fix EP OUT race conditions in OS build
When two tasks entered dcd_edpt_xfer() it was possible that
first disabled interrupt to setup total_len and actual_len
but second task for another endpoint enabled interrupt
between total_len and actual_len resulting in race
condition with interrupt, hence mutex is added on top of interrupt being blocked.
2022-01-19 09:48:04 +01:00
Jerzy Kasenberg 980ffe3b4e nrf5x: Fix DMA access race condition
In multi-thread mode starting DMA in thread mode was
prone to race condition resulting in infinite loop.
It may happen on single core CPU with strict priority based
tasks scheduler where ready high prio task never yields to
ready low prio task (Mynewt).

Sequence that failed (T1 - low priority task, T2 - high priority task)
- T1 called start_dma()
- T1 set _dcd.dma_running (DMA not started yet, context switch happens)
- T2 took CPU and saw that _dcd.dma_running is set, so waits for _dcd.dma_running to be 0
- T1 never gets CPU again, DMA is not started T2 waits forever

OSAL mutex resolves problem of DMA starting from thread-context.
2022-01-19 09:06:43 +01:00
Ha Thach 983abfd6d8
Merge pull request #1279 from kasjer/kasjer/nrf5x-int-race
nrf5x: Fix EP OUT race conditions
2022-01-19 10:33:37 +07:00
Jerzy Kasenberg f4725120a4 nrf5x: Request HFXO via OS
Mynewt (similar to Soft Device) has its own reference counting for
HFXO oscillator.
So far TinyUSB requested HFXO when VBUS was detected and stopped when
VBUS was removed.
But with Mynewt running HFXO can be stopped when other interested parties
don't require HFXO anymore. This results in very difficult to track
USB transmission errors.

This change enables Mynewt specific HFXO management in Soft Device fashion.
2022-01-18 08:12:49 +01:00
Jerzy Kasenberg da44fe3fc9 nrf5x: Fix EP OUT race conditions
When dcd_edpt_xfer() starts new transfer two separate problems were observed.
For both problems stream of OUT packets was pouring from host.

First problem was that total_len and actual_len were not atomic.
In case where incoming OUT packets are less (63) than MPS (64), actual_len and total_len
are set 63.
Then transfer complete from USBD is called that will schedule next 64 bytes transfer.
At that point incoming packet would start DMA if there is place in RAM, normally
it does not happen since actual_len == total_len.
If packets arrives and interrupt is raised after total_len is set (64) but actual_len is still 63 from
previous transfer, interrupt code sees that there is place in ram (1 byte) and transfer this 1 byte
to buffer that was already filled with previous packet.
To remedy this USB interrupt is blocked during transfer setup.

Second problem can happen when dcd_edpt_xfer setups xfer->total_len and actual_len correctly
but then context switch happens before xfer->data_received is checked.
If during this time two packets arrive one will be copied to RAM second will stay in endpoint with
data_received set to 1.
Then when xfer_edpt_xfer() checks data_receive flag it starts DMA again overwriting data.
To remedy this, data_received is checked together with check if data was already transferred.
If transfer was complete, there is no need to start DMA yet.
In such case data_received will be handled in same place by next xfer_edpt_xfer() correctly.
2022-01-14 09:46:39 +01:00
Jerzy Kasenberg 21db2351fd nrf5x: Fix race condition during startup
When NRF5x device is reset by software (after DFU for example),
power event is ready from the beginning.
When power interrupt is triggered before tud_init() finished
USBD_IRQn is enabled before it would be enabled in tud_init().
This in turn may result in BUS RESET event being sent from
USB interrupt to USB task when queue is not initialized yet.
This scenario often happens in Mynewt build where queue creation
takes more time.

To prevent this scenario USBD_IRQn is not enabled in power event
interrupt handler before dcd_init() was called.
2021-12-08 08:27:27 +01:00
hathach c9e9f4785f more clean up 2021-11-23 09:52:11 +07:00
hathach 0fc11746c0 clean up 2021-11-23 09:46:45 +07:00
hathach a994540860 fix nrf easy dma race condition 2021-11-23 09:36:28 +07:00
hathach 5af989384b remove ep descriptor wMaxPacketSize bitfield due to endian issue 2021-10-24 13:11:21 +07:00
hathach 0b249618b0 fix -Wcast-qual 2021-10-15 23:54:31 +07:00
hathach ad21b69277 fix nrf clear data toggle sequence when clearing stall 2021-08-29 12:05:34 +07:00
hathach 66c292e2ec fix a couple of nrf dcd issue
- limit out xact dma to prevent usbd overflow in certain situation after
stalled
- drained already acked data when stalling an OUT endpoint
2021-08-29 00:34:21 +07:00
hathach 8bad0af849 explicitly clear stall and data toggle for edpoint upon open() 2021-08-27 22:31:08 +07:00
hathach 81c73c235f implement dcd_edpt_close_all() for nrf52840 2021-08-27 22:30:30 +07:00
hathach 71e77e47fa
add dcd_edpt_close_all() for clear existing configured state
correctly responded to TD 9.13 Set Configuration Test
2021-08-26 17:07:03 +07:00
hathach ab2eec77d4 complete suspend, resume, remote wakeup for nrf52 2021-08-16 20:22:14 +07:00
DuMaM b5ce269675
GCC 11 build fix
During Adafruit Bootloader compilation, I spotted bellow error which do not allow me build project.

``` c
    inlined from 'hfclk_running' at lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c:785:13:
lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c:792:31: error: 'is_running' may be used uninitialized [-Werror=maybe-uninitialized]
  792 |     return (is_running ? true : false);
      |            ~~~~~~~~~~~~~~~~~~~^~~~~~~~
```
2021-06-26 17:03:46 +02:00
hathach 6b621baeb3 fix race condition with control since TASKS_EP0RCVOUT also require EasyDMA 2021-06-19 01:58:27 +07:00
hathach 9233269a2c only apply errata walkaround for nrf52 2021-05-11 18:38:23 +07:00
Jerzy Kasenberg e2f795067a Allow build for NRF5340 MCU
Errata code referred to NRF_USBD_BASE.
This definition is not present in NRF5340 but both NRF52 and NRF53
do have NRF_USBD which maps to NRF_USBD_BASE for NRF52 and
to NRF_USBD_S_BASE for NRF5340.
This just make build possible for NRF5340.
2021-05-11 12:37:08 +02:00
hathach 64f41dea62 fix race condition that could cause drop packet of Bulk OUT transfer
NRF_USBD->SIZE.EPOUT[epnum] only need to write once to enable
Bulk/Interrupt transfer. We only need to do it in dcd_edpt_open() and
dcd_edpt_clear_stall()
2021-03-18 15:07:07 +07:00
hathach 6e6e6265e4 use dcd_event_bus_reset() with speed to replace bus_signal 2021-01-08 22:34:36 +07:00
Jerzy Kasenberg 96da1ca4b8 nrf5x: Add support for ISO endpoints
ISO endpoints were not covered so far by the driver code.
This adds support for ISO IN and OUT endpoint handling.
Registers for ISO IN(OUT) endpoints are placed just after normal IN(OUT)
so in some cases common code could be used for handling all type of
transfers.
Generally code synchronizes ISO endpoint handling to SOF interrupt.
This code does not change the way of how non-ISO endpoints are treated.

Code uses strategy outlined in nRF52840 Produce Specification v1.0
sections 6.35.11.1 and 6.35.11.2.
2020-10-01 09:22:55 +02:00
Jerzy Kasenberg 6f5ee09511 nrf5x: Increase size of mps to 16 bits
msp stores max packet size.
For ISO endpoints 8 bits is not enough so it's changed to 16 bits.
2020-10-01 09:22:55 +02:00
Jerzy Kasenberg fceb8853c7 nrf5x: Add dcd_edpt_close
Closing endpoints can be important when there are alternate
instances. This adds functionality of closing endpoints
similar to what exists in other drivers.
2020-10-01 09:22:55 +02:00
Jerzy Kasenberg 62a76c0e04 nrf52: Fix edpt_dma_start() wrong condition check
Operator < used in while condition was obviously incorrect.
Loop starts with checking if unsigned variable is less then 0.
This condition is always false.

This reverses condition to follow intention of of the code.
2020-09-15 16:08:23 +02:00
hathach 7d9efd0697 manually submit unplugged event for nrf dcd_disconnect() 2020-08-04 14:18:12 +07:00
hathach 9bf2b33366
correct isr context for nrf DCD_EVENT_UNPLUGGED
also rename debug lookup to prevent conflict
2020-08-01 12:02:59 +07:00
hathach ff9994116e fix nrf hanged (blocking wait) when called within critical section 2020-05-21 21:22:12 +07:00
hathach 58b99e59d4 detect if SD is actually present on the flash using SD magic
even with SOFTDEVICE_PRESENT defined, SD may not be present on actual
flash.
2020-05-05 23:07:56 +07:00
hathach 6f9c256ad0
complete remove dcd_set_config(), fix unit test 2020-04-17 13:52:34 +07:00
hathach 50be9d7c3a
mass rename tud/dcd_irq_handler to tud/dcd_init_handler 2020-04-17 12:27:53 +07:00
Ha Thach 04a06ec401
Merge branch 'master' into refactor-irqhandler 2020-04-11 15:49:34 +07:00
hathach c1f3fbbc03 implement dcd connect/disconnect for samd and nrf 2020-04-09 11:47:02 +07:00
hathach 4179334aca call tud_irq_handler() for all nrf5x board 2020-04-06 17:35:11 +07:00
hathach c8247f0907 fix zlp for nrf52840 2020-03-08 14:20:28 +07:00
hathach 4008f0d1e6 update dcd nrf5x to be indepent from nrf_usbd.h
fix build error with nrfx 2.0
2019-11-11 00:01:12 +07:00
hathach 47cd08d526 clean up 2019-08-05 22:31:41 +07:00
hathach d1df041519 nrf5x remove the depenedent on nrfx_power.h 2019-08-05 22:09:37 +07:00
hathach d211035a0a merge dcd/hal_nrf5x.c into dcd/dcd_nrf5x.c 2019-08-05 21:54:20 +07:00
hathach 73d7ab201e
remove dcd_edpt_busy() 2019-06-10 22:29:18 +07:00
hathach 3e6d911ce9
more clean up use inline bit funciton instead of macros 2019-05-14 12:54:29 +07:00
hathach 61ec407752
update license year to 2019 2019-05-14 11:48:05 +07:00