109 lines
2.9 KiB
C
109 lines
2.9 KiB
C
|
#include<avr/io.h>
|
||
|
#include "common.h"
|
||
|
|
||
|
String tosend="Test";
|
||
|
|
||
|
void Delay(uint32_t);
|
||
|
uint8_t SendReceiveSPIData(uint8_t);
|
||
|
void SendSPIDataADF4350 (uint32_t);
|
||
|
|
||
|
void spi_setup()
|
||
|
{
|
||
|
// set direction of PB1 (DO) and PB2 (USCK) as output
|
||
|
DDRB=(1<<PB1)|(1<<PB2);
|
||
|
|
||
|
// set to three wire mode (SPI)
|
||
|
USICR=(1<<USIWM0);
|
||
|
//USICR=(0<<USIWM1); not needed
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// set instructions to configure the ADF4350 settings for a 1 GHz +5dBm output
|
||
|
uint32_t ar0=0x500000;
|
||
|
uint32_t ar1=0x8008011;
|
||
|
uint32_t ar2=0x4e42;
|
||
|
uint32_t ar3=0x4b3;
|
||
|
uint32_t ar4=0xac803c;
|
||
|
uint32_t ar5=0x580005;
|
||
|
|
||
|
spi_setup();
|
||
|
}
|
||
|
|
||
|
// general purpose delay
|
||
|
void Delay(uint32_t tmax)
|
||
|
{
|
||
|
uint32_t i;
|
||
|
for (i=0;i < tmax ; i++)
|
||
|
{
|
||
|
asm("nop");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// send an 8 bit word via SPI1 and receive an 8 bit word at the same time
|
||
|
uint8_t SendReceiveSPIData(uint8_t value)
|
||
|
{
|
||
|
uint8_t lout = 0;
|
||
|
short int i=0;
|
||
|
|
||
|
for(i=0;i<=3;i++)
|
||
|
{
|
||
|
USIDR=value[i]; // write data bytes in Data register, will cause them to get sent on clock
|
||
|
while(USIOIF==0) // check USI data counter overflow flag to detect the end of transmission every byte
|
||
|
{
|
||
|
USICR|=(1<<USICLK)|(1<<USITC); // enable clock for transmission and generate clock for slave deivce
|
||
|
}
|
||
|
USISR|=(1<<USIOIF) // clear USI data counter overflow flag
|
||
|
}
|
||
|
|
||
|
// Read in a 16 bit frame (replace with proper reading response thing)
|
||
|
///uint16_t inbyte = *(uint32_t *)(SPI1_BASE + 0x0c);
|
||
|
while (lout == 0)
|
||
|
{
|
||
|
// do stuff to check response?
|
||
|
lout = 1;
|
||
|
|
||
|
// Read the SPI status register
|
||
|
///uint32_t statusSPI = *(uint32_t *)(SPI1_BASE + 0x08);
|
||
|
// Just bit 1 the transmit buffer empty flag - wait for this to go high then leave this function
|
||
|
/// &= BIT_1;
|
||
|
// The condition has been met so signal to get out of this loop
|
||
|
///if (statusSPI == BIT_1) lout = 1;
|
||
|
}
|
||
|
return inbyte;
|
||
|
}
|
||
|
|
||
|
// send a 32 bit register value to the ADF4350
|
||
|
void SendSPIDataADF4350 (uint32_t outval)
|
||
|
{
|
||
|
// split into 4 x 8-bit words
|
||
|
uint8_t byte1 = (outval & 0xFF000000) >> 24;
|
||
|
uint8_t byte2 = (outval & 0x00FF0000) >> 16;
|
||
|
uint8_t byte3 = (outval & 0x0000FF00) >> 8;
|
||
|
uint8_t byte4 = outval & 0x000000FF;
|
||
|
|
||
|
// send these to the ADF4350 via SPI
|
||
|
SendReceiveSPIData (byte1);
|
||
|
SendReceiveSPIData (byte2);
|
||
|
SendReceiveSPIData (byte3);
|
||
|
SendReceiveSPIData (byte4);
|
||
|
uint8_t lout = 0;
|
||
|
// Loop waiting for the the SPI BSY flag to go low
|
||
|
while (lout == 0)
|
||
|
{
|
||
|
// Read the SPI status register
|
||
|
uint32_t statusSPI = *(uint32_t *)(SPI1_BASE + 0x08);
|
||
|
// Just bit 7 the SPI BSY flag - wait for this to go low then leave this function
|
||
|
statusSPI &= BIT_7;
|
||
|
// The condition has been met so signal to get out of this loop
|
||
|
if (statusSPI == 0) lout = 1;
|
||
|
}
|
||
|
// Add a delay here so the clock has gone low before LE is taken high
|
||
|
Delay(10);
|
||
|
// Take LE high to load the data into the register
|
||
|
*(uint32_t *)(GPIOA_BASE + 0x10) = BIT_2;
|
||
|
// Short delay while LE is high (minimum of 20ns)
|
||
|
Delay(30);
|
||
|
// Take LE low again
|
||
|
*(uint32_t *)(GPIOA_BASE + 0x14) = BIT_2;
|
||
|
}
|