2014-07-04 23:49:08 +08:00
|
|
|
#include <stdio.h>
|
2014-09-15 22:40:33 +08:00
|
|
|
#include <string.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 <time.h>
|
|
|
|
#include <generated/csr.h>
|
2015-04-22 15:01:32 +08:00
|
|
|
#include <hw/flags.h>
|
|
|
|
|
|
|
|
#ifdef CSR_ETHMAC_BASE
|
|
|
|
#include <netif/etharp.h>
|
|
|
|
#include <netif/liteethif.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>
|
|
|
|
#endif
|
2014-07-04 23:49:08 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
#include "flash_storage.h"
|
2015-04-29 12:58:37 +08:00
|
|
|
#include "clock.h"
|
2015-03-12 05:02:19 +08:00
|
|
|
#include "test_mode.h"
|
2015-05-01 12:34:47 +08:00
|
|
|
#include "kserver.h"
|
2015-04-22 01:31:31 +08:00
|
|
|
#include "session.h"
|
2014-07-24 07:10:49 +08:00
|
|
|
|
2015-04-22 15:01:32 +08:00
|
|
|
#ifdef CSR_ETHMAC_BASE
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static struct netif netif;
|
|
|
|
|
|
|
|
static void lwip_service(void)
|
|
|
|
{
|
|
|
|
sys_check_timeouts();
|
|
|
|
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-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};
|
|
|
|
char b[32];
|
|
|
|
char fs_macadr[6];
|
|
|
|
int i, r, s;
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
memcpy(macadr, default_macadr, 6);
|
|
|
|
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-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-05-01 12:34:47 +08:00
|
|
|
char cp[32];
|
2015-04-23 23:22:40 +08:00
|
|
|
|
2015-05-01 12:34:47 +08:00
|
|
|
IP4_ADDR(d, i1, i2, i3, i4);
|
2015-04-23 23:22:40 +08:00
|
|
|
|
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-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();
|
|
|
|
fsip_or_default(&local_ip, "ip", 192, 168, 0, 42);
|
|
|
|
fsip_or_default(&netmask, "netmask", 255, 255, 255, 0);
|
|
|
|
fsip_or_default(&gateway_ip, "gateway", 192, 168, 0, 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
|
|
|
}
|
|
|
|
|
|
|
|
static void regular_main(void)
|
|
|
|
{
|
2015-05-01 13:49:26 +08:00
|
|
|
puts("Accepting sessions on Ethernet.");
|
2015-04-29 12:58:37 +08:00
|
|
|
clock_init();
|
2015-04-23 23:22:40 +08:00
|
|
|
network_init();
|
|
|
|
kserver_init();
|
|
|
|
|
2015-05-01 13:49:26 +08:00
|
|
|
session_end();
|
2015-04-23 23:22:40 +08:00
|
|
|
while(1) {
|
|
|
|
lwip_service();
|
|
|
|
kserver_service();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* CSR_ETHMAC_BASE */
|
|
|
|
|
2015-04-28 02:11:58 +08:00
|
|
|
static void reset_serial_session(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
session_end();
|
|
|
|
/* Signal end-of-session inband with zero length packet. */
|
|
|
|
for(i=0;i<4;i++)
|
|
|
|
uart_write(0x5a);
|
|
|
|
for(i=0;i<4;i++)
|
|
|
|
uart_write(0x00);
|
|
|
|
session_start();
|
|
|
|
}
|
|
|
|
|
2015-04-23 23:22:40 +08:00
|
|
|
static void serial_service(void)
|
2014-09-15 22:40:33 +08:00
|
|
|
{
|
2015-04-22 01:31:31 +08:00
|
|
|
char *txdata;
|
|
|
|
int txlen;
|
|
|
|
static char rxdata;
|
|
|
|
static int rxpending;
|
|
|
|
int r, i;
|
|
|
|
|
|
|
|
if(!rxpending && uart_read_nonblock()) {
|
|
|
|
rxdata = uart_read();
|
|
|
|
rxpending = 1;
|
2014-09-15 22:40:33 +08:00
|
|
|
}
|
2015-04-22 01:31:31 +08:00
|
|
|
if(rxpending) {
|
|
|
|
r = session_input(&rxdata, 1);
|
|
|
|
if(r > 0)
|
|
|
|
rxpending = 0;
|
2015-04-28 02:11:58 +08:00
|
|
|
if(r < 0)
|
|
|
|
reset_serial_session();
|
2014-09-15 22:40:33 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 01:31:31 +08:00
|
|
|
session_poll((void **)&txdata, &txlen);
|
|
|
|
if(txlen > 0) {
|
|
|
|
for(i=0;i<txlen;i++)
|
|
|
|
uart_write(txdata[i]);
|
2015-04-23 23:22:40 +08:00
|
|
|
session_ack_data(txlen);
|
|
|
|
session_ack_mem(txlen);
|
2015-04-28 02:11:58 +08:00
|
|
|
} else if(txlen < 0)
|
|
|
|
reset_serial_session();
|
2015-04-05 22:04:50 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 01:31:31 +08:00
|
|
|
static void regular_main(void)
|
2014-07-04 23:49:08 +08:00
|
|
|
{
|
2015-05-01 13:49:26 +08:00
|
|
|
puts("Accepting sessions on serial link.");
|
2015-04-29 12:58:37 +08:00
|
|
|
clock_init();
|
|
|
|
|
2015-04-23 23:22:40 +08:00
|
|
|
/* Open the session for the serial control. */
|
2015-04-22 01:31:31 +08:00
|
|
|
session_start();
|
|
|
|
while(1)
|
2015-04-23 23:22:40 +08:00
|
|
|
serial_service();
|
2014-09-15 22:40:33 +08:00
|
|
|
}
|
|
|
|
|
2015-04-23 23:22:40 +08:00
|
|
|
#endif
|
2015-04-22 01:31:31 +08:00
|
|
|
|
2014-09-15 22:56:23 +08:00
|
|
|
static void blink_led(void)
|
|
|
|
{
|
|
|
|
int i, ev, p;
|
|
|
|
|
|
|
|
p = identifier_frequency_read()/10;
|
|
|
|
time_init();
|
|
|
|
for(i=0;i<3;i++) {
|
|
|
|
leds_out_write(1);
|
|
|
|
while(!elapsed(&ev, p));
|
|
|
|
leds_out_write(0);
|
|
|
|
while(!elapsed(&ev, p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 05:02:19 +08:00
|
|
|
static int check_test_mode(void)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
timer0_en_write(0);
|
|
|
|
timer0_reload_write(0);
|
|
|
|
timer0_load_write(identifier_frequency_read() >> 2);
|
|
|
|
timer0_en_write(1);
|
|
|
|
timer0_update_value_write(1);
|
|
|
|
while(timer0_value_read()) {
|
|
|
|
if(readchar_nonblock()) {
|
|
|
|
c = readchar();
|
|
|
|
if((c == 't')||(c == 'T'))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
timer0_update_value_write(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
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-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
|
|
|
}
|