glider/fw/fw.c

151 lines
3.9 KiB
C
Raw Normal View History

2022-09-04 19:16:18 +00:00
//
// Copyright 2022 Wenting Zhang <zephray@outlook.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/binary_info.h"
2024-01-16 04:03:25 +00:00
#include "pico/sleep.h"
2022-09-04 19:16:18 +00:00
#include "hardware/i2c.h"
2023-12-10 19:01:42 +00:00
#include "config.h"
2022-09-04 19:16:18 +00:00
#include "utils.h"
#include "tcpm_driver.h"
#include "usb_pd.h"
#include "ptn3460.h"
2024-04-30 21:29:38 +00:00
#include "adv7611.h"
#include "power.h"
#include "fpga.h"
2024-01-16 04:03:25 +00:00
#include "edid.h"
2024-04-28 01:32:58 +00:00
#include "button.h"
2024-01-24 04:50:18 +00:00
#include "caster.h"
2022-09-04 19:16:18 +00:00
int main()
{
stdio_init_all();
//sleep_ms(2000);
printf("\n");
printf("Glider\n");
2022-09-04 19:16:18 +00:00
2024-04-30 21:29:38 +00:00
#ifdef INPUT_ADV7611
// Need to release ADV from reset, otherwise it would hold the I2C bus
adv7611_early_init();
#endif
2024-04-28 01:32:58 +00:00
// Initialize I2C for TCPC/PTN3460/ADV7611 use
i2c_init(i2c1, 100*1000);
gpio_set_function(2, GPIO_FUNC_I2C);
gpio_set_function(3, GPIO_FUNC_I2C);
gpio_pull_up(2);
gpio_pull_up(3);
2024-04-30 21:29:38 +00:00
#ifdef HAS_TYPEC
2024-04-28 01:32:58 +00:00
int result = tcpm_init(0);
if (result)
fatal("Failed to initialize TCPC\n");
int cc1, cc2;
tcpc_config[0].drv->get_cc(0, &cc1, &cc2);
printf("CC status %d %d\n", cc1, cc2);
2024-04-30 21:29:38 +00:00
#endif
2024-04-28 01:32:58 +00:00
2024-01-16 04:03:25 +00:00
power_init();
2024-04-28 01:32:58 +00:00
pd_init(0);
sleep_ms(50);
2024-04-30 21:29:38 +00:00
edid_init();
#ifdef INPUT_PTN3460
ptn3460_init();
#endif
#ifdef INPUT_ADV7611
adv7611_init();
#endif
2024-01-16 04:03:25 +00:00
power_enable(true);
//sleep_run_from_xosc();
//sleep_goto_dormant_until_edge_high(8);
// https://ghubcoder.github.io/posts/awaking-the-pico/
2024-04-30 21:29:38 +00:00
//fpga_init();
2024-01-24 04:50:18 +00:00
//caster_init();
2024-04-30 21:29:38 +00:00
//button_init();
2024-01-24 04:50:18 +00:00
int mode_max = 6;
int mode = 1;
UPDATE_MODE modes[6] = {
UM_FAST_MONO_NO_DITHER,
UM_FAST_MONO_BAYER,
UM_FAST_MONO_BLUE_NOISE,
UM_FAST_GREY,
UM_AUTO_LUT_NO_DITHER,
UM_AUTO_LUT_ERROR_DIFFUSION
};
2022-09-04 19:16:18 +00:00
extern int dp_enabled;
bool hpd_sent = false;
bool dp_valid = false;
2022-09-04 19:16:18 +00:00
while (1) {
2024-04-30 21:29:38 +00:00
#ifdef HAS_TYPEC
2022-09-04 19:16:18 +00:00
// TODO: Implement interrupt
fusb302_tcpc_alert(0);
pd_run_state_machine(0);
if (dp_enabled && !hpd_sent && !pd_is_vdm_busy(0)) {
printf("DP enabled\n");
pd_send_hpd(0, hpd_high);
hpd_sent = true;
}
if (dp_valid != ptn3460_is_valid()) {
dp_valid = ptn3460_is_valid();
printf(dp_valid ? "Input is valid\n" : "Input is invalid\n");
}
2024-04-30 21:29:38 +00:00
#endif
2024-04-28 01:32:58 +00:00
// Key press logic
uint32_t keys = button_scan();
if (keys & 0x1) {
// First key short press
}
if (keys & 0x2) {
// First key long press
// Clear screen
caster_redraw(0,0,1600,1200);
}
if (keys & 0x4) {
// Second key short press
}
if (keys & 0x8) {
// Second key long press
// Switch mode
mode++;
if (mode >= mode_max) mode = 0;
caster_setmode(0,0,1600,1200,modes[mode]);
}
sleep_ms(1);
2022-09-04 19:16:18 +00:00
}
return 0;
}