gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Help understanding some UART code

#81436 - a4_jet - Sat Apr 29, 2006 4:19 am

Hi,
I'm fairly new to GB programming and have started my first program, which is a program to recieve serial information from a car's ECU and then display it to the driver.

Im trying to setup the UART on the GBA to recieve 8 bit RS232 data set at 9600 bps.

I had a look at this post to intialize the UART
http://forum.gbadev.org/viewtopic.php?t=1307&highlight=uart

I have used most of this code but i'm trying to debug it now using visual ham.
This is it so far
Code:

//UART intilization
// Register base address
#define REG_BASE          0x4000000

// Serial IO Registers
#define REG_SIOCNT        *(volatile unsigned short int *)(REG_BASE + 0x128)  // Serial control
#define REG_SIODATA8      *(volatile unsigned short int *)(REG_BASE + 0x12a)  // Serial data
#define REG_RCNT          *(volatile unsigned short int *)(REG_BASE + 0x134)  // General IO


// UART settings
#define SIO_USE_UART      0x3000 \\must be set for 1 for uart mode

// Baud Rate
#define SIO_BAUD_9600     0x0000 \\sets the input to 9600
#define SIO_BAUD_38400    0x0001
#define SIO_BAUD_57600    0x0002
#define SIO_BAUD_115200   0x0003

#define SIO_CTS           0x0004
#define SIO_PARITY_ODD    0x0008
#define SIO_SEND_DATA     0x0010
#define SIO_RECV_DATA     0x0020
#define SIO_ERROR         0x0040
#define SIO_LENGTH_8      0x0080 \\recieves 8 bit data
#define SIO_USE_FIFO      0x0100
#define SIO_USE_PARITY    0x0200
#define SIO_SEND_ENABLE   0x0400 \\sets the uart to send
#define SIO_RECV_ENABLE   0x0800 \\sets the uart to recieve
#define SIO_REQUEST_IRQ   0x4000 \\sets an interrupt when send, recieve and error flag are set.
//-----------------------------------------------------------------------------------

   // Init UART
   REG_RCNT = 0; //turns the uart on in the gba
   REG_SIOCNT = 0; //intializes all the uart control values to zero.
   REG_SIOCNT = SIO_BAUD_9600 | SIO_LENGTH_8 | SIO_RECV_ENABLE | SIO_USE_UART | SIO_REQUEST_IRQ ;
   REG_SIOCNT |= SIO_RECV_DATA | SIO_SEND_DATA;
//-----------------------------------------------------------------------------------


I am having trouble understanding this part of the code and it is the part im having difficulty debugging

Code:

REG_SIOCNT = SIO_BAUD_9600 | SIO_LENGTH_8 | SIO_RECV_ENABLE | SIO_USE_UART | SIO_REQUEST_IRQ ;
   REG_SIOCNT |= SIO_RECV_DATA | SIO_SEND_DATA;


I understand that it is setting up the UART port, but i'm not understanding the code. What does a | do in regards to the code?
Is it simplier way to enable all the registers for UART control at once? instead of writing individual code for each register you want to enable.

Im totally stuck i've been trying to read up on c programming but cant find anything about this "|".

Thanks
a4_jet


OOPS i should have put this in the C code help section, sorry...

#81462 - headspin - Sat Apr 29, 2006 7:55 am

The '|' is called a bitwise OR in C. So you are setting the appropriate bits in the REG_SIOCNT register all at once. The |= is just or'ing more bits to the same register. If you convert all the hex values to their binary equivalents and or them together you will see they are setting individual bits of the register.

Check out CowBite Virtual Harware Spec to find out what each bit of the register means.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#81476 - a4_jet - Sat Apr 29, 2006 12:12 pm

Thanks HeadSpin!

Im getting errors when i go to compile it in visual ham.
This is what pops up
main.c:57: error: parse error before "volatile"
main.c:58: error: parse error before "volatile"
main.c:59: error: parse error before "volatile"
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:59: error: stray '\' in program
main.c:60: error: parse error before "volatile"

Lines 57-60 refer to this code
Code:

   REG_RCNT = 0x0000000; //turns the uart on in the gba
   REG_SIOCNT = 0; //intializes all the uart control values to zero.
   REG_SIOCNT = SIO_BAUD_9600 | SIO_LENGTH_8 | SIO_RECV_ENABLE | SIO_USE_UART | SIO_REQUEST_IRQ ;
   REG_SIOCNT |= SIO_RECV_DATA | SIO_SEND_DATA;


Now i'm sure the error is not occuring here but further up at these lines
Code:

#define REG_SIOCNT        *(volatile unsigned short int *)(REG_BASE + 0x128);  // Serial control
#define REG_SIODATA8      *(volatile unsigned short int *)(REG_BASE + 0x12a);  // Serial data
#define REG_RCNT          *(volatile unsigned short int *)(REG_BASE + 0x134);  // General IO


I understand that this code is defining what address to write the bits into the memory to setup the UART port.
Im not understanding the code and the errors. I know that the volatile unsigned short is referring to a 16bit value that can be changed by anypart of the code(i think that is what volatile means)

i'm not sure what the pointers and the (REG_BASE + "hexadecimal number") are doing in the code.

It also seems to get less errors when i remove the pointers before "volatile".
Code:

#define REG_SIOCNT        (volatile unsigned short int *)(REG_BASE + 0x128);  // Serial control
#define REG_SIODATA8      (volatile unsigned short int *)(REG_BASE + 0x12a);  // Serial data
#define REG_RCNT          (volatile unsigned short int *)(REG_BASE + 0x134);  // General IO


Normally im pretty good at c code, i do some at uni programming microprocessors. But this is my first time having a go at a GBA system.

Any help would be greatly appreciated!

PS.
again this should be in the c code section sorry...

#81477 - Mighty Max - Sat Apr 29, 2006 12:19 pm

Remove the ";" at the end of the defines.

With the current defines, REG_RCNT = 0x0000000; will get substituted to
(volatile unsigned short int *)(REG_BASE + 0x134); = 0x0000000;

the \ Error comes from the SIO_ defines. Use // instead of \\ for comments
_________________
GBAMP Multiboot

#81480 - a4_jet - Sat Apr 29, 2006 1:32 pm

Thanks mightymax

Yes that was silly using \\ instead of // for comments. bit of newb mistake i guess

I think i know what was wrong now... I think you can only assign the bits inside a function. So i have now changed the code too.
Code:

#define REG_BASE          0x4000000


// Serial IO Registers
#define REG_SIOCNT        *(volatile unsigned short int *)(REG_BASE + 0x128)  // Serial control
#define REG_SIODATA8      *(volatile unsigned short int *)(REG_BASE + 0x12a)  // Serial data
#define REG_RCNT          *(volatile unsigned short int *)(REG_BASE + 0x134)  // General IO


// UART settings
#define SIO_USE_UART      0x3000 //must be set for 1 for uart mode

// Baud Rate
#define SIO_BAUD_9600     0x0000 //sets the input to 9600
#define SIO_BAUD_38400    0x0001
#define SIO_BAUD_57600    0x0002
#define SIO_BAUD_115200   0x0003

#define SIO_CTS           0x0004
#define SIO_PARITY_ODD    0x0008
#define SIO_SEND_DATA     0x0010
#define SIO_RECV_DATA     0x0020
#define SIO_ERROR         0x0040
#define SIO_LENGTH_8      0x0080 //recieves 8 bit data
#define SIO_USE_FIFO      0x0100
#define SIO_USE_PARITY    0x0200
#define SIO_SEND_ENABLE   0x0400 //sets the uart to send
#define SIO_RECV_ENABLE   0x0800 //sets the uart to recieve
#define SIO_REQUEST_IRQ   0x4000 //sets an interrupt when send, recieve and error flag are set.
//-----------------------------------------------------------------------------------

void InitUART()
{
   // Init UART
   REG_RCNT = 0x0000000; //turns the uart on in the gba
   REG_SIOCNT = 0; //intializes all the uart control values to zero.
   REG_SIOCNT = SIO_BAUD_9600 | SIO_LENGTH_8 | SIO_RECV_ENABLE | SIO_USE_UART | SIO_REQUEST_IRQ;
   REG_SIOCNT |= SIO_RECV_DATA | SIO_SEND_DATA;
//-----------------------------------------------------------------------------------
}


and bingo no parse errors!!!
Yippee

Thanks for all your help guys and dealing with my newb mistakes