void config_spi( int id )
{
Spi_Enable( id, 0 );
Spi_SetFrameFormat( id, SPI_MOTOROLA_MODE );
Spi_SetPolarity( id, SPI_POLARITY_LOW );
Spi_SetPhase( id, SPI_TXR_PHASE );
Spi_SetDataSize( id, ((SPI_UNIT_TR_BITS == 8) ? SPI_DATA_SIZE_8BIT : 0xf));
Spi_SetTransferMode( id, SPI_TX_RX_MODE );
Spi_DisableIrq( id, SPI_IRQ_ALL );
Spi_SetDmaTxDataLevel( id, 12 ); //12
Spi_SetDmaRxDataLevel( id, 3 ); //3
Spi_SetDmaControlDisable( id, SPI_DMA_RX_POS | SPI_DMA_TX_POS );
}
static void config_dma_rx( int ch )
{
Dma_ClearIsrBit(ch,(DMA_INTT_TXR | DMA_INTT_BLOCK | DMA_INTT_SOURCE | DMA_INTT_DEST | DMA_INTT_ERR)); // new add
Dma_SetTxrType( ch, DMA_TTFC_P2M_DMAC );
Dma_SetSrcWidth( ch, ((SPI_UNIT_TR_BITS == 8) ? DMA_TXR_8BITS : DMA_TXR_16BITS));
Dma_SetSrcSize( ch, DMA_BURST_4 );
Dma_SetDstWidth( ch, DMA_TXR_32BITS ); //DMA_TXR_32BITS
Dma_SetDstSize( ch, DMA_BURST_4 );
Dma_SetSrcIncDirection( ch, DMA_DIR_UNCHG );
Dma_SetDstIncDirection( ch, DMA_DIR_INC );
Dma_SetSrcHsMode( ch, DMA_HSMODE_HARDWARE );
Dma_SetFifoMode( ch, 1 );
Dma_SetFlowCtrl( ch, 1 );
Dma_SetHProt( ch, 6 );
Dma_EnableIsrBit( ch, DMA_INTT_BLOCK );
}
static void config_dma_tx( int ch )
{
Dma_ClearIsrBit(ch,(DMA_INTT_TXR | DMA_INTT_BLOCK | DMA_INTT_SOURCE | DMA_INTT_DEST | DMA_INTT_ERR));// new add
Dma_SetTxrType( ch, DMA_TTFC_M2P_DMAC );
Dma_SetSrcWidth( ch, DMA_TXR_32BITS ); //DMA_TXR_32BITS
Dma_SetSrcSize( ch, DMA_BURST_4 );
Dma_SetDstWidth( ch, ((SPI_UNIT_TR_BITS == 8) ? DMA_TXR_8BITS : DMA_TXR_16BITS));
Dma_SetDstSize( ch, DMA_BURST_4 );
Dma_SetSrcIncDirection( ch, DMA_DIR_INC );
Dma_SetDstIncDirection( ch, DMA_DIR_UNCHG );
Dma_SetDstHsMode( ch, DMA_HSMODE_HARDWARE );
Dma_SetFifoMode( ch, 1 );
Dma_SetFlowCtrl( ch, 1 );
Dma_SetHProt( ch, 6 );
Dma_EnableIsrBit( ch, DMA_INTT_BLOCK );
}
int spi_one_cycle_transfer(UINT8 idx,const UINT8 *tx_buff,UINT8 *rx_buff,UINT32 len)
{
UINT8 ret;
//1.wait master pull up ack gpio
OSSemPend(sem_wifi_ack_gpio, 0, &ret );
//2.pull up slaver ack gpio
//Gpio_SetPortX(HW_HELP_SPI_GPIO,1);
//3.block to exchange data
#if 0
Spi_WriteReadBlock(idx,tx_buff,rx_buff,len);
#else
config_spi(g_spi_chan);
Spi_SetDmaControlEnable( idx, SPI_DMA_RX_POS | SPI_DMA_TX_POS );
Spi_Enable( g_spi_chan, 1 );
//config tx
config_dma_tx( g_spitx_dma_chan );
Dma_SetSrcAddress( g_spitx_dma_chan, (unsigned int)tx_buff );
Dma_SetDstAddress( g_spitx_dma_chan, SPI_DATA_REG );
Dma_SetDstPer( g_spitx_dma_chan, DMA_HSP_SPI2TX );
Dma_SetTxrSize( g_spitx_dma_chan, len >> 2 );
Dma_EnableChan( g_spitx_dma_chan );
//config rx
config_dma_rx( g_spirx_dma_chan );
Dma_SetSrcAddress( g_spirx_dma_chan, SPI_DATA_REG );
Dma_SetDstAddress( g_spirx_dma_chan, (unsigned int)rx_buff );
Dma_SetSrcPer( g_spirx_dma_chan, DMA_HSP_SPI2RX );
Dma_SetTxrSize( g_spirx_dma_chan, (SPI_UNIT_TR_BITS == 8) ? len : (len >> 1));
Dma_EnableChan( g_spirx_dma_chan );
Gpio_SetPortX(HW_HELP_SPI_GPIO,1);
OSSemPend(g_dmaSpiRx_done, 0, &ret); //don't wait forever,need to change
#endif
//4.pull down slaver ack gpio
Gpio_SetPortX(HW_HELP_SPI_GPIO,0);
return 0;
}
static void _spiFrCCThreadFun(void *param)
{
static unsigned char slaveRxBuff[1200] = {0};
static unsigned char slaveTxBuff[1200] = "hellothisisfh8610";
UINT8 ret;
sem_wifi_ack_gpio = OSSemCreate(0);
while(1)
{
//1.check msg queue first.if it have,mean dsp want to send something initiative
//2.package data
//3.exchange data
spi_one_cycle_transfer(g_spi_chan, slaveTxBuff, slaveRxBuff, 600);
uprintf("_spiFrCCThreadFun slaveRxBuff=%x-%x-%x-%x-%x-%x\n",slaveRxBuff[0],
slaveRxBuff[1],slaveRxBuff[2],slaveRxBuff[3],slaveRxBuff[4],slaveRxBuff[5]);
//4.unpackage data
//5.analysis data
}
}