summaryrefslogtreecommitdiff
path: root/firmware/test/i2s/i2stest.c
blob: 8a2c598f8a052dc7011ae3e7ad0be59983b4a6d7 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            i2stest.c
 *
 *  Mon Sep  1 20:25:48 CEST 2014
 *  Copyright 2014 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pedal2Metal.
 *
 *  Pedal2Metal 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  Pedal2Metal 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 Pedal2Metal; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include <i2s.h>
#include <led.h>
#include <LPC17xx.h>

#include <cli.h>

#include "../test.h"

#include "../../src/sample.h"
unsigned int size = sizeof(samples) / sizeof(short);

static uint32_t cnt = 0;
static int num = 0;

#if 1
void I2S_IRQHandler (void)  
{
  num = 8 - i2s_get_state_tx_level();

  for(int i = 0; i < num; i++) {
    i2s_write_pcm_16_stereo(samples[cnt % size] * 10,
                            samples[cnt % size] * 10);
    //if((cnt % 24000) == 0) led_toggle();
    //i2s_write_pcm_16_stereo(cnt, cnt);
    //    if(cnt > (1<<16)) cnt = 0;    
    cnt++;
  }

  /*
  uint32_t RxCount = 0;

  if ( LPC_I2S->I2SSTATE & 0x01 )
  {
	RxCount = (LPC_I2S->I2SSTATE >> 8) & 0xFF;
	if ( (RxCount != RXFIFO_EMPTY) && !I2SRXDone )
	{
	  while ( RxCount > 0 )
	  {
		if ( I2SReadLength == BUFSIZE )
		{
		  LPC_I2S->I2SDAI |= ((0x01 << 3) | (0x01 << 4));
		  LPC_I2S->I2SIRQ &= ~(0x01 << 0);	// Disable RX	
		  I2SRXDone = 1;
		  break;
		}
		else
		{
		  I2SRXBuffer[I2SReadLength++] = LPC_I2S->I2SRXFIFO;
		}
		RxCount--;
	  }
	}
  }
  return;
*/
}
#endif

int main (void)
{
  led_init();    
  cli_init();
  /*
  // From: tools/clkcalc 48000 16 2
  int pclkdiv = 8;
  int bitrate = 3;
  int x = 57;
  int y = 59;
  // err = 16.949153 bits/second

  int bitwidth = 16;
  int channels = 2;

  int res = i2s_init(pclkdiv, bitrate, x, y, bitwidth, channels);
  */
  int res = i2s_clkcalc_init(48000, 16, 2);

  if(res) error();

  // BUG? 4pin mode = 0 works for some reason...?
	i2s_set_tx_mode_control(CLK_TX_SRC, 0, 1);

  //  i2s_tx_reset_fifo();
  /*
  // Set up IRQ
  uint32_t *i2sirq = (uint32_t*)0x400A801C;
  // *i2sirq &= ~(0b1111 << 16); // Reset tx irq depth
  int depth = 4;
  *i2sirq |= ((depth & 0b1111) << 16); // Set tx irq depth
  *i2sirq |= 0b1 << 1; // Enable IRQ, pg. 479 table 412

  NVIC_EnableIRQ(I2S_IRQn);  // I2S_IRQn = 27;
  */
  i2s_tx_start();

  cli_printf("loop...\n");

  //  int32_t *fifo = (int32_t*)0x400A8008;
  while(1) {
    while(i2s_get_state_tx_level() > 7) {}
    //    i2s_write_pcm_16_stereo(cnt, cnt);
    // *fifo = cnt;

    //    *fifo = (cnt / 40) | (cnt / 40) << 16;
    if(cnt < size) {
      i2s_write_pcm_16_stereo((samples[cnt] - 485) * 10,
                              (samples[cnt] - 485) * 10);
    } else {
      // repeat last sample
      i2s_write_pcm_16_stereo(0, 0);
    }

    if(cnt > size * 10) cnt = 0;

    //    if(cnt > 64000) cnt = 0;
    cnt++;
  }

}