简体   繁体   中英

how to write 4 bytes and read 2nd byte after some delay in C programming?

I am having an embedded SBC(master) and slave a 8051 based RF module having 32kbs of Internal ROM. I am having SPI bus to access that internal ROM.When i am sending some data from my master to slave using SPI bus, i am able see some data on MOSI line and after that data, i need to get some response from the slave.I am not confident that data is written properly on 00,01,02,03 address of flash ROM.I am expecting some data on MISO line also,but i am not getting anything.My doubt is whether the four bytes are written properly on my flash ROM starting 4 addresses or not?? I have added the code for your reference, please let me know what's wrong i am doing.

    typedef unsigned char uint8;

    void run_test(int fd)
    {
        int i;
        uint8 buffer[20];
        //int size,l,size1;
        uint8 *value[4] = {0xAC,0x53,0xAA,0x55};
        uint8 address=0x0000;

        /*Writing 4 bytes*/

        for(i=0;i<4;i++)
        {
             printf("address:%.4x \t value : %2X\n",address,value[i]);
         write(fd,&value,4);
         address++;
        }
   /*Reading the 2nd byte*/

    read (fd, buffer, sizeof (buffer));
    printf("%2X\n",);   
    }

I want to read my second byte from buffer.Please let me know what's wrong i am doing? And Moreover i need to have my address keeps on changing, and i want to write the first byte on zeroth address and so on.

Regards, Ravi

I'm not really familiar with your particular application so I don't really know what happens when you call "write" and "read" in your device's library. However, from a pure C/C++ point of view I noticed a couple of things that may or may not require attention. As I understood it you wanted to write 4 bytes of data to the first 4 bytes of memory via the SPI bus. In your write loop there are a couple of things I saw.

First, you loop 4 times and write 4 bytes each time. That is 16 total. Also, I don't see where "address" comes into play at all when you write. I noticed that the array of "value" you are passing the address of a uint* array. The write function takes a void* and you are essentially passing in a void***. This means you definitely are not writing the bytes that are in the value array you declared.

So the way I see it you can write 4 bytes, one byte at a time or 4 bytes at once like this.

   int i;
   uint8 value[4] = { 0xAC, 0x53, 0xAA, 0x55 };
   uint8 address = 0;

   // Writing 4 bytes METHOD 1
   for (i = 0; i < 4; ++i)
   {
      write(fd, &(value[i]), 1);
   }

   // Writing 4 bytes METHOD 2
   write(fd, value, 4);

And you can print the second byte from your buffer like this.

   uint8 buffer[20];
   read(fd, buffer, sizeof(buffer));
   printf("%2X\n", buffer[1]);  

I have worked with some microcontrollers and have not used file descriptors before to read/write on an SPI bus. So I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM