I need to use SPI and EXTCLK simultaneously in ATtiny 0/1-series (SOIC-8) and as you know PIN7 (PA3) functions EXTCLK/SCK. is there any way or trick to multiplex SCK to other pin?
Thanks for any suggestions. Ata
I need to use SPI and EXTCLK simultaneously in ATtiny 0/1-series (SOIC-8) and as you know PIN7 (PA3) functions EXTCLK/SCK. is there any way or trick to multiplex SCK to other pin?
Thanks for any suggestions. Ata
SPI is very easy to bit bang. You can do it in software on any two pins.
There are many software libraries that will do this. Pick one depending on if you need SPI master or slave. Or write your own, it is a very simple protocol.
There is one tricky way. Consider Attiny402, External clock 2MHz. PA1 as MOSI, PA3 as clock input (EXTCLK), PA7 as "rerouted" SPI SCK output. Trick is routing SPI SCK internaly to CCL LUT, configure LUT to pass SCK to its output.
#define F_CPU 2000000 // my External clock frequency
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA,CLKCTRL_CLKSEL_EXTCLK_gc);
PORTA.DIRSET = PIN7_bm; // LUT1_OUT as Output
PORTA.DIRSET = PIN1_bm; // MISO as output
// Set LUT1 INSEL0 input as SPI0 (SCK), other two inputs to "mask" (logical 0)
CCL.LUT1CTRLB = CCL_INSEL0_SPI0_gc | CCL_INSEL1_MASK_gc;
CCL.LUT1CTRLC = CCL_INSEL2_MASK_gc;
// Set thruth table to copy INSEL0 value
CCL.TRUTH1 = 0b10;
// Enable LUT and its output (PA7)
CCL.LUT1CTRLA = CCL_OUTEN_bm | CCL_ENABLE_bm;
// Enable CCL
CCL.CTRLA = CCL_ENABLE_bm;
// Setup SPI as Master (with buffer if you need)
SPI0.CTRLB = SPI_SSD_bm;
SPI0.CTRLA = SPI_MASTER_bm | SPI_ENABLE_bm | SPI_PRESC_DIV4_gc;
while (1)
{
SPI0.DATA = 0b11110110; // transmit data
while(!(SPI0.INTFLAGS & SPI_IF_bm)){} // wait until transmited
_delay_ms(1); // do some pause
}
}
If needed, LUT output can be again "rerouted" to EVOUT0 (PA2) using event system.