Hi Robert,

I want to read/write data directly from the registers for SPI0.

1) I read the arm335X manual for this.
2) I am coding in C.
3) According to the manual I have to first soft reset the the module. For
this purpose I have to set the RESETDONE bit in the MCSPI_SYSCONFIG
register.
4) So I am first using mmap to map the device register.
5) then using the offset I am reaching to the particular register's address
6) As soon as I do any operation with the content of the register I get Bus
Error.



this is my code

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
//Xenomai for mapping the register
#include <sys/mman.h>


#define SPI0_START_ADDR 0x48030000  //mcspi0 data registers
#define SPI0_END_ADDR   0x48030FFF
#define SPI0_SIZE (SPI0_END_ADDR - SPI0_START_ADDR + 0x01)   // 4KB
#define SPI1_START_ADDR 0x481A0000  //mcspi1 data registers
#define SPI1_END_ADDR   0x481A0FFF
#define SPI1_SIZE (SPI1_END_ADDR - SPI1_START_ADDR + 0x01)  // 4KB

//print_output(void *data) {
//      unsigned int *my_data =  &
//}

int main (int *argc, int **argv)
{

        printf("%d\n",sysconf(_SC_PHYS_PAGES));
        mlockall (MCL_CURRENT | MCL_FUTURE);
        int fd, outputfile;
        struct stat attr;
        void *spi0_addr = NULL;

        //SPI Registers
        volatile unsigned int *mcspi_revision = NULL;
        volatile unsigned int *mcspi_sysconfig = NULL;
        volatile unsigned int *mcspi_sysstatus = NULL;
        volatile unsigned int *mcspi_irqstatus = NULL;
        volatile unsigned int *mcspi_irqenable = NULL;
        volatile unsigned int *mcspi_syst = NULL;
        volatile unsigned int *mcspi_modulctrl = NULL;
        volatile unsigned int *mcspi_ch0conf = NULL;
        volatile unsigned int *mcspi_ch0stat = NULL;
        volatile unsigned int *mcspi_ch0ctrl = NULL;
        volatile unsigned int *mcspi_tx0 = NULL;
        volatile unsigned int *mcspi_rx0 = NULL;

        if ((fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
                fprintf(stderr, "can not open /dev/mem!\n");
                 exit(-1);
        }
        else {
                printf("Mapping %x - %x (size: 0x%x bytes)\n",
SPI0_START_ADDR, SPI0_END_ADDR, SPI0_SIZE);
        }
        if (fstat(fd,&attr) == -1) {
                fprintf(stderr,"Error while fstat\n");
        }
        //mapping the file /dev/mem
        spi0_addr = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE,
fd, SPI0_START_ADDR);
        if (spi0_addr == MAP_FAILED) {
                printf("Mapping of the SPI0 failed!! \n");
                exit(1);
        }
        else {
                printf("/dev/mem file is mapped\n");
        }

        //fprintf(stdout, "size of void* is : %d and size of volatile
unsigned int * is: %d\n",*((int *)spi0_addr),\
        //       sizeof(*mcspi_rx0));
        //fprintf(stdout,"Base address returned from the mmap is: %X and
size of value \
        //         returned is: %X \n",(unsigned)spi0_addr,
sizeof(*spi0_addr));


mcspi_revision = spi0_addr + 0x00;
        mcspi_sysconfig = spi0_addr + 0x110;
        mcspi_sysstatus = spi0_addr + 0x114;
        mcspi_irqstatus = spi0_addr + 0x118;
        mcspi_irqenable = spi0_addr + 0x11C;
        mcspi_syst = spi0_addr + 0x124;
        mcspi_modulctrl = spi0_addr + 0x128;
        // spi0 channel
        mcspi_ch0conf = spi0_addr + 0x12C;
        mcspi_ch0stat = spi0_addr + 0x130;
        mcspi_ch0ctrl = spi0_addr + 0x134;
        mcspi_tx0 = spi0_addr + 0x138;
        mcspi_rx0 = spi0_addr + 0x13C;

        // Soft reset
    *mcspi_sysconfig = ((*mcspi_sysconfig) | (0x2));    //<---------this is
the point where I am getting an error as I am trying to acces the content
of the register.
        int  temp  =  (*mcspi_sysconfig & 0x2);
        printf("0x%02X\n",temp);
        //checking if reset is done
        //while (1) {
                if (((*mcspi_sysstatus) & (0x00000001)) == 0x1) {
                        printf("reset is complete\n");
                        //break;
                }
                else {
                        printf("reset is going on\n");
                }
        //}


        if ((outputfile = open("/home/ubuntu/dac/output_file.txt", O_RDWR |
O_APPEND))<0) {
                printf("could not open the file\n");
        }
        close(outputfile);
        //mcspi_sysstatus = NULL;
        //fprintf(stdout, "ch0conf: %x\n", mcspi_sysstatus);
        //fpirntf(stdout, "ch0stat: %x\n", (unsigned int)mcspi_ch0stat);
        //fprintf(stdout, "ch0ctrl: %x\n", (unsigned int)mcspi_ch0ctrl);
        //fprintf(stdout, "tx0: %x\n", (unsigned int)mcspi_tx0);
        //fpirntf(stdout, "rx0: %x\n", (unsigned int)mcspi_rx0);


        close(fd);

        return 0;
}







Please give your suggestions about where I am going wrong.


*Best Regards,*


On 2 September 2014 19:24, John Syn <[email protected]> wrote:

>
> From: Amit Sama <[email protected]>
> Reply-To: "[email protected]" <[email protected]>
> Date: Sunday, August 31, 2014 at 4:27 AM
> To: "[email protected]" <[email protected]>
> Subject: Re: [beagleboard] Re: Device Tree Compiler
>
> Hi John,
>
> Thanks for your reply. You mean whatever we have to send and receive
> should be done via spidev1.0 only ?
>
> No, you can use any SPI interface and any chip select as long as they are
> defined in the devicetree. If you define the SPI device in the devicetree
> correctly then they will show up in /dev.
>
> Regards,
> John
>
>
> *Best Regards,*
> *Amit Sama*
>
>
>
> On 28 August 2014 16:28, John Syn <[email protected]> wrote:
>
>>
>> From: Amit Sama <[email protected]>
>> Reply-To: "[email protected]" <[email protected]>
>> Date: Thursday, August 28, 2014 at 7:49 AM
>> To: "[email protected]" <[email protected]>
>>
>> Subject: Re: [beagleboard] Re: Device Tree Compiler
>>
>> Hi all,
>>
>> Thanks for your support till now. I am really grateful to you.
>>
>> While I going through the reference manual of the arm processor on the
>> BBB, I came to know that the processor has the capacity for 4 SPI channels
>> but BBB only provides 2 channels.
>>
>> 1) So I want to know if spidev1.0 and spidev1.1 are the interfaces for
>> just one channel (because I have read the spidevB.C means channel B and
>> device C) why are they two in number. Does these files represent the SLAVES
>> which could be attached with this channel ?
>>
>> I believe that is SPI Channel and SPI Chip Select.
>>
>> Regards,
>> John
>>
>>
>> And please understand that I am not yelling or complaining. I am just new
>> to this kind of thing. While reading the material through web I gets
>> answers but also get questions. I hope you all don't mind my questions. I
>> apologize if you do.
>>
>>
>>
>> *Best Regards,*
>> *Amit Sama*
>> *Pursuing** M.Sc. Informatics*
>> *Technical University Munich*
>> *Contact : +49-15214455380 <%2B49-15214455380>*
>>
>>
>>
>> On 28 August 2014 01:31, William Hermans <[email protected]> wrote:
>>
>>> *What can be the meaning here?*
>>>>
>>>
>>> Learn how to use your tools before half fast using / complaining about
>>> them ?
>>>
>>>
>>> On Wed, Aug 27, 2014 at 12:55 PM, 'Mark Lazarewicz' via BeagleBoard <
>>> [email protected]> wrote:
>>>
>>>> What can be the meaning here?
>>>>
>>>> Sent from Yahoo Mail on Android
>>>> <http://overview.mail.yahoo.com/mobile/?.src=Android>
>>>>
>>>>  ------------------------------
>>>> * From: * Robert Nelson <[email protected]>;
>>>> * To: * Beagle Board <[email protected]>;
>>>> * Subject: * Re: [beagleboard] Re: Device Tree Compiler
>>>> * Sent: * Wed, Aug 27, 2014 7:18:21 PM
>>>>
>>>>   On Wed, Aug 27, 2014 at 4:39 AM, Amit Sama <[email protected]>
>>>> wrote:
>>>> > Hi Robert,
>>>> >
>>>> > Though I managed to enable the spi interface on my BB. I have
>>>> following
>>>> > questions/information:
>>>> >
>>>> > 1) I have following urls in /etc/apt/sources.list
>>>> >
>>>> > THESE WERE EARLIER IN THE BBB
>>>> >
>>>> > #deb http://ports.ubuntu.com/ubuntu-ports/ raring main universe
>>>> multiverse
>>>> > #deb-src http://ports.ubuntu.com/ubuntu-ports/ raring main universe
>>>> > multiverse
>>>> >
>>>> > #deb http://ports.ubuntu.com/ubuntu-ports/ raring-updates main
>>>> universe
>>>> > multiverse
>>>> > #deb-src http://ports.ubuntu.com/ubuntu-ports/ raring-updates main
>>>> universe
>>>> > multiverse
>>>> >
>>>> >
>>>> > THESE I COPIED FROM THE link IN ORDER TO SOLVE THE PROBLEM
>>>> >
>>>> > deb http://archive.ubuntu.com/ubuntu/ precise main restricted
>>>> universe
>>>> > multiverse
>>>> > deb http://archive.ubuntu.com/ubuntu/ precise-updates main restricted
>>>> > universe multiverse
>>>> > deb http://archive.ubuntu.com/ubuntu/ precise-backports main
>>>> restricted
>>>> > universe multiverse
>>>> > deb http://security.ubuntu.com/ubuntu precise-security main
>>>> restricted
>>>> > universe multiverse
>>>> > deb http://archive.canonical.com/ubuntu precise partner
>>>> > deb http://extras.ubuntu.com/ubuntu precise main
>>>> >
>>>> >
>>>> > THESE I COPIED FROM MY DESKTOP
>>>> > #deb http://archive.ubuntu.com/ubuntu/ precise main restricted
>>>> universe
>>>> > multiverse
>>>> > #deb http://security.ubuntu.com/ubuntu/ precise-security main
>>>> restricted
>>>> > universe multiverse
>>>> > #deb http://archive.ubuntu.com/ubuntu/ precise-update main restricted
>>>> > universe multiverse
>>>> >
>>>> > NONE OF THEM WORKS, DO TELL ME WHAT ARE THE RIGHT URLS
>>>>
>>>> "Yelling" isn't going to fix this problem, either is random coping the
>>>> right URLS's.
>>>>
>>>> So to repeat:
>>>>
>>>> <quote>
>>>> So at this point, i'm just going to say... "You" totally hosed your OS
>>>> doing some random stuff, that why it doesn't work..
>>>>
>>>> Start over with a fresh image.
>>>> </quote>
>>>>
>>>> > 2) I could see these two files in /dev/spidev1.0 and /dev/spidev1.1 .
>>>> Though
>>>> > I configures for the spi0 channel I think it should be like spidev0.0
>>>> and
>>>> > spidev0.1
>>>>
>>>> Nope it isn't..
>>>>
>>>> > 3) In order to check if I have configured the spidev correctly, I
>>>> copied a
>>>> > file from here
>>>> >
>>>> > http://osdir.com/ml/beagleboard/2013-05/msg00813.html
>>>> >
>>>> > NOW WHEN I compile it using
>>>> >
>>>> >
>>>> > gcc -c spidev_test.c
>>>> >
>>>> > It gives error like
>>>> >
>>>> > /usr/include/libio.h:334:3: error: unknown type name ‘size_t’
>>>>
>>>> Really! "It gives error like" considering "size_t" isn't in that
>>>> source. Your definition of "like" is way different!!! Wow..
>>>>
>>>>
>>>> Regards,
>>>>
>>>> --
>>>> Robert Nelson
>>>> http://www.rcn-ee.com/
>>>>
>>>> --
>>>> For more options, visit http://beagleboard.org/discuss
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "BeagleBoard" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>> --
>>>> For more options, visit http://beagleboard.org/discuss
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "BeagleBoard" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>>> For more options, visit http://beagleboard.org/discuss
>>> ---
>>> You received this message because you are subscribed to a topic in the
>>> Google Groups "BeagleBoard" group.
>>> To unsubscribe from this topic, visit
>>> https://groups.google.com/d/topic/beagleboard/zQ039ckqp3E/unsubscribe.
>>> To unsubscribe from this group and all its topics, send an email to
>>> [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> For more options, visit http://beagleboard.org/discuss
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "BeagleBoard" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>> --
>> For more options, visit http://beagleboard.org/discuss
>> ---
>> You received this message because you are subscribed to a topic in the
>> Google Groups "BeagleBoard" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/beagleboard/zQ039ckqp3E/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
>  --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "BeagleBoard" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/beagleboard/zQ039ckqp3E/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to