Permalink
Browse files

Simplify handling of serial protocol

Pass in the bytes directly to the function rather than using atoms.
Maybe there should be a map between atoms and symbols, so the caller
can pass in the name of the message type.
  • Loading branch information...
1 parent db8b30f commit 0e5af3b21a194b86ca1a84feedb5fea0d6774b66 @msantos committed Mar 7, 2012
Showing with 23 additions and 69 deletions.
  1. +23 −69 src/stk500.erl
View
@@ -45,7 +45,8 @@
hex_file/1, hex_file/2,
chunk/2,
- load/2, load/3
+ load/2, load/3,
+ cmd/2, cmd/3
]).
-define(DEV, "/dev/ttyUSB0").
@@ -196,99 +197,52 @@ load(FD, Bytes) ->
load(FD, Bytes, Opt) ->
reset(FD),
- ok = cmd(FD, enter_progmode),
+ ok = cmd(FD, <<?Cmnd_STK_ENTER_PROGMODE, ?Sync_CRC_EOP>>, Opt),
lists:foldl(
fun(Buf, Address) ->
- ok = cmd(FD, load_address, [{address, Address}] ++ Opt),
- ok = cmd(FD, prog_page, [{buf, Buf}] ++ Opt),
+ ok = cmd(FD, <<?Cmnd_STK_LOAD_ADDRESS,
+ Address:2/little-unsigned-integer-unit:8,
+ ?Sync_CRC_EOP>>, Opt),
+
+ ok = cmd(FD, <<?Cmnd_STK_PROG_PAGE,
+ (byte_size(Buf)):2/big-unsigned-integer-unit:8,
+ $F,
+ Buf/bytes,
+ ?Sync_CRC_EOP>>, Opt),
Address + byte_size(Buf) div 2
end,
0,
Bytes),
- cmd(FD, leave_progmode).
+ cmd(FD, <<?Cmnd_STK_LEAVE_PROGMODE, ?Sync_CRC_EOP>>, Opt).
cmd(FD, Cmd) ->
cmd(FD, Cmd, []).
-cmd(FD, enter_progmode, _Opt) ->
+cmd(FD, Cmd, Opt) ->
- Cmd = <<?Cmnd_STK_ENTER_PROGMODE, ?Sync_CRC_EOP>>,
-
- ok = serctl:write(FD, Cmd),
-
- case readx(FD, 2) of
- {ok, <<?Resp_STK_INSYNC, ?Resp_STK_OK>>} ->
- ok;
- {ok, Resp} ->
- {protocol_error, enter_progmode, Resp};
- {error, Error} ->
- {serial_error, enter_progmode, Error}
- end;
-
-% No more data to write, exit programming mode
-cmd(FD, leave_progmode, _Opt) ->
-
- Cmd = <<?Cmnd_STK_LEAVE_PROGMODE, ?Sync_CRC_EOP>>,
-
- ok = serctl:write(FD, Cmd),
-
- case readx(FD, 2) of
- {ok, <<?Resp_STK_INSYNC, ?Resp_STK_OK>>} ->
- ok;
- {ok, Resp} ->
- {protocol_error, leave_progmode, Resp};
- {error, Error} ->
- {serial_error, leave_progmode, Error}
- end;
-
-cmd(FD, load_address, Opt) ->
-
- Address = proplists:get_value(address, Opt, 0),
Verbose = proplists:get_value(verbose, Opt, false),
- Cmd = <<?Cmnd_STK_LOAD_ADDRESS,
- Address:2/little-unsigned-integer-unit:8,
- ?Sync_CRC_EOP
- >>,
-
- ok = serctl:write(FD, Cmd),
+ verbose(Verbose, [{cmd, Cmd}]),
- verbose(Verbose, [{address, Address}, {cmd, Cmd} ]),
-
- case readx(FD, 2) of
- {ok, <<?Resp_STK_INSYNC, ?Resp_STK_OK>>} ->
- ok;
- {ok, Resp} ->
- {protocol_error, load_address, Resp};
+ case serctl:write(FD, Cmd) of
+ ok ->
+ cmd_1(FD, Cmd, Opt);
{error, Error} ->
- {serial_error, load_address, Error}
- end;
-
-cmd(FD, prog_page, Opt) ->
-
- Buf = proplists:get_value(buf, Opt, <<>>),
- Verbose = proplists:get_value(verbose, Opt, false),
-
- Cmd = <<?Cmnd_STK_PROG_PAGE,
- (byte_size(Buf)):2/big-unsigned-integer-unit:8,
- $F,
- Buf/bytes,
- ?Sync_CRC_EOP>>,
-
- verbose(Verbose, [{cmd, Cmd}, {data, Buf}]),
+ {serial_error, Cmd, Error}
+ end.
- ok = serctl:write(FD, Cmd),
+cmd_1(FD, Cmd, _Opt) ->
case readx(FD, 2) of
{ok, <<?Resp_STK_INSYNC, ?Resp_STK_OK>>} ->
ok;
{ok, Resp} ->
- {protocol_error, prog_page, Resp};
+ {protocol_error, Cmd, Resp};
{error, Error} ->
- {serial_error, prog_page, Error}
+ {serial_error, Cmd, Error}
end.

0 comments on commit 0e5af3b

Please sign in to comment.