2014-07-04 23:49:08 +08:00
|
|
|
#include <stdio.h>
|
2014-09-15 22:40:33 +08:00
|
|
|
#include <string.h>
|
2016-01-10 21:04:55 +08:00
|
|
|
#include <alloc.h>
|
2014-07-04 23:49:08 +08:00
|
|
|
#include <irq.h>
|
|
|
|
#include <uart.h>
|
2015-03-12 05:02:19 +08:00
|
|
|
#include <console.h>
|
2014-07-04 23:49:08 +08:00
|
|
|
#include <system.h>
|
2014-09-15 22:56:23 +08:00
|
|
|
#include <generated/csr.h>
|
2015-04-22 15:01:32 +08:00
|
|
|
#include <hw/flags.h>
|
|
|
|
|
|
|
|
#include <lwip/init.h>
|
|
|
|
#include <lwip/memp.h>
|
|
|
|
#include <lwip/ip4_addr.h>
|
|
|
|
#include <lwip/ip4.h>
|
|
|
|
#include <lwip/netif.h>
|
|
|
|
#include <lwip/sys.h>
|
|
|
|
#include <lwip/tcp.h>
|
|
|
|
#include <lwip/timers.h>
|
2015-06-28 05:02:43 +08:00
|
|
|
#ifdef CSR_ETHMAC_BASE
|
2015-11-04 00:35:03 +08:00
|
|
|
#include <netif/etharp.h>
|
|
|
|
#include <liteethif.h>
|
2015-06-28 05:02:43 +08:00
|
|
|
#else
|
|
|
|
#include <netif/ppp/ppp.h>
|
|
|
|
#include <netif/ppp/pppos.h>
|
|
|
|
#include <lwip/sio.h>
|
2015-04-22 15:01:32 +08:00
|
|
|
#endif
|
2014-07-04 23:49:08 +08:00
|
|
|
|
2015-05-09 17:11:34 +08:00
|
|
|
#include "bridge_ctl.h"
|
|
|
|
#include "kloader.h"
|
2015-05-01 12:34:47 +08:00
|
|
|
#include "flash_storage.h"
|
2015-04-29 12:58:37 +08:00
|
|
|
#include "clock.h"
|
2015-07-28 00:19:07 +08:00
|
|
|
#include "rtiocrg.h"
|
2015-03-12 05:02:19 +08:00
|
|
|
#include "test_mode.h"
|
2015-07-30 18:45:57 +08:00
|
|
|
#include "net_server.h"
|
2015-04-22 01:31:31 +08:00
|
|
|
#include "session.h"
|
2015-12-18 00:13:22 +08:00
|
|
|
#include "analyzer.h"
|
2015-06-03 18:26:19 +08:00
|
|
|
#include "moninj.h"
|
2014-07-24 07:10:49 +08:00
|
|
|
|
2015-04-22 15:01:32 +08:00
|
|
|
u32_t sys_now(void)
|
|
|
|
{
|
2015-04-29 12:58:37 +08:00
|
|
|
return clock_get_ms();
|
2015-04-22 15:01:32 +08:00
|
|
|
}
|
|
|
|
|
2015-06-28 05:02:43 +08:00
|
|
|
u32_t sys_jiffies(void)
|
|
|
|
{
|
|
|
|
return clock_get_ms();
|
|
|
|
}
|
|
|
|
|
2015-04-22 15:01:32 +08:00
|
|
|
static struct netif netif;
|
|
|
|
|
2015-06-28 05:02:43 +08:00
|
|
|
#ifndef CSR_ETHMAC_BASE
|
|
|
|
static ppp_pcb *ppp;
|
|
|
|
#endif
|
|
|
|
|
2015-04-22 15:01:32 +08:00
|
|
|
static void lwip_service(void)
|
|
|
|
{
|
|
|
|
sys_check_timeouts();
|
2015-06-28 05:02:43 +08:00
|
|
|
#ifdef CSR_ETHMAC_BASE
|
2015-04-22 15:01:32 +08:00
|
|
|
if(ethmac_sram_writer_ev_pending_read() & ETHMAC_EV_SRAM_WRITER) {
|
|
|
|
liteeth_input(&netif);
|
|
|
|
ethmac_sram_writer_ev_pending_write(ETHMAC_EV_SRAM_WRITER);
|
|
|
|
}
|
2015-06-28 05:02:43 +08:00
|
|
|
#else
|
|
|
|
if(uart_read_nonblock()) {
|
|
|
|
u8_t c;
|
|
|
|
c = uart_read();
|
|
|
|
pppos_input(ppp, &c, 1);
|
|
|
|
}
|
|
|
|
#endif
|
2015-04-22 15:01:32 +08:00
|
|
|
}
|
|
|
|
|
2015-06-28 05:02:43 +08:00
|
|
|
#ifdef CSR_ETHMAC_BASE
|
2015-05-01 12:34:47 +08:00
|
|
|
unsigned char macadr[6];
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
static int hex2nib(int c)
|
2015-04-23 23:22:40 +08:00
|
|
|
{
|
2015-05-01 12:34:47 +08:00
|
|
|
if((c >= '0') && (c <= '9'))
|
|
|
|
return c - '0';
|
|
|
|
if((c >= 'a') && (c <= 'f'))
|
|
|
|
return c - 'a' + 10;
|
|
|
|
if((c >= 'A') && (c <= 'F'))
|
|
|
|
return c - 'A' + 10;
|
|
|
|
return -1;
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
static void init_macadr(void)
|
2015-04-23 23:22:40 +08:00
|
|
|
{
|
2015-05-01 12:34:47 +08:00
|
|
|
static const unsigned char default_macadr[6] = {0x10, 0xe2, 0xd5, 0x32, 0x50, 0x00};
|
2015-12-04 15:57:39 +08:00
|
|
|
#if (defined CSR_SPIFLASH_BASE && defined CONFIG_SPIFLASH_PAGE_SIZE)
|
2015-05-01 12:34:47 +08:00
|
|
|
char b[32];
|
|
|
|
char fs_macadr[6];
|
|
|
|
int i, r, s;
|
2015-05-02 12:20:20 +08:00
|
|
|
#endif
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
memcpy(macadr, default_macadr, 6);
|
2015-12-04 15:57:39 +08:00
|
|
|
#if (defined CSR_SPIFLASH_BASE && defined CONFIG_SPIFLASH_PAGE_SIZE)
|
2015-05-01 12:34:47 +08:00
|
|
|
r = fs_read("mac", b, sizeof(b) - 1, NULL);
|
|
|
|
if(r <= 0)
|
|
|
|
return;
|
|
|
|
b[r] = 0;
|
|
|
|
for(i=0;i<6;i++) {
|
|
|
|
r = hex2nib(b[3*i]);
|
|
|
|
s = hex2nib(b[3*i + 1]);
|
|
|
|
if((r < 0) || (s < 0))
|
|
|
|
return;
|
|
|
|
fs_macadr[i] = (r << 4) | s;
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
2015-05-01 12:34:47 +08:00
|
|
|
for(i=0;i<5;i++)
|
|
|
|
if(b[3*i + 2] != ':')
|
|
|
|
return;
|
|
|
|
memcpy(macadr, fs_macadr, 6);
|
2015-05-02 12:20:20 +08:00
|
|
|
#endif
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
static void fsip_or_default(struct ip4_addr *d, char *key, int i1, int i2, int i3, int i4)
|
2015-04-23 23:22:40 +08:00
|
|
|
{
|
|
|
|
int r;
|
2015-12-04 15:57:39 +08:00
|
|
|
#if (defined CSR_SPIFLASH_BASE && defined CONFIG_SPIFLASH_PAGE_SIZE)
|
2015-05-01 12:34:47 +08:00
|
|
|
char cp[32];
|
2015-05-02 12:20:20 +08:00
|
|
|
#endif
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
IP4_ADDR(d, i1, i2, i3, i4);
|
2015-12-04 15:57:39 +08:00
|
|
|
#if (defined CSR_SPIFLASH_BASE && defined CONFIG_SPIFLASH_PAGE_SIZE)
|
2015-05-01 12:34:47 +08:00
|
|
|
r = fs_read(key, cp, sizeof(cp) - 1, NULL);
|
|
|
|
if(r <= 0)
|
|
|
|
return;
|
|
|
|
cp[r] = 0;
|
|
|
|
if(!ip4addr_aton(cp, d))
|
|
|
|
return;
|
2015-05-02 12:20:20 +08:00
|
|
|
#endif
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
static void network_init(void)
|
2015-04-23 23:22:40 +08:00
|
|
|
{
|
2015-05-01 12:34:47 +08:00
|
|
|
struct ip4_addr local_ip;
|
|
|
|
struct ip4_addr netmask;
|
|
|
|
struct ip4_addr gateway_ip;
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
init_macadr();
|
2016-02-29 21:35:23 +08:00
|
|
|
fsip_or_default(&local_ip, "ip", 192, 168, 1, 50);
|
2015-05-01 12:34:47 +08:00
|
|
|
fsip_or_default(&netmask, "netmask", 255, 255, 255, 0);
|
2016-02-29 21:35:23 +08:00
|
|
|
fsip_or_default(&gateway_ip, "gateway", 192, 168, 1, 1);
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
lwip_init();
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
netif_add(&netif, &local_ip, &netmask, &gateway_ip, 0, liteeth_init, ethernet_input);
|
|
|
|
netif_set_default(&netif);
|
|
|
|
netif_set_up(&netif);
|
|
|
|
netif_set_link_up(&netif);
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
2015-06-28 05:02:43 +08:00
|
|
|
#else /* CSR_ETHMAC_BASE */
|
|
|
|
|
2016-01-19 12:04:51 +08:00
|
|
|
static int ppp_connected;
|
|
|
|
|
2015-06-28 05:02:43 +08:00
|
|
|
static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx)
|
|
|
|
{
|
2016-01-26 04:38:58 +08:00
|
|
|
if (err_code == PPPERR_NONE) {
|
|
|
|
ppp_connected = 1;
|
|
|
|
return;
|
|
|
|
} else if (err_code == PPPERR_USER) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ppp_connect(pcb, 1);
|
|
|
|
}
|
2015-06-28 05:02:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i=0;i<len;i++)
|
|
|
|
uart_write(data[i]);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void network_init(void)
|
|
|
|
{
|
2016-01-19 12:04:51 +08:00
|
|
|
lwip_init();
|
|
|
|
|
|
|
|
ppp_connected = 0;
|
2015-06-28 05:02:43 +08:00
|
|
|
ppp = pppos_create(&netif, NULL, ppp_status_cb, NULL);
|
|
|
|
ppp_set_auth(ppp, PPPAUTHTYPE_NONE, "", "");
|
|
|
|
ppp_set_default(ppp);
|
|
|
|
ppp_connect(ppp, 0);
|
2016-01-19 12:04:51 +08:00
|
|
|
|
|
|
|
while (!ppp_connected)
|
|
|
|
lwip_service();
|
2015-06-28 05:02:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CSR_ETHMAC_BASE */
|
|
|
|
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-12-17 16:55:39 +08:00
|
|
|
static struct net_server_instance session_inst = {
|
|
|
|
.port = 1381,
|
|
|
|
.start = session_start,
|
|
|
|
.end = session_end,
|
|
|
|
.input = session_input,
|
|
|
|
.poll = session_poll,
|
|
|
|
.ack_consumed = session_ack_consumed,
|
|
|
|
.ack_sent = session_ack_sent
|
|
|
|
};
|
|
|
|
|
2015-12-18 00:13:22 +08:00
|
|
|
#ifdef CSR_RTIO_ANALYZER_BASE
|
|
|
|
static struct net_server_instance analyzer_inst = {
|
|
|
|
.port = 1382,
|
|
|
|
.start = analyzer_start,
|
|
|
|
.end = analyzer_end,
|
|
|
|
.input = analyzer_input,
|
|
|
|
.poll = analyzer_poll,
|
|
|
|
.ack_consumed = analyzer_ack_consumed,
|
|
|
|
.ack_sent = analyzer_ack_sent
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-04-23 23:22:40 +08:00
|
|
|
static void regular_main(void)
|
|
|
|
{
|
2016-01-26 04:38:58 +08:00
|
|
|
puts("Accepting network sessions.");
|
2015-04-23 23:22:40 +08:00
|
|
|
network_init();
|
2015-12-17 16:55:39 +08:00
|
|
|
net_server_init(&session_inst);
|
2015-12-18 00:13:22 +08:00
|
|
|
#ifdef CSR_RTIO_ANALYZER_BASE
|
|
|
|
analyzer_init();
|
|
|
|
net_server_init(&analyzer_inst);
|
|
|
|
#endif
|
2015-06-03 18:26:19 +08:00
|
|
|
moninj_init();
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 13:49:26 +08:00
|
|
|
session_end();
|
2015-04-23 23:22:40 +08:00
|
|
|
while(1) {
|
|
|
|
lwip_service();
|
2015-07-25 16:26:04 +08:00
|
|
|
kloader_service_essential_kmsg();
|
2015-07-30 18:45:57 +08:00
|
|
|
net_server_service();
|
2015-04-23 23:22:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-15 22:56:23 +08:00
|
|
|
static void blink_led(void)
|
|
|
|
{
|
2015-07-08 23:22:43 +08:00
|
|
|
int i;
|
|
|
|
long long int t;
|
2014-09-15 22:56:23 +08:00
|
|
|
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
leds_out_write(1);
|
2015-07-08 23:22:43 +08:00
|
|
|
t = clock_get_ms();
|
|
|
|
while(clock_get_ms() < t + 250);
|
2014-09-15 22:56:23 +08:00
|
|
|
leds_out_write(0);
|
2015-07-08 23:22:43 +08:00
|
|
|
t = clock_get_ms();
|
|
|
|
while(clock_get_ms() < t + 250);
|
2014-09-15 22:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 05:02:19 +08:00
|
|
|
static int check_test_mode(void)
|
|
|
|
{
|
|
|
|
char c;
|
2015-07-08 23:22:43 +08:00
|
|
|
long long int t;
|
2015-03-12 05:02:19 +08:00
|
|
|
|
2015-07-08 23:22:43 +08:00
|
|
|
t = clock_get_ms();
|
|
|
|
while(clock_get_ms() < t + 1000) {
|
2015-03-12 05:02:19 +08:00
|
|
|
if(readchar_nonblock()) {
|
|
|
|
c = readchar();
|
|
|
|
if((c == 't')||(c == 'T'))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-10 21:04:55 +08:00
|
|
|
extern void _fheap, _eheap;
|
|
|
|
|
2014-09-15 22:40:33 +08:00
|
|
|
int main(void)
|
|
|
|
{
|
2014-09-05 12:03:22 +08:00
|
|
|
irq_setmask(0);
|
|
|
|
irq_setie(1);
|
|
|
|
uart_init();
|
2014-09-22 13:18:48 +08:00
|
|
|
|
2015-04-28 00:18:54 +08:00
|
|
|
puts("ARTIQ runtime built "__DATE__" "__TIME__"\n");
|
|
|
|
|
2016-01-10 21:04:55 +08:00
|
|
|
alloc_give(&_fheap, &_eheap - &_fheap);
|
2015-07-08 23:22:43 +08:00
|
|
|
clock_init();
|
2015-07-28 00:19:07 +08:00
|
|
|
rtiocrg_init();
|
2015-04-23 23:22:40 +08:00
|
|
|
puts("Press 't' to enter test mode...");
|
2014-09-15 22:56:23 +08:00
|
|
|
blink_led();
|
2015-03-12 05:02:19 +08:00
|
|
|
|
|
|
|
if(check_test_mode()) {
|
|
|
|
puts("Entering test mode.");
|
|
|
|
test_main();
|
|
|
|
} else {
|
|
|
|
puts("Entering regular mode.");
|
2015-10-31 23:26:09 +08:00
|
|
|
session_startup_kernel();
|
2015-04-22 01:31:31 +08:00
|
|
|
regular_main();
|
2015-03-12 05:02:19 +08:00
|
|
|
}
|
2014-09-05 12:03:22 +08:00
|
|
|
return 0;
|
2014-07-04 23:49:08 +08:00
|
|
|
}
|