Skip to content

Commit 56ad53a

Browse files
Fix some bugs, Tx DMA working but no packets are getting transmitted
1 parent 34999a3 commit 56ad53a

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

connectivity/drivers/emac/TARGET_STM/STM32EthMACv1.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void STM32EthMACv1::TxDMA::startDMA() {
4040
txDescs[TX_NUM_DESCS - 1].endOfRing = true;
4141

4242
// Set descriptor list address register
43-
base->DMARDLAR = reinterpret_cast<ptrdiff_t>(&txDescs[0]);
43+
base->DMATDLAR = reinterpret_cast<ptrdiff_t>(&txDescs[0]);
4444

4545
// Start Tx DMA
4646
base->DMAOMR |= ETH_DMAOMR_ST_Msk;
@@ -254,7 +254,7 @@ CompositeEMAC::ErrCode STM32EthMACv1::MACDriver::init() {
254254
ETH_SetMDIOClockRange(base);
255255

256256
// Configure MAC settings
257-
base->MACCR |= ETH_MACCR_APCS_Msk; // Strip padding and CRC from frames
257+
base->MACCR |= (1 << 25); // Strip CRC from frames. CSTF bit definition missing from CMSIS header for some reason?
258258
base->MACFFR = ETH_MACFFR_HPF_Msk | ETH_MACFFR_HM_Msk; // Use perfect and hash filters for multicast
259259

260260
// Configure DMA settings. Default STM32CubeHAL settings used.
@@ -278,6 +278,8 @@ CompositeEMAC::ErrCode STM32EthMACv1::MACDriver::init() {
278278
// trigger if we run out of Rx descriptors, and we don't want to fatal error
279279
// in that case.
280280
base->DMAIER = ETH_DMAIER_NISE | ETH_DMAIER_RIE | ETH_DMAIER_TIE | ETH_DMAIER_FBEIE | ETH_DMAIER_AISE;
281+
282+
return CompositeEMAC::ErrCode::SUCCESS;
281283
}
282284

283285
CompositeEMAC::ErrCode STM32EthMACv1::MACDriver::deinit() {
@@ -361,7 +363,7 @@ CompositeEMAC::ErrCode STM32EthMACv1::MACDriver::mdioRead(uint8_t devAddr, uint8
361363
// Get result
362364
result = base->MACMIIDR;
363365

364-
tr_info("MDIO read devAddr %" PRIu8 ", regAddr 0x%" PRIx8 " -> 0x%" PRIx16, devAddr, regAddr, result);
366+
tr_debug("MDIO read devAddr %" PRIu8 ", regAddr 0x%" PRIx8 " -> 0x%" PRIx16, devAddr, regAddr, result);
365367

366368
return ErrCode::SUCCESS;
367369
}
@@ -459,6 +461,8 @@ void STM32EthMACv1::MACDriver::setPromiscuous(bool enable) {
459461
}
460462
}
461463

464+
STM32EthMACv1 * STM32EthMACv1::instance = nullptr;
465+
462466
STM32EthMACv1::STM32EthMACv1():
463467
CompositeEMAC(txDMA, rxDMA, macDriver),
464468
base(ETH),
@@ -495,15 +499,14 @@ void STM32EthMACv1::irqHandler() {
495499
if(dma_flag & ETH_DMASR_FBES_Msk)
496500
{
497501
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER_ETHERNET, EIO), \
498-
"STM32 EMAC v2: Hardware reports fatal DMA error\n");
502+
"STM32 EMAC v1: Hardware reports fatal DMA error\n");
499503
}
500504
}
505+
}
501506

502507
// Provide default EMAC driver
503508
MBED_WEAK EMAC &EMAC::get_default_instance()
504509
{
505510
static mbed::STM32EthMACv1 emac;
506511
return emac;
507-
}
508-
509-
}
512+
}

connectivity/drivers/emac/include/GenericEthDMA.h

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ namespace mbed {
147147
txReclaimIndex = (txReclaimIndex + 1) % MBED_CONF_NSAPI_EMAC_TX_NUM_DESCS;
148148
++txDescsOwnedByApplication;
149149

150-
tr_debug("Reclaimed descriptor %zu", txReclaimIndex);
150+
tr_info("Reclaimed descriptor %zu", txReclaimIndex);
151151

152152
returnedAnyDescriptors = true;
153153
}
@@ -234,31 +234,38 @@ namespace mbed {
234234
SCB_CleanDCache_by_Addr(memory_manager->get_ptr(currBuf), memory_manager->get_len(currBuf));
235235
#endif
236236

237-
// Move to next buffer
238-
currBuf = memory_manager->get_next(currBuf);
239-
if(currBuf == nullptr)
237+
// Get next buffer
238+
const auto nextBuf = memory_manager->get_next(currBuf);
239+
if(nextBuf == nullptr)
240240
{
241-
// Last descriptor, store buffer address for freeing
241+
// Last descriptor, store head buffer address for freeing
242242
descStackBuffers[txSendIndex] = buf;
243243
}
244244
else
245245
{
246246
descStackBuffers[txSendIndex] = nullptr;
247247
}
248248

249+
// Get the pointer and length of the packet because this might not be doable in a critical section
250+
const auto bufferPtr = static_cast<uint8_t *>(memory_manager->get_ptr(currBuf));
251+
const auto bufferLen = memory_manager->get_len(currBuf);
252+
249253
// Enter a critical section, because we could run into weird corner cases if the
250254
// interrupt executes while we are half done configuring this descriptor and updating
251255
// the counters.
252256
core_util_critical_section_enter();
253257

254258
// Configure settings.
255-
giveToDMA(txSendIndex, static_cast<uint8_t *>(memory_manager->get_ptr(currBuf)), memory_manager->get_len(currBuf), descCount == 0, currBuf == nullptr);
259+
giveToDMA(txSendIndex, bufferPtr, bufferLen, descCount == 0, nextBuf == nullptr);
256260

257261
// Update descriptor count and index
258262
--txDescsOwnedByApplication;
259263
txSendIndex = (txSendIndex + 1) % MBED_CONF_NSAPI_EMAC_TX_NUM_DESCS;
260264

261265
core_util_critical_section_exit();
266+
267+
// Move to next buffer
268+
currBuf = nextBuf;
262269
}
263270

264271
return CompositeEMAC::ErrCode::SUCCESS;
@@ -558,9 +565,8 @@ namespace mbed {
558565
}
559566
#endif
560567

561-
tr_debug("Returning packet of length %lu, start %p from Rx descriptors %zu-%zu (%p-%p)\n",
562-
memory_manager->get_total_len(headBuffer), memory_manager->get_ptr(headBuffer), *firstDescIdx, *lastDescIdx,
563-
&rxDescs[*firstDescIdx], &rxDescs[*lastDescIdx]);
568+
tr_info("Returning packet of length %lu, start %p from Rx descriptors %zu-%zu\n",
569+
memory_manager->get_total_len(headBuffer), memory_manager->get_ptr(headBuffer), *firstDescIdx, *lastDescIdx);
564570

565571
return headBuffer;
566572
}

connectivity/drivers/emac/sources/CompositeEMAC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ namespace mbed {
221221
return false;
222222
}
223223

224-
// Init DMA rungs
224+
// Init DMA rings
225225
if(txDMA.init() != ErrCode::SUCCESS || rxDMA.init() != ErrCode::SUCCESS) {
226226
tr_err("power_up(): Failed to init DMA!");
227227
return false;

0 commit comments

Comments
 (0)