Contiki-NG
Loading...
Searching...
No Matches
startup-stubs.c
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include "flpr-shared.h"
9#include <stddef.h>
10
11/* Minimal mem-routines (no libc available). */
12void *memset(void *s, int c, size_t n) {
13 unsigned char *p = s;
14 while(n--) *p++ = (unsigned char)c;
15 return s;
16}
17
18void *memcpy(void *d, const void *s, size_t n) {
19 unsigned char *dp = d;
20 const unsigned char *sp = s;
21 while(n--) *dp++ = *sp++;
22 return d;
23}
24
25void *memmove(void *d, const void *s, size_t n) {
26 unsigned char *dp = d;
27 const unsigned char *sp = s;
28 if(dp < sp) {
29 while(n--) *dp++ = *sp++;
30 } else {
31 dp += n; sp += n;
32 while(n--) *--dp = *--sp;
33 }
34 return d;
35}
36
37int memcmp(const void *a, const void *b, size_t n) {
38 const unsigned char *ap = a, *bp = b;
39 while(n--) {
40 if(*ap != *bp) return (int)*ap - (int)*bp;
41 ap++; bp++;
42 }
43 return 0;
44}
45
46void SystemInit(void) { }
47
48extern int main(void);
49
50/* Override the spinning Trap_Handler from nrfx startup. Read mcause + mepc
51 * so we know what kind of exception fired and at what PC. The literal
52 * addresses/markers below mirror flpr-shared.h (a naked asm body cannot use the
53 * C macros): 0x2003F000 = FLPR_SHARED_COUNTER_ADDR (+4 = FLPR_FAULT_PC_ADDR),
54 * 0xFA110000 = FLPR_MARK_FAULT_BASE. */
55__attribute__((naked,aligned(8)))
56void my_trap_handler(void)
57{
58 __asm__ volatile (
59 "csrr t0, mcause \n"
60 "csrr t1, mepc \n"
61 "li t2, 0x2003F000 \n" /* FLPR_SHARED_COUNTER_ADDR */
62 "li a4, 0xFA110000 \n" /* FLPR_MARK_FAULT_BASE */
63 "andi t0, t0, 0xFF \n"
64 "or a4, a4, t0 \n"
65 "sw a4, 0(t2) \n" /* counter = 0xFA1100|cause */
66 "sw t1, 4(t2) \n" /* +4 = faulting PC */
67 "1: j 1b \n"
68 );
69}
70
71void _start(void) {
72 /* Reroute mtvec from the silent-spin Trap_Handler to ours. */
73 __asm__ volatile ("csrw mtvec, %0" :: "r"(my_trap_handler));
74 FLPR_SHARED_COUNTER = FLPR_MARK_BOOT;
75 main();
76 FLPR_SHARED_COUNTER = FLPR_MARK_EXIT;
77 for(;;);
78}
79
80void watchdog_periodic(void) { }
81void watchdog_init(void) { }
82void watchdog_start(void) { }
83void watchdog_stop(void) { }
84void watchdog_reboot(void) { for(;;); }
85
86void _exit(int code) { (void)code; for(;;); }
87
88int _write(int fd, const char *buf, int n) { (void)fd; (void)buf; return n; }
89int _read(int fd, char *buf, int n) { (void)fd; (void)buf; (void)n; return 0; }
90int _close(int fd) { (void)fd; return 0; }
91int _lseek(int fd, int off, int w) { (void)fd; (void)off; (void)w; return 0; }
92int _fstat(int fd, void *st) { (void)fd; (void)st; return 0; }
93int _isatty(int fd) { (void)fd; return 1; }
94int _kill(int pid, int sig) { (void)pid; (void)sig; return -1; }
95int _getpid(void) { return 1; }
96void *_sbrk(int incr) { (void)incr; return (void *)-1; }
static int off(void)
Definition cc2538-rf.c:533
void watchdog_reboot(void)
Keeps control until the WDT throws a reset signal.
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
void watchdog_periodic(void)
Writes the WDT clear sequence.
void watchdog_init(void)
Initialisation function for the WDT.
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.