aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/microblaze/apps/sd_bounce.c
blob: c1b48f1704e0ce3c633ffd54cf115b43a384d8b7 (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
/*
 * Copyright 2007,2008 Free Software Foundation, Inc.
 *
 * 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/>.
 */

/* 
 * Loopback SERDES to SERDES
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "u2_init.h"
#include "memory_map.h"
#include "spi.h"
#include "hal_io.h"
#include "buffer_pool.h"
#include "pic.h"
#include <stdbool.h>
#include "nonstdio.h"
#include "memset_wa.h"
#include <dbsm.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <clocks.h>



// ----------------------------------------------------------------

#define SERDES_RX_BUF_0		0
#define	SERDES_RX_BUF_1		1

/*
 * ================================================================
 *      configure SD RX double buffering state machine
 * ================================================================
 */

// receive from SERDES
buf_cmd_args_t sd_recv_args = {
  PORT_SERDES,
  0,
  BP_LAST_LINE
};

// send to SERDES
buf_cmd_args_t sd_send_args = {
  PORT_SERDES,
  0,		// starts with packet in line 0
  0,		// filled in from list_line register
};

dbsm_t sd_sm;	// the state machine




// ----------------------------------------------------------------

#if 0
static bool
check_packet(int *buf, int nlines)
{
  bool ok = true;
  int i = 0;
  for (i = 0; i < nlines; i++){
    int expected = ((2*i + 0) << 16) | (2*i+1);
    if (buf[i] != expected){
      ok = false;
      printf("buf[%d] = 0x%x  expected = 0x%x\n", i, buf[i], expected);
    }
  }
  return ok;
}

static void
zero_buffer(int bufno)
{
  memset_wa(buffer_ram(bufno), 0, BP_NLINES * 4);
}
#endif


bool
sd_rx_inspector(dbsm_t *sm, int buf_this)
{
  hal_toggle_leds(0x2);

#if 0
  int last_line = buffer_pool_status->last_line[buf_this];
  bool ok = check_packet(buffer_ram(buf_this), last_line);
  static int good = 0;
  static int bad = 0;

  if (ok)
    good++;
  else
    bad++;

  if(good+bad == 10000) {
    printf("Good %d\tBad %d\n",good,bad);
    good = 0;
    bad = 0;
  }
#endif

  return false;
}


inline static void
buffer_irq_handler(void)
{
  uint32_t  status = buffer_pool_status->status;
  dbsm_process_status(&sd_sm, status);
}


int
main(void)
{
  u2_init();

  putstr("\nsd_bounce\n");

  // Get our clock from the mimo interface
  clocks_mimo_config(MC_WE_LOCK_TO_MIMO);

  dbsm_init(&sd_sm, SERDES_RX_BUF_0,
	    &sd_recv_args, &sd_send_args,
	    sd_rx_inspector);

  // kick off the state machine
  dbsm_start(&sd_sm);

  while(1){
    buffer_irq_handler();
  }
}