void delay( uint32_t ms ) { if (ms == 0) return; uint32_t start = systickGetTicks(); do { yield(); } while (systickGetTicks() - start < ms); }
tsTouchError_t tsWaitForEvent(tsTouchData_t* data, uint32_t timeoutMS) { if (!_tsInitialised) tsInit(); tsRead(data, false); // Return the results right away if reading is valid if (data->valid) { return TS_ERROR_NONE; } // Handle timeout if delay > 0 milliseconds if (timeoutMS) { uint32_t startTick = systickGetTicks(); // Systick rollover may occur while waiting for timeout if (startTick > 0xFFFFFFFF - timeoutMS) { while (data->valid == false) { // Throw alert if timeout delay has been passed if ((systickGetTicks() < startTick) && (systickGetTicks() >= (timeoutMS - (0xFFFFFFFF - startTick)))) { return TS_ERROR_TIMEOUT; } tsRead(data, false); } } // No systick rollover will occur ... calculate timeout the simple way else { // Wait in infinite loop while (data->valid == false) { // Throw timeout if delay has been passed if ((systickGetTicks() - startTick) > timeoutMS) { return TS_ERROR_TIMEOUT; } tsRead(data, false); } } } // No timeout requested ... wait forever else { while (data->valid == false) { tsRead(data, false); } } // Indicate correct reading return TS_ERROR_NONE; }
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; }
error_t adxl345GetSensorEvent(sensors_event_t *event) { int16_t x, y, z; /* Clear the event */ memset(event, 0, sizeof(sensors_event_t)); event->version = sizeof(sensors_event_t); event->sensor_id = _adxl345SensorID; event->type = SENSOR_TYPE_ACCELEROMETER; event->timestamp = systickGetTicks(); /* Retrieve values from the sensor */ ASSERT_STATUS(adxl345GetXYZ(&x, &y, &z)); /* The ADXL345 returns a raw value where each lsb represents 4mg. To * convert this to a normal g value, multiply by 0.004 and then convert * it to the m/s^2 value that sensors_event_t is expecting. */ event->acceleration.x = x * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; event->acceleration.y = y * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; event->acceleration.z = z * ADXL345_MG2G_MULTIPLIER * SENSORS_GRAVITY_STANDARD; printf(" %08d %08d %08d%s", x, y, z, CFG_PRINTF_NEWLINE); return ERROR_NONE; }
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 }
void usbHIDGetInReport (uint8_t src[], uint32_t length) { usbhid_out_t out; out.gpio1Dir = GPIO_GPIO1DIR; out.gpio1Data = GPIO_GPIO1DATA; out.gpio2Dir = GPIO_GPIO2DIR; out.gpio2Data = GPIO_GPIO2DATA; out.gpio3Dir = GPIO_GPIO3DIR; out.gpio3Data = GPIO_GPIO3DATA; out.adc0 = adcRead(0); out.adc1 = adcRead(1); out.adc2 = adcRead(2); out.adc3 = adcRead(3); out.systicks = systickGetTicks(); out.rollovers = systickGetRollovers(); size_t i = 0; memcpy(&src[i], &out.gpio1Dir, sizeof out.gpio1Dir); i += sizeof out.gpio1Dir; memcpy(&src[i], &out.gpio1Data, sizeof out.gpio1Data); i += sizeof out.gpio1Data; memcpy(&src[i], &out.gpio2Dir, sizeof out.gpio2Dir); i += sizeof out.gpio2Dir; memcpy(&src[i], &out.gpio2Data, sizeof out.gpio2Data); i += sizeof out.gpio2Data; memcpy(&src[i], &out.gpio3Dir, sizeof out.gpio3Dir); i += sizeof out.gpio3Dir; memcpy(&src[i], &out.gpio3Data, sizeof out.gpio3Data); i += sizeof out.gpio3Data; memcpy(&src[i], &out.adc0, sizeof out.adc0); i += sizeof out.adc0; memcpy(&src[i], &out.adc1, sizeof out.adc1); i += sizeof out.adc1; memcpy(&src[i], &out.adc2, sizeof out.adc2); i += sizeof out.adc2; memcpy(&src[i], &out.adc3, sizeof out.adc3); i += sizeof out.adc3; memcpy(&src[i], &out.systicks, sizeof out.systicks); i += sizeof out.systicks; memcpy(&src[i], &out.rollovers, sizeof out.rollovers); i += sizeof out.rollovers; }
error_t lm75bGetSensorEvent(sensors_event_t *event) { int32_t temp; /* Clear the event */ memset(event, 0, sizeof(sensors_event_t)); event->version = sizeof(sensors_event_t); event->sensor_id = _lm75bSensorID; event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; event->timestamp = systickGetTicks(); /* Retrieve values from the sensor */ ASSERT_STATUS(lm75bGetTemperature(&temp)); event->temperature = temp * 0.125F; /* 0.125 per lsb */ return ERROR_NONE; }
uint32_t millis(void) { return systickGetTicks(); }
uint32_t micros(void) { //TODO: return systickGetTicks() * 1000; }
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; } } }
tsTouchError_t tsWaitForEvent(tsTouchData_t* data, uint32_t timeoutMS) { if (!_tsInitialised) tsInit(); uint32_t z1, z2; uint32_t xRaw1, xRaw2, yRaw1, yRaw2; z1 = z2 = 0; // Handle timeout if delay > 0 milliseconds if (timeoutMS) { uint32_t startTick = systickGetTicks(); // Systick rollover may occur while waiting for timeout if (startTick > 0xFFFFFFFF - timeoutMS) { // Wait for timeout or touch event while (z2 < CFG_TFTLCD_TS_THRESHOLD) { // Throw alert if timeout delay has been passed if ((systickGetTicks() < startTick) && (systickGetTicks() >= (timeoutMS - (0xFFFFFFFF - startTick)))) { return TS_ERROR_TIMEOUT; } tsReadZ(&z1, &z2); } } // No systick rollover will occur ... calculate timeout the simple way else { // Wait in infinite loop while (z2 < CFG_TFTLCD_TS_THRESHOLD) { // Throw timeout if delay has been passed if ((systickGetTicks() - startTick) > timeoutMS) { return TS_ERROR_TIMEOUT; } tsReadZ(&z1, &z2); } } } // No timeout requested ... wait forever else { while (z2 < CFG_TFTLCD_TS_THRESHOLD) { tsReadZ(&z1, &z2); } } // Get raw conversion results // Each value is read twice and compared to avoid erroneous readings xRaw1 = tsReadY(); // X and Y are reversed xRaw2 = tsReadY(); // X and Y are reversed yRaw1 = tsReadX(); // X and Y are reverse yRaw2 = tsReadX(); // X and Y are reverse // If both read attempts aren't identical, return mismatch error if ((xRaw1 != xRaw2) || (yRaw1 != yRaw2)) { return TS_ERROR_XYMISMATCH; } // Normalise X data->x = ((xRaw1 - _calibration.offsetLeft > TS_ADC_LIMIT ? 0 : xRaw1 - _calibration.offsetLeft) * 100) / _calibration.divisorX; if (data->x > lcdGetWidth() - 1) data->x = lcdGetWidth() - 1; // Normalise Y data->y = ((yRaw1 - _calibration.offsetTop > TS_ADC_LIMIT ? 0 : yRaw1 - _calibration.offsetTop) * 100) / _calibration.divisorY; if (data->y > lcdGetHeight() - 1) data->y = lcdGetHeight() - 1; // Indicate correct reading return TS_ERROR_NONE; }
int main(void) { // Configure cpu and mandatory peripherals systemInit(); // Check if 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 set to 1 in projectconfig.h for this example" #endif #if defined CFG_INTERFACE #error "CFG_INTERFACE must be disabled in projectconfig.h for this example" #endif #if defined CFG_CHIBI && CFG_CHIBI_PROMISCUOUS != 0 // Get a reference to the Chibi peripheral control block chb_pcb_t *pcb = chb_get_pcb(); // Wait for incoming frames and transmit the raw data over uart while(1) { // Check for incoming messages while (pcb->data_rcv) { // get the length of the data rx_data.len = chb_read(&rx_data); // make sure the length is nonzero if (rx_data.len) { // Enable LED to indicate message reception gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_ON); // Send raw data the to PC for processing using wsbridge uint8_t i; for (i=0; i<rx_data.len; i++) { #ifdef CFG_PRINTF_UART uartSendByte(rx_data.data[i]); #endif #ifdef CFG_PRINTF_USBCDC // ToDo: This really needs to be refactored! if (USB_Configuration) { cdcBufferWrite(rx_data.data[i]); // 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 64 byte chunks until end of data bytesRead = cdcBufferReadLen(frame, 64); // debug_printf("%d,", bytesRead); USB_WriteEP (CDC_DEP_IN, frame, bytesRead); systickDelay(1); } lastTick = currentTick; } } #endif } // Disable LED gpioSetValue (CFG_LED_PORT, CFG_LED_PIN, CFG_LED_OFF); } } } #endif return 0; }