hardware.c 5.4 KB
Newer Older
1
2
3
#include "epicardium.h"

#include "api/dispatcher.h"
swym's avatar
swym committed
4
#include "usb/epc_usb.h"
5
6
#include "modules/filesystem.h"
#include "modules/log.h"
7
#include "modules/modules.h"
8
#include "modules/stream.h"
9
10

#include "card10.h"
11
12
13
14
15
#include "display.h"
#include "leds.h"
#include "pb.h"
#include "pmic.h"
#include "portexpander.h"
16
#include "max86150.h"
17
18
19

#include "gpio.h"
#include "i2c.h"
20
#include "rtc.h"
21
#include "spi.h"
22
#include "wdt.h"
23
24
25
26
27
28
29
30

/*
 * Early init is called at the very beginning and is meant for modules which
 * absolutely need to start as soon as possible.  hardware_early_init() blocks
 * which means code in here should be fast.
 */
int hardware_early_init(void)
{
31
32
33
	/*
	 * Watchdog timer
	 */
34
	watchdog_init();
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	/*
	 * I2C bus for onboard peripherals (ie. PMIC, BMA400, BHI160, BME680,
	 * ...)
	 */
	I2C_Shutdown(MXC_I2C1_BUS0);
	I2C_Init(MXC_I2C1_BUS0, I2C_FAST_MODE, NULL);

#ifndef CARD10_DEBUG_CORE1
	/*
	 * SAO I2C bus
	 */
	I2C_Shutdown(MXC_I2C0_BUS0);
	I2C_Init(MXC_I2C0_BUS0, I2C_FAST_MODE, NULL);
#endif

	/*
	 * GPIO peripheral.
	 */
	GPIO_Init();

56
57
58
59
60
61
62
	/* Set the power hold pin, so the PMIC does not turn off again */
	const gpio_cfg_t pwr_hold_pin = {
		PORT_0, PIN_30, GPIO_FUNC_OUT, GPIO_PAD_NONE
	};
	GPIO_Config(&pwr_hold_pin);
	GPIO_OutSet(&pwr_hold_pin);

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	/*
	 * PMIC (MAX77650)
	 */
	pmic_init();
	pmic_set_led(0, 0);
	pmic_set_led(1, 0);
	pmic_set_led(2, 0);

	/*
	 * Harmonic Board Portexpander
	 */
	portexpander_init();

	/*
	 * Buttons
	 */
	PB_Init();

81
	/* Enable 32 kHz output */
schneider's avatar
schneider committed
82
83
	while (RTC_SquareWave(MXC_RTC, SQUARE_WAVE_ENABLED, F_32KHZ, NULL) ==
	       E_BUSY)
84
85
		;

schneider's avatar
schneider committed
86
87
88
89
90
	/*
	 * RNG
	 */
	rng_init();

91
	/* If we don't have a valid time yet, set it to 2019-01-01 */
92
	if (RTC_GetSecond() < 1546300800L) {
93
94
95
		epic_rtc_set_milliseconds(1546300800UL * 1000);
	}

96
97
98
	/*
	 * SPI for ECG
	 */
99
100
101
102
103
	const sys_cfg_spi_t spi17y_master_cfg = { .map    = MAP_A,
						  .ss0    = Enable,
						  .ss1    = Disable,
						  .ss2    = Disable,
						  .num_io = 2 };
104
105
106
107
108
109
110

	if (SPI_Init(SPI0, 0, SPI_SPEED, spi17y_master_cfg) != 0) {
		LOG_ERR("init", "Error configuring SPI");
		while (1)
			;
	}

111
112
113
114
115
	/*
	 * The bootloader has already initialized the display, so we only need
	 * to do the bare minimum here (mostly the gfx datastructures).
	 */
	display_init_slim();
116
117
118
119
120

	/*
	 * RGB LEDs
	 */
	leds_init();
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

#ifdef CARD10_DEBUG_CORE1
	/*
	 * The SAO pins can be reconfigured for SWCLK2 and SWDIO2 which allows
	 * debugging core 1.  This feature can optionally be enabled at
	 * compile-time.
	 */
	LOG_WARN("init", "Core 1 Debugger Mode");
	static const gpio_cfg_t swclk = {
		PORT_0,
		PIN_7,
		GPIO_FUNC_ALT3,
		GPIO_PAD_NONE,
	};
	static const gpio_cfg_t swdio = {
		PORT_0,
		PIN_6,
		GPIO_FUNC_ALT3,
		GPIO_PAD_NONE,
	};

	GPIO_Config(&swclk);
	GPIO_Config(&swdio);
#endif /* CARD10_DEBUG_CORE1 */

	/*
	 * Enable SEV-ON-PEND which is needed for proper working of the FreeRTOS
	 * tickless idle sleep in Epicardium.
	 */
	SCB->SCR |= SCB_SCR_SEVONPEND_Msk;

	/*
	 * USB-Serial
	 */
swym's avatar
swym committed
155
156
	if (epic_usb_cdcacm() < 0) {
		LOG_ERR("startup", "USB-Serial unavailable");
157
158
159
160
161
162
163
164
165
166
	}

	/*
	 * Flash & FatFS
	 */
	fatfs_init();

	/*
	 * API Dispatcher & API Interrupts
	 */
167
	interrupt_init();
168
169
170
171
172
173
174
	api_dispatcher_init();

	/*
	 * Sensor streams
	 */
	stream_init();

175
176
177
178
179
	/*
	 * Hardware/Peripheral Locks
	 */
	hwlock_init();

180
181
182
183
184
	/*
	 * API Dispatcher Mutex
	 */
	dispatcher_mutex_init();

185
186
187
188
189
	/*
	 * MAX30001 mutex init
	 */
	max30001_mutex_init();

190
191
192
193
194
	/*
	 * max86150 mutex init
	 */
	max86150_mutex_init();

195
196
	max86150_shut_down();

197
198
199
	/* Allow user space to trigger interrupts.
	 * Used for BLE, not sure if needed. */
	SCB->CCR |= SCB_CCR_USERSETMPEND_Msk;
200
201
202
203
204
205
206
207
208
209
210
211
212
	return 0;
}

/*
 * hardware_init() is called after the core has been bootstrapped and is meant
 * for less critical initialization steps.  Modules which initialize here should
 * be robust against a l0dable using their API before initialization is done.
 *
 * Ideally, acquire a lock in hardware_early_init() and release it in
 * hardware_init() once initialization is done.
 */
int hardware_init(void)
{
213
214
215
	/* Watchdog clearer software timer */
	watchdog_clearer_init();

216
	/* Light Sensor */
217
	LOG_DEBUG("init", "Starting light sensor ...");
218
219
	epic_light_sensor_run();

220
221
222
223
224
225
226
227
228
229
230
231
	return 0;
}

/*
 * hardware_reset() is called whenever a new l0dable is started.  hardware_reset()
 * should bring all peripherals back into a known initial state.  This does not
 * necessarily mean resetting the peripheral entirely but hardware_reset()
 * should at least bring the API facing part of a peripheral back into the state
 * a fresh booted l0dable expects.
 */
int hardware_reset(void)
{
232
233
234
	/*
	 * API Dispatcher & API Interrupts
	 */
235
	interrupt_init();
236
237
	api_dispatcher_init();

238
239
240
241
242
	/*
	 * close all FDs currently owned by core1
	 */
	fatfs_close_all(EPICARDIUM_COREMASK_1);

243
244
245
246
247
248
249
250
	/* Personal State */
	const int personal_state_is_persistent =
		epic_personal_state_is_persistent();

	if (personal_state_is_persistent == 0) {
		epic_personal_state_set(STATE_NONE, 0);
	}

251
252
253
	/*
	 * LEDs
	 */
254
255
256
257
258
	if (personal_state_is_persistent) {
		epic_leds_clear_all(0, 0, 0);
	} else {
		leds_init();
	}
259
260
261
262
263
264
265
266
	epic_leds_set_rocket(0, 0);
	epic_leds_set_rocket(1, 0);
	epic_leds_set_rocket(2, 0);

	/*
	 * Display
	 */
	display_init_slim();
267
268
269
270
271
272
	epic_disp_backlight(20);

	/*
	 * Vibration Motor
	 */
	epic_vibra_set(false);
273

274
275
276
277
278
	/*
	 * BHI160
	 */
	epic_bhi160_disable_all_sensors();

chris007's avatar
chris007 committed
279
280
281
282
283
	/*
	 * BME680 Sensor
	 */
	epic_bme680_deinit();

schneider's avatar
schneider committed
284
285
	epic_max30001_disable_sensor();

286
287
	epic_max86150_disable_sensor();

288
289
290
	/*
	 * BLE
	 */
291
	epic_ble_set_mode(false, false);
292

293
294
	return 0;
}