📄 spi_sam7x.c
字号:
/*----------------------------------------------------------------------------
* R T L - F l a s h F i l e S y s t e m
*----------------------------------------------------------------------------
* Name: SPI_SAM7X.C
* Purpose: Serial Peripheral Interface Driver for AT91SAM7X
* Rev.: V3.22
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2008 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <File_Config.h>
#include <AT91SAM7X256.H> /* AT91SAM7X256 definitions */
#include <lib_AT91SAM7X256.h>
/*----------------------------------------------------------------------------
* SPI Driver Functions
*----------------------------------------------------------------------------
* Required functions for SPI driver module:
* - void spi_init ()
* no DMA:
* - void spi_ss (U32 ss)
* - U8 spi_send (U8 outb)
* with DMA:
* - void spi_command ()
*---------------------------------------------------------------------------*/
/*--------------------------- spi_init --------------------------------------*/
void spi_init (void) {
/* Initialize and enable the SSP Interface module */
/* Enable Clocks */
AT91F_PMC_EnablePeriphClock (AT91C_BASE_PMC, (1 << AT91C_ID_PIOA) |
(1 << AT91C_ID_SPI0));
/* SPI0_MISO, SPI0_MOSI, SPI0_SPCK are SPI0_NPCS0 pins */
AT91F_PIO_CfgPeriph (AT91C_BASE_PIOA, AT91C_PA16_SPI0_MISO |
AT91C_PA17_SPI0_MOSI |
AT91C_PA18_SPI0_SPCK |
AT91C_PA12_SPI0_NPCS0, 0);
/* Initialize SPI0 Controller */
AT91F_SPI_Reset (AT91C_BASE_SPI0);
AT91F_SPI_CfgMode (AT91C_BASE_SPI0, AT91C_SPI_MSTR | AT91C_SPI_MODFDIS);
/* Set DLYBCT = 0(10ns), DLYBS = 15(300ns), SBCR = 3(16MHz SPI clock) */
AT91F_SPI_CfgCs (AT91C_BASE_SPI0, 0, AT91C_SPI_CPOL | AT91C_SPI_BITS_8 |
(0 << 24) | (15 << 16) | (3 << 8));
/* Use NPCS0 as chip select. */
AT91F_SPI_CfgPCS (AT91C_BASE_SPI0, 0x0E);
AT91F_SPI_Enable (AT91C_BASE_SPI0);
}
/*--------------------------- spi_command -----------------------------------*/
void spi_command (U8 *cmd, U8 *tbuf, U8 *rbuf, U32 sz) {
/* Write and Read a byte on SPI interface */
AT91C_BASE_SPI0->SPI_RPR = (U32)&cmd[1];
AT91C_BASE_SPI0->SPI_RCR = cmd[0];
AT91C_BASE_SPI0->SPI_RNPR = (U32)rbuf;
AT91C_BASE_SPI0->SPI_RNCR = sz;
AT91C_BASE_SPI0->SPI_TPR = (U32)&cmd[1];
AT91C_BASE_SPI0->SPI_TCR = cmd[0];
AT91C_BASE_SPI0->SPI_TNPR = (U32)tbuf;
AT91C_BASE_SPI0->SPI_TNCR = sz;
/* Start SPI DMA Transfer. */
AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
while (!(AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_TXEMPTY));
AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS;
}
/*----------------------------------------------------------------------------
* end of file
*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -