2017. 10. 26. 23:09 devel/code

arm hardfault handler


출처 : https://stackoverflow.com/questions/21293580/atmel-studio-dummy-handler



인터럽트 소스 얻어오는 방법


uint32_t phantomISR = 9999;

void Dummy_Handler(void)
{
    while(1) {
        phantomISR = __get_IPSR();
    }
}


ISR_NUMBER
This is the number of the current exception:
0 = Thread mode
1 = Reserved
2 = NMI
3 = Hard fault
4 = Memory management fault
5 = Bus fault
6 = Usage fault
7-10 = Reserved
11 = SVCall
12 = Reserved for Debug
13 = Reserved
14 = PendSV
15 = SysTick
16 = IRQ0
45 = IRQ29



SVC handler - gcc inline assembly


__attribute__((naked)) void SVC_Handler(void) {
    __asm__ __volatile__ (
    "   tst     lr, #0x4            \n\t"
    "   ite     eq                  \n\t"
    "   mrseq   r0, msp             \n\t"
    "   mrsne   r0, psp             \n\t"
    "   b   SVC_Handler_C           \n\t");
}
__attribute__((naked)) void SVC_Handler(void) {
    register unsigned int *svc_args __asm__("r0");

    __asm__ __volatile__ (
        "tst     lr, #0x4
         ite     eq
         mrseq   %0, msp
         mrsne   %0, psp" : "=r"(svc_args));

    return SVC_Handler_C(svc_args);
}



출처 : https://blog.frankvh.com/2011/12/07/cortex-m3-m4-hard-fault-handler/


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// From Joseph Yiu, minor edits by FVH
// hard fault handler in C,
// with stack frame location as input parameter
// called from HardFault_Handler in file xxx.s
void hard_fault_handler_c (unsigned int * hardfault_args)
{
  unsigned int stacked_r0;
  unsigned int stacked_r1;
  unsigned int stacked_r2;
  unsigned int stacked_r3;
  unsigned int stacked_r12;
  unsigned int stacked_lr;
  unsigned int stacked_pc;
  unsigned int stacked_psr;
 
  stacked_r0 = ((unsigned long) hardfault_args[0]);
  stacked_r1 = ((unsigned long) hardfault_args[1]);
  stacked_r2 = ((unsigned long) hardfault_args[2]);
  stacked_r3 = ((unsigned long) hardfault_args[3]);
 
  stacked_r12 = ((unsigned long) hardfault_args[4]);
  stacked_lr = ((unsigned long) hardfault_args[5]);
  stacked_pc = ((unsigned long) hardfault_args[6]);
  stacked_psr = ((unsigned long) hardfault_args[7]);
 
  printf ("\n\n[Hard fault handler - all numbers in hex]\n");
  printf ("R0 = %x\n", stacked_r0);
  printf ("R1 = %x\n", stacked_r1);
  printf ("R2 = %x\n", stacked_r2);
  printf ("R3 = %x\n", stacked_r3);
  printf ("R12 = %x\n", stacked_r12);
  printf ("LR [R14] = %x  subroutine call return address\n", stacked_lr);
  printf ("PC [R15] = %x  program counter\n", stacked_pc);
  printf ("PSR = %x\n", stacked_psr);
  printf ("BFAR = %x\n", (*((volatile unsigned long *)(0xE000ED38))));
  printf ("CFSR = %x\n", (*((volatile unsigned long *)(0xE000ED28))));
  printf ("HFSR = %x\n", (*((volatile unsigned long *)(0xE000ED2C))));
  printf ("DFSR = %x\n", (*((volatile unsigned long *)(0xE000ED30))));
  printf ("AFSR = %x\n", (*((volatile unsigned long *)(0xE000ED3C))));
  printf ("SCB_SHCSR = %x\n", SCB->SHCSR);
  
  while (1);
}


Posted by 쵸코케키

2017. 2. 17. 10:41 devel/code

argument parsing

http://linoxide.com/linux-command/use-ip-command-linux/



ip code 참조

Posted by 쵸코케키

Unnamed Structure and Union Fields - Anonymous Struct

검색해보니 대략 구조가 확정적이지 않을 때 사용할 수 있으나 좋은 방법은 아니라는 것 같다
union의 field 중 비구조체와 구조체를 같이 깔끔하게 넣을 수 있다
혹은 자료형을 확정적이지 않게 사용할 수 있다

c로 function overloading 도 가능하다 ㅎㅎ

struct sensors_poll_device_t {
    struct hw_device_t common;
    int (*activate)(struct sensors_poll_device_t *dev,
            int sensor_handle, int enabled);
    int (*setDelay)(struct sensors_poll_device_t *dev,
            int sensor_handle, int64_t sampling_period_ns);
    int (*poll)(struct sensors_poll_device_t *dev,
            sensors_event_t* data, int count);
};

typedef struct sensors_poll_device_1 {
    union {
        /* sensors_poll_device_1 is compatible with sensors_poll_device_t,
         * and can be down-cast to it
         */
        struct sensors_poll_device_t v0;

        struct {
            struct hw_device_t common;

            int (*activate)(struct sensors_poll_device_t *dev,
                    int sensor_handle, int enabled);
            int (*setDelay)(struct sensors_poll_device_t *dev,
                    int sensor_handle, int64_t sampling_period_ns);
            int (*poll)(struct sensors_poll_device_t *dev,
                    sensors_event_t* data, int count);
        };
    };

    int (*batch)(struct sensors_poll_device_1* dev,
            int sensor_handle, int flags, int64_t sampling_period_ns,
            int64_t max_report_latency_ns);
    int (*flush)(struct sensors_poll_device_1* dev, int sensor_handle);
    int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
    void (*reserved_procs[7])(void);
} sensors_poll_device_1_t;

struct sensors_poll_context_t {
    struct sensors_poll_device_t device; // must be first

        sensors_poll_context_t();
        ~sensors_poll_context_t();
    int activate(int handle, int enabled);
    ...

private:
    ...
};

정의는 이렇게 되어있고 실제 사용은 아래와 같다

static inline int sensors_open_1(const struct hw_module_t* module,
        sensors_poll_device_1_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}

static int open_sensors(const struct hw_module_t* module, const char* id, 
                        struct hw_device_t** device)
{
        sensors_poll_context_t *dev = new sensors_poll_context_t();
        memset(&dev->device, 0, sizeof(sensors_poll_device_t));


        *device = &dev->device.common;    //이 부분
        ...
}

sensors_poll_device_1_t 에서 struct hw_device_t 로 자유로이 형변환 하여 호출한다
overloading 하는 것 같은 느낌이 난다
왜냐하면 sensors_open_1 입장에서는 device 변수는 hw_device_t 로 활용하여 멤버 변수에 접근할 수 도 있고
아니면 그냥 sensors_poll_device_1 로 그대로 사용하여 batch나 flush 같은 애들을 호출할 수도 있다

https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html

'devel > code' 카테고리의 다른 글

arm hardfault handler  (0) 2017.10.26
argument parsing  (0) 2017.02.17
디버그 on/off에 따라 자료형을 다르게 하는 방법  (0) 2016.12.02
bus_for_each_dev - while 조건문  (0) 2016.10.10
구조체 선언 없이 값 리턴  (0) 2016.04.29
Posted by 쵸코케키

디버그 on/off에 따라 자료형을 다르게 하는 방법

원래 이렇게 해야하는걸

static const RegJNIRec gRegJNI[] = {
    { 엄청나게 긴 함수명, "엄청나게 긴 함수명" },
    ...
}

요렇게 하면!

#ifdef NDEBUG
    #define REG_JNI(name)      { name }
    struct RegJNIRec {
        int (*mProc)(JNIEnv*);
    };
#else
    #define REG_JNI(name)      { name, #name }
    struct RegJNIRec {
        int (*mProc)(JNIEnv*);
        const char* mName;
    };
#endif

static const RegJNIRec gRegJNI[] = {
    REG_JNI(register_com_android_internal_os_RuntimeInit),
    ...
};

static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
{
    for (size_t i = 0; i < count; i++) {
        if (array[i].mProc(env) < 0) {
#ifndef NDEBUG
            ALOGD("----------!!! %s failed to load\n", array[i].mName);
#endif
            return -1;
        }
    }
    return 0;
}

깔끔하다 perfect


출처

android/frameworks/base/core/jni/AndroidRuntime.cpp


'devel > code' 카테고리의 다른 글

argument parsing  (0) 2017.02.17
Unnamed Structure and Union Fields - 이름없는 구조체  (0) 2016.12.20
bus_for_each_dev - while 조건문  (0) 2016.10.10
구조체 선언 없이 값 리턴  (0) 2016.04.29
script ㅎㄸㄸ  (0) 2015.08.27
Posted by 쵸코케키
/**
 * bus_for_each_dev - device iterator.
 * @bus: bus type.
 * @start: device to start iterating from.
 * @data: data for the callback.
 * @fn: function to be called for each device.
 *
 * Iterate over @bus's list of devices, and call @fn for each,
 * passing it @data. If @start is not NULL, we use that device to
 * begin iterating from.
 *
 * We check the return of @fn each time. If it returns anything
 * other than 0, we break out and return that value.
 *
 * NOTE: The device that returns a non-zero value is not retained
 * in any way, nor is its refcount incremented. If the caller needs
 * to retain this data, it should do so, and increment the reference
 * count in the supplied callback.
 */
linux/drivers/base/bus.c
int bus_for_each_dev(struct bus_type *bus, struct device *start,
                     void *data, int (*fn)(struct device *, void *))
{
        struct klist_iter i;
        struct device *dev;
        int error = 0; 

        if (!bus || !bus->p)
                return -EINVAL;

        klist_iter_init_node(&bus->p->klist_devices, &i,
                             (start ? &start->p->knode_bus : NULL));
        while ((dev = next_device(&i)) && !error)
                error = fn(dev, data);
        klist_iter_exit(&i);
        return error;
}

while문을 보시라
이렇게 이쁘게도 할 수 있구나

Posted by 쵸코케키

커널 소스 보다가 발견

typedef struct test{
int                    a;
unsigned long     tvar;
char                  c;
}tt_t;

tt_t test_func(unsigned long val)
{
return (tt_t) {     .a = -9323,
     .tvar = val,
.c = 'a' };
}


int main()
{
unsigned long input = 7;
tt_t result = test_func(input);
printf("%d, %lu, %c\n", result.a, result.tvar, result.c);

return 0;
}

이런식으로 선언 없이 리턴 가능


'devel > code' 카테고리의 다른 글

디버그 on/off에 따라 자료형을 다르게 하는 방법  (0) 2016.12.02
bus_for_each_dev - while 조건문  (0) 2016.10.10
script ㅎㄸㄸ  (0) 2015.08.27
return 깔끔하게 처리  (0) 2015.01.16
ptr loop  (0) 2014.11.19
Posted by 쵸코케키

2015. 8. 27. 09:50 devel/code

script ㅎㄸㄸ

http://www.tldp.org/LDP/abs/html/colorizing.html


'devel > code' 카테고리의 다른 글

bus_for_each_dev - while 조건문  (0) 2016.10.10
구조체 선언 없이 값 리턴  (0) 2016.04.29
return 깔끔하게 처리  (0) 2015.01.16
ptr loop  (0) 2014.11.19
code 깔끔  (0) 2014.08.25
Posted by 쵸코케키



주구절절 if(written >0) else

이렇게 할 필요 없이 단 한줄로 끝..ㅎㄸㄸ

drivers/char/mem.c

'devel > code' 카테고리의 다른 글

구조체 선언 없이 값 리턴  (0) 2016.04.29
script ㅎㄸㄸ  (0) 2015.08.27
ptr loop  (0) 2014.11.19
code 깔끔  (0) 2014.08.25
bit field  (0) 2014.05.25
Posted by 쵸코케키

2014. 11. 19. 17:39 devel/code

ptr loop





'devel > code' 카테고리의 다른 글

script ㅎㄸㄸ  (0) 2015.08.27
return 깔끔하게 처리  (0) 2015.01.16
code 깔끔  (0) 2014.08.25
bit field  (0) 2014.05.25
struct device  (0) 2014.05.15
Posted by 쵸코케키

2014. 8. 25. 16:59 devel/code

code 깔끔





debug는 지옥?

ixgbe kcompat.h

'devel > code' 카테고리의 다른 글

return 깔끔하게 처리  (0) 2015.01.16
ptr loop  (0) 2014.11.19
bit field  (0) 2014.05.25
struct device  (0) 2014.05.15
쉘 스크립트 배열  (0) 2014.03.15
Posted by 쵸코케키
이전버튼 1 2 이전버튼

블로그 이미지
chocokeki
쵸코케키

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.3
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

최근에 올라온 글

최근에 달린 댓글

글 보관함