summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib/dev/avrisp/avrisp_frame.cc
blob: 04a1f1a5943fc27456acb1f6c60770113782522c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// ucoolib - Microcontroller object oriented library. {{{
//
// Copyright (C) 2013 Nicolas Schodet
//
// APBTeam:
//        Web: http://apbteam.org/
//      Email: team AT apbteam DOT org
//
// 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
// (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, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// }}}
#include "avrisp_frame.hh"

namespace ucoo {

static const uint8_t avrisp_frame_proto_start = 27;
static const uint8_t avrisp_frame_proto_token = 14;
static const uint8_t avrisp_frame_proto_answer_cksum_error = 0xb0;
static const uint8_t avrisp_frame_proto_status_cksum_error = 0xc1;

AvrIspFrame::AvrIspFrame (AvrIspProto &proto)
    : proto_ (proto), state_ (AVRISP_FRAME_STATE_START), buffer_send_index_ (-1)
{
}

void
AvrIspFrame::read_and_write (Stream &stream)
{
    // Design is inherited from byte based C interface.
    while (1)
    {
        if (buffer_send_index_ == -1)
        {
            // Read.
            int c = stream.getc ();
            if (c == -1)
                return;
            else
                accept_char (c);
        }
        else
        {
            // Write.
            int r = stream.write (reinterpret_cast<char *> (buffer_)
                                  + buffer_send_index_,
                                  buffer_len_ - buffer_send_index_);
            if (r <= 0)
                return;
            else
                buffer_send_index_ += r;
            if (buffer_send_index_ == buffer_len_)
                buffer_send_index_ = -1;
        }
    }
}

inline AvrIspFrameState
operator++ (AvrIspFrameState& state, int)
{
   const int i = static_cast<int> (state);
   state = static_cast<AvrIspFrameState> (i + 1);
   return state;
}

void
AvrIspFrame::accept_char (uint8_t c)
{
    switch (state_)
    {
    case AVRISP_FRAME_STATE_START:
        if (c == avrisp_frame_proto_start)
        {
            cksum_ = avrisp_frame_proto_start;
            state_++;
        }
        break;
    case AVRISP_FRAME_STATE_WAIT_SEQ:
        cksum_ ^= c;
        seq_ = c;
        state_++;
        break;
    case AVRISP_FRAME_STATE_WAIT_LEN_MSB:
        cksum_ ^= c;
        len_ = c << 8;
        state_++;
        break;
    case AVRISP_FRAME_STATE_WAIT_LEN_LSB:
        cksum_ ^= c;
        len_ |= c;
        buffer_len_ = 0;
        if (len_ == 0
            || len_ > (UCOO_CONFIG_DEV_AVRISP_FRAME_BUFFER_SIZE - proto_head_
                       - proto_tail_))
            state_ = AVRISP_FRAME_STATE_START;
        else
            state_++;
        break;
    case AVRISP_FRAME_STATE_WAIT_TOKEN:
        if (c == avrisp_frame_proto_token)
        {
            cksum_ ^= c;
            state_++;
        }
        else
            state_ = AVRISP_FRAME_STATE_START;
        break;
    case AVRISP_FRAME_STATE_DATA:
        cksum_ ^= c;
        buffer_[proto_head_ + buffer_len_++] = c;
        if (buffer_len_ == len_)
            state_++;
        break;
    case AVRISP_FRAME_STATE_WAIT_CKSUM:
        cksum_ ^= c;
        state_ = AVRISP_FRAME_STATE_START;
        process ();
        break;
    }
}

void
AvrIspFrame::send_frame (int len)
{
    int i = 0;
    uint8_t cksum;
    buffer_[i++] = avrisp_frame_proto_start;
    cksum = avrisp_frame_proto_start;
    buffer_[i++] = seq_;
    cksum ^= seq_;
    buffer_[i++] = len >> 8;
    cksum ^= len >> 8;
    buffer_[i++] = len & 0xff;
    cksum ^= len & 0xff;
    buffer_[i++] = avrisp_frame_proto_token;
    cksum ^= avrisp_frame_proto_token;
    for (; i < proto_head_ + len; i++)
        cksum ^= buffer_[i];
    buffer_[i] = cksum;
    buffer_len_ = proto_head_ + len + proto_tail_;
    buffer_send_index_ = 0;
}

void
AvrIspFrame::process ()
{
    if (cksum_ != 0)
    {
        /* Bad checksum. */
        buffer_[proto_head_] = avrisp_frame_proto_answer_cksum_error;
        buffer_[proto_head_ + 1] = avrisp_frame_proto_status_cksum_error;
        send_frame (2);
    }
    else
    {
        int r = proto_.accept (buffer_ + proto_head_, buffer_len_);
        if (r)
            send_frame (r);
    }
}

} // namespace ucoo