# pico_firmware.c -rw-r--r-- 1.7 KiB View raw
                                                                                
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
#include <pico/printf.h>
#include "pico/stdlib.h"

#define POWER_PIN 6
#define POWER_KEY_PIN 2
#define BOOTLOADER_KEY_PIN 3

int
main()
{
	// Configure the onboard led
	const uint LED_PIN = PICO_DEFAULT_LED_PIN;
	gpio_init(LED_PIN);
	gpio_set_dir(LED_PIN, GPIO_OUT);

	gpio_init(POWER_PIN);
	gpio_init(POWER_KEY_PIN);
	gpio_init(BOOTLOADER_KEY_PIN);
	gpio_set_pulls(POWER_PIN, false, false);
	gpio_set_pulls(POWER_KEY_PIN, false, false);
	gpio_set_pulls(BOOTLOADER_KEY_PIN, false, false);

	// Power control pin is pushpull
	gpio_set_dir(POWER_PIN, GPIO_OUT);
	gpio_put(POWER_PIN, 0);

	// Key control pins are open drain
	gpio_set_dir(POWER_KEY_PIN, GPIO_IN);
	gpio_set_dir(BOOTLOADER_KEY_PIN, GPIO_IN);

	// Bring up the USB CDC interface
	stdio_init_all();

	// Execute commands received from the USB serial
	// This allows setting the state for 3 pins by sending a char
	// board power supply [p = off, P = on]
	// power key [b = off, B = on]
	// bootloader key [r = off, R = on]
	while (true) {
		int cmd = getchar();
		gpio_put(LED_PIN, 1);
		sleep_ms(50);
		gpio_put(LED_PIN, 0);
		sleep_ms(50);

		switch (cmd) {
			case 'p':
				gpio_put(POWER_PIN, 0);
				break;
			case 'P':
				gpio_put(POWER_PIN, 1);
				break;
			case 'b':
				// Float the pin
				gpio_set_dir(POWER_KEY_PIN, GPIO_IN);
				break;
			case 'B':
				// Ground the pin
				gpio_set_dir(POWER_KEY_PIN, GPIO_OUT);
				gpio_put(POWER_KEY_PIN, 0);
				break;
			case 'r':
				// Float the pin
				gpio_set_dir(BOOTLOADER_KEY_PIN, GPIO_IN);
				break;
			case 'R':
				// Ground the pin
				gpio_set_dir(BOOTLOADER_KEY_PIN, GPIO_OUT);
				gpio_put(BOOTLOADER_KEY_PIN, 0);
				break;
			default:
				printf("Unknown command\n");
				break;
		}
	}
}