ST / X_NUCLEO_CCA01M1

Dependencies:   ST_I2S X_NUCLEO_COMMON

Dependents:   HelloWorld_CCA01M1 HelloWorld_CCA01M1_mbedOS HelloWorld_CCA01M1_mbedOS Karaoke_CCA01M1_CCA02M1_mbedOS ... more

Fork of X_NUCLEO_CCA01M1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers STA350BW.h Source File

STA350BW.h

Go to the documentation of this file.
00001 /**
00002 ******************************************************************************
00003 * @file    STA350BW.h
00004 * @author  Central Labs
00005 * @version V1.0.0
00006 * @date    18-August-2015
00007 * @brief   This file provides the STA350BW SOUND TERMINAL audio driver.
00008 ******************************************************************************
00009 * @attention
00010 *
00011 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00012 *
00013 * Redistribution and use in source and binary forms, with or without modification,
00014 * are permitted provided that the following conditions are met:
00015 *   1. Redistributions of source code must retain the above copyright notice,
00016 *      this list of conditions and the following disclaimer.
00017 *   2. Redistributions in binary form must reproduce the above copyright notice,
00018 *      this list of conditions and the following disclaimer in the documentation
00019 *      and/or other materials provided with the distribution.
00020 *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021 *      may be used to endorse or promote products derived from this software
00022 *      without specific prior written permission.
00023 *
00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 ******************************************************************************
00036 */
00037 
00038 
00039 /* Generated with STM32CubeTOO -----------------------------------------------*/
00040 
00041 
00042 /* Revision ------------------------------------------------------------------*/
00043 /*
00044     Repository:       http://svn.x-nucleodev.codex.cro.st.com/svnroot/X-NucleoDev
00045     Branch/Trunk/Tag: trunk
00046     Based on:         X-CUBE-SOUNDTER1/trunk/Drivers/BSP/Components/STA350BW/STA350BW.h
00047     Revision:         0
00048 */
00049 
00050 
00051 /* Define to prevent recursive inclusion -------------------------------------*/
00052 
00053 #ifndef __STA350BW_CLASS_H
00054 #define __STA350BW_CLASS_H
00055 
00056 
00057 /* Configuration -------------------------------------------------------------*/
00058 
00059 /**
00060  * Enable to debug the duration of functions related to the I2S and USB usage
00061  * through an oscilloscope.
00062  */
00063 //#define X_NUCLEO_CCA01M1_DEBUG
00064 
00065 
00066 /* Includes ------------------------------------------------------------------*/
00067 
00068 /* ACTION 1 ------------------------------------------------------------------*
00069  * Include here platform specific header files.                               *
00070  *----------------------------------------------------------------------------*/        
00071 #include "mbed.h"
00072 #include "I2S.h"
00073 #include "DevI2C.h"
00074 /* ACTION 2 ------------------------------------------------------------------*
00075  * Include here component specific header files.                              *
00076  *----------------------------------------------------------------------------*/        
00077 #include "STA350BW_def.h"
00078 /* ACTION 3 ------------------------------------------------------------------*
00079  * Include here interface specific header files.                              *
00080  *                                                                            *
00081  * Example:                                                                   *
00082  *   #include "Humidity_class.h"                                              *
00083  *   #include "Temperature_class.h"                                           *
00084  *----------------------------------------------------------------------------*/
00085 #include "SoundTerminal.h"
00086 
00087 
00088 /* Definitions ---------------------------------------------------------------*/
00089 
00090 #define MIN_VOLUME   0
00091 #define MAX_VOLUME 128
00092 
00093 
00094 /* Classes -------------------------------------------------------------------*/
00095 
00096 /**
00097  * @brief Class representing a STA350BW component.
00098  */
00099 class STA350BW : public SoundTerminal
00100 {
00101 public:
00102 
00103     /*** Constructor and Destructor Methods ***/
00104 
00105     /**
00106      * @brief Constructor.
00107      * @param reset   pin name of the RESET pin of the component.
00108      * @param address I2C address of the component.
00109      * @param i2c     I2C device to be used for communication.
00110      * @param dpin    pin name of the DPIN pin of the I2S device to be used for audio transmission.
00111      * @param clk     pin name of the CLK pin of the I2S device to be used for audio transmission.
00112      * @param wsel    pin name of the WSEL pin of the I2S device to be used for audio transmission.
00113      * @param fdpin   pin name of the FDPIN pin of the I2S device to be used for audio transmission.
00114      * @param mck     pin name of the MCK pin of the I2S device to be used for audio transmission.
00115      * @note  Initialization depends on the I2S interface you wish to use:
00116      *          I2S1) address = STA350BW_ADDRESS_1, dpin = PB_15, clk = PB_13, wsel = PB_12, fdpin = NC, mck = PC_6;
00117      *          I2S2) address = STA350BW_ADDRESS_2, dpin = PC_12, clk = PC_10, wsel = PA_4, fdpin = NC, mck = PC_7.
00118      */
00119     STA350BW(PinName reset, uint8_t address, DevI2C &i2c, PinName dpin, PinName clk, PinName wsel, PinName fdpin = NC, PinName mck = NC) :
00120         SoundTerminal(),
00121         _reset(reset),
00122         _address(address),
00123         _dev_i2c(i2c),
00124         dev_i2s(dpin, clk, wsel, fdpin, mck),
00125         _PCM_buffer(NULL),
00126         _PCM_buffer_bytes(0),
00127         _loop(false)
00128 #ifdef X_NUCLEO_CCA01M1_DEBUG
00129         , _i2s_signal(D11)
00130 #endif
00131     {
00132         /* ACTION 4 ----------------------------------------------------------*
00133          * Initialize here the component's member variables, one variable per *
00134          * line.                                                              *
00135          *                                                                    *
00136          * Example:                                                           *
00137          *   measure = 0;                                                     *
00138          *   instance_id = number_of_instances++;                             *
00139          *--------------------------------------------------------------------*/
00140     }
00141     
00142     /**
00143      * @brief Destructor.
00144      */
00145     virtual ~STA350BW(void) {}
00146     
00147 
00148     /*** Public Component Related Methods ***/
00149 
00150     /* ACTION 5 --------------------------------------------------------------*
00151      * Implement here the component's public methods, as wrappers of the C    *
00152      * component's functions.                                                 *
00153      * They should be:                                                        *
00154      *   + Methods with the same name of the C component's virtual table's    *
00155      *     functions (1);                                                     *
00156      *   + Methods with the same name of the C component's extended virtual   *
00157      *     table's functions, if any (2).                                     *
00158      *                                                                        *
00159      * Example:                                                               *
00160      *   virtual int GetValue(float *f)  //(1)                                *
00161      *   {                                                                    *
00162      *     return COMPONENT_GetValue(float *f);                               *
00163      *   }                                                                    *
00164      *                                                                        *
00165      *   virtual int EnableFeature(void) //(2)                                *
00166      *   {                                                                    *
00167      *     return COMPONENT_EnableFeature();                                  *
00168      *   }                                                                    *
00169      *------------------------------------------------------------------------*/
00170     /**
00171     * @brief  Initializing the STA350BW component.
00172     * @param  init Pointer to device specific initalization structure.
00173     * @retval "0" in case of success, an error code otherwise.
00174     */
00175     virtual int init(void *init = NULL)
00176     {
00177         if (STA350BW_Init((void *) init) != COMPONENT_OK)
00178             return COMPONENT_ERROR;
00179 
00180         /* Setting I2S parameters. */
00181         dev_i2s.mode(MASTER_TX, true);
00182 
00183         return COMPONENT_OK;
00184     }
00185 
00186     /**
00187      * @brief  Getting the ID of the component.
00188      * @param  id Pointer to an allocated variable to store the ID into.
00189      * @retval "0" in case of success, an error code otherwise.
00190      */
00191     virtual int read_id(uint8_t *id = NULL)
00192     {
00193         return (int) STA350BW_ReadID((uint8_t *) id);
00194     }
00195 
00196     /**
00197     * @brief  De-initializing the STA350BW component.
00198     * @param  None.
00199     * @retval "0" in case of success, an error code otherwise.
00200     */
00201     virtual int32_t de_init(void)
00202     {
00203         return (int32_t) STA350BW_DeInit();
00204     }
00205 
00206     /*
00207      * @brief  Start playing audio.
00208      * @param  PCM_buffer       The buffer of data to be played.
00209      * @param  PCM_buffer_bytes The size in bytes of the buffer of data to be played.
00210      * @param  loop             Loops indefinitely if true, just once otherwise.
00211      * @retval "0" in case of success, an error code otherwise.
00212      */
00213     virtual int32_t play(int16_t *PCM_buffer, uint16_t PCM_buffer_bytes, bool loop = false)
00214     {
00215         /* Storing data. */
00216         _PCM_buffer = PCM_buffer;
00217         _PCM_buffer_bytes = PCM_buffer_bytes;
00218         _loop = loop;
00219 
00220         /* Sending data to the speakers via I2S. */
00221         int res = dev_i2s.transfer(
00222             (void *) _PCM_buffer, _PCM_buffer_bytes,
00223             (void *) NULL, 0,
00224             event_callback_t(this, &STA350BW::i2s_callback),
00225             I2S_EVENT_ALL
00226         );
00227         if (res != 0)
00228             return COMPONENT_ERROR;
00229 
00230         return COMPONENT_OK;
00231     }
00232 
00233     /*
00234      * @brief  Stop playing audio.
00235      * @param  None.
00236      * @retval None.
00237      */
00238     virtual int32_t stop(void)
00239     {
00240         dev_i2s.abort_all_transfers();
00241         return (int32_t) STA350BW_Stop();
00242     }
00243 
00244     /*
00245      * @brief  Set the volume of the audio.
00246      * @param  channel The channel on which to set the volume of the audio.
00247      * @param  value   The value of the volume in the range [MIN_VOLUME..MAX_VOLUME].
00248      * @retval "0" in case of success, an error code otherwise.
00249      */
00250     virtual int32_t set_volume(uint8_t channel, uint8_t value)
00251     {
00252         if (!(value >= MIN_VOLUME && value <= MAX_VOLUME))
00253             return COMPONENT_ERROR;
00254 
00255         return (int32_t) STA350BW_SetVolume((uint8_t) channel, (uint8_t) MAX_VOLUME - value);
00256     }
00257 
00258     /*
00259      * @brief  Set the frequency of the audio.
00260      * @param  audio_freq The frequency of the audio.
00261      * @retval "0" in case of success, an error code otherwise.
00262      */
00263     virtual int32_t set_frequency(uint32_t audio_freq)
00264     {
00265         return (int32_t) STA350BW_SetFrequency((uint32_t) audio_freq);
00266     }
00267 
00268 
00269     /*** Public Interrupt Related Methods ***/
00270 
00271     /* ACTION 6 --------------------------------------------------------------*
00272      * Implement here interrupt related methods, if any.                      *
00273      * Note that interrupt handling is platform dependent, e.g.:              *
00274      *   + mbed:                                                              *
00275      *     InterruptIn feature_irq(pin);           //Interrupt object.        *
00276      *     feature_irq.fall(callback);             //Attach a callback.       *
00277      *     feature_irq.mode(PullNone);             //Set interrupt mode.      *
00278      *     feature_irq.enable_irq();               //Enable interrupt.        *
00279      *     feature_irq.disable_irq();              //Disable interrupt.       *
00280      *   + Arduino:                                                           *
00281      *     attachInterrupt(pin, callback, RISING); //Attach a callback.       *
00282      *     detachInterrupt(pin);                   //Detach a callback.       *
00283      *                                                                        *
00284      * Example (mbed):                                                        *
00285      *   void AttachFeatureIRQ(void (*fptr) (void))                           *
00286      *   {                                                                    *
00287      *     feature_irq.fall(fptr);                                            *
00288      *   }                                                                    *
00289      *                                                                        *
00290      *   void EnableFeatureIRQ(void)                                          *
00291      *   {                                                                    *
00292      *     feature_irq.enable_irq();                                          *
00293      *   }                                                                    *
00294      *                                                                        *
00295      *   void DisableFeatureIRQ(void)                                         *
00296      *   {                                                                    *
00297      *     feature_irq.disable_irq();                                         *
00298      *   }                                                                    *
00299      *------------------------------------------------------------------------*/
00300 
00301 
00302 protected:
00303 
00304     /*** Protected Component Related Methods ***/
00305 
00306     /* ACTION 7 --------------------------------------------------------------*
00307      * Declare here the component's specific methods.                         *
00308      * They should be:                                                        *
00309      *   + Methods with the same name of the C component's virtual table's    *
00310      *     functions (1);                                                     *
00311      *   + Methods with the same name of the C component's extended virtual   *
00312      *     table's functions, if any (2);                                     *
00313      *   + Helper methods, if any, like functions declared in the component's *
00314      *     source files but not pointed by the component's virtual table (3). *
00315      *                                                                        *
00316      * Example:                                                               *
00317      *   status_t COMPONENT_GetValue(float *f);   //(1)                       *
00318      *   status_t COMPONENT_EnableFeature(void);  //(2)                       *
00319      *   status_t COMPONENT_ComputeAverage(void); //(3)                       *
00320      *------------------------------------------------------------------------*/
00321     int32_t STA350BW_Init(void *init);
00322     int32_t STA350BW_ReadID(uint8_t *id);
00323     int32_t STA350BW_DeInit(void);
00324     int32_t STA350BW_Play(int16_t *pData, uint16_t Size);
00325     int32_t STA350BW_Stop(void);
00326     int32_t STA350BW_Pause(void);
00327     int32_t STA350BW_Resume(void);
00328     int32_t STA350BW_SetVolume(uint8_t channel, uint8_t value);
00329     int32_t STA350BW_SetFrequency(uint32_t audio_freq);
00330     int32_t STA350BW_PowerOn(void);
00331     int32_t STA350BW_PowerOff(void);
00332     int32_t STA350BW_Reset(void);
00333     int32_t STA350BW_SetEq(uint8_t ram_block, uint8_t filter_number, uint32_t * filter_values);
00334     int32_t STA350BW_SetTone(uint8_t tone_gain);
00335     int32_t STA350BW_SetMute(uint8_t channel, uint8_t state);
00336     int32_t STA350BW_SetDSPOption(uint8_t option, uint8_t state);
00337     int32_t STA350BW_WriteRAMSet(uint8_t RAM_block, uint8_t RAM_address, uint8_t * pIn);
00338 
00339     /**
00340      * @brief  I2S callback.
00341      * @param  narg Narg flag.
00342      * @retval None.
00343      */
00344     void i2s_callback(int narg)
00345     {
00346         if (!(narg & (I2S_EVENT_TX_COMPLETE | I2S_EVENT_TX_HALF_COMPLETE)))
00347             error("Unexpected transmission event.\r\n");
00348         else if ((narg & I2S_EVENT_TX_COMPLETE) && !_loop)
00349             stop();
00350 
00351 #ifdef X_NUCLEO_CCA01M1_DEBUG
00352         _i2s_signal = !_i2s_signal;
00353 #endif
00354     }
00355 
00356 
00357     /*** Component's I/O Methods ***/
00358 
00359     /**
00360      * @brief      Utility function to read data from STA350BW.
00361      * @param[out] pBuffer pointer to the buffer to read data into.
00362      * @param[in]  RegisterAddr specifies the internal address register to read from.
00363      * @param[in]  NumBytesToRead number of bytes to read.
00364      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00365      */
00366     status_t read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumBytesToRead)
00367     {
00368         if (_dev_i2c.i2c_read(pBuffer, _address, RegisterAddr, NumBytesToRead) != 0)
00369             return COMPONENT_ERROR;
00370         return COMPONENT_OK;
00371     }
00372     
00373     /**
00374      * @brief      Utility function to write data to STA350BW.
00375      * @param[in]  pBuffer pointer to the buffer of data to send.
00376      * @param[in]  RegisterAddr specifies the internal address register to write to.
00377      * @param[in]  NumBytesToWrite number of bytes to write.
00378      * @retval     COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00379      */
00380     status_t write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumBytesToWrite)
00381     {
00382         if (_dev_i2c.i2c_write(pBuffer, _address, RegisterAddr, NumBytesToWrite) != 0)
00383             return COMPONENT_ERROR;
00384         return COMPONENT_OK;
00385     }
00386 
00387     /* ACTION 8 --------------------------------------------------------------*
00388      * Implement here other I/O methods beyond those already implemented      *
00389      * above, which are declared extern within the component's header file.   *
00390      *------------------------------------------------------------------------*/
00391     uint8_t STA350BW_IO_Init(void)
00392     {
00393         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00394         return (uint8_t) 0;
00395     }
00396 
00397     uint8_t STA350BW_IO_Read(uint8_t reg, uint8_t *value)
00398     {
00399         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00400         return (uint8_t) read(value, reg, 1);
00401     }
00402 
00403     uint8_t STA350BW_IO_Write(uint8_t reg, uint8_t value)
00404     {
00405         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00406         return (uint8_t) write(&value, reg, 1);
00407     }
00408 
00409     uint8_t STA350BW_IO_ReadMulti(uint8_t *pBuffer, uint8_t reg, uint16_t length)
00410     {
00411         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00412         return (uint8_t) read(pBuffer, reg, length);
00413     }
00414 
00415     uint8_t STA350BW_IO_WriteMulti(uint8_t *pBuffer, uint8_t reg, uint16_t length)
00416     {
00417         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00418         return (uint8_t) write(pBuffer, reg, length);
00419     }
00420 
00421     uint8_t STA350BW_IO_Delay(uint32_t delay_ms)
00422     {
00423         /* TO BE IMPLEMENTED BY USING TARGET PLATFORM'S APIs. */
00424         wait_ms(delay_ms);
00425         return (uint8_t) 0;
00426     }
00427 
00428 
00429     /*** Component's Instance Variables ***/
00430 
00431     /* ACTION 9 --------------------------------------------------------------*
00432      * Declare here interrupt related variables, if needed.                   *
00433      * Note that interrupt handling is platform dependent, see                *
00434      * "Interrupt Related Methods" above.                                     *
00435      *                                                                        *
00436      * Example:                                                               *
00437      *   + mbed:                                                              *
00438      *     InterruptIn feature_irq;                                           *
00439      *------------------------------------------------------------------------*/
00440 
00441     /* ACTION 10 -------------------------------------------------------------*
00442      * Declare here other pin related variables, if needed.                   *
00443      *                                                                        *
00444      * Example:                                                               *
00445      *   + mbed:                                                              *
00446      *     DigitalOut standby_reset;                                          *
00447      *------------------------------------------------------------------------*/
00448     DigitalOut _reset;
00449 
00450     /* ACTION 11 -------------------------------------------------------------*
00451      * Declare here communication related variables, if needed.               *
00452      *                                                                        *
00453      * Example:                                                               *
00454      *   + mbed:                                                              *
00455      *     DigitalOut address;                                                *
00456      *     DevI2C &dev_i2c;                                                   *
00457      *------------------------------------------------------------------------*/
00458     /* Configuration. */
00459     uint8_t _address;
00460 
00461     /* IO Device. */
00462     DevI2C &_dev_i2c;
00463 
00464 public:
00465 
00466     /* I2S Audio Device (exclusive). */
00467     I2S dev_i2s;
00468 
00469 
00470 protected:
00471 
00472     /* ACTION 12 -------------------------------------------------------------*
00473      * Declare here identity related variables, if needed.                    *
00474      * Note that there should be only a unique identifier for each component, *
00475      * which should be the "who_am_i" parameter.                              *
00476      *------------------------------------------------------------------------*/
00477 
00478     /* ACTION 13 -------------------------------------------------------------*
00479      * Declare here the component's static and non-static data, one variable  *
00480      * per line.                                                              *
00481      *                                                                        *
00482      * Example:                                                               *
00483      *   float measure;                                                       *
00484      *   int instance_id;                                                     *
00485      *   static int number_of_instances;                                      *
00486      *------------------------------------------------------------------------*/
00487     /* Buffer of data to be played. */
00488     int16_t *_PCM_buffer;
00489     /* The size in bytes of the buffer of data to be played. */
00490     uint16_t _PCM_buffer_bytes;
00491     /* Loops indefinitely if true, just once otherwise. */
00492     bool _loop;
00493 
00494 #ifdef X_NUCLEO_CCA01M1_DEBUG
00495     /* Signals for debugging purposes. */
00496     DigitalOut _i2s_signal;
00497 #endif
00498 };
00499 
00500 #endif /* __STA350BW_CLASS_H */
00501 
00502 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/