PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Vorstellung - Xmega Tutorial - Linksammlung - Atmega32U4 Beispiel



stromflo
01.05.2012, 08:31
Guten Morgen,

Meine Seiten werden zurzeit komplett überarbeitet!

Gruß Florian

radbruch
01.05.2012, 09:34
Hallo

Über den ATMega32U4 bin ich auch schon gestolpert:
https://www.roboternetz.de/community/threads/25435-Erfolg-der-Woche?p=539603&viewfull=1#post539603

Aus Zeitmangel bin ich aber über die einfache USB-Kommunikation nicht hinausgekommen:


/* Simple example for Teensy USB Development Board
* http://www.pjrc.com/teensy/
* Copyright (c) 2008 PJRC.COM, LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdint.h>
#include <util/delay.h>
#include "usb_serial.h"

#define LED_CONFIG (DDRE |= (1<<6)) // PE6 ist die grüne LED mic 5.2.2012
#define LED_ON (PORTE |= (1<<6))
#define LED_OFF (PORTE &= ~(1<<6))
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))

void send_str(const char *s);
uint8_t recv_str(char *buf, uint8_t size);
void parse_and_execute_command(const char *buf, uint8_t num);

#if 1
// Very simple character echo test
int main(void)
{
CPU_PRESCALE(0);
LED_CONFIG;
LED_ON;
usb_init();
_delay_ms(1000);
LED_OFF;
while (1) {
int n = usb_serial_getchar();
if (n >= 0) {
usb_serial_putchar(n+1);
}
}
}

#else

// Basic command interpreter for controlling port pins
int main(void)
{
char buf[32];
uint8_t n;

// set for 16 MHz clock, and turn on the LED
CPU_PRESCALE(0);
LED_CONFIG;
LED_ON;

// initialize the USB, and then wait for the host
// to set configuration. If the Teensy is powered
// without a PC connected to the USB port, this
// will wait forever.
usb_init();
while (!usb_configured()) /* wait */ ;
_delay_ms(1000);

while (1) {
// wait for the user to run their terminal emulator program
// which sets DTR to indicate it is ready to receive.
while (!(usb_serial_get_control() & USB_SERIAL_DTR)) /* wait */ ;

// discard anything that was received prior. Sometimes the
// operating system or other software will send a modem
// "AT command", which can still be buffered.
usb_serial_flush_input();

// print a nice welcome message
send_str(PSTR("\r\nTeensy USB Serial Example, "
"Simple Pin Control Shell\r\n\r\n"
"Example Commands\r\n"
" B0? Read Port B, pin 0\r\n"
" C2=0 Write Port C, pin 1 LOW\r\n"
" D6=1 Write Port D, pin 6 HIGH (D6 is LED pin)\r\n\r\n"));

// and then listen for commands and process them
while (1); {
send_str(PSTR("> "));
n = recv_str(buf, sizeof(buf));
if (n == 255) break;
send_str(PSTR("\r\n"));
parse_and_execute_command(buf, n);
}
}
}
#endif

// Send a string to the USB serial port. The string must be in
// flash memory, using PSTR
//
void send_str(const char *s)
{
char c;
while (1) {
c = pgm_read_byte(s++);
if (!c) break;
usb_serial_putchar(c);
}
}

// Receive a string from the USB serial port. The string is stored
// in the buffer and this function will not exceed the buffer size.
// A carriage return or newline completes the string, and is not
// stored into the buffer.
// The return value is the number of characters received, or 255 if
// the virtual serial connection was closed while waiting.
//
uint8_t recv_str(char *buf, uint8_t size)
{
int16_t r;
uint8_t count=0;

while (count < size) {
r = usb_serial_getchar();
if (r != -1) {
if (r == '\r' || r == '\n') return count;
if (r >= ' ' && r <= '~') {
*buf++ = r;
usb_serial_putchar(r);
count++;
}
} else {
if (!usb_configured() ||
!(usb_serial_get_control() & USB_SERIAL_DTR)) {
// user no longer connected
return 255;
}
// just a normal timeout, keep waiting
}
}
return count;
}

// parse a user command and execute it, or print an error message
//
void parse_and_execute_command(const char *buf, uint8_t num)
{
uint8_t port, pin, val;

if (num < 3) {
send_str(PSTR("unrecognized format, 3 chars min req'd\r\n"));
return;
}
// first character is the port letter
if (buf[0] >= 'A' && buf[0] <= 'F') {
port = buf[0] - 'A';
} else if (buf[0] >= 'a' && buf[0] <= 'f') {
port = buf[0] - 'a';
} else {
send_str(PSTR("Unknown port \""));
usb_serial_putchar(buf[0]);
send_str(PSTR("\", must be A - F\r\n"));
return;
}
// second character is the pin number
if (buf[1] >= '0' && buf[1] <= '7') {
pin = buf[1] - '0';
} else {
send_str(PSTR("Unknown pin \""));
usb_serial_putchar(buf[0]);
send_str(PSTR("\", must be 0 to 7\r\n"));
return;
}
// if the third character is a question mark, read the pin
if (buf[2] == '?') {
// make the pin an input
*(uint8_t *)(0x21 + port * 3) &= ~(1 << pin);
// read the pin
val = *(uint8_t *)(0x20 + port * 3) & (1 << pin);
usb_serial_putchar(val ? '1' : '0');
send_str(PSTR("\r\n"));
return;
}
// if the third character is an equals sign, write the pin
if (num >= 4 && buf[2] == '=') {
if (buf[3] == '0') {
// make the pin an output
*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
// drive it low
*(uint8_t *)(0x22 + port * 3) &= ~(1 << pin);
return;
} else if (buf[3] == '1') {
// make the pin an output
*(uint8_t *)(0x21 + port * 3) |= (1 << pin);
// drive it high
*(uint8_t *)(0x22 + port * 3) |= (1 << pin);
return;
} else {
send_str(PSTR("Unknown value \""));
usb_serial_putchar(buf[3]);
send_str(PSTR("\", must be 0 or 1\r\n"));
return;
}
}
// otherwise, error message
send_str(PSTR("Unknown command \""));
usb_serial_putchar(buf[0]);
send_str(PSTR("\", must be ? or =\r\n"));
}


Wenn möglich würde ich dieses Board gerne mit LunaAVR (http://avr.myluna.de/doku.php?id=de:about) programmieren:

avr.device = atmega32u4
avr.clock = 16000000

porte.6.mode = output, pulldown ' grüne LED aus

Do
waitms 500
toggle porte.6
waitms 100
toggle porte.6
Loop


Gruß

mic

stromflo
01.05.2012, 13:48
LunaAVR kannte ich bisher noch nicht.
Hab das ganze mal ein wenig überfolgen und es schaut sehr interessant aus.
Mal schauen ob ich mal dazu komm, da ein wenig zu probieren.

Gruß Flo

rgf
02.05.2012, 14:35
Falls ein paar Tipps hilfreich sind, stehe ich gerne zur Verfügung.
Einige der Ausdrücke in C existieren auch in luna, beispielsweise BitShift mit << und >>.

Die Portierung von C-Source ist möglich, zum Großteil wird der geschriebene Code kürzer, beispielsweise beim setzen von Portpins.
Auch ist es sinnvoll in solchen Fällen eine entsprechende Klasse anzulegen, um den Code modular aufzubauen.
Ein gutes Beispiel hierfür sind die Klassen zur Ansteuerung von SD-Karten mit FAT32 und langen Dateinamen (http://avr.myluna.de/doku.php?id=de:sdcard-class).

Zu beachten ist, dass in luna im Unterschied zu C ein anderes Parameterkonzept für Methoden verwendet wird. D.h. Variablen/Arrays und Strukturen befinden sich in einem geschlossenen Namensraum, sodass die Übergabe von referenzierten Variablen oder Arrays in der Regel nicht notwendig ist. Die Implementation ist jedoch zusätzlich geplant.

Beispiel:

in vielen C-Funktionen werden Buffer-Arrays als Referenz/Zeiger übergeben:


uint8_t recv_str(char *buf, uint8_t size) { .. }


in luna erstellt man für die meisten Anwendungen bestenfalls eine Klasse, in welcher globale Arrays Klassenweit verfügbar sind. Hierdurch ist eine Referenzübergabe nicht notwendig, da es auf das Selbe hinausläuft:


class usb

dim buf(31) as byte

function recv_str(size as byte) as byte
dim i as byte
if size then
for i=0 to size
buf(i) = readByte()
next
return 1 ' success
endif
return 0 ' failed
endfunc

function readByte() as byte
return [...]
endfunc

endclass


aus dem Hautprogramm ist der Buffer ebenfalls über die Klasse erreichbar:



a = usb.buf(0)


Einfacher ist es jedoch, man nutzt die dynamische Speicherverwaltung in luna und damit die Möglichkeit dynamische Datenlängen einzulesen:


class usb

function recv_str(size as byte) as string
dim i as byte
dim s as string
if size then
s = spc(size) ' buffer erzeugen (schneller wenn Speicherbereich bereits angelegt)
for i=1 to size
s.ByteValue(i) = readByte()
next
endif
return s
endfunc

function readByte() as byte
return [...]
endfunc

endclass



Man benötigt prinzipiell also keinen Byte-Buffer für das Einlesen von Strings, sondern überlässt das der Speicherverwaltung.
Bei großen, festen Blöcken wie im Beispiel der SD-Karten-Sektoren und -Daten ist es demgegenüber dann natürlich sinnvoller und schneller einen innerhalb der Klasse globalen Speicherbereich zu allozieren. Hierfür gibt es die MemoryBlocks (http://avr.myluna.de/doku.php?id=de:memoryblock), auf die dann alle möglichen Zugriffe vorgenommen werden können.

Gruß, rgf