src/hello.c
author Tero Marttila <terom@paivola.fi>
Fri, 04 Apr 2014 00:26:16 +0300
changeset 55 04c625712e35
parent 54 ec42f36d8614
child 56 3b837eaf1b6d
permissions -rw-r--r--
hello: trivial spi bits
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     1
#include <avr/io.h>
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
     2
52
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     3
#include "stdlib.h"
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     4
#include "timer.c" // XXX
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
     5
#include "serial.c" // XXX
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
     6
#include "spi.c" // XXX
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
     7
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
     8
static enum state {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
     9
    START       = '\n',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    10
    CMD_SET     = 's',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    11
    ARG_CHAR    = '0',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    12
    ERROR       = '!',
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    13
} state = START, cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    14
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    15
static enum {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    16
    CMD_LED_1   = 0,
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    17
} cmd_led = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    18
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    19
static char arg_char;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    20
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    21
static unsigned char led_state = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    22
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    23
int command (enum state cmd)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    24
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    25
    switch (cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    26
        case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    27
            led_state = arg_char;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    28
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    29
            return 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    30
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    31
        default:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    32
            return 1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    33
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    34
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    35
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    36
char input (char c)
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    37
{
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    38
    if (c == '\r') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    39
        char ret = '?';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    40
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    41
        // command state
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    42
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    43
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    44
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    45
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    46
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    47
            case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    48
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    49
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    50
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    51
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    52
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    53
            case ERROR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    54
                cmd = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    55
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    56
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    57
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    58
        // command
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    59
        if (!cmd) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    60
            ret = ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    61
        } else if (command(cmd)) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    62
            ret = '!';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    63
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    64
            ret = '\n';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    65
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    66
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    67
        // return to START with response
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    68
        state = START;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    69
        return ret;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    70
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    71
    } else if (31 < c && c < 128) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    72
        // process input char
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    73
        switch (state) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    74
            case START:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    75
                if (c == 's') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    76
                    cmd = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    77
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    78
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    79
                    return state;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    80
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    81
                } else if (c == 'S') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    82
                    cmd = state = CMD_SET;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    83
                    arg_char = 255;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    84
                    return state;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    85
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    86
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    87
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    88
            /* 
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    89
            case CMD_SET:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    90
                if (c == '1') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    91
                    cmd_led = CMD_LED_1;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    92
                    state = ARG_CHAR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    93
                    arg_char = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    94
                } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    95
                    state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    96
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    97
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    98
                break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
    99
            */
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   100
            case ARG_CHAR:
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   101
                if (c >= '0' && c <= '9') {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   102
                    arg_char *= 10;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   103
                    arg_char += (c - '0');
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   104
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   105
                    return c;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   106
                }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   107
            break;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   108
        }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   109
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   110
        // reject
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   111
        state = ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   112
        return ERROR;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   113
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   114
    } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   115
        // ignore
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   116
        return ' ';
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   117
    }
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   118
}
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   119
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   120
#define LED7_DDR    DDRB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   121
#define LED7_PORT   PORTB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   122
#define LED7_OE     PORTB1
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   123
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   124
// common anode?
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   125
#define LED7_ANODE  1
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   126
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   127
static const uint8_t LED7SEG_FONT[] = {
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   128
    0b00111111,	// 0
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   129
    0b00000110,	// 1
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   130
    0b01011011,	// 2
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   131
    0b01001111,	// 3
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   132
    0b01100110,	// 4
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   133
    0b01101101,	// 5
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   134
    0b01111101,	// 6
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   135
    0b00000111,	// 7
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   136
    0b01111111,	// 8
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   137
    0b01100111,	// 9
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   138
    0b01110111,	// A
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   139
    0b01111100,	// B
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   140
    0b00111001,	// C
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   141
    0b01011110,	// D
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   142
    0b01111001,	// E
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   143
    0b01110001,	// F
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   144
};
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   145
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   146
enum {
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   147
    LED7SEG_DOT     = 0b10000000,
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   148
};
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   149
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   150
void led7_init (void)
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   151
{
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   152
    sbi(&LED7_PORT, LED7_OE); // disabled (low)
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   153
    sbi(&LED7_DDR, LED7_OE); // out
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   154
}
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   155
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   156
/*
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   157
 * Update external IO.
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   158
 */
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   159
void led7_update (void)
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   160
{
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   161
    static char i = 0;
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   162
    
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   163
    if (i) {
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   164
        // enable display
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   165
        cbi(&LED7_PORT, LED7_OE);
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   166
    }
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   167
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   168
    char led7 = LED7SEG_FONT[i % sizeof(LED7SEG_FONT)];
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   169
    
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   170
    if (LED7_ANODE)
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   171
        led7 = ~led7;
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   172
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   173
    spi_tx[0] = led7;
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   174
 
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   175
    i++;
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   176
}
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   177
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   178
#define DEBUG_DDR   DDRB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   179
#define DEBUG_PORT  PORTB
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   180
#define DEBUG_LED   0
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   181
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   182
int main (void)
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   183
{
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   184
    led7_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   185
    timer_init();
52
237d1f5c1c32 hello: split out timer; add serial
Tero Marttila <terom@paivola.fi>
parents: 50
diff changeset
   186
    serial_init();
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   187
    spi_init();
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   188
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   189
    // led_init();
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   190
    sbi(&DEBUG_DDR, DEBUG_LED);
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   191
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   192
    sei();
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   193
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   194
    // start
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   195
    char c;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   196
    unsigned short timeout = 0;
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   197
        
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   198
    serial_write(state);
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   199
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   200
    while (true) {
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   201
        // TODO: SPI timer
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   202
        led7_update();
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   203
        spi_update();
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   204
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   205
        // toggle
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   206
        if (led_state == 0) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   207
            timeout = 0;
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   208
            cbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   209
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   210
        } else if (led_state == 255) {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   211
            timeout = 0;
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   212
            sbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   213
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   214
        } else {
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   215
            // as Hz
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   216
            timeout = (16000 / led_state);
55
04c625712e35 hello: trivial spi bits
Tero Marttila <terom@paivola.fi>
parents: 54
diff changeset
   217
            xbi(&DEBUG_PORT, DEBUG_LED);
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   218
        }
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   219
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   220
        // sleep
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   221
        if (timer_sleep(timeout)) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   222
            c = '.';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   223
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   224
        } else if ((c = serial_read())) {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   225
            // got serial data
54
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   226
            c = input(c);
ec42f36d8614 hello: interactive console
Tero Marttila <terom@paivola.fi>
parents: 53
diff changeset
   227
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   228
        } else {
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   229
            c = '?';
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   230
        }
49
f01fb659e54d hello: timer-based sleeps
Tero Marttila <terom@paivola.fi>
parents: 47
diff changeset
   231
        
53
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   232
        // output...
dfe67409fbcd hello: interruptable timer sleep, with buffered serial IO
Tero Marttila <terom@paivola.fi>
parents: 52
diff changeset
   233
        serial_write(c);
47
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   234
    }
7f930a94ee1e bravely step into the modern world of C-programming, using avr-gcc for a hello world...
Tero Marttila <terom@paivola.fi>
parents:
diff changeset
   235
}