08-24-2024 06:15 PM - edited 08-24-2024 06:17 PM
I have an M25P80 connected with decoupling, pullup/down resistors connected to the NI8452
Using either a script VI or C code, how to simply read the ID register of this e2prom?
The device is first selected by driving S# LOW. Then the 8-bit command code is shifted
in and content is shifted out on DQ1 as follows: the 24-bit device identification that is
stored in the memory, the 8-bit CFD length, followed by 16 bytes of CFD content. Each
bit is shifted out during the falling edge of serial clock (C)
Single byte command 0x9F or 0x9E
issue this command and about 20 bytes of read data clock out. Something like this?
WriteSize = 1;
SendData[0] = 0x9f;
/* issue start condition */
errChk(ni845xSpiScriptCSLow(ScriptHandle, ChipSelect));
/* write the 0x9F command */
errChk(ni845xSpiScriptWriteRead(ScriptHandle, WriteSize, SendData,
&ScriptReadIndex));
/* issue stop condition */
errChk(ni845xSpiScriptCSHigh(ScriptHandle, ChipSelect));
/* issue start condition */
errChk(ni845xSpiScriptCSLow(ScriptHandle, ChipSelect));
/* write data array */
errChk(ni845xSpiScriptWriteRead(ScriptHandle, WriteSize, WriteData,
&ScriptReadIndex));
/* issue stop condition */
errChk(ni845xSpiScriptCSHigh(ScriptHandle, ChipSelect));
//errChk(ni845xDioWritePort(DeviceHandle, 0, 0));
/* disable script logging */
errChk(ni845xSpiScriptDisableSPI(ScriptHandle));
/* run the script */
errChk(ni845xSpiScriptRun(ScriptHandle, DeviceHandle, PortNumber));
/* extract the read data size */
errChk(ni845xSpiScriptExtractReadDataSize(ScriptHandle, ScriptReadIndex,
&ReadSize));
/* extract the read data */
errChk(ni845xSpiScriptExtractReadData(ScriptHandle, ScriptReadIndex,
ReadData));
/* if frames were received, display them */
if (ReadSize >= 1)
{
for (i = 3; i < ReadSize; i++)
{
/* convert returned data into string*/
sprintf_s(CharBuff, sizeof(CharBuff), "%3X", ReadData[i]);
printf("%s\n", CharBuff);
}
}
/* close the script and device handles */
errChk(ni845xSpiScriptClose(ScriptHandle));
errChk(ni845xClose(DeviceHandle));
08-24-2024 09:49 PM
You shouldn't be toggling the CS between command and data read as per the device's timing diagram.
Please try this logic,
08-25-2024 11:45 AM
Thanks for your help. Modified a little and
RESULT
FF
FF
CC
CC
CC
CC
CC
CC
CC
#include <stdio.h> // Include file for printf
#include <stdlib.h> // Include file for strtol
#include <string.h>
#include <windows.h> // Include file for Win32 time functions
#include "ni845x.h" // Include file for NI-485x functions and constants
/* the NI 845x handles */
NiHandle DeviceHandle;
NiHandle ScriptHandle;
/* error Function for NI 845x */
#ifndef errChk
#define errChk(fCall) if (Error = (fCall), Error < 0) {goto Error;} else
#endif
int main()
{
int Error = 0;
char FirstDevice[260]; // 260 characters for 845x resource name
uInt8 PortNumber = 0;
uInt32 ChipSelect = 0; // chip select pin (0)
uInt16 ClockRate = 50; // clock rate in KHz
uInt32 NumberToRead = 32; // number of data bytes to read (32)
uInt16 StartingOffset = 0; // start address (0)
uInt8 LowAddressByte = 0;
uInt8 HighAddressByte = 0;
uInt32 WriteSize = 0;
uInt8 WriteData[35]; // write array for bytes to write
uInt32 ReadSize = 0;
uInt8 ReadData[35]; // read array for bytes to read
uInt32 ScriptReadIndex = 0;
uInt8 SendData[512];
uInt32 i;
char CharBuff[10];
char ErrorMsg[1024];
printf("\n\nSearching for Devices\n\n");
/* find first device */
errChk(ni845xFindDevice(FirstDevice, NULL, NULL));
/* open device handle */
errChk(ni845xOpen(FirstDevice, &DeviceHandle));
/* Set the I/O Voltage Level */
errChk(ni845xSetIoVoltageLevel(DeviceHandle, kNi845x33Volts));
///* create the data array to send */
//LowAddressByte = StartingOffset & 0xFF;
//HighAddressByte = StartingOffset >> 8;
//WriteSize = NumberToRead + 3;
//WriteData[0] = 3; // READ instruction (0x3)
//WriteData[1] = HighAddressByte; // first address byte
//WriteData[2] = LowAddressByte; // second address byte
//for (i = 3; i < (WriteSize); i++)
//{
// WriteData[i] = (uInt8)(i - 3);
//}
/* create script */
errChk(ni845xSpiScriptOpen(&ScriptHandle));
/* enable script logging */
errChk(ni845xSpiScriptEnableSPI(ScriptHandle));
/* configure Polarity and Phase */
errChk(ni845xSpiScriptClockPolarityPhase(ScriptHandle,
kNi845xSpiClockPolarityIdleLow, kNi845xSpiClockPhaseFirstEdge));
/* configure clock rate */
errChk(ni845xSpiScriptClockRate(ScriptHandle, ClockRate));
printf("%s", FirstDevice);
printf(" initialized successfully\n\nData received:\n\n");
WriteSize = 1;
SendData[0] = 0x9e; // the WREN Instruction (0x6)
/* issue start condition */
errChk(ni845xSpiScriptCSLow(ScriptHandle, ChipSelect));
/* write WREN instruction array */
errChk(ni845xSpiScriptWriteRead(ScriptHandle, WriteSize, SendData,
&ScriptReadIndex));
/* issue stop condition */
// errChk(ni845xSpiScriptCSHigh(ScriptHandle, ChipSelect));
///* create the data array to send */
LowAddressByte = StartingOffset & 0xFF;
HighAddressByte = StartingOffset >> 8;
WriteSize = 4; //NumberToRead + 3;
WriteData[0] = 0; // READ instruction (0x3)
WriteData[1] = 0; // HighAddressByte; // first address byte
WriteData[2] = 0; //LowAddressByte; // second address byte
WriteData[3] = 0;
/*for (i = 3; i < (WriteSize); i++)
{
WriteData[i] = (uInt8)(i - 3);
}*/
errChk(ni845xSpiScriptWriteRead(ScriptHandle, WriteSize, WriteData,
&ScriptReadIndex));
/* issue start condition */
// errChk(ni845xSpiScriptCSLow(ScriptHandle, ChipSelect));
/* write data array */
/* errChk(ni845xSpiScriptWriteRead(ScriptHandle, WriteSize, WriteData,
&ScriptReadIndex));*/
/* issue stop condition */
errChk(ni845xSpiScriptCSHigh(ScriptHandle, ChipSelect));
/* disable script logging */
errChk(ni845xSpiScriptDisableSPI(ScriptHandle));
/* run the script */
errChk(ni845xSpiScriptRun(ScriptHandle, DeviceHandle, PortNumber));
/* extract the read data size */
errChk(ni845xSpiScriptExtractReadDataSize( ScriptHandle, ScriptReadIndex,
&ReadSize));
/* extract the read data */
errChk(ni845xSpiScriptExtractReadData(ScriptHandle, ScriptReadIndex,
ReadData));
/* if frames were received, display them FORCE READ SIZE*/
if (1 == 1)
{
for (i = 0; i < 10; i++) //3
{
/* convert returned data into string */
sprintf_s(CharBuff, sizeof(CharBuff), "%3X", ReadData[i]);
printf("%s\n", CharBuff);
}
}
/* close the script and device handles */
errChk(ni845xSpiScriptClose(ScriptHandle));
errChk(ni845xClose(DeviceHandle));
Error:
if (Error < 0)
{
ni845xStatusToString(Error, 1024, ErrorMsg);
printf("\nError %d %s \n", Error, ErrorMsg);
ni845xSpiScriptClose(ScriptHandle);
ni845xClose(DeviceHandle);
exit(1);
}
return 0;
}
08-25-2024 07:58 PM
and it is not the right data. FF FF CC CC CC CC
08-30-2024 10:48 AM
I connected another NI8452 USB to DIO/spi
now the data is something like:
20
42
20
16
CC
CC
CC
CC
it was the NI hardware. This NI8452 is a poorly made, poorly designed unit that goes into some glitch mode and never reports any problem. This is a hobbyist grade piece of hardware, not for serious applications.
I started using National Instruments software/systems in 1997. they were always good quality and good support. until now