void SPI_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init SPI */ /*---------------------------------------------------------------------------------------------------------*/ /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */ /* Set IP clock divider. SPI clock rate = 2MHz */ SPI_Open(SPI_FLASH, SPI_MASTER, SPI_MODE_0, 8, SPI_FLASH_CLK); SPI_Open(SPI_SRAM, SPI_MASTER, SPI_MODE_0, 8, SPI_SRAM_CLK); }
void Display_SetupSPI() { // Setup output pins PA0 = 0; GPIO_SetMode(PA, BIT0, GPIO_MODE_OUTPUT); PA1 = 0; GPIO_SetMode(PA, BIT1, GPIO_MODE_OUTPUT); PC4 = 0; GPIO_SetMode(PC, BIT4, GPIO_MODE_OUTPUT); PE10 = 0; GPIO_SetMode(PE, BIT10, GPIO_MODE_OUTPUT); PE12 = 0; GPIO_SetMode(PE, BIT12, GPIO_MODE_OUTPUT); // Setup MFP SYS->GPB_MFPL &= ~(SYS_GPE_MFPH_PE11MFP_Msk | SYS_GPE_MFPH_PE12MFP_Msk | SYS_GPE_MFPH_PE13MFP_Msk); SYS->GPE_MFPH |= SYS_GPE_MFPH_PE11MFP_SPI0_MOSI0 | SYS_GPE_MFPH_PE12MFP_SPI0_SS | SYS_GPE_MFPH_PE13MFP_SPI0_CLK; // SPI0 master, MSB first, 8bit transaction, SPI Mode-0 timing, 4MHz clock SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 8, 4000000); // Low level active SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW); // Start SPI SPI_ENABLE(SPI0); }
void SPIClass::begin() { if(init_flag==0); //init(); /* Unlock protected registers */ SYS_UnlockReg(); /* Enable IP clock */ CLK_EnableModuleClock(SPI1_MODULE); /* Select IP clock source and clock divider */ CLK_SetModuleClock(SPI1_MODULE,CLK_CLKSEL1_SPI1_S_HCLK,MODULE_NoMsk); SYS->GPC_MFP |= SYS_GPC_MFP_PC11_SPI1_MOSI0; SYS->GPA_MFP |= SYS_GPA_MFPH_GPA10_MFP_SPI1_MISO0 | SYS_GPA_MFPH_GPA11_MFP_SPI1_CLK; SYS->ALT_MFP |= SYS_ALT_MFP_PC11_SPI1_MOSI0 | SYS_ALT_MFP_PA11_SPI1_CLK | SYS_ALT_MFP_PA10_SPI1_MISO0; /* Lock protected registers */ SYS_LockReg(); /* Configure as a master, clock idle low, falling clock edge Tx, rising edge Rx and 8-bit transaction */ /* Set IP clock divider. SPI clock rate = 12MHz */ SPI_Open(spi, SPI_MASTER, SPI_MODE_0, 8, 6000000); //SPI_EnableFIFO(spi,2,2); //ouki setBitOrder(SS, MSBFIRST); }
void ConfigHWSpiport(void) { // plese ensure has opened the system clock of SPI1 mode. CLK_EnableModuleClock(SPI1_MODULE); //1st . config pin MFP function /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* SPI1: PC15=MOSI0, PD0=MISO0, PD1=CLK */ //note :there is a qustion that for the Register option must be "OR" bits. //othewith, will occured changed other Hardwave bits. SYS->GPC_MFPH |= (SYS_GPC_MFPH_PC15MFP_SPI1_MOSI0); SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD0MFP_SPI1_MISO0 | SYS_GPD_MFPL_PD1MFP_SPI1_CLK); //2st . config the SPI1 of Hardwave Port /* Configure SPI1 as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 400KHz */ SPI_Open(SPI1, SPI_MASTER, SPI_MODE_0, 8, 400000); //open SPI1 and config it. SPI_DisableAutoSS(SPI1);//closed the ss by hardwave and give it for softwave ctrl that well used ctrl SS/HSA pin select ADE7878 into the SPI mode. SPI_TRIGGER(SPI1);//start SPI1 transf. //3st . config the SS/HSA pin GPIO_SetMode(PC, BIT12, GPIO_MODE_OUTPUT);// SP1 SS pin by SOFTWAVE ctrl PC12=1;// hold the pin hight.. //2016 01 03 make by wangjunwei in bittel. }
void SPIClass::begin() { if(init_flag==0) init(); /* Unlock protected registers */ SYS_UnlockReg(); /* Enable IP clock */ CLK_EnableModuleClock(module); /* Select IP clock source and clock divider */ CLK_SetModuleClock(module,clksel,0); /* Lock protected registers */ SYS_LockReg(); /* Configure as a master, clock idle low, falling clock edge Tx, rising edge Rx and 8-bit transaction */ /* Set IP clock divider. SPI clock rate = 4MHz */ SPI_Open(spi, SPI_MASTER, SPI_MODE_0, 8, 4000000); #if defined(__NUC240__) | defined(__NANO100__) | defined(__NUC131__) SPI_EnableFIFO(spi,12,12); #endif setBitOrder(SS, MSBFIRST); #if defined(__M451__) SPI_ClearRxFIFO(spi); SPI_TRIGGER(spi); #endif }
void Scan_WriteData(uint16_t valueX, uint16_t valueY) { uint8_t valXL,valYL,valXH,valYH; valYH = (uint8_t)(valueY>>8); valXH = (uint8_t)(valueX>>8); valYL = (uint8_t)valueY; valXL = (uint8_t)valueX; SPI_Open(); SPI_Send(valXH,valYH); SPI_Send(valXL,valYL); SPI_Close(); }
void SPI_Init(void) { /*---------------------------------------------------------------------------------------------------------*/ /* Init SPI */ /*---------------------------------------------------------------------------------------------------------*/ /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */ /* Set IP clock divider. SPI clock rate = 1 MHz */ SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 1000000); /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */ SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW); }
void SPIClass::beginTransaction(uint8_t pin, SPISettings settings) { if(init_flag==0);// init(); /* Configure as a master, clock idle low, falling clock edge Tx, rising edge Rx and 8-bit transaction */ /* Set IP clock divider. SPI clock rate = 4MHz */ SPI_Open(spi, SPI_MASTER, settings.datmode, 8, settings.clock); //SPI_EnableFIFO(spi,12,12); setBitOrder(SS, settings.border); //#if defined(__M451__) | defined(__NANO100__) SPI_ClearRxFIFO(spi); SPI_TRIGGER(spi); //#endif }
/* Main */ int main(void) { uint32_t u32ByteCount, u32FlashAddress, u32PageNumber; uint32_t nError = 0; uint16_t u16ID; /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); /* Configure SPI_FLASH_PORT as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 2MHz */ SPI_Open(SPI_FLASH_PORT, SPI_MASTER, SPI_MODE_0, 8, 2000000); /* Enable the automatic hardware slave select function. Select the SS0 pin and configure as low-active. */ SPI_EnableAutoSS(SPI_FLASH_PORT, SPI_SS0, SPI_SS_ACTIVE_LOW); SPI_TRIGGER(SPI_FLASH_PORT); printf("\n+------------------------------------------------------------------------+\n"); printf("| NUC472/NUC442 SPI Dual Mode with Flash Sample Code |\n"); printf("+------------------------------------------------------------------------+\n"); /* Wait ready */ SpiFlash_WaitReady(); if((u16ID = SpiFlash_ReadMidDid()) != 0x1C14) { printf("Wrong ID, 0x%x\n", u16ID); return -1; } else printf("Flash found: EN25QH16 ...\n"); printf("Erase chip ..."); /* Erase SPI flash */ SpiFlash_ChipErase(); /* Wait ready */ SpiFlash_WaitReady(); printf("[OK]\n"); /* init source data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { SrcArray[u32ByteCount] = u32ByteCount; } printf("Start to normal write data to Flash ..."); /* Program SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page program */ SpiFlash_NormalPageProgram(u32FlashAddress, SrcArray); SpiFlash_WaitReady(); u32FlashAddress += 0x100; } printf("[OK]\n"); /* clear destination data buffer */ for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { DestArray[u32ByteCount] = 0; } printf("Dual Read & Compare ..."); /* Read SPI flash */ u32FlashAddress = 0; for(u32PageNumber=0; u32PageNumber<TEST_NUMBER; u32PageNumber++) { /* page read */ SpiFlash_DualFastRead(u32FlashAddress, DestArray); u32FlashAddress += 0x100; for(u32ByteCount=0; u32ByteCount<TEST_LENGTH; u32ByteCount++) { if(DestArray[u32ByteCount] != SrcArray[u32ByteCount]) nError ++; } } if(nError == 0) printf("[OK]\n"); else printf("[FAIL]\n"); while(1); }
int main(void) { uint32_t u32DataCount, u32TestCount, u32Err; /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); /* Configure SPI0 as a master, MSB first, 32-bit transaction, SPI Mode-0 timing, clock is 2MHz */ SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 2000000); /* Enable the automatic hardware slave select function. Select the SPI0_SS0 pin and configure as low-active. */ SPI_EnableAutoSS(SPI0, SPI_SS0, SPI_SS_ACTIVE_LOW); /* Configure SPI1 as a slave, MSB first, 32-bit transaction, SPI Mode-0 timing, clock is 4Mhz */ SPI_Open(SPI1, SPI_SLAVE, SPI_MODE_0, 32, 4000000); /* Configure SPI1 as a low level active device. */ SPI_SET_SS0_LOW(SPI1); printf("\n\n"); printf("+----------------------------------------------------------------------+\n"); printf("| SPI Driver Sample Code |\n"); printf("+----------------------------------------------------------------------+\n"); printf("\n"); printf("Configure SPI0 as a master and SPI1 as a slave.\n"); printf("Data width of a transaction: 32\n"); printf("SPI clock rate: %d Hz\n", SPI_GetBusClock(SPI0)); printf("The I/O connection for SPI0/SPI1 loopback:\n"); printf(" SPI0_SS (PE.4) <--> SPI1_SS(PC.12)\n SPI0_CLK(PE.5) <--> SPI1_CLK(PD.1)\n"); printf(" SPI0_MISO(PE.2) <--> SPI1_MISO(PD.0)\n SPI0_MOSI(PE.3) <--> SPI1_MOSI(PC.15)\n\n"); printf("Please connect SPI0 with SPI1, and press any key to start transmission ..."); getchar(); printf("\n"); printf("\nSPI0/1 Loopback test "); /* Enable the SPI1 unit transfer interrupt. */ SPI_EnableInt(SPI1, SPI_UNITIEN_MASK); NVIC_EnableIRQ(SPI1_IRQn); SPI_TRIGGER(SPI1); u32Err = 0; for(u32TestCount=0; u32TestCount<10000; u32TestCount++) { /* set the source data and clear the destination buffer */ for(u32DataCount=0; u32DataCount<TEST_COUNT; u32DataCount++) { g_au32SourceData[u32DataCount] = u32DataCount; g_au32DestinationData[u32DataCount] = 0; } u32DataCount=0; SPI1_INT_Flag = 0; if((u32TestCount&0x1FF) == 0) { putchar('.'); } SPI_TRIGGER(SPI0); /* write the first data of source buffer to Tx register of SPI0. And start transmission. */ SPI_WRITE_TX(SPI0, g_au32SourceData[0]); while(1) { if(SPI1_INT_Flag==1) { SPI1_INT_Flag = 0; if(u32DataCount<(TEST_COUNT-1)) { /* Read the previous retrieved data and trigger next transfer. */ g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI1); u32DataCount++; /* Write data to SPI0 Tx buffer and trigger the transfer */ SPI_WRITE_TX(SPI0, g_au32SourceData[u32DataCount]); } else { /* Just read the previous retrieved data but trigger next transfer, because this is the last transfer. */ g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI1); break; } } } /* Check the received data */ for(u32DataCount=0; u32DataCount<TEST_COUNT; u32DataCount++) { if(g_au32DestinationData[u32DataCount]!=g_au32SourceData[u32DataCount]) u32Err = 1; } if(u32Err) break; } /* Disable the SPI1 unit transfer interrupt. */ SPI_DisableInt(SPI1, SPI_UNITIEN_MASK); NVIC_DisableIRQ(SPI1_IRQn); if(u32Err) printf(" [FAIL]\n\n"); else printf(" [PASS]\n\n"); printf("\n\nExit SPI driver sample code.\n"); while(1); }