2021-07-05 06:27 PM
I have declared my SPI2 base address:
#define PERIPH_BASE 0x40000000U
#define APB1PERIPH_BASE PERIPH_BASE
#define SPI2_BASEADDR (APB1PERIPH_BASE + 0x3800)
I have a SPI Handle structure:
typedef struct
{
SPI_RegDef_t *pSPIx; //This holds the base address of SPIx peripheral
SPI_Config_t SPIConfig;
uint8_t *pTxBuffer; //to store the application's Tx buffer address because this will reused by the ISR code
uint8_t *pRxBuffer; //to store the application's Rx buffer address.
uint32_t TxLen; //to store Tx Len
uint32_t RxLen; // to store Rx Len
uint8_t TxState; //to store Tx state
uint8_t RxState; //to store Rx state
}SPI_Handle_t;
Then, I have SPI register definition structure:
typedef struct
{
__vo uint32_t CR1; //can get all the SPI register dalam reference manual SPI section (SPI register map)
__vo uint32_t CR2;
__vo uint32_t SR;
__vo uint32_t DR;
__vo uint32_t CRCPR;
__vo uint32_t RXCRCR;
__vo uint32_t TXCRCR;
__vo uint32_t I2SCFGR;
__vo uint32_t I2SPR;
}SPI_RegDef_t;
Then, I do my SPI2 initialization functions:
void SPI2_Inits(void)
{
SPI_Handle_t SPI2handle; //1st, initialize spi2 handle
SPI2handle.pSPIx = SPI2; //initialize SPI base address
SPI2handle.SPIConfig.SPI_BusConfig = SPI_BUS_CONFIG_FD; //SPI configuration bus use full duplex
SPI2handle.SPIConfig.SPI_DeviceMode = SPI_DEVICE_MODE_MASTER; //jadikan device kita sebagai master kita xdk slave utk exercise ni
SPI2handle.SPIConfig.SPI_SclkSpeed = SPI_SCLK_SPEED_DIV8; //sebab kita nak 2MHz so kne divide by 8
SPI2handle.SPIConfig.SPI_DFF = SPI_DFF_8BITS; //set data format to 8 bits
SPI2handle.SPIConfig.SPI_CPOL = SPI_CPOL_LOW; //LOW to default value
SPI2handle.SPIConfig.SPI_CPHA = SPI_CPHA_LOW; //LOW to default value
SPI2handle.SPIConfig.SPI_SSM = SPI_SSM_DI; //software slave management, disable ssm for NSS pin, sebab kita guna hardware
SPI_Init(&SPI2handle); //call spi init dan send all the address of SPI2handle
}
Then, my main code wants to execute this function:
SPI_SendDataIT(&SPI2handle,&dummy,1);
The SPI_SendDataIT function is:
uint8_t SPI_SendDataIT(SPI_Handle_t *pSPIHandle, uint8_t *pTxBuffer, uint32_t Len)
{
uint8_t state = pSPIHandle->TxState;
if(state != SPI_BUSY_IN_TX)
{
pSPIHandle->pTxBuffer = pTxBuffer;
pSPIHandle->TxLen = Len;
pSPIHandle->TxState = SPI_BUSY_IN_TX;
pSPIHandle->pSPIx->CR2 |= ( 1 << SPI_CR2_TXEIE );
}
return state;
}
The problem is, in the SPI_SendData() function at the line
pSPIHandle->pSPIx->CR2 |= ( 1 << SPI_CR2_TXEIE );
where the pSPIHandle did not shift the register of CR2 inside the pSPIx address which is SPI2. In this case, I want to SET the TXEIE, but this line did not SET it.
where the SPI_CR2_TXEIE I declared in:
#define SPI_CR2_RXDMAEN 0
#define SPI_CR2_TXDMAEN 1
#define SPI_CR2_SSOE 2
#define SPI_CR2_NSSP 3
#define SPI_CR2_FRF 4
#define SPI_CR2_ERRIE 5
#define SPI_CR2_RXNEIE 6
#define SPI_CR2_TXEIE 7
#define SPI_CR2_DS 8
#define SPI_CR2_FRXTH 12
#define SPI_CR2_LDMA_RX 13
#define SPI_CR2_LDMA_TX 14
I dont find where is the fault in my code. Could someone help me? Im using stm32f302r8
Solved! Go to Solution.
2021-07-05 09:26 PM
Already found the solution, It seems the udemy course is wrong. they declare pSPIhandle locally inside the SPI_inits functions.
But then, the main program need to use the pSPIhandle. So, should declare globally. Thank you TDK for you responses and help