#include <bcm2835.h>
int main (int atgc, char** argv)
{
if (!bcm2835_init())
{
return 1;
}
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
//CLOCK_DIVIDER: more information is provided here
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536);
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
//An example which sends a byte and then receives a byte
char sendBits = 0b11111111;
char returnBits = 0;
while(true)
{
bcm2835_spi_transfer(sendBits);
//send dummy bytes for receiving
returnBits = bcm2835_spi_transfer(0);
}
}
The code on the ATMega is the following (the three SPI functions explained in ATmega - Hardware-SPI have to be present in the program):
#include <avr/io.h>
int main()
{
SPI_SlaveInit();
char returnBits = 0;
char sendBits = 0b11111111;
while(true)
{
data = SPI_SlaveReceive();
SPI_SlaveSend(sendBits);
}
}
I guess you've connected CS0 (RPI) to the SS' pin of the atmega here?
ReplyDeleteYeah, that's right. I connected SCLK-SCK, MISO-MISO, MOSI-MOSI and CE0-SS. You can read the pin assignment for the Raspberry PI here: http://elinux.org/RPi_Low-level_peripherals#General_Purpose_Input.2FOutput_.28GPIO.29 and for the ATmega8 here: http://www.atmel.com/Images/Atmel-2486-8-bit-AVR-microcontroller-ATmega8_L_summary.pdf
Delete