Help implementing SPI using just registers
Hello everyone,
I am having trouble communicating with CC1101 radio SPI slave. With HAL, the transfers work well and I am able to read every register, but because I am using stm32f042 MCU, I'm low on flash memory because I'm also using USB CDC. So I'm trying to control the SPI1 peripheral using it's registers. Here is the code for the transfer and the init:
void SPI1_Init(void) {
/* full duplex master, 8 bit transfer, default phase and polarity */
SPI1->CR1 = SPI_CR1_MSTR | SPI_CR1_SPE | SPI_CR1_SSM | SPI_CR1_SSI;
/* Disable receive FIFO*/
SPI1->CR2 = SPI_CR2_FRXTH;
}
void SPI1_Transfer(uint8_t *outp, uint8_t *inp, int count) {
while(count--) {
while(!(SPI1->SR & SPI_SR_TXE))
;
*(volatile uint8_t *)&SPI1->DR = *outp++;
while(!(SPI1->SR & SPI_SR_RXNE))
;
*inp++ = *(volatile uint8_t *)&SPI1->DR;
}
}The problem is that it gets stuck on the while(!(SPI1->SR & SPI_SR_TXE)) loop. It seems the TXE flag is never set. I also have initialized MISO, MOSI and SCK in .ioc as what they are (they are now orange in color in the pinout diagram). Can someone help me figure out what I am missing? Also, here is the read register function which I haven't been able to test due to the issue:
uint8_t readReg(uint8_t addr){
uint8_t value;
uint8_t address = addr | 0x80;
uint8_t zero = 0x00;
HAL_GPIO_WritePin(GPIOA, NSS_Pin, GPIO_PIN_RESET);
SPI1_Transfer(&address, &value, 1);
SPI1_Transfer(&zero, &value, 1);
HAL_GPIO_WritePin(GPIOA, NSS_Pin, GPIO_PIN_SET);
return value;
}And also, because I am very tight on flash, if someone could help me setting up the hardware NSS using the registers would be awesome, as I am not sure how to do so, I know it evolves setting the SSM register but I am not sure If I need to set anything else.
Thank you
