nopnop2002 / Arduino-STM32-CAN

Can Example for Arduino Core STM32
218 stars 53 forks source link

Possible to read but can't send data. #48

Open Toernblom opened 2 years ago

Toernblom commented 2 years ago

Hi, currently using STM32F103R8T6.

CAN interface is connect like: PB8 -> CAN_RX -> TJA1050 PB9 -> CAN_TX -> TJA1050

in void Setup I'm using: bool ret = CANInit(CAN_500KBPS, 2); // CAN_RX mapped to PB8, CAN_TX mapped to PB9

What happens is that while debugging i notice that on this line, the while loop never ends.

Communication works ok outside this unit. Am i missing something?

Toernblom commented 2 years ago

image

Errors while running. I've used the same cables with another board using CAN so shouldn't be an issue. Terminations is ok aswell because other devices can talk to each other.

nopnop2002 commented 2 years ago

Regarding send fail, past issues (closed issues) are helpful.

Mostly due to terminating resistors or cable quality.

Many people have encountered this problem and solved it by changing the terminator or STAB wire cable. When STM32 is added to CAN-BUS in operation, the impedance may change due to STAB. When adding nodes, pay attention to the length of the STAB cable.

Toernblom commented 2 years ago

I currently have 3 nodes, 2 of them are talking and all has twisted shielded pairs which are 5-10cm in length and the shield is grounded. It's the only device on the network that can't talk. The resistance between CAN_L & CAN_H is ~60 Ohm so that isn't an issue either. I've also checked previous closed issues and tried those aswell. All baudrates is 100kbps to minimize noise.

image

These are the prints from your code. I noticed that the peripheral_clock_rate was at 32mhz while STM32F103R8 that i'm using is at 72mhz so i tried changing that aswell to the correct clock with no success

nopnop2002 commented 2 years ago

peripheral_clock_rate is not cpu frreuency. cpu frreuency = peripheral_clock_rate x 2

Arduino core support for STM32 based boards uses different clock frequencies depending on the Board Part Number. BlackPill & BluePill use 72Mhz. Generic board use 64Mhz.

If the receive works correctly, it's not a CPU frequency issue.

The resistance between CAN_L & CAN_H is ~60 Ohm

I think It's too small.

Toernblom commented 2 years ago

Hmm okey, then peripheral_clock_rate should be 36mhz then? Since STM32F103R8T6 is 72Mhz not 64.

The receive did work yesterday, not anymore for some reason.. I saw your previous comment on another issue with 150 Ohm termination, I'll try that!

nopnop2002 commented 2 years ago

Since STM32F103R8T6 is 72Mhz not 64.

peripheral_clock_rate is not cpu frreuency. cpu frreuency = peripheral_clock_rate x 2

Arduino core support for STM32 based boards uses different clock frequencies depending on the Board Part Number. BlackPill & BluePill use 72Mhz. Generic board use 64Mhz.

You can check your board CPU freqiency using the F_CPU macro

Serial.println(F_CPU);
Toernblom commented 2 years ago

Hmm, that result gave 64 Mhz. In my Platformio.ini file , the board is genericSTM32F103R8. Should i switch out the board part number?

nopnop2002 commented 2 years ago

Should i switch out the board part number?

If you use bluepill or blackpill in Platformio.ini, it will be 72Mhz.

It has nothing to do with this issue.

But everyone gets confused.

Toernblom commented 2 years ago

Haha i understand why. We use a custom made PCB with STM32F103R8T6 and It's the first time with the STM32 chip family. So I thought i'd should use the genericSTM32F103R8 in the .ini file , not blackpill or bluepill ..

But if i use the genericSTM32F103R8 F_CPU shows 64Mhz

nopnop2002 commented 2 years ago

I think you can build custom boards using BluePill and BlackPill variants.

However, the GPIO of the Serial object changes for the Generic board, BluePill, and BlackPill.

The output destination of Serial.print differs depending on the Board Part Number.

BluePill:PA9 BlackPill:PA9 Generic F103:PA2

everyone gets confused.

Toernblom commented 2 years ago

Can't use bluepill or blackpill in platformio.ini file to make serial work. If i use genericSTM32F103R8 with HardwareSerial its fine. image its because image

However, if i do use genericSTM32F103R8 F_CPU is wrong and if i use either bluepill or blackpill in .ini the f_CPU is correct..

I do understand that everyone gets confused..

Toernblom commented 2 years ago

I have replaced the termination resistors with 150 Ohm so that i have two terminations at the ends with a bus resistance at 75 Ohm now. No success in sending any data.

nopnop2002 commented 2 years ago

Keep STAB length as short as possible.

Toernblom commented 2 years ago

image

This is the setup, the connection between the raspberry pi on the right and the CAN analyze tool on the bottom has no shielded nor twisted pairs. I'm not sure the cables are the issues..

nopnop2002 commented 2 years ago

If you have a BluePill or BlackPill, you can identify the problem.

Try BluePill or BlackPill.

Toernblom commented 2 years ago

Sadly I don't. I can purchase one however if you think that would help ?

nopnop2002 commented 2 years ago

There may be a problem with your custom made PCB.

If it works fine with BluePill/BlackPill, then there may be a problem with your custom made PCB.

BluePill/BlackPill works absolutely.

Toernblom commented 2 years ago

The connection: image image

image

Your code says that the initialization was ok. I don't think theres anything wrong with the wiring here..

Toernblom commented 2 years ago

Alright, found out the reason why reading stop worked. It was cause i was monitoring on the UART. If i only powered it on and ran with debug i could read messages. Not send however. Its quite weird actually.

If dont use Hardware Serial at PA10 & PA9

Then the code gets stuck here CANInit(CAN_250KBPS, 2);

Stepping over CANInit function with debugging results in the debug can't step over and gets stuck inside this function ComputeCANTimings(HAL_RCC_GetPCLK1Freq(), target_bitrate, &timings);

Digging further it goes to stm32f1xx_hal_rcc.c and gets stuck here

uint32_t HAL_RCC_GetPCLK1Freq(void)
{
  /* Get HCLK source and Compute PCLK1 frequency ---------------------------*/
  return (HAL_RCC_GetHCLKFreq() >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1) >> RCC_CFGR_PPRE1_Pos]);
}

And if i do use Hardware Serial at PA10 & PA9 the code doesn't hang on that line and continues running and i can successfully read messages on the CAN bus. But CANSend hangs here:

while (CAN1->sTxMailBox[0].TIR & 0x1UL && count++ < 1000000);

nopnop2002 commented 2 years ago

It was cause i was monitoring on the UART.

This is the UART output. Something is wrong.

Testing on a custom PCB is even more confusing. Testing on a custom PCB doesn't reveal anything.

80896902-3d9e0680-8d2e-11ea-9add-0a102f43c3a7

Toernblom commented 2 years ago

Alright, If i put a delay between Serial.begin and CANInit the code doesn't hang anymore. Still getting Send Fail no matter what i do, I've got another CAN Node with the same tranciever, TJA1050 , exact same cables and length on the network that is working well, custom made PCB aswell..

amitesh-singh commented 8 months ago

@Toernblom have you found the solution?

nopnop2002 commented 8 months ago

@Toernblom @amitesh-singh

It's not a clone chip, right?

Doesn't work properly with clone chips.

https://hackaday.com/2020/10/22/stm32-clones-the-good-the-bad-and-the-ugly/

Toernblom commented 8 months ago

Hi, it was a fault within the PCB. I think it was some issue with different logic levels between the IC's of the MCU and CAN controller & transciever.

amitesh-singh commented 8 months ago

@Toernblom @nopnop2002 Thanks for the reply.

Is it due to powering tja1050 by 5v and stm32f103 is powered from 3.3v?

amitesh-singh commented 8 months ago

image image

cc @Toernblom @nopnop2002

amitesh-singh commented 8 months ago

the PB8 and PB9 pins are 5v tolerant and tja1050 should work with 3.3v devices, that's what they claim in their datasheet as tja1050 RXD is a totem pole and voltage varies from 0.7Vcc to 0.45v

nopnop2002 commented 8 months ago

@amitesh-singh

the PB8 and PB9 pins are 5v tolerant

Yes. You are right.

I powered the STM32F103 from USB and tried it with the 5V transceiver TJA1051. The terminating resistor worked correctly with both 100 ohm and 150 ohm. My TJA1051 has components marked 4017V installed. I don't know what this is.

IMG_5156 S1d81667d39154041a45fbe1b16230516k

My test circuit. The Nano on the left is a receiver with a terminating resistor. The Nano on the right is a transmitter without a terminating resistor. TJA1051 has a terminating resistor.

Test-Circuit

This is the receiver logging stm32f103-can

When CAN-BUS has a mix of 3.3V and 5V transceivers, +1V Ground Shift with Split Termination is effective https://www.ti.com/lit/an/slla337/slla337.pdf