📄 nbt_spi.c
字号:
//-------------------------------------------------------------
// _ _ ____ _____ ____ ____ ___ ____
// | \ | || __ )|_ _| / ___| | _ \|_ _| / ___|
// | \| || _ \ | | \___ \ | |_) || | | |
// | |\ || |_) | | | ___) || __/ | | _| |___
// |_| \_||____/ |_|_____|____/ |_| |___|(_)\____|
// |_____|
//
// (c) 2003 Altium
// Started: 04.12.2003 Ch.Weimann
// SPI driver Routines for NanoBoard Tester
// Adapted from Michael Rodway's Code
//-------------------------------------------------------------
#include "nbt_spi.h"
#include "hware.h"
#include "NBT_LCD.h"
#include "nbt_lcd.h"
#include "nbt_bargraph.h"
#include "nbt_kbd.h"
#include "stdio.h"
#define print printf
#ifndef BYTE
#define BYTE unsigned char
#endif
#ifndef BOOL
#define BOOL unsigned char
#endif
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define RESET_INTERNAL 0
#define RESET_EXTERNAL 1
/*----------- Platform specific functions for TSK51 processor ------------*/
inline void SPI_ModePin(BOOL state)
{
SPI_MODE = state;
}
inline void SPI_SelPin(BOOL state)
{
SPI_SEL = state;
}
inline void SPI_ClkPinHigh(void)
{
SPI_CLK = 1;
}
inline void SPI_ClkPinLow(void)
{
SPI_CLK = 0;
}
inline void SPI_DoutPin(BOOL state)
{
SPI_DOUT = state;
}
inline BOOL SPI_DinPin(void)
{
return SPI_DIN;
};
/*
// Reset CPU with internal or external memory selected.
void Reset(BOOL useExternal)
{
//Set desired
if (useExternal)
P0 |= 0x20;
else
P0 &= 0xdf;
// Activate reset
P0 &= 0x7f;
//Should never reach this line
for(;;);
}
*/
void SPI_startCommand(void);
/*
// Writes to external data memory goes to external program memory when set
void ProgramPin(BOOL state)
{
if (state)
P0 |= 0x40;
else
P0 &= 0xbf;
}
*/
//...............................................................................//
// Start an SPI command
void SPI_startCommand(void)
{
SPI_SelPin(FALSE);
SPI_ClkPinLow();
SPI_DoutPin(FALSE);
}
//...............................................................................//
// End an SPI command
void SPI_endCommand(void)
{
SPI_SelPin(TRUE);
SPI_ClkPinHigh();
SPI_DoutPin(TRUE);
}
//--------------------------------------------------------------------------------
// Send and receive a byte to the SPI device
//--------------------------------------------------------------------------------
unsigned char SPI_sendReceiveByte(unsigned char in)
{
BYTE i;
BYTE data = in;
SPI_ClkPinLow();
for(i=0; i<8; i++)
{
SPI_DoutPin(data & 0x80); // Send MSB
data = (data<<1) | SPI_DinPin(); // Read next bit
SPI_ClkPinHigh();
SPI_ClkPinLow();
}
return data;
}
//--------------------------------------------------------------------------------
// This writes to the SPI bus multiplexer on the Nanobord's FPGA and selects the
// SPI device.
//--------------------------------------------------------------------------------
void SPI_open(unsigned char device)
{
if(DEVICE_AUDIO_CODEC == device) // special case: select via multiplexer
{
SPI_ModePin(FALSE); // disable writes to Spartan 100
SPI_SEL_AUDIO = 1; // select SPI multiplexer for MAX1104EUA
return;
}
else
{
SPI_SEL_AUDIO = 0; // select SPI multiplexer for Nanoboard SPI bus
}
device |= 0x80; //Enable bus request bit in address register
SPI_ModePin(TRUE); //Access SPI address register in Nanoboard Controller
SPI_sendReceiveByte(device);
while (SPI_DinPin()==1) /* wait */;
SPI_ModePin(FALSE);
}
//--------------------------------------------------------------------------------
// Close SPI channel and clear the address register
//--------------------------------------------------------------------------------
void SPI_close(void)
{
SPI_SEL_AUDIO = 0; // select SPI multiplexer for Nanoboard SPI bus
SPI_ModePin(TRUE); //Release bus back to address select mode
SPI_sendReceiveByte(0); //Clear address latch
}
//----------------------------------------------------------------------------------//
// unsigned long flash_length = 0xffff; //Default length
// long flash_address = 0; //Default start address
// BYTE flash_device = DEVICE_FLASH_EMBEDDED; //Default memory device
//----------------------------------------------------------------------------------//
// read the electronic signature to wake the device
// also takes chip out of deep powerdown
//----------------------------------------------------------------------------------//
unsigned char Flash_readElectronicSignature(void)
{
BYTE result;
SPI_startCommand();
SPI_sendReceiveByte(M25P40_RES); // Send RES command
SPI_sendReceiveByte(0); // send three dummy bytes
SPI_sendReceiveByte(0);
SPI_sendReceiveByte(0);
result = SPI_sendReceiveByte(0); // Read signature
SPI_endCommand();
return result;
}
//----------------------------------------------------------------------------------//
// Send a 24 bit address to memory
//----------------------------------------------------------------------------------//
void Flash_sendAddress24(unsigned long flash_address)
{
SPI_sendReceiveByte(flash_address>>16); // Send bits 23..16
SPI_sendReceiveByte(flash_address>>8); // Send bits 15..8
SPI_sendReceiveByte(flash_address); // Send bits 7..0
}
//----------------------------------------------------------------------------------//
// Open a flash file for reading starting at 'address'
// subsequent SPI_sendReceiveByte commands will autoincrement the address
// leaves chip enable active
//----------------------------------------------------------------------------------//
void Flash_openRead(unsigned long address)
{
SPI_startCommand(); // set Chip enable line low
SPI_sendReceiveByte(M25P40_READ);
Flash_sendAddress24(address);
}
//----------------------------------------------------------------------------------//
/*
void Flash_copyToRAM(void)
{
BYTE data;
char __xdata *destAddress = (char __xdata *)0;
SPI_open(flash_device); //Open embedded flash memory
Flash_readElectronicSignature();
Flash_openRead();
while (flash_length>0)
{
data = SPI_sendReceiveByte(0); //Read next byte from flash
P1 = data; //Echo to LEDS
*(destAddress++) = data;
flash_length--;
}
SPI_close();
P1 = 0;
}
//----------------------------------------------------------------------------------//
//Load program and data from flash memory
void Flash_loader(void)
{
//Load program into memory
ProgramPin(TRUE);
flash_address = 0; //Start of program in flash
flash_length = 0xffff; //Length of program
flash_device = DEVICE_FLASH_CONFIGURATION;
Flash_copyToRAM();
//Load data into memory
ProgramPin(FALSE);
flash_address = 0x10000; //Start of data in flash
flash_length = 0xffff; //Length of data in RAM
flash_device = DEVICE_FLASH_EMBEDDED;
Flash_copyToRAM();
Reset(RESET_EXTERNAL); //Reset CPU and execute from external program memory
}
//----------------------------------------------------------------------------------//
void main(void)
{
Flash_loader();
}
*/
//------------------------------------------------------------------------------
// Programs the ICS307 to generate a new output frequency
// Output Frequency for CLK1 = Fref * 2 * (VDW + 8) / ((RDW + 2) * OD)
// Parameters:
// Config: Configuration word :
//
// MSB LSB
// +----+----+-----+----+----+----+----+----+
// | C1 C0 | TTL | F1 F0 | S2 S1 S0 |
// +----+----+--+--+--+-+-+--+-+--+----+--+-+
// | | | | | | |
// +-+-+ | +-+-+ +-----+----+
// | | | |
// Crystal Output CLK2 Output Divide
// Load Duty Output
// Impedance Cycle Select
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -