From 514b0f72f50b50b75ef855f008c888f29989d68e Mon Sep 17 00:00:00 2001 From: laforge Date: Wed, 20 Sep 2006 11:44:10 +0000 Subject: - Add OpenPICC register definition (and USB command handling) - Add automatic generation of include/compile.h with svn revision and compiletime - Add openpcd_compile_version structure to obtain version via USB - Move LED commands into new CMD_CLS_GENERIC family - Update TODO - Add support for large (2048 byte) request contexts in addition to 64byte - Shrink req_ctx size by collapsing rx and tx buffer into one - move definition of DFU_API_LOCATION to header file - Implement large req_ctx aware USB transmit / refill routines - Implement TX refilling for IRQ Endpoint - Print version information at startup time - move some generic req_ctx processing into usb_handler.c - Some further work on DFU (still not finished) - Only use '-Os' for DFU, use '-O2' for application code git-svn-id: https://svn.openpcd.org:2342/trunk@208 6dc7ffe9-61d6-0310-9af1-9938baff3ed1 --- firmware/src/picc/adc.c | 10 ++--- firmware/src/picc/decoder.c | 2 - firmware/src/picc/main_openpicc.c | 5 +++ firmware/src/picc/openpicc.c | 82 +++++++++++++++++++++++++++++++++++++++ firmware/src/picc/opicc_reg.h | 18 +++++++++ firmware/src/picc/pll.c | 1 + firmware/src/picc/ssc_picc.c | 21 +++++----- 7 files changed, 120 insertions(+), 19 deletions(-) create mode 100644 firmware/src/picc/openpicc.c create mode 100644 firmware/src/picc/opicc_reg.h (limited to 'firmware/src/picc') diff --git a/firmware/src/picc/adc.c b/firmware/src/picc/adc.c index c78f89d..f350a49 100644 --- a/firmware/src/picc/adc.c +++ b/firmware/src/picc/adc.c @@ -74,7 +74,7 @@ static void adc_irq(void) if (sr & AT91C_ADC_ENDRX) { /* rctx full, get rid of it */ DEBUGADC("sending rctx (val=%s) ", - hexdump(rctx->tx.data[4], 2)); + hexdump(rctx->data[4], 2)); req_ctx_set_state(rctx, RCTX_STATE_UDP_EP2_PENDING); adc_state.state = ADC_NONE; @@ -115,8 +115,7 @@ u_int16_T adc_read_pll_dem(void) static int adc_usb_in(struct req_ctx *rctx) { - struct openpcd_hdr *poh = (struct openpcd_hdr *) &rctx->rx.data[0]; - struct openpcd_hdr *pih = (struct openpcd_hdr *) &rctx->tx.data[0]; + struct openpcd_hdr *poh = (struct openpcd_hdr *) &rctx->data[0]; switch (poh->cmd) { case OPENPCD_CMD_ADC_READ: @@ -129,9 +128,8 @@ static int adc_usb_in(struct req_ctx *rctx) adc_state.state = ADC_READ_CONTINUOUS_USB; adc_state.rctx = rctx; - memcpy(pih, poh, sizeof(*pih)); - rctx->tx.tot_len = sizeof(*pih) + poh->val * 2; - AT91F_PDC_SetRx(AT91C_BASE_PDC_ADC, rctx->rx.data, poh->val); + rctx->tot_len = sizeof(*poh) + poh->val * 2; + AT91F_PDC_SetRx(AT91C_BASE_PDC_ADC, rctx->data, poh->val); AT91F_PDC_EnableRx(AT91C_BASE_PDC_ADC); AT91F_ADC_EnableChannel(AT91C_BASE_ADC, OPENPICC_ADC_CH_FIELDSTR); AT91F_ADC_EnableIt(AT91C_BASE_ADC, AT91C_ADC_ENDRX | diff --git a/firmware/src/picc/decoder.c b/firmware/src/picc/decoder.c index ab77250..bdd910e 100644 --- a/firmware/src/picc/decoder.c +++ b/firmware/src/picc/decoder.c @@ -17,8 +17,6 @@ * */ - - #include #include #include diff --git a/firmware/src/picc/main_openpicc.c b/firmware/src/picc/main_openpicc.c index c8d2fd0..df754de 100644 --- a/firmware/src/picc/main_openpicc.c +++ b/firmware/src/picc/main_openpicc.c @@ -47,6 +47,7 @@ static u_int8_t load_mod = 0; void _init_func(void) { + /* low-level hardware initialization */ pio_irq_init(); pll_init(); poti_init(); @@ -57,6 +58,10 @@ void _init_func(void) adc_init(); ssc_rx_init(); // ssc_tx_init(); + + /* high-level protocol */ + decoder_init(); + opicc_usbapi_init(); AT91F_PIO_CfgInput(AT91C_BASE_PIOA, OPENPICC_PIO_BOOTLDR); } diff --git a/firmware/src/picc/openpicc.c b/firmware/src/picc/openpicc.c new file mode 100644 index 0000000..7b2b5f2 --- /dev/null +++ b/firmware/src/picc/openpicc.c @@ -0,0 +1,82 @@ +/* Main state machine and register implementation for OpenPICC + * (C) 2006 by Harald Welte */ + +#include +#include +#include +#include +#include +#include + +#include "opicc_reg.h" + +/******************************************************************** + * OpenPICC Register set + ********************************************************************/ + +/* Our registers, including their power-up default values */ +static u_int16_t opicc_regs[_OPICC_NUM_REGS] = { + [OPICC_REG_14443A_UIDLEN] = 4, + [OPICC_REG_14443A_FDT0] = 1236, + [OPICC_REG_14443A_FDT1] = 1172, + [OPICC_REG_14443A_STATE] = ISO14443A_ST_POWEROFF, + [OPICC_REG_RX_CLK_DIV] = 32, + [OPICC_REG_RX_CLK_PHASE] = 0, + [OPICC_REG_RX_CONTROL] = 0, + [OPICC_REG_TX_CLK_DIV] = 16, + [OPICC_REG_TX_CONTROL] = 0, + [OPICC_REG_RX_COMP_LEVEL] = 0, +}; + +u_int16_t opicc_reg_read(enum opicc_reg reg) +{ + if (reg < _OPICC_NUM_REGS) + return opicc_regs[reg]; + return 0; +} + +void opicc_reg_write(enum opicc_reg reg, u_int16_t val) +{ + if (reg < _OPICC_NUM_REGS) + opicc_regs[reg] = val; + return; +} + +/******************************************************************** + * OpenPICC USB Commandset (access to register set, ...) + ********************************************************************/ + +static int opicc_reg_usb_in(struct req_ctx *rctx) +{ + struct openpcd_hdr *poh = (struct openpcd_hdr *) &rctx->data[0]; + u_int16_t *val16 = (u_int16_t *) poh->data; + + poh->val = 0; + rctx->tot_len = sizeof(*poh); + + switch (poh->cmd) { + case OPENPCD_CMD_PICC_REG_READ: + *val16 = opicc_reg_read(poh->reg); + rctx->tot_len += sizeof(u_int16_t); + poh->flags |= OPENPCD_FLAG_RESPOND; + break; + case OPENPCD_CMD_PICC_REG_WRITE: + if (rctx->tot_len < sizeof(*poh) + sizeof(u_int16_t)) { + poh->flags = OPENPCD_FLAG_ERROR; + } + opicc_reg_write(poh->reg, *val16); + break; + default: + return USB_ERR(USB_ERR_CMD_UNKNOWN); + } + + if (poh->flags & OPENPCD_FLAG_RESPOND) + return USB_RET_RESPOND; + + return 0; +} + +void opicc_usbapi_init(void) +{ + usb_hdlr_register(&opicc_reg_usb_in, OPENPCD_CMD_CLS_PICC); +} diff --git a/firmware/src/picc/opicc_reg.h b/firmware/src/picc/opicc_reg.h new file mode 100644 index 0000000..e77d157 --- /dev/null +++ b/firmware/src/picc/opicc_reg.h @@ -0,0 +1,18 @@ +#ifndef _OPCD_REG_H +#define _OPCD_REG_H + +#include +#include + +#ifdef DEBUG +u_int16_t opicc_reg_read(enum opicc_reg reg); +void opicc_reg_write(enum opicc_reg reg, u_int16_t val); +#else +u_int16_t opicc_regs[_OPICC_NUM_REGS]; +#define opicc_reg_read(x) (opicc_regs[x]) +#define opicc_reg_Write(x, y) (opicc_regs[x] = y) +#endif + +void opicc_usbapi_init(void); + +#endif diff --git a/firmware/src/picc/pll.c b/firmware/src/picc/pll.c index f98d171..7eb49a4 100644 --- a/firmware/src/picc/pll.c +++ b/firmware/src/picc/pll.c @@ -22,6 +22,7 @@ #include #include #include +#include #include "../openpcd.h" void pll_inhibit(int inhibit) diff --git a/firmware/src/picc/ssc_picc.c b/firmware/src/picc/ssc_picc.c index ea48de5..d4db71a 100644 --- a/firmware/src/picc/ssc_picc.c +++ b/firmware/src/picc/ssc_picc.c @@ -166,8 +166,8 @@ static struct openpcd_hdr opcd_ssc_hdr = { static inline void init_opcdhdr(struct req_ctx *rctx) { - memcpy(&rctx->tx.data[0], &opcd_ssc_hdr, sizeof(opcd_ssc_hdr)); - rctx->tx.tot_len = MAX_HDRSIZE + MAX_REQSIZE -1; + memcpy(rctx->data, &opcd_ssc_hdr, sizeof(opcd_ssc_hdr)); + rctx->tot_len = MAX_HDRSIZE + MAX_REQSIZE -1; } #ifdef DEBUG_SSC_REFILL @@ -191,7 +191,7 @@ static int8_t ssc_rx_refill(void) #if 1 struct req_ctx *rctx; - rctx = req_ctx_find_get(RCTX_STATE_FREE, RCTX_STATE_SSC_RX_BUSY); + rctx = req_ctx_find_get(1, RCTX_STATE_FREE, RCTX_STATE_SSC_RX_BUSY); if (!rctx) { DEBUGPCRF("no rctx for refill!"); return -1; @@ -200,12 +200,12 @@ static int8_t ssc_rx_refill(void) if (AT91F_PDC_IsRxEmpty(rx_pdc)) { DEBUGR("filling primary SSC RX dma ctx"); - AT91F_PDC_SetRx(rx_pdc, &rctx->rx.data[MAX_HDRSIZE], - (sizeof(rctx->rx.data)-MAX_HDRSIZE)>>2); + AT91F_PDC_SetRx(rx_pdc, &rctx->data[MAX_HDRSIZE], + (rctx->size-MAX_HDRSIZE)>>2); ssc_state.rx_ctx[0] = rctx; /* If primary is empty, secondary must be empty, too */ - rctx = req_ctx_find_get(RCTX_STATE_FREE, + rctx = req_ctx_find_get(1, RCTX_STATE_FREE, RCTX_STATE_SSC_RX_BUSY); if (!rctx) { DEBUGPCRF("no rctx for secondary refill!"); @@ -216,8 +216,8 @@ static int8_t ssc_rx_refill(void) if (AT91F_PDC_IsNextRxEmpty(rx_pdc)) { DEBUGR("filling secondary SSC RX dma ctx"); - AT91F_PDC_SetNextRx(rx_pdc, &rctx->rx.data[MAX_HDRSIZE], - (sizeof(rctx->rx.data)-MAX_HDRSIZE)>2); + AT91F_PDC_SetNextRx(rx_pdc, &rctx->data[MAX_HDRSIZE], + (rctx->size-MAX_HDRSIZE)>2); ssc_state.rx_ctx[1] = rctx; return 2; } else { @@ -331,8 +331,7 @@ void ssc_tx_init(void) static int ssc_usb_in(struct req_ctx *rctx) { - struct openpcd_hdr *poh = (struct openpcd_hdr *) &rctx->rx.data[0]; - struct openpcd_hdr *pih = (struct openpcd_hdr *) &rctx->tx.data[0]; + struct openpcd_hdr *poh = (struct openpcd_hdr *) rctx->data; switch (poh->cmd) { case OPENPCD_CMD_SSC_READ: @@ -366,7 +365,7 @@ void ssc_rx_init(void) /* Reset */ //ssc->SSC_CR = AT91C_SSC_SWRST; - /* don't divide clock */ + /* don't divide clock inside SSC, we do that in tc_cdiv */ ssc->SSC_CMR = 0; ssc->SSC_RCMR = AT91C_SSC_CKS_RK | AT91C_SSC_CKO_NONE | -- cgit v1.2.3