Compare commits
28 Commits
lab03.2-al
...
lab04
Author | SHA1 | Date | |
---|---|---|---|
04031523d4 | |||
f3676e9408 | |||
256059d358 | |||
55ed13b805 | |||
861f0c57d8 | |||
30bd26d471 | |||
22461eaff4 | |||
6635f07e8d | |||
39a457b101 | |||
079a8ff367 | |||
9e46b92744 | |||
500c36b81b | |||
9d5fff9fa0 | |||
f72a7b33e0 | |||
1fad6de2e1 | |||
14a6e4ff91 | |||
281d4d72c5 | |||
89b7339775 | |||
adc3ad6a4a | |||
f4c572a2e6 | |||
25b1feb71d | |||
39720c676e | |||
a836b3a624 | |||
51fc127b48 | |||
be366ed50e | |||
8b64aca736 | |||
aa4e954f76 | |||
275cf9fa28 |
7
Makefile
7
Makefile
@ -90,6 +90,13 @@ 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:
|
||||
|
41
README.md
Normal file
41
README.md
Normal file
@ -0,0 +1,41 @@
|
||||
# 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!
|
BIN
doc/arduino-mega-lcd1602-keypad-shield-placement.png
Normal file
BIN
doc/arduino-mega-lcd1602-keypad-shield-placement.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 159 KiB |
@ -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.
|
||||
|
||||

|
||||
|
||||
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
|
||||
|
||||

|
||||

|
||||
|
||||
Author: [Lauri Võsandi](http://lauri.võsandi.com/arduino/lcd1602-key-shield.html#hd44780)
|
||||
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
@ -6,12 +6,12 @@ This wiring schema uses only Tx from Arduino and is suitable to be used as stand
|
||||
|
||||
## Wiring illustration
|
||||
|
||||

|
||||

|
||||
|
||||
## 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 |
|
||||
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
11
lib/andygock_avr-uart/LICENSE.txt
Executable file
11
lib/andygock_avr-uart/LICENSE.txt
Executable 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
56
lib/andygock_avr-uart/README.md
Executable 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
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
415
lib/andygock_avr-uart/uart.h
Executable 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
|
||||
|
0
lib/hd44780_111/hd44780.h
Executable file → Normal file
0
lib/hd44780_111/hd44780.h
Executable file → Normal file
0
lib/hd44780_111/hd44780.txt
Executable file → Normal file
0
lib/hd44780_111/hd44780.txt
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings_example.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings_example.h
Executable file → Normal file
@ -1,26 +1,19 @@
|
||||
#include <avr/pgmspace.h>
|
||||
#ifndef _HMI_MSG_H_
|
||||
#define _HMI_MSG_H_
|
||||
|
||||
#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 PROG_VERSION "Version: %S built on: %S %S"
|
||||
#define LIBC_VERSION "avr-libc version: %S"
|
||||
#define STUD_NAME "Arti Zirk"
|
||||
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";
|
||||
#define GET_MONTH_MSG "Enter Month name first letter >"
|
||||
#define UPTIME_MSG "Uptime: %lu s"
|
||||
|
||||
PGM_P const months_table[] PROGMEM =
|
||||
{
|
||||
string_1,
|
||||
string_2,
|
||||
string_3,
|
||||
string_4,
|
||||
string_5,
|
||||
string_6
|
||||
};
|
||||
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_ */
|
||||
|
140
src/main.c
140
src/main.c
@ -1,66 +1,128 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.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)
|
||||
{
|
||||
/* 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();
|
||||
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;
|
||||
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};
|
||||
|
||||
// LCD init
|
||||
lcd_init();
|
||||
lcd_clrscr();
|
||||
|
||||
// Enable interupts
|
||||
sei();
|
||||
}
|
||||
|
||||
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__));
|
||||
|
||||
// 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];
|
||||
for (unsigned char i = 0; i < sizeof(ascii); i++) {
|
||||
ascii[i] = i;
|
||||
}
|
||||
|
||||
print_for_human(stdout, ascii, sizeof(ascii));
|
||||
|
||||
while (1) {
|
||||
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);
|
||||
// Bootstrap search_month message
|
||||
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
|
||||
}
|
||||
|
||||
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(&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]));
|
||||
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(' ');
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
lcd_putc(' ');
|
||||
}
|
||||
// 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 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);
|
||||
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();
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include "print_helper.h"
|
||||
|
||||
int print_ascii_tbl (FILE *stream)
|
||||
@ -16,16 +17,17 @@ 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++) {
|
||||
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");
|
||||
}
|
||||
|
72
src/uart.c
72
src/uart.c
@ -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;
|
||||
}
|
17
src/uart.h
17
src/uart.h
@ -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
37
src/uart_wrap.c
Normal 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
15
src/uart_wrap.h
Normal 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_ */
|
@ -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 \
|
||||
|
Reference in New Issue
Block a user