/** * Reads multiple bytes from a register. * * @param reg register to read from. * @param count number of bytes to read. * @param values target buffer. * @return pointer to the input buffer + count. */ uint8_t * max3421e_readMultiple(uint8_t reg, uint8_t count, uint8_t * values) { // Pull slave-select high to initiate transfer. MAX_SS(0); // Send a command byte containing the register number. SPDR = reg; while (!(SPSR & (1 << SPIF))); //wait // Read [count] bytes. while (count--) { // Send empty byte while reading. SPDR = 0; while (!(SPSR & (1 << SPIF))); *values = SPDR; values++; } // Pull slave-select low to signal transfer complete. MAX_SS(1); // Return the byte array + count. return (values); }
/* * Initialises the max3421e host shield. Initialises the SPI bus and sets the required pin directions. * Must be called before powerOn. */ void max3421e_init() { spi_begin(); #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Set MAX_INT and MAX_GPX pins to input mode. DDRH &= ~(0x40 | 0x20); // Set SPI !SS pint to output mode. DDRB |= 0x10; // Set RESET pin to output DDRH |= 0x10; #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Set MAX_INT and MAX_GPX pins to input mode. DDRB &= ~0x3; // Set RESET pin to output DDRD |= 0x80; // Set SS pin to output DDRB |= 0x4; #endif // Sparkfun botched their first attempt at cloning Oleg's max3421e shield and reversed the GPX and RESET pins. // This hack is in place to make MicroBridge work with those shields. (see http://www.sparkfun.com/products/9628) #ifdef SFHACK #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Set MAX_GPX pin to input mode. DDRH &= ~0x10; // Set RESET pin to output DDRH |= 0x20; #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Set GPX pin to input DDRD &= ~0x80; // Set RESET pin to output DDRB |= 0x1; #endif #endif // Pull SPI !SS high MAX_SS(1); // Reset MAX_RESET(1); }
/** * Writes a single register. * * @param reg register address. * @param value value to write. */ void max3421e_write(uint8_t reg, uint8_t value) { // Pull slave select low to indicate start of transfer. MAX_SS(0); // Transfer command byte, 0x02 indicates write. SPDR = (reg | 0x02); while (!(SPSR & (1 << SPIF))); // Transfer value byte. SPDR = value; while (!(SPSR & (1 << SPIF))); // Pull slave select high to indicate end of transfer. MAX_SS(1); return; }
/** * Reads a single register. * * @param reg register address. * @return result value. */ uint8_t max3421e_read(uint8_t reg) { // Pull slave-select high to initiate transfer. MAX_SS(0); // Send a command byte containing the register number. SPDR = reg; while (!(SPSR & (1 << SPIF))); // Send an empty byte while reading. SPDR = 0; while (!(SPSR & (1 << SPIF))); // Pull slave-select low to signal transfer complete. MAX_SS(1); // Return result byte. return (SPDR); }
/* * Initialises the max3421e host shield. Initialises the SPI bus and sets the required pin directions. * Must be called before powerOn. */ void max3421e_init() { SPI.begin(); #ifndef __AVR_ATmega2560__ pinMode(PIN_MAX_INT, INPUT); pinMode(PIN_MAX_GPX, INPUT); pinMode(PIN_MAX_SS, OUTPUT); pinMode(PIN_MAX_RESET, OUTPUT); #endif #ifdef __AVR_ATmega2560__ pinMode(PIN_MAX_SS, OUTPUT); DDRE &= ~ 0x40; // MAX_INT as input DDRJ &= ~ 0x08; //MAX_GPX as input DDRJ |= 0x04; //MAX_RESET as output #endif /* #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) // Set MAX_INT and MAX_GPX pins to input mode. DDRH &= ~(0x40 | 0x20); // Set SPI !SS pint to output mode. DDRB |= 0x10; // Set RESET pin to output DDRH |= 0x10; #elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Set MAX_INT and MAX_GPX pins to input mode. DDRB &= ~0x3; // Set RESET pin to output DDRD |= 0x80; // Set SS pin to output DDRB |= 0x4; #endif */ // Pull SPI !SS high MAX_SS(1); // Reset MAX_RESET(1); }
/** * Writes multiple bytes to a register. * @param reg register address. * @param count number of bytes to write. * @param vaues input values. * @return a pointer to values, incremented by the number of bytes written (values + length). */ uint8_t * max3421e_writeMultiple(uint8_t reg, uint8_t count, uint8_t * values) { // Pull slave select low to indicate start of transfer. MAX_SS(0); // Transfer command byte, 0x02 indicates write. SPDR = (reg | 0x02); while (!(SPSR & (1 << SPIF))); // Transfer values. while (count--) { // Send next value byte. SPDR = (*values); while (!(SPSR & (1 << SPIF))); values++; } // Pull slave select high to indicate end of transfer. MAX_SS(1); return (values); }