jeelabs / jeelib

JeeLib for Arduino IDE: Ports, RF12, and RF69 drivers from JeeLabs
https://jeelabs.org/202x/sw/jeelib/
The Unlicense
489 stars 215 forks source link

Fix RFM69 SPI init for ATMega32u4 #73

Closed mharizanov closed 9 years ago

mharizanov commented 9 years ago

Hi JCW, The pin definitions suppose pinMode usage rather than direct port manipulation, hence the SPI init was failing. One option is to use pinMode and keep the definitions, alternatively the defines may be changed and the direct port manipulation to remain..

jcw commented 9 years ago

This introduces Arduino-specific code, would it be ok to just fix the RFM_IRQ?

Update: ah, didn't read your comments. I'd prefer to keep this Arduino-independent, if it's easy to do.

mharizanov commented 9 years ago

The wrong RFM_IRQ issue is just part of the problem, the thing is that there is discrepancy between the RFM12 SPI init and RFM69 SPI Init.

rf12_spiInit uses Arduino code (pinMode, see https://github.com/jcw/jeelib/blob/master/RF12.cpp#L166 ) to init the SPI, thus the SPI pin defines use Arduino pin number rather than port bit.

For direct port manipulation we would need the pin bit within the port, so that _BV would return correct mask:

define _BV(bit) (1 << (bit))

Copying these defines over to the RF69 init code doesn't work as the RFM69 spiConfigPins uses direct port manipulation with the same pin defines copied over from the RFM12 code which uses pinMode.

This is alternative fix, using the correct defines for direct port manipulation; however this would make RFM12 and RFM69 defines different

elif defined(AVR_ATmega32U4) //Arduino Leonardo

define RFM_IRQ 0 // PD0, INT0, Digital3

define SS_DDR DDRB

define SS_PORT PORTB

define SS_BIT 6 // Dig10, PB6

define SPI_SS 0 // PB0, pin 8, Digital17

define SPI_MISO 3 // PB3, pin 11, Digital14

define SPI_MOSI 2 // PB2, pin 10, Digital16

define SPI_SCK 1 // PB1, pin 9, Digital15

static void spiConfigPins () { SS_PORT |= _BV(SS_BIT); SS_DDR |= _BV(SS_BIT); PORTB |= _BV(SPI_SS); DDRB |= _BV(SPI_SS) | _BV(SPI_MOSI) | _BV(SPI_SCK); }

jcw commented 9 years ago

I'll go with your latter suggestion - pulling in the Arduino runtime is not always practical have committed the changes, am cancelling this pull request...