小弟今天写了一段关于SPI的驱动代码。板子上是通过SPI对一块FLASH操作的。MPC作为master,FLASH作为slave。
下面是我的代码,主要就是一些寄存器的初始化,这是我第一次操刀写BSP,希望各位达人帮我看看有没有问题,大家相互交流一下!谢谢!
我把参数ram,rxBD和txBD,还有rxBuffer,txBuffer都放在dual_ram中,其中参数ram放在bank#11中,BD和BUFFER放在bank#2中。
在这段代码执行之前,我执行sysSerialHwInit(),在其中将Port D的16,17,18,19,20脚设置好,这段代码如下:
* M8260_IOP_PDDIR(immrVal) &= 0xFFFF07FC; /* clear first */
* M8260_IOP_PDDIR(immrVal) |= 0x00000002; /* TXD pin 30| RXD pin 31 */
* M8260_IOP_PDDIR(immrVal) |= 0x00000010; /* set bit 27 for SCC2 */
* M8260_IOP_PDDIR(immrVal) &= ~0x00000008; /* clear bit 28 for SCC2 */
* M8260_IOP_PDPAR(immrVal) |= 0x00000003; /* TXD pin 30| RXD pin 31 */
* M8260_IOP_PDPAR(immrVal) |= 0x00000018; /* set bits 27, 28 for SCC2*/
* M8260_IOP_PDPAR(immrVal) |= 0x00000004; /* set bit 29 for SCC2*/
* M8260_IOP_PDPAR(immrVal) |= 0x00000020; /* set bit 26 for SCC2*/
* M8260_IOP_PDPAR(immrVal) |= 0x0000F000; /* set bit 16,17,18,19 for SPI*/
* M8260_IOP_PDSO(immrVal) &= 0xFFFFFFF8; /* clear first */
* M8260_IOP_PDSO(immrVal) |= 0x00000002; /* TXD pin 30 and RXD pin 31 */
* M8260_IOP_PDSO(immrVal) &= ~0x00000018; /* clear bits 27, 28 for SCC2 */
* M8260_IOP_PDSO(immrVal) &= ~0x00000020; /* clear bit 26 for SCC2 */
* M8260_IOP_PDSO(immrVal) |= 0x0000F000; /* set bit 16,17,18,19 for SCC2 */
* M8260_IOP_PDODR(immrVal) &= 0xFFFF08ED;
* M8260_IOP_PDDAT(immrVal) = 0x00000000;
/*
DESCRIPTION
This library contains routines for M8280 SPI BSP serial device
initialization
*/
#include "vxWorks.h"
#include "iv.h"
#include "intLib.h"
#include "config.h"
#include "sysLib.h"
#include "drv/parallel/m8260IOPort.h"
#include "drv/sio/m8260Cp.h"
IMPORT UINT32 vxImmrGet (void);
#define M8260_SPMODE(base) ((VINT32 *)((base) + 0x11AA0))
#define M8260_SPIE(base) ((VINT32 *)((base) + 0x11AA6))
#define M8260_SPIM(base) ((VINT32 *)((base) + 0x11AAA))
#define M8260_SPCOM(base) ((VINT32 *)((base) + 0x11AAD))
#define M8260_SPI_BASE(base) ((VINT32 *)((base) + 0x89FC))
#define M8260_SPI_PARARAM_OFFSET 0x9000
#define M8260_SPI_RARARAM(base) ((VINT32 *)((base) + M8260_SPI_PARARAM_OFFSET))
#define M8260_SPI_RXBD_OFFSET 0x0800
#define M8260_SPI_RXBD(base) ((VINT32 *)((base) + M8260_SPI_RXBD_OFFSET))
#define M8260_SPI_TXBD_OFFSET 0x0808
#define M8260_SPI_TXBD(base) ((VINT32 *)((base) + M8260_SPI_RXBD_OFFSET))
void sysSPIHwInit (void)
{
int immrVal = vxImmrGet();
*M8260_SPI_BASE=M8260_SPI_PARARAM_OFFSET; /*assign the pointer to SPI parameter RAM*/
*M8260_SPI_RARARAM(immrVal)=0x08080800; /*set rx BD table and tx BD table base address*/
*M8260_SPI_RARARAM(immrVal+0x4)=0x00101010; /*set RFCR ,TFCR and MRBLR*/
*M8260_SPI_RXBD(immrVal)=0x0000B000; /*set rx BD */
*M8260_SPI_RXBD(immrVal+4)=immrVal+0x0840;
*M8260_SPI_TXBD(immrVal)=0x0000B800; /*set tx BD */
*M8260_SPI_TXBD(immrVal+4)=immrVal+0x0860;
*M8260_CPCR(immrVal)=0x24510000; /*init rx and tx parameters*/
*M8260_SPIE(immrVal)=0x000000FF; /*clear all SPI events*/
*M8260_SPIM(immrVal)=0x00000037; /*enable all SPI interrupts*/
*M8260_SPMODE(immrVal)=0x00000370; /*set SPI mode*/
}
明天去试试,希望各位大侠如果看出有什么问题,能给我指教!我感觉应该不止就这么点代码。
再次多谢了!!
我在MPC下的SPI驱动都是用GPIO模拟的,没用过SPI控制器,帮你顶