| // Copyright 2023 The Pigweed Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| |
| #include <bitset> |
| #include <cstdint> |
| |
| #include <Arduino.h> |
| #include <SPI.h> |
| |
| #include "pw_log/log.h" |
| #include "pw_result/result.h" |
| #include "pw_string/format.h" |
| |
| #include "gonk/fpga_control.h" |
| #include "gonk/pin_config.h" |
| #include "gonk/spi_flash.h" |
| |
| using gonk::fpga_control::FpgaControl; |
| using gonk::pin_config::FlashCS; |
| using gonk::pin_config::ICE40Done; |
| using gonk::pin_config::PinConfig; |
| using gonk::pin_config::StatusLed; |
| using gonk::spi_flash::SpiFlash; |
| |
| namespace { |
| |
| PinConfig pin_config = PinConfig(); |
| FpgaControl fpga_control = FpgaControl(&pin_config); |
| SpiFlash spi_flash = SpiFlash(FlashCS, /*baudrate=*/1000000); |
| |
| void ice40_done_rising_isr() { PW_LOG_DEBUG("ICE40 Done: HIGH"); } |
| |
| void (*current_task)(); |
| |
| } // namespace |
| |
| void IdleTask(); |
| void FpgaConfigTask(); |
| |
| void IdleTask() { |
| static uint32_t last_update = millis(); |
| static uint32_t this_update = millis(); |
| static uint16_t update_count = 0; |
| |
| this_update = millis(); |
| |
| // Check for serial input. |
| if (Serial.available()) { |
| int data_in = Serial.read(); |
| // Press enter to restart the FPGA config. |
| if (data_in == '\n') { |
| PW_LOG_INFO("Restarting FPGA Config."); |
| current_task = &FpgaConfigTask; |
| } |
| } |
| |
| // Output an idle state heartbeat message each second. |
| if (this_update > last_update + 1000) { |
| PW_LOG_INFO("Idle update: %d", update_count); |
| |
| last_update = this_update; |
| update_count = (update_count + 1) % UINT16_MAX; |
| |
| // Toggle status LED each loop. |
| if (update_count % 2 == 0) { |
| digitalWrite(StatusLed, HIGH); |
| } else { |
| digitalWrite(StatusLed, LOW); |
| } |
| } |
| } |
| |
| void FpgaConfigTask() { |
| auto result = fpga_control.StartConfig(); |
| if (result.ok()) { |
| current_task = &IdleTask; |
| } else { |
| PW_LOG_INFO("Restarting FPGA Config."); |
| current_task = &FpgaConfigTask; |
| } |
| } |
| |
| int main() { |
| // Debug interrupt to watch when ICE40Done goes high. |
| attachInterrupt(/*pin=*/ICE40Done, /*callback=*/&ice40_done_rising_isr, |
| /*mode=*/HIGH); |
| |
| pin_config.Init(); |
| pin_config.InitFpgaPins(); |
| delay(500); |
| |
| current_task = &FpgaConfigTask; |
| |
| // Start the task loop. |
| while (true) { |
| current_task(); |
| } |
| |
| PW_UNREACHABLE; |
| return 0; |
| } |