Участник:Vooon/crumb128/usb

Материал из roboforum.ru Wiki
Перейти к: навигация, поиск

Project tree

{{#tree:openlevels=2|

  • ./
    • doc
      • html
        • doxygen-html
      • latex
        • doxygen-latex
    • includes
      • portpc.h
      • portpc.cpp
    • main.h
    • main.cpp
    • Makefile
    • Doxyfile

}}

./

includes

portpc.h

<source lang="cpp"> /**

* \file portpc.h
* \version PortPC.h, v 0.0.1 01/10/2007 21:20 vovan
* \defgroup port_pc <portpc.h>: Соединение с ПК
* \code #include "portpc.h" \endcode
*/

/**

*/
  1. ifndef PORTPC_H
  2. define PORTPC_H
  1. include <avr/io.h>
  2. include <inttypes.h>

/**

* \ingroup port_pc
* Скорость обмена с ПК
*/

/*[[[cog import cog pc_baud = 115200 f_cpu = int(f_cpu) cog.outl( "#define PC_BAUD %s" % pc_baud ) ]]]*/

  1. define PC_BAUD 115200

//[[[end]]] (checksum: 515e903dc784c9393118884a663d767e)

/**

* \ingroup port_pc
* Формула для расчета значения делителя
*/

/*[[[cog pc_ubrr = int((f_cpu/(pc_baud*16)) - 1) pc_real_baud = int(f_cpu / (16 * (pc_ubrr + 1))) pc_baud_error = ((float(pc_real_baud)/float(pc_baud) - 1.0) * 100.0)

if pc_baud_error != 0 and pc_baud_error < 2: cog.msg( "WARNING: pc baudrate error: %s%%" % pc_baud_error ) if pc_baud_error > 2: cog.error( "pc baudrate error: %s%%" % pc_baud_error )

cog.outl( "#define PC_UBRR %s" % pc_ubrr ) cog.outl( "#define PC_UBRRH %s" % (pc_ubrr>>8) ) cog.outl( "#define PC_UBRRL %s" % (0xff & pc_ubrr) ) cog.outl( "#define PC_REAL_BAUD %s" % pc_real_baud ) cog.outl( "#define PC_BAUD_ERR %s" % pc_baud_error ) ]]]*/

  1. define PC_UBRR 7
  2. define PC_UBRRH 0
  3. define PC_UBRRL 7
  4. define PC_REAL_BAUD 115200
  5. define PC_BAUD_ERR 0.0

//[[[end]]] (checksum: 357c9b7125d3f4a2fca94deb9d6e94ea)

/**

* \ingroup port_pc
*/

class port_pc { public: void init(void); void proc(void); char is_char_available(void); char receive_char(void); void send_char(char data); };

  1. endif // PORTPC_H

</source>

portpc.cpp

<source lang="cpp"> /**

* \file portpc.c
* \version PortPC.c, v 0.0.1 01/10/2007 21:21 vovan
*/
  1. include <avr/io.h>
  2. include <inttypes.h>
  1. include "portpc.h"

/*

* Настройка USART
*/

void port_pc::init(void) { UBRR1L = PC_UBRRL; UBRR1H = PC_UBRRH; UCSR1C = (1<<UCSZ11) | (1<<UCSZ10); // 8N1 UCSR1B = (1<<RXEN1) | (1<<TXEN1); };

void port_pc::proc(void) { while(!is_char_available()); send_char(receive_char()); };

// //- stdio foo ----------------------------------------------------------------- //

char port_pc::is_char_available(void) {

       // Does the RX0 bit of the USART Status and Control Register
       // indicate a char has been received?

if ( (UCSR1A & _BV(RXC1)) ) return 1; else return 0; };

char port_pc::receive_char(void) {

       // Return the char in the UDR0 register

return UDR1; };

void port_pc::send_char(char data) { int i = 0;

   // To send data with the USART put the data in the USART data register

UDR1 = data;

   // Check to see if the global interrupts are enabled

if(SREG & 0x80) {

       // Wait until the byte is sent or we count out

while ( !(UCSR1A & _BV(TXC1)) && (i < 10000) ) { i++; } } else // Wait until the byte is sent while( !(UCSR1A & _BV(TXC1)) );

       // Clear the TXCflag

UCSR1A = UCSR1A | _BV(TXC1); }; </source>

Makefile

<source lang="bash">

  1. output filename

program_name := main

  1. source directories ('.' - current dir)
  2. source_dirs  := .

source_dirs  := . includes

  1. source_dirs  := $(addprefix ../,$(source_dirs))
  1. uC type

mcu := atmega128

  1. CPU freq

f_cpu := 14745600

  1. compiler flags

cpflags := -mmcu=$(mcu) -Wall -g -O2 -DF_CPU=$(f_cpu)

  1. linker flags

ldflags := -mmcu=$(mcu) -Wl

  1. - do not need to edit ----------------------------------------------

rm := rm -rf cog := cog.py -cr -D f_cpu=$(f_cpu)

  1. compiller

cc := avr-g++ cclog := compile.log

  1. avr-objcopy

objcpy := avr-objcopy

  1. doxygen

doxygen := doxygen doxylog := doxygen.log

search_wildcards := $(addsuffix /*.cpp,$(source_dirs))

$(addsuffix .out,$(program_name)): cog all

all: $(notdir $(patsubst %.cpp,%.o, $(wildcard $(search_wildcards) ) ) ) $(cc) $(ldflags) $^ -o $(addsuffix .out,$(program_name)) 2>>$(cclog) $(objcpy) -R.eeprom -O ihex $(addsuffix .out,$(program_name)) $(addsuffix .hex,$(program_name)) 2>>$(cclog) $(doxygen) 2>> $(doxylog)

VPATH := $(source_dirs)

%.o: %.cpp $(cc) $(cpflags) $(addprefix -I,$(source_dirs)) -MD -c $< 2>>$(cclog)

cog: $(cog) $(addprefix -I,$(source_dirs)) $(addsuffix /*.cpp,$(source_dirs)) $(addsuffix /*.h,$(source_dirs))

include $(wildcard *.d)

upload: uisp -dprog=abb -dlpt=/dev/parport0 --erase --upload --verify if=./$(addsuffix .hex,$(program_name))

clean: $(rm) $(addsuffix .out,$(program_name)) $(addsuffix .hex,$(program_name)) *.d *.o *.hex $(cclog) $(doxylog) doc $(cog) $(addprefix -I,$(source_dirs)) -x $(addsuffix /*.cpp,$(source_dirs)) $(addsuffix /*.h,$(source_dirs)) </source>