Permalink
Browse files

added initial FreeRTOS multitasking support and moved main to task in…

… rfid.c
  • Loading branch information...
1 parent 1d68b76 commit d7cc1285da9547136c281ecbb60a33aeab2ca8c1 @meriac meriac committed Mar 24, 2011
Showing with 136 additions and 117 deletions.
  1. +12 −1 firmware/lpc13xx/openpcd2/inc/config.h
  2. +6 −96 firmware/lpc13xx/openpcd2/src/main.c
  3. +118 −20 firmware/lpc13xx/openpcd2/src/rfid.c
@@ -31,6 +31,17 @@
#define USB_VENDOR_ID 0x2366
#define USB_PROD_ID 0x0003
-#define USB_DEVICE 1
+
+/* PN532 pin definitions */
+#define PN532_RESET_PORT 1
+#define PN532_RESET_PIN 11
+#define PN532_CS_PORT 0
+#define PN532_CS_PIN 2
+
+
+/* FreeRTOS configuration */
+#define ENABLE_FREERTOS
+#define TASK_RFID_STACK_SIZE 256
+#define TASK_RFID_PRIORITY (tskIDLE_PRIORITY + 2)
#endif/*__CONFIG_H__*/
@@ -23,115 +23,25 @@
#include <openbeacon.h>
#include "rfid.h"
-static void rfid_hexdump(const void *buffer, int size)
-{
- int i;
- const unsigned char *p = (unsigned char *) buffer;
-
- for (i = 0; i < size; i++)
- {
- if (i && ((i & 3) == 0))
- debug_printf(" ");
- debug_printf(" %02X", *p++);
- }
- debug_printf(" [size=%02i]\n", size);
-}
-
-static int rfid_execute(void *data, unsigned int isize, unsigned int osize)
-{
- int res;
- if (rfid_write(data, isize))
- {
- debug_printf("getting result\n");
- res = rfid_read(data, osize);
- if (res > 0)
- rfid_hexdump(data, res);
- else
- debug_printf("error: res=%i\n", res);
- }
- else
- {
- debug_printf("->NACK!\n");
- res = -1;
- }
- return res;
-}
-
-void WriteRegister(unsigned short address, unsigned char data)
-{
- unsigned char cmd[4];
-
- /* write register */
- cmd[0] = 0x08;
- /* high byte of address */
- cmd[1] = address >> 8;
- /* low byte of address */
- cmd[2] = address & 0xFF;
- /* data value */
- cmd[3] = data;
-
- rfid_execute(&cmd, sizeof(cmd), sizeof(data));
-}
-
int main(void)
{
- int i;
- unsigned char data[80], counter;
-
/* Initialize GPIO (sets up clock) */
GPIOInit();
/* Set LED port pin to output */
- GPIOSetDir(LED_PORT, LED_BIT, 1);
+ GPIOSetDir(LED_PORT, LED_BIT, LED_ON);
/* UART setup */
UARTInit(115200, 0);
/* Init RFID */
rfid_init();
- /* Hello World */
- debug_printf("Hello RFID!\n");
-
- /* main loop */
- counter = 0;
-
- /* read firmware revision */
- debug_printf("\nreading firmware version...\n");
- data[0] = 0x02; /* cmd: GetFirmWareVersion */
- rfid_execute(&data, 1, sizeof(data));
-
- /* enable debug output */
- debug_printf("\nenabling debug output...\n");
- WriteRegister(0x6328, 0xFC);
- // select test bus signal
- WriteRegister(0x6321, 6);
- // select test bus type
- WriteRegister(0x6322, 0x07);
+ /* Update Core Clock */
+ SystemCoreClockUpdate ();
- while (1)
- {
- /* detect cards in field */
- GPIOSetValue(LED_PORT, LED_BIT, LED_ON);
- debug_printf("\nchecking for cards...\n");
- data[0] = 0x4A; /* cmd: InListPassiveTarget */
- data[1] = 0x01; /* MaxTg - maximum cards */
- data[2] = 0x00; /* BrTy - 106 kbps type A */
- if (((i = rfid_execute(&data, 3, sizeof(data))) >= 11) && (data[1]
- == 0x01) && (data[2] == 0x01))
- {
- debug_printf("card id: ");
- rfid_hexdump(&data[7], data[6]);
- }
- else
- debug_printf("unknown response of %i bytes\n", i);
- GPIOSetValue(LED_PORT, LED_BIT, LED_OFF);
+ /* Start the tasks running. */
+ vTaskStartScheduler();
- /* turning field off */
- debug_printf("\nturning field off again...\n");
- data[0] = 0x32; /* cmd: RFConfiguration */
- data[1] = 0x01; /* CfgItem = 0x01 */
- data[2] = 0x00; /* RF Field = off */
- rfid_execute(&data, 3, sizeof(data));
- }
+ return 0;
}
@@ -21,35 +21,26 @@
*/
#include <openbeacon.h>
+#include "pn532.h"
#include "rfid.h"
-#define PN532_FIFO_SIZE 64
-#define PN532_ACK_NACK_SIZE 6
-
-#define RESET_PORT 0
-#define RESET_PIN 0
-#define CS_PORT 0
-#define CS_PIN 2
-
#define BIT_REVERSE(x) ((unsigned char)(__RBIT(x)>>24))
static void rfid_reset(unsigned char reset)
{
- GPIOSetValue(RESET_PORT, RESET_PIN, reset);
+ GPIOSetValue(PN532_RESET_PORT, PN532_RESET_PIN, reset ? 1 : 0);
}
static void rfid_cs(unsigned char cs)
{
- GPIOSetValue(CS_PORT, CS_PIN, cs);
+ GPIOSetValue(PN532_CS_PORT, PN532_CS_PIN, cs ? 1 : 0);
}
static unsigned char rfid_tx(unsigned char data)
{
- while ((LPC_SSP->SR & 0x02) == 0)
- ;
+ while ((LPC_SSP->SR & 0x02) == 0);
LPC_SSP->DR = BIT_REVERSE (data);
- while ((LPC_SSP->SR & 0x04) == 0)
- ;
+ while ((LPC_SSP->SR & 0x04) == 0);
data = BIT_REVERSE (LPC_SSP->DR);
return data;
}
@@ -220,10 +211,116 @@ int rfid_write(const void *data, int len)
return rfid_read(NULL, 0);
}
-void rfid_init(void)
+static void rfid_hexdump(const void *buffer, int size)
{
- volatile int i;
+ int i;
+ const unsigned char *p = (unsigned char *) buffer;
+
+ for (i = 0; i < size; i++)
+ {
+ if (i && ((i & 3) == 0))
+ debug_printf(" ");
+ debug_printf(" %02X", *p++);
+ }
+ debug_printf(" [size=%02i]\n", size);
+}
+static int rfid_execute(void *data, unsigned int isize, unsigned int osize)
+{
+ int res;
+ if (rfid_write(data, isize))
+ {
+ debug_printf("getting result\n");
+ res = rfid_read(data, osize);
+ if (res > 0)
+ rfid_hexdump(data, res);
+ else
+ debug_printf("error: res=%i\n", res);
+ }
+ else
+ {
+ debug_printf("->NACK!\n");
+ res = -1;
+ }
+ return res;
+}
+
+void WriteRegister(unsigned short address, unsigned char data)
+{
+ unsigned char cmd[4];
+
+ /* write register */
+ cmd[0] = 0x08;
+ /* high byte of address */
+ cmd[1] = address >> 8;
+ /* low byte of address */
+ cmd[2] = address & 0xFF;
+ /* data value */
+ cmd[3] = data;
+
+ rfid_execute(&cmd, sizeof(cmd), sizeof(data));
+}
+
+static
+void rfid_task(void *pvParameters)
+{
+ int i;
+ static unsigned char data[80];
+
+ /* touch unused Parameter */
+ (void) pvParameters;
+
+ /* release reset line after 400ms */
+ vTaskDelay( 400 / portTICK_RATE_MS);
+ rfid_reset(1);
+ /* wait for PN532 to boot */
+ vTaskDelay( 100 / portTICK_RATE_MS);
+
+ /* read firmware revision */
+ debug_printf("\nreading firmware version...\n");
+ data[0] = PN532_CMD_GetFirmwareVersion;
+ rfid_execute(&data, 1, sizeof(data));
+
+ /* enable debug output */
+ debug_printf("\nenabling debug output...\n");
+ WriteRegister(0x6328, 0xFC);
+ // select test bus signal
+ WriteRegister(0x6321, 6);
+ // select test bus type
+ WriteRegister(0x6322, 0x07);
+
+ while (1)
+ {
+ /* wait 100ms */
+ vTaskDelay( 100 / portTICK_RATE_MS);
+
+ /* detect cards in field */
+ GPIOSetValue(LED_PORT, LED_BIT, LED_ON);
+ debug_printf("\nchecking for cards...\n");
+ data[0] = PN532_CMD_InListPassiveTarget;
+ data[1] = 0x01; /* MaxTg - maximum cards */
+ data[2] = 0x00; /* BrTy - 106 kbps type A */
+ if (((i = rfid_execute(&data, 3, sizeof(data))) >= 11) && (data[1]
+ == 0x01) && (data[2] == 0x01))
+ {
+ debug_printf("card id: ");
+ rfid_hexdump(&data[7], data[6]);
+ }
+ else
+ debug_printf("unknown response of %i bytes\n", i);
+ GPIOSetValue(LED_PORT, LED_BIT, LED_OFF);
+
+ /* turning field off */
+ debug_printf("\nturning field off again...\n");
+ data[0] = PN532_CMD_RFConfiguration;
+ data[1] = 0x01; /* CfgItem = 0x01 */
+ data[2] = 0x00; /* RF Field = off */
+ rfid_execute(&data, 3, sizeof(data));
+ }
+}
+
+void rfid_init(void)
+{
/* reset SSP peripheral */
LPC_SYSCON->PRESETCTRL = 0x01;
@@ -245,12 +342,13 @@ void rfid_init(void)
/* Initialize chip select line */
rfid_cs(1);
- GPIOSetDir(CS_PORT, CS_PIN, 1);
+ GPIOSetDir(PN532_CS_PORT, PN532_CS_PIN, 1);
/* Initialize RESET line */
rfid_reset(0);
- GPIOSetDir(RESET_PORT, RESET_PIN, 1);
- for (i = 0; i < 100000; i++);
+ GPIOSetDir(PN532_RESET_PORT, PN532_RESET_PIN, 1);
- rfid_reset(1);
+ /* Create PN532 task */
+ xTaskCreate(rfid_task, (const signed char*) "RFID", TASK_RFID_STACK_SIZE,
+ NULL, TASK_RFID_PRIORITY, NULL);
}

0 comments on commit d7cc128

Please sign in to comment.