我先写的TCP客户端程序,可以拼通板子,但是读写寄存器都不对,莫名其妙。
我建了一个Harmony 3的工程,谁知生成的代码太难懂了,调用起来很不方便。
我自己写了几个
void SERCOM6_SPI_Write(unsigned char pTransmitData)
{
if(SERCOM6_SPI_IsTransmitterBusy()==false)
{
SERCOM6_REGS->SPIM.SERCOM_DATA=pTransmitData;
while((SERCOM6_REGS->SPIM.SERCOM_INTFLAG&0x02)==0);
SERCOM6_REGS->SPIM.SERCOM_INTFLAG|=0x02;
}
}
这是写一个字节函数。
uint8_t SERCOM6_SPI_Read(void)
{
uint8_t temp;
if(SERCOM6_SPI_IsTransmitterBusy()==false)
{
while((SERCOM6_REGS->SPIM.SERCOM_INTFLAG&0x04)==0);
temp=SERCOM6_REGS->SPIM.SERCOM_DATA;
return temp;
}
return 0;
}
这是读一个字节函数。
uint8_t SERCOM6_SPI_Read1(void)
{
uint8_t temp;
temp=SERCOM6_REGS->SPIM.SERCOM_DATA;
return temp;
}
这是虚读。
把它们放在w5500.c的函数里调用
void SPI1_Send_Byte(unsigned char dat)
{
SERCOM6_SPI_Write(dat);
}
这是SPI写一个字节。
unsigned char Read_W5500_1Byte(unsigned short reg)
{
unsigned char i;
GPIO_PC06_Clear();
SPI1_Send_Short(reg);//ͨ¹ýSPI1д16λ¼Ä´æÆ÷µØÖ·
SPI1_Send_Byte(FDM1|RWB_READ|COMMON_R);//ͨ¹ýSPI1д¿ØÖÆ×Ö½Ú,1¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡ÔñͨÓüĴæÆ÷
i=SERCOM6_SPI_Read1();
i=SERCOM6_SPI_Read();
GPIO_PC06_Set();
return i;//·µ»Ø¶ÁÈ¡µ½µÄ¼Ä´æÆ÷Êý¾Ý
}
读W5500一个字节。
还有,我看了数据手册,把初始化也改了
void SERCOM6_SPI_Initialize(void)
{
/* Instantiate the SERCOM6 SPI object */
sercom6SPIObj.callback = NULL ;
sercom6SPIObj.transferIsBusy = false ;
sercom6SPIObj.txSize = 0U;
sercom6SPIObj.rxSize = 0U;
/* Selection of the Character Size and Receiver Enable */
//SERCOM6_REGS->SPIM.SERCOM_CTRLB = SERCOM_SPIM_CTRLB_CHSIZE_8_BIT | SERCOM_SPIM_CTRLB_RXEN_Msk ;
SERCOM6_REGS->SPIM.SERCOM_CTRLB = 0x0002C200;
/* Wait for synchronization */
while((SERCOM6_REGS->SPIM.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
/* Selection of the Baud Value */
SERCOM6_REGS->SPIM.SERCOM_BAUD = (uint8_t)SERCOM_SPIM_BAUD_BAUD(SERCOM6_SPIM_BAUD_VALUE);
/* Configure Data Out Pin Out , Master Mode,
* Data In and Pin Out,Data Order and Standby mode if configured
* and Selection of the Clock Phase and Polarity and Enable the SPI Module
*/
//SERCOM6_REGS->SPIM.SERCOM_CTRLA = SERCOM_SPIM_CTRLA_MODE_SPI_MASTER | SERCOM_SPIM_CTRLA_DOPO_PAD0 | SERCOM_SPIM_CTRLA_DIPO_PAD0 | SERCOM_SPIM_CTRLA_CPOL_IDLE_LOW | SERCOM_SPIM_CTRLA_CPHA_LEADING_EDGE | SERCOM_SPIM_CTRLA_DORD_MSB | SERCOM_SPIM_CTRLA_ENABLE_Msk | SERCOM_SPIM_CTRLA_RUNSTDBY_Msk;
//SERCOM6_REGS->SPIM.SERCOM_CTRLA = 0x0030018e;
SERCOM6_REGS->SPIM.SERCOM_CTRLA = 0x0130018e;
/* Wait for synchronization */
while((SERCOM6_REGS->SPIM.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
SERCOM6_REGS->SPIM.SERCOM_CTRLC = 0x00000000;
/* Wait for synchronization */
while((SERCOM6_REGS->SPIM.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
SERCOM6_REGS->SPIM.SERCOM_DBGCTRL=0;
/* Wait for synchronization */
while((SERCOM6_REGS->SPIM.SERCOM_SYNCBUSY) != 0U)
{
/* Do nothing */
}
}
我不知道到底是初始化不对还是时序哪里有问题。请高手指教,谢谢!
|