123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- /*!
- \file gd32f30x_spi.c
- \brief SPI driver
- \version 2017-02-10, V1.0.0, firmware for GD32F30x
- \version 2018-10-10, V1.1.0, firmware for GD32F30x
- \version 2018-12-25, V2.0.0, firmware for GD32F30x
- \version 2020-09-30, V2.1.0, firmware for GD32F30x
- */
- /*
- Copyright (c) 2020, GigaDevice Semiconductor Inc.
- Redistribution and use in source and binary forms, with or without modification,
- are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holder nor the names of its contributors
- may be used to endorse or promote products derived from this software without
- specific prior written permission.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
- OF SUCH DAMAGE.
- */
- #include "gd32f30x_spi.h"
- #define SPI_ERROR_HANDLE(s) do{}while(1)
- /* SPI/I2S parameter initialization mask */
- #define SPI_INIT_MASK ((uint32_t)0x00003040U) /*!< SPI parameter initialization mask */
- #define I2S_INIT_MASK ((uint32_t)0x0000F047U) /*!< I2S parameter initialization mask */
- /* default value */
- #define SPI_I2SPSC_DEFAULT_VALUE ((uint32_t)0x00000002U) /*!< default value of SPI_I2SPSC register */
- /* I2S clock source selection, multiplication and division mask */
- #define I2S1_CLOCK_SEL ((uint32_t)0x00020000U) /*!< I2S1 clock source selection */
- #define I2S2_CLOCK_SEL ((uint32_t)0x00040000U) /*!< I2S2 clock source selection */
- #define I2S_CLOCK_MUL_MASK ((uint32_t)0x0000F000U) /*!< I2S clock multiplication mask */
- #define I2S_CLOCK_DIV_MASK ((uint32_t)0x000000F0U) /*!< I2S clock division mask */
- /*!
- \brief reset SPI and I2S
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_i2s_deinit(uint32_t spi_periph)
- {
- switch(spi_periph){
- case SPI0:
- /* reset SPI0 */
- rcu_periph_reset_enable(RCU_SPI0RST);
- rcu_periph_reset_disable(RCU_SPI0RST);
- break;
- case SPI1:
- /* reset SPI1 and I2S1 */
- rcu_periph_reset_enable(RCU_SPI1RST);
- rcu_periph_reset_disable(RCU_SPI1RST);
- break;
- case SPI2:
- /* reset SPI2 and I2S2 */
- rcu_periph_reset_enable(RCU_SPI2RST);
- rcu_periph_reset_disable(RCU_SPI2RST);
- break;
- default :
- break;
- }
- }
- /*!
- \brief initialize the parameters of SPI struct with default values
- \param[in] none
- \param[out] spi_parameter_struct: the initialized struct spi_parameter_struct pointer
- \retval none
- */
- void spi_struct_para_init(spi_parameter_struct *spi_struct)
- {
- /* configure the structure with default value */
- spi_struct->device_mode = SPI_SLAVE;
- spi_struct->trans_mode = SPI_TRANSMODE_FULLDUPLEX;
- spi_struct->frame_size = SPI_FRAMESIZE_8BIT;
- spi_struct->nss = SPI_NSS_HARD;
- spi_struct->clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
- spi_struct->prescale = SPI_PSC_2;
- spi_struct->endian = SPI_ENDIAN_MSB;
- }
- /*!
- \brief initialize SPI parameter
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] spi_struct: SPI parameter initialization stuct members of the structure
- and the member values are shown as below:
- device_mode: SPI_MASTER, SPI_SLAVE
- trans_mode: SPI_TRANSMODE_FULLDUPLEX, SPI_TRANSMODE_RECEIVEONLY,
- SPI_TRANSMODE_BDRECEIVE, SPI_TRANSMODE_BDTRANSMIT
- frame_size: SPI_FRAMESIZE_16BIT, SPI_FRAMESIZE_8BIT
- nss: SPI_NSS_SOFT, SPI_NSS_HARD
- endian: SPI_ENDIAN_MSB, SPI_ENDIAN_LSB
- clock_polarity_phase: SPI_CK_PL_LOW_PH_1EDGE, SPI_CK_PL_HIGH_PH_1EDGE
- SPI_CK_PL_LOW_PH_2EDGE, SPI_CK_PL_HIGH_PH_2EDGE
- prescale: SPI_PSC_n (n=2,4,8,16,32,64,128,256)
- \param[out] none
- \retval none
- */
- void spi_init(uint32_t spi_periph, spi_parameter_struct* spi_struct)
- {
- uint32_t reg = 0U;
- reg = SPI_CTL0(spi_periph);
- reg &= SPI_INIT_MASK;
- /* select SPI as master or slave */
- reg |= spi_struct->device_mode;
- /* select SPI transfer mode */
- reg |= spi_struct->trans_mode;
- /* select SPI frame size */
- reg |= spi_struct->frame_size;
- /* select SPI NSS use hardware or software */
- reg |= spi_struct->nss;
- /* select SPI LSB or MSB */
- reg |= spi_struct->endian;
- /* select SPI polarity and phase */
- reg |= spi_struct->clock_polarity_phase;
- /* select SPI prescale to adjust transmit speed */
- reg |= spi_struct->prescale;
- /* write to SPI_CTL0 register */
- SPI_CTL0(spi_periph) = (uint32_t)reg;
- SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SSEL);
- }
- /*!
- \brief enable SPI
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_enable(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SPIEN;
- }
- /*!
- \brief disable SPI
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_disable(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SPIEN);
- }
- /*!
- \brief initialize I2S parameter
- \param[in] spi_periph: SPIx(x=1,2)
- \param[in] i2s_mode: I2S operation mode
- only one parameter can be selected which is shown as below:
- \arg I2S_MODE_SLAVETX: I2S slave transmit mode
- \arg I2S_MODE_SLAVERX: I2S slave receive mode
- \arg I2S_MODE_MASTERTX: I2S master transmit mode
- \arg I2S_MODE_MASTERRX: I2S master receive mode
- \param[in] i2s_standard: I2S standard
- only one parameter can be selected which is shown as below:
- \arg I2S_STD_PHILLIPS: I2S phillips standard
- \arg I2S_STD_MSB: I2S MSB standard
- \arg I2S_STD_LSB: I2S LSB standard
- \arg I2S_STD_PCMSHORT: I2S PCM short standard
- \arg I2S_STD_PCMLONG: I2S PCM long standard
- \param[in] i2s_ckpl: I2S idle state clock polarity
- only one parameter can be selected which is shown as below:
- \arg I2S_CKPL_LOW: I2S clock polarity low level
- \arg I2S_CKPL_HIGH: I2S clock polarity high level
- \param[out] none
- \retval none
- */
- void i2s_init(uint32_t spi_periph, uint32_t i2s_mode, uint32_t i2s_standard, uint32_t i2s_ckpl)
- {
- uint32_t reg= 0U;
- reg = SPI_I2SCTL(spi_periph);
- reg &= I2S_INIT_MASK;
- /* enable I2S mode */
- reg |= (uint32_t)SPI_I2SCTL_I2SSEL;
- /* select I2S mode */
- reg |= (uint32_t)i2s_mode;
- /* select I2S standard */
- reg |= (uint32_t)i2s_standard;
- /* select I2S polarity */
- reg |= (uint32_t)i2s_ckpl;
- /* write to SPI_I2SCTL register */
- SPI_I2SCTL(spi_periph) = (uint32_t)reg;
- }
- /*!
- \brief configure I2S prescaler
- \param[in] spi_periph: SPIx(x=1,2)
- \param[in] i2s_audiosample: I2S audio sample rate
- only one parameter can be selected which is shown as below:
- \arg I2S_AUDIOSAMPLE_8K: audio sample rate is 8KHz
- \arg I2S_AUDIOSAMPLE_11K: audio sample rate is 11KHz
- \arg I2S_AUDIOSAMPLE_16K: audio sample rate is 16KHz
- \arg I2S_AUDIOSAMPLE_22K: audio sample rate is 22KHz
- \arg I2S_AUDIOSAMPLE_32K: audio sample rate is 32KHz
- \arg I2S_AUDIOSAMPLE_44K: audio sample rate is 44KHz
- \arg I2S_AUDIOSAMPLE_48K: audio sample rate is 48KHz
- \arg I2S_AUDIOSAMPLE_96K: audio sample rate is 96KHz
- \arg I2S_AUDIOSAMPLE_192K: audio sample rate is 192KHz
- \param[in] i2s_frameformat: I2S data length and channel length
- only one parameter can be selected which is shown as below:
- \arg I2S_FRAMEFORMAT_DT16B_CH16B: I2S data length is 16 bit and channel length is 16 bit
- \arg I2S_FRAMEFORMAT_DT16B_CH32B: I2S data length is 16 bit and channel length is 32 bit
- \arg I2S_FRAMEFORMAT_DT24B_CH32B: I2S data length is 24 bit and channel length is 32 bit
- \arg I2S_FRAMEFORMAT_DT32B_CH32B: I2S data length is 32 bit and channel length is 32 bit
- \param[in] i2s_mckout: I2S master clock output
- only one parameter can be selected which is shown as below:
- \arg I2S_MCKOUT_ENABLE: I2S master clock output enable
- \arg I2S_MCKOUT_DISABLE: I2S master clock output disable
- \param[out] none
- \retval none
- */
- void i2s_psc_config(uint32_t spi_periph, uint32_t i2s_audiosample, uint32_t i2s_frameformat, uint32_t i2s_mckout)
- {
- uint32_t i2sdiv = 2U, i2sof = 0U;
- uint32_t clks = 0U;
- uint32_t i2sclock = 0U;
-
- #ifdef GD32F30X_CL
- uint32_t pll2mf_4 = 0U;
- #endif /* GD32F30X_CL */
-
- /* judge whether the audiosample is 0 */
- if(0U == i2s_audiosample){
- SPI_ERROR_HANDLE("the parameter can not be 0 \r\n");
- }
- /* deinit SPI_I2SPSC register */
- SPI_I2SPSC(spi_periph) = SPI_I2SPSC_DEFAULT_VALUE;
- #ifdef GD32F30X_CL
- /* get the I2S clock source */
- if(((uint32_t)spi_periph) == SPI1){
- /* I2S1 clock source selection */
- clks = I2S1_CLOCK_SEL;
- }else{
- /* I2S2 clock source selection */
- clks = I2S2_CLOCK_SEL;
- }
-
- if(0U != (RCU_CFG1 & clks)){
- /* get RCU PLL2 clock multiplication factor */
- clks = (uint32_t)((RCU_CFG1 & I2S_CLOCK_MUL_MASK) >> 12U);
-
- pll2mf_4 = RCU_CFG1 & RCU_CFG1_PLL2MF_4;
-
- if( 0U == pll2mf_4){
- if((clks > 5U) && (clks < 15U)){
- /* multiplier is between 8 and 16 */
- clks += 2U;
- }else{
- if(15U == clks){
- /* multiplier is 20 */
- clks = 20U;
- }
- }
- }else{
- if(clks < 15U){
- /* multiplier is between 18 and 32 */
- clks += 18U;
- }else{
- if(15U == clks){
- /* multiplier is 40 */
- clks = 40U;
- }
- }
- }
- /* get the PREDV1 value */
- i2sclock = (uint32_t)(((RCU_CFG1 & I2S_CLOCK_DIV_MASK) >> 4U) + 1U);
- /* calculate i2sclock based on PLL2 and PREDV1 */
- i2sclock = (uint32_t)((HXTAL_VALUE / i2sclock) * clks * 2U);
- }else{
- /* get system clock */
- i2sclock = rcu_clock_freq_get(CK_SYS);
- }
- #else
- /* get system clock */
- i2sclock = rcu_clock_freq_get(CK_SYS);
- #endif /* GD32F30X_CL */
-
- /* config the prescaler depending on the mclk output state, the frame format and audio sample rate */
- if(I2S_MCKOUT_ENABLE == i2s_mckout){
- clks = (uint32_t)(((i2sclock / 256U) * 10U) / i2s_audiosample);
- }else{
- if(I2S_FRAMEFORMAT_DT16B_CH16B == i2s_frameformat){
- clks = (uint32_t)(((i2sclock / 32U) *10U ) / i2s_audiosample);
- }else{
- clks = (uint32_t)(((i2sclock / 64U) *10U ) / i2s_audiosample);
- }
- }
-
- /* remove the floating point */
- clks = (clks + 5U) / 10U;
- i2sof = (clks & 0x00000001U);
- i2sdiv = ((clks - i2sof) / 2U);
- i2sof = (i2sof << 8U);
- /* set the default values */
- if((i2sdiv < 2U) || (i2sdiv > 255U)){
- i2sdiv = 2U;
- i2sof = 0U;
- }
- /* configure SPI_I2SPSC */
- SPI_I2SPSC(spi_periph) = (uint32_t)(i2sdiv | i2sof | i2s_mckout);
- /* clear SPI_I2SCTL_DTLEN and SPI_I2SCTL_CHLEN bits */
- SPI_I2SCTL(spi_periph) &= (uint32_t)(~(SPI_I2SCTL_DTLEN | SPI_I2SCTL_CHLEN));
- /* configure data frame format */
- SPI_I2SCTL(spi_periph) |= (uint32_t)i2s_frameformat;
- }
- /*!
- \brief enable I2S
- \param[in] spi_periph: SPIx(x=1,2)
- \param[out] none
- \retval none
- */
- void i2s_enable(uint32_t spi_periph)
- {
- SPI_I2SCTL(spi_periph) |= (uint32_t)SPI_I2SCTL_I2SEN;
- }
- /*!
- \brief disable I2S
- \param[in] spi_periph: SPIx(x=1,2)
- \param[out] none
- \retval none
- */
- void i2s_disable(uint32_t spi_periph)
- {
- SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SEN);
- }
- /*!
- \brief enable SPI NSS output
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nss_output_enable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_NSSDRV;
- }
- /*!
- \brief disable SPI NSS output
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nss_output_disable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_NSSDRV);
- }
- /*!
- \brief SPI NSS pin high level in software mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nss_internal_high(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SWNSS;
- }
- /*!
- \brief SPI NSS pin low level in software mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nss_internal_low(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SWNSS);
- }
- /*!
- \brief enable SPI DMA send or receive
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] dma: SPI DMA mode
- only one parameter can be selected which is shown as below:
- \arg SPI_DMA_TRANSMIT: SPI transmit data use DMA
- \arg SPI_DMA_RECEIVE: SPI receive data use DMA
- \param[out] none
- \retval none
- */
- void spi_dma_enable(uint32_t spi_periph, uint8_t dma)
- {
- if(SPI_DMA_TRANSMIT == dma){
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMATEN;
- }else{
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMAREN;
- }
- }
- /*!
- \brief disable SPI DMA send or receive
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] dma: SPI DMA mode
- only one parameter can be selected which is shown as below:
- \arg SPI_DMA_TRANSMIT: SPI transmit data use DMA
- \arg SPI_DMA_RECEIVE: SPI receive data use DMA
- \param[out] none
- \retval none
- */
- void spi_dma_disable(uint32_t spi_periph, uint8_t dma)
- {
- if(SPI_DMA_TRANSMIT == dma){
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMATEN);
- }else{
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMAREN);
- }
- }
- /*!
- \brief configure SPI/I2S data frame format
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] frame_format: SPI frame size
- only one parameter can be selected which is shown as below:
- \arg SPI_FRAMESIZE_16BIT: SPI frame size is 16 bits
- \arg SPI_FRAMESIZE_8BIT: SPI frame size is 8 bits
- \param[out] none
- \retval none
- */
- void spi_i2s_data_frame_format_config(uint32_t spi_periph, uint16_t frame_format)
- {
- /* clear SPI_CTL0_FF16 bit */
- SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_FF16);
- /* confige SPI_CTL0_FF16 bit */
- SPI_CTL0(spi_periph) |= (uint32_t)frame_format;
- }
- /*!
- \brief SPI transmit data
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] data: 16-bit data
- \param[out] none
- \retval none
- */
- void spi_i2s_data_transmit(uint32_t spi_periph, uint16_t data)
- {
- SPI_DATA(spi_periph) = (uint32_t)data;
- }
- /*!
- \brief SPI receive data
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval 16-bit data
- */
- uint16_t spi_i2s_data_receive(uint32_t spi_periph)
- {
- return ((uint16_t)SPI_DATA(spi_periph));
- }
- /*!
- \brief configure SPI bidirectional transfer direction
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] transfer_direction: SPI transfer direction
- only one parameter can be selected which is shown as below:
- \arg SPI_BIDIRECTIONAL_TRANSMIT: SPI work in transmit-only mode
- \arg SPI_BIDIRECTIONAL_RECEIVE: SPI work in receive-only mode
- \retval none
- */
- void spi_bidirectional_transfer_config(uint32_t spi_periph, uint32_t transfer_direction)
- {
- if(SPI_BIDIRECTIONAL_TRANSMIT == transfer_direction){
- /* set the transmit only mode */
- SPI_CTL0(spi_periph) |= (uint32_t)SPI_BIDIRECTIONAL_TRANSMIT;
- }else{
- /* set the receive only mode */
- SPI_CTL0(spi_periph) &= SPI_BIDIRECTIONAL_RECEIVE;
- }
- }
- /*!
- \brief clear SPI/I2S format error flag status
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] flag: SPI/I2S frame format error flag
- \arg SPI_FLAG_FERR: only for SPI work in TI mode
- \arg I2S_FLAG_FERR: for I2S
- \param[out] none
- \retval none
- */
- void spi_i2s_format_error_clear(uint32_t spi_periph, uint32_t flag)
- {
- SPI_STAT(spi_periph) = (uint32_t)(~flag);
- }
- /*!
- \brief set SPI CRC polynomial
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] crc_poly: CRC polynomial value
- \param[out] none
- \retval none
- */
- void spi_crc_polynomial_set(uint32_t spi_periph,uint16_t crc_poly)
- {
- /* set SPI CRC polynomial */
- SPI_CRCPOLY(spi_periph) = (uint32_t)crc_poly;
- }
- /*!
- \brief get SPI CRC polynomial
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval 16-bit CRC polynomial
- */
- uint16_t spi_crc_polynomial_get(uint32_t spi_periph)
- {
- return ((uint16_t)SPI_CRCPOLY(spi_periph));
- }
- /*
- \brief turn on CRC function
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_crc_on(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCEN;
- }
- /*!
- \brief turn off CRC function
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_crc_off(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_CRCEN);
- }
- /*!
- \brief SPI next data is CRC value
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_crc_next(uint32_t spi_periph)
- {
- SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCNT;
- }
- /*!
- \brief get SPI CRC send value or receive value
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] crc: SPI crc value
- only one parameter can be selected which is shown as below:
- \arg SPI_CRC_TX: get transmit crc value
- \arg SPI_CRC_RX: get receive crc value
- \param[out] none
- \retval 16-bit CRC value
- */
- uint16_t spi_crc_get(uint32_t spi_periph,uint8_t crc)
- {
- if(SPI_CRC_TX == crc){
- return ((uint16_t)(SPI_TCRC(spi_periph)));
- }else{
- return ((uint16_t)(SPI_RCRC(spi_periph)));
- }
- }
- /*!
- \brief clear SPI CRC error flag status
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_crc_error_clear(uint32_t spi_periph)
- {
- SPI_STAT(spi_periph) = (uint32_t)(~SPI_FLAG_CRCERR);
- }
- /*!
- \brief enable SPI TI mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_ti_mode_enable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_TMOD;
- }
- /*!
- \brief disable SPI TI mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_ti_mode_disable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_TMOD);
- }
- /*!
- \brief enable SPI NSS pulse mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nssp_mode_enable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_NSSP;
- }
- /*!
- \brief disable SPI NSS pulse mode
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[out] none
- \retval none
- */
- void spi_nssp_mode_disable(uint32_t spi_periph)
- {
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_NSSP);
- }
- /*!
- \brief enable quad wire SPI
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_enable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_QMOD;
- }
- /*!
- \brief disable quad wire SPI
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_disable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_QMOD);
- }
- /*!
- \brief enable quad wire SPI write
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_write_enable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_QRD);
- }
- /*!
- \brief enable quad wire SPI read
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_read_enable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_QRD;
- }
- /*!
- \brief enable SPI_IO2 and SPI_IO3 pin output
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_io23_output_enable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_IO23_DRV;
- }
- /*!
- \brief disable SPI_IO2 and SPI_IO3 pin output
- \param[in] spi_periph: SPIx(only x=0)
- \param[out] none
- \retval none
- */
- void spi_quad_io23_output_disable(uint32_t spi_periph)
- {
- SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_IO23_DRV);
- }
- /*!
- \brief enable SPI and I2S interrupt
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] interrupt: SPI/I2S interrupt
- only one parameter can be selected which is shown as below:
- \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
- \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
- \arg SPI_I2S_INT_ERR: CRC error,configuration error,reception overrun error,
- transmission underrun error and format error interrupt
- \param[out] none
- \retval none
- */
- void spi_i2s_interrupt_enable(uint32_t spi_periph, uint8_t interrupt)
- {
- switch(interrupt){
- /* SPI/I2S transmit buffer empty interrupt */
- case SPI_I2S_INT_TBE:
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_TBEIE;
- break;
- /* SPI/I2S receive buffer not empty interrupt */
- case SPI_I2S_INT_RBNE:
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_RBNEIE;
- break;
- /* SPI/I2S error */
- case SPI_I2S_INT_ERR:
- SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_ERRIE;
- break;
- default:
- break;
- }
- }
- /*!
- \brief disable SPI and I2S interrupt
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] interrupt: SPI/I2S interrupt
- only one parameter can be selected which is shown as below:
- \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
- \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
- \arg SPI_I2S_INT_ERR: CRC error,configuration error,reception overrun error,
- transmission underrun error and format error interrupt
- \param[out] none
- \retval none
- */
- void spi_i2s_interrupt_disable(uint32_t spi_periph, uint8_t interrupt)
- {
- switch(interrupt){
- /* SPI/I2S transmit buffer empty interrupt */
- case SPI_I2S_INT_TBE:
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_TBEIE);
- break;
- /* SPI/I2S receive buffer not empty interrupt */
- case SPI_I2S_INT_RBNE:
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_RBNEIE);
- break;
- /* SPI/I2S error */
- case SPI_I2S_INT_ERR:
- SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_ERRIE);
- break;
- default :
- break;
- }
- }
- /*!
- \brief get SPI and I2S interrupt flag status
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] interrupt: SPI/I2S interrupt flag status
- only one parameter can be selected which is shown as below:
- \arg SPI_I2S_INT_FLAG_TBE: transmit buffer empty interrupt flag
- \arg SPI_I2S_INT_FLAG_RBNE: receive buffer not empty interrupt flag
- \arg SPI_I2S_INT_FLAG_RXORERR: overrun interrupt flag
- \arg SPI_INT_FLAG_CONFERR: config error interrupt flag
- \arg SPI_INT_FLAG_CRCERR: CRC error interrupt flag
- \arg I2S_INT_FLAG_TXURERR: underrun error interrupt flag
- \arg SPI_I2S_INT_FLAG_FERR: format error interrupt flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus spi_i2s_interrupt_flag_get(uint32_t spi_periph, uint8_t interrupt)
- {
- uint32_t reg1 = SPI_STAT(spi_periph);
- uint32_t reg2 = SPI_CTL1(spi_periph);
- switch(interrupt){
- /* SPI/I2S transmit buffer empty interrupt */
- case SPI_I2S_INT_FLAG_TBE:
- reg1 = reg1 & SPI_STAT_TBE;
- reg2 = reg2 & SPI_CTL1_TBEIE;
- break;
- /* SPI/I2S receive buffer not empty interrupt */
- case SPI_I2S_INT_FLAG_RBNE:
- reg1 = reg1 & SPI_STAT_RBNE;
- reg2 = reg2 & SPI_CTL1_RBNEIE;
- break;
- /* SPI/I2S overrun interrupt */
- case SPI_I2S_INT_FLAG_RXORERR:
- reg1 = reg1 & SPI_STAT_RXORERR;
- reg2 = reg2 & SPI_CTL1_ERRIE;
- break;
- /* SPI config error interrupt */
- case SPI_INT_FLAG_CONFERR:
- reg1 = reg1 & SPI_STAT_CONFERR;
- reg2 = reg2 & SPI_CTL1_ERRIE;
- break;
- /* SPI CRC error interrupt */
- case SPI_INT_FLAG_CRCERR:
- reg1 = reg1 & SPI_STAT_CRCERR;
- reg2 = reg2 & SPI_CTL1_ERRIE;
- break;
- /* I2S underrun error interrupt */
- case I2S_INT_FLAG_TXURERR:
- reg1 = reg1 & SPI_STAT_TXURERR;
- reg2 = reg2 & SPI_CTL1_ERRIE;
- break;
- /* SPI/I2S format error interrupt */
- case SPI_I2S_INT_FLAG_FERR:
- reg1 = reg1 & SPI_STAT_FERR;
- reg2 = reg2 & SPI_CTL1_ERRIE;
- break;
- default :
- break;
- }
- /*get SPI/I2S interrupt flag status */
- if(reg1 && reg2){
- return SET;
- }else{
- return RESET;
- }
- }
- /*!
- \brief get SPI and I2S flag status
- \param[in] spi_periph: SPIx(x=0,1,2)
- \param[in] flag: SPI/I2S flag status
- only one parameter can be selected which is shown as below:
- \arg SPI_FLAG_TBE: transmit buffer empty flag
- \arg SPI_FLAG_RBNE: receive buffer not empty flag
- \arg SPI_FLAG_TRANS: transmit on-going flag
- \arg SPI_FLAG_RXORERR: receive overrun error flag
- \arg SPI_FLAG_CONFERR: mode config error flag
- \arg SPI_FLAG_CRCERR: CRC error flag
- \arg SPI_FLAG_FERR: format error flag
- \arg I2S_FLAG_TBE: transmit buffer empty flag
- \arg I2S_FLAG_RBNE: receive buffer not empty flag
- \arg I2S_FLAG_TRANS: transmit on-going flag
- \arg I2S_FLAG_RXORERR: overrun error flag
- \arg I2S_FLAG_TXURERR: underrun error flag
- \arg I2S_FLAG_CH: channel side flag
- \arg I2S_FLAG_FERR: format error flag
- \param[out] none
- \retval FlagStatus: SET or RESET
- */
- FlagStatus spi_i2s_flag_get(uint32_t spi_periph, uint32_t flag)
- {
- if(SPI_STAT(spi_periph) & flag){
- return SET;
- }else{
- return RESET;
- }
- }
|