4 Commits

Author SHA1 Message Date
22968ef6a4 bla 2016-11-02 15:18:43 +02:00
09f82d5258 bla 2016-11-02 15:15:45 +02:00
a61348e1c7 lab03.2 2016-11-02 15:14:51 +02:00
5d4b4a1e6d Merge https://gitlab.com/I237-2016/skeleton into HEAD 2016-11-02 14:13:58 +02:00
17 changed files with 150 additions and 2148 deletions

View File

@ -90,13 +90,6 @@ clean:
rm -fr $(BUILD_LIBS_DIR)/*/*.o
install:
@if [ ! -c "$(ARDUINO)" ]; then \
echo -e "\n\nEnvironment variable ARDUINO is \"$(ARDUINO)\" and that is invalid."\
"\nDid you do \"export ARDUINO=/dev/ttyACM0\" before running make install?"\
"\nAlso make sure that ARDUINO env var points to a valid tty device\n\n"; \
exit 1;\
fi
$(AVRDUDE) $(AVRDUDEARGS) -U flash:w:$(TARGET)
format:

View File

@ -1,41 +0,0 @@
# I237 Hardware Programming
This here is my repository for hardware programming course at
Estonian IT College that is based around a
[Arduino Mega 2560](https://www.arduino.cc/en/Main/ArduinoBoardMega2560).
This code us usualy updated once every two weeks when a new excersice is put up
by Silver Kits.
# How to install
Currently to test this code you have to clone the repository
```bash
git clone https://git.wut.ee/arti/i237.git hardware
cd hardware
```
And export a enviroment variable that points to a Arduino Mega board. The
real path will depend on the OS and Computer that you are using. For me it is
usualy `/dev/ttyACM0`.
```bash
export ARDUINO=/dev/ttyACM0
```
After that you can `make` the project.
```bash
make clean
make
make install
```
You can also chekcout previous labs using `git checkout <lab name>`. For example
```bash
git checkout lab02
```
Don't forget to `make clean` after each checkout!

View File

@ -1,11 +0,0 @@
Copyright (C) 2012 Andy Gock
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

View File

@ -1,56 +0,0 @@
avr-uart
========
An interrupt driven UART Library for 8-bit AVR microcontrollers
Maintained by Andy Gock
https://github.com/andygock/avr-uart
Derived from original library by Peter Fleury
Interrupt UART library using the built-in UART with transmit and receive circular buffers.
An interrupt is generated when the UART has finished transmitting or
receiving a byte. The interrupt handling routines use circular buffers
for buffering received and transmitted data.
## Setting up
The `UART_RXn_BUFFER_SIZE` and `UART_TXn_BUFFER_SIZE` constants define
the size of the circular buffers in bytes. Note that these constants must be a power of 2.
You may need to adapt this constants to your target and your application by adding to your
compiler options:
-DUART_RXn_BUFFER_SIZE=nn -DUART_TXn_BUFFER_SIZE=nn
`RXn` and `TXn` refer to UART number, for UART3 with 128 byte buffers, add:
-DUART_RX3_BUFFER_SIZE=128 -DUART_TX3_BUFFER_SIZE=128
UART0 is always enabled by default, to enable the other available UARTs, add the following
to your compiler options (or symbol options), for the relevant USART number:
-DUSART1_ENABLED -DUSART2_ENABLED -DUSART3_ENABLED
To enable large buffer support (over 256 bytes, up to 2^16 bytes) use:
-DUSARTn_LARGE_BUFFER
Where n = USART number.
Supports AVR devices with up to 4 hardware USARTs.
## Documentation
Doxygen based documentation will be coming soon.
## Notes
### Buffer overflow behaviour
When the RX circular buffer is full, and it receives further data from the UART, a buffer overflow condition occurs. Any new data is dropped. The RX buffer must be read before any more incoming data from the UART is placed into the RX buffer.
If the TX buffer is full, and new data is sent to it using one of the `uartN_put*()` functions, this function will loop and wait until the buffer is not full any more. It is important to make sure you have not disabled your UART transmit interrupts (`TXEN*`) elsewhere in your application (e.g with `cli()`) before calling the `uartN_put*()` functions, as the application will lock up. The UART interrupts are automatically enabled when you use the `uartN_init()` functions. This is probably not the idea behaviour, I'll probably fix this some time.
For now, make sure `TXEN*` interrupts are enabled when calling `uartN_put*()` functions. This should not be an issue unless you have code elsewhere purposely turning it off.

File diff suppressed because it is too large Load Diff

View File

@ -1,415 +0,0 @@
#ifndef UART_H
#define UART_H
/************************************************************************
Title: Interrupt UART library with receive/transmit circular buffers
Author: Andy Gock
Software: AVR-GCC 4.1, AVR Libc 1.4
Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
License: GNU General Public License
Usage: see Doxygen manual
Based on original library by Peter Fluery, Tim Sharpe, Nicholas Zambetti.
https://github.com/andygock/avr-uart
LICENSE:
Copyright (C) 2012 Andy Gock
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
LICENSE:
Copyright (C) 2006 Peter Fleury
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
************************************************************************/
/************************************************************************
uart_available, uart_flush, uart1_available, and uart1_flush functions
were adapted from the Arduino HardwareSerial.h library by Tim Sharpe on
11 Jan 2009. The license info for HardwareSerial.h is as follows:
HardwareSerial.h - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
************************************************************************/
/************************************************************************
Changelog for modifications made by Tim Sharpe, starting with the current
library version on his Web site as of 05/01/2009.
Date Description
=========================================================================
05/12/2009 Added Arduino-style available() and flush() functions for both
supported UARTs. Really wanted to keep them out of the library, so
that it would be as close as possible to Peter Fleury's original
library, but has scoping issues accessing internal variables from
another program. Go C!
************************************************************************/
/**
* @defgroup avr-uart UART Library
* @code #include <uart.h> @endcode
*
* @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers.
*
* This library can be used to transmit and receive data through the built in UART.
*
* An interrupt is generated when the UART has finished transmitting or
* receiving a byte. The interrupt handling routines use circular buffers
* for buffering received and transmitted data.
*
* The UART_RXn_BUFFER_SIZE and UART_TXn_BUFFER_SIZE constants define
* the size of the circular buffers in bytes. Note that these constants must be a power of 2.
*
* You need to define these buffer sizes in uart.h
*
* @note Based on Atmel Application Note AVR306
* @author Andy Gock <andy@gock.net>
* @note Based on original library by Peter Fleury and Tim Sharpe.
*/
/**@{*/
#include <stdint.h>
#include <avr/io.h>
#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
#endif
/*
* constants and macros
*/
/* Enable USART 1, 2, 3 as required */
#define USART0_ENABLED
//#define USART1_ENABLED
//#define USART2_ENABLED
#define USART3_ENABLED
/* Set size of receive and transmit buffers */
#ifndef UART_RX0_BUFFER_SIZE
#define UART_RX0_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
#endif
#ifndef UART_RX1_BUFFER_SIZE
#define UART_RX1_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
#endif
#ifndef UART_RX2_BUFFER_SIZE
#define UART_RX2_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
#endif
#ifndef UART_RX3_BUFFER_SIZE
#define UART_RX3_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */
#endif
#ifndef UART_TX0_BUFFER_SIZE
#define UART_TX0_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
#endif
#ifndef UART_TX1_BUFFER_SIZE
#define UART_TX1_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
#endif
#ifndef UART_TX2_BUFFER_SIZE
#define UART_TX2_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
#endif
#ifndef UART_TX3_BUFFER_SIZE
#define UART_TX3_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */
#endif
/* Check buffer sizes are not too large for 8-bit positioning */
#if (UART_RX0_BUFFER_SIZE > 256 & !defined(USART0_LARGE_BUFFER))
#error "Buffer too large, please use -DUSART0_LARGE_BUFFER switch in compiler options"
#endif
#if (UART_RX1_BUFFER_SIZE > 256 & !defined(USART1_LARGE_BUFFER))
#error "Buffer too large, please use -DUSART1_LARGE_BUFFER switch in compiler options"
#endif
#if (UART_RX2_BUFFER_SIZE > 256 & !defined(USART2_LARGE_BUFFER))
#error "Buffer too large, please use -DUSART2_LARGE_BUFFER switch in compiler options"
#endif
#if (UART_RX3_BUFFER_SIZE > 256 & !defined(USART3_LARGE_BUFFER))
#error "Buffer too large, please use -DUSART3_LARGE_BUFFER switch in compiler options"
#endif
/* Check buffer sizes are not too large for *_LARGE_BUFFER operation (16-bit positioning) */
#if (UART_RX0_BUFFER_SIZE > 65536)
#error "Buffer too large, maximum allowed is 65536 bytes"
#endif
#if (UART_RX1_BUFFER_SIZE > 65536)
#error "Buffer too large, maximum allowed is 65536 bytes"
#endif
#if (UART_RX2_BUFFER_SIZE > 65536)
#error "Buffer too large, maximum allowed is 65536 bytes"
#endif
#if (UART_RX3_BUFFER_SIZE > 65536)
#error "Buffer too large, maximum allowed is 65536 bytes"
#endif
/** @brief UART Baudrate Expression
* @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz
* @param baudRate baudrate in bps, e.g. 1200, 2400, 9600
*/
#define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu)+8UL*(baudRate))/(16UL*(baudRate))-1UL)
/** @brief UART Baudrate Expression for ATmega double speed mode
* @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz
* @param baudRate baudrate in bps, e.g. 1200, 2400, 9600
*/
#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ((((xtalCpu)+4UL*(baudRate))/(8UL*(baudRate))-1)|0x8000)
/* test if the size of the circular buffers fits into SRAM */
#if defined(USART0_ENABLED) && ( (UART_RX0_BUFFER_SIZE+UART_TX0_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX0_BUFFER_SIZE + UART_TX0_BUFFER_SIZE larger than size of SRAM"
#endif
#if defined(USART1_ENABLED) && ( (UART_RX1_BUFFER_SIZE+UART_TX1_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX1_BUFFER_SIZE + UART_TX1_BUFFER_SIZE larger than size of SRAM"
#endif
#if defined(USART2_ENABLED) && ( (UART_RX2_BUFFER_SIZE+UART_RX2_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX2_BUFFER_SIZE + UART_TX2_BUFFER_SIZE larger than size of SRAM"
#endif
#if defined(USART3_ENABLED) && ( (UART_RX3_BUFFER_SIZE+UART_RX3_BUFFER_SIZE) >= (RAMEND-0x60 ) )
#error "size of UART_RX3_BUFFER_SIZE + UART_TX3_BUFFER_SIZE larger than size of SRAM"
#endif
/*
** high byte error return code of uart_getc()
*/
#define UART_FRAME_ERROR 0x0800 /**< Framing Error by UART */
#define UART_OVERRUN_ERROR 0x0400 /**< Overrun condition by UART */
#define UART_BUFFER_OVERFLOW 0x0200 /**< receive ringbuffer overflow */
#define UART_NO_DATA 0x0100 /**< no receive data available */
/* Macros, to allow use of legacy names */
#define uart_init(b) uart0_init(b)
#define uart_getc() uart0_getc()
#define uart_putc(d) uart0_putc(d)
#define uart_puts(s) uart0_puts(s)
#define uart_puts_p(s) uart0_puts_p(s)
#define uart_available() uart0_available()
#define uart_flush() uart0_flush()
/*
** function prototypes
*/
/**
@brief Initialize UART and set baudrate
@param baudrate Specify baudrate using macro UART_BAUD_SELECT()
@return none
*/
extern void uart0_init(uint16_t baudrate);
/**
* @brief Get received byte from ringbuffer
*
* Returns in the lower byte the received character and in the
* higher byte the last receive error.
* UART_NO_DATA is returned when no data is available.
*
* @return lower byte: received byte from ringbuffer
* @return higher byte: last receive status
* - \b 0 successfully received data from UART
* - \b UART_NO_DATA
* <br>no receive data available
* - \b UART_BUFFER_OVERFLOW
* <br>Receive ringbuffer overflow.
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
* - \b UART_OVERRUN_ERROR
* <br>Overrun condition by UART.
* A character already present in the UART UDR register was
* not read by the interrupt handler before the next character arrived,
* one or more received characters have been dropped.
* - \b UART_FRAME_ERROR
* <br>Framing Error by UART
*/
extern uint16_t uart0_getc(void);
/**
* @brief Peek at next byte in ringbuffer
*
* Returns the next byte (character) of incoming UART data without removing it from the
* internal ring buffer. That is, successive calls to uartN_peek() will return the same
* character, as will the next call to uartN_getc().
*
* UART_NO_DATA is returned when no data is available.
*
* @return lower byte: next byte in ringbuffer
* @return higher byte: last receive status
* - \b 0 successfully received data from UART
* - \b UART_NO_DATA
* <br>no receive data available
* - \b UART_BUFFER_OVERFLOW
* <br>Receive ringbuffer overflow.
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
* - \b UART_OVERRUN_ERROR
* <br>Overrun condition by UART.
* A character already present in the UART UDR register was
* not read by the interrupt handler before the next character arrived,
* one or more received characters have been dropped.
* - \b UART_FRAME_ERROR
* <br>Framing Error by UART
*/
extern uint16_t uart0_peek(void);
/**
* @brief Put byte to ringbuffer for transmitting via UART
* @param data byte to be transmitted
* @return none
*/
extern void uart0_putc(uint8_t data);
/**
* @brief Put string to ringbuffer for transmitting via UART
*
* The string is buffered by the uart library in a circular buffer
* and one character at a time is transmitted to the UART using interrupts.
* Blocks if it can not write the whole string into the circular buffer.
*
* @param s string to be transmitted
* @return none
*/
extern void uart0_puts(const char *s );
/**
* @brief Put string from program memory to ringbuffer for transmitting via UART.
*
* The string is buffered by the uart library in a circular buffer
* and one character at a time is transmitted to the UART using interrupts.
* Blocks if it can not write the whole string into the circular buffer.
*
* @param s program memory string to be transmitted
* @return none
* @see uart0_puts_P
*/
extern void uart0_puts_p(const char *s );
/**
* @brief Macro to automatically put a string constant into program memory
* \param __s string in program memory
*/
#define uart_puts_P(__s) uart0_puts_p(PSTR(__s))
#define uart0_puts_P(__s) uart0_puts_p(PSTR(__s))
/**
* @brief Return number of bytes waiting in the receive buffer
* @return bytes waiting in the receive buffer
*/
extern uint16_t uart0_available(void);
/**
* @brief Flush bytes waiting in receive buffer
*/
extern void uart0_flush(void);
/** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */
extern void uart1_init(uint16_t baudrate);
/** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */
extern uint16_t uart1_getc(void);
/** @brief Peek at next byte in USART1 ringbuffer */
extern uint16_t uart1_peek(void);
/** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */
extern void uart1_putc(uint8_t data);
/** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */
extern void uart1_puts(const char *s );
/** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */
extern void uart1_puts_p(const char *s );
/** @brief Macro to automatically put a string constant into program memory */
#define uart1_puts_P(__s) uart1_puts_p(PSTR(__s))
/** @brief Return number of bytes waiting in the receive buffer */
extern uint16_t uart1_available(void);
/** @brief Flush bytes waiting in receive buffer */
extern void uart1_flush(void);
/** @brief Initialize USART2 (only available on selected ATmegas) @see uart_init */
extern void uart2_init(uint16_t baudrate);
/** @brief Get received byte of USART2 from ringbuffer. (only available on selected ATmega) @see uart_getc */
extern uint16_t uart2_getc(void);
/** @brief Peek at next byte in USART2 ringbuffer */
extern uint16_t uart2_peek(void);
/** @brief Put byte to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_putc */
extern void uart2_putc(uint8_t data);
/** @brief Put string to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts */
extern void uart2_puts(const char *s );
/** @brief Put string from program memory to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts_p */
extern void uart2_puts_p(const char *s );
/** @brief Macro to automatically put a string constant into program memory */
#define uart2_puts_P(__s) uart2_puts_p(PSTR(__s))
/** @brief Return number of bytes waiting in the receive buffer */
extern uint16_t uart2_available(void);
/** @brief Flush bytes waiting in receive buffer */
extern void uart2_flush(void);
/** @brief Initialize USART3 (only available on selected ATmegas) @see uart_init */
extern void uart3_init(uint16_t baudrate);
/** @brief Get received byte of USART3 from ringbuffer. (only available on selected ATmega) @see uart_getc */
extern uint16_t uart3_getc(void);
/** @brief Peek at next byte in USART3 ringbuffer */
extern uint16_t uart3_peek(void);
/** @brief Put byte to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_putc */
extern void uart3_putc(uint8_t data);
/** @brief Put string to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts */
extern void uart3_puts(const char *s );
/** @brief Put string from program memory to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts_p */
extern void uart3_puts_p(const char *s );
/** @brief Macro to automatically put a string constant into program memory */
#define uart3_puts_P(__s) uart3_puts_p(PSTR(__s))
/** @brief Return number of bytes waiting in the receive buffer */
extern uint16_t uart3_available(void);
/** @brief Flush bytes waiting in receive buffer */
extern void uart3_flush(void);
/**@}*/
#endif // UART_H

0
lib/hd44780_111/hd44780.h Normal file → Executable file
View File

0
lib/hd44780_111/hd44780.txt Normal file → Executable file
View File

0
lib/hd44780_111/hd44780_settings.h Normal file → Executable file
View File

0
lib/hd44780_111/hd44780_settings_example.h Normal file → Executable file
View File

View File

@ -1,19 +1,26 @@
#include <avr/pgmspace.h>
#ifndef _HMI_MSG_H_
#define _HMI_MSG_H_
#define PROG_VERSION "Version: %S built on: %S %S\n"
#define LIBC_VERSION "avr-libc version: %S\n"
#define VER_FW "Version: %S built on: %S %S\n"
#define VER_LIBC "avr-libc version: %S\n"
#define UI_GET_MONTH_LETTER "Enter Month name first letter >"
#define STUD_NAME "Arti Zirk"
#define GET_MONTH_MSG "Enter Month name first letter >"
#define UPTIME "Uptime: %lu s"
const char string_1[] PROGMEM = "January";
const char string_2[] PROGMEM = "February";
const char string_3[] PROGMEM = "March";
const char string_4[] PROGMEM = "April";
const char string_5[] PROGMEM = "May";
const char string_6[] PROGMEM = "June";
const char m1[] PROGMEM = "January";
const char m2[] PROGMEM = "February";
const char m3[] PROGMEM = "March";
const char m4[] PROGMEM = "April";
const char m5[] PROGMEM = "May";
const char m6[] PROGMEM = "June";
PGM_P const months_table[] PROGMEM =
{
string_1,
string_2,
string_3,
string_4,
string_5,
string_6
};
PGM_P const months[] PROGMEM = {m1,m2,m3,m4,m5,m6};
#endif /* _HMI_MSG_H_ */

View File

@ -1,116 +1,66 @@
#include <stdio.h>
#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include "hmi_msg.h"
#include "uart-wrapper.h"
#include "uart.h"
#include "print_helper.h"
#include "../lib/hd44780_111/hd44780.h"
#include "../lib/andygock_avr-uart/uart.h"
#define BAUD 9600
#define BLINK_DELAY_MS 100
volatile uint32_t time;
static inline void init_system_clock(void)
{
TCCR5A = 0; // Clear control register A
TCCR5B = 0; // Clear control register B
TCCR5B |= _BV(WGM52) | _BV(CS52); // CTC and fCPU/256
OCR5A = 62549; // 1 s
TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable
}
static inline void hw_init() {
DDRA |= _BV(DDA3); // setup Arduino pin 25 as output
uart0_init(UART_BAUD_SELECT(BAUD, F_CPU));
uart3_init(UART_BAUD_SELECT(BAUD, F_CPU));
init_system_clock();
sei(); // Enable interupts
stdout = stdin = &uart0_io;
stderr = &uart3_out;
lcd_init();
lcd_clrscr();
}
static inline void print_prog_version() {
fprintf_P(stderr, PSTR(PROG_VERSION),
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
fprintf_P(stderr, PSTR(LIBC_VERSION), PSTR(__AVR_LIBC_VERSION_STRING__));
}
static inline void print_program_startup() {
fprintf_P(stdout, PSTR(STUD_NAME));
fputc('\n', stdout); /* Add a new line to the uart printout */
lcd_puts_P(PSTR(STUD_NAME));
/* ASCII table print */
print_ascii_tbl(stdout);
unsigned char ascii[128];
for (unsigned char i = 0; i < sizeof(ascii); i++) {
ascii[i] = i;
}
print_for_human(stdout, ascii, sizeof(ascii));
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
}
static inline void search_month() {
/* Month search and print */
char letter;
fscanf(stdin, "%c", &letter);
fprintf(stdout, "%c\n", letter);
lcd_goto(0x40); /* Got to the beginning of the next line */
for (int i = 0; i < 6; i++) {
if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) {
fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i]));
fputc('\n', stdout);
lcd_puts_P((PGM_P)pgm_read_word(&months[i]));
lcd_putc(' ');
}
}
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
lcd_puts_P(PSTR(" ")); /* Clear the end of the line */
}
static inline void heartbeat() {
static uint32_t last_time;
uint32_t cur_time;
ATOMIC_BLOCK(ATOMIC_FORCEON) {
cur_time = time;
}
if ((last_time - cur_time) > 0) {
// Toggle led on arduino pin 25
PORTA ^= _BV(PORTA3);
fprintf_P(stderr, PSTR(UPTIME "\n"), cur_time);
}
last_time = cur_time;
}
int main (void)
{
/* set pin 3 of PORTA for output*/
DDRA |= _BV(DDA3);
/* Init stdio on UART0 and UART3 and print user code info */
uart0_init();
uart3_init();
lcd_init();
lcd_clrscr();
stdout = stdin = &uart0_io;
stderr = &uart3_out;
fprintf_P(stderr, PSTR(VER_FW),
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
fprintf_P(stderr, PSTR(VER_LIBC), PSTR(__AVR_LIBC_VERSION_STRING__));
/* End stdio init and info print */
fprintf_P(stdout, PSTR(STUD_NAME "\n"));
lcd_puts_P(PSTR(STUD_NAME));
/* ASCII table print */
print_ascii_tbl(stdout);
unsigned char ascii[128] = {0};
hw_init();
print_prog_version();
print_program_startup();
for (unsigned char i = 0; i < sizeof(ascii); i++) {
ascii[i] = i;
}
print_for_human(stdout, ascii, sizeof(ascii));
while (1) {
heartbeat();
if(uart0_available()) {
search_month();
char month_first_leter;
fprintf_P(stdout, PSTR(UI_GET_MONTH_LETTER));
fscanf(stdin, "%c", &month_first_leter);
fprintf(stdout, "%c\n", month_first_leter);
lcd_goto(0x40);
for (int i = 0; i < 6; i++) {
if (!strncmp_P(&month_first_leter, (PGM_P)pgm_read_word(&months_table[i]), 1)) {
fprintf_P(stdout, PSTR("%S\n"), (PGM_P)pgm_read_word(&months_table[i]));
lcd_puts_P((PGM_P)pgm_read_word(&months_table[i]));
lcd_putc(' ');
}
}
for (int i = 0; i < 16; i++) {
lcd_putc(' ');
}
/* set pin 3 high to turn led on */
PORTA |= _BV(PORTA3);
_delay_ms(BLINK_DELAY_MS);
/* set pin 3 low to turn led off */
PORTA &= ~_BV(PORTA3);
_delay_ms(BLINK_DELAY_MS);
}
}
/* System clock ISR */
ISR(TIMER5_COMPA_vect)
{
time++;
}

View File

@ -1,5 +1,4 @@
#include <stdio.h>
#include <avr/pgmspace.h>
#include "print_helper.h"
int print_ascii_tbl (FILE *stream)
@ -17,17 +16,16 @@ int print_ascii_tbl (FILE *stream)
int print_for_human (FILE *stream, const unsigned char *array, const int len)
{
for (int i = 0; i < len; i++) {
unsigned char c = array[i];
if (c >= ' ' && c <= '~') {
if (!fprintf(stream, "%c", c)) {
if (array[i] >= ' ' && array[i] <= '~') {
if (!fprintf(stream, "%c", array[i])) {
return 0;
}
} else {
if (!fprintf(stream, "\"0x%02x\"", c)) {
if (!fprintf(stream, "\"0x%02x\"", array[i])) {
return 0;
}
}
}
return fprintf(stream, "\n");
return fprintf(stream, "\n");;
}

View File

@ -1,33 +0,0 @@
#include <avr/io.h>
#include <stdio.h>
#include "../lib/andygock_avr-uart/uart.h"
int uart0_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart0_putchar('\r', stream);
}
uart0_putc(c);
return 0;
}
int uart3_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart3_putchar('\r', stream);
}
uart3_putc(c);
return 0;
}
int uart0_getchar(FILE *stream)
{
(void) stream;
return uart0_getc() & 0xff;
}

72
src/uart.c Normal file
View File

@ -0,0 +1,72 @@
#include <avr/io.h>
#include <stdio.h>
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#ifndef BAUD
#define BAUD 9600
#endif
#include <util/setbaud.h>
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
void uart0_init(void)
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= _BV(U2X0);
#else
UCSR0A &= ~(_BV(U2X0));
#endif
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
}
void uart3_init(void)
{
UBRR3H = UBRRH_VALUE;
UBRR3L = UBRRL_VALUE;
#if USE_2X
UCSR3A |= _BV(U2X3);
#else
UCSR3A &= ~(_BV(U2X3));
#endif
UCSR3C = _BV(UCSZ31) | _BV(UCSZ30); /* 8-bit data */
UCSR3B = _BV(TXEN3); /* Enable TX */
}
int uart0_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart0_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
int uart3_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart3_putchar('\r', stream);
}
loop_until_bit_is_set(UCSR3A, UDRE3);
UDR3 = c;
return 0;
}
int uart0_getchar(FILE *stream)
{
(void) stream;
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}

View File

@ -1,15 +1,17 @@
#ifndef _UART_WRAPPER_H_
#define _UART_WRAPPER_H_
#ifndef _UART_H_
#define _UART_H_
int uart0_putchar(char c, FILE *stream);
int uart0_getchar(FILE *stream);
int uart3_putchar(char c, FILE *stream);
void uart0_init(void);
void uart3_init(void);
/* http://www.ermicro.com/blog/?p=325 */
FILE uart0_io = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW);
FILE uart3_out = FDEV_SETUP_STREAM(uart3_putchar, NULL, _FDEV_SETUP_WRITE);
#endif /* _UART_WRAPPER_H_ */
#endif /* _UART_H_ */

View File

@ -18,8 +18,10 @@ for FILE in "$@"
do
RESULT="$(astyle --style=1tbs \
--indent-col1-comments \
--break-blocks \
--pad-oper \
--pad-header \
--delete-empty-lines \
--add-brackets \
--convert-tabs \
--max-code-length=80 \