summaryrefslogtreecommitdiff
path: root/firmware/drivers/wm8523.c
blob: 08b8d97574fd54f757c1013ca8883f4fcef31c7e (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
/* -*- 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 <cli.h>

#include "spi.h"

#define WM8523_WRITE 0
#define WM8523_READ 1

typedef union {
  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;
  } val;
  uint8_t data[3];
} WM8523_transfer_t;

void WM8523_init()
{  
  cli_write("sizeof: %d", sizeof(WM8523_transfer_t));
  spi_init();
}

void WM8523_deinit()
{
  spi_deinit();
}

void WM8523_write(uint8_t reg, uint16_t data)
{
  WM8523_transfer_t t;
  t.val.rw = WM8523_WRITE;
  t.val.reg = reg;
  t.val.data = data;

  WM8523_transfer_t r;
  r.val.data = 0xffff;

  spi_read_write(t.data, r.data, 3);
}

uint16_t WM8523_read(uint8_t reg)
{
  WM8523_transfer_t t;
  t.val.rw = WM8523_READ;
  t.val.reg = reg;
  t.val.data = 0xffff;
  
  WM8523_transfer_t r;

  spi_read_write(t.data, r.data, 3);
  
  return r.data[0] | (r.data[1] << 8);//r.val.data;
}

/**
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()
{
  spi_configure();

  uint16_t id = WM8523_read(0); // Read chip id from reg0.

  cli_write("=%d=", id); // should be 34595 (0x8523)
}