Skip to content

Firmware Code Book Blue

andy diller edited this page Jun 25, 2024 · 3 revisions

FujiNet Firmware Diving

We will start at the beginning with

main.cpp

Here's a summary of its key components:

  1. Platform-specific conditional compilation:

    • Differentiates between ESP platform (likely ESP32) and other platforms (Windows, Linux, macOS).
  2. Includes various headers for different functionalities:

    • Debug, bus, device, WiFi, filesystem, HTTP service, Bluetooth (if supported), and more.
  3. Main setup function:

    • Initializes hardware, loads configuration, sets up file systems.
    • Creates and configures various devices based on the target platform (e.g., printers, modems).
    • Sets up different bus systems (SIO, IEC, ComLynx, RS232, etc.) depending on the build target.
  4. Main service loop:

    • Handles continuous operations like servicing the active bus system.
    • Manages WiFi or Bluetooth connections.
    • On non-ESP platforms, it also handles HTTP service and task management.
  5. Platform-specific main entry points:

    • For ESP platform: app_main() function that creates a high-priority task for the main loop.
    • For other platforms: Standard main() function that calls setup and enters the service loop.
  6. Shutdown and reboot handling:

    • Includes functions to handle graceful shutdown and reboot scenarios.

The code is designed to be highly modular and adaptable to different hardware platforms and configurations, with extensive use of conditional compilation to include or exclude features based on the target system.

Main Loop

The service loop is the heart of the FujiNet system, continuously running to handle various tasks. Let's break down its key components and actions:

  1. WiFi/Bluetooth Initialization:

    • If Bluetooth is enabled in the configuration, it starts the Bluetooth manager.
    • If WiFi is enabled, it initiates the WiFi connection.
  2. Main Loop: The loop runs continuously, performing these tasks:

    a. Bluetooth Service (if active):

    • If Bluetooth is active, it calls fnBtManager.service() to handle Bluetooth operations.

    b. Bus Service:

    • Calls SYSTEM_BUS.service() to handle operations on the main system bus.
    • This likely involves processing commands and data transfers for connected devices.

    c. Platform-specific Operations:

    • On ESP platforms:
      • Yields to other tasks using taskYIELD() to allow multitasking.
    • On non-ESP platforms:
      • Services the HTTP server (fnHTTPD.service()).
      • Manages tasks with taskMgr.service().
      • Checks for deferred reboot requests.

    d. Reboot Handling (non-ESP platforms):

    • If a reboot is requested:
      • Stops the web server.
      • Initiates a system reboot with a special exit code.
  3. Loop Control:

    • On ESP platforms, the loop runs indefinitely.
    • On other platforms, it continues until a shutdown is requested.
  4. Resource Management:

    • Includes debug prints for heap memory usage on ESP platforms.
  5. Multitasking (ESP platform):

    • The service loop runs as a high-priority task on a specific CPU core.
    • This allows for efficient multitasking, especially important for real-time operations.

The service loop essentially acts as the central coordinator, ensuring that all components of the FujiNet system (communication buses, network services, device management) are continuously serviced. It's designed to be responsive to various events and requests while maintaining the overall system's functionality across different hardware platforms.

Clone this wiki locally