From 413333331aa5db1a9d18be6bfb76ccd84f761ac4 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@6dc7ffe9-61d6-0310-9af1-9938baff3ed1> Date: Fri, 21 Jul 2006 23:47:38 +0000 Subject: fix spi git-svn-id: https://svn.openpcd.org:2342/trunk@9 6dc7ffe9-61d6-0310-9af1-9938baff3ed1 --- openpcd/firmware/Makefile | 4 +- openpcd/firmware/include/openpcd.h | 19 +++++++- openpcd/firmware/src/dbgu.c | 37 ++++++++++++++ openpcd/firmware/src/dbgu.h | 11 +++-- openpcd/firmware/src/led.c | 61 +++++++++++++++++++++++ openpcd/firmware/src/led.h | 9 ++++ openpcd/firmware/src/main.c | 94 ++++++++++++++++++++++++++---------- openpcd/firmware/src/openpcd.h | 12 +++-- openpcd/firmware/src/pcd_enumerate.c | 10 +--- openpcd/firmware/src/rc632.c | 51 ++++++++++++++----- openpcd/firmware/src/rc632.h | 2 + openpcd/firmware/src/rc632_debug.c | 30 ++++++++++++ openpcd/firmware/src/rc632_debug.h | 6 +++ 13 files changed, 291 insertions(+), 55 deletions(-) create mode 100644 openpcd/firmware/src/led.c create mode 100644 openpcd/firmware/src/led.h create mode 100644 openpcd/firmware/src/rc632_debug.c create mode 100644 openpcd/firmware/src/rc632_debug.h diff --git a/openpcd/firmware/Makefile b/openpcd/firmware/Makefile index 1882fe9..f9f81fc 100644 --- a/openpcd/firmware/Makefile +++ b/openpcd/firmware/Makefile @@ -70,7 +70,7 @@ SRC = # List C source files here which must be compiled in ARM-Mode. # use file-extension c for "c-only"-files -SRCARM = lib/lib_AT91SAM7S64.c src/pcd_enumerate.c src/fifo.c src/dbgu.c src/rc632.c src/$(TARGET).c compil/SrcWinARM/Cstartup_SAM7.c +SRCARM = lib/lib_AT91SAM7S64.c src/pcd_enumerate.c src/fifo.c src/dbgu.c src/led.c src/rc632.c src/rc632_debug.c src/$(TARGET).c compil/SrcWinARM/Cstartup_SAM7.c # List C++ source files here. # use file-extension cpp for C++-files (use extension .cpp) @@ -134,7 +134,7 @@ AT91LIBNOWARN = yes CSTANDARD = -std=gnu99 # Place -D or -U options for C here -CDEFS = -D$(RUN_MODE) +CDEFS = -D$(RUN_MODE) -DDEBUG -DOLIMEX # Place -I options here CINCS = -Ihelper -Isrc diff --git a/openpcd/firmware/include/openpcd.h b/openpcd/firmware/include/openpcd.h index dd68d52..cd85af2 100644 --- a/openpcd/firmware/include/openpcd.h +++ b/openpcd/firmware/include/openpcd.h @@ -1,9 +1,24 @@ #ifndef _OPENPCD_H #define _OPENPCD_H +#include #include -#define OPENPCD_RC632_IRQ AT91C_ID_IRQ1 -#define OPENPCD_RC632_RESET AT91C_PIO_PA29 +struct openpcd_hdr { + u_int8_t cmd; /* command */ + u_int8_t flags; + u_int8_t reg; /* register */ + u_int8_t val; /* value (in case of write *) */ + u_int16_t len; + u_int8_t data[0]; +} __attribute__ ((packed)); + +#define OPENPCD_CMD_WRITE_REG 0x01 +#define OPENPCD_CMD_WRITE_FIFO 0x02 +#define OPENPCD_CMD_WRITE_VFIFO 0x03 +#define OPENPCD_CMD_READ_REG 0x11 +#define OPENPCD_CMD_READ_FIFO 0x12 +#define OPENPCD_CMD_READ_VFIFO 0x13 +#define OPENPCD_CMD_SET_LED 0x21 #endif diff --git a/openpcd/firmware/src/dbgu.c b/openpcd/firmware/src/dbgu.c index 8e95202..7c94141 100644 --- a/openpcd/firmware/src/dbgu.c +++ b/openpcd/firmware/src/dbgu.c @@ -19,6 +19,7 @@ // Include Standard files #include "Board.h" #include "dbgu.h" +#include "rc632_debug.h" #define USART_SYS_LEVEL 4 /*---------------------------- Global Variable ------------------------------*/ //*--------------------------1-------------------------------------------------- @@ -67,6 +68,22 @@ static void DBGU_irq_handler(void) AT91F_DBGU_Printk("Clear Pull up\n\r"); // Reset Application Send_reset(); + break; + case '2': + AT91F_DBGU_Printk("Toggling LED 1\n\r"); + led_toggle(1); + break; + case '3': + AT91F_DBGU_Printk("Toggling LED 2\n\r"); + led_toggle(2); + break; + case '4': + AT91F_DBGU_Printk("Testing RC632 : "); + if (rc632_test() == 0) + AT91F_DBGU_Printk("SUCCESS!\n\r"); + else + AT91F_DBGU_Printk("ERROR!\n\r"); + break; default: AT91F_DBGU_Printk("\n\r"); @@ -184,3 +201,23 @@ void AT91F_DBGU_scanf(char *type, unsigned int *val) } //* End #endif + +#ifdef DEBUG +#include +#include +#include +static char dbg_buf[256]; +void debugp(const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + vsnprintf(dbg_buf, sizeof(dbg_buf)-1, format, ap); + va_end(ap); + + dbg_buf[sizeof(dbg_buf)-1] = '\0'; \ + AT91F_DBGU_Printk(dbg_buf); \ +} +#endif + + diff --git a/openpcd/firmware/src/dbgu.h b/openpcd/firmware/src/dbgu.h index 69c4997..8f7f0a4 100644 --- a/openpcd/firmware/src/dbgu.h +++ b/openpcd/firmware/src/dbgu.h @@ -15,11 +15,9 @@ #ifndef dbgu_h #define dbgu_h -#include - #define AT91C_DBGU_BAUD 115200 -#define DEBUGP(x) AT91F_DBGU_Printk(x) +//#define DEBUGP(x) AT91F_DBGU_Printk(x) //* ----------------------- External Function Prototype ----------------------- @@ -31,4 +29,11 @@ int AT91F_DBGU_Get( char *val); void AT91F_DBGU_scanf(char * type,unsigned int * val); #endif +#ifdef DEBUG +extern void debugp(const char *format, ...); +#define DEBUGP(x, args ...) debugp(x, ## args) +#else +#define DEBUGP(x, args ...) +#endif + #endif /* dbgu_h */ diff --git a/openpcd/firmware/src/led.c b/openpcd/firmware/src/led.c new file mode 100644 index 0000000..7f5db72 --- /dev/null +++ b/openpcd/firmware/src/led.c @@ -0,0 +1,61 @@ + +#include +#include +#include "openpcd.h" +#include "dbgu.h" + +static int led2port(int led) +{ + if (led == 1) + return OPENPCD_LED1; + else if (led == 2) + return OPENPCD_LED2; + else + return -1; +} + +void led_switch(int led, int on) +{ + int port = led2port(led); + + if (port == -1) + return; + + if (on) + AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, port); + else + AT91F_PIO_SetOutput(AT91C_BASE_PIOA, port); +} + +int led_get(int led) +{ + int port = led2port(led); + + if (port == -1) + return -1; + + return !(AT91F_PIO_GetOutputDataStatus(AT91C_BASE_PIOA) & port); +} + +int led_toggle(int led) +{ + int on = led_get(led); + if (on == -1) + return -1; + + if (on) { + DEBUGP("led%d on, switching off\r\n", led); + led_switch(led, 0); + } else { + DEBUGP("led%d off, switching on\r\n", led); + led_switch(led, 1); + } +} + +void led_init(void) +{ + AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, OPENPCD_LED1); + AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, OPENPCD_LED2); + led_switch(1, 0); + led_switch(2, 0); +} diff --git a/openpcd/firmware/src/led.h b/openpcd/firmware/src/led.h new file mode 100644 index 0000000..394107b --- /dev/null +++ b/openpcd/firmware/src/led.h @@ -0,0 +1,9 @@ +#ifndef _LED_H +#define _LED_H + +extern void led_init(void); +extern void led_switch(int led, int on); +extern int led_get(int led); +extern int led_toggle(int led); + +#endif diff --git a/openpcd/firmware/src/main.c b/openpcd/firmware/src/main.c index 2a569d4..a7e3171 100644 --- a/openpcd/firmware/src/main.c +++ b/openpcd/firmware/src/main.c @@ -17,8 +17,13 @@ //* 1.4 27/Apr/05 JPP : Unset the USART_COM and suppress displaying data //*-------------------------------------------------------------------------------------- +#include +#include #include +#include #include "dbgu.h" +#include "rc632.h" +#include "led.h" #include "pcd_enumerate.h" #define MSG_SIZE 1000 @@ -51,21 +56,65 @@ static void AT91F_USB_Open(void) AT91F_CDC_Open(AT91C_BASE_UDP); } -//*-------------------------------------------------------------------------------------- -//* Function Name : main -//* Object : -//*-------------------------------------------------------------------------------------- +static int usb_in(int len) +{ + struct openpcd_hdr *poh; + struct openpcd_hdr *pih; + u_int16_t data_len; + + if (len < sizeof(*poh)) + return -EINVAL; + + //data_len = ntohs(poh->len); + + memcpy(pih, poh, sizeof(*poh)); + + switch (poh->cmd) { + case OPENPCD_CMD_WRITE_REG: + DEBUGP("WRITE_REG "); + rc632_reg_write(poh->reg, poh->val); + break; + case OPENPCD_CMD_WRITE_FIFO: + DEBUGP("WRITE FIFO "); + if (len - sizeof(*poh) < data_len) + return -EINVAL; + rc632_fifo_write(data_len, poh->data); + break; + case OPENPCD_CMD_WRITE_VFIFO: + DEBUGP("WRITE VFIFO "); + break; + case OPENPCD_CMD_READ_REG: + DEBUGP("READ REG "); + pih->val = rc632_reg_read(poh->reg); + break; + case OPENPCD_CMD_READ_FIFO: + DEBUGP("READ FIFO "); + pih->len = rc632_fifo_read(poh->len, pih->data); + break; + case OPENPCD_CMD_READ_VFIFO: + DEBUGP("READ VFIFO "); + break; + case OPENPCD_CMD_SET_LED: + DEBUGP("SET LED "); + led_switch(poh->reg, poh->val); + break; + default: + return -EINVAL; + } +} + int main(void) { - char data[MSG_SIZE]; - unsigned int length; + int i, state = 0; - char message[30]; // Init trace DBGU AT91F_DBGU_Init(); AT91F_DBGU_Printk - ("\n\r-I- Basic USB loop back\n\r 0) Set Pull-UP 1) Clear Pull UP\n\r"); + ("\n\r-I- OpenPCD USB loop back\n\r 0) Set Pull-UP 1) Clear Pull UP\n\r"); + led_init(); + rc632_init(); + //printf("test 0x%02x\n\r", 123); // Enable User Reset and set its minimal assertion to 960 us @@ -79,24 +128,17 @@ int main(void) while (1) { // Check enumeration if (AT91F_UDP_IsConfigured()) { -#if 0 -#ifndef USART_COM - // Loop - length = AT91F_CDC_Read(&pCDC, data, MSG_SIZE); - pCDC.Write(&pCDC, data, length); - /// mt sprintf(message,"-I- Len %d:\n\r",length); - siprintf(message, "-I- Len %d:\n\r", length); - // send char - AT91F_DBGU_Frame(message); -#else - // Loop - length = pCDC.Read(&pCDC, data, MSG_SIZE); - data[length] = 0; - Trace_Toggel_LED(LED1); - AT91F_US_Put(data); - /// AT91F_DBGU_Frame(data); -#endif -#endif + + } + if (state == 0) { + led_switch(1, 1); + led_switch(2, 0); + state = 1; + } else { + led_switch(1, 0); + led_switch(2, 1); + state = 0; } + for (i = 0; i < 0x7fffff; i++) {} } } diff --git a/openpcd/firmware/src/openpcd.h b/openpcd/firmware/src/openpcd.h index 9f723da..40cc44a 100644 --- a/openpcd/firmware/src/openpcd.h +++ b/openpcd/firmware/src/openpcd.h @@ -1,10 +1,16 @@ #ifndef _OPENPCD_H #define _OPENPCD_H -#define OPENPCD_RC632_IRQ AT91C_PA30_IRQ1 +#ifdef OLIMEX +#define OPENPCD_LED1 AT91C_PIO_PA18 +#define OPENPCD_LED2 AT91C_PIO_PA17 +#else +#define OPENPCD_LED1 AT91C_PIO_PA25 +#define OPENPCD_LED2 AT91C_PIO_PA26 +#endif + +#define OPENPCD_RC632_IRQ AT91C_ID_IRQ1 #define OPENPCD_RC632_RESET AT91C_PIO_PA29 -#define OPENPCD_LED1_GREEN AT91C_PIO_PA25 -#define OPENPCD_LED2_RED AT91C_PIO_PA26 #define MAX_REQSIZE 256 #define MAX_HDRSIZE 8 diff --git a/openpcd/firmware/src/pcd_enumerate.c b/openpcd/firmware/src/pcd_enumerate.c index 46b2763..a8f55e3 100644 --- a/openpcd/firmware/src/pcd_enumerate.c +++ b/openpcd/firmware/src/pcd_enumerate.c @@ -25,8 +25,6 @@ #include "pcd_enumerate.h" #include "dbgu.h" -static char dbg_buf[256]; - static struct _AT91S_CDC pCDC; static AT91PS_CDC pCdc = &pCDC; @@ -140,10 +138,7 @@ static void udp_irq(void) AT91PS_UDP pUDP = pCDC.pUdp; AT91_REG isr = pUDP->UDP_ISR; - sprintf(dbg_buf, "udp_irq(imr=0x%04x, isr=0x%04x): ", pUDP->UDP_IMR, isr); - DEBUGP(dbg_buf); - - //DEBUGP("udp_irq: "); + DEBUGP("udp_irq(imr=0x%04x, isr=0x%04x): ", pUDP->UDP_IMR, isr); AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_UDP); @@ -342,8 +337,7 @@ static void AT91F_USB_SendData(AT91PS_UDP pUdp, const char *pData, u_int32_t len u_int32_t cpt = 0; AT91_REG csr; - sprintf(dbg_buf, "send_data: %u bytes ", length); - DEBUGP(dbg_buf); + DEBUGP("send_data: %u bytes ", length); do { cpt = MIN(length, 8); diff --git a/openpcd/firmware/src/rc632.c b/openpcd/firmware/src/rc632.c index 0de7765..4961a6b 100644 --- a/openpcd/firmware/src/rc632.c +++ b/openpcd/firmware/src/rc632.c @@ -18,14 +18,16 @@ static void spi_irq(void) { u_int32_t status = pSPI->SPI_SR; - DEBUGP("spi_irq: "); + DEBUGP("spi_irq: 0x%08x", status); + + AT91F_AIC_ClearIt(AT91C_BASE_AIC, AT91C_ID_SPI); if (status & AT91C_SPI_OVRES) DEBUGP("Overrun detected "); if (status & AT91C_SPI_MODF) DEBUGP("Mode Fault detected "); - DEBUGP("\n"); + DEBUGP("\r\n"); } /* stupid polling transceiver routine */ @@ -39,16 +41,28 @@ static int spi_transceive(const u_int8_t *tx_data, u_int16_t tx_len, *rx_len = 0; } + AT91F_SPI_Enable(pSPI); while (1) { u_int32_t sr = pSPI->SPI_SR; - if (sr & AT91C_SPI_TDRE) + DEBUGP("SPI_SR 0x%08x\r\n", sr); + if (sr & AT91C_SPI_TDRE) { + DEBUGP("TDRE, sending byte\r\n"); pSPI->SPI_TDR = tx_data[tx_cur++]; + } if (rx_len && *rx_len < rx_len_max) { - if (sr & AT91C_SPI_RDRF) + if (sr & AT91C_SPI_RDRF) { + DEBUGP("RDRF, reading byte\r\n"); rx_data[(*rx_len)++] = pSPI->SPI_RDR; + } + } + if (tx_cur >= tx_len) { + if (!rx_len) + return 0; + + if (*rx_len >= tx_len) + return 0; } - if (tx_cur >= tx_len) - return 0; + } return 0; } @@ -180,22 +194,36 @@ void rc632_reset(void) void rc632_init(void) { - fifo_init(&rc632.fifo, 256, NULL, &rc632); + //fifo_init(&rc632.fifo, 256, NULL, &rc632); + DEBUGP("rc632_pmc\r\n"); AT91F_SPI_CfgPMC(); - AT91F_SPI_CfgPIO(); /* check whether we really need all this */ - AT91F_SPI_Enable(pSPI); + DEBUGP("rc632_pio\r\n"); + AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA, + AT91C_PA11_NPCS0|AT91C_PA12_MISO| + AT91C_PA13_MOSI |AT91C_PA14_SPCK, 0); + + DEBUGP("rc632_en_spi\r\n"); + //AT91F_SPI_Enable(pSPI); + + DEBUGP("rc632_cfg_it_spi\r\n"); AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_SPI, AT91C_AIC_PRIOR_LOWEST, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, &spi_irq); + DEBUGP("rc632_en_it_spi\r\n"); AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SPI); - AT91F_SPI_EnableIt(pSPI, AT91C_SPI_MODF|AT91C_SPI_OVRES); + DEBUGP("rc632_spi_en_it\r\n"); + //AT91F_SPI_EnableIt(pSPI, AT91C_SPI_MODF|AT91C_SPI_OVRES|AT91C_SPI_RDRF|AT91C_SPI_TDRE); + DEBUGP("rc632_spi_cfg_mode\r\n"); AT91F_SPI_CfgMode(pSPI, AT91C_SPI_MSTR|AT91C_SPI_PS_FIXED); /* CPOL = 0, NCPHA = 1, CSAAT = 0, BITS = 0000, SCBR = 10 (4.8MHz), * DLYBS = 0, DLYBCT = 0 */ + DEBUGP("rc632_spi_cfg_cs\r\n"); AT91F_SPI_CfgCs(pSPI, 0, AT91C_SPI_BITS_8|AT91C_SPI_NCPHA|(10<<8)); - AT91F_SPI_Reset(pSPI); + + //DEBUGP("rc632_spi_reset\r\n"); + //AT91F_SPI_Reset(pSPI); /* Register rc632_irq */ AT91F_AIC_ConfigureIt(AT91C_BASE_AIC, AT91C_ID_IRQ1, AT91C_AIC_PRIOR_LOWEST, @@ -204,6 +232,7 @@ void rc632_init(void) AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, OPENPCD_RC632_RESET); + DEBUGP("rc632_reset\r\n"); rc632_reset(); }; diff --git a/openpcd/firmware/src/rc632.h b/openpcd/firmware/src/rc632.h index 281e757..bd82c03 100644 --- a/openpcd/firmware/src/rc632.h +++ b/openpcd/firmware/src/rc632.h @@ -1,6 +1,8 @@ #ifndef _RC623_API_H #define _RC632_API_H +#include + extern void rc632_write_reg(u_int8_t addr, u_int8_t data); extern void rc632_write_fifo(u_int8_t len, u_int8_t *data); extern u_int8_t rc632_read_reg(u_int8_t addr); diff --git a/openpcd/firmware/src/rc632_debug.c b/openpcd/firmware/src/rc632_debug.c new file mode 100644 index 0000000..fae9b2b --- /dev/null +++ b/openpcd/firmware/src/rc632_debug.c @@ -0,0 +1,30 @@ + +#include "rc632.h" +#include "dbgu.h" +#include + +#ifdef DEBUG +static int rc632_reg_write_verify(u_int8_t reg, u_int8_t val) +{ + u_int8_t tmp; + + rc632_reg_write(reg, val); + tmp = rc632_reg_read(reg); + + DEBUGP("reg=0x%02x, write=0x%02x, read=0x%02x ", reg, val, tmp); + + return (val == tmp); +} + +int rc632_test(void) +{ + if (rc632_reg_write_verify(RC632_REG_MOD_WIDTH, 0x55) != 1) + return -1; + + if (rc632_reg_write_verify(RC632_REG_MOD_WIDTH, 0xAA) != 1) + return -1; + + return 0; +} + +#endif diff --git a/openpcd/firmware/src/rc632_debug.h b/openpcd/firmware/src/rc632_debug.h new file mode 100644 index 0000000..26f341a --- /dev/null +++ b/openpcd/firmware/src/rc632_debug.h @@ -0,0 +1,6 @@ +#ifndef _RC632_DEBUG_H +#define _RC632_DEBUG_H + +extern int rc632_test(void); + +#endif -- cgit v1.2.3