⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 z1slaveio.c

📁 nordic 2.4G RF transceiver nRF24z01 firmware source code.
💻 C
字号:
/*= z1slaveio.c ================================================================
 *
 * Copyright (C) 2005 Nordic Semiconductor
 *
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * WARRANTY OF ANY KIND.
 *
 * Author(s): B鴕ge Strand
 *
 * Description:
 *
 *   Various functions regarding reading and writing z1 internal registers
 *   General file, no reference to specific hardware
 *   All functions named "z1_..." for nRF24Z1
 * 
 * Compiler: Tested with WinAVR, avr-gcc (GCC) 3.4.3
 *
 * Revision: 1.0 
 *
 *==============================================================================
 */


#include "z1slaveio.h"


// Read a single byte from Z1's slave interface
char z1_singleread(char adr) {
	extern char slaveinbuf[];						// Global data buffer from Z1 slave to MCU

	z1_multiread (adr, adr);						// Start and end addresses are the same
	return slaveinbuf[0];							// Give back the one byte that was read
}


// Write a single byte to Z1's slave interface
void z1_singlewrite(char adr, char data) {
	extern char slaveoutbuf[]; 					// Global data buffer from MCU to Z1 slave

	slaveoutbuf[0] = data;							// Prepare to output a single byte
	z1_multiwrite(adr, adr);						// Start and end addresses are the same
}


// Read multiple bytes from Z1's slave interface
void z1_multiread(char startadr, char endadr) {
	#ifdef SLAVE_SPI
		// Set the MSB of addresses to 1 for read acces
		mcu_spicycle(startadr|0x80, endadr|0x80); 		// Do the actual transfer
	#endif
	#ifdef SLAVE_2W
		mcu_2wread(startadr, endadr);
	#endif
}


// Write multiple bytes to Z1's slave interface
void z1_multiwrite(char startadr, char endadr) {
	#ifdef SLAVE_SPI
		// Set the MSB of addresses to 0 for write acces
		mcu_spicycle(startadr&0x7F, endadr&0x7F); 		// Do the actual transfer
	#endif
	#ifdef SLAVE_2W
		mcu_2wwrite(startadr, endadr);
	#endif
}


// Wait until a flag indicates that registers are ready. Time out if it takes too long!
char z1_flagready(char flag) {
	char inbyte;
	int counter = MAXPOLLITER;
	error = OKAY;									// Nothing wrong found yet!
	
	while (counter-- > 0) {						// Only run permitted number of times
		inbyte = z1_singleread(flag);				// Read the flag through slave interface
		if (inbyte==FLAGREADY)						// Flag is ready, 
			return OKAY;							// all is well
		if (inbyte==Z1TIMEOUT)						// 0x02 means internal Z1 timeout
			return TIMEOUT;						// Here if Z1 wasn't able to complete a transfer
	}			
	return TIMEOUT;								// Sorry, this function timed out!
}


// Write 0x01 to a flag in order to initiate a transfer etc.
void z1_setflag(char flag) {
	z1_singlewrite (flag, 0x01);
}


// Check if z1 has a link, this is a simple binary test!
char z1_haslink(void) {
	if (z1_singleread(LNKSTA)==LINKPRESENT)		// Flag is ready, 
			return 1;								// all is well
	else 
		return 0;
}


// Reads a single byte from a Microchip 25AA640 or compatible SPI EEPROM on the ARX
char z1_read_eeprom(char adrh, char adrl) {
	extern char error;								// Use the global error register 
	// Maybe rather use pointers for data and return TIMEOUT/OKAY?

	// How to read from external EEPROM:
	// Put command bytes into Z1 ATX's RXBUF write buffer, max 16 bytes
	// Update RXWCNT with the number of command bytes
	// Update RXRCNT with the number of bytes to be received, max 16 bytes
	// RXRCNT must be 1 if the EEPROM doesn't have auto increment
	// Execute the transfer by writing to the RXEXEC flag
	// Get the result back from RXBUF register

	error = OKAY;
	if (z1_flagready(RXEXEC)==OKAY) {
		z1_singlewrite(RXDCMD, 0x8c);				// Use ARX's master SPI interface at 250kbps
		z1_singlewrite(RXBUF_0, 0x03);				// Buffer 0x03 for read command to 25AA640
		z1_singlewrite(RXBUF_1, adrh);				// Buffer most significant address to 25AA640
		z1_singlewrite(RXBUF_2, adrl);				// Buffer least significant address to 25AA640
		z1_singlewrite(RXWCNT, 0x03);				// We are going to send 3 bytes to 25AA640
		z1_singlewrite(RXRCNT, 0x01);				// We are going to read 1 byte from 25AA640
		z1_setflag(RXEXEC);							// Execute the read/write transfer
	
		if (z1_flagready(RXEXEC)==OKAY)			// Must wait a while before the answer is ready, 
			return z1_singleread(RXBUF_0);			// Return the one byte from the ATX's read buffer
	}
	error = TIMEOUT;
	return 0;
}


// Write a single byte to a Microchip 25AA640 or compatible SPI EEPROM on the ARX
char z1_write_eeprom(char adrh, char adrl, char data) {

	// How to write to external EEPROM:
	// Put command bytes into Z1 ATX's RXBUF write buffer, max 16 bytes
	// Update RXWCNT with the number of command bytes
	// Repeat for write enable command and then the actual write
	// Execute the transfer by writing to the RXEXEC flag

	if (z1_flagready(RXEXEC)==OKAY) {
		z1_singlewrite(RXDCMD, 0x8c);				// Use ARX's master SPI interface at 250kbps
		z1_singlewrite(RXBUF_0, 0x06);				// Buffer 0x06 for write enable command to 25AA640
		z1_singlewrite(RXWCNT, 0x01);				// We are going to send 1 byte to 25AA640
		z1_singlewrite(RXRCNT, 0x00);				// We are going to read 0 bytes from 25AA640
		z1_setflag(RXEXEC);							// Execute the write enable command
		
		if (z1_flagready(RXEXEC)==OKAY) {			// If the write enable came through, go for the write
			z1_singlewrite(RXBUF_0, 0x02);			// Buffer 0x02 for write command to 25AA640
			z1_singlewrite(RXBUF_1, adrh);			// Buffer most significant address to 25AA640
			z1_singlewrite(RXBUF_2, adrl);			// Buffer least significant address to 25AA640
			z1_singlewrite(RXBUF_3, data);			// Buffer the data byte to 25AA640
			z1_singlewrite(RXWCNT, 0x04);			// We are going to send 3 bytes to 25AA640
			z1_singlewrite(RXRCNT, 0x00);			// We are going to read 0 bytes from 25AA640
			z1_setflag(RXEXEC);						// Execute the read/write transfer
			return OKAY;
		}
	}
	return TIMEOUT;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -