-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.c
254 lines (211 loc) · 4.85 KB
/
core.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* @file libesoup/core.c
*
* @author John Whitmore
*
* @brief File containing the function to initialise the libesoup library
*
* Copyright 2017-2020 electronicSoup Limited
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the version 2 of the GNU Lesser General Public License
* as published by the Free Software Foundation
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************
*
*/
#include "libesoup_config.h"
#ifdef SYS_SERIAL_LOGGING
#define DEBUG_FILE
#ifdef XC16
static const char __attribute__((unused)) *TAG = "CORE";
#elif __XC8
static const char *TAG = "CORE";
#endif // XC16 elif XC8
#include "libesoup/logger/serial_log.h"
#endif // SYS_SERIAL_LOGGING
#include "libesoup/errno.h"
#include "libesoup/boards/board.h"
#ifdef SYS_HW_TIMERS
extern void hw_timer_init(void);
#endif
#ifdef SYS_SW_TIMERS
#include "libesoup/timers/sw_timers.h"
extern void sw_timer_init(void);
extern void timer_tick(void);
#endif
#if defined(SYS_UART1) || defined(SYS_UART2) || defined(SYS_UART3) || defined(SYS_UART4)
extern void uart_init(void);
#endif
#ifdef SYS_JOBS
#include "libesoup/jobs/jobs.h"
#endif
#ifdef SYS_HW_RTC
#include "libesoup/timers/rtc.h"
#endif
#if defined(SYS_SPI1) || defined(SYS_SPI2) || defined(SYS_SPI3)
extern result_t spi_init(void);
#endif
#if defined(SYS_I2C_1) || defined(SYS_I2C_2) || defined(SYS_I2C_3)
extern result_t i2c_tasks(void);
extern result_t i2c_init(void);
#endif
#ifdef SYS_RAND
#include "libesoup/utils/rand.h"
#endif
#ifdef SYS_CAN_BUS
#include "libesoup/comms/can/can.h"
#endif
#ifdef SYS_ADC
extern result_t adc_init(void);
extern result_t adc_tasks(void);
#endif
#ifdef SYS_PWM
extern result_t pwm_init(void);
#endif
#ifdef SYS_CHANGE_NOTIFICATION
#include "libesoup/gpio/change_notification.h"
#endif // SYS_CHANGE_NOTIFICATION
#ifdef SYS_MODBUS
extern result_t modbus_init(void);
#endif
#ifdef SYS_CHANGE_NOTIFICATION
extern result_t change_notifier_init(void);
#endif
#ifdef SYS_USB_KEYBOARD
#include "system.h"
#include "usb.h"
extern result_t usb_keyboard_tasks(void);
extern void SYSTEM_Initialize( SYSTEM_STATE SYSTEM_STATE_USB_START );
#endif
#ifdef SYS_MIDI_RX
extern void midi_init(void);
#endif
#ifdef SYS_LOOPER
extern result_t loppers_init(void);
#endif // SYS_LOOPER
/*
* The Instruction Clock Frequency being used by the system.
*
* SYS_CLOCK_FREQ is the frequency requested by libesoup_config.h but that
* may not be possible, if invalid.
*/
uint32_t sys_clock_freq;
result_t libesoup_init(void)
{
// uint32_t loop;
#ifdef XC16
result_t rc __attribute__((unused)) = 0;
#else
result_t rc = 0;
#endif
#if __XC8
#ifdef SYS_SERIAL_LOGGING
TAG = TAG;
#endif // SYS_SERIAL_LOGGING
rc = 0;
#endif
cpu_init();
CLEAR_WDT
#if defined(SYS_UART1) || defined(SYS_UART2) || defined(SYS_UART3) || defined(SYS_UART4)
uart_init();
__asm__ ("CLRWDT");
#endif
#ifdef SYS_SERIAL_LOGGING
rc = serial_logging_init();
RC_CHECK
__asm__ ("CLRWDT");
#endif
#ifdef SYS_HW_TIMERS
hw_timer_init();
__asm__ ("CLRWDT");
#endif
#ifdef SYS_SW_TIMERS
sw_timer_init();
__asm__ ("CLRWDT");
#endif
#ifdef SYS_HW_RTC
rc = rtc_init();
RC_CHECK
__asm__ ("CLRWDT");
#endif
#ifdef SYS_JOBS
jobs_init();
__asm__ ("CLRWDT");
#endif
#if defined(SYS_SPI1) || defined(SYS_SPI2) || defined(SYS_SPI3)
spi_init();
__asm__ ("CLRWDT");
#endif
#if defined(SYS_I2C_1) || defined(SYS_I2C_2) || defined(SYS_I2C_3)
i2c_init();
CLEAR_WDT
#endif
#ifdef SYS_RAND
random_init();
__asm__ ("CLRWDT");
CLEAR_WDT
#endif
#ifdef SYS_CHANGE_NOTIFICATION
rc = change_notifier_init();
RC_CHECK
__asm__ ("CLRWDT");
CLEAR_WDT
#endif // SYS_CHANGE_NOTIFICATION
#ifdef SYS_ADC
adc_init();
CLEAR_WDT
#endif
#ifdef SYS_PWM
rc = pwm_init();
#endif
CLEAR_WDT
#ifdef SYS_MODBUS
rc = modbus_init();
RC_CHECK
#endif
CLEAR_WDT
#ifdef SYS_USB_KEYBOARD
SYSTEM_Initialize( SYSTEM_STATE_USB_START );
USBDeviceInit();
USBDeviceAttach();
#endif
#ifdef SYS_MIDI_RX
midi_init();
#endif
#ifdef SYS_LOOPER
rc = loppers_init();
#endif // SYS_LOOPER
CLEAR_WDT
return(board_init());
}
result_t libesoup_tasks(void)
{
result_t rc = 0;
#ifdef SYS_SW_TIMERS
if(timer_ticked) timer_tick();
#endif
#ifdef SYS_ADC
rc = adc_tasks();
RC_CHECK
#endif
#ifdef SYS_CAN_BUS
can_tasks();
#endif
#ifdef SYS_USB_KEYBOARD
rc = usb_keyboard_tasks();
#endif
#if defined(SYS_I2C1) || defined(SYS_I2C2) || defined(SYS_I2C3)
rc = i2c_tasks();
#endif
CLEAR_WDT
return(rc);
}