2013-05-19 23:12:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/audio/vs1053.c
|
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2021-04-10 08:50:57 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2014-08-01 00:36:09 +02:00
|
|
|
|
2020-11-13 11:02:08 +01:00
|
|
|
#include <inttypes.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <stdint.h>
|
2014-08-01 00:36:09 +02:00
|
|
|
#include <stdbool.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <stdio.h>
|
2013-10-27 15:00:16 +01:00
|
|
|
#include <fcntl.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <string.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-06 18:15:01 +02:00
|
|
|
#include <nuttx/signal.h>
|
2017-10-10 16:43:10 +02:00
|
|
|
#include <nuttx/mqueue.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2013-05-19 23:12:28 +02:00
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/fs/ioctl.h>
|
2013-05-21 21:13:05 +02:00
|
|
|
#include <nuttx/audio/audio.h>
|
|
|
|
#include <nuttx/audio/vs1053.h>
|
|
|
|
|
|
|
|
#include "vs1053.h"
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 15:15:32 +02:00
|
|
|
* Pre-processor Definitions
|
2013-05-19 23:12:28 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2013-05-21 21:13:05 +02:00
|
|
|
#ifndef CONFIG_VS1053_SPIMODE
|
|
|
|
# define CONFIG_VS1053_SPIMODE SPIDEV_MODE0
|
|
|
|
#endif
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_VS1053_XTALI
|
|
|
|
# define CONFIG_VS1053_XTALI 12288000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_VS1053_MP3_DECODE_FREQ
|
|
|
|
# define CONFIG_VS1053_MP3_DECODE_FREQ 43000000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_VS1053_MSG_PRIO
|
|
|
|
# define CONFIG_VS1053_MSG_PRIO 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_VS1053_BUFFER_SIZE
|
|
|
|
# define CONFIG_VS1053_BUFFER_SIZE 8192
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_VS1053_NUM_BUFFERS
|
|
|
|
# define CONFIG_VS1053_NUM_BUFFERS 2
|
|
|
|
#endif
|
|
|
|
|
2013-11-10 18:03:19 +01:00
|
|
|
#ifndef CONFIG_VS1053_WORKER_STACKSIZE
|
|
|
|
# define CONFIG_VS1053_WORKER_STACKSIZE 768
|
|
|
|
#endif
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#define VS1053_DUMMY 0xFF
|
|
|
|
#define VS1053_DEFAULT_XTALI 12288000
|
|
|
|
#define VS1053_DATA_FREQ 20000000
|
|
|
|
#define VS1053_RST_USECS 2000
|
|
|
|
|
2013-05-19 23:12:28 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct vs1053_struct_s
|
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
/* We are an audio lower half driver */
|
|
|
|
|
|
|
|
struct audio_lowerhalf_s lower;
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/* Our specific driver data goes here */
|
|
|
|
|
2022-04-04 12:46:23 +02:00
|
|
|
FAR const struct vs1053_lower_s *hw_lower; /* Pointer to the hardware lower functions */
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct spi_dev_s *spi; /* Pointer to the SPI bus */
|
|
|
|
FAR struct ap_buffer_s *apb; /* Pointer to the buffer we are processing */
|
|
|
|
struct dq_queue_s apbq; /* Our queue for enqueued buffers */
|
|
|
|
unsigned long spi_freq; /* Frequency to run the SPI bus at. */
|
|
|
|
unsigned long chip_freq; /* Current chip frequency */
|
2020-12-24 13:53:17 +01:00
|
|
|
struct file mq; /* Message queue for receiving messages */
|
2015-10-10 18:41:00 +02:00
|
|
|
char mqname[16]; /* Our message queue name */
|
|
|
|
pthread_t threadid; /* ID of our thread */
|
2022-09-06 08:18:45 +02:00
|
|
|
mutex_t apbq_lock; /* Audio Pipeline Buffer Queue mutex access */
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
2015-10-10 18:41:00 +02:00
|
|
|
int16_t volume; /* Current volume level */
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
|
2015-10-10 18:41:00 +02:00
|
|
|
int16_t balance; /* Current balance level */
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_BALANCE */
|
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_TONE
|
2015-10-10 18:41:00 +02:00
|
|
|
uint8_t bass; /* Bass level */
|
|
|
|
uint8_t treble; /* Bass level */
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
uint16_t endfillbytes;
|
2015-10-10 18:41:00 +02:00
|
|
|
uint8_t endfillchar; /* Fill char to send when no more data */
|
2014-08-01 00:36:09 +02:00
|
|
|
bool running;
|
|
|
|
bool paused;
|
|
|
|
bool endmode;
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2014-08-01 00:36:09 +02:00
|
|
|
bool cancelmode;
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
2015-10-10 18:41:00 +02:00
|
|
|
bool busy; /* Set true when device reserved */
|
2013-05-19 23:12:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_getcaps(FAR struct audio_lowerhalf_s *lower, int type,
|
|
|
|
FAR struct audio_caps_s *caps);
|
|
|
|
static int vs1053_shutdown(FAR struct audio_lowerhalf_s *lower);
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session,
|
|
|
|
FAR const struct audio_caps_s *caps);
|
|
|
|
static int vs1053_start(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session);
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_stop(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session);
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_pause(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session);
|
|
|
|
static int vs1053_resume(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session);
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void** session);
|
|
|
|
static int vs1053_release(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session);
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR const struct audio_caps_s *caps);
|
|
|
|
static int vs1053_start(FAR struct audio_lowerhalf_s *lower);
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_stop(FAR struct audio_lowerhalf_s *lower);
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_pause(FAR struct audio_lowerhalf_s *lower);
|
|
|
|
static int vs1053_resume(FAR struct audio_lowerhalf_s *lower);
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower);
|
|
|
|
static int vs1053_release(FAR struct audio_lowerhalf_s *lower);
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif /* CONFIG_AUDIO_MULTI_SESION */
|
2024-08-24 19:25:04 +02:00
|
|
|
static int vs1053_enqueuebuffer(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR struct ap_buffer_s *apb);
|
|
|
|
static int vs1053_cancelbuffer(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR struct ap_buffer_s *apb);
|
|
|
|
static int vs1053_ioctl(FAR struct audio_lowerhalf_s *lower, int cmd,
|
|
|
|
unsigned long arg);
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct audio_ops_s g_audioops =
|
|
|
|
{
|
|
|
|
vs1053_getcaps, /* getcaps */
|
|
|
|
vs1053_configure, /* configure */
|
|
|
|
vs1053_shutdown, /* shutdown */
|
|
|
|
vs1053_start, /* start */
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2013-05-19 23:12:28 +02:00
|
|
|
vs1053_stop, /* stop */
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
vs1053_pause, /* pause */
|
|
|
|
vs1053_resume, /* resume */
|
|
|
|
#endif
|
|
|
|
NULL, /* alloc_buffer */
|
|
|
|
NULL, /* free_buffer */
|
2013-05-19 23:12:28 +02:00
|
|
|
vs1053_enqueuebuffer, /* enqueue_buffer */
|
2013-05-21 21:13:05 +02:00
|
|
|
vs1053_cancelbuffer, /* cancel_buffer */
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_ioctl, /* ioctl */
|
|
|
|
NULL, /* read */
|
|
|
|
NULL, /* write */
|
|
|
|
vs1053_reserve, /* reserve */
|
|
|
|
vs1053_release /* release */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Volume control log table. This table is in increments of 2% of
|
|
|
|
* requested volume level and is the register value that should be
|
2020-02-23 09:50:23 +01:00
|
|
|
* programmed to the VS1053 to achieve that volume percentage.
|
2013-10-27 15:00:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
2024-08-24 19:25:04 +02:00
|
|
|
static const uint8_t g_logtable[] =
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
254, 170, 140, 122, 110, /* 0 - 8 */
|
|
|
|
100, 92, 85, 80, 74, /* 10 - 18 */
|
|
|
|
70, 66, 62, 59, 55, /* 20 - 28 */
|
|
|
|
52, 49, 47, 44, 42, /* 30 - 38 */
|
|
|
|
40, 38, 36, 34, 32, /* 40 - 48 */
|
|
|
|
30, 28, 27, 25, 24, /* 50 - 58 */
|
|
|
|
22, 21, 19, 18, 17, /* 60 - 68 */
|
|
|
|
15, 14, 13, 12, 11, /* 70 - 78 */
|
|
|
|
10, 9, 8, 7, 6, /* 80 - 88 */
|
2020-04-21 09:45:24 +02:00
|
|
|
5, 4, 3, 2, 1, /* 90 - 98 */
|
|
|
|
0 /* 100 */
|
2013-05-19 23:12:28 +02:00
|
|
|
};
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
2013-05-21 21:13:05 +02:00
|
|
|
* Name: vs1053_spi_lock
|
2020-04-21 09:45:24 +02:00
|
|
|
****************************************************************************/
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
static void vs1053_spi_lock(FAR struct spi_dev_s *dev,
|
|
|
|
unsigned long freq_mhz)
|
2013-05-21 21:13:05 +02:00
|
|
|
{
|
2020-02-23 09:50:23 +01:00
|
|
|
/* On SPI buses where there are multiple devices, it will be necessary to
|
|
|
|
* lock SPI to have exclusive access to the buses for a sequence of
|
2013-05-21 21:13:05 +02:00
|
|
|
* transfers. The bus should be locked before the chip is selected.
|
|
|
|
*
|
2020-04-21 09:45:24 +02:00
|
|
|
* This is a blocking call and will not return until we have exclusive
|
|
|
|
* access to the SPI bus.
|
|
|
|
* We will retain that exclusive access until the bus is unlocked.
|
2013-05-21 21:13:05 +02:00
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, true);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/* After locking the SPI bus, the we also need call the setfrequency,
|
|
|
|
* setbits, and setmode methods to make sure that the SPI is properly
|
|
|
|
* configured for the device.
|
|
|
|
* If the SPI bus is being shared, then it may have been left in an
|
|
|
|
* incompatible state.
|
2013-05-21 21:13:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
SPI_SETMODE(dev, CONFIG_VS1053_SPIMODE);
|
|
|
|
SPI_SETBITS(dev, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(dev, 0);
|
|
|
|
SPI_SETFREQUENCY(dev, freq_mhz);
|
2013-05-21 21:13:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
2013-05-21 21:13:05 +02:00
|
|
|
* Name: vs1053_spi_unlock
|
2020-04-21 09:45:24 +02:00
|
|
|
****************************************************************************/
|
2013-05-21 21:13:05 +02:00
|
|
|
|
|
|
|
static inline void vs1053_spi_unlock(FAR struct spi_dev_s *dev)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(dev, false);
|
2013-05-21 21:13:05 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
2013-05-21 21:13:05 +02:00
|
|
|
* Name: vs1053_readreg - Read the specified 16-bit register from the
|
|
|
|
* VS1053 device. Caller must hold the SPI lock.
|
2020-04-21 09:45:24 +02:00
|
|
|
****************************************************************************/
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
static uint16_t vs1053_readreg(FAR struct vs1053_struct_s *dev, uint8_t reg)
|
2013-05-21 21:13:05 +02:00
|
|
|
{
|
|
|
|
uint16_t ret;
|
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
|
|
|
|
|
|
|
/* Select the AUDIO_CTRL device on the SPI bus */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), true);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
|
|
|
/* Send the WRITE command followed by the address */
|
|
|
|
|
|
|
|
SPI_SEND(spi, VS1053_OPCODE_READ);
|
|
|
|
SPI_SEND(spi, reg);
|
|
|
|
|
|
|
|
/* Now read the 16-bit value */
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
ret = SPI_SEND(spi, VS1053_DUMMY) << 8;
|
|
|
|
ret |= SPI_SEND(spi, VS1053_DUMMY);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
|
|
|
/* Deselect the CODEC */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), false);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
2013-10-27 15:00:16 +01:00
|
|
|
* Name: vs1053_writereg - Write the specified 16-bit register to the
|
|
|
|
* VS1053 device. Caller must hold the SPI lock.
|
2020-04-21 09:45:24 +02:00
|
|
|
****************************************************************************/
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
static void vs1053_writereg(FAR struct vs1053_struct_s *dev,
|
2024-08-24 19:25:04 +02:00
|
|
|
uint8_t reg, uint16_t val)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Select the AUDIO_CTRL device on the SPI bus */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Write Reg %d = 0x%0X\n", reg, val);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), true);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Send the WRITE command followed by the address */
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
SPI_SEND(spi, VS1053_OPCODE_WRITE);
|
|
|
|
SPI_SEND(spi, reg);
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Now read the 16-bit value */
|
|
|
|
|
|
|
|
SPI_SEND(spi, val >> 8);
|
2020-04-21 09:45:24 +02:00
|
|
|
SPI_SEND(spi, val & 0xff);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Deselect the CODEC */
|
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_CTRL(0), false);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Short delay after a write for VS1053 processing time */
|
|
|
|
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(10);
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2013-10-27 15:00:16 +01:00
|
|
|
* Name: vs1053_setfrequency
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2013-10-27 15:00:16 +01:00
|
|
|
* Description: Get the audio device capabilities
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
static int vs1053_setfrequency(FAR struct vs1053_struct_s *dev,
|
|
|
|
uint32_t freq)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
double factor;
|
|
|
|
uint16_t reg;
|
|
|
|
uint8_t timeout;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Entry\n");
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Calculate the clock divisor based on the input frequency */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
factor = (double)freq / (double)CONFIG_VS1053_XTALI * 10.0 + 0.5;
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Check the input frequency against bounds */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
if (factor > 50.0)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Frequency too high! Limiting to XTALI * 5\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
factor = 50.0;
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
if (factor < 10.0)
|
|
|
|
{
|
|
|
|
factor = 10.0;
|
|
|
|
}
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Calculate the clock mulit register based on the factor */
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
if ((int)factor == 10)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
reg = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
reg = (((int)factor - 15) / 5) << VS1053_SC_MULT_SHIFT;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the MULT_ADD factor to the max to allow the chip to dynamically
|
|
|
|
* increase the frequency the maximum amount as needed
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
reg |= (VS1053_SC_ADD_XTALI_X20 << VS1053_SC_ADD_SHIFT);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* If we aren't running with a 12.228Mhz input crystal, then we
|
|
|
|
* must tell the chip what the frequency is
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (CONFIG_VS1053_XTALI != VS1053_DEFAULT_XTALI)
|
|
|
|
{
|
|
|
|
/* Calculate register value based on equation: (XTALI - 8000000) / 4000
|
|
|
|
* per the datasheet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
reg |= (CONFIG_VS1053_XTALI - 8000000) / 4000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set the new clock multiplier register */
|
|
|
|
|
|
|
|
vs1053_writereg(dev, VS1053_SCI_CLOCKF, reg);
|
|
|
|
|
|
|
|
/* Wait for DREQ to go active */
|
|
|
|
|
|
|
|
timeout = 200;
|
|
|
|
while (!dev->hw_lower->read_dreq(dev->hw_lower) && timeout)
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(1000);
|
2013-10-27 15:00:16 +01:00
|
|
|
timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update our internal variables */
|
|
|
|
|
|
|
|
dev->chip_freq = freq;
|
|
|
|
dev->spi_freq = freq / 7;
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_logapprox -
|
|
|
|
* Approximate the register value in .5 dB increments
|
|
|
|
* level based on the percentage using a log table since
|
|
|
|
* math libraries aren't available.
|
|
|
|
****************************************************************************/
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
|
|
|
uint8_t vs1053_logapprox(int percent)
|
2013-05-21 21:13:05 +02:00
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Check percentage for bounds */
|
|
|
|
|
|
|
|
if (percent >= 100)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
return (g_logtable[percent >> 1] + g_logtable[(percent + 1) >> 1]) >> 1;
|
2013-05-21 21:13:05 +02:00
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_setvolume -
|
|
|
|
* Set the right and left volume values in the VS1053
|
|
|
|
* device based on the current volume and balance settings.
|
|
|
|
****************************************************************************/
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
|
|
|
static void vs1053_setvolume(FAR struct vs1053_struct_s *dev)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
|
|
|
uint32_t leftlevel;
|
|
|
|
uint32_t rightlevel;
|
2020-04-21 09:45:24 +02:00
|
|
|
uint8_t leftreg;
|
|
|
|
uint8_t rightreg;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Constrain balance */
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
|
|
|
|
if (dev->balance > 1000)
|
|
|
|
{
|
|
|
|
dev->balance = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the left channel volume level */
|
|
|
|
|
|
|
|
if (dev->balance <= 500)
|
|
|
|
{
|
|
|
|
leftlevel = dev->volume;
|
|
|
|
}
|
|
|
|
else if (dev->balance == 1000)
|
|
|
|
{
|
|
|
|
leftlevel = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
leftlevel = dev->volume * (1000 - dev->balance) / 500;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the right channel volume level */
|
|
|
|
|
|
|
|
if (dev->balance >= 500)
|
|
|
|
{
|
|
|
|
rightlevel = dev->volume;
|
|
|
|
}
|
|
|
|
else if (dev->balance == 0)
|
|
|
|
{
|
|
|
|
rightlevel = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rightlevel = dev->volume * dev->balance / 500;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
leftlevel = rightlevel = dev->volume;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Calculate the left and right register values */
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* The register sets the volume in dB which is a logrithmic scale,
|
|
|
|
* so we must use log() to calculate the register value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
leftreg = vs1053_logapprox(leftlevel / 10);
|
|
|
|
rightreg = vs1053_logapprox(rightlevel / 10);
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
/* Lock the SPI bus to get exclusive access to the chip. */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
vs1053_spi_lock(spi, dev->spi_freq);
|
|
|
|
vs1053_writereg(dev, VS1053_SCI_VOL, (leftreg << 8) | rightreg);
|
|
|
|
vs1053_spi_unlock(spi);
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/****************************************************************************
|
2013-10-27 15:00:16 +01:00
|
|
|
* Name: vs1053_setbass - Set the bass and treble level as specified in the
|
|
|
|
* context's bass and treble variables..
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2013-10-27 15:00:16 +01:00
|
|
|
* The level and range are in whole percentage levels (0-100).
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2020-04-21 09:45:24 +02:00
|
|
|
****************************************************************************/
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_TONE
|
|
|
|
static void vs1053_setbass(FAR struct vs1053_struct_s *dev)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
2024-08-24 19:25:04 +02:00
|
|
|
int bass_range;
|
|
|
|
int bass_boost;
|
|
|
|
int treble_range;
|
|
|
|
int treble_boost;
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Calculate range and boost based on level */
|
|
|
|
|
|
|
|
bass_boost = 15 * dev->bass / 100;
|
|
|
|
bass_range = 15;
|
|
|
|
treble_boost = 15 * dev->treble / 100;
|
|
|
|
treble_range = 15;
|
|
|
|
|
|
|
|
/* Lock the SPI bus to get exclsive access to the chip. */
|
|
|
|
|
|
|
|
vs1053_spi_lock(spi, dev->spi_freq);
|
2020-04-21 09:45:24 +02:00
|
|
|
vs1053_writereg(dev, VS1053_SCI_BASS,
|
2024-08-24 19:25:04 +02:00
|
|
|
(treble_boost << 12) | (treble_range << 8) |
|
|
|
|
(bass_boost << 4) | bass_range);
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(spi);
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_TONE */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2013-10-27 15:00:16 +01:00
|
|
|
* Name: vs1053_getcaps
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
2013-10-27 15:00:16 +01:00
|
|
|
* Description: Get the audio device capabilities
|
2013-05-19 23:12:28 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
static int vs1053_getcaps(FAR struct audio_lowerhalf_s *lower, int type,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct audio_caps_s *caps)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Entry\n");
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Validate the structure */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
DEBUGASSERT(caps->ac_len >= sizeof(struct audio_caps_s));
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Fill in the caller's structure based on requested info */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_format.hw = 0;
|
|
|
|
caps->ac_controls.w = 0;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_type)
|
2013-05-19 23:12:28 +02:00
|
|
|
{
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Caller is querying for the types of units we support */
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
case AUDIO_TYPE_QUERY:
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Provide our overall capabilities. The interfacing software
|
|
|
|
* must then call us back for specific info for each capability.
|
|
|
|
*/
|
2013-05-19 23:12:28 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_channels = 2; /* Stereo output */
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_subtype)
|
2013-10-28 19:11:52 +01:00
|
|
|
{
|
|
|
|
case AUDIO_TYPE_QUERY:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-28 19:11:52 +01:00
|
|
|
/* The input formats we can decode / accept */
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_format.hw = 0
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_AC3
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_AC3 - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_MP3
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_MP3 - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_WMA
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_WMA - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_MIDI
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_MIDI - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_PCM
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_PCM - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_OGG_VORBIS
|
2013-10-28 19:11:52 +01:00
|
|
|
| (1 << (AUDIO_FMT_OGG_VORBIS - 1))
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
2013-10-28 19:11:52 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
/* The types of audio units we implement */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_TYPE_OUTPUT |
|
|
|
|
AUDIO_TYPE_FEATURE |
|
|
|
|
AUDIO_TYPE_PROCESSING;
|
2013-10-28 19:11:52 +01:00
|
|
|
|
|
|
|
break;
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-28 19:11:52 +01:00
|
|
|
/* Report sub-formats for MIDI if requested */
|
2013-05-21 21:13:05 +02:00
|
|
|
|
2013-10-28 19:11:52 +01:00
|
|
|
#ifdef CONFIG_AUDIO_FORMAT_MIDI
|
|
|
|
case AUDIO_FMT_MIDI:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-28 19:11:52 +01:00
|
|
|
/* We only support Format 0 */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_SUBFMT_MIDI_0;
|
|
|
|
caps->ac_controls.b[1] = AUDIO_SUBFMT_END;
|
2013-10-28 19:11:52 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_SUBFMT_END;
|
2013-10-28 19:11:52 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Provide capabilities of our OUTPUT unit */
|
|
|
|
|
|
|
|
case AUDIO_TYPE_OUTPUT:
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_channels = 2;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_subtype)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
case AUDIO_TYPE_QUERY:
|
|
|
|
|
|
|
|
/* Report the Sample rates we support */
|
|
|
|
|
2024-08-22 15:20:52 +02:00
|
|
|
caps->ac_controls.hw[0] = AUDIO_SAMP_RATE_8K |
|
|
|
|
AUDIO_SAMP_RATE_11K |
|
|
|
|
AUDIO_SAMP_RATE_16K |
|
|
|
|
AUDIO_SAMP_RATE_22K |
|
|
|
|
AUDIO_SAMP_RATE_32K |
|
|
|
|
AUDIO_SAMP_RATE_44K |
|
|
|
|
AUDIO_SAMP_RATE_48K;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUDIO_FMT_MP3:
|
|
|
|
case AUDIO_FMT_WMA:
|
|
|
|
case AUDIO_FMT_PCM:
|
2020-04-21 09:45:24 +02:00
|
|
|
/* Report the Bit rates we support.
|
|
|
|
* The bit rate support is actually a complex function of the
|
|
|
|
* format and selected sample rate, and the datasheet has
|
|
|
|
* multiple tables to indicate the supported bit rate vs sample
|
|
|
|
* rate vsformat.
|
|
|
|
* The selected sample rate should be provided in the ac_format
|
|
|
|
* field of the query, and only a single sample rate should be
|
|
|
|
* given.
|
2013-10-27 15:00:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO: Create a table or set of tables to report this! */
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Provide capabilities of our FEATURE units */
|
|
|
|
|
|
|
|
case AUDIO_TYPE_FEATURE:
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/* If the sub-type is UNDEF,
|
|
|
|
* then report the Feature Units we support
|
|
|
|
*/
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
if (caps->ac_subtype == AUDIO_FU_UNDEF)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
/* Fill in the ac_controls section with the
|
|
|
|
* Feature Units we have
|
|
|
|
*/
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_FU_VOLUME |
|
|
|
|
AUDIO_FU_BASS |
|
|
|
|
AUDIO_FU_TREBLE;
|
|
|
|
caps->ac_controls.b[1] = AUDIO_FU_BALANCE >> 8;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
/* TODO:
|
|
|
|
* Do we need to provide specific info for the Feature Units,
|
2013-10-27 15:00:16 +01:00
|
|
|
* such as volume setting ranges, etc.?
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Provide capabilities of our PROCESSING unit */
|
|
|
|
|
|
|
|
case AUDIO_TYPE_PROCESSING:
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_subtype)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
case AUDIO_PU_UNDEF:
|
|
|
|
|
|
|
|
/* Provide the type of Processing Units we support */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_PU_STEREO_EXTENDER;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AUDIO_PU_STEREO_EXTENDER:
|
|
|
|
|
|
|
|
/* Proivde capabilities of our Stereo Extender */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_controls.b[0] = AUDIO_STEXT_ENABLE |
|
|
|
|
AUDIO_STEXT_WIDTH;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/* Other types of processing uint we don't support */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* All others we don't support */
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/* Zero out the fields to indicate no support */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
caps->ac_subtype = 0;
|
|
|
|
caps->ac_channels = 0;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the length of the audio_caps_s struct for validation of
|
|
|
|
* proper Audio device type.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
return caps->ac_len;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_configure
|
|
|
|
*
|
|
|
|
* Description: Configure the audio device for the specified mode of
|
|
|
|
* operation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR void *session,
|
|
|
|
FAR const struct audio_caps_s *caps)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_configure(FAR struct audio_lowerhalf_s *lower,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR const struct audio_caps_s *caps)
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if !defined(CONFIG_AUDIO_EXCLUDE_VOLUME) || !defined(CONFIG_AUDIO_EXCLUDE_TONE)
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Entry\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Process the configure operation */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_type)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
case AUDIO_TYPE_FEATURE:
|
|
|
|
|
|
|
|
/* Process based on Feature Unit */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (caps->ac_format.hw)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
|
|
|
case AUDIO_FU_VOLUME:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Set the volume */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
dev->volume = caps->ac_controls.hw[0];
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_setvolume(dev);
|
|
|
|
|
|
|
|
break;
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_VOLUME */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
#if !defined(CONFIG_AUDIO_EXCLUDE_TONE) && !defined(CONFIG_AUDIO_EXCLUDE_VOLUME)
|
|
|
|
case AUDIO_FU_BALANCE:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Set the volume */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
dev->balance = caps->ac_controls.hw[0];
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_setvolume(dev);
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_TONE
|
|
|
|
case AUDIO_FU_BASS:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Set the bass. The percentage level (0-100) is in the
|
|
|
|
* ac_controls[0] parameter.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
dev->bass = caps->ac_controls.b[0];
|
2013-10-27 15:00:16 +01:00
|
|
|
if (dev->bass > 100)
|
2024-08-24 19:25:04 +02:00
|
|
|
{
|
|
|
|
dev->bass = 100;
|
|
|
|
}
|
2024-08-25 01:21:12 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_setbass(dev);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AUDIO_FU_TREBLE:
|
|
|
|
/* Set the treble. The percentage level (0-100) is in the
|
2014-07-24 18:21:04 +02:00
|
|
|
* ac_controls.b[0] parameter.
|
2013-10-27 15:00:16 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
dev->treble = caps->ac_controls.b[0];
|
2013-10-27 15:00:16 +01:00
|
|
|
if (dev->treble > 100)
|
2024-08-24 19:25:04 +02:00
|
|
|
{
|
|
|
|
dev->treble = 100;
|
|
|
|
}
|
2024-08-25 01:21:12 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_setbass(dev);
|
|
|
|
|
|
|
|
break;
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_TONE */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
default:
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
/* Others we don't support */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-07-24 17:56:39 +02:00
|
|
|
case AUDIO_TYPE_OUTPUT:
|
|
|
|
break;
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
case AUDIO_TYPE_PROCESSING:
|
|
|
|
|
|
|
|
/* We only support STEREO_EXTENDER */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
if (caps->ac_format.hw == AUDIO_PU_STEREO_EXTENDER)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
return OK;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_softreset
|
|
|
|
*
|
|
|
|
* Description: Performs a soft reset on the VS1053 chip by setting the
|
|
|
|
* RESET bit of the MODE register.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_softreset(FAR struct vs1053_struct_s *dev)
|
|
|
|
{
|
|
|
|
uint16_t reg;
|
|
|
|
uint16_t timeout;
|
|
|
|
|
|
|
|
/* First disable interrupts, lower the frequency and lock the SPI bus */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
|
|
|
vs1053_spi_lock(dev->spi, VS1053_DEFAULT_XTALI / 7);
|
|
|
|
|
|
|
|
/* Now issue a reset command */
|
|
|
|
|
|
|
|
reg = vs1053_readreg(dev, VS1053_SCI_MODE);
|
|
|
|
vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_RESET);
|
|
|
|
|
|
|
|
/* Now wait for the SM_RESET to go inactive */
|
|
|
|
|
|
|
|
timeout = 1000;
|
|
|
|
while (vs1053_readreg(dev, VS1053_SCI_MODE) & VS1053_SM_RESET && timeout)
|
|
|
|
{
|
|
|
|
timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to low frequency, Unlock the SPI bus and exit */
|
|
|
|
|
|
|
|
vs1053_setfrequency(dev, CONFIG_VS1053_XTALI);
|
|
|
|
vs1053_spi_unlock(dev->spi);
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_hardreset
|
|
|
|
*
|
|
|
|
* Description: Performs a hardware reset on the VS1053 chip by toggling
|
|
|
|
* the RST line, disabling IRQ, and setting the default
|
|
|
|
* XTALI frequency.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_hardreset(FAR struct vs1053_struct_s *dev)
|
|
|
|
{
|
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
|
|
|
dev->hw_lower->reset(dev->hw_lower, false);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(10);
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->reset(dev->hw_lower, true);
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(VS1053_RST_USECS);
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_setfrequency(dev, CONFIG_VS1053_XTALI); /* Slow speed at first */
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_shutdown
|
|
|
|
*
|
|
|
|
* Description: Shutdown the VS1053 chip and put it in the lowest power
|
|
|
|
* state possible.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_shutdown(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Entry\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_lock(spi, dev->spi_freq); /* Lock the device */
|
|
|
|
vs1053_setfrequency(dev, CONFIG_VS1053_XTALI); /* Reduce speed to minimum */
|
2020-04-21 09:45:24 +02:00
|
|
|
vs1053_writereg(dev, VS1053_SCI_VOL, 0xfefe); /* Power down the DAC outputs */
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(spi); /* Unlock the device */
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_feeddata
|
|
|
|
*
|
|
|
|
* Description: Feeds more data to the vs1053 chip from the enqueued
|
|
|
|
* buffers. It will continue feeding data until the DREQ
|
|
|
|
* line indicates it can't accept any more data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void vs1053_feeddata(FAR struct vs1053_struct_s *dev)
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
int bytecount;
|
|
|
|
int ret;
|
|
|
|
uint8_t *samp = NULL;
|
|
|
|
uint16_t reg;
|
|
|
|
FAR struct ap_buffer_s *apb;
|
|
|
|
FAR struct spi_dev_s *spi = dev->spi;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Check for false interrupt caused by an SCI transaction */
|
|
|
|
|
|
|
|
if (!dev->hw_lower->read_dreq(dev->hw_lower) || dev->paused)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Grab the SPI bus. We can run at 20Mhz because we increased the
|
|
|
|
* chip frequency above 40Mhz for the decode operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vs1053_spi_lock(spi, VS1053_DATA_FREQ); /* Lock the SPI bus */
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), true); /* Select the VS1053 data bus */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Local stack copy of our active buffer */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
apb = dev->apb;
|
2020-04-21 09:45:24 +02:00
|
|
|
|
|
|
|
/* audinfo("Entry apb=%p, Bytes left=%d\n",
|
|
|
|
* apb, apb->nbytes - apb->curbyte);
|
|
|
|
*/
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Setup pointer to the next sample in the buffer */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
if (apb)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
samp = &apb->samp[apb->curbyte];
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
else if (!dev->endmode)
|
|
|
|
{
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), false);
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(spi);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop until the FIFO is full */
|
|
|
|
|
|
|
|
while (dev->hw_lower->read_dreq(dev->hw_lower))
|
|
|
|
{
|
|
|
|
/* If endmode, then send fill characters */
|
|
|
|
|
|
|
|
if (dev->endmode)
|
|
|
|
{
|
|
|
|
bytecount = 32;
|
|
|
|
while (bytecount)
|
|
|
|
{
|
|
|
|
SPI_SEND(spi, dev->endfillchar);
|
|
|
|
bytecount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For the VS1053, after the file has been played, we must
|
|
|
|
* send 2052 bytes of endfillchar per the datasheet.
|
|
|
|
*/
|
|
|
|
|
|
|
|
dev->endfillbytes += 32;
|
|
|
|
|
|
|
|
/* Process end mode logic. We send 2080 bytes of endfillchar as
|
|
|
|
* directed by the datasheet, then set SM_CANCEL. Then we wait
|
|
|
|
* until the chip clears SM_CANCEL while sending endfillchar
|
|
|
|
* 32 bytes at a time.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
if (dev->endfillbytes == 32 * 65)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* After at least 2052 bytes, we send an SM_CANCEL */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
|
2013-10-27 15:00:16 +01:00
|
|
|
reg = vs1053_readreg(dev, VS1053_SCI_MODE);
|
|
|
|
vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_CANCEL);
|
|
|
|
dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
|
|
|
|
}
|
2020-04-21 09:45:24 +02:00
|
|
|
else if (dev->endfillbytes >= 32 * 130)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* Do a hard reset and terminate */
|
|
|
|
|
|
|
|
vs1053_hardreset(dev);
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->running = false;
|
|
|
|
dev->endmode = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-04-21 09:45:24 +02:00
|
|
|
else if (dev->endfillbytes > 32 * 65)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* After each 32 byte of endfillchar, check the status
|
2014-08-01 00:36:09 +02:00
|
|
|
* register to see if SM_CANCEL has been cleared. If
|
2013-10-27 15:00:16 +01:00
|
|
|
* it has been cleared, then we're done.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
if (!(vs1053_readreg(dev, VS1053_SCI_STATUS) &
|
|
|
|
VS1053_SM_CANCEL))
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
2016-06-12 17:26:00 +02:00
|
|
|
|
|
|
|
audinfo("HDAT1: 0x%0X HDAT0: 0x%0X\n",
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT1),
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT0));
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
vs1053_writereg(dev,
|
|
|
|
VS1053_SCI_WRAMADDR,
|
|
|
|
VS1053_END_FILL_BYTE);
|
|
|
|
dev->endfillchar = vs1053_readreg(dev,
|
|
|
|
VS1053_SCI_WRAM) >> 8;
|
2016-06-12 17:26:00 +02:00
|
|
|
|
|
|
|
audinfo("EndFillChar: 0x%0X\n", dev->endfillchar);
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
reg = vs1053_readreg(dev, VS1053_SCI_MODE);
|
2020-04-21 09:45:24 +02:00
|
|
|
vs1053_writereg(dev,
|
|
|
|
VS1053_SCI_MODE,
|
|
|
|
reg | VS1053_SM_RESET);
|
2014-08-01 00:36:09 +02:00
|
|
|
|
|
|
|
dev->running = false;
|
|
|
|
dev->endmode = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Send 32 more bytes. We only send 32 at a time because this is
|
|
|
|
* the meaning of DREQ active from the chip ... that it can
|
|
|
|
* accept at least 32 more bytes. After each 32 byte block, we
|
|
|
|
* will recheck the DREQ line again.
|
|
|
|
*/
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
bytecount = apb->nbytes - apb->curbyte;
|
2013-10-27 15:00:16 +01:00
|
|
|
if (bytecount > 32)
|
|
|
|
{
|
|
|
|
bytecount = 32;
|
|
|
|
}
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#if 1
|
2020-04-21 09:45:24 +02:00
|
|
|
SPI_SNDBLOCK(spi, samp, bytecount);
|
|
|
|
samp += bytecount;
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
bytecount = bytecount;
|
|
|
|
while (bytecount--)
|
|
|
|
{
|
|
|
|
/* Send next byte from the buffer */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
SPI_SEND(spi, *samp);
|
|
|
|
samp++;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
2020-04-21 09:45:24 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
2014-08-01 00:36:09 +02:00
|
|
|
apb->curbyte += bytecount;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Test if we are in cancel mode. If we are, then we need
|
|
|
|
* to continue sending file data and check for the SM_CANCEL
|
|
|
|
* bit going inactive.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
if (dev->cancelmode)
|
|
|
|
{
|
|
|
|
/* Read the VS1053 MODE register */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
|
2013-10-27 15:00:16 +01:00
|
|
|
reg = vs1053_readreg(dev, VS1053_SCI_MODE);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, VS1053_DATA_FREQ);
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
|
|
|
|
|
|
|
|
/* Check the SM_CANCEL bit */
|
|
|
|
|
|
|
|
if (!(reg & VS1053_SM_CANCEL))
|
|
|
|
{
|
|
|
|
/* Cancel has begun. Switch to endmode */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
apb->nbytes = 0;
|
|
|
|
apb->curbyte = 0;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_STOP */
|
|
|
|
|
|
|
|
/* Test if we are at the end of the buffer */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
if (apb->curbyte >= apb->nbytes)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2014-08-01 00:36:09 +02:00
|
|
|
/* Check if this was the final buffer in stream */
|
|
|
|
|
|
|
|
if ((apb->flags & AUDIO_APB_FINAL) != 0)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* This is the final buffer. Get the VS1053 endfillchar */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, dev->spi_freq);
|
2020-04-21 09:45:24 +02:00
|
|
|
vs1053_writereg(dev,
|
|
|
|
VS1053_SCI_WRAMADDR,
|
|
|
|
VS1053_END_FILL_BYTE);
|
|
|
|
dev->endfillchar = vs1053_readreg(dev,
|
|
|
|
VS1053_SCI_WRAM) >> 8;
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SETFREQUENCY(dev->spi, VS1053_DATA_FREQ);
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
|
|
|
|
|
|
|
|
/* Mark the device as endmode */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->endmode = true;
|
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
if (dev->cancelmode)
|
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
/* If we are in cancel mode, then we don't dequeue the
|
|
|
|
* buffer or need to send another SM_CANCEL, so jump
|
|
|
|
* into the middle of the stop sequence.
|
2013-10-27 15:00:16 +01:00
|
|
|
*/
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
dev->endfillbytes = 32 * 65 + 1;
|
2013-10-27 15:00:16 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
2020-01-31 19:07:39 +01:00
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_STOP */
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
dev->endfillbytes = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We referenced the buffer so we must free it */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
apb_free(apb);
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_DEQUEUE,
|
2024-08-24 19:25:04 +02:00
|
|
|
apb, OK, NULL);
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_DEQUEUE,
|
2024-08-24 19:25:04 +02:00
|
|
|
apb, OK);
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Lock the buffer queue to pop the next buffer */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
if ((ret = nxmutex_lock(&dev->apbq_lock)) < 0)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2024-08-24 19:25:04 +02:00
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_IOERR,
|
|
|
|
NULL, ret, NULL);
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
2024-08-24 19:25:04 +02:00
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_IOERR,
|
|
|
|
NULL, ret);
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
2016-06-12 17:26:00 +02:00
|
|
|
auderr("ERROR: I/O error!\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop the next entry */
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
apb = (FAR struct ap_buffer_s *)dq_remfirst(&dev->apbq);
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->apb = apb;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
/* audinfo("Next Buffer = %p, bytes = %d\n",
|
|
|
|
* apb, apb ? apb->nbytes : 0);
|
|
|
|
*/
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
if (apb == NULL)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
samp = &apb->samp[apb->curbyte];
|
2014-08-01 00:36:09 +02:00
|
|
|
apb_reference(apb); /* Add our buffer reference */
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deselect the SPI bus and unlock it */
|
2017-10-04 23:22:27 +02:00
|
|
|
|
2013-10-27 15:00:16 +01:00
|
|
|
err_out:
|
2017-04-29 20:26:52 +02:00
|
|
|
SPI_SELECT(spi, SPIDEV_AUDIO_DATA(0), false);
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(spi);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_dreq_isr
|
|
|
|
*
|
|
|
|
* This is the ISR that services the DREQ pin from the VS1053, which
|
|
|
|
* indicates the chip is ready to receive additional data. We use it to
|
|
|
|
* send a message to our workertherad message queue so it knows to wake
|
|
|
|
* up and send more data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-02-27 17:44:13 +01:00
|
|
|
static int vs1053_dreq_isr(int irq, FAR void *context, FAR void *arg)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)arg;
|
|
|
|
struct audio_msg_s msg;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2017-02-27 17:44:13 +01:00
|
|
|
DEBUGASSERT(dev != NULL);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Now create a message and send it to the workerthread */
|
|
|
|
|
|
|
|
if (dev->running)
|
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
msg.msg_id = AUDIO_MSG_DATA_REQUEST;
|
2020-12-24 13:53:17 +01:00
|
|
|
file_mq_send(&dev->mq, (FAR const char *)&msg, sizeof(msg),
|
|
|
|
CONFIG_VS1053_MSG_PRIO);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
msg.msg_id = AUDIO_MSG_DATA_REQUEST;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_workerthread
|
|
|
|
*
|
|
|
|
* This is the thread that feeds data to the chip and keeps the audio
|
|
|
|
* stream going.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void *vs1053_workerthread(pthread_addr_t pvarg)
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)pvarg;
|
|
|
|
struct audio_msg_s msg;
|
|
|
|
FAR struct ap_buffer_s *apb;
|
|
|
|
int size;
|
|
|
|
unsigned int prio;
|
2014-02-11 01:08:49 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2024-08-24 19:25:04 +02:00
|
|
|
uint16_t reg;
|
2014-02-11 01:08:49 +01:00
|
|
|
#endif
|
2024-08-24 19:25:04 +02:00
|
|
|
uint8_t timeout;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Entry\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->cancelmode = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->endmode = false;
|
|
|
|
dev->endfillbytes = 0;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Fill the VS1053 FIFO with initial data. */
|
|
|
|
|
|
|
|
vs1053_feeddata(dev); /* Fill the VS1053 FIFO */
|
|
|
|
|
|
|
|
/* Wait for DREQ to go active so we can issue a READ command */
|
|
|
|
|
|
|
|
timeout = 200;
|
|
|
|
while (!dev->hw_lower->read_dreq(dev->hw_lower) && timeout)
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(100);
|
2013-10-27 15:00:16 +01:00
|
|
|
timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop as long as we are supposed to be running */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->running = true;
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
|
|
|
|
while (dev->running || dev->endmode)
|
|
|
|
{
|
|
|
|
if (dev->hw_lower->read_dreq(dev->hw_lower))
|
|
|
|
{
|
|
|
|
vs1053_feeddata(dev); /* Feed more data to the VS1053 FIFO */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for messages from our message queue */
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
size = file_mq_receive(&dev->mq, (FAR char *)&msg, sizeof(msg), &prio);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Handle the case when we return with no message */
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
/* Should we just stop running? */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->running = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process the message */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
switch (msg.msg_id)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* The ISR has requested more data */
|
|
|
|
|
|
|
|
case AUDIO_MSG_DATA_REQUEST:
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(500);
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_feeddata(dev); /* Feed more data to the VS1053 FIFO */
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Stop the playback */
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
case AUDIO_MSG_STOP:
|
|
|
|
if (!dev->hw_lower->read_dreq(dev->hw_lower))
|
|
|
|
{
|
2017-10-06 18:15:01 +02:00
|
|
|
nxsig_usleep(300);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Send CANCEL message to VS1053 */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower);
|
|
|
|
vs1053_spi_lock(dev->spi, dev->spi_freq);
|
|
|
|
reg = vs1053_readreg(dev, VS1053_SCI_MODE);
|
|
|
|
vs1053_writereg(dev, VS1053_SCI_MODE, reg | VS1053_SM_CANCEL);
|
|
|
|
vs1053_spi_unlock(dev->spi);
|
|
|
|
dev->hw_lower->enable(dev->hw_lower);
|
|
|
|
|
|
|
|
/* Set cancelmode */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->cancelmode = true;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We will wake up when a new buffer enqueued just in case */
|
|
|
|
|
|
|
|
case AUDIO_MSG_ENQUEUE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Disable the DREQ interrupt */
|
|
|
|
|
|
|
|
dev->hw_lower->disable(dev->hw_lower);
|
|
|
|
|
|
|
|
/* Cancel any leftover buffer in our queue */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
if (nxmutex_lock(&dev->apbq_lock) == OK)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* Get the next buffer from the queue */
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
while ((apb = (FAR struct ap_buffer_s *)dq_remfirst(&dev->apbq))
|
2020-04-21 09:45:24 +02:00
|
|
|
!= NULL)
|
2013-10-27 15:00:16 +01:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Free the active buffer */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
if (dev->apb != NULL)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2014-08-01 00:36:09 +02:00
|
|
|
apb_free(dev->apb);
|
|
|
|
dev->apb = NULL;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the message queue */
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
file_mq_close(&dev->mq);
|
|
|
|
file_mq_unlink(dev->mqname);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Send an AUDIO_MSG_COMPLETE message to the client */
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK, NULL);
|
|
|
|
#else
|
|
|
|
dev->lower.upper(dev->lower.priv, AUDIO_CALLBACK_COMPLETE, NULL, OK);
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Exit\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_start
|
|
|
|
*
|
|
|
|
* Description: Start the configured operation (audio streaming, volume
|
|
|
|
* enabled, etc.).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int vs1053_start(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_start(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
struct mq_attr attr;
|
|
|
|
struct sched_param sparam;
|
|
|
|
pthread_attr_t tattr;
|
|
|
|
int ret;
|
|
|
|
FAR void *value;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Entry\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Entry HDAT1=0x%0X HDAT0=0x%0X\n",
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT1),
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT0));
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(dev->spi);
|
|
|
|
|
|
|
|
/* Do a soft reset, just in case */
|
|
|
|
|
|
|
|
vs1053_softreset(dev);
|
|
|
|
|
|
|
|
/* Increase the frequency of the part during processing */
|
|
|
|
|
|
|
|
vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
|
|
|
|
vs1053_setfrequency(dev, CONFIG_VS1053_MP3_DECODE_FREQ);
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Reset HDAT1=0x%0X HDAT0=0x%0X\n",
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT1),
|
|
|
|
vs1053_readreg(dev, VS1053_SCI_HDAT0));
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_spi_unlock(dev->spi);
|
|
|
|
|
|
|
|
/* Create a message queue for the worker thread */
|
|
|
|
|
2020-11-13 11:02:08 +01:00
|
|
|
snprintf(dev->mqname, sizeof(dev->mqname), "/tmp/%" PRIXPTR,
|
|
|
|
(uintptr_t)dev);
|
2013-10-27 15:00:16 +01:00
|
|
|
attr.mq_maxmsg = 16;
|
|
|
|
attr.mq_msgsize = sizeof(struct audio_msg_s);
|
|
|
|
attr.mq_curmsgs = 0;
|
|
|
|
attr.mq_flags = 0;
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = file_mq_open(&dev->mq, dev->mqname,
|
|
|
|
O_RDWR | O_CREAT, 0644, &attr);
|
|
|
|
if (ret < 0)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* Error creating message queue! */
|
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
auderr("ERROR: Couldn't allocate message queue\n");
|
2020-12-24 13:53:17 +01:00
|
|
|
return ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pop the first enqueued buffer */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
if ((ret = nxmutex_lock(&dev->apbq_lock)) == OK)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
dev->apb = (FAR struct ap_buffer_s *)dq_remfirst(&dev->apbq);
|
2014-08-01 00:36:09 +02:00
|
|
|
apb_reference(dev->apb); /* Add our buffer reference */
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
auderr("ERROR: Error getting APB Queue sem\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Join any old worker thread we had created to prevent a memory leak */
|
|
|
|
|
|
|
|
if (dev->threadid != 0)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Joining old thread\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
pthread_join(dev->threadid, &value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start our thread for sending data to the device */
|
|
|
|
|
|
|
|
pthread_attr_init(&tattr);
|
|
|
|
sparam.sched_priority = sched_get_priority_max(SCHED_FIFO) - 3;
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_attr_setschedparam(&tattr, &sparam);
|
|
|
|
pthread_attr_setstacksize(&tattr, CONFIG_VS1053_WORKER_STACKSIZE);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Starting workerthread\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
ret = pthread_create(&dev->threadid, &tattr, vs1053_workerthread,
|
2024-08-24 19:25:04 +02:00
|
|
|
(pthread_addr_t)dev);
|
2013-10-27 15:00:16 +01:00
|
|
|
if (ret != OK)
|
|
|
|
{
|
2021-02-15 09:08:51 +01:00
|
|
|
auderr("ERROR: Can't create worker thread, ret=%d\n", ret);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
else
|
2013-11-10 18:03:19 +01:00
|
|
|
{
|
|
|
|
pthread_setname_np(dev->threadid, "vs1053");
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("Created worker thread\n");
|
2013-11-10 18:03:19 +01:00
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_stop
|
|
|
|
*
|
|
|
|
* Description: Stop the configured operation (audio streaming, volume
|
|
|
|
* disabled, etc.).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_STOP
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int vs1053_stop(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_stop(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
struct audio_msg_s term_msg;
|
|
|
|
FAR void *value;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Send a message to stop all audio streaming */
|
|
|
|
|
2020-04-21 09:45:24 +02:00
|
|
|
term_msg.msg_id = AUDIO_MSG_STOP;
|
2013-10-27 15:00:16 +01:00
|
|
|
term_msg.u.data = 0;
|
2020-12-24 13:53:17 +01:00
|
|
|
file_mq_send(&dev->mq, (FAR const char *)&term_msg, sizeof(term_msg),
|
|
|
|
CONFIG_VS1053_MSG_PRIO);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Join the worker thread */
|
|
|
|
|
|
|
|
pthread_join(dev->threadid, &value);
|
|
|
|
dev->threadid = 0;
|
|
|
|
|
|
|
|
/* Reduce the decoder's operating frequency to save power */
|
|
|
|
|
|
|
|
vs1053_spi_lock(dev->spi, dev->spi_freq); /* Lock the device */
|
|
|
|
vs1053_setfrequency(dev, CONFIG_VS1053_XTALI);
|
|
|
|
vs1053_spi_unlock(dev->spi);
|
|
|
|
|
|
|
|
/* Wait for a bit */
|
|
|
|
|
|
|
|
up_mdelay(40);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_pause
|
|
|
|
*
|
|
|
|
* Description: Pauses the playback.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int vs1053_pause(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_pause(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
if (!dev->running)
|
2014-08-01 00:36:09 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
/* Disable interrupts to prevent us from supplying any more data */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->paused = true;
|
2013-10-27 15:00:16 +01:00
|
|
|
dev->hw_lower->disable(dev->hw_lower); /* Disable the DREQ interrupt */
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_resume
|
|
|
|
*
|
|
|
|
* Description: Resuems the playback.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
2020-04-21 09:45:24 +02:00
|
|
|
static int vs1053_resume(FAR struct audio_lowerhalf_s *lower,
|
|
|
|
FAR void *session)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_resume(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
if (!dev->running)
|
2014-08-01 00:36:09 +02:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Enable interrupts to allow suppling data */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->paused = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
vs1053_feeddata(dev);
|
|
|
|
dev->hw_lower->enable(dev->hw_lower); /* Enable the DREQ interrupt */
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_AUDIO_EXCLUDE_PAUSE_RESUME */
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_enqueuebuffer
|
|
|
|
*
|
|
|
|
* Description: Enqueue an Audio Pipeline Buffer for playback/ processing.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_enqueuebuffer(FAR struct audio_lowerhalf_s *lower,
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct ap_buffer_s *apb)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
struct audio_msg_s term_msg;
|
|
|
|
int ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
audinfo("Entry\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Lock access to the apbq */
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
if ((ret = nxmutex_lock(&dev->apbq_lock)) == OK)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
/* We can now safely add the buffer to the queue */
|
|
|
|
|
|
|
|
apb->curbyte = 0;
|
2014-08-01 00:36:09 +02:00
|
|
|
apb->flags |= AUDIO_APB_OUTPUT_ENQUEUED;
|
2013-10-27 15:00:16 +01:00
|
|
|
dq_addlast(&apb->dq_entry, &dev->apbq);
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Send a message indicating a new buffer enqueued */
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
if (dev->mq.f_inode != NULL)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2020-04-21 09:45:24 +02:00
|
|
|
term_msg.msg_id = AUDIO_MSG_ENQUEUE;
|
2013-10-27 15:00:16 +01:00
|
|
|
term_msg.u.data = 0;
|
2020-12-24 13:53:17 +01:00
|
|
|
file_mq_send(&dev->mq, (FAR const char *)&term_msg,
|
|
|
|
sizeof(term_msg), CONFIG_VS1053_MSG_PRIO);
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_cancelbuffer
|
|
|
|
*
|
|
|
|
* Description: Called when an enqueued buffer is being cancelled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_cancelbuffer(FAR struct audio_lowerhalf_s *lower,
|
2015-10-10 18:41:00 +02:00
|
|
|
FAR struct ap_buffer_s *apb)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_ioctl
|
|
|
|
*
|
|
|
|
* Description: Perform a device ioctl
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int vs1053_ioctl(FAR struct audio_lowerhalf_s *lower, int cmd,
|
2024-08-24 19:25:04 +02:00
|
|
|
unsigned long arg)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2020-06-05 03:34:01 +02:00
|
|
|
int ret = OK;
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
|
2020-04-21 09:45:24 +02:00
|
|
|
FAR struct ap_buffer_info_s *bufinfo;
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Deal with ioctls passed from the upper-half driver */
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* Check for AUDIOIOC_HWRESET ioctl. This ioctl is passed straight
|
|
|
|
* through from the upper-half audio driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case AUDIOIOC_HWRESET:
|
2024-08-24 19:25:04 +02:00
|
|
|
vs1053_hardreset((FAR struct vs1053_struct_s *)lower);
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Report our preferred buffer size and quantity */
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_DRIVER_SPECIFIC_BUFFERS
|
|
|
|
case AUDIOIOC_GETBUFFERINFO:
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
bufinfo = (FAR struct ap_buffer_info_s *)arg;
|
2020-04-21 09:45:24 +02:00
|
|
|
bufinfo->buffer_size = CONFIG_VS1053_BUFFER_SIZE;
|
|
|
|
bufinfo->nbuffers = CONFIG_VS1053_NUM_BUFFERS;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
2020-06-05 03:34:01 +02:00
|
|
|
ret = -ENOTTY;
|
2013-10-27 15:00:16 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-06-05 03:34:01 +02:00
|
|
|
return ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_reserve
|
|
|
|
*
|
|
|
|
* Description: Reserves a session (the only one we have).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR void **psession)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_reserve(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
int ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Borrow the APBQ mutex for thread sync */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->apbq_lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dev->busy)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Initialize the session context. We don't really use it. */
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
*psession = NULL;
|
|
|
|
#endif
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->busy = true;
|
|
|
|
dev->running = false;
|
|
|
|
dev->paused = false;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_release
|
|
|
|
*
|
|
|
|
* Description: Releases the session (the only one we have).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_AUDIO_MULTI_SESSION
|
|
|
|
static int vs1053_release(FAR struct audio_lowerhalf_s *lower,
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR void *psession)
|
2013-10-27 15:00:16 +01:00
|
|
|
#else
|
|
|
|
static int vs1053_release(FAR struct audio_lowerhalf_s *lower)
|
|
|
|
#endif
|
|
|
|
{
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct vs1053_struct_s *dev = (FAR struct vs1053_struct_s *)lower;
|
|
|
|
FAR void *value;
|
|
|
|
int ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
/* Join any old worker thread we had created to prevent a memory leak */
|
|
|
|
|
|
|
|
if (dev->threadid != 0)
|
|
|
|
{
|
|
|
|
pthread_join(dev->threadid, &value);
|
|
|
|
dev->threadid = 0;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* Borrow the APBQ mutex for thread sync */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
ret = nxmutex_lock(&dev->apbq_lock);
|
2017-10-04 23:22:27 +02:00
|
|
|
if (ret < 0)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
2017-10-04 23:22:27 +02:00
|
|
|
return ret;
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Really we should free any queued buffers here */
|
|
|
|
|
2014-08-01 00:36:09 +02:00
|
|
|
dev->busy = false;
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: vs1053_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the VS1053 device
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* spidevice - This is a placeholder argument until the Audio interface
|
|
|
|
* has been flushed out a bit.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-24 19:25:04 +02:00
|
|
|
FAR struct audio_lowerhalf_s *vs1053_initialize(FAR struct spi_dev_s *spi,
|
|
|
|
FAR const struct vs1053_lower_s *lower,
|
|
|
|
unsigned int devno)
|
2013-10-27 15:00:16 +01:00
|
|
|
{
|
|
|
|
FAR struct vs1053_struct_s *dev;
|
|
|
|
uint16_t status;
|
|
|
|
uint8_t id;
|
|
|
|
uint8_t retry;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
DEBUGASSERT(spi != NULL);
|
|
|
|
DEBUGASSERT(lower != NULL);
|
|
|
|
DEBUGASSERT(lower->reset != NULL);
|
|
|
|
|
|
|
|
/* Allocate a VS1053 device structure */
|
|
|
|
|
2023-08-28 09:39:47 +02:00
|
|
|
dev = kmm_zalloc(sizeof(struct vs1053_struct_s));
|
2013-10-27 15:00:16 +01:00
|
|
|
if (dev)
|
|
|
|
{
|
|
|
|
/* Initialize the VS1053 device structure */
|
|
|
|
|
2014-07-25 18:04:20 +02:00
|
|
|
dev->lower.ops = &g_audioops;
|
|
|
|
dev->hw_lower = lower;
|
|
|
|
dev->spi_freq = CONFIG_VS1053_XTALI / 7;
|
|
|
|
dev->spi = spi;
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_VOLUME
|
2014-07-25 18:04:20 +02:00
|
|
|
dev->volume = 250; /* 25% volume as default */
|
2013-10-27 15:00:16 +01:00
|
|
|
#ifndef CONFIG_AUDIO_EXCLUDE_BALANCE
|
2014-07-25 18:04:20 +02:00
|
|
|
dev->balance = 500; /* Center balance */
|
2013-10-27 15:00:16 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_init(&dev->apbq_lock);
|
2013-10-27 15:00:16 +01:00
|
|
|
dq_init(&dev->apbq);
|
|
|
|
|
|
|
|
/* Reset the VS1053 chip */
|
|
|
|
|
|
|
|
lower->reset(lower, false);
|
|
|
|
up_udelay(10);
|
|
|
|
lower->reset(lower, true);
|
|
|
|
up_udelay(VS1053_RST_USECS);
|
|
|
|
|
|
|
|
#if CONFIG_VS1053_XTALI == VS1053_DEFAULT_XTALI
|
|
|
|
|
|
|
|
/* If we have a standard crystal, then wait extra time
|
|
|
|
* for the DREQ to be active indicating the device is ready
|
|
|
|
*/
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
retry = 200;
|
2013-10-27 15:00:16 +01:00
|
|
|
while (!lower->read_dreq(lower) && retry)
|
|
|
|
{
|
|
|
|
up_udelay(10);
|
|
|
|
retry--;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Do device detection to validate the chip is there.
|
|
|
|
* We have to hold the SPI lock during reads / writes.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vs1053_spi_lock(spi, dev->spi_freq);
|
|
|
|
status = vs1053_readreg(dev, VS1053_SCI_STATUS);
|
|
|
|
vs1053_spi_unlock(spi);
|
|
|
|
|
|
|
|
/* Validate the device ID read from the chip */
|
|
|
|
|
|
|
|
id = (status & VS1053_SS_VER) >> VS1053_VER_SHIFT;
|
|
|
|
if (id != VS1053_VER_VS1053)
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
auderr("ERROR: Unexpected VER bits: 0x%0X\n", id);
|
2014-09-01 01:04:02 +02:00
|
|
|
kmm_free(dev);
|
2013-10-27 15:00:16 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-12 17:26:00 +02:00
|
|
|
audinfo("VS1053 Detected!\n");
|
2013-10-27 15:00:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach our ISR to this device */
|
|
|
|
|
2017-02-27 17:44:13 +01:00
|
|
|
dev->hw_lower->attach(dev->hw_lower, vs1053_dreq_isr, dev);
|
2013-10-27 15:00:16 +01:00
|
|
|
|
2017-02-27 17:44:13 +01:00
|
|
|
/* Do some initialization of the codec */
|
2013-10-27 15:00:16 +01:00
|
|
|
|
|
|
|
vs1053_shutdown(&dev->lower); /* Go to shutdown state */
|
2013-05-19 23:12:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &dev->lower;
|
|
|
|
}
|