您好:
我在54418板上,用spi0在地址0x0000F0处,写入0xBA,读取时为0xFF。代码如下:
//************************************************************
MQX_FILE_PTR fpSPI0 = NULL;
uchar ucSendBuffer[1 + 3 + 0x0100] = {0};
int si32Ret = 0;
uint_8 u8Status = 0;
uint_8 u8Data = 0;
//************************************************************
fpSPI0 = fopen("spi0:", NULL);
if (NULL == fpSPI0) {
printf("ERROR: EGUI_LCDInit(): open SPI0 failed.\n");
return;
}
//************************************************************
//开启锁存器,以允许内存读写
ucSendBuffer[0] = 0x06;
si32Ret = fwrite(ucSendBuffer, 1, 1, fpSPI0);
fflush(fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 set latch failed.\n");
return;
}
//读取内存状态
ucSendBuffer[0] = 0x05;
si32Ret = fwrite(ucSendBuffer, 1, 1, fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 write read status command failed.\n");
return;
}
si32Ret = fread(&u8Status, 1, 1, fpSPI0); //【读到的状态为0xFF】
fflush(fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 read status failed.\n");
return;
}
//向指定内存地址写入数据
ucSendBuffer[0] = 0x02;
ucSendBuffer[1] = ((0x0000F0 >> 16) & 0xFF);
ucSendBuffer[2] = ((0x0000F0 >> 8) & 0xFF);
ucSendBuffer[3] = ((0x0000F0 >> 0) & 0xFF);
ucSendBuffer[4] = 0xBA; //【要写入的数据0xBA】
si32Ret = fwrite(ucSendBuffer, 1, (1 + 3 + 1), fpSPI0); //【返回值确实为5】
fflush(fpSPI0);
if ((1 + 3 + 1) != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 write data failed.\n");
return;
}
_time_delay(5);
//************************************************************
//读取内存状态
ucSendBuffer[0] = 0x05;
si32Ret = fwrite(ucSendBuffer, 1, 1, fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 write read status command failed.\n");
return;
}
si32Ret = fread(&u8Status, 1, 1, fpSPI0); //【读到的状态为0xFF】
fflush(fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 read status failed.\n");
return;
}
//************************************************************
//从指定内存地址读取数据
ucSendBuffer[0] = 0x03;
ucSendBuffer[1] = ((0x0000F0 >> 16) & 0xFF);
ucSendBuffer[2] = ((0x0000F0 >> 8) & 0xFF);
ucSendBuffer[3] = ((0x0000F0 >> 0) & 0xFF);
si32Ret = fwrite(ucSendBuffer, 1, (1 + 3), fpSPI0);
if ((1 + 3) != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 write read data command failed.\n");
return;
}
si32Ret = fread(&u8Data, 1, 1, fpSPI0); //【读到的数据位0xFF,不是0xBA???】
fflush(fpSPI0);
if (1 != si32Ret) {
printf("ERROR: EGUI_LCDInit(): SPI0 read data failed.\n");
return;
}
请高手指点…… |