1
0
Эх сурвалжийг харах

[DEVFS] small C code optimization

Dmitry Filimonchuk 5 жил өмнө
parent
commit
c2fef81199

+ 19 - 17
src/usbd_stm32f103_devfs.c

@@ -347,21 +347,21 @@ void ep_deconfig(uint8_t ep) {
 }
 
 static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) {
+    uint16_t tmp;
     uint16_t *pma = PMA(rx->addr);
     uint16_t rxcnt = rx->cnt & 0x03FF;
     rx->cnt &= ~0x3FF;
-    if (blen > rxcnt) {
-        blen = rxcnt;
-    }
-    rxcnt = blen;
-    while (blen) {
-        uint16_t _t = *pma;
-        *buf++ = _t & 0xFF;
-        if (--blen) {
-            *buf++ = _t >> 8;
+    for(int idx = 0; idx < rxcnt; idx++) {
+        if ((idx & 0x01) == 0) {
+            tmp = *pma;
             pma += PMA_STEP;
-            blen--;
-        } else break;
+        }
+        if (idx < blen) {
+            buf[idx] = tmp & 0xFF;
+            tmp >>= 8;
+        } else {
+            return blen;
+        }
     }
     return rxcnt;
 }
@@ -411,14 +411,16 @@ int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
 
 static void pma_write(const uint8_t *buf, uint16_t blen, pma_rec *tx) {
     uint16_t *pma = PMA(tx->addr);
+    uint16_t tmp = 0;
     tx->cnt = blen;
-    while (blen > 1) {
-        *pma = buf[1] << 8 | buf[0];
-        pma += PMA_STEP;
-        buf += 2;
-        blen -= 2;
+    for (int idx=0; idx < blen; idx++) {
+        tmp |= buf[idx] << ((idx & 0x01) ? 8 : 0);
+        if ((idx & 0x01) || (idx + 1) == blen) {
+            *pma = tmp;
+            pma += PMA_STEP;
+            tmp = 0;
+        }
     }
-    if (blen) *pma = *buf;
 }
 
 int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {

+ 18 - 18
src/usbd_stm32l052_devfs.c

@@ -273,22 +273,20 @@ void ep_deconfig(uint8_t ep) {
 }
 
 static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) {
+    uint16_t tmp;
     uint16_t *pma = (void*)(USB_PMAADDR + rx->addr);
     uint16_t rxcnt = rx->cnt & 0x03FF;
     rx->cnt &= ~0x3FF;
-
-    if (blen > rxcnt) {
-        blen = rxcnt;
-    }
-    rxcnt = blen;
-    while (blen) {
-        uint16_t _t = *pma;
-        *buf++ = _t & 0xFF;
-        if (--blen) {
-            *buf++ = _t >> 8;
-            pma++;
-            blen--;
-        } else break;
+    for(int idx = 0; idx < rxcnt; idx++) {
+        if ((idx & 0x01) == 0) {
+            tmp = *pma++;
+        }
+        if (idx < blen) {
+            buf[idx] = tmp & 0xFF;
+            tmp >>= 8;
+        } else {
+            return blen;
+        }
     }
     return rxcnt;
 }
@@ -338,13 +336,15 @@ int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
 
 static void pma_write(uint8_t *buf, uint16_t blen, pma_rec *tx) {
     uint16_t *pma = (void*)(USB_PMAADDR + tx->addr);
+    uint16_t tmp = 0;
     tx->cnt = blen;
-    while (blen > 1) {
-        *pma++ = buf[1] << 8 | buf[0];
-        buf += 2;
-        blen -= 2;
+    for (int idx=0; idx < blen; idx++) {
+        tmp |= buf[idx] << ((idx & 0x01) ? 8 : 0);
+        if ((idx & 0x01) || (idx + 1) == blen) {
+            *pma++ = tmp;
+            tmp = 0;
+        }
     }
-    if (blen) *pma = *buf;
 }
 
 int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {

+ 18 - 18
src/usbd_stm32l100_devfs.c

@@ -257,21 +257,20 @@ void ep_deconfig(uint8_t ep) {
 }
 
 static uint16_t pma_read (uint8_t *buf, uint16_t blen, pma_rec *rx) {
+    uint16_t tmp;
     uint16_t *pma = (void*)(USB_PMAADDR + 2 * rx->addr);
     uint16_t rxcnt = rx->cnt & 0x03FF;
     rx->cnt &= ~0x3FF;
-    if (blen > rxcnt) {
-        blen = rxcnt;
-    }
-    rxcnt = blen;
-    while (blen) {
-        uint16_t _t = *pma;
-        *buf++ = _t & 0xFF;
-        if (--blen) {
-            *buf++ = _t >> 8;
-            pma += 2;
-            blen--;
-        } else break;
+    for(int idx = 0; idx < rxcnt; idx++) {
+        if ((idx & 0x01) == 0) {
+            tmp = *pma++;
+        }
+        if (idx < blen) {
+            buf[idx] = tmp & 0xFF;
+            tmp >>= 8;
+        } else {
+            return blen;
+        }
     }
     return rxcnt;
 }
@@ -321,14 +320,15 @@ int32_t ep_read(uint8_t ep, void *buf, uint16_t blen) {
 
 static void pma_write(const uint8_t *buf, uint16_t blen, pma_rec *tx) {
     uint16_t *pma = (void*)(USB_PMAADDR + 2 * (tx->addr));
+    uint16_t tmp = 0;
     tx->cnt = blen;
-    while (blen > 1) {
-        *pma = buf[1] << 8 | buf[0];
-        pma += 2;
-        buf += 2;
-        blen -= 2;
+    for (int idx=0; idx < blen; idx++) {
+        tmp |= buf[idx] << ((idx & 0x01) ? 8 : 0);
+        if ((idx & 0x01) || (idx + 1) == blen) {
+            *pma++ = tmp;
+            tmp = 0;
+        }
     }
-    if (blen) *pma = *buf;
 }
 
 int32_t ep_write(uint8_t ep, void *buf, uint16_t blen) {