aboutsummaryrefslogtreecommitdiffstats
path: root/src/sam3x8e/main.c
blob: ca002c917a91a40a46ef82f5882eb2d8fa95e674 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Main starting point for SAM3x8e boards.
//
// Copyright (C) 2016  Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.

#include "board/misc.h" // console_get_input
#include "sam3x8e.h" // WDT
#include "sched.h" // sched_main


/****************************************************************
 * watchdog handler
 ****************************************************************/

static void
watchdog_reset(void)
{
    WDT->WDT_CR = 0xA5000001;
}
DECL_TASK(watchdog_reset);

static void
watchdog_init(void)
{
    uint32_t timeout = 32768 / 2;  // 500ms timeout
    WDT->WDT_MR = WDT_MR_WDRSTEN | WDT_MR_WDV(timeout) | WDT_MR_WDD(timeout);
}
DECL_INIT(watchdog_init);


/****************************************************************
 * irq clearing on fault
 ****************************************************************/

// Clear the active irq if a shutdown happened in an irq handler
static void
clear_active_irq(void)
{
    uint32_t psr;
    asm volatile("mrs %0, psr" : "=r" (psr));
    if (!(psr & 0x1ff))
        // Shutdown did not occur in an irq - nothing to do.
        return;
    // Clear active irq status
    psr &= ~0x1ff;
    psr |= 1<<24; // T-bit
    uint32_t temp;
    asm volatile(
        "  push { %1 }\n"
        "  adr %0, 1f\n"
        "  push { %0 }\n"
        "  push { r0, r1, r2, r3, r12, lr }\n"
        "  bx %2\n"
        "1:\n"
        : "=&r"(temp) : "r"(psr), "r"(0xfffffff9));
}
DECL_SHUTDOWN(clear_active_irq);


/****************************************************************
 * misc functions
 ****************************************************************/

size_t
alloc_maxsize(size_t reqsize)
{
    return reqsize;
}

void * __visible
_sbrk(int nbytes)
{
    extern char _end;
    static void *heap_ptr = (void *)&_end;
    void *pos = heap_ptr;
    heap_ptr = pos + nbytes;
    return pos;
}

// Main entry point
int
main(void)
{
    SystemInit();
    sched_main();
    return 0;
}