convert all src_simtrace code to kernel coding style

Let's use the Osmocom standard, based on the Linux kernel standard:
tab-indent and 8-charracter tab width.
This commit is contained in:
Harald Welte
2016-03-03 12:32:04 +01:00
parent 1605564489
commit 7dd3dfd992
9 changed files with 1025 additions and 986 deletions

View File

@@ -46,22 +46,28 @@
/** Maximum ATR ucSize in bytes.*/ /** Maximum ATR ucSize in bytes.*/
#define MAX_ATR_SIZE 55 #define MAX_ATR_SIZE 55
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Internal variables * Internal variables
*------------------------------------------------------------------------------*/ *------------------------------------------------------------------------------*/
/** ISO7816 pins */ /** ISO7816 pins */
static const Pin pinsISO7816[] = {PINS_ISO7816}; static const Pin pinsISO7816[] = { PINS_ISO7816 };
/** Bus switch pins */ /** Bus switch pins */
static const Pin pinsBus[] = {PINS_BUS_DEFAULT}; static const Pin pinsBus[] = { PINS_BUS_DEFAULT };
/* SIMcard power pin */ /* SIMcard power pin */
static const Pin pinsPower[] = {PWR_PINS}; static const Pin pinsPower[] = { PWR_PINS };
/** ISO7816 RST pin */ /** ISO7816 RST pin */
static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC; static const Pin pinIso7816RstMC = PIN_ISO7816_RSTMC;
static uint8_t sim_inserted = 0; static uint8_t sim_inserted = 0;
static struct Usart_info usart_info = {.base = USART_SIM, .id = ID_USART_SIM, .state = USART_RCV}; static struct Usart_info usart_info = {
.base = USART_SIM,
.id = ID_USART_SIM,
.state = USART_RCV
};
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Optional smartcard detection * Optional smartcard detection
@@ -74,7 +80,7 @@ static const Pin pinSmartCard = SMARTCARD_CONNECT_PIN;
* PIO interrupt service routine. Checks if the smartcard has been connected * PIO interrupt service routine. Checks if the smartcard has been connected
* or disconnected. * or disconnected.
*/ */
static void ISR_PioSmartCard( const Pin *pPin ) static void ISR_PioSmartCard(const Pin * pPin)
{ {
/* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number? /* FIXME: why is pinSmartCard.pio->PIO_ISR the wrong number?
printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR); printf("+++++ Trying to check for pending interrupts (PIO ISR: 0x%X)\n\r", pinSmartCard.pio->PIO_ISR);
@@ -83,38 +89,35 @@ Output:
+++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10 +++++ Trying to check for pending interrupts (PIO ISR: 0x400)) = 1<<10
+++++ Mask: 0x100 = 1<<8 +++++ Mask: 0x100 = 1<<8
*/ */
// PA10 is DTXD, which is the debug uart transmit pin // PA10 is DTXD, which is the debug uart transmit pin
printf("Interrupt!!\n\r"); printf("Interrupt!!\n\r");
/* Check all pending interrupts */ /* Check all pending interrupts */
// FIXME: this if condition is not always true... // FIXME: this if condition is not always true...
// if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 ) // if ( (pinSmartCard.pio->PIO_ISR & pinSmartCard.mask) != 0 )
{ {
/* Check current level on pin */ /* Check current level on pin */
if ( PIO_Get( &pinSmartCard ) == 0 ) if (PIO_Get(&pinSmartCard) == 0) {
{ sim_inserted = 1;
sim_inserted = 1; printf("-I- Smartcard inserted\n\r");
printf( "-I- Smartcard inserted\n\r" ) ; CCID_Insertion();
CCID_Insertion(); } else {
} sim_inserted = 0;
else printf("-I- Smartcard removed\n\r");
{ CCID_Removal();
sim_inserted = 0; }
printf( "-I- Smartcard removed\n\r" ) ; }
CCID_Removal();
}
}
} }
/** /**
* Configures the smartcard detection pin to trigger an interrupt. * Configures the smartcard detection pin to trigger an interrupt.
*/ */
static void ConfigureCardDetection( void ) static void ConfigureCardDetection(void)
{ {
printf("+++++ Configure PIOs\n\r"); printf("+++++ Configure PIOs\n\r");
PIO_Configure( &pinSmartCard, 1 ) ; PIO_Configure(&pinSmartCard, 1);
NVIC_EnableIRQ( PIOA_IRQn ); NVIC_EnableIRQ(PIOA_IRQn);
PIO_EnableIt( &pinSmartCard ) ; PIO_EnableIt(&pinSmartCard);
} }
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
@@ -122,62 +125,64 @@ static void ConfigureCardDetection( void )
*-----------------------------------------------------------------------------*/ *-----------------------------------------------------------------------------*/
extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID; extern CCIDDriverConfigurationDescriptors configurationDescriptorCCID;
void CCID_configure ( void ) { void CCID_configure(void)
CCIDDriver_Initialize(); {
CCIDDriver_Initialize();
// FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10); // FIXME: Do we need to set priority?: NVIC_SetPriority( PIOA_IRQn, 10);
PIO_ConfigureIt( &pinSmartCard, ISR_PioSmartCard ) ; PIO_ConfigureIt(&pinSmartCard, ISR_PioSmartCard);
} }
void CCID_exit ( void ) { void CCID_exit(void)
PIO_DisableIt( &pinSmartCard ) ;
USART_SetTransmitterEnabled(usart_info.base, 0);
USART_SetReceiverEnabled(usart_info.base, 0);
}
void CCID_init( void )
{ {
uint8_t pAtr[MAX_ATR_SIZE]; PIO_DisableIt(&pinSmartCard);
uint8_t ucSize ; USART_SetTransmitterEnabled(usart_info.base, 0);
USART_SetReceiverEnabled(usart_info.base, 0);
// FIXME: do we want to print ATR?
/* Initialize Atr buffer */
memset( pAtr, 0, sizeof( pAtr ) ) ;
ConfigureCardDetection() ;
// Configure ISO7816 driver
PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816));
PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus));
PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower));
/* power up the card */
// PIO_Set(&pinsPower[0]);
ISO7816_Init(&usart_info, CLK_MASTER);
USART_SetTransmitterEnabled(usart_info.base, 1);
USART_SetReceiverEnabled(usart_info.base, 1);
ISO7816_Set_Reset_Pin(&pinIso7816RstMC);
/* Read ATR */
ISO7816_warm_reset() ;
ISO7816_Datablock_ATR( pAtr, &ucSize ) ;
/* Decode ATR and print it */
ISO7816_Decode_ATR( pAtr ) ;
// FIXME. what if smcard is not inserted?
if(PIO_Get(&pinSmartCard) == 0) {
printf("SIM card inserted\n\r");
CCID_Insertion();
}
} }
void CCID_run( void ) void CCID_init(void)
{
uint8_t pAtr[MAX_ATR_SIZE];
uint8_t ucSize;
// FIXME: do we want to print ATR?
/* Initialize Atr buffer */
memset(pAtr, 0, sizeof(pAtr));
ConfigureCardDetection();
// Configure ISO7816 driver
PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816));
PIO_Configure(pinsBus, PIO_LISTSIZE(pinsBus));
PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower));
/* power up the card */
// PIO_Set(&pinsPower[0]);
ISO7816_Init(&usart_info, CLK_MASTER);
USART_SetTransmitterEnabled(usart_info.base, 1);
USART_SetReceiverEnabled(usart_info.base, 1);
ISO7816_Set_Reset_Pin(&pinIso7816RstMC);
/* Read ATR */
ISO7816_warm_reset();
ISO7816_Datablock_ATR(pAtr, &ucSize);
/* Decode ATR and print it */
ISO7816_Decode_ATR(pAtr);
// FIXME. what if smcard is not inserted?
if (PIO_Get(&pinSmartCard) == 0) {
printf("SIM card inserted\n\r");
CCID_Insertion();
}
}
void CCID_run(void)
{ {
//if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument); //if (USBD_Read(INT, pBuffer, dLength, fCallback, pArgument);
CCID_SmartCardRequest(); CCID_SmartCardRequest();
} }
#endif #endif

View File

@@ -14,14 +14,14 @@
* Internal variables * Internal variables
*------------------------------------------------------------------------------*/ *------------------------------------------------------------------------------*/
typedef struct { typedef struct {
/* static initialization, called whether or not the usb config is active */ /* static initialization, called whether or not the usb config is active */
void (* configure) ( void ); void (*configure) (void);
/* initialization function after the config was selected */ /* initialization function after the config was selected */
void (* init) ( void ); void (*init) (void);
/* de-initialization before selecting new config */ /* de-initialization before selecting new config */
void (* exit) ( void ); void (*exit) (void);
/* main loop content for given configuration */ /* main loop content for given configuration */
void (* run) ( void ); void (*run) (void);
} conf_func; } conf_func;
static const conf_func config_func_ptrs[] = { static const conf_func config_func_ptrs[] = {
@@ -55,12 +55,11 @@ static const conf_func config_func_ptrs[] = {
.configure = MITM_configure, .configure = MITM_configure,
.init = MITM_init, .init = MITM_init,
.exit = MITM_exit, .exit = MITM_exit,
.run = MITM_run .run = MITM_run,
}, },
#endif #endif
}; };
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Internal variables * Internal variables
*------------------------------------------------------------------------------*/ *------------------------------------------------------------------------------*/
@@ -72,86 +71,86 @@ static volatile enum confNum simtrace_config = CFG_NUM_PHONE;
static volatile enum confNum simtrace_config = CFG_NUM_CCID; static volatile enum confNum simtrace_config = CFG_NUM_CCID;
#endif #endif
/*---------------------------------------------------------------------------- /*----------------------------------------------------------------------------
* Callbacks * Callbacks
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum) void USBDDriverCallbacks_ConfigurationChanged(uint8_t cfgnum)
{ {
TRACE_INFO_WP("cfgChanged%d ", cfgnum); TRACE_INFO_WP("cfgChanged%d ", cfgnum);
simtrace_config = cfgnum; simtrace_config = cfgnum;
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Main * Main
*------------------------------------------------------------------------------*/ *------------------------------------------------------------------------------*/
#define MAX_USB_ITER BOARD_MCK/72 // This should be around a second #define MAX_USB_ITER BOARD_MCK/72 // This should be around a second
extern int main( void ) extern int main(void)
{ {
uint8_t isUsbConnected = 0; uint8_t isUsbConnected = 0;
enum confNum last_simtrace_config = simtrace_config; enum confNum last_simtrace_config = simtrace_config;
unsigned int i = 0; unsigned int i = 0;
LED_Configure(LED_NUM_RED); LED_Configure(LED_NUM_RED);
LED_Configure(LED_NUM_GREEN); LED_Configure(LED_NUM_GREEN);
LED_Set(LED_NUM_RED); LED_Set(LED_NUM_RED);
/* Disable watchdog*/ /* Disable watchdog */
WDT_Disable( WDT ) ; WDT_Disable(WDT);
req_ctx_init(); req_ctx_init();
PIO_InitializeInterrupts(0); PIO_InitializeInterrupts(0);
SIMtrace_USB_Initialize(); SIMtrace_USB_Initialize();
TRACE_INFO("USB init...\n\r"); TRACE_INFO("USB init...\n\r");
while(USBD_GetState() < USBD_STATE_CONFIGURED){ while (USBD_GetState() < USBD_STATE_CONFIGURED) {
if(i >= MAX_USB_ITER*3) { if (i >= MAX_USB_ITER * 3) {
TRACE_ERROR("Resetting board (USB could not be configured)\n"); TRACE_ERROR("Resetting board (USB could "
NVIC_SystemReset(); "not be configured)\n");
} NVIC_SystemReset();
i++; }
} i++;
}
TRACE_DEBUG("calling configure of all configurations...\n\r"); TRACE_DEBUG("calling configure of all configurations...\n\r");
for (i = 1; i < sizeof(config_func_ptrs)/sizeof(config_func_ptrs[0]); ++i) for (i = 1; i < sizeof(config_func_ptrs) / sizeof(config_func_ptrs[0]);
{ ++i) {
if (config_func_ptrs[i].configure) if (config_func_ptrs[i].configure)
config_func_ptrs[i].configure(); config_func_ptrs[i].configure();
} }
TRACE_DEBUG("calling init of config %u...\n\r", simtrace_config); TRACE_DEBUG("calling init of config %u...\n\r", simtrace_config);
config_func_ptrs[simtrace_config].init(); config_func_ptrs[simtrace_config].init();
last_simtrace_config = simtrace_config; last_simtrace_config = simtrace_config;
TRACE_DEBUG("entering main loop...\n\r"); TRACE_DEBUG("entering main loop...\n\r");
while(1) { while (1) {
const char rotor[] = { '-', '\\', '|', '/' }; const char rotor[] = { '-', '\\', '|', '/' };
putchar('\b'); putchar('\b');
putchar(rotor[i++ % ARRAY_SIZE(rotor)]); putchar(rotor[i++ % ARRAY_SIZE(rotor)]);
if (USBD_GetState() < USBD_STATE_CONFIGURED) { if (USBD_GetState() < USBD_STATE_CONFIGURED) {
if (isUsbConnected) { if (isUsbConnected) {
isUsbConnected = 0; isUsbConnected = 0;
} }
} } else if (isUsbConnected == 0) {
else if (isUsbConnected == 0) { TRACE_INFO("USB is now configured\n\r");
TRACE_INFO("USB is now configured\n\r"); LED_Set(LED_NUM_GREEN);
LED_Set(LED_NUM_GREEN); LED_Clear(LED_NUM_RED);
LED_Clear(LED_NUM_RED);
isUsbConnected = 1; isUsbConnected = 1;
} }
if (last_simtrace_config != simtrace_config) { if (last_simtrace_config != simtrace_config) {
TRACE_INFO("USB config chg %u -> %u\r\n", last_simtrace_config, simtrace_config); TRACE_INFO("USB config chg %u -> %u\r\n",
config_func_ptrs[last_simtrace_config].exit(); last_simtrace_config, simtrace_config);
config_func_ptrs[simtrace_config].init(); config_func_ptrs[last_simtrace_config].exit();
last_simtrace_config = simtrace_config; config_func_ptrs[simtrace_config].init();
} else { last_simtrace_config = simtrace_config;
config_func_ptrs[simtrace_config].run(); } else {
} config_func_ptrs[simtrace_config].run();
} }
}
} }

View File

@@ -37,32 +37,31 @@
#include <string.h> #include <string.h>
static const Pin pins_bus[] = { PINS_BUS_DEFAULT };
static const Pin pins_bus[] = {PINS_BUS_DEFAULT}; void MITM_configure(void)
void MITM_configure( void )
{ {
Phone_configure(); Phone_configure();
CCID_configure(); CCID_configure();
} }
void MITM_init( void ) void MITM_init(void)
{ {
CCID_init(); CCID_init();
Phone_init(); Phone_init();
return; return;
} }
void MITM_exit( void ) void MITM_exit(void)
{ {
Phone_exit(); Phone_exit();
CCID_exit(); CCID_exit();
} }
void MITM_run( void ) void MITM_run(void)
{ {
Phone_run(); Phone_run();
CCID_run(); CCID_run();
} }
#endif /* HAVE_MITM */ #endif /* HAVE_MITM */

View File

@@ -62,31 +62,36 @@ extern volatile uint8_t timeout_occured;
unsigned char USBState = STATE_IDLE; unsigned char USBState = STATE_IDLE;
/** ISO7816 pins */ /** ISO7816 pins */
static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE}; static const Pin pinsISO7816_PHONE[] = { PINS_ISO7816_PHONE };
/** Bus switch pins */ /** Bus switch pins */
#if DEBUG_PHONE_SNIFF #if DEBUG_PHONE_SNIFF
# warning "Debug phone sniff via logic analyzer is enabled" #warning "Debug phone sniff via logic analyzer is enabled"
// Logic analyzer probes are easier to attach to the SIM card slot // Logic analyzer probes are easier to attach to the SIM card slot
static const Pin pins_bus[] = {PINS_BUS_SNIFF}; static const Pin pins_bus[] = { PINS_BUS_SNIFF };
#else #else
static const Pin pins_bus[] = {PINS_BUS_DEFAULT}; static const Pin pins_bus[] = { PINS_BUS_DEFAULT };
#endif #endif
/** ISO7816 RST pin */ /** ISO7816 RST pin */
static uint8_t sim_inserted = 0; static uint8_t sim_inserted = 0;
static const Pin pPwr[] = { static const Pin pPwr[] = {
/* Enable power converter 4.5-6V to 3.3V; low: off */ /* Enable power converter 4.5-6V to 3.3V; low: off */
{SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT},
/* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */ /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */
{VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
}; };
const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE; const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE;
static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE, .state = USART_RCV}; static struct Usart_info usart_info = {
.base = USART_PHONE,
.id = ID_USART_PHONE,
.state = USART_RCV,
};
/* ===================================================*/ /* ===================================================*/
/* Taken from iso7816_4.c */ /* Taken from iso7816_4.c */
@@ -101,82 +106,91 @@ static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE
static uint8_t host_to_sim_buf[BUFLEN]; static uint8_t host_to_sim_buf[BUFLEN];
static bool change_fidi = false; static bool change_fidi = false;
static void receive_from_host( void ); static void receive_from_host(void);
static void sendResponse_to_phone( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining) static void sendResponse_to_phone(uint8_t * pArg, uint8_t status,
uint32_t transferred, uint32_t remaining)
{ {
if (status != USBD_STATUS_SUCCESS) { if (status != USBD_STATUS_SUCCESS) {
TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status); TRACE_ERROR("USB err status: %d (%s)\n", __FUNCTION__, status);
return; return;
} }
TRACE_DEBUG("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining); TRACE_DEBUG("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status,
TRACE_DEBUG("Resp: %x %x %x .. %x\n", host_to_sim_buf[0], host_to_sim_buf[1], host_to_sim_buf[2], host_to_sim_buf[transferred-1]); transferred, remaining);
TRACE_DEBUG("Resp: %x %x %x .. %x\n", host_to_sim_buf[0],
host_to_sim_buf[1], host_to_sim_buf[2],
host_to_sim_buf[transferred - 1]);
USART_SetReceiverEnabled(USART_PHONE, 0); USART_SetReceiverEnabled(USART_PHONE, 0);
USART_SetTransmitterEnabled(USART_PHONE, 1); USART_SetTransmitterEnabled(USART_PHONE, 1);
uint32_t i = 0; uint32_t i = 0;
if (host_to_sim_buf[0] == 0xff) { if (host_to_sim_buf[0] == 0xff) {
printf("Change FIDI detected\n"); printf("Change FIDI detected\n");
// PTS command, change FIDI after command // PTS command, change FIDI after command
i = 2; i = 2;
change_fidi = true; change_fidi = true;
} }
for (; i < transferred; i++ ) { for (; i < transferred; i++) {
ISO7816_SendChar(host_to_sim_buf[i], &usart_info); ISO7816_SendChar(host_to_sim_buf[i], &usart_info);
} }
USART_SetTransmitterEnabled(USART_PHONE, 0); USART_SetTransmitterEnabled(USART_PHONE, 0);
USART_SetReceiverEnabled(USART_PHONE, 1); USART_SetReceiverEnabled(USART_PHONE, 1);
if (change_fidi == true) { if (change_fidi == true) {
printf("Change FIDI: %x\n", host_to_sim_buf[2]); printf("Change FIDI: %x\n", host_to_sim_buf[2]);
update_fidi(host_to_sim_buf[2]); update_fidi(host_to_sim_buf[2]);
change_fidi = false; change_fidi = false;
} }
receive_from_host(); receive_from_host();
} }
static void receive_from_host() static void receive_from_host()
{ {
int ret; int ret;
if ((ret = USBD_Read(PHONE_DATAOUT, &host_to_sim_buf, sizeof(host_to_sim_buf), if ((ret = USBD_Read(PHONE_DATAOUT, &host_to_sim_buf,
(TransferCallback)&sendResponse_to_phone, 0)) == USBD_STATUS_SUCCESS) { sizeof(host_to_sim_buf),
} else { (TransferCallback) &sendResponse_to_phone,
TRACE_ERROR("USB Err: %X\n", ret); 0)) == USBD_STATUS_SUCCESS) {
} } else {
TRACE_ERROR("USB Err: %X\n", ret);
}
} }
void Phone_configure( void ) { void Phone_configure(void)
PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
NVIC_EnableIRQ( PIOA_IRQn );
}
void Phone_exit( void ) {
PIO_DisableIt( &pinPhoneRST ) ;
NVIC_DisableIRQ(USART1_IRQn);
USART_DisableIt( USART_PHONE, US_IER_RXRDY) ;
USART_SetTransmitterEnabled(USART_PHONE, 0);
USART_SetReceiverEnabled(USART_PHONE, 0);
}
void Phone_init( void ) {
PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ;
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
PIO_Configure( &pinPhoneRST, 1);
PIO_EnableIt( &pinPhoneRST ) ;
ISO7816_Init(&usart_info, CLK_SLAVE);
USART_SetTransmitterEnabled(USART_PHONE, 0);
USART_SetReceiverEnabled(USART_PHONE, 1);
USART_EnableIt(USART_PHONE, US_IER_RXRDY);
NVIC_EnableIRQ(USART1_IRQn);
receive_from_host();
}
void Phone_run( void )
{ {
check_data_from_phone(); PIO_ConfigureIt(&pinPhoneRST, ISR_PhoneRST);
NVIC_EnableIRQ(PIOA_IRQn);
}
void Phone_exit(void)
{
PIO_DisableIt(&pinPhoneRST);
NVIC_DisableIRQ(USART1_IRQn);
USART_DisableIt(USART_PHONE, US_IER_RXRDY);
USART_SetTransmitterEnabled(USART_PHONE, 0);
USART_SetReceiverEnabled(USART_PHONE, 0);
}
void Phone_init(void)
{
PIO_Configure(pinsISO7816_PHONE, PIO_LISTSIZE(pinsISO7816_PHONE));
PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
PIO_Configure(&pinPhoneRST, 1);
PIO_EnableIt(&pinPhoneRST);
ISO7816_Init(&usart_info, CLK_SLAVE);
USART_SetTransmitterEnabled(USART_PHONE, 0);
USART_SetReceiverEnabled(USART_PHONE, 1);
USART_EnableIt(USART_PHONE, US_IER_RXRDY);
NVIC_EnableIRQ(USART1_IRQn);
receive_from_host();
}
void Phone_run(void)
{
check_data_from_phone();
} }

View File

@@ -1,40 +1,40 @@
#include "ringbuffer.h" #include "ringbuffer.h"
#include "trace.h" #include "trace.h"
void rbuf_reset(volatile ringbuf *rb) void rbuf_reset(volatile ringbuf * rb)
{ {
rb->ird = 0; rb->ird = 0;
rb->iwr = 0; rb->iwr = 0;
} }
uint8_t rbuf_read(volatile ringbuf *rb) uint8_t rbuf_read(volatile ringbuf * rb)
{ {
uint8_t val = rb->buf[rb->ird]; uint8_t val = rb->buf[rb->ird];
rb->ird = (rb->ird + 1)%RING_BUFLEN; rb->ird = (rb->ird + 1) % RING_BUFLEN;
return val; return val;
} }
uint8_t rbuf_peek(volatile ringbuf *rb) uint8_t rbuf_peek(volatile ringbuf * rb)
{ {
return rb->buf[rb->ird]; return rb->buf[rb->ird];
} }
void rbuf_write(volatile volatile ringbuf *rb, uint8_t item) void rbuf_write(volatile volatile ringbuf * rb, uint8_t item)
{ {
if(!rbuf_is_full(rb)) { if (!rbuf_is_full(rb)) {
rb->buf[rb->iwr] = item; rb->buf[rb->iwr] = item;
rb->iwr = (rb->iwr + 1)%RING_BUFLEN; rb->iwr = (rb->iwr + 1) % RING_BUFLEN;
} else { } else {
TRACE_ERROR("Ringbuffer full, losing bytes!"); TRACE_ERROR("Ringbuffer full, losing bytes!");
} }
} }
bool rbuf_is_empty(volatile ringbuf *rb) bool rbuf_is_empty(volatile ringbuf * rb)
{ {
return rb->ird == rb->iwr; return rb->ird == rb->iwr;
} }
bool rbuf_is_full(volatile ringbuf *rb) bool rbuf_is_full(volatile ringbuf * rb)
{ {
return rb->ird == (rb->iwr+1)%RING_BUFLEN; return rb->ird == (rb->iwr + 1) % RING_BUFLEN;
} }

View File

@@ -8,16 +8,16 @@
#define RING_BUFLEN 1024 #define RING_BUFLEN 1024
typedef struct ringbuf { typedef struct ringbuf {
uint8_t buf[RING_BUFLEN]; uint8_t buf[RING_BUFLEN];
size_t ird; size_t ird;
size_t iwr; size_t iwr;
} ringbuf; } ringbuf;
void rbuf_reset(volatile ringbuf *rb); void rbuf_reset(volatile ringbuf * rb);
uint8_t rbuf_read(volatile ringbuf *rb); uint8_t rbuf_read(volatile ringbuf * rb);
uint8_t rbuf_peek(volatile ringbuf *rb); uint8_t rbuf_peek(volatile ringbuf * rb);
void rbuf_write(volatile ringbuf *rb, uint8_t item); void rbuf_write(volatile ringbuf * rb, uint8_t item);
bool rbuf_is_empty(volatile ringbuf *rb); bool rbuf_is_empty(volatile ringbuf * rb);
bool rbuf_is_full(volatile ringbuf *rb); bool rbuf_is_full(volatile ringbuf * rb);
#endif /* end of include guard: SIMTRACE_RINGBUF_H */ #endif /* end of include guard: SIMTRACE_RINGBUF_H */

View File

@@ -46,32 +46,36 @@ volatile ringbuf sim_rcv_buf = { {0}, 0, 0 };
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Interrupt routines * Interrupt routines
*-----------------------------------------------------------------------------*/ *-----------------------------------------------------------------------------*/
static void Callback_PhoneRST_ISR( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining) static void Callback_PhoneRST_ISR(uint8_t * pArg, uint8_t status,
uint32_t transferred, uint32_t remaining)
{ {
printf("rstCB\n\r"); printf("rstCB\n\r");
PIO_EnableIt( &pinPhoneRST ) ; PIO_EnableIt(&pinPhoneRST);
} }
void ISR_PhoneRST( const Pin *pPin)
void ISR_PhoneRST(const Pin * pPin)
{ {
int ret; int ret;
// FIXME: no printfs in ISRs? // FIXME: no printfs in ISRs?
printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR); printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR);
if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) ) if (((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0)) {
{ if (PIO_Get(&pinPhoneRST) == 0) {
if(PIO_Get( &pinPhoneRST ) == 0) { printf(" 0 ");
printf(" 0 "); } else {
} else { printf(" 1 ");
printf(" 1 "); }
} }
}
if ((ret = USBD_Write( PHONE_INT, "R", 1, (TransferCallback)&Callback_PhoneRST_ISR, 0 )) != USBD_STATUS_SUCCESS) { if ((ret =
TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__); USBD_Write(PHONE_INT, "R", 1,
return; (TransferCallback) & Callback_PhoneRST_ISR,
} 0)) != USBD_STATUS_SUCCESS) {
TRACE_ERROR("USB err status: %d (%s)\n", ret, __FUNCTION__);
return;
}
/* Interrupt enabled after ATR is sent to phone */ /* Interrupt enabled after ATR is sent to phone */
PIO_DisableIt( &pinPhoneRST ) ; PIO_DisableIt(&pinPhoneRST);
} }
extern void usart_irq_rx(uint8_t num); extern void usart_irq_rx(uint8_t num);
@@ -79,40 +83,40 @@ extern void usart_irq_rx(uint8_t num);
* char_stat is zero if no error occured. * char_stat is zero if no error occured.
* Otherwise it is filled with the content of the status register. * Otherwise it is filled with the content of the status register.
*/ */
void USART1_IrqHandler( void ) void USART1_IrqHandler(void)
{ {
#if 0 #if 0
uint32_t stat; uint32_t stat;
char_stat = 0; char_stat = 0;
// Rcv buf full // Rcv buf full
/* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) { /* if((stat & US_CSR_RXBUFF) == US_CSR_RXBUFF) {
TRACE_DEBUG("Rcv buf full"); TRACE_DEBUG("Rcv buf full");
USART_DisableIt(USART1, US_IDR_RXBUFF); USART_DisableIt(USART1, US_IDR_RXBUFF);
} }
*/ */
uint32_t csr = USART_PHONE->US_CSR; uint32_t csr = USART_PHONE->US_CSR;
if (csr & US_CSR_TXRDY) { if (csr & US_CSR_TXRDY) {
/* transmit buffer empty, nothing to transmit */ /* transmit buffer empty, nothing to transmit */
} }
if (csr & US_CSR_RXRDY) { if (csr & US_CSR_RXRDY) {
stat = (csr&(US_CSR_OVRE|US_CSR_FRAME| stat = (csr & (US_CSR_OVRE | US_CSR_FRAME |
US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| US_CSR_PARE | US_CSR_TIMEOUT | US_CSR_NACK |
(1<<10))); (1 << 10)));
uint8_t c = (USART_PHONE->US_RHR) & 0xFF; uint8_t c = (USART_PHONE->US_RHR) & 0xFF;
// printf(" %x", c); // printf(" %x", c);
if (stat == 0 ) { if (stat == 0) {
/* Fill char into buffer */ /* Fill char into buffer */
rbuf_write(&sim_rcv_buf, c); rbuf_write(&sim_rcv_buf, c);
} else { } else {
TRACE_DEBUG("e %x st: %x\n", c, stat); TRACE_DEBUG("e %x st: %x\n", c, stat);
} /* else: error occured */ } /* else: error occured */
char_stat = stat; char_stat = stat;
} }
#else #else
usart_irq_rx(0); usart_irq_rx(0);
#endif #endif
} }
@@ -121,13 +125,13 @@ void update_fidi(uint8_t fidi)
{ {
int rc; int rc;
uint8_t fi = fidi >> 4; uint8_t fi = fidi >> 4;
uint8_t di = fidi & 0xf; uint8_t di = fidi & 0xf;
rc = compute_fidi_ratio(fi, di); rc = compute_fidi_ratio(fi, di);
if (rc > 0 && rc < 0x400) { if (rc > 0 && rc < 0x400) {
TRACE_INFO("computed Fi(%u) Di(%u) ratio: %d", fi, di, rc); TRACE_INFO("computed Fi(%u) Di(%u) ratio: %d", fi, di, rc);
/* make sure UART uses new F/D ratio */ /* make sure UART uses new F/D ratio */
USART_PHONE->US_CR |= US_CR_RXDIS | US_CR_RSTRX; USART_PHONE->US_CR |= US_CR_RXDIS | US_CR_RSTRX;
USART_PHONE->US_FIDI = rc & 0x3ff; USART_PHONE->US_FIDI = rc & 0x3ff;
USART_PHONE->US_CR |= US_CR_RXEN | US_CR_STTTO; USART_PHONE->US_CR |= US_CR_RXEN | US_CR_STTTO;

View File

@@ -50,50 +50,58 @@
* Internal variables * Internal variables
*------------------------------------------------------------------------------*/ *------------------------------------------------------------------------------*/
/** ISO7816 pins */ /** ISO7816 pins */
static const Pin pinsISO7816_sniff[] = {PINS_SIM_SNIFF_SIM}; static const Pin pinsISO7816_sniff[] = { PINS_SIM_SNIFF_SIM };
static const Pin pins_bus[] = {PINS_BUS_SNIFF}; static const Pin pins_bus[] = { PINS_BUS_SNIFF };
static const Pin pPwr[] = { static const Pin pPwr[] = {
/* Enable power converter 4.5-6V to 3.3V; low: off */ /* Enable power converter 4.5-6V to 3.3V; low: off */
{SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT},
/* Enable second power converter: VCC_PHONE to VCC_SIM; high: on */ /* Enable second power converter: VCC_PHONE to VCC_SIM; high: on */
{VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT}
}; };
static struct Usart_info usart_info = {.base = USART_PHONE, .id = ID_USART_PHONE, .state = USART_RCV}; static struct Usart_info usart_info = {
.base = USART_PHONE,
.id = ID_USART_PHONE,
.state = USART_RCV,
};
/*----------------------------------------------------------------------------- /*-----------------------------------------------------------------------------
* Initialization routine * Initialization routine
*-----------------------------------------------------------------------------*/ *-----------------------------------------------------------------------------*/
void Sniffer_configure( void ){ void Sniffer_configure(void)
TRACE_INFO("Sniffer config\n");
}
void Sniffer_exit( void ){
TRACE_INFO("Sniffer exit\n");
USART_DisableIt(USART_PHONE, US_IER_RXRDY);
NVIC_DisableIRQ(USART1_IRQn);
USART_SetReceiverEnabled(USART_PHONE, 0);
}
void Sniffer_init( void )
{ {
TRACE_INFO("Sniffer Init\n"); TRACE_INFO("Sniffer config\n");
/* Configure ISO7816 driver */
PIO_Configure( pinsISO7816_sniff, PIO_LISTSIZE( pinsISO7816_sniff ) ) ;
PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ;
PIO_Configure(pPwr, PIO_LISTSIZE( pPwr ));
ISO7816_Init(&usart_info, CLK_SLAVE);
USART_SetReceiverEnabled(USART_PHONE, 1);
USART_EnableIt(USART_PHONE, US_IER_RXRDY);
NVIC_EnableIRQ(USART1_IRQn);
} }
void Sniffer_run( void ) void Sniffer_exit(void)
{ {
check_data_from_phone(); TRACE_INFO("Sniffer exit\n");
USART_DisableIt(USART_PHONE, US_IER_RXRDY);
NVIC_DisableIRQ(USART1_IRQn);
USART_SetReceiverEnabled(USART_PHONE, 0);
}
void Sniffer_init(void)
{
TRACE_INFO("Sniffer Init\n");
/* Configure ISO7816 driver */
PIO_Configure(pinsISO7816_sniff, PIO_LISTSIZE(pinsISO7816_sniff));
PIO_Configure(pins_bus, PIO_LISTSIZE(pins_bus));
PIO_Configure(pPwr, PIO_LISTSIZE(pPwr));
ISO7816_Init(&usart_info, CLK_SLAVE);
USART_SetReceiverEnabled(USART_PHONE, 1);
USART_EnableIt(USART_PHONE, US_IER_RXRDY);
NVIC_EnableIRQ(USART1_IRQn);
}
void Sniffer_run(void)
{
check_data_from_phone();
} }
#endif /* HAVE_SNIFFER */ #endif /* HAVE_SNIFFER */

File diff suppressed because it is too large Load Diff