mirror of
https://github.com/Proxmark/proxmark3.git
synced 2024-09-20 14:56:21 +08:00
usb communication (device side) refactoring
* merge cmd.c into usb_cdc.c * move back usb_cdc.[ch] to common/ * declare low level functions usb_read() and usb_write() and more functions as static * use cmd_receive() in bootrom.c and appmain.c * remove unused memory wasting csrTab[100] in usb_cdc.c * replace more byte_t by uint8_t * more whitespace fixes
This commit is contained in:
parent
72622d6429
commit
867e10a5fd
|
@ -50,8 +50,7 @@ THUMBSRC = start.c \
|
|||
printf.c \
|
||||
util.c \
|
||||
string.c \
|
||||
usb_cdc.c \
|
||||
cmd.c
|
||||
usb_cdc.c
|
||||
|
||||
# Compile these in thumb mode optimized for speed (still smaller than ARM mode)
|
||||
THUMBOPTSRC = $(SRC_ISO15693)
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
#include <stdarg.h>
|
||||
|
||||
#include "usb_cdc.h"
|
||||
#include "cmd.h"
|
||||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "fpga.h"
|
||||
|
@ -936,9 +935,7 @@ void ListenReaderField(int limit) {
|
|||
}
|
||||
|
||||
|
||||
void UsbPacketReceived(uint8_t *packet, int len) {
|
||||
|
||||
UsbCommand *c = (UsbCommand *)packet;
|
||||
void UsbPacketReceived(UsbCommand *c) {
|
||||
|
||||
// Dbprintf("received %d bytes, with command: 0x%04x and args: %d %d %d",len,c->cmd,c->arg[0],c->arg[1],c->arg[2]);
|
||||
|
||||
|
@ -1479,10 +1476,13 @@ void __attribute__((noreturn)) AppMain(void) {
|
|||
LCDInit();
|
||||
#endif
|
||||
|
||||
uint8_t rx[sizeof(UsbCommand)];
|
||||
size_t rx_len;
|
||||
|
||||
UsbCommand rx;
|
||||
|
||||
for(;;) {
|
||||
if (cmd_receive(&rx)) {
|
||||
UsbPacketReceived(&rx);
|
||||
}
|
||||
|
||||
WDT_HIT();
|
||||
if (usb_poll() && (rx_len = usb_read(rx, sizeof(rx)))) {
|
||||
UsbPacketReceived(rx, rx_len);
|
||||
|
|
86
armsrc/cmd.c
86
armsrc/cmd.c
|
@ -1,86 +0,0 @@
|
|||
/*
|
||||
* Proxmark send and receive commands
|
||||
*
|
||||
* Copyright (c) 2012, Roel Verdult
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @file cmd.c
|
||||
* @brief
|
||||
*/
|
||||
|
||||
#include "cmd.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "common.h"
|
||||
#include "usb_cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
|
||||
|
||||
bool cmd_receive(UsbCommand* cmd) {
|
||||
|
||||
// Check if there is a usb packet available
|
||||
if (!usb_poll())
|
||||
return false;
|
||||
|
||||
// Try to retrieve the available command frame
|
||||
size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand));
|
||||
|
||||
// Check if the transfer was complete
|
||||
if (rxlen != sizeof(UsbCommand))
|
||||
return false;
|
||||
|
||||
// Received command successfully
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {
|
||||
UsbCommand txcmd;
|
||||
|
||||
for (size_t i = 0; i < sizeof(UsbCommand); i++) {
|
||||
((uint8_t*)&txcmd)[i] = 0x00;
|
||||
}
|
||||
|
||||
// Compose the outgoing command frame
|
||||
txcmd.cmd = cmd;
|
||||
txcmd.arg[0] = arg0;
|
||||
txcmd.arg[1] = arg1;
|
||||
txcmd.arg[2] = arg2;
|
||||
|
||||
// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
|
||||
if (data && len) {
|
||||
len = MIN(len, USB_CMD_DATA_SIZE);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Send frame and make sure all bytes are transmitted
|
||||
if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
44
armsrc/cmd.h
44
armsrc/cmd.h
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Proxmark send and receive commands
|
||||
*
|
||||
* Copyright (c) 2010, Roel Verdult
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @file cmd.h
|
||||
* @brief
|
||||
*/
|
||||
|
||||
#ifndef PROXMARK_CMD_H__
|
||||
#define PROXMARK_CMD_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_cmd.h"
|
||||
|
||||
extern bool cmd_receive(UsbCommand* cmd);
|
||||
extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);
|
||||
|
||||
#endif // PROXMARK_CMD_H__
|
|
@ -15,7 +15,7 @@
|
|||
#include "iso14443a.h"
|
||||
#include "iso14443b.h"
|
||||
#include "epa.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "fpgaloader.h"
|
||||
#include "string.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -14,8 +14,7 @@
|
|||
#include "BigBuf.h"
|
||||
#include "util.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h" // for usb_poll_validate_length
|
||||
#include "usb_cdc.h"
|
||||
#include "fpga.h"
|
||||
#include "fpgaloader.h"
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include "hitag2.h"
|
||||
|
||||
#include "proxmark3.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "apps.h"
|
||||
#include "util.h"
|
||||
#include "hitag.h"
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <stdlib.h>
|
||||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "util.h"
|
||||
#include "hitag.h"
|
||||
#include "string.h"
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include "mifareutil.h" // for MF_DBGLEVEL
|
||||
#include "BigBuf.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
|
||||
#ifdef WITH_SMARTCARD
|
||||
#include "smartcard.h"
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
#include "string.h"
|
||||
#include "printf.h"
|
||||
#include "common.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "iso14443a.h"
|
||||
#include "iso15693.h"
|
||||
// Needed for CRC in emulation mode;
|
||||
|
@ -34,7 +34,6 @@
|
|||
#include "iso15693tools.h"
|
||||
#include "protocols.h"
|
||||
#include "optimized_cipher.h"
|
||||
#include "usb_cdc.h" // for usb_poll_validate_length
|
||||
#include "fpgaloader.h"
|
||||
|
||||
// iCLASS has a slightly different timing compared to ISO15693. According to the picopass data sheet the tag response is expected 330us after
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "util.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "iso14443crc.h"
|
||||
#include "crapto1/crapto1.h"
|
||||
#include "mifareutil.h"
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "util.h"
|
||||
#include "string.h"
|
||||
#include "iso14443crc.h"
|
||||
|
|
|
@ -58,7 +58,7 @@
|
|||
#include "string.h"
|
||||
#include "iso15693tools.h"
|
||||
#include "protocols.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "BigBuf.h"
|
||||
#include "fpgaloader.h"
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "util.h"
|
||||
#include "string.h"
|
||||
#include "legic_prng.h"
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
#include "lfdemod.h"
|
||||
#include "lfsampling.h"
|
||||
#include "protocols.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h" // for usb_poll_validate_length
|
||||
#include "usb_cdc.h"
|
||||
#include "fpgaloader.h"
|
||||
|
||||
/**
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include <stdint.h>
|
||||
|
||||
#include "proxmark3.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "crapto1/crapto1.h"
|
||||
#include "iso14443a.h"
|
||||
#include "BigBuf.h"
|
||||
|
|
|
@ -20,7 +20,6 @@
|
|||
#include "fpgaloader.h"
|
||||
#include "proxmark3.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "cmd.h"
|
||||
#include "protocols.h"
|
||||
#include "apps.h"
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include "crapto1/crapto1.h"
|
||||
#include "mifareutil.h"
|
||||
#include "common.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "BigBuf.h"
|
||||
#include "fpgaloader.h"
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "proxmark3.h"
|
||||
#include "apps.h"
|
||||
#include "cmd.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "lfsampling.h"
|
||||
#include "pcf7931.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
# DO NOT use thumb mode in the phase 1 bootloader since that generates a section with glue code
|
||||
ARMSRC =
|
||||
THUMBSRC = cmd.c usb_cdc.c bootrom.c
|
||||
THUMBSRC = usb_cdc.c bootrom.c
|
||||
ASMSRC = ram-reset.s flash-reset.s
|
||||
VERSIONSRC = version.c
|
||||
|
||||
|
|
|
@ -6,17 +6,15 @@
|
|||
// Main code for the bootloader
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <proxmark3.h>
|
||||
#include "proxmark3.h"
|
||||
#include "usb_cdc.h"
|
||||
#include "cmd.h"
|
||||
//#include "usb_hid.h"
|
||||
|
||||
void DbpString(char *str) {
|
||||
byte_t len = 0;
|
||||
uint8_t len = 0;
|
||||
while (str[len] != 0x00) {
|
||||
len++;
|
||||
}
|
||||
cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len);
|
||||
cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(uint8_t*)str,len);
|
||||
}
|
||||
|
||||
struct common_area common_area __attribute__((section(".commonarea")));
|
||||
|
@ -89,15 +87,10 @@ static void Fatal(void)
|
|||
for(;;);
|
||||
}
|
||||
|
||||
void UsbPacketReceived(uint8_t *packet, int len) {
|
||||
void UsbPacketReceived(UsbCommand *c) {
|
||||
int i, dont_ack=0;
|
||||
UsbCommand* c = (UsbCommand *)packet;
|
||||
volatile uint32_t *p;
|
||||
|
||||
if(len != sizeof(UsbCommand)) {
|
||||
Fatal();
|
||||
}
|
||||
|
||||
uint32_t arg0 = (uint32_t)c->arg[0];
|
||||
|
||||
switch(c->cmd) {
|
||||
|
@ -199,21 +192,17 @@ static void flash_mode(int externally_entered)
|
|||
start_addr = 0;
|
||||
end_addr = 0;
|
||||
bootrom_unlocked = 0;
|
||||
byte_t rx[sizeof(UsbCommand)];
|
||||
size_t rx_len;
|
||||
UsbCommand rx;
|
||||
|
||||
usb_enable();
|
||||
for (volatile size_t i=0; i<0x100000; i++) {};
|
||||
usb_enable();
|
||||
for (volatile size_t i=0; i<0x100000; i++) {};
|
||||
|
||||
for(;;) {
|
||||
WDT_HIT();
|
||||
|
||||
if (usb_poll()) {
|
||||
rx_len = usb_read(rx,sizeof(UsbCommand));
|
||||
if (rx_len) {
|
||||
UsbPacketReceived(rx,rx_len);
|
||||
}
|
||||
}
|
||||
if (cmd_receive(&rx)) {
|
||||
UsbPacketReceived(&rx);
|
||||
}
|
||||
|
||||
if(!externally_entered && !BUTTON_PRESS()) {
|
||||
/* Perform a reset to leave flash mode */
|
||||
|
|
|
@ -207,7 +207,7 @@ static const char StrDescProduct[] = {
|
|||
};
|
||||
|
||||
|
||||
const char* getStringDescriptor(uint8_t idx) {
|
||||
static const char* getStringDescriptor(uint8_t idx) {
|
||||
switch (idx) {
|
||||
case STR_LANGUAGE_CODES:
|
||||
return StrDescLanguageCodes;
|
||||
|
@ -290,7 +290,7 @@ AT91S_CDC_LINE_CODING line = {
|
|||
8}; // 8 Data bits
|
||||
|
||||
|
||||
void AT91F_CDC_Enumerate();
|
||||
static void AT91F_CDC_Enumerate();
|
||||
|
||||
AT91PS_UDP pUdp = AT91C_BASE_UDP;
|
||||
uint8_t btConfiguration = 0;
|
||||
|
@ -350,7 +350,7 @@ void usb_enable() {
|
|||
//* \fn usb_check
|
||||
//* \brief Test if the device is configured and handle enumeration
|
||||
//*----------------------------------------------------------------------------
|
||||
bool usb_check() {
|
||||
static bool usb_check() {
|
||||
AT91_REG isr = pUdp->UDP_ISR;
|
||||
|
||||
if (isr & AT91C_UDP_ENDBUSRES) {
|
||||
|
@ -395,7 +395,7 @@ bool usb_poll_validate_length() {
|
|||
//* \fn usb_read
|
||||
//* \brief Read available data from Endpoint OUT
|
||||
//*----------------------------------------------------------------------------
|
||||
uint32_t usb_read(uint8_t* data, size_t len) {
|
||||
static uint32_t usb_read(uint8_t* data, size_t len) {
|
||||
uint8_t bank = btReceiveBank;
|
||||
uint32_t packetSize, nbBytesRcv = 0;
|
||||
uint32_t time_out = 0;
|
||||
|
@ -427,7 +427,7 @@ uint32_t usb_read(uint8_t* data, size_t len) {
|
|||
//* \fn usb_write
|
||||
//* \brief Send through endpoint 2
|
||||
//*----------------------------------------------------------------------------
|
||||
uint32_t usb_write(const uint8_t* data, const size_t len) {
|
||||
static uint32_t usb_write(const uint8_t* data, const size_t len) {
|
||||
size_t length = len;
|
||||
uint32_t cpt = 0;
|
||||
|
||||
|
@ -476,9 +476,6 @@ uint32_t usb_write(const uint8_t* data, const size_t len) {
|
|||
//* \fn AT91F_USB_SendData
|
||||
//* \brief Send Data through the control endpoint
|
||||
//*----------------------------------------------------------------------------
|
||||
unsigned int csrTab[100] = {0x00};
|
||||
unsigned char csrIdx = 0;
|
||||
|
||||
static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t length) {
|
||||
uint32_t cpt = 0;
|
||||
AT91_REG csr;
|
||||
|
@ -521,7 +518,7 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, uint32_t leng
|
|||
//* \fn AT91F_USB_SendZlp
|
||||
//* \brief Send zero length packet through the control endpoint
|
||||
//*----------------------------------------------------------------------------
|
||||
void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
|
||||
static void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
|
||||
UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_TXPKTRDY);
|
||||
while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_TXCOMP))
|
||||
/* wait */;
|
||||
|
@ -535,7 +532,7 @@ void AT91F_USB_SendZlp(AT91PS_UDP pUdp) {
|
|||
//* \fn AT91F_USB_SendStall
|
||||
//* \brief Stall the control endpoint
|
||||
//*----------------------------------------------------------------------------
|
||||
void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
|
||||
static void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
|
||||
UDP_SET_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_FORCESTALL);
|
||||
while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_ISOERROR))
|
||||
/* wait */;
|
||||
|
@ -549,7 +546,7 @@ void AT91F_USB_SendStall(AT91PS_UDP pUdp) {
|
|||
//* \fn AT91F_CDC_Enumerate
|
||||
//* \brief This function is a callback invoked when a SETUP packet is received
|
||||
//*----------------------------------------------------------------------------
|
||||
void AT91F_CDC_Enumerate() {
|
||||
static void AT91F_CDC_Enumerate() {
|
||||
uint8_t bmRequestType, bRequest;
|
||||
uint16_t wValue, wIndex, wLength, wStatus;
|
||||
|
||||
|
@ -665,7 +662,7 @@ void AT91F_CDC_Enumerate() {
|
|||
|
||||
// handle CDC class requests
|
||||
case SET_LINE_CODING:
|
||||
while ( (pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0))
|
||||
while (!(pUdp->UDP_CSR[AT91C_EP_CONTROL] & AT91C_UDP_RX_DATA_BK0))
|
||||
/* wait */;
|
||||
UDP_CLEAR_EP_FLAGS(AT91C_EP_CONTROL, AT91C_UDP_RX_DATA_BK0);
|
||||
AT91F_USB_SendZlp(pUdp);
|
||||
|
@ -682,3 +679,55 @@ void AT91F_CDC_Enumerate() {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//***************************************************************************
|
||||
// Interface to the main program
|
||||
//***************************************************************************
|
||||
|
||||
// The function to receive a command from the client via USB
|
||||
bool cmd_receive(UsbCommand* cmd) {
|
||||
|
||||
// Check if there is a usb packet available
|
||||
if (!usb_poll())
|
||||
return false;
|
||||
|
||||
// Try to retrieve the available command frame
|
||||
size_t rxlen = usb_read((uint8_t*)cmd, sizeof(UsbCommand));
|
||||
|
||||
// Check if the transfer was complete
|
||||
if (rxlen != sizeof(UsbCommand))
|
||||
return false;
|
||||
|
||||
// Received command successfully
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// The function to send a response to the client via USB
|
||||
bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len) {
|
||||
UsbCommand txcmd;
|
||||
|
||||
for (size_t i = 0; i < sizeof(UsbCommand); i++) {
|
||||
((uint8_t*)&txcmd)[i] = 0x00;
|
||||
}
|
||||
|
||||
// Compose the outgoing command frame
|
||||
txcmd.cmd = cmd;
|
||||
txcmd.arg[0] = arg0;
|
||||
txcmd.arg[1] = arg1;
|
||||
txcmd.arg[2] = arg2;
|
||||
|
||||
// Add the (optional) content to the frame, with a maximum size of USB_CMD_DATA_SIZE
|
||||
if (data && len) {
|
||||
len = MIN(len, USB_CMD_DATA_SIZE);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
txcmd.d.asBytes[i] = ((uint8_t*)data)[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Send frame and make sure all bytes are transmitted
|
||||
if (usb_write((uint8_t*)&txcmd, sizeof(UsbCommand)) != 0) return false;
|
||||
|
||||
return true;
|
||||
}
|
|
@ -38,13 +38,13 @@
|
|||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_cmd.h"
|
||||
|
||||
extern void usb_disable();
|
||||
extern void usb_enable();
|
||||
extern bool usb_check();
|
||||
extern bool usb_poll();
|
||||
extern bool usb_poll_validate_length();
|
||||
extern uint32_t usb_read(uint8_t* data, size_t len);
|
||||
extern uint32_t usb_write(const uint8_t* data, const size_t len);
|
||||
extern bool cmd_receive(UsbCommand* cmd);
|
||||
extern bool cmd_send(uint32_t cmd, uint32_t arg0, uint32_t arg1, uint32_t arg2, void* data, size_t len);
|
||||
|
||||
#endif // _USB_CDC_H__
|
||||
#endif // USB_CDC_H__
|
Loading…
Reference in a new issue