C++ build fixed, C dropped
This commit is contained in:
parent
c34b20ad8c
commit
a2f6e40abb
5 changed files with 335 additions and 1 deletions
19
inc/fault.h
Normal file
19
inc/fault.h
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
#ifndef __FAULT_H
|
||||||
|
#define __FAULT_H
|
||||||
|
|
||||||
|
#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" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void app_fault_callback(uint32_t reason);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* __FAULT_H */
|
31
inc/strutil.h
Normal file
31
inc/strutil.h
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
/*
|
||||||
|
* strutil.h
|
||||||
|
*
|
||||||
|
* Created on: Feb 11, 2017
|
||||||
|
* Author: compi
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _STM32PLUS_STRUTIL_H_
|
||||||
|
#define _STM32PLUS_STRUTIL_H_
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t strcpy_ex(char *dst, char const *src);
|
||||||
|
size_t uitodec(char* buffer, uint32_t data);
|
||||||
|
size_t uitohex(char* buffer, uint32_t data, uint8_t chars);
|
||||||
|
size_t itodec(char* buffer, int data);
|
||||||
|
size_t itohex(char* buffer, int data);
|
||||||
|
void strrev(char *first, char *last);
|
||||||
|
char tochr(const uint8_t in, const uint8_t upper);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _STM32PLUS_STRUTIL_H_ */
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "f4ll/consolehandler.h"
|
#include "f4ll/consolehandler.h"
|
||||||
#include <f4ll_c/strutil.h>
|
#include "f4ll/strutil.h"
|
||||||
|
|
||||||
namespace f4ll {
|
namespace f4ll {
|
||||||
|
|
||||||
|
|
174
src/fault.cpp
Normal file
174
src/fault.cpp
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
/*
|
||||||
|
* fault.c
|
||||||
|
*
|
||||||
|
* Created on: Oct 1, 2019
|
||||||
|
* Author: abody
|
||||||
|
* -c "tpiu config internal <logfile_full_path> uart off <cpufreq>"
|
||||||
|
*/
|
||||||
|
#include <inttypes.h>
|
||||||
|
//#include <core_cm4.h>
|
||||||
|
#include "stm32f4xx.h"
|
||||||
|
#include "f4ll/strutil.h"
|
||||||
|
#include "f4ll/fault.h"
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint32_t R0;
|
||||||
|
uint32_t R1;
|
||||||
|
uint32_t R2;
|
||||||
|
uint32_t R3;
|
||||||
|
uint32_t R4;
|
||||||
|
uint32_t R5;
|
||||||
|
uint32_t R6;
|
||||||
|
uint32_t R7;
|
||||||
|
uint32_t R8;
|
||||||
|
uint32_t R9;
|
||||||
|
uint32_t R10;
|
||||||
|
uint32_t R11;
|
||||||
|
uint32_t R12;
|
||||||
|
uint32_t SP;
|
||||||
|
uint32_t LR;
|
||||||
|
uint32_t PC;
|
||||||
|
uint32_t xPSR;
|
||||||
|
uint32_t PSP;
|
||||||
|
uint32_t MSP;
|
||||||
|
uint32_t EXC_RETURN;
|
||||||
|
uint32_t CONTROL;
|
||||||
|
} fault_context_t;
|
||||||
|
|
||||||
|
fault_context_t g_faultContext;
|
||||||
|
|
||||||
|
void __attribute__((weak)) app_fault_callback(uint32_t reason)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void SwoSendStr(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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void fault_print_str(char const *fmtstr, uint32_t *values)
|
||||||
|
{
|
||||||
|
char hex_str[9]={0};
|
||||||
|
char const *nextChunk = fmtstr;
|
||||||
|
|
||||||
|
while(*fmtstr) {
|
||||||
|
if(*fmtstr == '%') {
|
||||||
|
SwoSendStr(nextChunk, fmtstr-nextChunk, 0);
|
||||||
|
uitohex(hex_str, *values++, 8);
|
||||||
|
SwoSendStr(hex_str, 8, 0);
|
||||||
|
nextChunk = fmtstr +1;
|
||||||
|
}
|
||||||
|
++fmtstr;
|
||||||
|
}
|
||||||
|
if(nextChunk != fmtstr)
|
||||||
|
SwoSendStr(nextChunk, fmtstr-nextChunk, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
__attribute__((noreturn)) void FaultHandler(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
|
||||||
|
};
|
||||||
|
|
||||||
|
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(
|
||||||
|
"\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);
|
||||||
|
|
||||||
|
fault_print_str(
|
||||||
|
"\nHFSR : %"
|
||||||
|
"\nMMFSR: %"
|
||||||
|
"\nBFSR : %"
|
||||||
|
"\nUFSR : %"
|
||||||
|
"\nDFSR : %"
|
||||||
|
"\nAFSR : %"
|
||||||
|
"\nSHCSR: %",
|
||||||
|
FSR);
|
||||||
|
|
||||||
|
app_fault_callback(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
110
src/strutil.cpp
Normal file
110
src/strutil.cpp
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "f4ll/strutil.h"
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t strcpy_ex(char *dst, char const *src)
|
||||||
|
{
|
||||||
|
size_t ret = 0;
|
||||||
|
do {
|
||||||
|
*dst++ = *src;
|
||||||
|
++ret;
|
||||||
|
} while(*src++);
|
||||||
|
return ret - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
void strrev(char *first, char *last)
|
||||||
|
{
|
||||||
|
char tmp;
|
||||||
|
while(last > first) {
|
||||||
|
tmp = *first;
|
||||||
|
*first++ = *last;
|
||||||
|
*last-- = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
char tochr(const uint8_t in, const uint8_t upper)
|
||||||
|
{
|
||||||
|
return in + ((in < 10) ? '0' : (upper ? 'A' : 'a') - 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t uitodec(char* buffer, uint32_t data)
|
||||||
|
{
|
||||||
|
char *b2 = buffer;
|
||||||
|
if(!data) {
|
||||||
|
*b2++ = '0';
|
||||||
|
*b2 = '\0';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(data) {
|
||||||
|
*b2++ = (data % 10) + '0';
|
||||||
|
data /= 10;
|
||||||
|
}
|
||||||
|
size_t ret = b2 - buffer;
|
||||||
|
|
||||||
|
*b2-- = 0;
|
||||||
|
|
||||||
|
strrev(buffer, b2);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t uitohex(char* buffer, uint32_t data, uint8_t chars)
|
||||||
|
{
|
||||||
|
char *b2 = buffer;
|
||||||
|
size_t ret = 0;
|
||||||
|
|
||||||
|
if(chars == 0xff || !chars)
|
||||||
|
{
|
||||||
|
if(!data) {
|
||||||
|
*b2++ = '0';
|
||||||
|
*b2 = '\0';
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while(data) {
|
||||||
|
uint8_t curval = data & 0x0f;
|
||||||
|
*b2++ = tochr(curval, 1);
|
||||||
|
data >>= 4;
|
||||||
|
}
|
||||||
|
ret = b2 - buffer;
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = chars;
|
||||||
|
for(uint8_t pos = 0; pos < (uint8_t)ret; ++pos) {
|
||||||
|
*b2++ = tochr(data & 0x0f, 1);
|
||||||
|
data >>= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
*b2-- = 0;
|
||||||
|
strrev(buffer, b2);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t itodec(char* buffer, int data)
|
||||||
|
{
|
||||||
|
if(data < 0) {
|
||||||
|
*buffer++ = '-';
|
||||||
|
return uitodec(buffer, -data) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return uitodec(buffer, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
|
size_t itohex(char* buffer, int data)
|
||||||
|
{
|
||||||
|
if(data < 0) {
|
||||||
|
*buffer++ = '-';
|
||||||
|
return uitohex(buffer, -data, 0) + 1;
|
||||||
|
}
|
||||||
|
return uitohex(buffer, data, 0);
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue