/* -*- 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 #include #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) }