Since I could not get CAN to work with the internal CAN of an STM32 (F103C8T6), I bought a few CAN boards working with SPI, like these:
However, when I send something I get the following results using a logic analyzer:
- Channels 1, 2 and 3 are Clock, MISO and MOSI of the receiving SPI
- Channels 4, 5 and 6 are Clock, MISO and MOSI of the transmitting SPI
- Channel 6 and 7 are CANH resp CANL
My relevant code for the sender side:
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
main:
while (1)
{
uint8_t data[] = { 1, 2, 3, 4, 5 };
if (HAL_SPI_Transmit(&hspi1, data, 5, 100) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(1000);
}
(note the Error_Handler is not called, so the return value is HAL_OK.
Receiver code:
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
main:
while (1)
{
HAL_SPI_Receive(&hspi1, data, 5, 100);
}
Questions:
Why don't I get information on the receiving side? It seems the sending SPI is ok, the sending CANH can be ok, but CANL seems not.
There are two jumpers on the CAN PCB, but I'm not sure what they do (when I put jumpers I still get nothing on the receiving SPI side).
I put both the Transmitter and Receiver to Master (because later they both need to send messages). I also tried with the Transmitter to Master and Receiver to Slave, but again no difference. What should be the correct setting?