Skip to content

Commit 68ed825

Browse files
committed
stm32_common: simple way of f42x detection
1 parent 81f30e7 commit 68ed825

2 files changed

Lines changed: 4 additions & 50 deletions

File tree

firmware/hw_layer/ports/mpu_util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void causeHardFault();
1818
// If mcu can erase/write part of its internal memory without stalling CPU
1919
bool mcuCanFlashWhileRunning();
2020

21-
bool ramReadProbe(volatile const char *read_address);
2221
#if defined(STM32F4)
2322
bool isStm32F42x();
2423
#endif // STM32F4

firmware/hw_layer/ports/stm32/stm32_common.cpp

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -211,60 +211,15 @@ int getRemainingStack(thread_t *otp) {
211211
#endif /* CH_DBG_ENABLE_STACK_CHECK */
212212
}
213213

214-
#if defined(STM32F4) || defined(STM32F7) || defined(STM32H7)
215-
216-
#define HWREG(x) \
217-
(*((volatile unsigned long *)(x)))
218-
219-
#define NVIC_FAULT_STAT 0xE000ED28 // Configurable Fault Status
220-
#define NVIC_FAULT_STAT_BFARV 0x00008000 // Bus Fault Address Register Valid
221-
#define NVIC_CFG_CTRL_BFHFNMIGN 0x00000100 // Ignore Bus Fault in NMI and
222-
// Fault
223-
#define NVIC_CFG_CTRL 0xE000ED14 // Configuration and Control
224-
225-
226-
/**
227-
* @brief Probe an address to see if can be read without generating a bus fault
228-
* @details This function must be called with the processor in privileged mode.
229-
* It:
230-
* - Clear any previous indication of a bus fault in the BFARV bit
231-
* - Temporarily sets the processor to Ignore Bus Faults with all interrupts and fault handlers disabled
232-
* - Attempt to read from read_address, ignoring the result
233-
* - Checks to see if the read caused a bus fault, by checking the BFARV bit is set
234-
* - Re-enables Bus Faults and all interrupts and fault handlers
235-
* @param[in] read_address The address to try reading a byte from
236-
* @return Returns true if no bus fault occurred reading from read_address, or false if a bus fault occurred.
237-
*/
238-
bool ramReadProbe(volatile const char *read_address) {
239-
bool address_readable = true;
240-
241-
/* Clear any existing indication of a bus fault - BFARV is write one to clear */
242-
HWREG (NVIC_FAULT_STAT) |= NVIC_FAULT_STAT_BFARV;
243-
244-
HWREG (NVIC_CFG_CTRL) |= NVIC_CFG_CTRL_BFHFNMIGN;
245-
asm volatile (" CPSID f;");
246-
*read_address;
247-
if ((HWREG (NVIC_FAULT_STAT) & NVIC_FAULT_STAT_BFARV) != 0)
248-
{
249-
address_readable = false;
250-
}
251-
asm volatile (" CPSIE f;");
252-
HWREG (NVIC_CFG_CTRL) &= ~NVIC_CFG_CTRL_BFHFNMIGN;
253-
254-
return address_readable;
255-
}
256-
257-
#endif
258-
259214
#if defined(STM32F4)
260215
bool isStm32F42x() {
261-
// really it's enough to just check 0x20020010
262-
return ramReadProbe((const char *)0x20000010) && ramReadProbe((const char *)0x20020010) && !ramReadProbe((const char *)0x20070010);
216+
// Device identifier
217+
// 0x419 for STM32F42xxx and STM32F43xxx
218+
// 0x413 for STM32F405xx/07xx and STM32F415xx/17xx
219+
return ((DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID_Msk) == 0x419);
263220
}
264-
265221
#endif
266222

267-
268223
// Stubs for per-board low power helpers
269224
PUBLIC_API_WEAK void boardPrepareForStop() {
270225
// Default implementation - wake up on PA0 - boards should override this

0 commit comments

Comments
 (0)