arduino_nano/main.c

256 lines
6.4 KiB
C

/* 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 3 of the License, or
* (at your option) 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h> // Standard Integer Types
#include <stdio.h> // Standard IO facilities
#include <stdlib.h> // General utilities
#include <stdbool.h> // Boolean
#include <string.h> // Strings
#include <avr/io.h> // AVR device-specific IO definitions
#include <util/delay.h> // Convenience functions for busy-wait delay loops
#include <avr/interrupt.h> // Interrupts
#include <avr/wdt.h> // Watchdog timer handling
#include <avr/pgmspace.h> // Program Space Utilities
#include <avr/sleep.h> // Power Management and Sleep Modes
#include "uart.h" // basic UART functions
#include "main.h" // main definitions
/* help strings */
const char help_00[] PROGMEM = "commands:\n";
const char help_01[] PROGMEM = "\thelp display this help\n";
const char help_02[] PROGMEM = "\tled on|off|toggle change LED state\n";
const char help_03[] PROGMEM = "\tread [B-D][0-7] read value from pin\n";
const char help_04[] PROGMEM = "\twrite [B-D][0-7] 0|1 write value on pin\n";
PGM_P const help_table[] PROGMEM = {
help_00,
help_01,
help_02,
help_03,
help_04
};
/* global variables */
char uart_in[32] = {0}; // user input from USART
volatile uint8_t uart_in_i = 0; // how many UART characters have been input
/* flags, set in the interrupts and handled in the main program */
volatile bool uart_flag = false; // UART input data is available
/* switch off LED */
void led_off(void)
{
LED_PORT &= ~(1<<LED_IO); // remove power to LED
}
/* switch on LED */
void led_on(void)
{
LED_PORT |= (1<<LED_IO); // provide power to LED
}
/* toggle LED */
void led_toggle(void)
{
LED_PIN |= (1<<LED_IO);
}
void help(void)
{
char* str;
for (uint8_t i=0; i<sizeof(help_table)/sizeof(PGM_P); i++) { /* display all help lines */
str = malloc(strlen_PF((uint_farptr_t)pgm_read_word(&(help_table[i]))));
strcpy_PF(str, (uint_farptr_t)pgm_read_word(&(help_table[i])));
printf(str);
free(str);
}
}
/* save the UART input into a string */
ISR(USART_RX_vect) { /* UART receive interrupt */
uart_in[uart_in_i] = getchar(); // save input
if (uart_in_i<sizeof(uart_in)-1) { // go to next if there is space left (else overwrite last)
uart_in_i++; // prepare for next character
uart_in[uart_in_i] = 0; // always end the string
}
uart_flag = true; // notify new character is there
}
/* disable watchdog when booting */
void wdt_init(void) __attribute__((naked)) __attribute__((section(".init3")));
void wdt_init(void)
{
MCUSR = 0;
wdt_disable();
return;
}
/* initialize GPIO */
void io_init(void)
{
/* use UART as terminal */
uart_init();
stdout = &uart_output;
stdin = &uart_input;
/* gpio */
/* LED */
LED_DDR |= (1<<LED_IO); // LED is driven by pin (set as output)
led_off();
sei(); /* enable interrupts */
}
/* process command */
void command_action()
{
/* split command */
const char* delimiter = " ";
char* word = strtok(uart_in,delimiter);
if (!word) {
goto error;
}
/* parse command */
if (0==strcmp(word,"help")) {
help();
} else if (0==strcmp(word,"led")) {
/* expecting state */
word = strtok(NULL,delimiter);
if (!word) {
goto error;
}
if (0==strcmp(word,"on")) {
led_on();
} else if (0==strcmp(word,"off")) {
led_off();
} else if (0==strcmp(word,"toggle")) {
led_toggle();
} else {
goto error;
}
} else if (0==strcmp(word,"read")) {
/* expecting pin */
word = strtok(NULL,delimiter);
if (!word) {
goto error;
}
if (2==strlen(word) && word[0]>='B' && word[0]<='D' && word[1]>='0' && word[1]<='7') {
volatile uint8_t * pin = NULL;
volatile uint8_t * ddr = NULL;
switch (word[0]) {
case 'B':
pin = &PINB;
ddr = &DDRB;
break;
case 'C':
pin = &PINC;
ddr = &DDRC;
break;
case 'D':
pin = &PIND;
ddr = &DDRD;
break;
}
*ddr &= ~(1<<(word[1]-'0')); // set as input
printf("%s: %u\n", word, (*pin>>(word[1]-'0'))&0x1); // print value
} else {
goto error;
}
} else if (0==strcmp(word,"write")) {
/* expecting pin */
word = strtok(NULL,delimiter);
if (!word) {
goto error;
}
if (2==strlen(word) && word[0]>='B' && word[0]<='D' && word[1]>='0' && word[1]<='7') {
uint8_t pin = word[1]-'0';
volatile uint8_t * port = NULL;
volatile uint8_t * ddr = NULL;
switch (word[0]) {
case 'B':
port = &PORTB;
ddr = &DDRB;
break;
case 'C':
port = &PORTC;
ddr = &DDRC;
break;
case 'D':
port = &PORTD;
ddr = &DDRD;
break;
}
*ddr |= (1<<pin); // set as output
/* expecting value */
word = strtok(NULL,delimiter);
if (!word || strlen(word)!=1) {
goto error;
}
/* write value */
if ('0'==word[0]) {
*port &= ~(1<<pin);
} else if ('1'==word[0]) {
*port |= (1<<pin);
} else {
goto error;
}
} else {
goto error;
}
} else {
goto error;
}
return;
error:
printf(PSTR("command not recognized\n"));
return;
}
int main(void)
{
uint8_t uart_out_i = 0; // how many UART input characters have been processed
io_init(); // initialize IOs
printf(PSTR("welcome to the demo code\n"));
while (true) { // endless loop for micro-controller
while (uart_flag) { // new UART input
uart_flag = false;
while (uart_out_i<uart_in_i) {
putchar(uart_in[uart_out_i++]); // print current character
}
if ('\n'==uart_in[uart_in_i-1] || '\r'==uart_in[uart_in_i-1]) { // end of line detected
if ('\r'==uart_in[uart_in_i-1]) { // be sure to print a newline (for windows and mac EOL)
putchar('\n');
}
uart_in[uart_in_i-1] = 0; // remove end of line
if (uart_in_i>1) { // ignore empty lines
command_action(); // process command
}
uart_out_i = 0; // reset output
uart_in_i = 0; // reset input
}
}
/* go to sleep and wait for next interrupt */
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
return 0;
}