summaryrefslogtreecommitdiff
path: root/firmware/drivers/wm8523.c
blob: 36d293a8e8e69cbeb9915979a3dc379db6fb258c (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            wm8523.c
 *
 *  Tue Jun  4 20:47:15 CEST 2013
 *  Copyright 2013 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 "wm8523.h"

#include <string.h>
#include <lpc17xx_spi.h>

#define WM8523_WRITE 0
#define WM8523_READ 1

static SPI_CFG_Type spi_cfg;
static LPC_SPI_TypeDef spi;

typedef struct __attribute__ ((packed)) {
  uint8_t rw:1; ///< See WM8523_WRITE and WM8523_READ
  uint8_t reg:7; ///< See page 35 in the WM8523 manual.
  uint16_t data;
} WM8523_transfer_t;

void WM8523_Init()
{
  // 16 bit data tranfers
	spi_cfg.Databit = SPI_DATABIT_16;

	// WM8523 samples bits on rising edges. Mode 0 or 3 will work
	// http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase

	// Mode 0
	spi_cfg.CPHA = SPI_CPHA_FIRST; // 0
	spi_cfg.CPOL = SPI_CPOL_HI; // 0
	/*
	// Mode 3
	spi_cfg.CPHA = SPI_CPHA_SECOND; // 1
	spi_cfg.CPOL = SPI_CPOL_LO; // 1
	*/

	// We are the master
	spi_cfg.Mode = SPI_MASTER_MODE;

	// We run on a little endian mcu.
	spi_cfg.DataOrder = SPI_DATA_LSB_FIRST;

	// Se WM8523 manual page 14. Min clock pulse width 40ns ~= 25MHz
	spi_cfg.ClockRate = 1000000000; // 1MHz

	SPI_Init(&spi, &spi_cfg);
}

static volatile int spi_done = 0;
void spi_mark_as_done()
{
  spi_done = 1;
}

static void send(void *data, size_t size)
{
  SPI_DATA_SETUP_Type dst;
  memset(&dst, 0, sizeof(dst));
  dst.rx_data = data;
  dst.length = size;
  dst.callback = spi_mark_as_done;
  
  spi_done = 0;
  SPI_ReadWrite(&spi, &dst, SPI_TRANSFER_INTERRUPT);

  while(!spi_done) {}
}

static void receive(void *data, size_t size)
{
  // http://anhnvnguyen.blogspot.dk/2010/04/lpc17xx-gpio-basic_05.html
  // TODO: set SSEL0 low
  // PINSEL3[13:12] = p1.21 (SSEL0)
  LPC_PINCON->PINSEL3 &= ~((1<<13)|(1<<12)); // set to 00

  SPI_DATA_SETUP_Type dst;
  memset(&dst, 0, sizeof(dst));
  dst.tx_data = data;
  dst.length = size;
  dst.callback = spi_mark_as_done;
  
  spi_done = 0;
  SPI_ReadWrite(&spi, &dst, SPI_TRANSFER_INTERRUPT);
  
  while(!spi_done) {}

  // TODO: set SSEL0 high
}

/**
Volume update registers R06h and R07h are unavailable in SPI control mode.
To use volume update in software control mode, I2C mode must be used.
*/
void WM8523_Configure()
{
  WM8523_transfer_t transfer;
  transfer.rw = WM8523_READ;
  transfer.reg = 0; // R0 read version : 0x8523
  transfer.data = 0;
  //send(&transfer, sizeof(transfer));
  receive(&transfer, sizeof(transfer));
}