Hi All and thanks for your tips :-)
By a stroke of luck Simon Ford had posted a very informative answer in this topic: Problems with Memory Pointers to mbed NXP1768 Peripherals which gave me the idea of delving into how the CANbus is setup. I tried setting different CANbus speeds and reading the value of LPC_CAN2->BTR for each setting - this is what I found:
speed = 50000 LPC_CAN2->BTR = 0x52002F
speed = 100000 LPC_CAN2->BTR = 0x520017
speed = 500000 LPC_CAN2->BTR = 0x410005
speed = 600000 LPC_CAN2->BTR = 0x520003
speed = 1000000 LPC_CAN2->BTR = 0x410002
but with the (unusual?) values I want I saw this:
speed = 47619 LPC_CAN2->BTR = 0xFFC3FF
speed = 615000 LPC_CAN2->BTR = 0xFFC3FF
Which very much looks like the CANbus frequency (sorry Andrew, speed ;-) setting doesn't work properly for all speeds.
With a not so quick reading of the NXP LPC176x User Manual and a lot of help from the CANbus guide on this site: http://www.kvaser.com/index.htm , particularly the section on CAN bit timing, I was able to work out that mbed must have a 24 MHz speed/frequency (whichever one it is!). Knowing that I was able to find the BTR values for 615000 had to give a division of 39 from 24MHz:
24,000,000 / 615,000 = 39.024; 39 is the closest number and 24MHz/39 = 615,385 which hopefully would be close enough :-)
39 = 3 x 13 which makes things a bit easier and I used 3 for the BPR value. I noticed that kvaser.com seems to suggest that the TSEG1 value is usually larger than TSEG2 and I chose TSEG1 = 9 and TSEG2 = 3
BPR = 3 (put 2 in the BTR register because mbed adds 1 to this value)
TSEG1 = 9 (put 8 in the BTR register because mbed adds 1 to this value)
TSEG2 = 3 (put 2 in the BTR register because mbed adds 1 to this value)
and an extra 1 for the sync bit (which isn't included in the BTR register!!!)
So I tried setting LPC_CAN2->BTR = 0x280002 like this:
can2.frequency(600000); // nearest mbed MBED CAN speed setting to the one I want
LPC_CAN2->BTR = 0x280002; // correct BTR setting for 615k speed
Now I've got CANbus messages reliably going between my mbed and my car - phew!!! (now all I need to do to is make sense of them lol)
BTW, following kvaser.com's guideline of making TSEG1 bigger than TSEG2 would imply that the CANbus library isn't quite right in this respect and maybe the values should be:
speed = 50000 LPC_CAN2->BTR = 0x25002F
speed = 500000 LPC_CAN2->BTR = 0x140005
etc.
Perhaps someone on the mbed team can check my working out :-)
Sophie x
Hi,
I'm a bit confused about the CANbus frequency setting, does anyone know if I can use any value to set up the frequency or is there a limited set of frequencies? e.g 50kHz, 125kHz, 250kHz, 500kHz, 800kHz and 1MHz seem to be the standard ones I've seen when looking around on the web.
I ask because I need to be able to use 47,619Hz, 500kHz or 615kHz and results have not been promising; I don't see anything happen when I use those frequencies. Eventually, after a lot of experimentation I had some success with 600kHz. I was able to receive some of the messages I was sending (I think the CANbus receives it's own messages if everything is OK) but it's all a bit random. I've read that this can happen if the frequency is not exactly right. So far I haven't been able to receive any messages from my ECU box either.
Any help or clues would be very much appreciated, thanks in advance,
Sophie x
[edit: remove space at end of my post lol]