STM32F1 Spi Slave select with DMA - Page 1
Forum section: electronics / general

author: tn88test
author_rank: Newbie
timestamp: November 19, 2014, 03:08:14 am
content: I am unable to deselect the slave. The MISO pin of the slave is still output the data its received from the master even when the NSS pin high.The datasheet says:QuoteCode:#include "stm32f10x.h"/* DEFINE */#define BUFFSIZE32/* GLOBAL VARIABLE */static volatile uint8_tspi_tx_buff = {0},spi_rx_buff = {0};/* FUNCTION PROTOTYPES */void RCC_Config(void);void GPIO_Config(void);void NVIC_Config(void);void SPI1_Config(void);void DMA1_SPI1_TX_Config(void);void DMA1_SPI1_RX_Config(void);int main(void){/* Config */RCC_Config();GPIO_Config();NVIC_Config();SPI1_Config();DMA1_SPI1_TX_Config();DMA1_SPI1_RX_Config();/* SysTick 1ms */SysTick_Config(SystemCoreClock / 1000);while(1){}}/* FUNCTION -------------------------------------------------------------------*//* CONFIG ---------------------------------------------------------------------*/void RCC_Config(void){RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);}void GPIO_Config(void){GPIO_InitTypeDefGPIO_InitStructure;/* GPIO Alternate function *//* SPI1: PA4 -> NSS */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);/* SPI1: PA5 -> SCK */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);/* SPI1: PA6 -> MISO */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);/* SPI1: PA7 -> MOSI */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);}void NVIC_Config(void){NVIC_InitTypeDefNVIC_InitStructure;/* DMA1_Channel2_IRQn */NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}void SPI1_Config(void){SPI_InitTypeDefSPI_InitStructure;SPI_I2S_DeInit(SPI1);SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;SPI_Init(SPI1, &SPI_InitStructure);SPI_Cmd(SPI1, ENABLE);SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);}void DMA1_SPI1_TX_Config(void){DMA_InitTypeDefDMA_InitStructure;DMA_DeInit(DMA1_Channel3);DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_rx_buff;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;DMA_InitStructure.DMA_BufferSize = BUFFSIZE;DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;DMA_InitStructure.DMA_Priority = DMA_Priority_High;DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel3, &DMA_InitStructure);DMA_Cmd(DMA1_Channel3, ENABLE);}void DMA1_SPI1_RX_Config(void){DMA_InitTypeDefDMA_InitStructure;DMA_DeInit(DMA1_Channel2);DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)spi_rx_buff;DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;DMA_InitStructure.DMA_BufferSize = BUFFSIZE;DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel2, &DMA_InitStructure);DMA_Cmd(DMA1_Channel2, ENABLE);DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);}/* INTERRUPT ------------------------------------------------------------------*/void SysTick_Handler(void){}void DMA1_Channel2_IRQHandler(void){/* SPI1: RX */if(DMA_GetITStatus(DMA1_IT_TC2) != RESET){DMA_ClearITPendingBit(DMA1_FLAG_GL2);}}

author: andersm
author_rank: Super Contributor
timestamp: November 19, 2014, 07:46:49 pm
content: The hardware documentation does not actually state that the slave transmitter is disabled when the NSS pin is deasserted. You may have to do it manually in software.