I'm trying to initialize SPI1 on PE7-PE10
#define spi1_select() SPI1->TC = 0x44
#define spi1_deselect() SPI1->TC = 0xC4
void spi1_init (void)
{
// Enable SPI1 clock
CCU->BUS_CLK_GATING0 |= (1 << 21); // SPI1 clock gating
CCU->BUS_SOFT_RST0 &= ~(1 << 21);
CCU->BUS_SOFT_RST0 |= (1 << 21);
// Initialize GPIO for SPI1
PE->CFG1= 0x4444; // PE7-CS, PE8-MOSI, PE9-CLK, PE10-MISO
// Set SPI1 clock rate to ~1 MHz (192 MHz / 256)
SPI1->CC = 256;
// Initialize SPI1 Global Control Register (GC)
for (SPI1->GC = 0x80000083; SPI1->GC & 0x80000000; ) {};
// De-select the SPI device
spi1_deselect();
// Initialize SPI1 FIFO Control Register (FC)
for (SPI1->FC = 0x80408001; SPI1->FC & 0x80008000; ) {};
}
void test_spi1(void) {
spi1_select();
SPI1->MBC = 4;
SPI1->MTC = 4;
SPI1->BCC = 4;
SPI1->TX.word = __builtin_bswap32((0x000100 & 0x00FFFFFF) | 0x03000000);
for(SPI1->TC |= (1U << 31); SPI1->TC & (1U << 31); ) {};
spi1_deselect();
}
在示波器上观察时,没有时钟或CS信号的变化。
可能是引脚配置不正确?
测试是在LicheePi Nano上进行的。
非常感谢您的帮助。
There is no clock or CS line effect when observed from oscilloscope.
Maybe the pins are not configured properly?
Testing is being done o n licheepi Nano.
Any help is very much appreciated.
离线
SPI clock needs to be set a bit differently. Bit 12 needs to be 1, then lower bits are n with 2*(n+1) being the AHB clock divisor.
SPI1->CC = 0x105F; in my config sets clock to 1MHz
PE pin config is split between CFG0 for PE7 and CFG1 for PE8,PE9,PE10
PE->CFG0 = 0x47777777; // PE7-CS, PE8-MOSI, PE9-CLK, PE10-MISO
PE->CFG1 = 0x77777444;
The rest is more or less correct. I have output on the SPI pins now. I can't say it's the correct format yet, but at least the scope shows CS, Clock and MOSI with data.
离线