Other Parts Discussed in Thread: MSP430FR6989, TCAN4550
Hello,
I am trying to connect TCAN4550EVM to my STM32F407VET6 board and I'm stuck in the first step. I can't even read the device ID correctly. What I got from address 'h000 & 'h0004 are random values.
I'm able to use USART and COM function and read the device id of a flash memory on my MCU board, so the MCU itself is not broken.
The compiler I'm using is Keil V5 Lite. The code sample on the website seems to be for other compiler and MCU so I cannot use it directly. I simplified my code to SPI-related and hope someone could check out anything wrong. I have very limited experience in MCU application. Maybe they are simple mistakes.
I set the SPI frequency to 10MHz(APB clock is 80Mhz), CPHA=0, CPOL=0. Only 6 wires are connected: VBAT and GND to a 12V power supply, SCLK to PA5, SDI to PA7, SDO to PA6, nCS to PA4.
void SPISendByte(unsigned char tmpData)
{
while((SPI1->SR&2)==0); //wait for TXE
SPI1->DR = tmpData;
}
unsigned char SPIGetByte()
{
while((SPI1->SR&2)==0); //wait for TXE
while(SPI1->SR&(1<<7)); //wait for !BSY
SPI1->DR = 0; //send dummy data
while(!(SPI1->SR&1)); //wait for RXNE
return SPI1->DR;
}
void TCANReadBytes(unsigned short address, unsigned char *tmpC, unsigned short len1)
{
GPIOA->BSRRH |= GPIO_Pin_4; //set to 0, enable
SPISendByte(0x41); //read
SPISendByte(address>>8); //MSB
SPISendByte(address&0xFF); //LSB
SPISendByte(len1); //len1*32bit
while((SPI1->SR&2)==0); //wait for TXE
while(SPI1->SR&(1<<7)); //wait for !BSY
SPI1->DR; //read the last dummy byte
for (unsigned short idx1=0; idx1<len1*4; ++idx1)
tmpC[idx1] = SPIGetByte();
GPIOA->BSRRL |= GPIO_Pin_4; //set to 1, disable
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Init(115200);
//SPI settings
RCC->AHB1ENR |= RCC_AHB1Periph_GPIOA;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //nCS
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIOA->BSRRL |= GPIO_Pin_4; //set to 1, disable NCS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
RCC->APB2ENR |= (1<<12); //enable SPI1 clock
SPI1->CR1 |= 0 << 0; //CPHA = 0
SPI1->CR1 |= 0 << 1; //CPOL = 0
SPI1->CR1 |= 1 << 2; //master mode
SPI1->CR1 |= 2 << 3; //baudrate = fPCLK /8
SPI1->CR1 |= 0 << 7; //MSB first
SPI1->CR1 |= 1 << 9; //software slave management
SPI1->CR1 |= 1 << 8; //software slave internal
SPI1->CR1 |= 0 << 10; //RXonly =0, full-duplex
SPI1->CR1 |= 0 << 11; //frame format: 8 bit data
SPI1->CR1 |= 1 << 6; //enable external
unsigned char tmpC[5];
tmpC[4] = 10;
for(int idx1=0;idx1<8;idx1=idx1+4)
{
tmpC[0] = 0;
tmpC[1] = 0;
tmpC[2] = 0;
tmpC[3] = 0;
TCANReadBytes(idx1, tmpC, 1);
USART1SendChar((char*)tmpC, 5); //send the response to PC via COM port
}
while (1)
{
}
}
The responses are
[2023-07-20 10:43:10.502]# RECV HEX>
00 00 00 00 0A C0 7F F2 40 0A
[2023-07-20 10:43:12.824]# RECV HEX>
FF FF EA 00 0A 02 00 00 00 0A
[2023-07-20 10:43:15.247]# RECV HEX>
FF FF 92 00 0A 02 00 00 00 0A
[2023-07-20 10:43:17.708]# RECV HEX>
FF FF F5 40 0A 02 00 00 00 0A
[2023-07-20 10:43:20.001]# RECV HEX>
FF FF 92 00 0A 00 00 00 00 0A
[2023-07-20 10:43:23.255]# RECV HEX>
00 00 00 00 0A 00 00 00 52 0A
[2023-07-20 10:43:25.510]# RECV HEX>
FF FF 89 00 0A 00 01 39 05 0A
[2023-07-20 10:43:27.581]# RECV HEX>
00 00 00 00 0A 00 00 00 00 0A
[2023-07-20 10:43:29.656]# RECV HEX>
00 00 00 00 0A 00 00 00 00 0A
[2023-07-20 10:43:31.665]# RECV HEX>
00 00 00 00 0A 00 00 00 00 0A
[2023-07-20 10:43:33.786]# RECV HEX>
00 00 00 00 0A 00 00 00 00 0A
[2023-07-20 10:43:35.888]# RECV HEX>
00 00 00 00 0A 00 00 00 00 0A
[2023-07-20 10:43:38.188]# RECV HEX>
7F FF FF FF 0A C4 80 00 00 0A
[2023-07-20 10:43:40.176]# RECV HEX>
FF FF 92 00 0A 00 01 39 05 0A
I tried to read the TCAN ID during the start of MCU and tested it with the reset button of MCU board.

