📄 s8051.c
字号:
/*
** ============================================================================
**
** FILE
** S8051.c
**
** DESCRIPTION
** Contains all the low level, 8051 dependent functions
**
** CREATED
** Silicon Laboratories Hungary Ltd
**
** COPYRIGHT
** Copyright 2008 Silicon Laboratories, Inc.
** http://www.silabs.com
**
** ============================================================================
*/
#include "S8051.h"
#include "timers.h"
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SetHwMasterSpi(void)
+
+ DESCRIPTION: Initialize the HW SPI port
+
+ INPUT: data
+
+ RETURN: None
+
+ NOTES: It doesn't control the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SetHwMasterSpi(void)
{
SPI1CFG = 0x40; //Master SPI, CKPHA=0, CKPOL=0
SPI1CN = 0x00; //3-wire Single Master, SPI enabled
SPI1CKR = (SYSCLK/(2*SPI_CLOCK))-1;
SPI1EN = 1; // Enable SPI1 module
//set nSEL pins to high
RF_NSEL_PIN = 1;
EE_NSEL_PIN = 1;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SpiWrite(uint8 spi_in)
+
+ DESCRIPTION: sends 8 length data through the SPI port
+
+ INPUT: data
+
+ RETURN: None
+
+ NOTES: It doesn't control the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiWrite(uint8 spi_in)
{
SPI1DAT = spi_in; //write data into the SPI register
while( SPIF1 == 0); //wait for sending the data
SPIF1 = 0; //clear interrupt flag
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: uint8 SpiReadWrite(uint8 data)
+
+ DESCRIPTION: sends and read 8 length data through the SPI port
+
+ INPUT: data
+
+ RETURN: received byte
+
+ NOTES: it doesn't control the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
uint8 SpiReadWrite(uint8 spi_in)
{
SPI1DAT = spi_in; //write data into the SPI register
while( SPIF1 == 0); //wait for sending the data
SPIF1 = 0; //clear interrupt flag
return SPI1DAT; //read received bytes
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SpiRfWriteWord(uint16 spi_in)
+
+ DESCRIPTION: sends 16 length data through the SPI port
+
+ INPUT: data
+
+ RETURN: None
+
+ NOTES: It controls the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiRfWriteWord(UU16 spi_in)
{
RF_NSEL_PIN = 0;
SpiWrite( spi_in.U8[MSB] );
SpiWrite( spi_in.U8[LSB] );
RF_NSEL_PIN = 1;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SpiRfWriteAddressData(uint8 address, uint8 data1)
+
+ DESCRIPTION: sends 16 length data through the SPI port (address and data)
+
+ INPUT: address - register address
+ data - 8bit data
+
+ RETURN: None
+
+ NOTES: It controls the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiRfWriteAddressData(uint8 address, uint8 d)
{
RF_NSEL_PIN = 0;
SpiWrite(address);
SpiWrite(d);
RF_NSEL_PIN = 1;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: uint16 SpiRfReadWriteWord(uint16 spi_in)
+
+ DESCRIPTION: sends and read 16 length data through the SPI port
+
+ INPUT: data
+
+ RETURN: received word
+
+ NOTES: it controls the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 SpiRfReadWriteWord(UU16 spi_in)
{
UU16 temp16;
RF_NSEL_PIN = 0;
temp16.U8[MSB] = SpiReadWrite( spi_in.U8[MSB] );
temp16.U8[LSB] = SpiReadWrite( spi_in.U8[LSB] );
RF_NSEL_PIN = 1;
return temp16;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: uint8 SpiReadRegister(uint8 address)
+
+ DESCRIPTION: Read a register of the radio
+
+ INPUT: address - register address
+
+ RETURN: value of the register
+
+ NOTES: it controls the nSEL pin of the radio
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
uint8 SpiRfReadRegister(uint8 address)
{
uint8 temp8;
RF_NSEL_PIN = 0;
SpiReadWrite( address );
temp8 = SpiReadWrite( 0x00 );
RF_NSEL_PIN = 1;
return temp8;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: uint16 SpiReadWriteAddressData(uint8 address, uint8 data1)
+
+ DESCRIPTION: sends and read 16 length data through the SPI port
+
+ INPUT: address - register address
+ data - data
+
+ RETURN: received word
+
+ NOTES: it controls the nSEL pin
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
UU16 SpiRfReadWriteAddressData(uint8 address, uint8 d)
{
UU16 temp16;
RF_NSEL_PIN = 0;
temp16.U8[MSB] = SpiReadWrite( address );
temp16.U8[LSB] = SpiReadWrite( d );
RF_NSEL_PIN = 1;
return temp16;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: uint8 SpiReadByteFromTestcardEEPROM(uint16 address)
+
+ DESCRIPTION: read on byte from the EEPROM populated to the Testcard
+
+ RETURN: value read out
+
+ INPUT: address of the data in the EEPROM
+
+ NOTES:
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
uint8 SpiReadByteFromTestcardEEPROM(uint16 address)
{
xdata uint8 temp8;
//select the EEPROM
EE_NSEL_PIN = 0;
//send instruction + address MSB
SpiWrite(0x03);
//send address upper byte
SpiWrite((uint8)(address >> 8));
//send address lower byte
SpiWrite((uint8)(address & 0x00FF));
//get data
temp8 = SpiReadWrite(0x00);
//release nSEL
EE_NSEL_PIN = 1;
return temp8;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SpiWriteByteToTestcardEEPROM(uint16 address, uint8 d)
+
+ DESCRIPTION: write one byte to the EEPROM populated on the Testcard
+
+ RETURN: None
+
+ INPUT: address, data
+
+ NOTES:
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiWriteByteToTestcardEEPROM(uint16 address, uint8 d)
{
UU16 temp;
EE_NSEL_PIN = 0;
SpiWrite(0x06);
EE_NSEL_PIN = 1;
//select the EEPROM
EE_NSEL_PIN = 0;
//send instruction + address MSB
SpiWrite(0x02);
//send address upper byte
SpiWrite((uint8)(address >> 8));
//send address lower byte
SpiWrite((uint8)(address & 0x00FF));
//send data
SpiWrite(d);
//release nSEL
EE_NSEL_PIN = 1;
temp.U16 = 3334;
StartTmr3(TMR3_12, temp, FALSE); //wait 5ms after every write circule
while( Tmr3Expired() == FALSE );
EE_NSEL_PIN = 0;
SpiWrite(0x04);
EE_NSEL_PIN = 1;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void SpiReadSegmentFromTestcardEEPROM(uint16 star_address, uint8 * data, uint8 length)
+
+ DESCRIPTION: read a segment from the EEPROM populated to the Testcard
+
+ RETURN: data stored in the place where the data pointer defined
+
+ INPUT: start_address - starting address of the segment needs to be read out
+ length - length of the segment needs to be read out
+
+ NOTES:
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SpiReadSegmentFromTestcardEEPROM(uint16 start_address, uint8 * d, uint8 length)
{
xdata uint8 temp8;
//select the EEPROM
EE_NSEL_PIN = 0;
//send instruction + address MSB
SpiWrite(0x03);
//send address upper byte
SpiWrite((uint8)(start_address >> 8));
//send address lower byte
SpiWrite((uint8)(start_address & 0x00FF));
for(temp8=0;temp8<length;temp8++)
{
//get data
*d++ = SpiReadWrite(0x00);
}
//release nSEL
EE_NSEL_PIN = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -