Howdy! I'm currently working on a project for my Formula SAE team implementing CAN bus nodes throughout our team's car. I have an ATmega32A MCU that I'm programming and since I have some familiarity with Arduino IDE and I like the simplicity of it, I decided to program this MCU using the SparkFun Pocket AVR programmer and a pin adaptation library.
The pin adaptation library works fine for pretty much everything, I'm able to manipulate all the digital pins, output SPI information directly from the Arduino sketch, do serial output etc. I'm using a CAN bus library from SeeedStudios since I have one of their Uno CAN shields. I know that this library works fine since it works fine with the Arduino Uno.
The problem is when I try to use the CAN bus library in sketches for the mega32. I've hooked up my oscilloscope and I'm not seeing any output whatsoever on the MOSI or SCK pin. The SS pin works just fine, but this is because the library modifies this pin using the digitalWrite(); command. The problem lies when this library tries to call the SPI.h library! I've done some debugging and it is clear that the program simply skips over the SPI.transfer commands.
void MCP_CAN::mcp2515_modifyRegister(const INT8U address, const INT8U mask, const INT8U data)
{
Serial.print("MCP2515_SELECT \r\n");
MCP2515_SELECT();
spi_readwrite(MCP_BITMOD);
spi_readwrite(address);
spi_readwrite(mask);
spi_readwrite(data);
delay(10);
Serial.print("MCP2515_UNSELECT \r\n");
MCP2515_UNSELECT();
}
This is the first function called in the sketch that involves SPI, and when I run my program it does indeed call this function (I know this from the Serial outputs showing it runs through it).
Here are the headers for this file:
#include "mcp_can.h"
#include "pins_arduino.h"
#define spi_readwrite SPI.transfer
#define spi_read() spi_readwrite(0x00)
Does anyone have any ideas why this code wouldn't work with my mega32? I imagine there is some problem with the fact that I'm using a programmer and a different board, but I'm not sure what to look at next. I'll attach the hardware change files and the full CAN library .cpp, .h and _dfs.h
I'm sorry if this is too much information, but any suggestions would be appreciated! I'll answer any questions, too.
Thanks!
mcp_can.cpp (32.3 KB)
mcp_can_dfs.h (9.96 KB)
mcp_can.h (6.03 KB)
pins_arduino.h (4.97 KB)
boards.txt (1.28 KB)