/* a fiber busy waits, then reports through a fifo */ static void test_fiber_busy_wait(int ticks, int unused) { ARG_UNUSED(unused); uint32_t usecs = ticks * sys_clock_us_per_tick; TC_PRINT(" fiber busy waiting for %d usecs (%d ticks)\n", usecs, ticks); sys_thread_busy_wait(usecs); TC_PRINT(" fiber busy waiting completed\n"); /* * Ideally the test should verify that the correct number of ticks * have elapsed. However, when run under QEMU the tick interrupt * may be processed on a very irregular basis, meaning that far * fewer than the expected number of ticks may occur for a given * number of clock cycles vs. what would ordinarily be expected. * * Consequently, the best we can do for now to test busy waiting is * to invoke the API and verify that it returns. (If it takes way * too long, or never returns, the main test task may be able to * time out and report an error.) */ nano_fiber_sem_give(&reply_timeout); }
int bmg160_init(struct device *dev) { struct bmg160_device_config *cfg = dev->config->config_info; struct bmg160_device_data *bmg160 = dev->driver_data; uint8_t chip_id = 0; uint16_t range_dps; bmg160->i2c = device_get_binding((char *)cfg->i2c_port); if (!bmg160->i2c) { SYS_LOG_DBG("I2C master controller not found!"); return -EINVAL; } nano_sem_init(&bmg160->sem); nano_sem_give(&bmg160->sem); if (bmg160_read_byte(dev, BMG160_REG_CHIPID, &chip_id) < 0) { SYS_LOG_DBG("Failed to read chip id."); return -EIO; } if (chip_id != BMG160_CHIP_ID) { SYS_LOG_DBG("Unsupported chip detected (0x%x)!", chip_id); return -ENODEV; } /* reset the chip */ bmg160_write_byte(dev, BMG160_REG_BGW_SOFTRESET, BMG160_RESET); sys_thread_busy_wait(1000); /* wait for the chip to come up */ if (bmg160_write_byte(dev, BMG160_REG_RANGE, BMG160_DEFAULT_RANGE) < 0) { SYS_LOG_DBG("Failed to set range."); return -EIO; } range_dps = bmg160_gyro_range_map[BMG160_DEFAULT_RANGE]; bmg160->scale = BMG160_RANGE_TO_SCALE(range_dps); if (bmg160_write_byte(dev, BMG160_REG_BW, BMG160_DEFAULT_ODR) < 0) { SYS_LOG_DBG("Failed to set sampling frequency."); return -EIO; } /* disable interrupts */ if (bmg160_write_byte(dev, BMG160_REG_INT_EN0, 0) < 0) { SYS_LOG_DBG("Failed to disable all interrupts."); return -EIO; } #ifdef CONFIG_BMG160_TRIGGER bmg160_trigger_init(dev); #endif dev->driver_api = &bmg160_api; return 0; }
/********************* * Generic functions * ********************/ static void _usleep(uint32_t usec) { static void (*func[3])(int32_t timeout_in_ticks) = { NULL, fiber_sleep, task_sleep, }; if (sys_execution_context_type_get() == 0) { sys_thread_busy_wait(usec); return; } /* Timeout in ticks: */ usec = USEC(usec); /** Most likely usec will generate 0 ticks, * so setting at least to 1 */ if (!usec) { usec = 1; } func[sys_execution_context_type_get()](usec); }
/******************************************************************************* * delay - delay in microseconds * * * Arguments: ms - delay value * * Returns: void */ void delayMicroseconds(unsigned int us) { sys_thread_busy_wait(us); }
/******************************************************************************* * delay - delay in milliseconds * * * Arguments: ms - delay value * * Returns: void */ void delay(unsigned long ms) { sys_thread_busy_wait(ms*1000); }
void main(void) { struct device *dev; u32_t baudrate, dtr = 0; int ret; dev = device_get_binding(CONFIG_CDC_ACM_PORT_NAME); if (!dev) { SYS_LOG_ERR("CDC ACM device not found"); return; } SYS_LOG_DBG("Wait for DTR"); while (1) { uart_line_ctrl_get(dev, LINE_CTRL_DTR, &dtr); if (dtr) break; } uart_dev = dev; SYS_LOG_DBG("DTR set, continue"); #if CONFIG_DCD_DSR /* They are optional, we use them to test the interrupt endpoint */ ret = uart_line_ctrl_set(dev, LINE_CTRL_DCD, 1); if (ret) printk("Failed to set DCD, ret code %d\n", ret); ret = uart_line_ctrl_set(dev, LINE_CTRL_DSR, 1); if (ret) printk("Failed to set DSR, ret code %d\n", ret); /* Wait 1 sec for the host to do all settings */ sys_thread_busy_wait(1000000); #endif ret = uart_line_ctrl_get(dev, LINE_CTRL_BAUD_RATE, &baudrate); if (ret) printk("Failed to get baudrate, ret code %d\n", ret); else printk("Baudrate detected: %d\n", baudrate); SYS_LOG_INF("USB serial initialized"); /* Initialize net_pkt */ net_pkt_init(); /* Initialize RX queue */ init_rx_queue(); /* Initialize TX queue */ init_tx_queue(); /* Initialize ieee802154 device */ if (!init_ieee802154()) { SYS_LOG_ERR("Unable to initialize ieee802154"); return; }; uart_irq_callback_set(dev, interrupt_handler); /* Enable rx interrupts */ uart_irq_rx_enable(dev); /* Enable tx interrupts */ uart_irq_tx_enable(dev); }