DSPRelated.com
Forums

C code explanation

Started by sord...@hotmail.com September 6, 2006
Hi to everyone,

I have found an example program which uses McBSP and although I know quite well how to program in C, I can't understand what these lines below mean:

------------------------

void mcbsp0_init()
{
*(unsigned volatile int *)McBSP0_SPCR = 0;
*(unsigned volatile int *)McBSP0_PCR = 0;
*(unsigned volatile int *)McBSP0_RCR = 0x10040;
*(unsigned volatile int *)McBSP0_XCR = 0x10040;
*(unsigned volatile int *)McBSP0_DXR = 0;
*(unsigned volatile int *)McBSP0_SPCR = 0x12001;
}

void mcbsp0_write(int out_data)
{
int temp;

if (polling)
{
temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
while ( temp == 0)
temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
}
*(unsigned volatile int *)McBSP0_DXR = out_data;
}
-------------------------

I know that in the first function, it is setting some constants, whose names have been set in a header file that I haven't included. I am not sure what kind of data is *(unsigned volatile int*), because it doesn't appear in the manual. I don't know why it has two '*' which is used for pointers in C.

The other question is next line:
temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
I dont't know what operation is doing '&'. Is an AND gate operation??

Thanks in advance,

Pablo L.
> *(unsigned volatile int *)McBSP0_SPCR = 0;

McBSP0_SPCR is an address declared as a numerical constant.
(unsigned volatile int *) casts it to a particular datatype
and the leading * dereferences this pointer. So the statement
means "set the unsigned integer at address McBSP0_SPCR to 0".

This can clearly be seen by adding more parentheses instead of using
operator precedence:

*((unsigned volatile int *)McBSP0_SPCR) = 0;

You should find a definition of 'volatile' fairly easily.

> temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;

Again, explicitly showing operator precedence, we have:

temp = (*((unsigned volatile int *)McBSP0_SPCR)) & 0x20000;

so the sequence is:
1) cast address to pointer
2) retrieve pointer contents
3) and with constant
4) assign result to temp
Pablo,

*(unsigned volatile int *)McBSP0_SPCR = 0;

In the above statement, *(unsigned volatile int *)* is typecasting.
McBSP0_SPCR holds an address. So outer * is used to get the value present in
that particular address.

& used is bit wise AND operator.

My suggestion: Refer a C book. You will find answers to your above queries.

On 9/6/06, s...@hotmail.com
wrote:
>
> Hi to everyone,
>
> I have found an example program which uses McBSP and although I know quite
> well how to program in C, I can't understand what these lines below mean:
>
> ----------------------
>
> void mcbsp0_init()
> {
> *(unsigned volatile int *)McBSP0_SPCR = 0;
> *(unsigned volatile int *)McBSP0_PCR = 0;
> *(unsigned volatile int *)McBSP0_RCR = 0x10040;
> *(unsigned volatile int *)McBSP0_XCR = 0x10040;
> *(unsigned volatile int *)McBSP0_DXR = 0;
> *(unsigned volatile int *)McBSP0_SPCR = 0x12001;
> }
>
> void mcbsp0_write(int out_data)
> {
> int temp;
>
> if (polling)
> {
> temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
> while ( temp == 0)
> temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
> }
> *(unsigned volatile int *)McBSP0_DXR = out_data;
> }
> ----------------------
>
> I know that in the first function, it is setting some constants, whose
> names have been set in a header file that I haven't included. I am not sure
> what kind of data is *(unsigned volatile int*), because it doesn't appear in
> the manual. I don't know why it has two '*' which is used for pointers in C.
>
> The other question is next line:
> temp = *(unsigned volatile int *)McBSP0_SPCR & 0x20000;
> I dont't know what operation is doing '&'. Is an AND gate operation??
>
> Thanks in advance,
>
> Pablo L.
>

--
----
Raju Udava
Bangalore.

" I am responsible for myself. No one can HELP "
- My Love
Hi sordoknha,

--- s...@hotmail.com wrote:
> Hi to everyone,
>
> I have found an example program which uses
> McBSP and although I know quite well how to program
> in C,
> I can't understand what these lines below
> mean:
------------------------
>
> void mcbsp0_init()
> {
> *(unsigned volatile int *)McBSP0_SPCR = 0;
> *(unsigned volatile int *)McBSP0_PCR = 0;
> *(unsigned volatile int *)McBSP0_RCR = 0x10040;
> *(unsigned volatile int *)McBSP0_XCR = 0x10040;
> *(unsigned volatile int *)McBSP0_DXR = 0;
> *(unsigned volatile int *)McBSP0_SPCR = 0x12001;
> }
>
> void mcbsp0_write(int out_data)
> {
> int temp;
>
> if (polling)
> {
> temp = *(unsigned volatile int *)McBSP0_SPCR &
> 0x20000;
> while ( temp == 0)
> temp = *(unsigned volatile int *)McBSP0_SPCR &
> 0x20000;
> }
> *(unsigned volatile int *)McBSP0_DXR = out_data;
> }
>
-------------------------
>
> I know that in the first function, it is
> setting some constants, whose names have been set in
> a header file that I haven't included. I am not sure
> what kind of data is *(unsigned volatile int*),
> because it doesn't appear in the manual.

It doesn't because it isn't CCS specific, it is just
C.

> I don't
> know why it has two '*' which is used for pointers
> in C.

(unsigned volatile int *) is a type casting operator.
And the leading * is to get the value stored at the
followed pointer. You can read about "type casting"
and "volatile" in C books.

>
> The other question is next line:
> temp = *(unsigned volatile int *)McBSP0_SPCR &
> 0x20000;
> I dont't know what operation is doing '&'. Is
> an AND gate operation??
>

& is bit AND operator. When the above code is
executed, temp will be the result when ANDing each bit
stored at address pointed by McBSP0_SPCR with each bit
in 0x20000 in the same bit order. You can also read
about "bitwise operation" in C book.

Tien