aboutsummaryrefslogtreecommitdiff
path: root/main/init.c
blob: 1cfec9ac4843ba8935e0da249c5f996513f3a196 (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
#include <FreeRTOS.h>
#include <task.h>

#include <pico/stdio.h>
#include <pico/cyw43_arch.h>

#include "config.h"
#include "init.h"
#include "tasks.h"
#include "drv/rp2040/mod.h"

static void init_stdio() {
	stdio_init_all();
}

static void init_cyw34() {
	if (cyw43_arch_init_with_country(CFG_NET_COUNTRY))
		panic("cyw43_arch_init_with_country failed\n");
}

static void init_wifi() {
	// enable 'station' mode (connect to an access point instead of acting like one)
	cyw43_arch_enable_sta_mode();

	if (cyw43_arch_wifi_connect_timeout_ms(CFG_NET_SSID, CFG_NET_PASS, CFG_NET_AUTH, CFG_NET_CONN_TIMEOUT))
		panic("cyw43_arch_wifi_connect failed\n");

	printf("connected to Wi-Fi\n");

	// TODO: announce hostname(?)
}

static void init_i2c() {
	gpio_set_function(CFG_SDA_PIN, GPIO_FUNC_I2C);
	gpio_set_function(CFG_SCL_PIN, GPIO_FUNC_I2C);

	pbdrv_setup();
	//printf("i2c setup\n");
}

static void async_init() {
	init_cyw34();
	init_i2c();
#ifndef CFG_NET_DISABLE
	init_wifi();
#endif
	init_tasks();

	// delete self
	vTaskDelete(NULL);
}

void init() {
	// used for debug `printf` and `panic` on errors
	init_stdio();

	// defer other initialization until the task scheduler is running (important)
	xTaskCreate((TaskFunction_t) async_init, "init", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 4, NULL);
}