4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
32 * \brief pocketBus protocol Command layer implementation.
34 * This module implements command layer over pocketBus
36 * Payload packets received by pocketBus are first checked for
38 * If a packet is addressed to us we look for a suitable
39 * callback function to call.
41 * The received payload format is as follows:
43 * +----------------------------------------+
45 * +----------------------------------------+
50 * The CMD ID used is the same supplied by the master when
51 * the command was sent.
55 * \author Francesco Sacchi <batt@develer.com>
58 #include "pocketcmd.h"
59 #include "pocketbus.h"
61 #include <cfg/macros.h>
62 #include <cfg/debug.h>
63 #include <cfg/module.h>
65 #include <drv/timer.h>
67 #include <cpu/byteorder.h>
68 #include <cpu/detect.h>
73 * pocketBus Command poll function.
74 * Call it to read and process pocketBus commands.
76 void pocketcmd_poll(struct PocketCmdCtx *ctx)
80 /* Try to read a packet from pocketBus */
81 while (pocketbus_recv(ctx->bus_ctx, &msg))
84 if (msg.addr == ctx->addr ||
85 msg.addr == POCKETBUS_BROADCAST_ADDR)
89 const PocketCmdHdr *hdr = (const PocketCmdHdr *)msg.payload;
92 #warning Fix alignment problem..
94 * The code below make one memcopy, this the only way to
95 * solve alignment problem on ARM. If you are use other
96 * architecture you should find other way to optimize
101 memcpy(&hd, msg.payload, sizeof(PocketCmdHdr));
102 const PocketCmdHdr *hdr = &hd;
105 pocketcmd_t cmd = be16_to_cpu(hdr->cmd);
107 /* We're no longer waiting for a reply (in case we were) */
108 if (cmd == ctx->waiting)
109 ctx->waiting = PKTCMD_NULL;
111 /* Check for command callback */
112 pocketcmd_hook_t callback = ctx->search(cmd);
114 /* Call it if exists */
117 PocketCmdMsg cmd_msg;
119 cmd_msg.cmd_ctx = ctx;
121 cmd_msg.len = msg.len - sizeof(PocketCmdHdr);
122 cmd_msg.buf = msg.payload + sizeof(PocketCmdHdr);
131 * Send command \a cmd to/from slave adding \a len arguments in \a buf.
132 * Address used is contained in \a ctx->addr .
133 * If we are master and the message has a reply, you must set \a wait_reply to true.
134 * \return true if all is ok, false if we are already waiting a replay from another slave.
136 bool pocketcmd_send(struct PocketCmdCtx *ctx, pocketcmd_t cmd, const void *buf, size_t len, bool wait_reply)
138 /* Check if we are waiting a reply from someone */
139 if (ctx->waiting != PKTCMD_NULL)
141 /* Check is reply timeout is elapsed */
142 if (timer_clock() - ctx->reply_timer < ms_to_ticks(PKTCMD_REPLY_TIMEOUT))
144 TRACEMSG("Pkt discard! waiting cmd[%04X]\n", ctx->waiting);
149 TRACEMSG("Timeout waiting cmd[%04X]\n", ctx->waiting);
150 ctx->waiting = PKTCMD_NULL;
155 cmd = cpu_to_be16(cmd);
158 pocketbus_begin(ctx->bus_ctx, ctx->addr);
159 pocketbus_write(ctx->bus_ctx, &cmd, sizeof(cmd));
160 pocketbus_write(ctx->bus_ctx, buf, len);
161 pocketbus_end(ctx->bus_ctx);
166 ctx->reply_timer = timer_clock();
172 * Init pocketBus command layer.
173 * \a ctx is pocketBus command layer context.
174 * \a bus_ctx is pocketBus context.
175 * \a addr is slave address (see pocketcmd_setAddr for details.)
176 * \a search is the lookup function used to search command ID callbacks.
178 void pocketcmd_init(struct PocketCmdCtx *ctx, struct PocketBusCtx *bus_ctx, pocketbus_addr_t addr, pocketcmd_lookup_t search)
185 memset(ctx, 0, sizeof(*ctx));
186 ctx->bus_ctx = bus_ctx;
187 ctx->search = search;
188 pocketcmd_setAddr(ctx, addr);
192 * Helper function used to reply to master with an ACK.
194 void pocketcmd_replyAck(struct PocketCmdMsg *msg)
196 uint8_t ack[] = { POCKETBUS_ACK };
198 pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, ack, sizeof(ack));
202 * Helper function used to reply to master with a NAK.
204 void pocketcmd_replyNak(struct PocketCmdMsg *msg)
206 uint8_t nak[] = { POCKETBUS_NAK };
208 pocketcmd_slaveReply(msg->cmd_ctx, msg->cmd, nak, sizeof(nak));