// ========================================================================= // 函数功能:spi口初始化,本驱动中,cs由spi模块按gpio的output方式访问,故cs一律设 // 置为gpio输出功能。根据l138的引脚管理特点,pinmux中的设置优先于 // spi模块中的设置,如果在pinmux中把spi0的scs0引脚设置为GP2[14]的 // 话,该引脚将由系统gpio模块控制,spi模块对它的所有设置和操作均无 // 效。因此,配置spi之前,应该先在pinmux中把该引脚配置为spi的cs功 // 能,ti的文档有误,pinmux中只能把这些引脚分配给spi使用,而不能指 // 定其为cs还是gpio。是否作为cs使用,由spi模块决定 // 输入参数:tpSpi,被操作的spi控制结构的地址 // tagpInConfig,配置结构,包含配置信息 // 输出参数:无 // 返回值 :true=成功,false=失败 // ========================================================================= bool_t Spi_Init(volatile tagSpiReg * tpSpi, tagSpiConfig* tagpInConfig) { u32 u32Prescaler; if(tpSpi == g_ptSpi0Reg) Cpucfg_EnableLPSC(cn_PSC0_SPI0); else if(tpSpi == g_ptSpi1Reg) Cpucfg_EnableLPSC(cn_PSC1_SPI1); else return false; if (tagpInConfig != NULL) { // reset tpSpi port. tpSpi->GCR0 = 0; tpSpi->GCR0 |= CN_SPI_RESET; // config master/slave mode. if (SPI_MODE_MASTER == tagpInConfig->tagMode) { // set clkmod and master for master mode. tpSpi->GCR1 = CN_SPI_CLKMOD | CN_SPI_MASTER; } else if (SPI_MODE_SLAVE == tagpInConfig->tagMode) { // clear spigcr1 for slave mode. tpSpi->GCR1 = 0; } else { goto error_exit; } // config pin options. switch (tagpInConfig->tagPinOption) { case SPI_3PIN: // enable tpSpi SOMI, SIMO, and CLK. tpSpi->PC0 = SOMI | SIMO | CLK; break; case SPI_4PIN_CS: // enable tpSpi SOMI, SIMO, CLK, and set cs[0~7] as gpio. tpSpi->PC0 = SOMI | SIMO | CLK; tpSpi->PC0 &= ~all_cs; //所有片选脚设为gpio tpSpi->PC4 = all_cs; //拉高所有片选 tpSpi->PC1 |= all_cs; //所有片选脚设为输出 break; case SPI_4PIN_EN: // enable tpSpi SOMI, SIMO, CLK, and ENA. tpSpi->PC0 = SOMI | SIMO | CLK | ENA; break; case SPI_5PIN: // enable tpSpi SOMI, SIMO, CLK, SCS0, ENA and set cs[0~7] as gpio. tpSpi->PC0 = SOMI | SIMO | CLK | ENA; tpSpi->PC0 &= ~all_cs; //所有片选脚设为gpio tpSpi->PC4 = all_cs; //拉高所有片选 tpSpi->PC1 |= all_cs; //所有片选脚设为输出 break; default: goto error_exit; } // config tpSpi direction, polarity, and phase. tpSpi->FMT0 = 0; if (SPI_SHIFT_LSB == tagpInConfig->tagShiftDir) { tpSpi->FMT0 |= SHIFTDIR; } if (tagpInConfig->polarity) { tpSpi->FMT0 |= POLARITY; } if (tagpInConfig->phase) { tpSpi->FMT0 |= PHASE; } // set the u32Prescaler and character length. u32Prescaler = (((CN_CFG_PLL0_SYSCLK2 / tagpInConfig->freq) - 1) & 0xFF); //u32Prescaler = 0x80; tpSpi->FMT0 |= u32Prescaler << SHIFT_PRESCALE; tpSpi->FMT0 |= (tagpInConfig->char_len&0x1f)<< SHIFT_CHARLEN; tpSpi->DELAY = (8 << 24) | (8 << 16); // disable interrupts. tpSpi->INT = 0x00; tpSpi->LVL = 0x00; // enable tpSpi. tpSpi->GCR1 |= CN_SPI_ENABLE; } return true; error_exit: if(tpSpi == g_ptSpi0Reg) Cpucfg_DisableLPSC(cn_PSC0_SPI0); else if(tpSpi == g_ptSpi1Reg) Cpucfg_DisableLPSC(cn_PSC1_SPI1); return false; }
void Lcd_Display_Off(void) { Cpucfg_DisableLPSC(cn_PSC1_LCDC); }