error_t pn532_bus_HWInit(void) { #ifdef PN532_DEBUGMODE PN532_DEBUG("Initialising I2C%s", CFG_PRINTF_NEWLINE); #endif i2cInit(I2CMASTER); // Set reset pin as output and reset device GPIOSetDir(CFG_PN532_RSTPD_PORT, CFG_PN532_RSTPD_PIN, 1); #ifdef PN532_DEBUGMODE PN532_DEBUG("Resetting the PN532%s", CFG_PRINTF_NEWLINE); #endif LPC_GPIO->CLR[CFG_PN532_RSTPD_PORT] = (1 << CFG_PN532_RSTPD_PIN); systickDelay(400); LPC_GPIO->SET[CFG_PN532_RSTPD_PORT] = (1 << CFG_PN532_RSTPD_PIN); // Wait for the PN532 to finish booting systickDelay(100); // Ping the I2C device first to see if it exists! if (i2cCheckAddress(PN532_I2C_ADDRESS) == false) { #ifdef PN532_DEBUGMODE PN532_DEBUG("Can't find PN532 on the I2C bus%s", CFG_PRINTF_NEWLINE); #endif return ERROR_I2C_DEVICENOTFOUND; } // Set IRQ pin to input GPIOSetDir(CFG_PN532_I2C_IRQPORT, CFG_PN532_I2C_IRQPIN, 0); return ERROR_NONE; }
bool pn532_bus_i2c_WaitForReady(uint32_t timeout) { uint8_t busy = 1; uint8_t busyTimeout = 0; if (timeout) { /* Wait up to the specified number of ms for the IRQ */ while (busy) { busy = GPIOGetPinValue(CFG_PN532_I2C_IRQPORT, CFG_PN532_I2C_IRQPIN); systickDelay(1); busyTimeout++; if (busyTimeout == PN532_I2C_READYTIMEOUT) { return false; } } } else { /* Wait forever for the IRQ */ while (busy) { busy = GPIOGetPinValue(CFG_PN532_I2C_IRQPORT, CFG_PN532_I2C_IRQPIN); systickDelay(1); } } return true; }
static void showBatteryLevel(void) { uint8_t i,batLevel; float vbat; vbat = battery.vcellf[0] + battery.vcellf[1] + battery.vcellf[2]; if(vbat <= 10.0){ batLevel = 0; }else{ batLevel = (uint8_t)((vbat - 9) * 10/3.6); } /* Show battery level like MacBookPro's Battery level gauge */ GPIO2DATA = 0xffffff; /* all leds off */ if( batLevel == 0){ for(i = 0; i <= 7; i++) { GPIO2DATA ^= _BV(0); systickDelay(100); } }else{ for(i = 0; i < batLevel; i++) { GPIO2DATA &= ~(_BV(i)); systickDelay(200); } systickDelay(1500); GPIO2DATA = 0xffffff; /* all leds off */ } switch_flag = 0; }
pn532_error_t pn532_bus_Wakeup(void) { pn532_error_t error = PN532_ERROR_NONE; byte_t abtWakeUp[] = { 0x55,0x55,0x00,0x00,0x00,0x00,0x00,0xff,0x03,0xfd,0xd4,0x14,0x01,0x17,0x00,0x00,0xff,0x03,0xfd,0xd4,0x14,0x01,0x17,0x00 }; pn532_pcb_t *pn532 = pn532GetPCB(); #ifdef PN532_DEBUGMODE PN532_DEBUG("Sending Wakeup Sequence%s", CFG_PRINTF_NEWLINE); #endif error = pn532_bus_i2c_WriteData(abtWakeUp,sizeof(abtWakeUp)); if (error) { #ifdef PN532_DEBUGMODE PN532_DEBUG("Wakeup Failed (Error: %d)%s", error, CFG_PRINTF_NEWLINE); #endif return error; } systickDelay(100); // Wait for the IRQ/Ready flag to indicate a response is ready if (!(pn532_bus_i2c_WaitForReady(PN532_I2C_TIMEOUT))) { #ifdef PN532_DEBUGMODE PN532_DEBUG ("Timed out waiting for IRQ/Ready%s", CFG_PRINTF_NEWLINE); #endif error = PN532_ERROR_READYSTATUSTIMEOUT; } // Read and discard the ACK frame I2CWriteLength = 0; I2CReadLength = 7; // ACK + Ready bit = 7 I2CMasterBuffer[0] = PN532_I2C_ADDRESS | PN532_I2C_READBIT; i2cEngine(); systickDelay(1); // Wait for the IRQ/Ready flag to indicate a response is ready if (!(pn532_bus_i2c_WaitForReady(PN532_I2C_TIMEOUT))) { error = PN532_ERROR_READYSTATUSTIMEOUT; } #ifdef PN532_DEBUGMODE PN532_DEBUG("Wakeup Complete%s", CFG_PRINTF_NEWLINE); #endif pn532->state = PN532_STATE_READY; return error; }
int main(void) { // Configure cpu and mandatory peripherals systemInit(); // Make sure that projectconfig.h is properly configured for this example #if !defined CFG_CHIBI #error "CFG_CHIBI must be enabled in projectconfig.h for this example" #endif #if CFG_CHIBI_PROMISCUOUS != 0 #error "CFG_CHIBI_PROMISCUOUS must be set to 0 in projectconfig.h for this example" #endif #ifdef CFG_CHIBI uint32_t counter = 0; chb_pcb_t *pcb = chb_get_pcb(); while(1) { // Enable LED gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON); // Create and send the message char text[10]; counter++; itoa(counter, text, 10); chb_write(0xFFFF, text, strlen(text) + 1); // Disable LED gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF); systickDelay(250); } #endif return 0; }
void systemInit() { cpuInit(); // Configure the CPU systickInit(CFG_SYSTICK_DELAY_IN_MS); // Start systick timer gpioInit(); // Enable GPIO pmuInit(); // Configure power management step_timer_init(); // Initialise USB CDC #ifdef CFG_USBCDC lastTick = systickGetTicks(); // Used to control output/printf timing CDC_Init(); // Initialise VCOM USB_Init(); // USB Initialization USB_Connect(TRUE); // USB Connect // Wait until USB is configured or timeout occurs uint32_t usbTimeout = 0; while (usbTimeout < CFG_USBCDC_INITTIMEOUT / 10) { if (USB_Configuration) break; systickDelay(10); // Wait 10ms usbTimeout++; } #endif // Printf can now be used with UART or USBCDC }
tcs3414Error_e tcs3414GetRGBL(uint16_t *red, uint16_t *green, uint16_t *blue, uint16_t *clear) { if (!_tcs3414Initialised) tcs3414Init(); tcs3414Error_e error = TCS3414_ERROR_OK; // Enable the device by setting the control bit to 0x03 (power + ADC on) error = tcs3414Write8(TCS3414_COMMAND_BIT | TCS3414_REGISTER_CONTROL, TCS3414_CONTROL_POWERON); if (error) return error; // Wait >12ms for ADC to complete systickDelay(13); // Reads two byte red value error = tcs3414Read16(TCS3414_COMMAND_BIT | TCS3414_WORD_BIT | TCS3414_REGISTER_REDLOW, red); if (error) return error; // Reads two byte green value error = tcs3414Read16(TCS3414_COMMAND_BIT | TCS3414_WORD_BIT | TCS3414_REGISTER_GREENLOW, green); if (error) return error; // Reads two byte blue value error = tcs3414Read16(TCS3414_COMMAND_BIT | TCS3414_WORD_BIT | TCS3414_REGISTER_BLUELOW, blue); if (error) return error; // Reads two byte clear value error = tcs3414Read16(TCS3414_COMMAND_BIT | TCS3414_WORD_BIT | TCS3414_REGISTER_CLEARLOW, clear); if (error) return error; // Turn the device off to save power error = tcs3414Write8(TCS3414_COMMAND_BIT | TCS3414_REGISTER_CONTROL, TCS3414_CONTROL_POWEROFF); if (error) return error; return error; }
int main () { init (); gpioSetDir (3, 1, 1); gpioSetValue (3, 1, 1); int availBytes; char buf[32]; uint8_t frame[64]; for (int i = 0; i < 64; ++i) { frame[i] = i; } while (1) { systickDelay (1); USB_WriteEP (CDC_DEP_IN, frame, 64); // CDC_WrOutBuf (text, &textLen); CDC_OutBufAvailChar (&availBytes); if (availBytes > 0) { int bytesToRead = availBytes > 32 ? 32 : availBytes; int bytesRead = CDC_RdOutBuf (buf, &bytesToRead); gpioSetValue (3, 1, 0); } } }
int puts(const char * str) { // There must be at least 1ms between USB frames (of up to 64 bytes) // This buffers all data and writes it out from the buffer one frame // and one millisecond at a time #ifdef CFG_PRINTF_USBCDC if (USB_Configuration) { while(*str) cdcBufferWrite(*str++); // Check if we can flush the buffer now or if we need to wait unsigned int currentTick = systickGetTicks(); if (currentTick != lastTick) { uint8_t frame[64]; uint32_t bytesRead = 0; while (cdcBufferDataPending()) { // Read up to 64 bytes as long as possible bytesRead = cdcBufferReadLen(frame, 64); USB_WriteEP (CDC_DEP_IN, frame, bytesRead); systickDelay(1); } lastTick = currentTick; } } #else // Handle output character by character in __putchar while(*str) __putchar(*str++); #endif return 0; }
void lcdInit(void) { // Set control pins to output gpioSetDir(ST7735_PORT, ST7735_RS_PIN, 1); gpioSetDir(ST7735_PORT, ST7735_SDA_PIN, 1); gpioSetDir(ST7735_PORT, ST7735_SCL_PIN, 1); gpioSetDir(ST7735_PORT, ST7735_CS_PIN, 1); gpioSetDir(ST7735_PORT, ST7735_RES_PIN, 1); gpioSetDir(ST7735_PORT, ST7735_BL_PIN, 1); // Set pins low by default (except reset) CLR_RS; CLR_SDA; CLR_SCL; CLR_CS; CLR_BL; SET_RES; // Turn backlight on lcdBacklight(TRUE); // Reset display CLR_RES; systickDelay(50); SET_RES; // Run LCD init sequence st7735InitDisplay(); // Fill black lcdFillRGB(COLOR_BLACK); }
mpl115a2Error_t mpl115a2ReadPressureTemp(uint16_t *pressure, uint16_t *temp) { // Clear write buffers uint32_t i; for ( i = 0; i < I2C_BUFSIZE; i++ ) { I2CMasterBuffer[i] = 0x00; I2CSlaveBuffer[i] = 0x00; } I2CWriteLength = 3; I2CReadLength = 1; I2CMasterBuffer[0] = MPL115A2_ADDRESS; I2CMasterBuffer[1] = MPL115A2_REGISTER_STARTCONVERSION; I2CMasterBuffer[2] = 0x00; // Why is this necessary to get results? i2cEngine(); // Wait a bit for the conversion to complete (3ms max) systickDelay(5); I2CWriteLength = 2; I2CReadLength = 4; I2CMasterBuffer[0] = MPL115A2_ADDRESS; I2CMasterBuffer[1] = MPL115A2_REGISTER_PRESSURE_MSB; I2CMasterBuffer[2] = MPL115A2_ADDRESS | MPL115A2_READBIT; i2cEngine(); // Shift values to create properly formed integers *pressure = ((I2CSlaveBuffer[0] << 8) | (I2CSlaveBuffer[1])) >> 6; *temp = ((I2CSlaveBuffer[2] << 8) | (I2CSlaveBuffer[3])) >> 6; return MPL115A2_ERROR_OK; }
void tsCalibrate(void) { tsTouchData_t data; /* --------------- Welcome Screen --------------- */ data = tsRenderCalibrationScreen(lcdGetWidth() / 2, lcdGetHeight() / 2, 5); systickDelay(250); /* ----------------- First Dot ------------------ */ // 10% over and 10% down data = tsRenderCalibrationScreen(lcdGetWidth() / 10, lcdGetHeight() / 10, 5); _tsLCDPoints[0].x = lcdGetWidth() / 10; _tsLCDPoints[0].y = lcdGetHeight() / 10; _tsTSPoints[0].x = data.xraw; _tsTSPoints[0].y = data.yraw; printf("Point 1 - LCD X:%04d Y:%04d TS X:%04d Y:%04d \r\n", (int)_tsLCDPoints[0].x, (int)_tsLCDPoints[0].y, (int)_tsTSPoints[0].x, (int)_tsTSPoints[0].y); systickDelay(250); /* ---------------- Second Dot ------------------ */ // 50% over and 90% down data = tsRenderCalibrationScreen(lcdGetWidth() / 2, lcdGetHeight() - lcdGetHeight() / 10, 5); _tsLCDPoints[1].x = lcdGetWidth() / 2; _tsLCDPoints[1].y = lcdGetHeight() - lcdGetHeight() / 10; _tsTSPoints[1].x = data.xraw; _tsTSPoints[1].y = data.yraw; printf("Point 2 - LCD X:%04d Y:%04d TS X:%04d Y:%04d \r\n", (int)_tsLCDPoints[1].x, (int)_tsLCDPoints[1].y, (int)_tsTSPoints[1].x, (int)_tsTSPoints[1].y); systickDelay(250); /* ---------------- Third Dot ------------------- */ // 90% over and 50% down data = tsRenderCalibrationScreen(lcdGetWidth() - lcdGetWidth() / 10, lcdGetHeight() / 2, 5); _tsLCDPoints[2].x = lcdGetWidth() - lcdGetWidth() / 10; _tsLCDPoints[2].y = lcdGetHeight() / 2; _tsTSPoints[2].x = data.xraw; _tsTSPoints[2].y = data.yraw; printf("Point 3 - LCD X:%04d Y:%04d TS X:%04d Y:%04d \r\n", (int)_tsLCDPoints[2].x, (int)_tsLCDPoints[2].y, (int)_tsTSPoints[2].x, (int)_tsTSPoints[2].y); systickDelay(250); // Do matrix calculations for calibration and store to EEPROM setCalibrationMatrix(&_tsLCDPoints[0], &_tsTSPoints[0], &_tsMatrix); }
tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) { if (!_tsl2561Initialised) tsl2561Init(); tsl2561Error_t error = TSL2561_ERROR_OK; // Enable the device by setting the control bit to 0x03 error = tsl2561Enable(); if (error) return error; // Wait x ms for ADC to complete switch (_tsl2561IntegrationTime) { case TSL2561_INTEGRATIONTIME_13MS: systickDelay(14); break; case TSL2561_INTEGRATIONTIME_101MS: systickDelay(102); break; default: systickDelay(400); break; } // Reads two byte value from channel 0 (visible + infrared) error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); if (error) return error; // Reads two byte value from channel 1 (infrared) error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); if (error) return error; // Turn the device off to save power error = tsl2561Disable(); if (error) return error; return error; }
void init () { cpuInit (); systickInit (1); gpioInit (); pmuInit (); CDC_Init (); USB_Init (); USB_Connect (TRUE); gpioSetDir (3, 0, 1); gpioSetValue (3, 0, 1); while (!USB_Configuration) { systickDelay (10); } gpioSetValue (3, 0, 0); }
void itg3200Calibrate(GyroData *data, uint32_t cnt, uint32_t delay) { int i; float x, y, z; data->x_bias = 0; data->y_bias = 0; data->z_bias = 0; for(i = 0; i < cnt; ++i) { itg3200GetData(data); x += data->X; y += data->Y; z += data->Z; systickDelay(delay); } data->x_bias = -x / cnt; data->y_bias = -y / cnt; data->z_bias = -z / cnt; }
/** * @brief Low level spi output for display * @param 8-bit data to be sent * @return void */ void low_level_output(uint8_t data) { uint8_t i = 0; systickDelay(1); for (i = 0; i < 8; i++) { CLR_SCL; //systickDelay(1); if (data & 0x80) SET_SDA; else CLR_SDA; //systickDelay(1); data <<= 1; SET_SCL; //systickDelay(1); } }
error_t pca9685SetFrequency(uint16_t freqHz) { uint32_t prescaleValue; uint8_t oldMode, newMode; ASSERT(_pca9685Initialised, ERROR_DEVICENOTINITIALISED); if (freqHz < 40) { freqHz = 40; } if (freqHz > 1000) { freqHz = 1000; } // prescaleValue = round(25MHz / (4096*updateRate)) - 1 prescaleValue = 25000000; // 25 MHz prescaleValue /= 4096; // 12-bit prescaleValue /= freqHz; prescaleValue -= 1; ASSERT_STATUS(pca9685Read8(PCA9685_REG_MODE1, &oldMode)); newMode = (oldMode & 0x7F) | 0x10; // Go to sleep ASSERT_STATUS(pca9685Write8(PCA9685_REG_MODE1, newMode)); // Set prescale ASSERT_STATUS(pca9685Write8(PCA9685_REG_PRESCALE, prescaleValue & 0xFF)); // Wakeup ASSERT_STATUS(pca9685Write8(PCA9685_REG_MODE1, oldMode)); systickDelay(5); ASSERT_STATUS(pca9685Write8(PCA9685_REG_MODE1, oldMode | 0x80)); return ERROR_NONE; }
int main(void) { // Configure cpu and mandatory peripherals systemInit(); while (1) { // Wait one second systickDelay(1000); // Toggle the LED if (gpioGetValue(CFG_LED_PORT, CFG_LED_PIN) == CFG_LED_OFF) { gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON); } else { gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF); } } return 0; }
isl12022mError_t isl12022mGetTemp(uint8_t *celsius) { isl12022mError_t error = ISL12022M_ERROR_OK; uint8_t buffer[2]; uint32_t temp; if (!_isl12022mInitialised) { error = isl12022mInit(); if (error) return error; } // Enable temperature sensing if required error = isl12022mReadBuffer(ISL12022M_RTC_ADDRESS, ISL12022M_REG_CSR_BETA, buffer, 1); if (!error) { if (!(buffer[0] & ISL12022M_BETA_TEMPENABLE)) { // Temp sensor is not enabled ... enable it now error = isl12022mWrite8(ISL12022M_RTC_ADDRESS, ISL12022M_REG_CSR_BETA, buffer[0] | ISL12022M_BETA_TEMPENABLE); if (error) return error; } } // Wait 100ms for conversion to complete systickDelay(100); // Read low and high temp bytes (0x28 and 0x29) error = isl12022mReadBuffer(ISL12022M_RTC_ADDRESS, ISL12022M_REG_TEMP_TKOL, buffer, 2); if (error) return error; // Convert value to degrees celsius (value/2 - 273 = degrees C) temp = ((buffer[0]) | (buffer[1] << 8)) / 2 - 273; *celsius = (uint8_t)temp & 0xFF; return error; }
void cmd_sysinfo(uint8_t argc, char **argv) { printf("%-25s : %d.%d MHz %s", "System Clock", CFG_CPU_CCLK / 1000000, CFG_CPU_CCLK % 1000000, CFG_PRINTF_NEWLINE); printf("%-25s : %d.%d.%d %s", "Firmware", CFG_FIRMWARE_VERSION_MAJOR, CFG_FIRMWARE_VERSION_MINOR, CFG_FIRMWARE_VERSION_REVISION, CFG_PRINTF_NEWLINE); // 128-bit MCU Serial Number IAP_return_t iap_return; iap_return = iapReadSerialNumber(); if(iap_return.ReturnCode == 0) { printf("%-25s : %08X %08X %08X %08X %s", "Serial Number", iap_return.Result[0],iap_return.Result[1],iap_return.Result[2],iap_return.Result[3], CFG_PRINTF_NEWLINE); } // Check the battery voltage #ifdef CFG_BAT uint32_t c; gpioSetDir(CFG_BAT_ENPORT, CFG_BAT_ENPIN, gpioDirection_Output ); gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 1 ); // Enable the voltage divider systickDelay(5); c = adcRead(CFG_BAT_ADC); // Pre-read ADC to warm it up systickDelay(10); c = adcRead(CFG_BAT_ADC); c = (c * CFG_VREG_VCC_MAIN) / 1000; // Value in millivolts relative to supply voltage c = (c * CFG_BAT_MULTIPLIER) / 1000; // Battery voltage in millivolts (depends on resistor values) gpioSetValue(CFG_BAT_ENPORT, CFG_BAT_ENPIN, 0 ); // Turn the voltage divider back off to save power printf("%-25s : %u.%u V %s", "Supply Voltage", (unsigned int)(c / 1000), (unsigned int)(c % 1000), CFG_PRINTF_NEWLINE); #endif // Wireless Settings (if CFG_CHIBI enabled) #ifdef CFG_CHIBI chb_pcb_t *pcb = chb_get_pcb(); printf("%-25s : %s %s", "RF Transceiver", "AT86RF212", CFG_PRINTF_NEWLINE); #if CFG_CHIBI_PROMISCUOUS == 1 printf("%-25s : %s %s", "RF Receive Mode", "Promiscuous", CFG_PRINTF_NEWLINE); #else printf("%-25s : %s %s", "RF Receive Mode", "Normal", CFG_PRINTF_NEWLINE); #endif printf("%-25s : 0x%04X (%d) %s", "802.15.4 PAN ID", CFG_CHIBI_PANID, CFG_CHIBI_PANID, CFG_PRINTF_NEWLINE); printf("%-25s : 0x%04X (%d) %s", "802.15.4 Node Address", pcb->src_addr, pcb->src_addr, CFG_PRINTF_NEWLINE); printf("%-25s : %d %s", "802.15.4 Channel", CFG_CHIBI_CHANNEL, CFG_PRINTF_NEWLINE); #endif // CLI and buffer Settings #ifdef CFG_INTERFACE printf("%-25s : %d bytes %s", "Max CLI Command", CFG_INTERFACE_MAXMSGSIZE, CFG_PRINTF_NEWLINE); #endif // System Uptime (based on systick timer) printf("%-25s : %u s %s", "System Uptime", (unsigned int)systickGetSecondsActive(), CFG_PRINTF_NEWLINE); // System Temperature (if LM75B Present) #ifdef CFG_LM75B int32_t temp = 0; lm75bGetTemperature(&temp); temp *= 125; printf("%-25s : %d.%d C %s", "Temperature", (int)(temp / 1000), (int)(temp % 1000), CFG_PRINTF_NEWLINE); #endif #ifdef CFG_SDCARD printf("%-25s : %s %s", "SD Card Present", gpioGetValue(CFG_SDCARD_CDPORT, CFG_SDCARD_CDPIN) ? "True" : "False", CFG_PRINTF_NEWLINE); #endif }
pn532_error_t pn532_mifareclassic_AuthenticateBlock (byte_t * pbtCUID, size_t szCUIDLen, uint32_t uiBlockNumber, uint8_t uiKeyType, byte_t * pbtKeys) { pn532_error_t error; byte_t abtCommand[17]; byte_t abtResponse[PN532_RESPONSELEN_INDATAEXCHANGE]; size_t szLen; #ifdef PN532_DEBUGMODE PN532_DEBUG("Trying to authenticate card "); pn532PrintHex(pbtCUID, szCUIDLen); #endif /* Prepare the authentication command */ abtCommand[0] = PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ abtCommand[1] = 1; /* Max card numbers */ abtCommand[2] = (uiKeyType) ? PN532_MIFARE_CMD_AUTH_A : PN532_MIFARE_CMD_AUTH_B; abtCommand[3] = uiBlockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ memcpy (abtCommand+4, pbtKeys, 6); uint8_t i; for (i = 0; i < szCUIDLen; i++) { abtCommand[10+i] = pbtCUID[i]; /* 4 byte card ID */ } /* Send the command */ error = pn532Write(abtCommand, 10+szCUIDLen); if (error) { /* Problem with the serial bus, etc. */ #ifdef PN532_DEBUGMODE PN532_DEBUG("Authentification failed%s", CFG_PRINTF_NEWLINE); #endif return error; } /* Read the authentication response */ memset(abtResponse, 0, PN532_RESPONSELEN_INDATAEXCHANGE); do { systickDelay(25); error = pn532Read(abtResponse, &szLen); } while (error == PN532_ERROR_RESPONSEBUFFEREMPTY); if (error) { #ifdef PN532_DEBUGMODE PN532_DEBUG("Authentification failed%s", CFG_PRINTF_NEWLINE); #endif return error; } // ToDo: How to check if authentification really worked (bad key, etc.)? /* Output the authentification data */ #ifdef PN532_DEBUGMODE PN532_DEBUG("Authenticated block %d %s", uiBlockNumber, CFG_PRINTF_NEWLINE); #endif // Return OK signal return PN532_ERROR_NONE; }
void samsungvfdHome(void) { samsungvfd_command(SAMSUNGVFD_RETURNHOME); // Set cursor position to zero systickDelay(2); // this command takes a long time! }
void st7735InitDisplay(void) { st7735WriteCmd(ST7735_SWRESET); // software reset systickDelay(50); st7735WriteCmd(ST7735_SLPOUT); // out of sleep mode systickDelay(500); st7735WriteCmd(ST7735_COLMOD); // set color mode st7735WriteData(0x05); // 16-bit color systickDelay(10); st7735WriteCmd(ST7735_FRMCTR1); // frame rate control st7735WriteData(0x00); // fastest refresh st7735WriteData(0x06); // 6 lines front porch st7735WriteData(0x03); // 3 lines backporch systickDelay(10); st7735WriteCmd(ST7735_MADCTL); // memory access control (directions) st7735WriteData(0xC8); // row address/col address, bottom to top refresh st7735WriteCmd(ST7735_DISSET5); // display settings #5 st7735WriteData(0x15); // 1 clock cycle nonoverlap, 2 cycle gate rise, 3 cycle oscil. equalize st7735WriteData(0x02); // fix on VTL st7735WriteCmd(ST7735_INVCTR); // display inversion control st7735WriteData(0x0); // line inversion st7735WriteCmd(ST7735_PWCTR1); // power control st7735WriteData(0x02); // GVDD = 4.7V st7735WriteData(0x70); // 1.0uA systickDelay(10); st7735WriteCmd(ST7735_PWCTR2); // power control st7735WriteData(0x05); // VGH = 14.7V, VGL = -7.35V st7735WriteCmd(ST7735_PWCTR3); // power control st7735WriteData(0x01); // Opamp current small st7735WriteData(0x02); // Boost frequency st7735WriteCmd(ST7735_VMCTR1); // power control st7735WriteData(0x3C); // VCOMH = 4V st7735WriteData(0x38); // VCOML = -1.1V systickDelay(10); st7735WriteCmd(ST7735_PWCTR6); // power control st7735WriteData(0x11); st7735WriteData(0x15); st7735WriteCmd(ST7735_GMCTRP1); st7735WriteData(0x09); st7735WriteData(0x16); st7735WriteData(0x09); st7735WriteData(0x20); st7735WriteData(0x21); st7735WriteData(0x1B); st7735WriteData(0x13); st7735WriteData(0x19); st7735WriteData(0x17); st7735WriteData(0x15); st7735WriteData(0x1E); st7735WriteData(0x2B); st7735WriteData(0x04); st7735WriteData(0x05); st7735WriteData(0x02); st7735WriteData(0x0E); st7735WriteCmd(ST7735_GMCTRN1); st7735WriteData(0x0B); st7735WriteData(0x14); st7735WriteData(0x08); st7735WriteData(0x1E); st7735WriteData(0x22); st7735WriteData(0x1D); st7735WriteData(0x18); st7735WriteData(0x1E); st7735WriteData(0x1B); st7735WriteData(0x1A); st7735WriteData(0x24); st7735WriteData(0x2B); st7735WriteData(0x06); st7735WriteData(0x06); st7735WriteData(0x02); st7735WriteData(0x0F); systickDelay(10); st7735WriteCmd(ST7735_CASET); // column addr set st7735WriteData(0x00); st7735WriteData(0x02); // XSTART = 2 st7735WriteData(0x00); st7735WriteData(0x81); // XEND = 129 st7735WriteCmd(ST7735_RASET); // row addr set st7735WriteData(0x00); st7735WriteData(0x02); // XSTART = 1 st7735WriteData(0x00); st7735WriteData(0x81); // XEND = 160 st7735WriteCmd(ST7735_NORON); // normal display on systickDelay(10); st7735WriteCmd(ST7735_DISPON); systickDelay(500); }
char alphaHandleTouchEvent(void) { tsTouchData_t data; char result = '\0'; uint8_t row, col; int32_t tsError = -1; // Blocking delay until a valie touch event occurs while (tsError) { tsError = tsWaitForEvent(&data, 0); } // Attempt to convert touch data to char if ((data.y < ALPHA_ROW1_TOP) || (data.y > ALPHA_ROW6_TOP + ALPHA_BTN_HEIGHT)) { return result; } // Get column if ((data.x > alphaBtnX[0]) && (data.x < alphaBtnX[0] + ALPHA_BTN_WIDTH)) col = 0; else if ((data.x > alphaBtnX[1]) && (data.x < alphaBtnX[1] + ALPHA_BTN_WIDTH)) col = 1; else if ((data.x > alphaBtnX[2]) && (data.x < alphaBtnX[2] + ALPHA_BTN_WIDTH)) col = 2; else if ((data.x > alphaBtnX[3]) && (data.x < alphaBtnX[3] + ALPHA_BTN_WIDTH)) col = 3; else if ((data.x > ALPHA_COL5_LEFT) && (data.x < ALPHA_COL5_LEFT + ALPHA_BTN_WIDTH)) col = 4; else return result; // Get row if ((data.y > ALPHA_ROW1_TOP) && (data.y < ALPHA_ROW1_TOP + ALPHA_BTN_HEIGHT)) row = 0; else if ((data.y > ALPHA_ROW2_TOP) && (data.y < ALPHA_ROW2_TOP + ALPHA_BTN_HEIGHT)) row = 1; else if ((data.y > ALPHA_ROW3_TOP) && (data.y < ALPHA_ROW3_TOP + ALPHA_BTN_HEIGHT)) row = 2; else if ((data.y > ALPHA_ROW4_TOP) && (data.y < ALPHA_ROW4_TOP + ALPHA_BTN_HEIGHT)) row = 3; else if ((data.y > ALPHA_ROW5_TOP) && (data.y < ALPHA_ROW5_TOP + ALPHA_BTN_HEIGHT)) row = 4; else if ((data.y > ALPHA_ROW6_TOP) && (data.y < ALPHA_ROW6_TOP + ALPHA_BTN_HEIGHT)) row = 5; else return result; // Match found ... update button and process the results alphaRenderButton(alphaPage, col, row, true); result = alphaKeys[alphaPage][row][col]; if (result == '<') { // Trim character if backspace was pressed if (alphaString_ptr > alphaString) { alphaString_ptr--; *alphaString_ptr = '\0'; } } else if (result == '*') { // Switch page if the shift button was pressed alphaPage++; if (alphaPage > 3) { alphaPage = 0; } // Update the UI alphaRefreshScreen(); } else if (result == '>') { // OK button systickDelay(CFG_TFTLCD_TS_KEYPADDELAY); return '>'; } else { // Add text to string buffer *alphaString_ptr++ = result; } // Brief delay systickDelay(CFG_TFTLCD_TS_KEYPADDELAY); // Return button to deselected state alphaRefreshScreen(); return result; }
void samsungvfdClear(void) { samsungvfd_command(SAMSUNGVFD_CLEARDISPLAY); // clear display, set cursor position to zero systickDelay(2); // this command takes a long time! }
static void checkBatteryStatus(void){ GPIO2DATA ^= _BV(0); /* led0 on */ if(getMonitorStatus() == 1) wakeBq29312a(); writeBQ29312A(FUNCTION_CTL, VMEN); /* enable voltage monitoring */ /* -------------------------------------------- */ //writeBQ29312A(CELL_SEL, 0b00001100); //systickDelay(1); //battery.vref = (float)((float)adcRead(1) * (float)(3.3/1024)); //writeBQ29312A(CELL_SEL, VC4_5); //systickDelay(1); //battery.vo4_5 = (float)((float)adcRead(1) * (float)(3.3/1024)); //writeBQ29312A(CELL_SEL, 0b00001000); //systickDelay(1); //battery.voutr = (float)((float)adcRead(1) * (float)(3.3/1024)); /* -------------------------------------------- */ /* Measure Cell Voltage */ writeBQ29312A(CELL_SEL, VC4_5); systickDelay(1); battery.adcf[0] = (float)adcRead(1) * (float)(3.3/1024); writeBQ29312A(CELL_SEL, VC4_3); systickDelay(1); battery.adcf[1] = (float)adcRead(1) * (float)(3.3/1024); writeBQ29312A(CELL_SEL, VC3_2); systickDelay(1); battery.adcf[2] = (float)adcRead(1) * (float)(3.3/1024); writeBQ29312A(FUNCTION_CTL, VMEN | PACKOUT); //uint8_t chgFet = getFetState(FET_CHG); //setChgFet(0); //setChgPin(0); // chg circuit on systickDelay(8); battery.adcf[3] = (float)adcRead(1) * (float)(3.3/1024); //setChgPin(1); // chg curcuit off //setChgFet(chgFet); /* Covert Cell Voltage from adc value *but this value is little inaccuracy */ battery.vcellf[0] = (0.975 - battery.adcf[0]) / 0.15; battery.vcellf[1] = (0.975 - battery.adcf[1]) / 0.15; battery.vcellf[2] = (0.975 - battery.adcf[2]) / 0.15; battery.vcellf[3] = battery.adcf[3] * 25; //battery.vcellf[0] = (float)(-0.15 * battery.adcf[0] + 0.975); //battery.kact = (battery.vo4_5 - battery.voutr) / battery.vref; //battery.vosact = (battery.vo4_5 - battery.vref) / (1 + battery.kact); //battery.res = (battery.adcf[0] - battery.vcellf[0]) / battery.kact; //float res = (vref + ( 1 + kact ) * vosact - adcf[0] ) / kact - ( vo4_5 - adcf[0] ) / kact; //float res = (-adcf[0] + vref ) / kact; /* Check DC_in status */ if(battery.vcellf[3] > 12.6){ /* DC connected */ dc.stat = 1; GPIO2DATA &= ~(_BV(9)); /* led9 on */ }else{ /* DC disconnected */ dc.stat = 0; GPIO2DATA |= _BV(9); /* led9 off */ } /* When DCin is disconnect. CHG FET set to ON for reduce the voltage drop */ if(dc.stat == 0) setChgFet(1); if(dc.stat == 1) setChgFet(0); //if(dc.stat == 1){ // setDsgFet(0); //} /* Low voltage alert */ if( (battery.vcellf[0] <= 3.6) || (battery.vcellf[1] <= 3.6) || (battery.vcellf[2] <= 3.6) ) { battery.batlow = 1; GPIO2DATA |= _BV(0); // led0 off } else { battery.batlow = 0; } /* Low voltage shutdown */ if( (battery.vcellf[0] <= 3.4) || (battery.vcellf[1] <= 3.4) || (battery.vcellf[2] <= 3.4) ) { setDCDC(0); setDsgFet(0); sleepBq29312a(); shipBq29312a(); /* BQ29312A enter ShipMode. The way to return to NormalMode is only HardwareReset */ __disable_irq(); /* Todo: lpc1114 will enter deepsleep mode */ } else { setDCDC(1); setDsgFet(1); } battery.checkBattery = 0; }
pn532_error_t pn532_mifareclassic_WaitForPassiveTarget (byte_t * pbtCUID, size_t * szCUIDLen) { byte_t abtResponse[PN532_RESPONSELEN_INLISTPASSIVETARGET]; pn532_error_t error; size_t szLen; #ifdef PN532_DEBUGMODE PN532_DEBUG("Waiting for an ISO14443A Card%s", CFG_PRINTF_NEWLINE); #endif /* Try to initialise a single ISO14443A tag at 106KBPS */ /* Note: To wait for a card with a known UID, append the four byte */ /* UID to the end of the command. */ byte_t abtCommand[] = { PN532_COMMAND_INLISTPASSIVETARGET, 0x01, PN532_MODULATION_ISO14443A_106KBPS}; error = pn532Write(abtCommand, sizeof(abtCommand)); if (error) return error; /* Wait until we get a valid response or a timeout */ do { systickDelay(25); error = pn532Read(abtResponse, &szLen); } while (error == PN532_ERROR_RESPONSEBUFFEREMPTY); if (error) return error; /* Check SENSE_RES to make sure this is a Mifare Classic card */ /* Classic 1K = 00 04 */ /* Classic 4K = 00 02 */ /* Classic Emulated = 00 08 */ if ((abtResponse[10] == 0x02) || (abtResponse[10] == 0x04) || (abtResponse[10] == 0x08)) { /* Card appears to be Mifare Classic */ *szCUIDLen = abtResponse[12]; uint8_t i; for (i=0; i < *szCUIDLen; i++) { pbtCUID[i] = abtResponse[13+i]; } #ifdef PN532_DEBUGMODE PN532_DEBUG("Card Found: %s", CFG_PRINTF_NEWLINE); PN532_DEBUG(" ATQA: "); pn532PrintHex(abtResponse+9, 2); PN532_DEBUG(" SAK: %02x%s", abtResponse[11], CFG_PRINTF_NEWLINE); PN532_DEBUG(" UID: "); pn532PrintHex(pbtCUID, *szCUIDLen); #endif } else { /* Card is ISO14443A but doesn't appear to be Mifare Classic */ /* Mifare Ultralight = 0x0044 */ /* Mifare DESFire = 0x0344 */ /* Innovision Jewel = 0x0C00 */ #ifdef PN532_DEBUGMODE PN532_DEBUG("Wrong Card Type (Expected ATQA 00 02, 00 04 or 00 08) %s%s", CFG_PRINTF_NEWLINE, CFG_PRINTF_NEWLINE); PN532_DEBUG(" ATQA : "); pn532PrintHex(abtResponse+9, 2); PN532_DEBUG(" SAK : %02x%s", abtResponse[11], CFG_PRINTF_NEWLINE); PN532_DEBUG(" UID Length : %d%s", abtResponse[12], CFG_PRINTF_NEWLINE); PN532_DEBUG(" UID : "); size_t pos; for (pos=0; pos < abtResponse[12]; pos++) { printf("%02x ", abtResponse[13 + pos]); } printf("%s%s", CFG_PRINTF_NEWLINE, CFG_PRINTF_NEWLINE); #endif return PN532_ERROR_WRONGCARDTYPE; } return PN532_ERROR_NONE; }
int main (void) { uint8_t i; dc.stat = 0; dc.count = 0; switch_flag = 0; /* misc I/Os Direction regs */ gpioSetDir(3, 0, 1); // DC-DC Convertor EN gpioSetDir(3, 1, 1); // Chg Disable pin gpioSetDir(3, 2, 1); // Low Chg pin gpioSetDir(1, 11, 0); // Switch /* LED Array Direction regs set to output */ gpioSetDir(2, 0, 1); // led no.0 the most left led. gpioSetDir(2, 1, 1); // led no.1 gpioSetDir(2, 2, 1); // led no.2 gpioSetDir(2, 3, 1); // led no.3 gpioSetDir(2, 4, 1); // led no.4 gpioSetDir(2, 5, 1); // led no.5 gpioSetDir(2, 6, 1); // led no.6 gpioSetDir(2, 7, 1); // led no.7 gpioSetDir(2, 8, 1); // led no.8 gpioSetDir(2, 9, 1); // led no.9 setChgPin(1); /* charge circuit off */ setLowChgPin(0); /* low charge off */ /* Enable SysTick timer in interval of 1 ms */ SYST_RVR = F_CPU / 1000 - 1; SYST_CSR = 0x07; initBQ29312A(); /* Initialize BQ29312A */ /* DON'T DSG FET TURN TO OFF WHEN DCIN DOESN'T CONNECT */ setDsgFet(1); // DSG FET turn to ON /* DON'T CHG FET TURN TO ON WHEN CONNECTING BATTERY */ setChgFet(0); // CHG FET turn to OFF GPIO2DATA = 0xffffff; /* all leds off */ setDCDC(1); /* Start DC5V output */ /* flash all LEDs */ for(i = 0; i <= 5; i++) { GPIO2DATA ^= ( _BV(0) | _BV(1) | _BV(2) | _BV(3) | _BV(4) | _BV(5) | _BV(6) | _BV(7) | _BV(8) | _BV(9) ); systickDelay(100); } /* Initialize ADC module */ adcInit(); checkBatteryStatus(); /* Call setBatteryFlag() 2000ms interval */ setTimer32Interval(1, 2000, setBatteryFlag); __enable_irqn(CT32B1_IRQn); /* Enale GPIO1-11 interrupt for battery check switch */ GPIO1IS &= ~(_BV(11)); /* Interrupt is Edge Sense */ GPIO1IBE &= ~(_BV(11)); /* Interrupt is Single Triger */ GPIO1IEV &= ~(_BV(11)); /* Select interrupt source (falling edge of P1.11 pin) */ GPIO1IE = _BV(11); /* Unmask interrupt of P1.11 pin */ __enable_irqn(PIO_1_IRQn); /* Enable PIO1 interrupt */ for (;;) { if(battery.checkBattery) checkBatteryStatus(); if(switch_flag) showBatteryLevel(); __WFI(); /* Wait for interrupt */ } }
pn532_error_t pn532_mifareclassic_ReadDataBlock (uint8_t uiBlockNumber, byte_t * pbtData) { pn532_error_t error; byte_t abtCommand[4]; byte_t abtResponse[PN532_RESPONSELEN_INDATAEXCHANGE]; size_t szLen; #ifdef PN532_DEBUGMODE PN532_DEBUG("Reading 16 bytes at block %03d%s", uiBlockNumber, CFG_PRINTF_NEWLINE); #endif /* Prepare the command */ abtCommand[0] = PN532_COMMAND_INDATAEXCHANGE; abtCommand[1] = 1; /* Card number */ abtCommand[2] = PN532_MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ abtCommand[3] = uiBlockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ /* Send the commands */ error = pn532Write(abtCommand, sizeof(abtCommand)); if (error) { /* Bus error, etc. */ #ifdef PN532_DEBUGMODE PN532_DEBUG("Read failed%s", CFG_PRINTF_NEWLINE); #endif return error; } /* Read the response */ memset(abtResponse, 0, PN532_RESPONSELEN_INDATAEXCHANGE); do { systickDelay(50); error = pn532Read(abtResponse, &szLen); } while (error == PN532_ERROR_RESPONSEBUFFEREMPTY); if (error) { #ifdef PN532_DEBUGMODE PN532_DEBUG("Read failed%s", CFG_PRINTF_NEWLINE); #endif return error; } /* Make sure we have a valid response (should be 26 bytes long) */ if (szLen == 26) { /* Copy the 16 data bytes to the output buffer */ /* Block content starts at byte 9 of a valid response */ memcpy (pbtData, abtResponse+8, 16); } else { #ifdef PN532_DEBUGMODE PN532_DEBUG("Unexpected response reading block %d. Bad key?%s", uiBlockNumber, CFG_PRINTF_NEWLINE); #endif return PN532_ERROR_BLOCKREADFAILED; } /* Display data for debug if requested */ #ifdef PN532_DEBUGMODE PN532_DEBUG("Block %03d: ", uiBlockNumber, CFG_PRINTF_NEWLINE); pn532PrintHexVerbose(pbtData, 16); #endif // Return OK signal return PN532_ERROR_NONE; }
void main_kerosin(void) { uint8_t enterCnt = 0; uint8_t buffer[64]; // cpuInit(); // Configure the CPU systickInit(CFG_SYSTICK_DELAY_IN_MS); // Start systick timer gpioInit(); // Enable GPIO pmuInit(); // Configure power management adcInit(); // Config adc pins to save power lcdInit(); DoString(10,5,"USB plug"); // Initialise USB CDC #ifdef CFG_USBCDC DoString(10, 15, "USBCDC"); lastTick = systickGetTicks(); // Used to control output/printf timing CDC_Init(); // Initialise VCOM USB_Init(); // USB Initialization USB_Connect(TRUE); // USB Connect // Wait until USB is configured or timeout occurs uint32_t usbTimeout = 0; while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) { if (USB_Configuration) break; systickDelay(10); // Wait 10ms usbTimeout++; } #endif // Printf can now be used with UART or USBCDC puts("Hello World"); DoString(10, 25, "Enter:"); lcdDisplay(); int readData; while (1) { switch (getInput()) { case BTN_ENTER: enterCnt++; puts("ENTER\t"); buffer[0] = '0' + enterCnt; buffer[1] = 0; puts(buffer); puts("\r\n"); DoInt(50, 25, (int) (enterCnt)); lcdDisplay(); break; case BTN_LEFT: readData = CDC_GetInputBuffer(buffer, sizeof(buffer)); DoString(5, 40, buffer); DoString(1, 50, "Received: "); DoInt(57, 50, readData); lcdDisplay(); break; default: break; } } }