Date:2017-09-11 13:58:32 (1 year 2 months ago)
Author:Josef Filzmaier
Commit:8c574484b889c7b17d2ba4b6929947050f87d91e
Message:atusb/fw: Introduce DEBUG flag

This flag can be used to enable debugging over uart. Currently only
available for boards with the at90usb1287 chip.

Signed-off-by: Josef Filzmaier <j.filzmaier@gmx.at>
Files: atusb/fw/Makefile (3 diffs)
atusb/fw/atusb.c (2 diffs)
atusb/fw/ep0.c (1 diff)
atusb/fw/uart.c (1 diff)
atusb/fw/uart.h (1 diff)

Change Details

atusb/fw/Makefile
11#
2# Makefile - Makefile of the ATUSB firmware
2# Makefile - Makefile of the ATUSB firmware
33#
44# Written 2010-2011, 2013 by Werner Almesberger
55# Copyright 2010-2011, 2013 by Werner Almesberger
...... 
1313SHELL = /bin/bash
1414
1515NAME = atusb
16DEBUG = false
1617
1718CFLAGS = -g -mmcu=$(CHIP) -DBOOT_ADDR=$(BOOT_ADDR) \
1819     -Wall -Wextra -Wshadow -Werror -Wno-unused-parameter \
1920     -Wmissing-prototypes -Wmissing-declarations -Wstrict-prototypes
2021
22ifeq ($(DEBUG),true)
23CFLAGS += -DDEBUG
24endif
25
2126ifeq ($(NAME),rzusb)
2227CHIP=at90usb1287
2328CFLAGS += -DRZUSB -DAT86RF230
...... 
4651BOOT_OBJS = boot.o board.o sernum.o spi.o flash.o dfu.o \
4752            dfu_common.o usb.o boot-atu2.o
4853
54ifeq ($(DEBUG),true)
55OBJS += uart.o
56endif
4957
5058ifeq ($(NAME),rzusb)
5159OBJS += board_rzusb.o
atusb/fw/atusb.c
2424#include "spi.h"
2525#include "atusb/ep0.h"
2626
27#ifdef DEBUG
28#include "uart.h"
29#endif
30
2731
2832int main(void)
2933{
...... 
3539
3640    /* now we should be at 8 MHz */
3741
42#ifdef DEBUG
43    uart_init();
44    static FILE atben_stdout = FDEV_SETUP_STREAM(uart_write_char, NULL,
45                             _FDEV_SETUP_WRITE);
46    stdout = &atben_stdout;
47#endif
48
3849    usb_init();
3950    ep0_init();
4051#ifdef ATUSB
atusb/fw/ep0.c
4545#define HW_TYPE HW_TYPE_RZUSB
4646#endif
4747
48#ifdef DEBUG
49#include "uart.h"
50#include <stdio.h>
51#define debug(FORMAT,args...) printf(FORMAT,##args)
52#define error(FORMAT,args...) printf(FORMAT,##args)
53#else
4854#define debug(...)
4955#define error(...)
56#endif
5057
5158
5259static const uint8_t id[] = { EP0ATUSB_MAJOR, EP0ATUSB_MINOR, HW_TYPE };
atusb/fw/uart.c
1/*
2 * fw/uart.h - Functions needed for debugging over uart
3 *
4 * Code adapted from http://www.roboternetz.de/wissen/index.php/UART_mit_avr-gcc
5 * and http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial
6 *
7 * Published under the Creative Commons Share-Alike licence
8 * https://creativecommons.org/licenses/by-sa/2.0/de/
9 *
10 * S. Salewski 2007
11 *
12 * Adapted by
13 * Josef Filzmaier 2017
14 */
15
16#include <avr/io.h>
17#include "uart.h"
18
19#define USART_BAUD 38400UL
20#define F_CPU 8000000UL
21
22#define Wait_USART_Ready() while (!(UCSR1A & (1<<UDRE1)))
23#define UART_UBRR (F_CPU/(16L*USART_BAUD)-1)
24
25// initialize USART, 8N1 mode
26void
27uart_init(void)
28{
29/* TODO: Find a working configuration for uart for the atmega32u2 */
30#if CHIP == at90usb1287
31    CLKPR = (1 << CLKPCE);
32    CLKPR = 0; // clock prescaler == 0, so we have 16 MHz mpu frequency
33    UBRR1 = UART_UBRR;
34    UCSR1C = (1 << UCSZ10) | (1 << UCSZ11);
35    UCSR1B = (1 << TXEN1);
36    do
37    {
38        UDR1;
39    }
40    while (UCSR1A & (1 << RXC1));
41#endif
42
43}
44
45int uart_write_char(char c, FILE* stream)
46{
47    if (c == '\n'){
48        uart_new_line();
49    }
50    else {
51        Wait_USART_Ready();
52        UDR1 = c;
53    }
54    return 0;
55}
56
57void
58uart_new_line(void)
59{
60    Wait_USART_Ready();
61    UDR1 = '\r';
62    Wait_USART_Ready();
63    UDR1 = '\n';
64}
atusb/fw/uart.h
1/*
2 * fw/uart.h - Functions needed for debugging over uart
3 *
4 * Code adapted from http://www.roboternetz.de/wissen/index.php/UART_mit_avr-gcc
5 * and http://www.mikrocontroller.net/articles/AVR-GCC-Tutorial
6 *
7 * Published under the Creative Commons Share-Alike licence
8 * https://creativecommons.org/licenses/by-sa/2.0/de/
9 *
10 * S. Salewski 2007
11 *
12 * Adapted by
13 * Josef Filzmaier 2017
14 */
15
16#ifndef UART_H_
17#define UART_H_
18
19#include <stdio.h>
20
21void uart_init(void);
22int uart_write_char(char c, FILE* stream);
23void uart_new_line(void);
24
25#endif /* UART_H_ */

Archive Download the corresponding diff file



interactive