aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/usrp3/n230/n230_main.c
blob: f9d93bf4a598de4c17e6cbe6ae02731eb23c72cd (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Copyright 2014 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

#include <cron.h>
#include <wb_soft_reg.h>
#include <u3_net_stack.h>
#include <trace.h>
#include "n230_eth_handlers.h"
#include "n230_fw_defs.h"
#include "n230_fw_host_iface.h"
#include "n230_init.h"

//The version hash should come from a cmake build variable
//If it doesn't then the build system does not support the feature
//so just default to 0xFFFFFFFF
#ifndef UHD_VERSION_HASH
#define UHD_VERSION_HASH 0xFFFFFFFF
#endif

//TODO: This is just for initial debugging.
static soft_reg_t g_led_register;

//Shared memory
static n230_host_shared_mem_t g_host_shared_mem;

//Functions
static void n230_handle_claim();

/***********************************************************************
 * Main loop runs all the handlers
 **********************************************************************/
int main(void)
{
    //Initialize host shared mem
    g_host_shared_mem.data.fw_compat_num    = N230_FW_COMPAT_NUM;
    g_host_shared_mem.data.fw_version_hash  = UHD_VERSION_HASH;

    //Main initialization function
    n230_init();

    //Initialize UDP Handlers
    n230_register_udp_fw_comms_handler(&g_host_shared_mem);
    n230_register_udp_prog_framer();
    n230_register_flash_comms_handler();

    initialize_writeonly_soft_reg(&g_led_register, SR_ADDR(WB_SBRB_BASE, SR_ZPU_LEDS));

    uint32_t heart_beat = 0;
    while(true)
    {
        //TODO: This is just for initial debugging. Once the firmware
        //is somewhat stable we should delete this cron job
        if (cron_job_run_due(PER_SECOND_CRON_JOBID)) {
            //Everything in this block runs approx once per second
            if (heart_beat % 10 == 0) {
                UHD_FW_TRACE_FSTR(INFO, "0.1Hz Heartbeat (%u)", heart_beat);
            }
            heart_beat++;
        }

        if (cron_job_run_due(PER_MILLISEC_CRON_JOBID)) {
            //Everything in this block runs approx once per millisecond
            n230_handle_claim();
            n230_handle_sfp_updates(&g_led_register);
        }

        //run the network stack - poll and handle
        u3_net_stack_handle_one();
    }
    return 0;
}

// Watchdog timer for claimer
static void n230_handle_claim()
{
    static uint32_t last_time = 0;
    static size_t timeout = 0;

    if (g_host_shared_mem.data.claim_time == 0) {
        //If time is 0 if the claim was forfeit
        g_host_shared_mem.data.claim_status = 0;
    } else if (last_time != g_host_shared_mem.data.claim_time) {
        //If the time changes, reset timeout
        g_host_shared_mem.data.claim_status = 1;
        timeout = 0;
    } else {
        //Otherwise increment for timeout
        timeout++;
    }

    //Always stash the last seen time
    last_time = g_host_shared_mem.data.claim_time;

    //Timeout logic
    if (timeout > N230_CLAIMER_TIMEOUT_IN_MS) {
        g_host_shared_mem.data.claim_time = 0;
    }
}