diff --git a/.clang-format b/.clang-format index 6d24e95..624e7ab 100644 --- a/.clang-format +++ b/.clang-format @@ -14,6 +14,10 @@ FixNamespaceComments: false PackConstructorInitializers: Never AlignAfterOpenBracket: AlwaysBreak InsertBraces: true +SpaceBeforeParens: Custom +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterFunctionDefinitionName: false BraceWrapping: AfterClass: true # false AfterControlStatement: false diff --git a/inc/f4ll/console_handler.h b/inc/f4ll/console_handler.h index 9972539..27a2a7e 100644 --- a/inc/f4ll/console_handler.h +++ b/inc/f4ll/console_handler.h @@ -7,15 +7,15 @@ #pragma once +#include #include #include -#include namespace f4ll { -class console_handler : public usart_core, public singleton +class console_handler : public usart_core, public initialized_singleton { - friend class singleton; + friend class initialized_singleton; public: void print(char const *s); diff --git a/inc/f4ll/crc_handler.h b/inc/f4ll/crc_handler.h index 650eebb..8fb6b27 100644 --- a/inc/f4ll/crc_handler.h +++ b/inc/f4ll/crc_handler.h @@ -7,15 +7,15 @@ #pragma once #include -#include +#include #include #include namespace f4ll { -class crc_handler : public singleton +class crc_handler : public initialized_singleton { - friend class singleton; + friend class initialized_singleton; public: struct icallback diff --git a/inc/f4ll/dma_helper.h b/inc/f4ll/dma_helper.h index 534b59f..82347e7 100644 --- a/inc/f4ll/dma_helper.h +++ b/inc/f4ll/dma_helper.h @@ -5,8 +5,7 @@ * Author: abody */ -#ifndef LL_DMAHELPER_H_ -#define LL_DMAHELPER_H_ +#pragma once #include #include @@ -56,5 +55,3 @@ private: }; } /* namespace f4ll */ - -#endif /* LL_DMAHELPER_H_ */ diff --git a/inc/f4ll/fault.h b/inc/f4ll/fault.h index b6787ea..8bb1f2e 100644 --- a/inc/f4ll/fault.h +++ b/inc/f4ll/fault.h @@ -1,10 +1,9 @@ -#ifndef __FAULT_H -#define __FAULT_H +#pragma once -#define FAULT_REASON_HARD_FAULT 1 -#define FAULT_REASON_MEMMANAGE_FAULT 2 -#define FAULT_REASON_BUS_FAULT 3 -#define FAULT_REASON_USAGE_FAULT 4 +#define FAULT_REASON_HARD_FAULT 1 +#define FAULT_REASON_MEMMANAGE_FAULT 2 +#define FAULT_REASON_BUS_FAULT 3 +#define FAULT_REASON_USAGE_FAULT 4 #ifdef __cplusplus extern "C" { @@ -43,5 +42,3 @@ __attribute__((noreturn)) void fault_handler(uint32_t type, fault_context_t *con #ifdef __cplusplus } #endif - -#endif /* __FAULT_H */ diff --git a/inc/f4ll/initialized_singleton.h b/inc/f4ll/initialized_singleton.h new file mode 100644 index 0000000..ff9936d --- /dev/null +++ b/inc/f4ll/initialized_singleton.h @@ -0,0 +1,27 @@ +#pragma once + +#include + +namespace f4ll { + +template class initialized_singleton +{ +public: + static T &instance() { return *m_instance; } + template static T &init(args_t &&...args) + { + static T instance{std::forward(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 T *initialized_singleton::m_instance = nullptr; + +} // namespace f1ll { diff --git a/inc/f4ll/irq_lock.h b/inc/f4ll/irq_lock.h index f5c03a5..b36ef75 100644 --- a/inc/f4ll/irq_lock.h +++ b/inc/f4ll/irq_lock.h @@ -1,5 +1,4 @@ -#ifndef _IRQLOCK_H_INCLUDED -#define _IRQLOCK_H_INCLUDED +#pragma once #include #include @@ -19,8 +18,6 @@ public: inline ~irq_lock() { __set_PRIMASK(m_primask); } private: - uint32_t m_primask; + uint32_t m_primask; }; } - -#endif // _IRQLOCK_H_INCLUDED diff --git a/inc/f4ll/memcpy_dma.h b/inc/f4ll/memcpy_dma.h index 2b078f6..4fcd299 100644 --- a/inc/f4ll/memcpy_dma.h +++ b/inc/f4ll/memcpy_dma.h @@ -7,13 +7,13 @@ #pragma once #include -#include +#include namespace f4ll { -class memcpy_dma : public singleton, private dma_helper +class memcpy_dma : public initialized_singleton, private dma_helper { - friend class singleton; + friend class initialized_singleton; public: void *copy(void *dst, void const *src, uint16_t length); @@ -24,4 +24,4 @@ private: bool volatile m_busy = false; }; -} /* namespace f4ll */ +} // namespace f4ll diff --git a/inc/f4ll/packet_usart.h b/inc/f4ll/packet_usart.h index 649a813..1bb9a96 100644 --- a/inc/f4ll/packet_usart.h +++ b/inc/f4ll/packet_usart.h @@ -5,11 +5,12 @@ * Author: abody */ -#ifndef LL_HSUSART_H_ -#define LL_HSUSART_H_ +#pragma once + +#include + #include #include -#include namespace f4ll { @@ -119,4 +120,3 @@ private: }; } -#endif /* LL_HSUSART_H_ */ diff --git a/inc/f4ll/singleton.h b/inc/f4ll/singleton.h deleted file mode 100644 index e9a8fe8..0000000 --- a/inc/f4ll/singleton.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef SINGLETON_H_ -#define SINGLETON_H_ - -#include - -namespace f4ll { - -template class singleton -{ -public: - static T &instance() - { - return *m_instance; - } - template static T &init(args_t &&...args) - { - static T instance{std::forward(args)...}; - m_instance = &instance; - return instance; - } - -protected: - singleton() = default; - singleton(const singleton &) = delete; - singleton &operator=(const singleton &) = delete; - static T *m_instance; -}; - -template T *singleton::m_instance = nullptr; - -} // namespace f1ll { - -#endif /* SINGLETON_H_ */ diff --git a/inc/f4ll/str_util.h b/inc/f4ll/str_util.h index 49ddc39..01bb412 100644 --- a/inc/f4ll/str_util.h +++ b/inc/f4ll/str_util.h @@ -5,8 +5,7 @@ * Author: compi */ -#ifndef _STM32PLUS_STRUTIL_H_ -#define _STM32PLUS_STRUTIL_H_ +#pragma once #include #include @@ -27,5 +26,3 @@ char tochr(const uint8_t in, const uint8_t upper); #ifdef __cplusplus } #endif - -#endif /* _STM32PLUS_STRUTIL_H_ */ diff --git a/inc/f4ll/usart_core.h b/inc/f4ll/usart_core.h index f4f44be..508df34 100644 --- a/inc/f4ll/usart_core.h +++ b/inc/f4ll/usart_core.h @@ -5,8 +5,8 @@ * Author: abody */ -#ifndef LL_USARTCORE_H_ -#define LL_USARTCORE_H_ +#pragma once + #include #include @@ -44,11 +44,10 @@ private: virtual void tx_dma_half_transfer(void) = 0; virtual void tx_dma_error(dma_helper::dma_error_type reason) = 0; +public: void usart_isr(); void rx_dma_isr(); void tx_dma_isr(); }; } /* namespace f4ll */ - -#endif /* LL_USARTCORE_H_ */ diff --git a/src/console_handler.cpp b/src/console_handler.cpp index 694e107..a4ba4a6 100644 --- a/src/console_handler.cpp +++ b/src/console_handler.cpp @@ -71,19 +71,15 @@ size_t console_handler::append(char const *s) void console_handler::flush() { - bool busy; - if (!m_tx_buffer.uncommited()) { return; } m_tx_buffer.commit(); - { - irq_lock l; - busy = m_in_flight_size != 0; - } - if (busy) { + + if (m_in_flight_size) { return; } + uint8_t const *chunk; m_tx_buffer.get_chunk(m_tx_buffer.size(), chunk, m_in_flight_size); if (m_in_flight_size) { diff --git a/src/crc_handler.cpp b/src/crc_handler.cpp index 8693a6f..7144fb3 100644 --- a/src/crc_handler.cpp +++ b/src/crc_handler.cpp @@ -4,6 +4,7 @@ * Created on: Oct 26, 2019 * Author: compi */ + #include namespace f4ll { diff --git a/src/fault.cpp b/src/fault.cpp index 2187ade..83ae3c2 100644 --- a/src/fault.cpp +++ b/src/fault.cpp @@ -6,7 +6,7 @@ * -c "tpiu config internal uart off " */ #include -//#include +// #include #include #include #include @@ -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) { - while(len) { - if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && // ITM enabled - ((ITM->TER & (1UL << port) ) != 0UL) ) // ITM Port enabled - { - // Wait until shift register is free - while (ITM->PORT[port].u32 == 0UL) { - __ASM volatile ("nop"); - } - if(len >= 4) { - ITM->PORT[port].u32 = *(uint32_t*)(str); - str += 4; - len -= 4; - } else if(len >= 2) { - ITM->PORT[port].u16 = *(uint16_t*)(str); - str += 2; - len -= 2; - } else { - ITM->PORT[port].u8 = *(uint8_t*)(str); - ++str; - --len; - } - } else - break; - } + while (len) { + if (((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0UL) && // ITM enabled + ((ITM->TER & (1UL << port)) != 0UL)) // ITM Port enabled + { + // Wait until shift register is free + while (ITM->PORT[port].u32 == 0UL) { + __ASM volatile("nop"); + } + if (len >= 4) { + ITM->PORT[port].u32 = *(uint32_t *)(str); + str += 4; + len -= 4; + } else if (len >= 2) { + ITM->PORT[port].u16 = *(uint16_t *)(str); + str += 2; + len -= 2; + } else { + ITM->PORT[port].u8 = *(uint8_t *)(str); + ++str; + --len; + } + } else { + break; + } + } } 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; - while(*fmtstr) { - if(*fmtstr == '%') { + while (*fmtstr) { + if (*fmtstr == '%') { swo_send_str(next_chunk, fmtstr - next_chunk, 0); uitohex(hex_str, *values++, 8); swo_send_str(hex_str, 8, 0); @@ -71,80 +72,71 @@ void fault_print_str(char const *fmtstr, uint32_t *values) void fault_handler(uint32_t type, fault_context_t *context) { - uint32_t FSR[9] = { - SCB->HFSR, - 0xff & SCB->CFSR, - (0xff00 & SCB->CFSR) >> 8, - (0xffff0000 & SCB->CFSR) >> 16, - SCB->DFSR, - SCB->AFSR, - SCB->SHCSR, - SCB->MMFAR, - SCB->BFAR - }; + uint32_t FSR[9] = { + SCB->HFSR, 0xff & SCB->CFSR, (0xff00 & SCB->CFSR) >> 8, (0xffff0000 & SCB->CFSR) >> 16, SCB->DFSR, SCB->AFSR, SCB->SHCSR, + SCB->MMFAR, SCB->BFAR}; - while(1) { - fault_print_str("\n++ Fault Handler ++\n\nFaultType: ",NULL); - switch( type ) { - case FAULT_REASON_HARD_FAULT: - fault_print_str("HardFault",NULL); - break; - case FAULT_REASON_MEMMANAGE_FAULT: - fault_print_str("MemManageFault",NULL); - break; - case FAULT_REASON_BUS_FAULT: - fault_print_str("BusFault",NULL); - break; - case FAULT_REASON_USAGE_FAULT: - fault_print_str("UsageFault",NULL); - break; - default: - fault_print_str("Unknown Fault",NULL); - break; - } + while (1) { + fault_print_str("\n++ Fault Handler ++\n\nFaultType: ", NULL); + switch (type) { + case FAULT_REASON_HARD_FAULT: + fault_print_str("HardFault", NULL); + break; + case FAULT_REASON_MEMMANAGE_FAULT: + fault_print_str("MemManageFault", NULL); + break; + case FAULT_REASON_BUS_FAULT: + fault_print_str("BusFault", NULL); + break; + case FAULT_REASON_USAGE_FAULT: + fault_print_str("UsageFault", NULL); + break; + default: + fault_print_str("Unknown Fault", NULL); + break; + } - fault_print_str("\n\nContext:",NULL); + fault_print_str("\n\nContext:", NULL); - fault_print_str( - "\nR0 : %" - "\nR1 : %" - "\nR2 : %" - "\nR3 : %" - "\nR4 : %" - "\nR5 : %" - "\nR6 : %" - "\nR7 : %" - "\nR8 : %" - "\nR9 : %" - "\nR10 : %" - "\nR11 : %" - "\nR12 : %" - "\nSP : %" - "\nLR : %" - "\nPC : %" - "\nxPSR : %" - "\nPSP : %" - "\nMSP : %", - (uint32_t *)context); + fault_print_str( + "\nR0 : %" + "\nR1 : %" + "\nR2 : %" + "\nR3 : %" + "\nR4 : %" + "\nR5 : %" + "\nR6 : %" + "\nR7 : %" + "\nR8 : %" + "\nR9 : %" + "\nR10 : %" + "\nR11 : %" + "\nR12 : %" + "\nSP : %" + "\nLR : %" + "\nPC : %" + "\nxPSR : %" + "\nPSP : %" + "\nMSP : %", + (uint32_t *)context); - //Capture CPUID to get core/cpu info - fault_print_str("\nCPUID: %",(uint32_t *)&SCB->CPUID); + // Capture CPUID to get core/cpu info + fault_print_str("\nCPUID: %", (uint32_t *)&SCB->CPUID); - fault_print_str( - "\nHFSR : %" - "\nMMFSR: %" - "\nBFSR : %" - "\nUFSR : %" - "\nDFSR : %" - "\nAFSR : %" - "\nSHCSR: %", - FSR); + fault_print_str( + "\nHFSR : %" + "\nMMFSR: %" + "\nBFSR : %" + "\nUFSR : %" + "\nDFSR : %" + "\nAFSR : %" + "\nSHCSR: %", + FSR); - app_fault_callback(type); - } + app_fault_callback(type); + } } #ifdef __cplusplus } #endif - diff --git a/src/packet_usart.cpp b/src/packet_usart.cpp index f56a0b6..046d5ea 100644 --- a/src/packet_usart.cpp +++ b/src/packet_usart.cpp @@ -5,7 +5,8 @@ * Author: abody */ #include -#include + +#include namespace f4ll { diff --git a/src/str_util.cpp b/src/str_util.cpp index 7bc9bcb..54b95df 100644 --- a/src/str_util.cpp +++ b/src/str_util.cpp @@ -1,5 +1,6 @@ #include -#include + +#include ////////////////////////////////////////////////////////////////////////////// size_t strcpy_ex(char *dst, char const *src)