26 Commits

Author SHA1 Message Date
04031523d4 Style fixes and added some comments 2016-11-20 00:51:04 +02:00
f3676e9408 Merge branch 'master' of https://gitlab.com/I237-2016/skeleton 2016-11-20 00:15:30 +02:00
256059d358 Rename all files in doc directory to lower case to avoid problems in case-insensitive and case-aware file systems. 2016-11-19 12:43:54 +02:00
55ed13b805 Add LCD Keypad shield placement image 2016-11-19 12:30:47 +02:00
861f0c57d8 Fix USB-UART wiring table mistake pointed out by Liina 2016-11-19 12:30:22 +02:00
30bd26d471 Add heartbeat and system time 2016-11-07 02:30:33 +02:00
22461eaff4 Async uart 2016-11-07 01:42:18 +02:00
6635f07e8d Merge branch 'make-install-warning' of https://gitlab.com/artizirk/skeleton 2016-11-06 20:57:16 +02:00
39a457b101 Error out from make install if ARDUINO env var is unset 2016-11-06 20:37:15 +02:00
079a8ff367 Merge branch 'master' of https://gitlab.com/I237-2016/skeleton 2016-11-06 18:52:37 +02:00
8b7377dff3 Add interrupt driven UART library from https://github.com/andygock/avr-uart 2016-10-31 20:13:51 +02:00
51e4a8bc9b Enable to use main() without return value 2016-10-31 19:52:30 +02:00
9e46b92744 Constants shouldn't be uppercase 2016-10-24 16:08:34 +03:00
500c36b81b Last fixes for lab03.2 2016-10-24 00:01:26 +03:00
9d5fff9fa0 Better final version of lab03.2 2016-10-23 22:58:24 +03:00
f72a7b33e0 Final version of lab03.2 2016-10-23 22:49:49 +03:00
1fad6de2e1 Even lower memory usage 2016-10-23 22:11:20 +03:00
14a6e4ff91 Merge Silvers Makefile changes 2016-10-22 01:36:04 +02:00
281d4d72c5 Format only .c files in source directory with make format 2016-10-22 01:35:04 +02:00
2ea46d4402 Format only .c files in source directory with make format 2016-10-21 13:48:22 +03:00
e6888bd251 Add LTO and unused data/functions optimisations suggested by Arti 2016-10-21 13:40:53 +03:00
89b7339775 Enable Link Time Optimization and function garbage collection 2016-10-19 22:27:25 +02:00
adc3ad6a4a Merge https://gitlab.com/I237-2016/skeleton 2016-10-19 21:53:05 +02:00
f4c572a2e6 Change formating script 2016-10-19 21:51:12 +02:00
de62693b6d Add make size target. This target can be used to print firmware memory usage in AVR format 2016-10-11 12:08:53 +03:00
25b1feb71d make format 2016-10-11 00:43:31 +03:00
18 changed files with 2163 additions and 156 deletions

View File

@ -8,6 +8,7 @@ CC = avr-gcc
OBJCOPY = avr-objcopy
AVRDUDE = avrdude
CODE_FORMATTER = tooling/format-code.sh
AVRSIZE = avr-size
BOARD = atmega2560
@ -42,13 +43,19 @@ CFLAGS = -Wall \
-Werror \
-Wfatal-errors \
-Os \
-flto \
-fdata-sections \
-ffunction-sections \
-mmcu=$(BOARD) \
-DF_CPU=16000000UL \
-DGIT_DESCR=\"$(shell git describe --abbrev=6 --dirty --always --tags --long)\" \
-ffreestanding \
-std=c11
# Linker flags
LDFLAGS = -mmcu=$(BOARD)
LDFLAGS = -mmcu=$(BOARD) \
-flto \
-Wl,-gc-sections
OBJCOPYARGS = -O ihex \
-R .eeprom
@ -62,6 +69,9 @@ AVRDUDEARGS = -p $(BOARD) \
-V \
-D
AVRSIZEARGS = -C \
--mcu=$(BOARD)
all: $(ELF) $(TARGET)
%.o : %.c
@ -80,9 +90,19 @@ 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:
$(CODE_FORMATTER) $(SRC)
$(CODE_FORMATTER) $(SRCDIR)/*.c
.PHONY: clean install format
size:
$(AVRSIZE) $(AVRSIZEARGS) $(ELF)
.PHONY: clean install format size

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View File

@ -10,9 +10,21 @@ This shield consists of three logical parts:
<div class=pagebreak></div>
## Connection to Arduino Mega
Following illustration shows how shield is connected to Arduino mega board.
![arduino-mega-lcd1602-keypad-shield-placement.png](arduino-mega-lcd1602-keypad-shield-placement.png)
Image source: [sainsmart.com](http://www.sainsmart.com/media/catalog/product/2/_/2_16_6.jpg)
Additional installation details and tutorials can be found with help of keywords [Arduino 1602 lcd keypad shield tutorial](https://www.google.ee/webhp?q=Arduino+1602+lcd+keypad+shield+tutorial).
<div class=pagebreak></div>
## Wiring illustration
![Arduino Mega LCD1602 Keypad shield wiring LCD part.png](Arduino-Mega-LCD1602-keypad-shield-wiring.png)
![arduino-mega-lcd1602-keypad-shield-wiring.png](arduino-mega-lcd1602-keypad-shield-wiring.png)
Author: [Lauri Võsandi](http://lauri.võsandi.com/arduino/lcd1602-key-shield.html#hd44780)

View File

Before

Width:  |  Height:  |  Size: 119 KiB

After

Width:  |  Height:  |  Size: 119 KiB

View File

@ -6,12 +6,12 @@ This wiring schema uses only Tx from Arduino and is suitable to be used as stand
## Wiring illustration
![Arduino Mega USB UART wiring.png](Arduino-Mega-USB-UART-wiring.png)
![arduino-mega-usb-uart-wiring.png](arduino-mega-usb-uart-wiring.png)
## Wiring table
| Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | USB UART converter pin |
| --- | --- | --- | --- |
| Ground (GND) | GND | GND | GND |
| Transmit data from Arduino (TxD) | PORTJ pin 1 (TXD3) | Digital pin 14 (TX3) | TxD |
| Transmit data from Arduino (TxD) | PORTJ pin 1 (TXD3) | Digital pin 14 (TX3) | RxD |

View File

Before

Width:  |  Height:  |  Size: 133 KiB

After

Width:  |  Height:  |  Size: 133 KiB

View File

@ -0,0 +1,11 @@
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.

56
lib/andygock_avr-uart/README.md Executable file
View File

@ -0,0 +1,56 @@
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.

1466
lib/andygock_avr-uart/uart.c Executable file

File diff suppressed because it is too large Load Diff

415
lib/andygock_avr-uart/uart.h Executable file
View File

@ -0,0 +1,415 @@
#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

View File

@ -2,17 +2,18 @@
#ifndef _HMI_MSG_H_
#define _HMI_MSG_H_
const char PROG_VERSION[] PROGMEM = "Version: %s built on: %s %s\n";
const char LIBC_VERSION[] PROGMEM = "avr-libc version: %s\n";
const char STUD_NAME[] PROGMEM = "Arti Zirk";
const char GET_MONTH_MSG[] PROGMEM = "Enter Month name first letter >";
const char ENG_MONTH[6][9] PROGMEM = {
"January",
"February",
"March",
"April",
"May",
"June",
};
#define PROG_VERSION "Version: %S built on: %S %S"
#define LIBC_VERSION "avr-libc version: %S"
#define STUD_NAME "Arti Zirk"
#define GET_MONTH_MSG "Enter Month name first letter >"
#define UPTIME_MSG "Uptime: %lu s"
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[] PROGMEM = {m1,m2,m3,m4,m5,m6};
#endif /* _HMI_MSG_H_ */

View File

@ -1,69 +1,128 @@
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "hmi_msg.h"
#include "uart.h"
#include "../lib/andygock_avr-uart/uart.h"
#include "uart_wrap.h"
#include "print_helper.h"
#include "../lib/hd44780_111/hd44780.h"
#define BLINK_DELAY_MS 100
#define BAUDRATE 9600
int main (void)
// For configuring arduino mega pin 25
#define LED_INIT DDRA |= _BV(DDA3);
#define LED_TOGGLE PORTA ^= _BV(PORTA3)
static inline void init_system_clock(void)
{
/* Init */
DDRA |= _BV(DDA3);
uart0_init();
uart3_init();
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 init_hw (void)
{
// IO init
/// Set arduino pin 25 as output
LED_INIT;
// System clock
init_system_clock();
// UART init
uart0_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
uart3_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
stdout = stdin = &uart0_io;
stderr = &uart3_out;
// LCD init
lcd_init();
lcd_clrscr();
/* End init */
/* Print version info to stderr */
fprintf_P(stderr, PROG_VERSION, GIT_DESCR, __DATE__, __TIME__);
fprintf_P(stderr, LIBC_VERSION, __AVR_LIBC_VERSION_STRING__);
/* End version print */
// Enable interupts
sei();
}
fprintf_P(stdout, STUD_NAME);
fputc('\n', stdout); /* Add a new line to the uart printout */
lcd_puts_P(STUD_NAME);
lcd_goto(0x40); /* Got to the beginning of the next line */
static inline void start_ui (void)
{
// Print program and libc versions
fprintf_P(stderr, PSTR(PROG_VERSION "\n"),
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
fprintf_P(stderr, PSTR(LIBC_VERSION "\n"), PSTR(__AVR_LIBC_VERSION_STRING__));
/* ASCII table print */
// print student name
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] = {0};
unsigned char ascii[128];
for (unsigned char i = 0; i < sizeof(ascii); i++) {
ascii[i] = i;
}
print_for_human(stdout, ascii, sizeof(ascii));
while (1) {
/* set pin 3 high to turn led on */
PORTA |= _BV(PORTA3);
_delay_ms(BLINK_DELAY_MS);
// Bootstrap search_month message
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
}
/* Month search and print */
char month_first_leter;
fprintf_P(stdout, GET_MONTH_MSG);
fscanf(stdin, "%c", &month_first_leter);
fprintf(stdout, "%c\n", month_first_leter);
for (int i = 0; i < 6; i++) {
if (!strncmp_P(&month_first_leter, ENG_MONTH[i], 1)) {
fprintf_P(stdout, ENG_MONTH[i]);
fputc('\n', stdout);
lcd_puts_P(ENG_MONTH[i]);
lcd_putc(' ');
}
static inline void search_month (void)
{
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(' ');
}
lcd_puts_P(PSTR(" ")); /* Clear the end of the line */
lcd_goto(0x40); /* Got to the beginning of the next line */
}
// this is fine because even when the hd44780 address counter goes over 0xf4
// we still have quite a few addresses left until address counter overflow
// and we also dont care about the data that is at the end of the ddram
lcd_puts_P(PSTR(" ")); // Clear the end of the line
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
}
/* set pin 3 low to turn led off */
PORTA &= ~_BV(PORTA3);
_delay_ms(BLINK_DELAY_MS);
static inline void heartbeat (void)
{
static time_t time_prev;
time_t time_cur = time(NULL);
if (time_cur <= time_prev) {
return;
}
time_prev = time_cur;
fprintf_P(stderr, PSTR(UPTIME_MSG "\n"), time_cur);
LED_TOGGLE;
}
int main (void)
{
init_hw();
start_ui();
while (1) {
heartbeat();
if (uart0_available()) {
search_month();
}
}
}
// System clock
ISR(TIMER5_COMPA_vect)
{
system_tick();
}

View File

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

View File

@ -1,72 +0,0 @@
#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,17 +0,0 @@
#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_H_ */

37
src/uart_wrap.c Normal file
View File

@ -0,0 +1,37 @@
#include <avr/io.h>
#include <stdio.h>
#include "../lib/andygock_avr-uart/uart.h"
int uart0_putc_wrap(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart0_putc_wrap('\r', stream);
}
uart0_putc(c);
return 0;
}
int uart0_getc_wrap(FILE *stream)
{
(void) stream;
// Probabbly should add some error checking in here but because
// this function is only called out when there is at least one character
// available in the input buffer (see main.c line 114) then error checking
// is not currently necessary.
return (char)uart0_getc();
}
int uart3_putc_wrap(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart3_putc_wrap('\r', stream);
}
uart3_putc(c);
return 0;
}

15
src/uart_wrap.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef _UART_WRAP_H_
#define _UART_WRAP_H_
int uart0_putc_wrap(char c, FILE *stream);
int uart0_getc_wrap(FILE *stream);
int uart3_putc_wrap(char c, FILE *stream);
/* http://www.ermicro.com/blog/?p=325 */
FILE uart0_io = FDEV_SETUP_STREAM(uart0_putc_wrap, uart0_getc_wrap, _FDEV_SETUP_RW);
FILE uart3_out = FDEV_SETUP_STREAM(uart3_putc_wrap, NULL, _FDEV_SETUP_WRITE);
#endif /* _UART_WRAP_H_ */

View File

@ -18,10 +18,8 @@ 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 \