Hello,
I am trying to program MIBSPI5 in SPI compatiblity mode, but I am unable to receive any data from it (currently I'm not using any interrupts, I'm just sending data from SPI2 and try to receive it in MIBSPI5 usgin Hitex's functional safety development kit).
It receives data when in master mode but not in slave mode. Here's my configuration code:
/** * @def CONFIGURE_SPI * @brief SPI configuration macro * * All SPI modules are configured and initialized the same way. This macro allows the programmer to * configure the SPI module as desired * * @in SPI - Desired SPI port * @in SPIDATA - Structure containing the SPI's configuration data. It is a spi_config_data type. **/ #define CONFIGURE_SPI(SPI, SPIDATA) \ do { \ unsigned int prescale = (VCLK/SPIDATA.baud_MHz) - 1U; \ \ SPI##Reg->SPIGCR0 = 0x1U; /*Sets the module out of reset*/ \ SPI##Reg->SPIPC0 = ( 1U /* SCS[0] */ \ | (1U << 1U) /* SCS[1] */ \ | (1U << 8U) /* ENA */ \ | (1U << 9U) /* CLK */ \ | (1U << 10U) /* SIMO */ \ | (1U << 11U)); /* SOMI */ \ \ SPI##Reg->SPIGCR1 = ( SPIDATA.mode /*Master mode enabled*/ \ | SPIDATA.mode << 1U); /*Clock mode: internal*/ \ if(SPIDATA.mode == MASTER) { \ SPI##Reg->SPIFMT0 = ( SPIDATA.n_Bits /*Data Bits*/ \ | (prescale << 8U) /*Speed*/ \ | (0U << 24U)); /*No parity bit is transmitted*/ \ \ SPI##Reg->SPIFMT1 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ \ SPI##Reg->SPIFMT2 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ \ SPI##Reg->SPIFMT3 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ } else { \ SPI##Reg->SPIFMT0 = ( SPIDATA.n_Bits /*Data Bits*/ \ | (prescale << 8U) /*Speed*/ \ | (0U << 24U)); /*No parity bit is transmitted*/ \ \ SPI##Reg->SPIFMT1 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ \ SPI##Reg->SPIFMT2 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ \ SPI##Reg->SPIFMT3 = ( SPIDATA.n_Bits /*Same as SPIFMT0*/ \ | (prescale << 8U) /*Same as SPIFMT0*/ \ | (0U << 24U)); /*Same as SPIFMT0*/ \ \ } \ SPI##Reg->SPIPC1 = ( (1U & SPIDATA.mode) /*CS... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 8) /*ENA... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 9) /*CLK... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 10) /*SIMO... 1 -> output, 0-> input*/ \ | ((~(1U & SPIDATA.mode) & 0x1U) << 11));/*SOMI... 1 -> output, 0-> input*/ \ /*Configure open-drain*/ \ SPI##Reg->SPIPC6 = ( (0U & SPIDATA.mode) /*CS... 1 -> output, 0-> input*/ \ | (0U & SPIDATA.mode << 8) /*ENA... 1 -> output, 0-> input*/ \ | (0U & SPIDATA.mode << 9) /*CLK... 1 -> output, 0-> input*/ \ | (0U & SPIDATA.mode << 10) /*SIMO... 1 -> output, 0-> input*/ \ | (0U & SPIDATA.mode << 11)); /*SOMI... 1 -> output, 0-> input*/ \ /*Configure pull-up*/ \ SPI##Reg->SPIPC8 = ( (1U & SPIDATA.mode) /*CS... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 8) /*ENA... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 9) /*CLK... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 10) /*SIMO... 1 -> output, 0-> input*/ \ | (1U & SPIDATA.mode << 11)); /*SOMI... 1 -> output, 0-> input*/ \ \ SPI##Reg->SPIDELAY = (0U << 24U); \ \ SPI##Reg->SPIDAT1 = (1U << SPIDATA.CS); /*Choose chip select*/ \ \ SPI##Reg->SPIFLG = 0xFFFFFFFFU; \ \ SPI##Reg->SPIGCR1 |= (1U << 24U); /*Enable SPI*/ \ } while (0) \
#define MASTER 0x1U #define SLAVE 0x0U struct spi_config_data { unsigned int baud_MHz; /*Desired data baud rate in MHz*/ unsigned int n_Bits; /*Number of bits to be transmitted. Can vary from 2 to 16, according to the microcontroller's technical reference manual*/ unsigned int CS; /*Desired chip selects*/ unsigned int mode; /*Desired mode to operate SPI. Master/Slave*/ }; static struct spi_config_data SPI5_config = { .baud_MHz = 1, .n_Bits = 8, .CS = CS1, .mode = SLAVE }; void SPI5_init() { CONFIGURE_SPI(SPI5, SPI5_config); } unsigned int SPI5_send(char data) { SEND_BYTE(SPI5, data); return 1; } unsigned int SPI5_receive() { return RECEIVE_BYTE(SPI5) } static void SPI5_send_byte(char data) { while (!(SPI5Reg->SPIFLG & 0x200U)); SPI5Reg->SPIDAT1 = ( data | (1U << 16U)); } static unsigned int SPI5_receive_byte() { unsigned int n = 0; while(!(SPI5Reg->SPIFLG & 0x100U) && (n < 500000U)) n++; return (SPI5Reg->SPIBUF & 0xFFU); /*0xFF because 8 bit communication is being used*/ }
Those are the send, receiveand init functions. When in master mode everything works fine, the problem comes when in slave mode. It simply doesn't receive any data.
As I mentioned earlier I'm using Hitex's functional safety kit, and to be able to test SPI2 with MIBSPI5. In order to make these 2 work together I removed the accelerometer from the board and simply put some wires. Then I connect SPI2's SCL with SPI5's SCL and SPI2's SIMO with SPI5's SOMI
At the moment I'm running the following code in main:
unsigned int check main = 0, i =0;
InitSPI2(master)
InitSPI5(slave)
while(1) {
SPI2_send_byte(0xAAU);
SPI5_receive_byte()
for(i = 0, i < 500000; i++);
}
PS: I know it is an ugly hack, but it works :)