target specific spi funtions
							parent
							
								
									09e741a3ea
								
							
						
					
					
						commit
						939c3c5e63
					
				| @ -1,50 +1,37 @@ | |||||||
| #include <avr/io.h> |  | ||||||
| #include "spi.h" | #include "spi.h" | ||||||
| #include "globals.h" |  | ||||||
| 
 | 
 | ||||||
| /*
 | void spi_select(void) | ||||||
|  * Initialize SPI bus. | { | ||||||
|  */ |     CS_PORT&=~(1<<CS_BIT); | ||||||
| 
 | } | ||||||
| //~ // From working SPI ENC28J60 driver
 |  | ||||||
| //~ #define ENC28J60_CONTROL_PORT   PORTB
 |  | ||||||
| //~ #define ENC28J60_CONTROL_DDR    DDRB
 |  | ||||||
| //~ 
 |  | ||||||
| //~ #define ENC28J60_CONTROL_CS PORTB6
 |  | ||||||
| //~ #define ENC28J60_CONTROL_SO PORTB3
 |  | ||||||
| //~ #define ENC28J60_CONTROL_SI PORTB2
 |  | ||||||
| //~ #define ENC28J60_CONTROL_SCK PORTB1
 |  | ||||||
| //~ #define ENC28J60_CONTROL_SS PORTB0
 |  | ||||||
| //~ 
 |  | ||||||
| //~ // set CS to 0 = active
 |  | ||||||
| //~ #define CSACTIVE ENC28J60_CONTROL_PORT&=~(1<<ENC28J60_CONTROL_CS)
 |  | ||||||
| //~ // set CS to 1 = passive
 |  | ||||||
| //~ #define CSPASSIVE ENC28J60_CONTROL_PORT|=(1<<ENC28J60_CONTROL_CS)
 |  | ||||||
| //
 |  | ||||||
| //~ #define waitspi() while(!(SPSR&(1<<SPIF)))
 |  | ||||||
| 
 | 
 | ||||||
|  | void spi_deselect(void) | ||||||
|  | { | ||||||
|  |     CS_PORT|=(1<<CS_BIT); | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| void | unsigned char spi_xchg(unsigned char val) | ||||||
| spi_init(void) |  | ||||||
| { | { | ||||||
|   // CS PIN for FLASH
 | 	SPDR = val; | ||||||
|   DDRB	|= _BV(WIZNET_CS); // CS to OUT && Disable
 | 	while (!(SPSR & (1 << SPIF))) ; | ||||||
|   SPI_WIZNET_DISABLE(); | 	return SPDR; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|   /* Initalize ports for communication with SPI units. */ | uint8_t spi_read(){ | ||||||
|   /* CSN=SS and must be output when master! */ | 	return spi_xchg(0x00); | ||||||
|   DDRB  |= _BV(MOSI) | _BV(SCK) | _BV(CSN); | } | ||||||
|   PORTB |= _BV(MOSI) | _BV(SCK); |  | ||||||
| 
 | 
 | ||||||
|   /* Enables SPI, selects "master", clock rate FCK / 4 - 4Mhz, and SPI mode 0 */ | void spi_write(uint8_t d){ | ||||||
|   SPCR = _BV(SPE) | _BV(MSTR); | 	spi_xchg(d); | ||||||
| #if defined(SPI_8_MHZ) | } | ||||||
|   SPSR = _BV(SPI2X); //FCK / 2 - 8Mhz
 |  | ||||||
| #elif defined (SPI_4_MHZ) |  | ||||||
|   SPSR = 0x0; //FCK / 4 - 4Mhz
 |  | ||||||
| #else |  | ||||||
|   SPSR = 0x0; //FCK / 4 - 4Mhz
 |  | ||||||
| #endif |  | ||||||
| 
 | 
 | ||||||
|  | void spi_init(){ | ||||||
|  | 	CS_PORT |= (1 << CS_BIT);	// pull CS pin high
 | ||||||
|  | 	CS_DDR |= (1 << CS_BIT);	// now make it an output
 | ||||||
| 
 | 
 | ||||||
|  | 	SPI_PORT |= (1 << 0);	// make sure SS is high
 | ||||||
|  | 	SPI_DDR = (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0);	// set MOSI, SCK and SS as output, others as input
 | ||||||
|  | 	SPCR = (1 << SPE) | (1 << MSTR);	// enable SPI, master mode 0
 | ||||||
|  | 	SPCR |= (1 << SPR0) | (0<<SPR1); // div 128
 | ||||||
|  | 	SPSR |= (0 << SPI2X);	// set the clock rate fck/2
 | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue