|
| 1 | +/* |
| 2 | + * This file is part of Cleanflight and Betaflight. |
| 3 | + * |
| 4 | + * Cleanflight and Betaflight are free software. You can redistribute |
| 5 | + * this software and/or modify this software under the terms of the |
| 6 | + * GNU General Public License as published by the Free Software |
| 7 | + * Foundation, either version 3 of the License, or (at your option) |
| 8 | + * any later version. |
| 9 | + * |
| 10 | + * Cleanflight and Betaflight are distributed in the hope that they |
| 11 | + * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | + * See the GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this software. |
| 17 | + * |
| 18 | + * If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +extern "C" { |
| 22 | + |
| 23 | +#include "platform.h" |
| 24 | +#include "drivers/dma.h" |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +#include "unittest_macros.h" |
| 29 | +#include "gtest/gtest.h" |
| 30 | + |
| 31 | +// Mirrors sdcardDmaClaim() (drivers/sdcard.c) and sdioDmaClaim() (drivers/sdio_f4xx.c, |
| 32 | +// drivers/sdio_f7xx.c -- byte-identical body, confirmed by direct read) rather than compiling |
| 33 | +// those files. Real-compile was investigated, not assumed disproportionate: sdcard.c's |
| 34 | +// SDCARD_STATE_SENDING_WRITE completion path (STM32F4 branch) needs a real DMA_Stream_TypeDef |
| 35 | +// with ->ref/->completeFlag fields, a two-arg DMA_GetFlagStatus()/DMA_ClearFlag() pair distinct |
| 36 | +// from the one-arg mock already in test/unit/platform.h, DMA_Cmd(), SPI_I2S_GetFlagStatus(), |
| 37 | +// LL_SPI_DisableDMAReq_TX(), and direct ->DR register access -- none of which the file's own |
| 38 | +// DMA-claim logic uses, but all of which must still link since the whole .c compiles as one |
| 39 | +// translation unit. sdio_f4xx.c/f7xx.c need the same class of surface an order of magnitude |
| 40 | +// larger (118 direct SDIO->/RCC-> field accesses, real GPIO AF config, NVIC). Both are the |
| 41 | +// same disproportionate-mocking case bus_spi_ll.c/bus_spi_stdperiph.c already were in |
| 42 | +// feat/dma-ll-unittest-infra (20+ mocks needed for functions that weren't even the test |
| 43 | +// target). Follows the same mirror-with-fake-DMA-state convention already established for |
| 44 | +// uartDmaClaim() in serial_uart_dma_claim_unittest.cc. |
| 45 | +// |
| 46 | +// sdcard_init() (SPI mode) can be called from both fc_init.c (boot) and |
| 47 | +// usbd_storage_sd_spi.c (USB MSC passthrough). SD_Initialize_LL() (SDIO mode) can be called |
| 48 | +// from both sdcard_sdio_baremetal.c (boot) and usbd_storage_sdio.c (USB MSC passthrough). In |
| 49 | +// both cases the second call is a legitimate reopen of the same OWNER_SDCARD claim, not a |
| 50 | +// conflict, and must not be misidentified as one now that the caller-side return check exists. |
| 51 | +namespace { |
| 52 | + |
| 53 | +resourceOwner_e fakeOwner = OWNER_FREE; |
| 54 | +uint8_t fakeResourceIndex = 0; |
| 55 | + |
| 56 | +void resetFakeDmaState() { |
| 57 | + fakeOwner = OWNER_FREE; |
| 58 | + fakeResourceIndex = 0; |
| 59 | +} |
| 60 | + |
| 61 | +resourceOwner_e fakeDmaGetOwner(dmaIdentifier_e) { |
| 62 | + return fakeOwner; |
| 63 | +} |
| 64 | + |
| 65 | +uint8_t fakeDmaGetResourceIndex(dmaIdentifier_e) { |
| 66 | + return fakeResourceIndex; |
| 67 | +} |
| 68 | + |
| 69 | +bool fakeDmaAllocate(dmaIdentifier_e, resourceOwner_e owner, uint8_t resourceIndex) { |
| 70 | + if (fakeOwner != OWNER_FREE) { |
| 71 | + return false; |
| 72 | + } |
| 73 | + fakeOwner = owner; |
| 74 | + fakeResourceIndex = resourceIndex; |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +bool sdcardDmaClaim(dmaIdentifier_e identifier, resourceOwner_e owner, uint8_t resourceIndex) { |
| 79 | + if (fakeDmaGetOwner(identifier) == owner && fakeDmaGetResourceIndex(identifier) == resourceIndex) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + return fakeDmaAllocate(identifier, owner, resourceIndex); |
| 83 | +} |
| 84 | + |
| 85 | +bool sdioDmaClaim(dmaIdentifier_e identifier, resourceOwner_e owner, uint8_t resourceIndex) { |
| 86 | + if (fakeDmaGetOwner(identifier) == owner && fakeDmaGetResourceIndex(identifier) == resourceIndex) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + return fakeDmaAllocate(identifier, owner, resourceIndex); |
| 90 | +} |
| 91 | + |
| 92 | +const dmaIdentifier_e kStream = DMA1_ST3_HANDLER; |
| 93 | + |
| 94 | +} // namespace |
| 95 | + |
| 96 | +// sdcardDmaClaim() -- SPI-mode (drivers/sdcard.c), real callers always pass OWNER_SDCARD / index 0. |
| 97 | + |
| 98 | +TEST(SdcardDmaClaimUnittest, FirstClaimOnFreeStreamSucceeds) { |
| 99 | + resetFakeDmaState(); |
| 100 | + EXPECT_TRUE(sdcardDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 101 | + EXPECT_EQ(fakeOwner, OWNER_SDCARD); |
| 102 | +} |
| 103 | + |
| 104 | +TEST(SdcardDmaClaimUnittest, UsbMscReopenBySameOwnerAndIndexSucceeds) { |
| 105 | + resetFakeDmaState(); |
| 106 | + fakeOwner = OWNER_SDCARD; |
| 107 | + fakeResourceIndex = 0; |
| 108 | + // sdcard_init() called again from usbd_storage_sd_spi.c after fc_init.c already claimed it. |
| 109 | + EXPECT_TRUE(sdcardDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 110 | +} |
| 111 | + |
| 112 | +TEST(SdcardDmaClaimUnittest, ConflictWithForeignOwnerFails) { |
| 113 | + resetFakeDmaState(); |
| 114 | + fakeOwner = OWNER_SPI_SDI; |
| 115 | + fakeResourceIndex = 0; |
| 116 | + EXPECT_FALSE(sdcardDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 117 | + // failed claim must not disturb the existing owner's bookkeeping. |
| 118 | + EXPECT_EQ(fakeOwner, OWNER_SPI_SDI); |
| 119 | +} |
| 120 | + |
| 121 | +TEST(SdcardDmaClaimUnittest, SameOwnerDifferentResourceIndexFails) { |
| 122 | + resetFakeDmaState(); |
| 123 | + fakeOwner = OWNER_SDCARD; |
| 124 | + fakeResourceIndex = 1; |
| 125 | + // spec-level guard: same owner enum alone is not sufficient, index must match too. |
| 126 | + EXPECT_FALSE(sdcardDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 127 | +} |
| 128 | + |
| 129 | +// sdioDmaClaim() -- SDIO-mode (drivers/sdio_f4xx.c, drivers/sdio_f7xx.c), same real callers |
| 130 | +// always pass OWNER_SDCARD / index 0. |
| 131 | + |
| 132 | +TEST(SdioDmaClaimUnittest, FirstClaimOnFreeStreamSucceeds) { |
| 133 | + resetFakeDmaState(); |
| 134 | + EXPECT_TRUE(sdioDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 135 | + EXPECT_EQ(fakeOwner, OWNER_SDCARD); |
| 136 | +} |
| 137 | + |
| 138 | +TEST(SdioDmaClaimUnittest, UsbMscReopenBySameOwnerAndIndexSucceeds) { |
| 139 | + resetFakeDmaState(); |
| 140 | + fakeOwner = OWNER_SDCARD; |
| 141 | + fakeResourceIndex = 0; |
| 142 | + // SD_Initialize_LL() called again from usbd_storage_sdio.c after sdcard_sdio_baremetal.c |
| 143 | + // already claimed it during boot. |
| 144 | + EXPECT_TRUE(sdioDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 145 | +} |
| 146 | + |
| 147 | +TEST(SdioDmaClaimUnittest, ConflictWithForeignOwnerFails) { |
| 148 | + resetFakeDmaState(); |
| 149 | + fakeOwner = OWNER_SPI_SDI; |
| 150 | + fakeResourceIndex = 0; |
| 151 | + EXPECT_FALSE(sdioDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 152 | + EXPECT_EQ(fakeOwner, OWNER_SPI_SDI); |
| 153 | +} |
| 154 | + |
| 155 | +TEST(SdioDmaClaimUnittest, SameOwnerDifferentResourceIndexFails) { |
| 156 | + resetFakeDmaState(); |
| 157 | + fakeOwner = OWNER_SDCARD; |
| 158 | + fakeResourceIndex = 1; |
| 159 | + EXPECT_FALSE(sdioDmaClaim(kStream, OWNER_SDCARD, 0)); |
| 160 | +} |
0 commit comments