Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's this macro doing? #5

Closed
mcauser opened this issue Mar 4, 2017 · 2 comments
Closed

What's this macro doing? #5

mcauser opened this issue Mar 4, 2017 · 2 comments

Comments

@mcauser
Copy link

mcauser commented Mar 4, 2017

Inside /arduino/nRF905/nRF905.cpp, line 75.

Can you please explain what these lines are used for?

#define STANDBY (enableStandbyMode())
#define CHIPSELECT(standby) standby; \
							for(bool cs = cselect(); cs; cs = cdeselect())

Is the CHIPSELECT macro an optimised way of wrapping a block of code with cselect() + cdeselect() and optionally calling enableStandbyMode() first?

eg.

CHIPSELECT(STANDBY)
{
	spi_transfer_nr(NRF905_CMD_W_CONFIG | NRF905_REG_CHANNEL);
	spi_transfer_nr(channel);
	spi_transfer_nr(config.reg1);
}

I noticed the pattern is repeated in the NRF905_ATOMIC macro:

#define NRF905_ATOMIC() for(bool cs = interrupt_off(); cs; cs = interrupt_on())
@ZakKemble
Copy link
Owner

You've hit the nail on the head on what the macro does :P
The end compiled code is the same as if cselect() and cdeselect() were used directly. I use the macro as I find it a little bit neater, really. I first came across the idea when looking at how AVR-LIBC did the ATOMIC_BLOCK stuff http://www.nongnu.org/avr-libc/user-manual/atomic_8h_source.html
You can also exit the block early with a 'continue;' and cdeselect() will still be called (I can't remember what happens with a return or break).

CHIPSELECT can be used inside an ISR or if you use the STANDBY option you can use it outside an ISR.
If a section of code should not be interrupted by the ISR (like coping buffers) then it should be wrapped with NRF905_ATOMIC.

@mcauser
Copy link
Author

mcauser commented Mar 4, 2017

Nice work! I'm porting your library to MicroPython so I can mix Arduino nodes with pyboard (STM32) and ESP8266 nodes.
Will share the GitHub link once it's ready.

@mcauser mcauser closed this as completed Mar 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants