Rename singleton to initialized_singleton

Use #pragma once instead of guard definitions in every header
This commit is contained in:
Attila Body 2025-06-10 21:57:31 +02:00
parent a4df3252f2
commit a4c1baa410
17 changed files with 151 additions and 175 deletions

View file

@ -14,6 +14,10 @@ FixNamespaceComments: false
PackConstructorInitializers: Never PackConstructorInitializers: Never
AlignAfterOpenBracket: AlwaysBreak AlignAfterOpenBracket: AlwaysBreak
InsertBraces: true InsertBraces: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterFunctionDefinitionName: false
BraceWrapping: BraceWrapping:
AfterClass: true # false AfterClass: true # false
AfterControlStatement: false AfterControlStatement: false

View file

@ -7,15 +7,15 @@
#pragma once #pragma once
#include <f4ll/initialized_singleton.h>
#include <f4ll/packet_usart.h> #include <f4ll/packet_usart.h>
#include <f4ll/ringbuffer.h> #include <f4ll/ringbuffer.h>
#include <f4ll/singleton.h>
namespace f4ll { namespace f4ll {
class console_handler : public usart_core, public singleton<console_handler> class console_handler : public usart_core, public initialized_singleton<console_handler>
{ {
friend class singleton<console_handler>; friend class initialized_singleton<console_handler>;
public: public:
void print(char const *s); void print(char const *s);

View file

@ -7,15 +7,15 @@
#pragma once #pragma once
#include <f4ll/dma_helper.h> #include <f4ll/dma_helper.h>
#include <f4ll/singleton.h> #include <f4ll/initialized_singleton.h>
#include <inttypes.h> #include <inttypes.h>
#include <platform/dma_ll.h> #include <platform/dma_ll.h>
namespace f4ll { namespace f4ll {
class crc_handler : public singleton<crc_handler> class crc_handler : public initialized_singleton<crc_handler>
{ {
friend class singleton<crc_handler>; friend class initialized_singleton<crc_handler>;
public: public:
struct icallback struct icallback

View file

@ -5,8 +5,7 @@
* Author: abody * Author: abody
*/ */
#ifndef LL_DMAHELPER_H_ #pragma once
#define LL_DMAHELPER_H_
#include <inttypes.h> #include <inttypes.h>
#include <platform/dma_ll.h> #include <platform/dma_ll.h>
@ -56,5 +55,3 @@ private:
}; };
} /* namespace f4ll */ } /* namespace f4ll */
#endif /* LL_DMAHELPER_H_ */

View file

@ -1,5 +1,4 @@
#ifndef __FAULT_H #pragma once
#define __FAULT_H
#define FAULT_REASON_HARD_FAULT 1 #define FAULT_REASON_HARD_FAULT 1
#define FAULT_REASON_MEMMANAGE_FAULT 2 #define FAULT_REASON_MEMMANAGE_FAULT 2
@ -43,5 +42,3 @@ __attribute__((noreturn)) void fault_handler(uint32_t type, fault_context_t *con
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* __FAULT_H */

View file

@ -0,0 +1,27 @@
#pragma once
#include <utility>
namespace f4ll {
template <typename T> class initialized_singleton
{
public:
static T &instance() { return *m_instance; }
template <typename... args_t> static T &init(args_t &&...args)
{
static T instance{std::forward<args_t>(args)...};
m_instance = &instance;
return instance;
}
protected:
initialized_singleton() = default;
initialized_singleton(const initialized_singleton &) = delete;
initialized_singleton &operator=(const initialized_singleton &) = delete;
static T *m_instance;
};
template <typename T> T *initialized_singleton<T>::m_instance = nullptr;
} // namespace f1ll {

View file

@ -1,5 +1,4 @@
#ifndef _IRQLOCK_H_INCLUDED #pragma once
#define _IRQLOCK_H_INCLUDED
#include <inttypes.h> #include <inttypes.h>
#include <stm32f4xx.h> #include <stm32f4xx.h>
@ -22,5 +21,3 @@ private:
uint32_t m_primask; uint32_t m_primask;
}; };
} }
#endif // _IRQLOCK_H_INCLUDED

View file

@ -7,13 +7,13 @@
#pragma once #pragma once
#include <f4ll/dma_helper.h> #include <f4ll/dma_helper.h>
#include <f4ll/singleton.h> #include <f4ll/initialized_singleton.h>
namespace f4ll { namespace f4ll {
class memcpy_dma : public singleton<memcpy_dma>, private dma_helper class memcpy_dma : public initialized_singleton<memcpy_dma>, private dma_helper
{ {
friend class singleton<memcpy_dma>; friend class initialized_singleton<memcpy_dma>;
public: public:
void *copy(void *dst, void const *src, uint16_t length); void *copy(void *dst, void const *src, uint16_t length);
@ -24,4 +24,4 @@ private:
bool volatile m_busy = false; bool volatile m_busy = false;
}; };
} /* namespace f4ll */ } // namespace f4ll

View file

@ -5,11 +5,12 @@
* Author: abody * Author: abody
*/ */
#ifndef LL_HSUSART_H_ #pragma once
#define LL_HSUSART_H_
#include <platform/usart_ll.h>
#include <f4ll/crc_handler.h> #include <f4ll/crc_handler.h>
#include <f4ll/usart_core.h> #include <f4ll/usart_core.h>
#include <platform/usart_ll.h>
namespace f4ll { namespace f4ll {
@ -119,4 +120,3 @@ private:
}; };
} }
#endif /* LL_HSUSART_H_ */

View file

@ -1,33 +0,0 @@
#ifndef SINGLETON_H_
#define SINGLETON_H_
#include <utility>
namespace f4ll {
template <typename T> class singleton
{
public:
static T &instance()
{
return *m_instance;
}
template <typename... args_t> static T &init(args_t &&...args)
{
static T instance{std::forward<args_t>(args)...};
m_instance = &instance;
return instance;
}
protected:
singleton() = default;
singleton(const singleton &) = delete;
singleton &operator=(const singleton &) = delete;
static T *m_instance;
};
template <typename T> T *singleton<T>::m_instance = nullptr;
} // namespace f1ll {
#endif /* SINGLETON_H_ */

View file

@ -5,8 +5,7 @@
* Author: compi * Author: compi
*/ */
#ifndef _STM32PLUS_STRUTIL_H_ #pragma once
#define _STM32PLUS_STRUTIL_H_
#include <stddef.h> #include <stddef.h>
#include <inttypes.h> #include <inttypes.h>
@ -27,5 +26,3 @@ char tochr(const uint8_t in, const uint8_t upper);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif /* _STM32PLUS_STRUTIL_H_ */

View file

@ -5,8 +5,8 @@
* Author: abody * Author: abody
*/ */
#ifndef LL_USARTCORE_H_ #pragma once
#define LL_USARTCORE_H_
#include <platform/usart_ll.h> #include <platform/usart_ll.h>
#include <f4ll/dma_helper.h> #include <f4ll/dma_helper.h>
@ -44,11 +44,10 @@ private:
virtual void tx_dma_half_transfer(void) = 0; virtual void tx_dma_half_transfer(void) = 0;
virtual void tx_dma_error(dma_helper::dma_error_type reason) = 0; virtual void tx_dma_error(dma_helper::dma_error_type reason) = 0;
public:
void usart_isr(); void usart_isr();
void rx_dma_isr(); void rx_dma_isr();
void tx_dma_isr(); void tx_dma_isr();
}; };
} /* namespace f4ll */ } /* namespace f4ll */
#endif /* LL_USARTCORE_H_ */

View file

@ -71,19 +71,15 @@ size_t console_handler::append(char const *s)
void console_handler::flush() void console_handler::flush()
{ {
bool busy;
if (!m_tx_buffer.uncommited()) { if (!m_tx_buffer.uncommited()) {
return; return;
} }
m_tx_buffer.commit(); m_tx_buffer.commit();
{
irq_lock l; if (m_in_flight_size) {
busy = m_in_flight_size != 0;
}
if (busy) {
return; return;
} }
uint8_t const *chunk; uint8_t const *chunk;
m_tx_buffer.get_chunk(m_tx_buffer.size(), chunk, m_in_flight_size); m_tx_buffer.get_chunk(m_tx_buffer.size(), chunk, m_in_flight_size);
if (m_in_flight_size) { if (m_in_flight_size) {

View file

@ -4,6 +4,7 @@
* Created on: Oct 26, 2019 * Created on: Oct 26, 2019
* Author: compi * Author: compi
*/ */
#include <f4ll/crc_handler.h> #include <f4ll/crc_handler.h>
namespace f4ll { namespace f4ll {

View file

@ -6,7 +6,7 @@
* -c "tpiu config internal <logfile_full_path> uart off <cpufreq>" * -c "tpiu config internal <logfile_full_path> uart off <cpufreq>"
*/ */
#include <inttypes.h> #include <inttypes.h>
//#include <core_cm4.h> // #include <core_cm4.h>
#include <f4ll/fault.h> #include <f4ll/fault.h>
#include <f4ll/str_util.h> #include <f4ll/str_util.h>
#include <stm32f4xx.h> #include <stm32f4xx.h>
@ -24,39 +24,40 @@ void __attribute__((weak)) app_fault_callback(uint32_t reason)
void swo_send_str(char const *str, uint8_t len, uint8_t port) void swo_send_str(char const *str, uint8_t len, uint8_t port)
{ {
while(len) { while (len) {
if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && // ITM enabled if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && // ITM enabled
((ITM->TER & (1UL << port) ) != 0UL) ) // ITM Port enabled ((ITM->TER & (1UL << port)) != 0UL)) // ITM Port enabled
{ {
// Wait until shift register is free // Wait until shift register is free
while (ITM->PORT[port].u32 == 0UL) { while (ITM->PORT[port].u32 == 0UL) {
__ASM volatile ("nop"); __ASM volatile("nop");
} }
if(len >= 4) { if (len >= 4) {
ITM->PORT[port].u32 = *(uint32_t*)(str); ITM->PORT[port].u32 = *(uint32_t *)(str);
str += 4; str += 4;
len -= 4; len -= 4;
} else if(len >= 2) { } else if (len >= 2) {
ITM->PORT[port].u16 = *(uint16_t*)(str); ITM->PORT[port].u16 = *(uint16_t *)(str);
str += 2; str += 2;
len -= 2; len -= 2;
} else { } else {
ITM->PORT[port].u8 = *(uint8_t*)(str); ITM->PORT[port].u8 = *(uint8_t *)(str);
++str; ++str;
--len; --len;
} }
} else } else {
break; break;
} }
}
} }
void fault_print_str(char const *fmtstr, uint32_t *values) void fault_print_str(char const *fmtstr, uint32_t *values)
{ {
char hex_str[9]={0}; char hex_str[9] = {0};
char const *next_chunk = fmtstr; char const *next_chunk = fmtstr;
while(*fmtstr) { while (*fmtstr) {
if(*fmtstr == '%') { if (*fmtstr == '%') {
swo_send_str(next_chunk, fmtstr - next_chunk, 0); swo_send_str(next_chunk, fmtstr - next_chunk, 0);
uitohex(hex_str, *values++, 8); uitohex(hex_str, *values++, 8);
swo_send_str(hex_str, 8, 0); swo_send_str(hex_str, 8, 0);
@ -72,38 +73,30 @@ void fault_print_str(char const *fmtstr, uint32_t *values)
void fault_handler(uint32_t type, fault_context_t *context) void fault_handler(uint32_t type, fault_context_t *context)
{ {
uint32_t FSR[9] = { uint32_t FSR[9] = {
SCB->HFSR, SCB->HFSR, 0xff & SCB->CFSR, (0xff00 & SCB->CFSR) >> 8, (0xffff0000 & SCB->CFSR) >> 16, SCB->DFSR, SCB->AFSR, SCB->SHCSR,
0xff & SCB->CFSR, SCB->MMFAR, SCB->BFAR};
(0xff00 & SCB->CFSR) >> 8,
(0xffff0000 & SCB->CFSR) >> 16,
SCB->DFSR,
SCB->AFSR,
SCB->SHCSR,
SCB->MMFAR,
SCB->BFAR
};
while(1) { while (1) {
fault_print_str("\n++ Fault Handler ++\n\nFaultType: ",NULL); fault_print_str("\n++ Fault Handler ++\n\nFaultType: ", NULL);
switch( type ) { switch (type) {
case FAULT_REASON_HARD_FAULT: case FAULT_REASON_HARD_FAULT:
fault_print_str("HardFault",NULL); fault_print_str("HardFault", NULL);
break; break;
case FAULT_REASON_MEMMANAGE_FAULT: case FAULT_REASON_MEMMANAGE_FAULT:
fault_print_str("MemManageFault",NULL); fault_print_str("MemManageFault", NULL);
break; break;
case FAULT_REASON_BUS_FAULT: case FAULT_REASON_BUS_FAULT:
fault_print_str("BusFault",NULL); fault_print_str("BusFault", NULL);
break; break;
case FAULT_REASON_USAGE_FAULT: case FAULT_REASON_USAGE_FAULT:
fault_print_str("UsageFault",NULL); fault_print_str("UsageFault", NULL);
break; break;
default: default:
fault_print_str("Unknown Fault",NULL); fault_print_str("Unknown Fault", NULL);
break; break;
} }
fault_print_str("\n\nContext:",NULL); fault_print_str("\n\nContext:", NULL);
fault_print_str( fault_print_str(
"\nR0 : %" "\nR0 : %"
@ -127,8 +120,8 @@ void fault_handler(uint32_t type, fault_context_t *context)
"\nMSP : %", "\nMSP : %",
(uint32_t *)context); (uint32_t *)context);
//Capture CPUID to get core/cpu info // Capture CPUID to get core/cpu info
fault_print_str("\nCPUID: %",(uint32_t *)&SCB->CPUID); fault_print_str("\nCPUID: %", (uint32_t *)&SCB->CPUID);
fault_print_str( fault_print_str(
"\nHFSR : %" "\nHFSR : %"
@ -147,4 +140,3 @@ void fault_handler(uint32_t type, fault_context_t *context)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -5,7 +5,8 @@
* Author: abody * Author: abody
*/ */
#include <f4ll/packet_usart.h> #include <f4ll/packet_usart.h>
#include <string.h>
#include <cstring>
namespace f4ll { namespace f4ll {

View file

@ -1,5 +1,6 @@
#include <f4ll/str_util.h> #include <f4ll/str_util.h>
#include <stdint.h>
#include <cstdint>
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
size_t strcpy_ex(char *dst, char const *src) size_t strcpy_ex(char *dst, char const *src)