#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;
}
}
}