static void loop_rfid (void) { int res, line, t; // uint32_t counter; static unsigned char data[80]; /* fully initialized */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); /* read firmware revision */ debug_printf ("\nreading firmware version...\n"); data[0] = PN532_CMD_GetFirmwareVersion; while ((res = rfid_execute (&data, 1, sizeof (data))) < 0) { debug_printf ("Reading Firmware Version Error [%i]\n", res); pmu_wait_ms (450); GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (10); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); } debug_printf ("PN532 Firmware Version: "); if (data[1] != 0x32) rfid_hexdump (&data[1], data[0]); else debug_printf ("v%i.%i\n", data[2], data[3]); /* Set PN532 to virtual card */ data[0] = PN532_CMD_SAMConfiguration; data[1] = 0x02; /* Virtual Card Mode */ data[2] = 0x00; /* No Timeout Control */ data[3] = 0x00; /* No IRQ */ if ((res = rfid_execute (&data, 4, sizeof (data))) > 0) { debug_printf ("SAMConfiguration: "); rfid_hexdump (&data, res); } /* init RFID emulator */ rfid_init_emulator (); /* enable debug output */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); line = 0; t = 0; while (1) { #if 0 counter = LPC_TMR32B0->TC; pmu_wait_ms (100); counter = (LPC_TMR32B0->TC - counter) * 10; debug_printf ("LPC_TMR32B0[%08u]: %uHz\n", line++, counter); #endif GPIOSetValue (LED_PORT, LED_BIT, GPIOGetValue (1, 8)); } }
int turn_rf_off (uint8_t *data, uint8_t size) { /* wait 0.5s */ pmu_wait_ms(5); /* turning field off */ data[0] = PN532_CMD_RFConfiguration; data[1] = 0x01; /* CfgItem = 0x01 */ data[2] = 0x00; /* RF Field = off */ return rfid_execute(&data[0], 3, size); }
void init_usbserial (void) { fifo_out_count = fifo_in_count = 0; CDC_DepInEmpty = TRUE; CDC_Init (); /* USB Initialization */ USB_Init (); /* Connect to USB port */ USB_Connect (TRUE); /* wait until USB is initialized */ while (!USB_Configuration) { pmu_wait_ms (90); GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (10); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); } }
uint8_t nRFAPI_Init (uint8_t channel, const uint8_t * mac, uint8_t mac_size, uint8_t features) { uint8_t i; // init IO layer of nRF24L01 nRFCMD_Init (); /* wait for nRF to boot */ pmu_wait_ms (10); // check validity if (mac_size < 3 || mac_size > 5 || !nRFAPI_DetectChip ()) return 0; // update mac nRFAPI_SetSizeMac (mac_size); nRFAPI_SetTxMAC (mac, mac_size); // enables pipe nRFAPI_SetRxMAC (mac, mac_size, 0); nRFAPI_PipesEnable (ERX_P0); nRFAPI_PipesAck (0); // set payload sizes for (i = 0; i <= 5; i++) nRFAPI_SetPipeSizeRX (i, 16); // set TX retry count nRFAPI_TxRetries (0); // set selected channel nRFAPI_SetChannel (channel); // set Tx power nRFAPI_SetTxPower (3); // flush FIFOs nRFAPI_FlushRX (); nRFAPI_FlushTX (); if (features != 0) nRFAPI_SetFeatures (features); return 1; }
int rfid_read(void *data, unsigned char size) { int res; unsigned char *p, c, pkt_size, crc, prev, t; /* wait 100ms max till PN532 response is ready */ t=0; while ((rfid_status() & 1) == 0) { if(t++>10) return -8; pmu_wait_ms( 10 ); } /* enable chip select */ rfid_cs(0); /* read from FIFO command */ rfid_tx(0x03); /* default result */ res = -9; /* find preamble */ t = 0; prev = rfid_rx(); while ((!(((c = rfid_rx()) == 0xFF) && (prev == 0x00))) && (t < PN532_FIFO_SIZE) ) { prev = c; t++; } if (t >= PN532_FIFO_SIZE) res = -3; else { /* read packet size */ pkt_size = rfid_rx(); /* special treatment for NACK and ACK */ if ((pkt_size == 0x00) || (pkt_size == 0xFF)) { /* verify if second length byte is inverted */ if (rfid_rx() != (unsigned char) (~pkt_size)) res = -2; else { /* eat Postamble */ rfid_rx(); /* -1 for NACK, 0 for ACK */ res = pkt_size ? -1 : 0; } } else { /* verify packet size against LCS */ if (((pkt_size + rfid_rx()) & 0xFF) != 0) res = -4; else { /* remove TFI from packet size */ pkt_size--; /* verify if packet fits into buffer */ if (pkt_size > size) res = -5; else { /* remember actual packet size */ size = pkt_size; /* verify TFI */ if ((crc = rfid_rx()) != 0xD5) res = -6; else { /* read packet */ p = (unsigned char *) data; while (pkt_size--) { /* read data */ c = rfid_rx(); /* maintain crc */ crc += c; /* save payload */ if (p) *p++ = c; } /* add DCS to CRC */ crc += rfid_rx(); /* verify CRC */ if (crc) res = -7; else { /* eat Postamble */ rfid_rx(); /* return actual size as result */ res = size; } } } } } } rfid_cs(1); /* everything fine */ return res; }
int main (void) { double t; int i, word, x, y; const TWordPos *w; TRGB color, *p; /* Initialize GPIO (sets up clock) */ GPIOInit (); /* Set LED port pin to output */ GPIOSetDir (LED_PORT, LED_PIN0, 1); GPIOSetValue (LED_PORT, LED_PIN0, LED_OFF); /* Init Power Management Routines */ pmu_init (); /* setup SPI chipselect pin */ spi_init (); spi_init_pin (SPI_CS_RGB); /* transmit image */ t = 0; word = 0; while(1) { /* set background to red */ memset(g_data, g_cie[0x00], sizeof(g_data)); for(y=0; y<LED_Y; y++) for(x=0; x<LED_X; x++) { g_data[y][x].r = g_cie[0x16]; g_data[y][x].g = g_cie[0x0B]; } /* get next word */ i = g_sentence[word/DELAY]; word++; if(word>=(WORD_COUNT*DELAY)) word=0; if(i>=0) { w = &g_words[i]; for(i=0; i<w->length; i++) { /* word coordinates */ x = w->x + i; y = w->y; /* update color */ color.r = (sin( x*0.1+cos(y*0.1+t))*CIE_MAX_INDEX2)+CIE_MAX_INDEX2; color.g = (cos(-y*0.2-sin(x*0.3-t))*CIE_MAX_INDEX2)+CIE_MAX_INDEX2; color.b = (cos( x*0.5-cos(y*0.4+t))*CIE_MAX_INDEX2)+CIE_MAX_INDEX2; p = &g_data[y][x]; p->r = g_cie[color.r]; p->g = g_cie[color.g]; p->b = g_cie[color.b]; } } /* send data */ update_leds(); pmu_wait_ms(1); t+=0.01; } }
int main (void) { uint64_t time_us; /* Initialize GPIO (sets up clock) */ GPIOInit (); /* Set eMeter port pin to input/pullup/hysteresis/CAP0 */ LPC_IOCON->PIO1_5 = 2| 2<<3 | 1<<5; GPIOSetDir (EMETER_PORT, EMETER_PIN, 0); GPIOSetValue (EMETER_PORT, EMETER_PIN, 0); /* Set LED port pin to output */ GPIOSetDir (LED_PORT, LED_PIN0, 1); GPIOSetDir (LED_PORT, LED_PIN1, 1); GPIOSetValue (LED_PORT, LED_PIN0, LED_OFF); GPIOSetValue (LED_PORT, LED_PIN1, LED_OFF); /* Init Power Management Routines */ pmu_init (); /* CDC USB Initialization */ init_usbserial (); /* configure TMR32B0 for capturing eMeter pulse duration in us */ LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 9); LPC_TMR32B0->PR = SYSTEM_CORE_CLOCK/1000000; LPC_TMR32B0->TCR = 2; LPC_TMR32B0->CCR = 6; /* enable TMR32B0 timer IRQ */ pulse_count = pulse_length = 0; timer_old = LPC_TMR32B0->TC; NVIC_EnableIRQ(TIMER_32_0_IRQn); /* release counter */ LPC_TMR32B0->TCR = 1; time_us=0; while(TRUE) { if(pulse_length) { time_us+=pulse_length; /* print count, time[s], power[mWh] */ debug_printf ( "%u,%u,%u\n", pulse_count, (uint32_t)(time_us/1000000UL), (uint32_t)(3600000000000ULL/pulse_length) ); pulse_length = 0; } /* blink once per second */ GPIOSetValue (LED_PORT, LED_PIN0, LED_ON); pmu_wait_ms(50); GPIOSetValue (LED_PORT, LED_PIN0, LED_OFF); pmu_wait_ms(950); } return 0; }
static void loop_rfid (void) { int res; static unsigned char data[80]; /* release reset line after 400ms */ pmu_wait_ms (400); rfid_reset (1); /* wait for PN532 to boot */ pmu_wait_ms (100); /* fully initialized */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); /* read firmware revision */ debug_printf ("\nreading firmware version...\n"); data[0] = PN532_CMD_GetFirmwareVersion; while ((res = rfid_execute (&data, 1, sizeof (data))) < 0) { debug_printf ("Reading Firmware Version Error [%i]\n", res); pmu_wait_ms (450); GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (10); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); } debug_printf ("PN532 Firmware Version: "); if (data[1] != 0x32) rfid_hexdump (&data[1], data[0]); else debug_printf ("v%i.%i\n", data[2], data[3]); /* enable debug output */ debug_printf ("\nenabling debug output...\n"); rfid_write_register (0x6328, 0xFC); // select test bus signal rfid_write_register (0x6321, 6); // select test bus type rfid_write_register (0x6322, 0x07); /* enable debug output */ GPIOSetValue (LED_PORT, LED_BIT, LED_ON); while (1) { /* wait 10ms */ pmu_wait_ms (10); /* detect cards in field */ data[0] = PN532_CMD_InListPassiveTarget; data[1] = 0x01; /* MaxTg - maximum cards */ data[2] = 0x00; /* BrTy - 106 kbps type A */ if (((res = rfid_execute (&data, 3, sizeof (data))) >= 11) && (data[1] == 0x01) && (data[2] == 0x01)) { GPIOSetValue (LED_PORT, LED_BIT, LED_ON); pmu_wait_ms (50); GPIOSetValue (LED_PORT, LED_BIT, LED_OFF); debug_printf ("card id: "); rfid_hexdump (&data[7], data[6]); } else { GPIOSetValue (LED_PORT, LED_BIT, LED_ON); if (res != -8) debug_printf ("PN532 error res=%i\n", res); } /* turning field off */ data[0] = PN532_CMD_RFConfiguration; data[1] = 0x01; /* CfgItem = 0x01 */ data[2] = 0x00; /* RF Field = off */ rfid_execute (&data, 3, sizeof (data)); } }