Skip to content

Commit b66c1b9

Browse files
hujun260xiaoxiang781216
authored andcommitted
use small lock in following files:
arch/arm/src/rp2040/rp2040_usbdev.c arch/arm/src/rp23xx/rp23xx_uart.c arch/arm/src/s32k1xx/s32k1xx_enet.c arch/arm/src/sam34/sam_lowputc.c arch/arm/src/sama5/sam_lowputc.c arch/arm/src/samd2l2/sam_serial.c arch/arm/src/samd5e5/sam_serial.c arch/arm/src/samv7/sam_lowputc.c arch/arm/src/stm32/stm32_hciuart.c arch/arm/src/stm32/stm32_qencoder.c arch/arm/src/stm32/stm32_serial.c arch/arm/src/stm32f0l0g0/stm32_serial_v1.c Signed-off-by: hujun5 <[email protected]>
1 parent c96b8cd commit b66c1b9

File tree

12 files changed

+161
-133
lines changed

12 files changed

+161
-133
lines changed

arch/arm/src/rp2040/rp2040_usbdev.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ struct rp2040_usbdev_s
267267

268268
uint16_t next_offset; /* Unused DPSRAM buffer offset */
269269
uint8_t dev_addr; /* USB device address */
270+
spinlock_t lock; /* USB device address */
270271
enum rp2040_zlp_e zlp_stat; /* Pending EP0 ZLP status */
271272
uint16_t used; /* used epphy */
272273
bool stalled;
@@ -496,6 +497,7 @@ static void rp2040_update_buffer_control(struct rp2040_ep_s *privep,
496497
static int rp2040_epwrite(struct rp2040_ep_s *privep, uint8_t *buf,
497498
uint16_t nbytes)
498499
{
500+
struct rp2040_usbdev_s *priv = privep->dev;
499501
uint32_t val;
500502
irqstate_t flags;
501503

@@ -513,9 +515,9 @@ static int rp2040_epwrite(struct rp2040_ep_s *privep, uint8_t *buf,
513515

514516
/* Start the transfer */
515517

516-
flags = spin_lock_irqsave(NULL);
518+
flags = spin_lock_irqsave(&priv->lock);
517519
rp2040_update_buffer_control(privep, 0, val);
518-
spin_unlock_irqrestore(NULL, flags);
520+
spin_unlock_irqrestore(&priv->lock, flags);
519521

520522
return nbytes;
521523
}
@@ -530,6 +532,7 @@ static int rp2040_epwrite(struct rp2040_ep_s *privep, uint8_t *buf,
530532

531533
static int rp2040_epread(struct rp2040_ep_s *privep, uint16_t nbytes)
532534
{
535+
struct rp2040_usbdev_s *priv = privep->dev;
533536
uint32_t val;
534537
irqstate_t flags;
535538

@@ -542,9 +545,9 @@ static int rp2040_epread(struct rp2040_ep_s *privep, uint16_t nbytes)
542545

543546
/* Start the transfer */
544547

545-
flags = spin_lock_irqsave(NULL);
548+
flags = spin_lock_irqsave(&priv->lock);
546549
rp2040_update_buffer_control(privep, 0, val);
547-
spin_unlock_irqrestore(NULL, flags);
550+
spin_unlock_irqrestore(&priv->lock, flags);
548551

549552
return OK;
550553
}
@@ -1708,15 +1711,12 @@ static int rp2040_epcancel(struct usbdev_ep_s *ep,
17081711
*
17091712
****************************************************************************/
17101713

1711-
static int rp2040_epstall_exec(struct usbdev_ep_s *ep)
1714+
static int rp2040_epstall_exec_nolock(struct usbdev_ep_s *ep)
17121715
{
17131716
struct rp2040_ep_s *privep = (struct rp2040_ep_s *)ep;
1714-
irqstate_t flags;
17151717

17161718
usbtrace(TRACE_EPSTALL, privep->epphy);
17171719

1718-
flags = spin_lock_irqsave(NULL);
1719-
17201720
if (privep->epphy == 0)
17211721
{
17221722
setbits_reg32(privep->in ?
@@ -1731,10 +1731,22 @@ static int rp2040_epstall_exec(struct usbdev_ep_s *ep)
17311731

17321732
privep->pending_stall = false;
17331733

1734-
spin_unlock_irqrestore(NULL, flags);
17351734
return OK;
17361735
}
17371736

1737+
static int rp2040_epstall_exec(struct usbdev_ep_s *ep)
1738+
{
1739+
struct rp2040_ep_s *privep = (struct rp2040_ep_s *)ep;
1740+
struct rp2040_usbdev_s *priv = privep->dev;
1741+
irqstate_t flags;
1742+
int ret;
1743+
1744+
flags = spin_lock_irqsave(&priv->lock);
1745+
ret = rp2040_epstall_exec_nolock(ep);
1746+
spin_unlock_irqrestore(&priv->lock, flags);
1747+
return ret;
1748+
}
1749+
17381750
/****************************************************************************
17391751
* Name: rp2040_epstall
17401752
*
@@ -1749,7 +1761,7 @@ static int rp2040_epstall(struct usbdev_ep_s *ep, bool resume)
17491761
struct rp2040_usbdev_s *priv = privep->dev;
17501762
irqstate_t flags;
17511763

1752-
flags = spin_lock_irqsave(NULL);
1764+
flags = spin_lock_irqsave(&priv->lock);
17531765

17541766
if (resume)
17551767
{
@@ -1784,13 +1796,13 @@ static int rp2040_epstall(struct usbdev_ep_s *ep, bool resume)
17841796
{
17851797
/* Stall immediately */
17861798

1787-
rp2040_epstall_exec(ep);
1799+
rp2040_epstall_exec_nolock(ep);
17881800
}
17891801

17901802
priv->zlp_stat = RP2040_ZLP_NONE;
17911803
}
17921804

1793-
spin_unlock_irqrestore(NULL, flags);
1805+
spin_unlock_irqrestore(&priv->lock, flags);
17941806

17951807
return OK;
17961808
}
@@ -2027,6 +2039,7 @@ void arm_usbinitialize(void)
20272039
g_usbdev.eplist[i].ep.eplog = 0;
20282040
}
20292041

2042+
spin_lock_init(&g_usbdev.lock);
20302043
if (irq_attach(RP2040_USBCTRL_IRQ, rp2040_usbinterrupt, &g_usbdev) != 0)
20312044
{
20322045
usbtrace(TRACE_DEVERROR(RP2040_TRACEERR_IRQREGISTRATION),
@@ -2139,7 +2152,7 @@ int usbdev_unregister(struct usbdevclass_driver_s *driver)
21392152

21402153
usbtrace(TRACE_DEVUNREGISTER, 0);
21412154

2142-
flags = spin_lock_irqsave(NULL);
2155+
flags = spin_lock_irqsave(&priv->lock);
21432156

21442157
/* Unbind the class driver */
21452158

@@ -2157,7 +2170,7 @@ int usbdev_unregister(struct usbdevclass_driver_s *driver)
21572170

21582171
priv->driver = NULL;
21592172

2160-
spin_unlock_irqrestore(NULL, flags);
2173+
spin_unlock_irqrestore(&priv->lock, flags);
21612174

21622175
return OK;
21632176
}

arch/arm/src/rp23xx/rp23xx_uart.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@
116116
* Private Data
117117
****************************************************************************/
118118

119+
static spinlock_t g_rp23xx_uart_lock = SP_UNLOCKED;
120+
119121
/****************************************************************************
120122
* Private Functions
121123
****************************************************************************/
@@ -189,7 +191,7 @@ void rp23xx_setbaud(uintptr_t uartbase, uint32_t basefreq, uint32_t baud)
189191
uint32_t div;
190192
uint32_t lcr_h;
191193

192-
irqstate_t flags = spin_lock_irqsave(NULL);
194+
irqstate_t flags = spin_lock_irqsave(&g_rp23xx_uart_lock);
193195

194196
div = basefreq / (16 * baud / 100);
195197
ibrd = div / 100;
@@ -214,5 +216,5 @@ void rp23xx_setbaud(uintptr_t uartbase, uint32_t basefreq, uint32_t baud)
214216
putreg32(lcr_h, uartbase + RP23XX_UART_UARTLCR_H_OFFSET);
215217

216218
finish:
217-
spin_unlock_irqrestore(NULL, flags);
219+
spin_unlock_irqrestore(&g_rp23xx_uart_lock, flags);
218220
}

arch/arm/src/s32k1xx/s32k1xx_enet.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ struct s32k1xx_driver_s
260260
struct work_s pollwork; /* For deferring poll work to the work queue */
261261
struct enet_desc_s *txdesc; /* A pointer to the list of TX descriptor */
262262
struct enet_desc_s *rxdesc; /* A pointer to the list of RX descriptors */
263+
spinlock_t lock; /* Spinlock */
263264

264265
/* This holds the information visible to the NuttX network */
265266

@@ -541,7 +542,7 @@ static int s32k1xx_transmit(struct s32k1xx_driver_s *priv)
541542

542543
/* Make the following operations atomic */
543544

544-
flags = spin_lock_irqsave(NULL);
545+
flags = spin_lock_irqsave(&priv->lock);
545546

546547
/* Enable TX interrupts */
547548

@@ -558,7 +559,7 @@ static int s32k1xx_transmit(struct s32k1xx_driver_s *priv)
558559

559560
putreg32(ENET_TDAR, S32K1XX_ENET_TDAR);
560561

561-
spin_unlock_irqrestore(NULL, flags);
562+
spin_unlock_irqrestore(&priv->lock, flags);
562563
return OK;
563564
}
564565

@@ -2443,6 +2444,8 @@ int s32k1xx_netinitialize(int intf)
24432444
#endif
24442445
priv->dev.d_private = g_enet; /* Used to recover private state from dev */
24452446

2447+
spin_lock_init(&priv->lock);
2448+
24462449
#ifdef CONFIG_NET_ETHERNET
24472450
/* Determine a semi-unique MAC address from MCU UID
24482451
* We use UID Low and Mid Low registers to get 64 bits, from which we keep

arch/arm/src/sam34/sam_lowputc.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@
249249
* Private Data
250250
****************************************************************************/
251251

252+
#ifdef HAVE_CONSOLE
253+
static spinlock_t g_sam_lowputc_lock = SP_UNLOCKED;
254+
#endif
255+
252256
/****************************************************************************
253257
* Private Functions
254258
****************************************************************************/
@@ -270,30 +274,19 @@ void arm_lowputc(char ch)
270274
#ifdef HAVE_CONSOLE
271275
irqstate_t flags;
272276

273-
for (; ; )
274-
{
277+
flags = spin_lock_irqsave(&g_sam_lowputc_lock);
278+
275279
/* Wait for the transmitter to be available */
276280

277-
while ((getreg32(SAM_CONSOLE_BASE + SAM_UART_SR_OFFSET) &
278-
UART_INT_TXEMPTY) == 0);
281+
while ((getreg32(SAM_CONSOLE_BASE + SAM_UART_SR_OFFSET) &
282+
UART_INT_TXEMPTY) == 0);
279283

280-
/* Disable interrupts so that the test and the transmission are
281-
* atomic.
282-
*/
284+
/* Send the character */
283285

284-
flags = spin_lock_irqsave(NULL);
285-
if ((getreg32(SAM_CONSOLE_BASE + SAM_UART_SR_OFFSET) &
286-
UART_INT_TXEMPTY) != 0)
287-
{
288-
/* Send the character */
286+
putreg32((uint32_t)ch, SAM_CONSOLE_BASE + SAM_UART_THR_OFFSET);
289287

290-
putreg32((uint32_t)ch, SAM_CONSOLE_BASE + SAM_UART_THR_OFFSET);
291-
spin_unlock_irqrestore(NULL, flags);
292-
return;
293-
}
288+
spin_unlock_irqrestore(&g_sam_lowputc_lock, flags);
294289

295-
spin_unlock_irqrestore(NULL, flags);
296-
}
297290
#endif
298291
}
299292

arch/arm/src/sama5/sam_lowputc.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@
211211
UART_MR_CHMODE_NORMAL)
212212
#endif
213213

214+
/****************************************************************************
215+
* Private Data
216+
****************************************************************************/
217+
218+
#if defined(SAMA5_HAVE_UART_CONSOLE) || defined(SAMA5_HAVE_USART_CONSOLE)
219+
static spinlock_t g_sam_lowputc_lock = SP_UNLOCKED;
220+
#endif
221+
214222
/****************************************************************************
215223
* Public Functions
216224
****************************************************************************/
@@ -228,30 +236,16 @@ void arm_lowputc(char ch)
228236
#if defined(SAMA5_HAVE_UART_CONSOLE) || defined(SAMA5_HAVE_USART_CONSOLE)
229237
irqstate_t flags;
230238

231-
for (; ; )
232-
{
233-
/* Wait for the transmitter to be available */
239+
/* Wait for the transmitter to be available */
234240

235-
while ((getreg32(SAM_CONSOLE_VBASE + SAM_UART_SR_OFFSET) &
236-
UART_INT_TXEMPTY) == 0);
241+
flags = spin_lock_irqsave(&g_sam_lowputc_lock);
242+
while ((getreg32(SAM_CONSOLE_VBASE + SAM_UART_SR_OFFSET) &
243+
UART_INT_TXEMPTY) == 0);
237244

238-
/* Disable interrupts so that the test and the transmission are
239-
* atomic.
240-
*/
241-
242-
flags = spin_lock_irqsave(NULL);
243-
if ((getreg32(SAM_CONSOLE_VBASE + SAM_UART_SR_OFFSET) &
244-
UART_INT_TXEMPTY) != 0)
245-
{
246-
/* Send the character */
245+
/* Send the character */
247246

248-
putreg32((uint32_t)ch, SAM_CONSOLE_VBASE + SAM_UART_THR_OFFSET);
249-
spin_unlock_irqrestore(NULL, flags);
250-
return;
251-
}
252-
253-
spin_unlock_irqrestore(NULL, flags);
254-
}
247+
putreg32((uint32_t)ch, SAM_CONSOLE_VBASE + SAM_UART_THR_OFFSET);
248+
spin_unlock_irqrestore(&g_sam_lowputc_lock, flags);
255249

256250
#elif defined(SAMA5_HAVE_FLEXCOM_CONSOLE)
257251
irqstate_t flags;

arch/arm/src/samd2l2/sam_serial.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ static bool sam_txempty(struct uart_dev_s *dev);
260260
* Private Data
261261
****************************************************************************/
262262

263+
#ifdef HAVE_SERIAL_CONSOLE
264+
static spinlock_t g_sam_serial_lock = SP_UNLOCKED;
265+
#endif
266+
263267
static const struct uart_ops_s g_uart_ops =
264268
{
265269
.setup = sam_setup,
@@ -1048,9 +1052,9 @@ void up_putc(int ch)
10481052
* interrupts from firing in the serial driver code.
10491053
*/
10501054

1051-
flags = spin_lock_irqsave(NULL);
1055+
flags = spin_lock_irqsave(&g_sam_serial_lock);
10521056
sam_lowputc(ch);
1053-
spin_unlock_irqrestore(NULL, flags);
1057+
spin_unlock_irqrestore(&g_sam_serial_lock, flags);
10541058
#endif
10551059
}
10561060

arch/arm/src/samd5e5/sam_serial.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ static bool sam_txempty(struct uart_dev_s *dev);
327327
* Private Data
328328
****************************************************************************/
329329

330+
#ifdef HAVE_SERIAL_CONSOLE
331+
static spinlock_t g_sam_serial_lock = SP_UNLOCKED;
332+
#endif
333+
330334
static const struct uart_ops_s g_uart_ops =
331335
{
332336
.setup = sam_setup,
@@ -1107,9 +1111,9 @@ void up_putc(int ch)
11071111
* interrupts from firing in the serial driver code.
11081112
*/
11091113

1110-
flags = spin_lock_irqsave(NULL);
1114+
flags = spin_lock_irqsave(&g_sam_serial_lock);
11111115
sam_lowputc(ch);
1112-
spin_unlock_irqrestore(NULL, flags);
1116+
spin_unlock_irqrestore(&g_sam_serial_lock, flags);
11131117
#endif
11141118
}
11151119

0 commit comments

Comments
 (0)