FPC: got RX working, got client over usart somehow working..., see detailed commit msg

* using WITH_FPC:
  * activate basic usart
  * no double buffer for now, no interrupt
  * usart_dataavailable/usart_readbuffer/usart_writebuffer, to demo it:
    * pm3 client over USB, minicom over usart
    * analyse a d 414243
* using WITH_FPC_HOST:
  * it implies WITH_FPC as it's based on it
  * control pm3 with client over usart
  * EXPERIMENTAL! still some frame desync issues
  * you can connect both from usart & USB with two pm3 clients
    * actually you *have* to connect USB for the moment because
      it's used to send debug messages about buggy usart... See Dbprintf_usb below
  * "sessions": msgs are directed to the latest client to have sent a cmd
  * Dbprintf_usb macro to send msgs to USB client to help debugging usart...
  * We now have an option to run client at different speed as usart is 115200:
    client/proxmark3 /dev/ttyUSB0 -b 115200
  * Consequently, argc,argv handling is a bit revamped, it was so messy...
  * USB and flashing are still at 460800, don't try flashing over usart yet ^^
This commit is contained in:
Philippe Teuwen 2019-04-02 22:06:10 +02:00
commit 21be6d4400
15 changed files with 174 additions and 84 deletions

View file

@ -10,6 +10,7 @@
//-----------------------------------------------------------------------------
#include "usart.h"
#include "string.h"
#include "apps.h" // for Dbprintf
#define AT91_BAUD_RATE 115200
@ -39,52 +40,58 @@ void usart_close(void) {
}
*/
static uint8_t us_inbuf[sizeof(UsbCommand)];
static uint8_t us_outbuf[sizeof(UsbCommand)];
/// Reads data from an USART peripheral
/// \param data Pointer to the buffer where the received data will be stored.
/// \param len Size of the data buffer (in bytes).
inline int16_t usart_readbuffer(uint8_t *data, size_t len) {
// Check if the first PDC bank is free
if (!(pUS1->US_RCR)) {
pUS1->US_RPR = (uint32_t)data;
pUS1->US_RCR = len;
pUS1->US_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTDIS;
return 2;
}
// Check if the second PDC bank is free
else if (!(pUS1->US_RNCR)) {
pUS1->US_RNPR = (uint32_t)data;
pUS1->US_RNCR = len;
pUS1->US_PTCR = AT91C_PDC_RXTEN | AT91C_PDC_TXTDIS;
return 1;
// transfer from client to device
inline int16_t usart_readbuffer(uint8_t *data) {
uint32_t rcr = pUS1->US_RCR;
if (rcr < sizeof(us_inbuf)) {
pUS1->US_PTCR = AT91C_PDC_RXTDIS;
memcpy(data, us_inbuf, sizeof(us_inbuf) - rcr);
// Reset DMA buffer
pUS1->US_RPR = (uint32_t)us_inbuf;
pUS1->US_RCR = sizeof(us_inbuf);
pUS1->US_PTCR = AT91C_PDC_RXTEN;
return sizeof(us_inbuf) - rcr;
} else {
return 0;
}
}
inline bool usart_dataavailable(void) {
return (pUS1->US_CSR & AT91C_US_RXRDY) != 0;
return pUS1->US_RCR < sizeof(us_inbuf);
}
inline int16_t usart_readcommand(uint8_t *data) {
if (pUS1->US_RCR == 0)
return usart_readbuffer(data);
else
return 0;
}
inline bool usart_commandavailable(void) {
return pUS1->US_RCR == 0;
}
// transfer from device to client
inline int16_t usart_writebuffer(uint8_t *data, size_t len) {
if (pUS1->US_TCR == 0) {
if (pUS1->US_CSR & AT91C_US_ENDTX) {
memcpy(us_outbuf, data, len);
pUS1->US_TPR = (uint32_t)us_outbuf;
pUS1->US_TCR = len;
pUS1->US_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTDIS;
while((pUS1->US_CSR & AT91C_US_TXEMPTY) ==0) {};
return 2;
pUS1->US_PTCR = AT91C_PDC_TXTEN;
while(!(pUS1->US_CSR & AT91C_US_ENDTX)) {};
pUS1->US_PTCR = AT91C_PDC_TXTDIS;
return len;
} else {
return 0;
}
}
void usart_init(void) {
// For a nice detailed sample, interrupt driven but still relevant.
@ -128,6 +135,17 @@ void usart_init(void) {
pUS1->US_FIDI = 0;
pUS1->US_IF = 0;
// Disable double buffers for now
pUS1->US_TNPR = (uint32_t)0;
pUS1->US_TNCR = 0;
pUS1->US_RNPR = (uint32_t)0;
pUS1->US_RNCR = 0;
// re-enable receiver / transmitter
pUS1->US_CR = (AT91C_US_RXEN | AT91C_US_TXEN);
// ready to receive
pUS1->US_RPR = (uint32_t)us_inbuf;
pUS1->US_RCR = sizeof(us_inbuf);
pUS1->US_PTCR = AT91C_PDC_RXTEN;
}