summaryrefslogtreecommitdiffstats
path: root/app/steth.c
blob: d7eea7e56e2e7f55827ddb54e18921c02b7a27a4 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <project.h>


#define PHY PHY0

#define TXEN    GPIO11
#define TXEN_PORT GPIOB

#define TXD0    GPIO12
#define TXD0_PORT GPIOB

#define TXD1    GPIO13
#define TXD1_PORT GPIOB

#define RXD0    GPIO4
#define RXD0_PORT GPIOC

#define RXD1    GPIO5
#define RXD1_PORT GPIOC

#define CRS_DV    GPIO7
#define CRS_DV_PORT GPIOA

#define MDIO    GPIO2
#define MDIO_PORT GPIOA

#define MDC   GPIO1
#define MDC_PORT  GPIOC

#define REF_CLK   GPIO1
#define REF_CLK_PORT  GPIOA

#define NRST    GPIO2
#define NRST_PORT GPIOE


#define DESC_SZ ETH_DES_EXT_SIZE
#define FRAME_SZ 1516

#define TX_BUFS 4
#define RX_BUFS 4

#define ETH_BUF_LEN ((TX_BUFS+ RX_BUFS) * (DESC_SZ+FRAME_SZ))


static int running;

static uint8_t __attribute__ ((aligned (4))) eth_buf[ETH_BUF_LEN];

static uint8_t sa[ETHARP_HWADDR_LEN] = { 0xc0, 0xf1, 0xee, 0xc0, 0xff, 0xdd };

extern uint32_t TxBD;
extern uint32_t RxBD;



bool
phy_link_an_done (uint8_t phy)
{
  return eth_smi_read (PHY, PHY_REG_BSR) & PHY_REG_BSR_ANDONE;
}



static err_t
steth_tx (struct netif *netif, struct pbuf *p)
{
#if 0
  static uint8_t tx_buf[FRAME_SZ];
  uint32_t len, left;

  len = 0;
  left = FRAME_SZ;

  for (; p != NULL; p = p->next) {
    uint32_t sz = (left > p->len) ? p->len : left;
    memcpy (&tx_buf[len], (u8_t *) p->payload, sz);
    len += sz;
    left -= sz;
  }

  //      usart6_write ("X", 1, 0);

  //  hexdump(tx_buf,len);

  while (!eth_tx (tx_buf, len));

#else

  if (p->next) return ERR_IF;

  while (!eth_tx (p->payload, p->len));

#endif

#if 0
  {
    static int tx_cnt = 0;
    printf ("TX %d\r\n", tx_cnt++);
  }
#endif

  return ERR_OK;
}

static err_t
steth_rx (void)
{
  //err_t err;
  struct pbuf *p;
  uint32_t len;

#if 0
  struct pbuf *q;
  static uint8_t rx_buf[FRAME_SZ];

  len = 0;

  if (!eth_rx (rx_buf, &len, sizeof (rx_buf))) {
    usart6_write ("?", 1, 0);
    return ERR_OK;
  }

  /* We allocate a pbuf chain of pbufs from the Lwip buffer pool */
  p = pbuf_alloc (PBUF_RAW, len, PBUF_POOL);

  if (!p) {
    usart6_write ("!", 1, 0);
    return ERR_MEM;
  }

  len = 0;

  for (q = p; q != NULL; q = q->next) {
    memcpy ((u8_t *) q->payload, (u8_t *) & rx_buf[len], q->len);
    len += q->len;
  }

#else
  p = pbuf_alloc (PBUF_RAW, MTU, PBUF_POOL);

  if (!p) return ERR_MEM;

  len = 0;

  if (!eth_rx (p->payload, &len, MTU)) {
    pbuf_free (p);
    return ERR_IF;
  }

  pbuf_realloc (p, len);
#endif



  //      usart6_write (".", 1, 0);

#if 0
  {
    static int rx_cnt = 0;
    printf ("RX %d\r\n", rx_cnt++);
  }
#endif


  return if0.input (p, &if0);
}



static void
steth_nis (void)
{
  if (eth_irq_ack_pending (ETH_DMASR_RS))
    steth_rx();
}

void
eth_isr (void)
{
  if (eth_irq_ack_pending (ETH_DMASR_NIS))
    steth_nis();
}

void
steth_isr (void)
{
#if 1
    //printf ("eth\r\n");

  if (eth_irq_ack_pending (ETH_DMASR_NIS))
    steth_nis();

#endif

}


err_t
steth_lwip_init (struct netif *netif)
{
  LWIP_ASSERT ("netif != NULL", (netif != NULL));

#if LWIP_NETIF_HOSTNAME
  /* Initialize interface hostname */
  netif->hostname = "lwip";
#endif /* LWIP_NETIF_HOSTNAME */

  netif->name[0] = 's';
  netif->name[1] = 't';
  /* We directly use etharp_output() here to save a function call.
   * You can instead declare your own function an call etharp_output()
   * from it if you have to do some checks before sending (e.g. if link
   * is available...) */
  netif->output = etharp_output;
  netif->linkoutput = steth_tx;

  /* set MAC hardware address length */
  netif->hwaddr_len = ETHARP_HWADDR_LEN;

  /* set MAC hardware address */
  memcpy (netif->hwaddr, sa, ETHARP_HWADDR_LEN);

  /* maximum transfer unit */
  netif->mtu = 1500;

  /* device capabilities */
  netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;

  return ERR_OK;
}


void
steth_init (void)
{
unsigned i;

  MAP_AF (TXEN, GPIO_AF11);
  MAP_AF (TXD0, GPIO_AF11);
  MAP_AF (TXD1, GPIO_AF11);

  MAP_AF (RXD0, GPIO_AF11);
  MAP_AF (RXD1, GPIO_AF11);

  MAP_AF (CRS_DV, GPIO_AF11);

  MAP_INPUT_PU(MDC);

  MAP_AF_PU (MDIO, GPIO_AF11);
  MAP_AF_PU (MDC, GPIO_AF11);

  MAP_AF (REF_CLK, GPIO_AF11);

  MAP_OUTPUT_PP (NRST);


  CLEAR(NRST);
  delay_ms(1);
  SET (NRST);

TRACE;

  /* The switch to RMII has be done with steth under reset, with no clock */

  rcc_periph_clock_enable (RCC_APB2ENR_SYSCFGEN);

  RCC_AHB1RSTR |= RCC_AHB1RSTR_ETHMACRST; /*Assert RESET */

#if 0
  rcc_periph_clock_disable (RCC_ETHMACPTP);
  rcc_periph_clock_disable (RCC_ETHMACRX);
  rcc_periph_clock_disable (RCC_ETHMACTX);
  rcc_periph_clock_disable (RCC_ETHMAC);
#endif

  delay_us (1);

#ifndef SYSCFG_PMC_MII_RMII_SEL
#define SYSCFG_PMC_MII_RMII_SEL (1UL << 23)
#endif

  SYSCFG_PMC |= SYSCFG_PMC_MII_RMII_SEL;

TRACE;
  RCC_AHB1RSTR &= ~RCC_AHB1RSTR_ETHMACRST; /*De-sssert RESET */


  rcc_periph_clock_enable (RCC_ETHMAC);
  rcc_periph_clock_enable (RCC_ETHMACTX);
  rcc_periph_clock_enable (RCC_ETHMACRX);
  rcc_periph_clock_enable (RCC_ETHMACPTP);

  delay_ms (10);
  ETH_DMABMR|=1;
  delay_ms (10);

TRACE;
  eth_desc_init (eth_buf, TX_BUFS, RX_BUFS, FRAME_SZ, FRAME_SZ, 1);
TRACE;

  for (i=0;i<32;++i) {
	printf("phy:%x %x\r\n",i,eth_smi_read(PHY,i));
  }

  /*MDC = HCLK / 102 (0b100) => 1.6MHz */

  eth_init (PHY, 0x4);
TRACE;
//  eth_init (PHY, 0x14);
TRACE;
  eth_enable_checksum_offload();
TRACE;

  eth_set_mac (sa);



  printf ("Waiting for link\r\n");

  while (!phy_link_isup (PHY));

  phy_autoneg_enable (PHY);
  printf ("Waiting for autonegociation\r\n");

  while (!phy_link_an_done (PHY));

  switch (phy_link_status (PHY)) {
  case LINK_HD_10M:
	TRACE;
  case LINK_FD_10M:
	TRACE;
    ETH_MACCR &= ~ETH_MACCR_FES;
    break;

  default:
	TRACE;
    ;
  }

	printf("phy link status %x\r\n",phy_link_status(PHY));

  switch (phy_link_status (PHY)) {
  case LINK_HD_10M:
	TRACE;
  case LINK_HD_100M:
	TRACE;
    ETH_MACCR &= ~ETH_MACCR_DM;
    ETH_MACCR |= ETH_MACCR_ROD;
    break;

  default:
    ;
  }

  ETH_MACCR &= ~ETH_MACCR_RD;



  nvic_disable_irq (NVIC_ETH_IRQ);

  eth_irq_enable (ETH_DMAIER_NISE);
  eth_irq_enable (ETH_DMAIER_RIE);
  eth_irq_enable (ETH_DMASR_TS);
  //
  eth_start();


  running++;

  printf ("Running\r\n");

}


void
steth_dispatch (void)
{
#if 1
  uint32_t d, s;
#endif

  if (!running)
    return;

#if 1
  printf ("Net:\r\n");
  printf (" ETH_MACCR:    %08" PRIx32 "\r\n", ETH_MACCR);
#if 0
  printf (" ETH_MACFFR:   %08" PRIx32 "\r\n", ETH_MACFFR);
  printf (" ETH_MACFCR:   %08" PRIx32 "\r\n", ETH_MACFCR);
  printf (" ETH_MACDBGR:  %08" PRIx32 "\r\n", ETH_MACDBGR);
  printf (" ETH_MACSR:    %08" PRIx32 "\r\n", ETH_MACSR);
#endif
  printf (" ETH_DMAOMR:   %08" PRIx32 "\r\n", ETH_DMAOMR);
  printf (" ETH_DMASR:    %08" PRIx32 " ebs=%x tps=%x rps=%x\r\n", ETH_DMASR, 
		(ETH_DMASR >>23) & 7 ,
		(ETH_DMASR >>20) & 7 ,
		(ETH_DMASR >>17) & 7 );
  printf (" ETH_DMAIER:   %08" PRIx32 "\r\n", ETH_DMAIER);
  printf (" ETH_DMACHTDR: %08" PRIx32 "\r\n", ETH_DMACHTDR);
  printf (" ETH_DMACHRDR: %08" PRIx32 "\r\n", ETH_DMACHRDR);
  printf (" ETH_DMAOMR:   %08" PRIx32 "\r\n", ETH_DMAOMR);
  printf (" ETH_DMATDLAR: %08" PRIx32 "\r\n", ETH_DMATDLAR);
  printf (" ETH_DMARDLAR: %08" PRIx32 "\r\n", ETH_DMARDLAR);
  printf (" ETH_DMABMR:   %08" PRIx32 "\r\n", ETH_DMABMR);


  s = d = RxBD;


  do {
    printf ("  %08" PRIx32 ": %08" PRIx32 " %08" PRIx32 " %08" PRIx32 " %08"
            PRIx32 "\r\n", d, ETH_DES0 (d), ETH_DES1 (d), ETH_DES2 (d),
            ETH_DES3 (d));

    d = ETH_DES3 (d);

  } while (d != s);

#endif

{
int i;
for (i=0;i<1000;++i) {
delay_ms(1);
  steth_isr();
}

}
}