eebhoi01
Advanced Member level 4
Hi,
I am a newbie to SPI programming and this is my first attempt in learning and coding SPI protocol, I am using Atmel Studio 7 (w/ Atmel 644). My friend gave me a code, for my reference, regarding SPI. I modified the code so that it will work as how I needed it to be. Unfortunately, I got this error "ld returned 1 exit status".
for my SPI.h file
for my SPI.cpp File
Can anyone enlighten me please, why I get such error? I reviewed everything I learned so far, but I can't seem to comprehend this particular error. Thank you for your help and time.
Regards,
eebhoi01
I am a newbie to SPI programming and this is my first attempt in learning and coding SPI protocol, I am using Atmel Studio 7 (w/ Atmel 644). My friend gave me a code, for my reference, regarding SPI. I modified the code so that it will work as how I needed it to be. Unfortunately, I got this error "ld returned 1 exit status".
for my SPI.h file
Code:
#ifndef _SPI_H
#define _SPI_H
#include <avr/io.h>
#define LSB = 0;
#define MSB = 1;
//Defining SPI pins configuration ATMEGA 644P
#define SPI_SS 0x10;
#define SPI_MOSI 0x20;
#define SPI_MISO 0x00;
#define SPI_SCK 0x80;
#define SPI_PORT PORTB
#define PIN_SPI PINB
#define BIT_SPI_MISO 3
//Defining SPI function configuration
#define SPI_CLCK_Div4 0x00;
#define SPI_CLCK_Div16 0x01;
#define SPI_ClCK_Div64 0x02;
#define SPI_CLCK_Div128 0x03;
#define SPI_CLCK_Div2 0x04;
#define SPI_CLCK_Div8 0x05;
#define SPI_CLCK_Div32 0x06;
#define SPI_Mode0 0x00;
#define SPI_Mode1 0x04;
#define SPI_Mode2 0x08;
#define SPI_Mode3 0x0C;
#define SPI_MODE_MASK 0x0C;
#define SPI_CLOCK_MASK 0x03;
#define SPI_2XCLOCK_MASK 0X01;
#define WAIT_SPI() while(!(SPSR & _BV(SPIF)))
class SPI
{
public:
void init();
uint8_t send(uint8_t value);
inline static uint8_t transfer(uint8_t _data, uint8_t mode);
// SPI Configuration methods
inline static void attachInterrupt();
inline static void detachInterrupt(); // Default
static void end();
static void setBitOrder(uint8_t);
static void setDataMode(uint8_t);
static uint8_t getDataMode();
static void setClockDivider(uint8_t);
};
extern SPI objSPI;
uint8_t SPI::transfer(uint8_t _data, uint8_t mode)
{
uint8_t ret,prevmode;
prevmode = getDataMode();
setDataMode(mode);
SPDR = _data;
while (!(SPSR & _BV(SPIF)))
;
ret = SPDR;
setDataMode(prevmode);
return ret;
}
#endif
for my SPI.cpp File
Code:
#include "SPI.h"
void SPI::init()
{
SPI_PORT |= _BV(SPI_SS);
// Configure SPI pins
DDR(SPI_PORT) &= ~_BV(SPI_MISO);
DDR(SPI_PORT) |= _BV(SPI_SS);
SPI_PORT &= ~_BV(SPI_MOSI);
SPI_PORT |= _BV(SPI_SS);
// SPI speed = clk/4
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
DDR(SPI_PORT) |= _BV(SPI_MOSI);
DDR(SPI_PORT) |= _BV(SPI_SCK);
}
uint8_t SPI::send(uint8_t value)
{
SPDR = value; // Transfer uint8_t via SPI
WAIT_SPI(); // Wait until SPI operation is terminated
return SPDR;
}
void SPI::setBitOrder(uint8_t bitOrder)
{
if(bitOrder == LSB) {
SPCR |= _BV(DORD);
} else {
SPCR &= ~(_BV(DORD));
}
}
void SPI::setDataMode(uint8_t mode)
{
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
if (mode == SPI_Mode3 || mode==SPI_Mode2)
{
SPI_PORT |= _BV(SPI_SCK);
}
else
{
SPI_PORT &= ~_BV(SPI_SCK);
}
}
uint8_t SPI::getDataMode()
{
return (SPCR & SPI_MODE_MASK);
}
void SPI::setClockDivider(uint8_t rate)
{
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
}
void SPI::attachInterrupt() {
SPCR |= _BV(SPIE);
}
void SPI::detachInterrupt() {
SPCR &= ~_BV(SPIE);
}
SPI objSPI;
Can anyone enlighten me please, why I get such error? I reviewed everything I learned so far, but I can't seem to comprehend this particular error. Thank you for your help and time.
Regards,
eebhoi01