操作S3C6410的SPI端口驱动W5200网络模块,传输网络参数时发生错误,求解
先贴上主函数:u8 MAC[6] = {0x00, 0x08, 0xDC, 0x01, 0x02, 0x03};//MAC Address
u8 IP[4] = {192, 168, 0, 103};//IP Address
u8 GateWay[4] = {192, 168, 11, 1};//Gateway Address
u8 SubNet[4] = {255, 255, 255, 0};//SubnetMask Address
void main(void)
{
SYSTEM_InitException();
SYSTEM_InitMmu();
SYSC_POWER_Set();
InitUartPort(0, FALSE);
UART_InitDebugCh(0, 115200);
UART_SetConfig(3,0,1,1,4,1,2,1,115200,1,1,1,1);
UART_Open2(3);
GPIO_Init();
printf("yanjiasi\n");
Reset_W5200(); ///yanjiasi///5.23////
WIZ_Config(); // network config & Call Set_network ();
// Start Application
printf("\r\n\r\n------------------------------------------- \r\n");
printf("TCP Server Loopback using W5200\r\n");
// printf("UDP Loopback using W5200\r\n");
printf("------------------------------------------- \n");
//while(1) {
// tempa++;
// tempa=0;
// loopback_tcps(0, 5000);
// loopback_tcps(1, 5001);
// loopback_tcps(2, 5002);
// loopback_tcps(3, 5003);
// loopback_tcpc(4, 5004);
// loopback_udp(5, 3000);
// loopback_udp(6, 3001);
// loopback_udp(7, 3002);
//}
}
贴上ResetW5200()的代码:
void Reset_W5200(void)
{
GPIO_SetFunctionEach(eGPIO_E, eGPIO_0, 0x1);
GPIO_SetPullUpDownEach(eGPIO_E, eGPIO_0, 0);
Delay_us(2);
GPIO_SetPullUpDownEach(eGPIO_E, eGPIO_0, 1);
Delay_ms(150);
*rGPEDAT=1;
}
其实我有点怀疑我的w5200是否是重置过,重置的过程要求先给一个至少2us的低电平重置信号,然后加高电平至少150ms,我不明白S3C官网给的这个GPIO_SetPullUpDownEach()函数到底是什么意思,,反正最后就是拉高了电平。
再贴上 WIZ_Config()的代码:
void WIZ_Config(void)
{
// Call Set_network(): Set network configuration, Init. TX/RX MEM SIZE., and Set RTR/RCR
Set_network(); // at util.c
}
void Set_network(void)
{
uint8 tmp_array[6];
uint8 i;
// MAC ADDRESS
for (i = 0 ; i < 6; i++)
{
Config_Msg.Mac[i] = MAC[i];
}
// Local IP ADDRESS
Config_Msg.Lip[0] = IP[0]; Config_Msg.Lip[1] = IP[1]; Config_Msg.Lip[2] = IP[2]; Config_Msg.Lip[3] = IP[3];
// GateWay ADDRESS
Config_Msg.Gw[0] = GateWay[0]; Config_Msg.Gw[1] = GateWay[1]; Config_Msg.Gw[2] = GateWay[2]; Config_Msg.Gw[3] = GateWay[3];
// Subnet Mask ADDRESS
Config_Msg.Sub[0] = SubNet[0]; Config_Msg.Sub[1] = SubNet[1]; Config_Msg.Sub[2] = SubNet[2]; Config_Msg.Sub[3] = SubNet[3];
setSHAR(Config_Msg.Mac);
// setSUBR(Config_Msg.Sub);
saveSUBR(Config_Msg.Sub);
setGAR(Config_Msg.Gw);
setSIPR(Config_Msg.Lip);
// Set DHCP
Config_Msg.DHCP = Enable_DHCP;
//Destination IP address for TCP Client
Chconfig_Type_Def.destip[0] = Dest_IP[0]; Chconfig_Type_Def.destip[1] = Dest_IP[1];
Chconfig_Type_Def.destip[2] = Dest_IP[2]; Chconfig_Type_Def.destip[3] = Dest_IP[3];
Chconfig_Type_Def.port = Dest_PORT;
//Set RTR and RCR register
setRTR(6000);
setRCR(3);
//Init. TX & RX Memory size
sysinit(txsize, rxsize);
printf("\r\n----------------------------------------- \r\n");
printf("W5200E01-M3 \r\n");
printf("Network Configuration Information \r\n");
printf("----------------------------------------- \n");
printf("\r\nMAC : %.2X.%.2X.%.2X.%.2X.%.2X.%.2X\n", IINCHIP_READ(SHAR0+0),IINCHIP_READ(SHAR0+1),IINCHIP_READ(SHAR0+2),
IINCHIP_READ(SHAR0+3),IINCHIP_READ(SHAR0+4),IINCHIP_READ(SHAR0+5));
getSIPR (tmp_array);
printf("\r\nIP : %d.%d.%d.%d", tmp_array[0],tmp_array[1],tmp_array[2],tmp_array[3]);
getSUBR(tmp_array);
printf("\r\nSN : %d.%d.%d.%d\n", tmp_array[0],tmp_array[1],tmp_array[2],tmp_array[3]);
getGAR(tmp_array);
printf("\r\nGW : %d.%d.%d.%d", tmp_array[0],tmp_array[1],tmp_array[2],tmp_array[3]);
}
以setSHAR()为例子,就是写入物理地址:
void setSHAR(
uint8 * addr /**< a pointer to a 6 -byte array responsible to set the MAC address. */
)
{
IINCHIP_WRITE((SHAR0 + 0),addr[0]);
IINCHIP_WRITE((SHAR0 + 1),addr[1]);
IINCHIP_WRITE((SHAR0 + 2),addr[2]);
IINCHIP_WRITE((SHAR0 + 3),addr[3]);
IINCHIP_WRITE((SHAR0 + 4),addr[4]);
IINCHIP_WRITE((SHAR0 + 5),addr[5]);
}
写函数的定义:
uint8 IINCHIP_WRITE(uint16 addr,uint8 data)
{
IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
//SPI MODE I/F
SPI_CSControl(0);
delayLoop(0xffff); // CS=0, SPI start
IINCHIP_SpiSendData((addr & 0xFF00) >> 8); // Address byte 1
// printf("step1\n");
IINCHIP_SpiSendData(addr & 0x00FF); // Address byte 2
IINCHIP_SpiSendData(0x80); // Data write command and Write data length 1
IINCHIP_SpiSendData(0x01); // Write data length 2
IINCHIP_SpiSendData(data); // Data write (write 1byte data)
// printf("step last\n");
delayLoop(0xffff);
SPI_CSControl(1);
delayLoop(0xffff); // CS=1, SPI end
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
return 1;
}
读函数的定义:
/**
@brief This function reads the value from W5200 registers.
*/
uint8 IINCHIP_READ(uint16 addr)
{
uint8 data;
IINCHIP_ISR_DISABLE(); // Interrupt Service Routine Disable
SPI_CSControl(0); // CS=0, SPI start
delayLoop(0xffff);
IINCHIP_SpiSendData((addr & 0xFF00) >> 8); // Address byte 1
IINCHIP_SpiSendData(addr & 0x00FF); // Address byte 2
IINCHIP_SpiSendData(0x00); // Data read command and Read data length 1
IINCHIP_SpiSendData(0x01); // Read data length 2
data = IINCHIP_SpiSendData(0x00); // Data read (read 1byte data)
delayLoop(0xffff);
SPI_CSControl(1); // CS=1, SPI end
delayLoop(0xffff);
IINCHIP_ISR_ENABLE(); // Interrupt Service Routine Enable
return data;
}
发送函数的定义:
u8 IINCHIP_SpiSendData(uint8 dat)
{
printf("dat = %d ",dat);
return(SPI_WriteByte(dat));
}
u8 SPI_WriteByte(u8 dat)
{
SPI_channel *ch1;
u8* uMTxBufAddr = (u8*)SPI_getBuffer(0, TRUE, TRUE);
u8* uMRxBufAddr = (u8*)SPI_getBuffer(1, FALSE, TRUE);
ch1=SPI_open(TEST_CHANNEL1,SPI_MASTER, SPI_TEST_CPOL, SPI_TEST_FORMAT, CLK_SOURCE, SPI_SPEED,
SPI_TEST_CHANNEL_WIDTH, SPI_TEST_BUS_WIDTH, NULL );
uMTxBufAddr[0]=dat;
printf("uMT[0] = %d ",uMTxBufAddr[0]);
SPI_transaction(ch1, SPI_POLLING_MODE, uMTxBufAddr, uMRxBufAddr, 1, NULL);
printf("uMR[0] = %d\n",uMRxBufAddr[0]);
return uMRxBufAddr[0];
}
SPI_open()参数:
#define SPI_BUFFER_SIZE 0x100000 // Reserved Buffer Size.
#define SPI_SPEED 20000000 // . // Transfer Speed, that is limit to clock source.
#define SPI_DATA_COUNT (0x4) // max Buffer size.
#define CLK_SOURCE SPI_PCLK // SPI Clock Source.
#define TEST_CHANNEL0 0 // SPI Test Peripheral 0
#define TEST_CHANNEL1 1 // SPI Test Peripheral 1
#define SPI_TEST_CPOL SPI_ACTIVE_LOW // SPI Test polarity
#define SPI_TEST_CHANNEL_WIDTH SPI_BYTE // SPI Channel Size
#define SPI_TEST_BUS_WIDTH SPI_WORD // SPI Bus Size
#define SPI_TEST_DMA_TYPE SPI_DMA_SINGLE // SPI DMA Mode
#define SPI_TEST_FORMAT SPI_FORMAT_A // SPI Test Phase
// When polling mode test, must using board to board test.
#define SPI_TEST_TRANSFER_MODE SPI_POLLING_MODE // SPI transfer mode selection
代码先贴到这里,请大神们给意见,我再贴出程序运行的结果截图,就是读出来的不对: