First XMEGA-A1 USART usage and PHP Frontend

Today I had a little time to work on my recently ordered Atmel AVR XMEGA-A1 Xplained, which I need to evaluate all necessary functions used for my university project which is a humanoid robot made of a lot servos.

So I just started with some research on google because there is no material delivered with a Xplained kit, not even a hardware user’s guide which could tell what port is used for the integrated stuff like LED, switches, USART, etc. but after some time I got all informations and started coding, my goal was to get a nice looking terminal from which I’m able to toggle the LED by just pressing the corresponding numbers e.g. to lit up LED3 so press 3 in terminal window, LED3 turns on and get a response which LED was toggled.

I will just start with the code now and after some lines explain what they do.

So here I do the first break to explain that all those includes stand for:

  • asf.h – include all needed Atmel frameworks
  • gpio.h – this header file includes all used stuff defined (LED0_GPIO, LED1_GPIO and so on)
  • string.h – just needed to use strlen later
  • sysclk.h – used to synchronize the systems clock

Just define some variables, we need them later a lot.

Since the Xplained board uses LED as active LOW the whole PORT (4 or PORTE) has to be set high by using gpio_set_pin_group_high(LED_PORT_ID, LED_PORT_MASK);, this function first needs to get the PORT and then a bitmask, both are defined above.

Now write some text back from the XMEGA to the terminal window. Here the strlen function is used to get the character count we hand in via const char text[] and then put every character each by each to our terminal using usart_putchar(USART_SERIAL_PORT, text[i]);. This function needs first to know to which USART it should write to and the character, in this case position i inside the text[] array.

This function is very easy and don’t need a lot explanation, just hand the menu to pre-defined function send_uart_text(), it’s possible to write almost everything.

Almost done, first define used vars received_byte and i, then initialize the Xplain board so the compiler knows where to find PORTx, PINx, LEDx etc. with board_init(); then synchronize the systems clock sysclk_init();. After the board is ready we can start with passing the predefined USART options into USART_SERIAL_OPTIONS to use it right after.
In usart_init_rs232(USART_SERIAL_PORT, &USART_SERIAL_OPTIONS); it needs the used USART port (USART_SERIAL_PORT) and of course the whole options-set (&USART_SERIAL_OPTIONS). The following show_menu(); will just show the predesigned menu.

Now let’s enter the main while-loop and catch some user interactions. First use the predefined var received_byte = usart_getchar(USART_SERIAL_PORT); and use a function to get the character received from terminal. Now it’s possible to just use some “if-code” to let the XMEGA doanything. In this case I just catch die numbers from 0-7 to toggle LED and a return to show the menu again, remember to use “\r” to detect a return. light_single_led(LED7_GPIO); just uses the function made before to toggle a specified LED, send_uart_text(“\n\rLED7″); will send some text to terminal.

After I got everything to work I got this idea to send some commands from my iPhone, but since I wanted to keep it really simple I came to the point to just install XAMPP and wrote some really simple PHP code, I will not explain the PHP, it’s really simple stuff.

That’s it! I can open a terminal connection to my XMEGA, send any command I want and also control it from any browser available.
What else? Bluetooth! Later I will attach a BTM-222 and control it via Bluetooth.