Page 2 of 2

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Tue Mar 13, 2012 3:56 pm
by Giovanni
Hi,

It will work much like the SPI driver but it will also have a continuous mode like the ADC driver. This is the current status:

Code: Select all

/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT 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 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    i2s.c
 * @brief   I2S Driver code.
 *
 * @addtogroup I2S
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_I2S || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   I2S Driver initialization.
 * @note    This function is implicitly invoked by @p halInit(), there is
 *          no need to explicitly initialize the driver.
 *
 * @init
 */
void i2sInit(void) {

  i2s_lld_init();
}

/**
 * @brief   Initializes the standard part of a @p I2SDriver structure.
 *
 * @param[out] i2sp     pointer to the @p I2SDriver object
 *
 * @init
 */
void i2sObjectInit(I2SDriver *i2sp) {

  i2sp->state  = I2S_STOP;
  i2sp->config = NULL;
}

/**
 * @brief   Configures and activates the I2S peripheral.
 *
 * @param[in] i2sp      pointer to the @p I2SDriver object
 * @param[in] config    pointer to the @p I2SConfig object
 *
 * @api
 */
void i2sStart(I2SDriver *i2sp, const I2SConfig *config) {

  chDbgCheck((i2sp != NULL) && (config != NULL), "i2sStart");

  chSysLock();
  chDbgAssert((i2sp->state == I2S_STOP) || (i2sp->state == I2S_READY),
              "i2sStart(), #1", "invalid state");
  i2sp->config = config;
  i2s_lld_start(i2sp);
  i2sp->state = I2S_READY;
  chSysUnlock();
}

/**
 * @brief   Deactivates the I2S peripheral.
 *
 * @param[in] i2sp      pointer to the @p I2SDriver object
 *
 * @api
 */
void i2sStop(I2SDriver *i2sp) {

  chDbgCheck(i2sp != NULL, "i2sStop");

  chSysLock();
  chDbgAssert((i2sp->state == I2S_STOP) || (i2sp->state == I2S_READY),
              "i2sStop(), #1", "invalid state");
  i2s_lld_stop(i2sp);
  i2sp->state = I2S_STOP;
  chSysUnlock();
}

/**
 * @brief   Starts a I2S transmission.
 * @details The transmission is started and it is executes until the specified
 *          buffer has entirely transmitted.
 * @post    A callback is invoked when the buffer has been fully transmitted.
 *
 * @param[in] i2sp      pointer to the @p I2SDriver object
 * @param[in] n         number of samples to send
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @api
 */
void i2sStartSendOnce(I2SDriver *i2sp, size_t n,
                      const i2ssample_t *txbuf) {

  chDbgCheck((i2sp != NULL) && (n > 0) && (txbuf != NULL),
             "i2sStartSendOnce");

  chSysLock();
  chDbgAssert(i2sp->state == I2S_READY, "i2sStartSendOnce(), #1",
              "not ready");
  i2sStartSendOnceI(i2sp, n, txbuf);
  chSysUnlock();
}

/**
 * @brief   Starts a continuous I2S transmission.
 * @details The transmission is started and it is executes continuously
 *          until @p i2sStopSend() has been explicitly invoked .
 * @post    A callback is invoked when the buffer has been half transmitted
 *          and fully transmitted.
 *
 * @param[in] i2sp      pointer to the @p I2SDriver object
 * @param[in] n         number of samples to send
 * @param[in] txbuf     the pointer to the transmit buffer
 *
 * @api
 */
void i2sStartSendContinuous(I2SDriver *i2sp, size_t n,
                            const i2ssample_t *txbuf) {

  chDbgCheck((i2sp != NULL) && (n > 0) && (txbuf != NULL),
             "i2sStartSendContinuous");

  chSysLock();
  chDbgAssert(i2sp->state == I2S_READY, "i2sStartSendContinuous(), #1",
              "not ready");
  i2sStartSendContinuousI(i2sp, n, txbuf);
  chSysUnlock();
}

/**
 * @brief   Stops the ongoing transmission.
 * @details The ongoing transmission, if any, is stopped, if the driver
 *          was not transmitting the function does nothing.
 *
 * @param[in] i2sp      pointer to the @p I2SDriver object
 *
 * @api
 */
void i2sStopSend(I2SDriver *i2sp) {

  chDbgCheck((i2sp != NULL), "i2sStopSend");

  chSysLock();
  chDbgAssert((i2sp->state == I2S_READY) ||
              (i2sp->state == I2S_ACTIVE) ||
              (i2sp->state == I2S_COMPLETE),
              "i2sStopSend(), #1", "not ready");
  i2sStopSendI(i2sp);
  chSysUnlock();
}

#endif /* HAL_USE_I2S */

/** @} */


Of course it is very preliminary.

Giovanni

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Wed Mar 14, 2012 2:19 am
by watz
I2SConfig holds all the secrets :D Any details available ?

I switched to circular tx dma buffer usage for I2S. Again, unbelievably, it worked right away. No more random stuttering on SD reads :-)
Once I cleaned up the mess I'll post the Project somewhere online. It might be a good starting point for ppl until ChibiOS has official I2S support and driver for the CS43L22 DAC.

I'd like to know the CPU usage of the decoding thread. I have the feeling that this MCU can do much more besides decoding that MP3. In ~1997, only the brand new Winamp was able to play MP3s without stuttering at almost 100% CPU on my AMD K5 PR133 (100Mhz). Funny.

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Wed Mar 14, 2012 8:49 am
by Giovanni
It is work in progress, I do most of the work during weekends so expect new commits before next week.

Giovanni

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Sat Jun 30, 2012 4:58 am
by Abhishek
Hello,

I was just about to open a new topic for audio playback support in ChibiOS. But I did some work lately on the Discovery board and am able to play back wave files well without issues using double buffering, and it also has a working QVGA LCD display and UI on top of it, and I have been working (and will continue to be working) on the ChibiOS GLCD and UI Lib for quite some time.

I've used DMA directly and am not using the SPI drivers as of now. I access SPI HW registers directly to configure I2S and use the STM32 DMA API to send audio to the codec. Even no STM32 Standard Peripherals lib or the 'official' codec driver, and I found that the resultant code was much cleaner.

Also, I implemented beep generator support for CS43L22 DAC. However it requires MCLK on the input, otherwise no sound :(

Have a look at my code, specifically the wavePlayer.c, codec_CS43L22.c files on the repo : https://github.com/abhishek-kakkar/STM3 ... overy_demo , and my project in the user projects section.

I plan to support any generic codec in the future, not just CS43L22. Currently planned to add TLV320AIC23B, WM8731 among others.

Could we continue discussions here, or should I start a separate topic?

Abhishek

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Sat Jun 30, 2012 6:25 am
by Giovanni
Hi, as you prefer, you may open a new topic in "User Projects" or continue here.

Giovanni

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Sat Jun 30, 2012 5:16 pm
by Abhishek
I downloaded the latest copy of the SVN trunk of the development version of ChibiOS. Took a look at the I2S drivers, it looks that the groundwork is done. The actual data transfer code seems to be missing.

Also, I don't know if this is right, but I guess instead of providing I2SPR info in the config, we could provide sample rate and number of bits info? Or should it be a part of the high level audio playback framework.

I had tried data transfer via polling for I2S for my project, but it doesn't make sense at all. There is a lot of static in the output if it is done so. So IMHO we should only consider DMA transfers only for the LLD implementation of the driver. I can currently test on F4 only, specifically my F4D board.

Also, I would like to ask what needs to be done to support I2S Slave mode on the STM32. On F1 mainstream devices (except connectivity-line devices using an audio-class 14.7456MHz crystal), there appears to be a lot of error if MCLK support is enabled. We could configure the micro to provide MCO @ 12MHz (standard for USB devices, almost all codec support this frequency), and then the codec could provide the needed clock and data just needs to be available and shifted out.

I guess audio playback and I2S support are interdependent, so that we cannot code them independently. We would need to work out a way for both to communicate, specifically, make the DMA half transfer and transfer complete callbacks compulsory and handle them. I will do a circular buffer implementation for audio playback to replace my current double-buffer mechanism as it still leads to some random glitches when running continuously for say, half an hour. I also need to move the microSD card from SPI1 onto SDIO for improving the overall speed, I don't like the F4 doing 1.5 MB/s on file system reads.

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Sun Jul 01, 2012 6:58 am
by Abhishek
I'm now going to add some code to the currently empty exchange, using my files. It'd currently be write only, I guess. I had used the SPI driver flags as a starting point, will change them to the I2S constants that have been defined.

Also should i2s_lld_stop_exchange just disable the DMA request flag in the SPI CR2 register (which I am doing right now), or disable the DMA stream altogether?

Abhishek

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Sun Jul 01, 2012 8:28 am
by Giovanni
Hi, just disable the channel but do not release it. The release is done in xxxStop() usually, see the SPI driver for an example.

Giovanni

I2S Support

Posted: Wed Jan 29, 2014 5:24 pm
by grubi
Hi!
I'm working on a project with the STM32f4. It uses a lot of the chips peripherals (LAN, SPI, I2S, ...). I already got many examples working, but I still haven't found a good solution for the I2S (I need it to communicate with a DAC). At the time I'm using chibiOS 2.6.2. I found that there're already some I2S on the Repository version. Has somebody a running I2S example. Will there be a finished I2S driver in 2.6.x?

Re: STM32F4 Discovery - How to set up SPI3 for I2S

Posted: Wed Jan 29, 2014 7:27 pm
by Giovanni
Hi,

There is no official I2S driver implementation yet but there is a contributed code (by Abhishek) you could use.

Giovanni