static int gpiopwm_set_off(SYSCTLFN_ARGS) { struct sysctlnode node; struct gpiopwm_softc *sc; int val, error; node = *rnode; sc = node.sysctl_data; callout_halt(&sc->sc_pulse, NULL); gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); node.sysctl_data = &val; val = sc->sc_ticks_off; error = sysctl_lookup(SYSCTLFN_CALL(&node)); if (error || newp == NULL) return error; sc->sc_ticks_off = val; if (sc->sc_ticks_on > 0 && sc->sc_ticks_off > 0) { gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH); callout_schedule(&sc->sc_pulse, sc->sc_ticks_on); } return 0; }
void gpioiic_bb_set_bits(void *cookie, uint32_t bits) { struct gpioiic_softc *sc = cookie; gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_sda, bits & GPIOIIC_SDA ? GPIO_PIN_HIGH : GPIO_PIN_LOW); gpio_pin_write(sc->sc_gpio, &sc->sc_map, sc->sc_pin_scl, bits & GPIOIIC_SCL ? GPIO_PIN_HIGH : GPIO_PIN_LOW); }
static void gpiopwm_pulse(void *arg) { struct gpiopwm_softc *sc; sc = arg; if (gpio_pin_read(sc->sc_gpio, &sc->sc_map, 0) == GPIO_PIN_HIGH) { gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); callout_schedule(&sc->sc_pulse, sc->sc_ticks_off); } else { gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_HIGH); callout_schedule(&sc->sc_pulse, sc->sc_ticks_on); } }
void send_rgb(struct device *gpio_dev, uint32_t rgb) { int i; for (i = 0; i < 32; i++) { /* MSB goes in first */ gpio_pin_write(gpio_dev, GPIO_DATA_PIN, !!(rgb & 0x80000000)); /* Latch data into LED */ gpio_pin_write(gpio_dev, GPIO_CLK_PIN, 1); gpio_pin_write(gpio_dev, GPIO_CLK_PIN, 0); rgb <<= 1; } }
void notmain() { led_init(); gpio_init(); gpio_set_pullup(GPIO_PIN23); gpio_set_output(GPIO_PIN23); while (1) { int i; gpio_pin_write(GPIO_PIN23, 0); for (i = 0; i < 100; i++) {} gpio_pin_write(GPIO_PIN23, 1); for (i = 0; i < 100; i++) {} } }
static void trigger_callback(struct device *dev, int enable_cb) { gpio_pin_write(dev, PIN_OUT, 0); k_sleep(100); cb_cnt[0] = 0; cb_cnt[1] = 0; if (enable_cb == 1) { gpio_pin_enable_callback(dev, PIN_IN); } else { gpio_pin_disable_callback(dev, PIN_IN); } k_sleep(100); gpio_pin_write(dev, PIN_OUT, 1); k_sleep(1000); }
/* * This program toggles PIN IO 8 so if you connect an LED to that pin you * should see something. */ void main(void) { struct device *gpio_device; struct nano_timer timer; void *timer_data[1]; int ret; int pin_state = 1; nano_timer_init(&timer, timer_data); gpio_device = device_get_binding(GPIO_DRV_NAME); if (!gpio_device) { return; } ret = gpio_pin_configure(gpio_device, GPIO_OUT_PIN, (GPIO_DIR_OUT)); if (ret) { return; } while (1) { gpio_pin_write(gpio_device, GPIO_OUT_PIN, pin_state); pin_state = !pin_state; nano_timer_start(&timer, SECONDS(1)); nano_timer_test(&timer, TICKS_UNLIMITED); } }
void main(void) { struct nano_timer timer; void *timer_data[1]; struct device *gpio_dev; int ret; int toggle = 1; nano_timer_init(&timer, timer_data); gpio_dev = device_get_binding(GPIO_DRV_NAME); if (!gpio_dev) { printk("Cannot find %s!\n", GPIO_DRV_NAME); } /* Setup GPIO output */ ret = gpio_pin_configure(gpio_dev, GPIO_OUT_PIN, (GPIO_DIR_OUT)); if (ret) { printk("Error configuring " GPIO_NAME "%d!\n", GPIO_OUT_PIN); } /* Setup GPIO input, and triggers on rising edge. */ ret = gpio_pin_configure(gpio_dev, GPIO_INT_PIN, (GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE)); if (ret) { printk("Error configuring " GPIO_NAME "%d!\n", GPIO_INT_PIN); } gpio_init_callback(&gpio_cb, gpio_callback, BIT(GPIO_INT_PIN)); ret = gpio_add_callback(gpio_dev, &gpio_cb); if (ret) { printk("Cannot setup callback!\n"); } ret = gpio_pin_enable_callback(gpio_dev, GPIO_INT_PIN); if (ret) { printk("Error enabling callback!\n"); } while (1) { printk("Toggling " GPIO_NAME "%d\n", GPIO_OUT_PIN); ret = gpio_pin_write(gpio_dev, GPIO_OUT_PIN, toggle); if (ret) { printk("Error set " GPIO_NAME "%d!\n", GPIO_OUT_PIN); } if (toggle) { toggle = 0; } else { toggle = 1; } nano_timer_start(&timer, SLEEPTICKS); nano_timer_test(&timer, TICKS_UNLIMITED); } }
void main(void) { int cnt = 0; struct device *gpiob; gpiob = device_get_binding(PORT); gpio_pin_configure(gpiob, LED1, GPIO_DIR_OUT); gpio_pin_configure(gpiob, LED2, GPIO_DIR_OUT); while (1) { gpio_pin_write(gpiob, LED1, cnt % 2); gpio_pin_write(gpiob, LED2, (cnt + 1) % 2); task_sleep(SECONDS(1)); cnt++; } }
void gpioow_bb_set(void *arg, int value) { struct gpioow_softc *sc = arg; gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, value ? GPIO_PIN_HIGH : GPIO_PIN_LOW); }
void pwm_init(pwm_timer_t timer_id, pwm_timer_prescaler_t pwm_prescaler) { // ensure PWM outputs are outputs: const uint8_t pinA = pwm_timer_to_pin_id(timer_id,PWM_PIN_OCnA); const uint8_t pinB = pwm_timer_to_pin_id(timer_id,PWM_PIN_OCnB); gpio_pin_write(pinA, false); gpio_pin_mode(pinA, OUTPUT); gpio_pin_write(pinB, false); gpio_pin_mode(pinB, OUTPUT); // init timer: switch (timer_id) { case PWM_TIMER0: // 8bit counter { OCR0A = 0; OCR0B = 0; // See datasheet page 103 // Mode(WGM2:0)=1: phase correct PWM. TOP=0xff TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00); TCCR0B = (pwm_prescaler & 0x03) << CS00; } break; case PWM_TIMER2: // 8bit counter { OCR2A = 0; OCR2B = 0; // See datasheet page 103 // Mode(WGM2:0)=1: phase correct PWM. TOP=0xff TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM20); TCCR2B = (pwm_prescaler & 0x03) << CS20; } break; default: // Do nothing for other timers break; }; }
static int bt_spi_open(void) { /* Configure RST pin and hold BLE in Reset */ gpio_pin_configure(rst_dev, GPIO_RESET_PIN, GPIO_DIR_OUT | GPIO_PUD_PULL_UP); gpio_pin_write(rst_dev, GPIO_RESET_PIN, 0); spi_configure(spi_dev, &spi_conf); #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) /* Configure the CS (Chip Select) pin */ gpio_pin_configure(cs_dev, GPIO_CS_PIN, GPIO_DIR_OUT | GPIO_PUD_PULL_UP); gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ /* Configure IRQ pin and the IRQ call-back/handler */ gpio_pin_configure(irq_dev, GPIO_IRQ_PIN, GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH); gpio_init_callback(&gpio_cb, bt_spi_isr, BIT(GPIO_IRQ_PIN)); if (gpio_add_callback(irq_dev, &gpio_cb)) { return -EINVAL; } if (gpio_pin_enable_callback(irq_dev, GPIO_IRQ_PIN)) { return -EINVAL; } /* Start RX thread */ k_thread_spawn(rx_stack, sizeof(rx_stack), (k_thread_entry_t)bt_spi_rx_thread, NULL, NULL, NULL, K_PRIO_COOP(7), 0, K_NO_WAIT); /* Take BLE out of reset */ gpio_pin_write(rst_dev, GPIO_RESET_PIN, 1); /* Device will let us know when it's ready */ k_sem_take(&sem_initialised, K_FOREVER); return 0; }
static inline void update_pins(struct mb_display *disp, u32_t val) { if (IS_ENABLED(CONFIG_MICROBIT_DISPLAY_PIN_GRANULARITY)) { u32_t pin, prev = (disp->cur + 2) % 3; /* Disable the previous row */ gpio_pin_write(disp->dev, ROW_PIN(prev), 0); /* Set the column pins to their correct values */ for (pin = LED_COL1_GPIO_PIN; pin <= LED_COL9_GPIO_PIN; pin++) { gpio_pin_write(disp->dev, pin, !!(val & BIT(pin))); } /* Enable the new row */ gpio_pin_write(disp->dev, ROW_PIN(disp->cur), 1); } else { gpio_port_write(disp->dev, val); } }
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state) { struct device *usb_disconnect; usb_disconnect = device_get_binding( DT_ST_STM32_USB_0_DISCONNECT_GPIOS_CONTROLLER); gpio_pin_configure(usb_disconnect, DT_ST_STM32_USB_0_DISCONNECT_GPIOS_PIN, GPIO_DIR_OUT); if (state) { gpio_pin_write(usb_disconnect, DT_ST_STM32_USB_0_DISCONNECT_GPIOS_PIN, DT_ST_STM32_USB_0_DISCONNECT_GPIOS_FLAGS); } else { gpio_pin_write(usb_disconnect, DT_ST_STM32_USB_0_DISCONNECT_GPIOS_PIN, !DT_ST_STM32_USB_0_DISCONNECT_GPIOS_FLAGS); } }
static void spi_control_cs(struct device *dev, bool active) { struct spi_qmsi_runtime *context = dev->driver_data; struct spi_qmsi_config *config = dev->config->config_info; struct device *gpio = context->gpio_cs; if (!gpio) return; gpio_pin_write(gpio, config->cs_pin, !active); }
static void callback(struct device *dev, struct gpio_callback *gpio_cb, u32_t pins) { static int cb_cnt; TC_PRINT("callback triggered: %d\n", ++cb_cnt); if (cb_cnt >= MAX_INT_CNT) { cb_triggered = true; gpio_pin_write(dev, GPIO_OUT, 0); } }
/* * * @param taskname task identification string * @param mySem task's own semaphore * @param otherSem other task's semaphore * */ void helloLoop(const char *taskname, ksem_t mySem, ksem_t otherSem) { while (1) { task_sem_take(mySem, TICKS_UNLIMITED);; if (gpio) { if (mySem == TASKASEM) { gpio_pin_write(gpio, 6, 1); gpio_pin_write(gpio, 8, 0); gpio_pin_write(gpio_sus, 5, 0); } else if (mySem == TASKBSEM) { gpio_pin_write(gpio, 8, 1); gpio_pin_write(gpio, 6, 0); gpio_pin_write(gpio_sus, 5, 1); } } /* say "hello" */ PRINT("%s: Hello World!\n", taskname); /* wait a while, then let other task have a turn */ task_sleep(SLEEPTICKS); task_sem_give(otherSem); } }
static struct device *gpio_cs_init(struct spi_qmsi_config *config) { struct device *gpio; if (!config->cs_port) return NULL; gpio = device_get_binding(config->cs_port); if (!gpio) return NULL; gpio_pin_configure(gpio, config->cs_pin, GPIO_DIR_OUT); gpio_pin_write(gpio, config->cs_pin, 1); return gpio; }
void disable_backlight(void) { DECLARE_GLOBAL_DATA_PTR; u32 l; l = read_gpt8_reg(TCLR); l &= ~GPT8_PWM_EN; write_gpt8_reg(TLDR, 0x0); write_gpt8_reg(TTGR, 0x0); write_gpt8_reg(TMAR, 0x0); sr32(CM_FCLKEN_PER, 9, 1, 0x0); /* FCLKEN GPT8 */ sr32(CM_ICLKEN_PER, 9, 1, 0x0); /* ICLKEN GPT8 */ gpio_pin_write( GPIO_BACKLIGHT_EN_EVT2, 1 ); }
static int dht_init(struct device *dev) { struct dht_data *drv_data = dev->driver_data; drv_data->gpio = device_get_binding(CONFIG_DHT_GPIO_DEV_NAME); if (drv_data->gpio == NULL) { SYS_LOG_ERR("Failed to get GPIO device."); return -EINVAL; } gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, GPIO_DIR_OUT); gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1); return 0; }
void lcd_disable(void) { disable_backlight(); sr32(CM_FCLKEN_DSS, 0, 32, 0x0); sr32(CM_ICLKEN_DSS, 0, 32, 0x0); gpio_pin_write(36, 0); // Restore SPI registers MUX_VAL(CP(McBSP1_CLKR), (IEN | PTD | DIS | M1)) /*McSPI4-CLK*/ \ MUX_VAL(CP(McBSP1_DX), (IDIS | PTD | DIS | M1)) /*McSPI4-SIMO*/ \ MUX_VAL(CP(McBSP1_DR), (IEN | PTD | DIS | M1)) /*McSPI4-SOMI*/\ MUX_VAL(CP(McBSP1_FSX), (IDIS | PTD | DIS | M1)) /*McSPI4-CS0*/ lcd_disabled = 1; }
int gpiopwm_detach(device_t self, int flags) { struct gpiopwm_softc *sc = device_private(self); callout_halt(&sc->sc_pulse, NULL); callout_destroy(&sc->sc_pulse); gpio_pin_write(sc->sc_gpio, &sc->sc_map, 0, GPIO_PIN_LOW); pmf_device_deregister(self); gpio_pin_unmap(sc->sc_gpio, &sc->sc_map); if (sc->sc_log != NULL) { sysctl_teardown(&sc->sc_log); sc->sc_log = NULL; } return 0; }
static int pwr_ctrl_init(struct device *dev) { const struct pwr_ctrl_cfg *cfg = dev->config->config_info; struct device *gpio; gpio = device_get_binding(cfg->port); if (!gpio) { printk("Could not bind device \"%s\"\n", cfg->port); return -ENODEV; } gpio_pin_configure(gpio, cfg->pin, GPIO_DIR_OUT); gpio_pin_write(gpio, cfg->pin, 1); k_sleep(1); /* Wait for the rail to come up and stabilize */ return 0; }
static void init_callback(struct device *dev) { gpio_pin_disable_callback(dev, PIN_IN); gpio_pin_disable_callback(dev, PIN_OUT); /* 1. set PIN_OUT */ gpio_pin_configure(dev, PIN_OUT, GPIO_DIR_OUT); gpio_pin_write(dev, PIN_OUT, 0); /* 2. configure PIN_IN callback and trigger condition */ gpio_pin_configure(dev, PIN_IN, GPIO_DIR_IN | GPIO_INT | GPIO_INT_EDGE | \ GPIO_INT_ACTIVE_HIGH | GPIO_INT_DEBOUNCE); gpio_init_callback(&cb_data[0].gpio_cb, callback_1, BIT(PIN_IN)); gpio_add_callback(dev, &cb_data[0].gpio_cb); gpio_init_callback(&cb_data[1].gpio_cb, callback_2, BIT(PIN_IN)); gpio_add_callback(dev, &cb_data[1].gpio_cb); }
static struct device *gpio_cs_init(const struct spi_qmsi_config *config) { struct device *gpio; if (!config->cs_port) { return NULL; } gpio = device_get_binding(config->cs_port); if (!gpio) { return NULL; } if (gpio_pin_configure(gpio, config->cs_pin, GPIO_DIR_OUT) != 0) { return NULL; } if (gpio_pin_write(gpio, config->cs_pin, 1) != 0) { return NULL; } return gpio; }
/** * @brief Initialize UART channel * * This routine is called to reset the chip in a quiescent state. * It is assumed that this function is called only once per UART. * * @param dev UART device struct * * @return 0 on success */ static int uart_nrfx_init(struct device *dev) { struct device *gpio_dev; int err; gpio_dev = device_get_binding(CONFIG_GPIO_NRF5_P0_DEV_NAME); __ASSERT(gpio_dev, "UART init failed. Cannot find %s", CONFIG_GPIO_NRF5_P0_DEV_NAME); /* Setting default height state of the TX PIN to avoid glitches * on the line during peripheral activation/deactivation. */ gpio_pin_write(gpio_dev, CONFIG_UART_0_NRF_TX_PIN, 1); gpio_pin_configure(gpio_dev, CONFIG_UART_0_NRF_TX_PIN, GPIO_DIR_OUT); gpio_pin_configure(gpio_dev, CONFIG_UART_0_NRF_RX_PIN, GPIO_DIR_IN); nrf_uart_txrx_pins_set(NRF_UART0, CONFIG_UART_0_NRF_TX_PIN, CONFIG_UART_0_NRF_RX_PIN); #ifdef CONFIG_UART_0_NRF_FLOW_CONTROL /* Setting default height state of the RTS PIN to avoid glitches * on the line during peripheral activation/deactivation. */ gpio_pin_write(gpio_dev, CONFIG_UART_0_NRF_RTS_PIN, 1); gpio_pin_configure(gpio_dev, CONFIG_UART_0_NRF_RTS_PIN, GPIO_DIR_OUT); gpio_pin_configure(gpio_dev, CONFIG_UART_0_NRF_CTS_PIN, GPIO_DIR_IN); nrf_uart_hwfc_pins_set(NRF_UART0, CONFIG_UART_0_NRF_RTS_PIN, CONFIG_UART_0_NRF_CTS_PIN); #endif /* CONFIG_UART_0_NRF_FLOW_CONTROL */ nrf_uart_configure(NRF_UART0, #ifdef CONFIG_UART_0_NRF_PARITY_BIT NRF_UART_PARITY_INCLUDED, #else NRF_UART_PARITY_EXCLUDED, #endif /* CONFIG_UART_0_NRF_PARITY_BIT */ #ifdef CONFIG_UART_0_NRF_FLOW_CONTROL NRF_UART_HWFC_ENABLED); #else NRF_UART_HWFC_DISABLED); #endif /* CONFIG_UART_0_NRF_PARITY_BIT */ /* Set baud rate */ err = baudrate_set(dev, CONFIG_UART_0_BAUD_RATE); if (err) { return err; } /* Enable receiver and transmitter */ nrf_uart_enable(NRF_UART0); nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_TXDRDY); nrf_uart_event_clear(NRF_UART0, NRF_UART_EVENT_RXDRDY); nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTTX); nrf_uart_task_trigger(NRF_UART0, NRF_UART_TASK_STARTRX); #ifdef CONFIG_UART_INTERRUPT_DRIVEN IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_UART0), CONFIG_UART_0_IRQ_PRI, uart_nrfx_isr, DEVICE_GET(uart_nrfx_uart0), 0); irq_enable(NRFX_IRQ_NUMBER_GET(NRF_UART0)); #endif return 0; }
int _galileo_pinmux_set_pin(struct device *port, u8_t pin, u32_t func) { struct galileo_data * const drv_data = port->driver_data; u8_t mux_index = 0; u8_t i = 0; struct mux_path *enable = NULL; struct pin_config *mux_config = drv_data->mux_config; if (pin > PINMUX_NUM_PINS) { return -ENOTSUP; } mux_config[pin].mode = func; /* NUM_PIN_FUNCS being the number of alt functions */ mux_index = NUM_PIN_FUNCS * pin; /* * functions are in numeric order, we can just skip to the index * needed */ mux_index += func; enable = &_galileo_path[mux_index]; for (i = 0; i < 5; i++) { switch (enable->path[i].mux) { case EXP0: gpio_pin_write(drv_data->exp0, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->exp0, enable->path[i].pin, enable->path[i].cfg); break; case EXP1: gpio_pin_write(drv_data->exp1, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->exp1, enable->path[i].pin, enable->path[i].cfg); break; case EXP2: gpio_pin_write(drv_data->exp2, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->exp2, enable->path[i].pin, enable->path[i].cfg); break; case PWM0: pwm_pin_set_cycles(drv_data->pwm0, enable->path[i].pin, 100, enable->path[i].level ? 100 : 0); break; case G_DW: gpio_pin_write(drv_data->gpio_dw, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->gpio_dw, enable->path[i].pin, enable->path[i].cfg); break; case G_CW: gpio_pin_write(drv_data->gpio_core, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->gpio_core, enable->path[i].pin, enable->path[i].cfg); break; case G_RW: gpio_pin_write(drv_data->gpio_resume, enable->path[i].pin, enable->path[i].level); gpio_pin_configure(drv_data->gpio_resume, enable->path[i].pin, enable->path[i].cfg); break; case NONE: /* no need to do anything */ break; } } return 0; }
static int bt_spi_send(struct net_buf *buf) { u8_t header[5] = { SPI_WRITE, 0x00, 0x00, 0x00, 0x00 }; u32_t pending; /* Buffer needs an additional byte for type */ if (buf->len >= SPI_MAX_MSG_LEN) { BT_ERR("Message too long"); return -EINVAL; } /* Allow time for the read thread to handle interrupt */ while (true) { gpio_pin_read(irq_dev, GPIO_IRQ_PIN, &pending); if (!pending) { break; } k_sleep(1); } k_sem_take(&sem_busy, K_FOREVER); switch (bt_buf_get_type(buf)) { case BT_BUF_ACL_OUT: net_buf_push_u8(buf, HCI_ACL); break; case BT_BUF_CMD: net_buf_push_u8(buf, HCI_CMD); break; default: BT_ERR("Unsupported type"); k_sem_give(&sem_busy); return -EINVAL; } /* Poll sanity values until device has woken-up */ do { #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); gpio_pin_write(cs_dev, GPIO_CS_PIN, 0); #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ spi_transceive(spi_dev, header, 5, rxmsg, 5); /* * RX Header (rxmsg) must contain a sanity check Byte and size * information. If it does not contain BOTH then it is * sleeping or still in the initialisation stage (waking-up). */ } while (rxmsg[STATUS_HEADER_READY] != READY_NOW || (rxmsg[1] | rxmsg[2] | rxmsg[3] | rxmsg[4]) == 0); /* Transmit the message */ do { spi_transceive(spi_dev, buf->data, buf->len, rxmsg, buf->len); } while (rxmsg[0] == 0); #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) /* Deselect chip */ gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ k_sem_give(&sem_busy); spi_dump_message("TX:ed", buf->data, buf->len); #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) /* * Since a RESET has been requested, the chip will now restart. * Unfortunately the BlueNRG will reply with "reset received" but * since it does not send back a NOP, we have no way to tell when the * RESET has actually taken palce. Instead, we use the vendor command * EVT_BLUE_INITIALIZED as an indication that it is safe to proceed. */ if (bt_spi_get_cmd(buf->data) == BT_HCI_OP_RESET) { k_sem_take(&sem_initialised, K_FOREVER); } #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ net_buf_unref(buf); return 0; }
static void bt_spi_rx_thread(void) { struct net_buf *buf; u8_t header_master[5] = { SPI_READ, 0x00, 0x00, 0x00, 0x00 }; u8_t header_slave[5]; struct bt_hci_acl_hdr acl_hdr; u8_t size; memset(&txmsg, 0xFF, SPI_MAX_MSG_LEN); while (true) { k_sem_take(&sem_request, K_FOREVER); /* Disable IRQ pin callback to avoid spurious IRQs */ gpio_pin_disable_callback(irq_dev, GPIO_IRQ_PIN); k_sem_take(&sem_busy, K_FOREVER); do { #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); gpio_pin_write(cs_dev, GPIO_CS_PIN, 0); #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ spi_transceive(spi_dev, header_master, 5, header_slave, 5); } while (header_slave[STATUS_HEADER_TOREAD] == 0 || header_slave[STATUS_HEADER_TOREAD] == 0xFF); size = header_slave[STATUS_HEADER_TOREAD]; do { spi_transceive(spi_dev, &txmsg, size, &rxmsg, size); } while (rxmsg[0] == 0); gpio_pin_enable_callback(irq_dev, GPIO_IRQ_PIN); #if defined(CONFIG_BLUETOOTH_SPI_BLUENRG) gpio_pin_write(cs_dev, GPIO_CS_PIN, 1); #endif /* CONFIG_BLUETOOTH_SPI_BLUENRG */ k_sem_give(&sem_busy); spi_dump_message("RX:ed", rxmsg, size); switch (rxmsg[PACKET_TYPE]) { case HCI_EVT: switch (rxmsg[EVT_HEADER_EVENT]) { case BT_HCI_EVT_VENDOR: /* Vendor events are currently unsupported */ bt_spi_handle_vendor_evt(rxmsg); continue; case BT_HCI_EVT_CMD_COMPLETE: case BT_HCI_EVT_CMD_STATUS: buf = bt_buf_get_cmd_complete(K_FOREVER); break; default: buf = bt_buf_get_rx(K_FOREVER); break; } bt_buf_set_type(buf, BT_BUF_EVT); net_buf_add_mem(buf, &rxmsg[1], rxmsg[EVT_HEADER_SIZE] + 2); break; case HCI_ACL: buf = bt_buf_get_rx(K_FOREVER); bt_buf_set_type(buf, BT_BUF_ACL_IN); memcpy(&acl_hdr, &rxmsg[1], sizeof(acl_hdr)); net_buf_add_mem(buf, &acl_hdr, sizeof(acl_hdr)); net_buf_add_mem(buf, &rxmsg[5], sys_le16_to_cpu(acl_hdr.len)); break; default: BT_ERR("Unknown BT buf type %d", rxmsg[0]); continue; } if (rxmsg[PACKET_TYPE] == HCI_EVT && bt_hci_evt_is_prio(rxmsg[EVT_HEADER_EVENT])) { bt_recv_prio(buf); } else { bt_recv(buf); } } }
static int dht_sample_fetch(struct device *dev, enum sensor_channel chan) { struct dht_data *drv_data = dev->driver_data; int ret = 0; s8_t signal_duration[DHT_DATA_BITS_NUM]; s8_t max_duration, min_duration, avg_duration; u8_t buf[5]; unsigned int i, j; __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL); /* send start signal */ gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 0); k_busy_wait(DHT_START_SIGNAL_DURATION); gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1); /* switch to DIR_IN to read sensor signals */ gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, GPIO_DIR_IN); /* wait for sensor response */ if (dht_measure_signal_duration(drv_data, 1) == -1) { ret = -EIO; goto cleanup; } /* read sensor response */ if (dht_measure_signal_duration(drv_data, 0) == -1) { ret = -EIO; goto cleanup; } /* wait for sensor data */ if (dht_measure_signal_duration(drv_data, 1) == -1) { ret = -EIO; goto cleanup; } /* read sensor data */ for (i = 0; i < DHT_DATA_BITS_NUM; i++) { /* LOW signal to indicate a new bit */ if (dht_measure_signal_duration(drv_data, 0) == -1) { ret = -EIO; goto cleanup; } /* HIGH signal duration indicates bit value */ signal_duration[i] = dht_measure_signal_duration(drv_data, 1); if (signal_duration[i] == -1) { ret = -EIO; goto cleanup; } } /* * the datasheet says 20-40us HIGH signal duration for a 0 bit and * 80us for a 1 bit; however, since dht_measure_signal_duration is * not very precise, compute the threshold for deciding between a * 0 bit and a 1 bit as the average between the minimum and maximum * if the durations stored in signal_duration */ min_duration = signal_duration[0]; max_duration = signal_duration[0]; for (i = 1; i < DHT_DATA_BITS_NUM; i++) { if (min_duration > signal_duration[i]) { min_duration = signal_duration[i]; } if (max_duration < signal_duration[i]) { max_duration = signal_duration[i]; } } avg_duration = ((s16_t)min_duration + (s16_t)max_duration) / 2; /* store bits in buf */ j = 0; memset(buf, 0, sizeof(buf)); for (i = 0; i < DHT_DATA_BITS_NUM; i++) { if (signal_duration[i] >= avg_duration) { buf[j] = (buf[j] << 1) | 1; } else { buf[j] = buf[j] << 1; } if (i % 8 == 7) { j++; } } /* verify checksum */ if (((buf[0] + buf[1] + buf[2] + buf[3]) & 0xFF) != buf[4]) { SYS_LOG_DBG("Invalid checksum in fetched sample"); ret = -EIO; } else { memcpy(drv_data->sample, buf, 4); } cleanup: /* switch to DIR_OUT and leave pin to HIGH until next fetch */ gpio_pin_configure(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, GPIO_DIR_OUT); gpio_pin_write(drv_data->gpio, CONFIG_DHT_GPIO_PIN_NUM, 1); return ret; }