aboutsummaryrefslogtreecommitdiff
path: root/src/init.c
blob: 8d895f5e63ee19db39b181ec03293382ceeb284a (plain) (blame)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stddef.h>
#include <stdint.h>

#include "isr_vector.h"
#include "ch573/gpio.h"

_Static_assert(offsetof(typeof(isr_vector_t), nmi_cb) == 0x8, "Offset wrong");

_Static_assert(
    offsetof(typeof(isr_vector_t), systick_cb) == 0x30, "Offset wrong ");

_Static_assert(
    offsetof(typeof(isr_vector_t), tmr0_cb) == 0x40, "Offset wrong ");

_Static_assert(
    offsetof(typeof(isr_vector_t), tmr1_cb) == 0x60, "Offset wrong ");

_Static_assert(
    offsetof(typeof(isr_vector_t), pwmx_cb) == 0x7c, "Offset wrong ");

_Static_assert(
    offsetof(typeof(isr_vector_t), wdog_bat_cb) == 0x8c, "Offset wrong ");

void on_reset(void);

void __attribute__((weak, interrupt)) default_irq_handler(void)
{
  return;
}

#define WEAK_IRQ(irq) \
  void __attribute__((weak, alias("default_irq_handler"))) irq(void)

WEAK_IRQ(irq_on_nmi);
WEAK_IRQ(irq_on_exc);
WEAK_IRQ(irq_on_systick);
WEAK_IRQ(irq_on_swi);
WEAK_IRQ(irq_on_tmr0);
WEAK_IRQ(irq_on_gpio_a);
WEAK_IRQ(irq_on_gpio_b);
WEAK_IRQ(irq_on_spi0);
WEAK_IRQ(irq_on_blel);
WEAK_IRQ(irq_on_bleb);
WEAK_IRQ(irq_on_usb);
WEAK_IRQ(irq_on_tmr1);
WEAK_IRQ(irq_on_tmr2);
WEAK_IRQ(irq_on_uart0);
WEAK_IRQ(irq_on_uart1);
WEAK_IRQ(irq_on_rtc);
WEAK_IRQ(irq_on_adc);
WEAK_IRQ(irq_on_pwmx);
WEAK_IRQ(irq_on_tmr3);
WEAK_IRQ(irq_on_uart2);
WEAK_IRQ(irq_on_uart3);
WEAK_IRQ(irq_on_wdog_bat);

/** The ISR Vector structure. This is linked to starting at address 0. */
__attribute((__section__(".isr_vector"))) volatile isr_vector_t isr_vectors = {
    .reset_cb = on_reset,
    .nmi_cb = irq_on_nmi,
    .exc_cb = irq_on_exc,
    .systick_cb = irq_on_systick,
    .swi_cb = irq_on_swi,
    .tmr0_cb = irq_on_tmr0,
    .gpio_a_cb = irq_on_gpio_a,
    .gpio_b_cb = irq_on_gpio_b,
    .spi0_cb = irq_on_spi0,
    .blel_cb = irq_on_blel,
    .bleb_cb = irq_on_bleb,
    .usb_cb = irq_on_usb,
    .tmr1_cb = irq_on_tmr1,
    .tmr2_cb = irq_on_tmr2,
    .uart0_cb = irq_on_uart0,
    .uart1_cb = irq_on_uart1,
    .rtc_cb = irq_on_rtc,
    .adc_cb = irq_on_adc,
    .pwmx_cb = irq_on_pwmx,
    .tmr3_cb = irq_on_tmr3,
    .uart2_cb = irq_on_uart2,
    .uart3_cb = irq_on_uart3,
    .wdog_bat_cb = irq_on_wdog_bat,
};

// Memory-mapped address of the data values in flash.
extern uint32_t* DATA_VALUES_IN_FLASH;

// Where the data is located in sram.
extern uint32_t* DATA_SEGMENT_START;
extern uint32_t* DATA_SEGMENT_STOP;

// Where 0-initialized data is in sram.
extern uint32_t* BSS_START;
extern uint32_t* BSS_STOP;

/*
 * Initialize the data segment and the bss segment.
 *
 * This function loads data in the .data section from flash and puts it in
 * memory where it can be edited and initialized the BSS data to 0.
 */
void init_data_segments(void)
{
  uint32_t* src = DATA_VALUES_IN_FLASH;
  uint32_t* dest = DATA_SEGMENT_START;

  while (dest != DATA_SEGMENT_STOP) {
    *(dest++) = *(src++);
  }

  dest = BSS_START;
  while (dest != BSS_STOP) {
    *(dest++) = 0;
  }
}

/*
 *  External reference to the main() function.
 */
extern void main(void);

#define GPIO_PORT_A ch573_gpio__gpio_port_a
#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF
/* Start function. Responsible for initializing the system and jumping to the
 * main function. */
// static void start(void)
// {
//   // init_data_segments();
//   GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8);
//   GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 8);
//   GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8);
// 
//   main();
// }

/*
 * The reset callback.This has to be a naked function because the stack pointer
 * may not be initialized!!.
 */

/*
 * The reset callback.This has to be a naked function because the stack pointer
 * may not be initialized!!.
 */