void SX1276IoInit(void) { struct hal_spi_settings spi_settings; int rc; #if MYNEWT_VAL(SX1276_HAS_ANT_SW) rc = hal_gpio_init_out(SX1276_RXTX, 0); assert(rc == 0); #endif rc = hal_gpio_init_out(RADIO_NSS, 1); assert(rc == 0); hal_spi_disable(RADIO_SPI_IDX); spi_settings.data_order = HAL_SPI_MSB_FIRST; spi_settings.data_mode = HAL_SPI_MODE0; spi_settings.baudrate = MYNEWT_VAL(SX1276_SPI_BAUDRATE); spi_settings.word_size = HAL_SPI_WORD_SIZE_8BIT; rc = hal_spi_config(RADIO_SPI_IDX, &spi_settings); assert(rc == 0); rc = hal_spi_enable(RADIO_SPI_IDX); assert(rc == 0); }
void task1_handler(void *arg) { struct os_task *t; /* Set the led pin for the E407 devboard */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); while (1) { t = os_sched_get_current_task(); assert(t->t_func == task1_handler); ++g_task1_loops; /* Wait one second */ os_time_delay(1000); /* Toggle the LED */ hal_gpio_toggle(g_led_pin); /* Release semaphore to task 2 */ os_sem_release(&g_test_sem); } }
static void task1_handler(void *arg) { struct os_task *t; /* Set the led pin for the E407 devboard */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); console_printf("\nSensors Test App\n"); while (1) { t = os_sched_get_current_task(); assert(t->t_func == task1_handler); ++g_task1_loops; /* Wait one second */ os_time_delay(OS_TICKS_PER_SEC * MYNEWT_VAL(SENSOR_OIC_OBS_RATE)); /* Toggle the LED */ (void)hal_gpio_toggle(g_led_pin); /* Release semaphore to task 2 */ os_sem_release(&g_test_sem); } }
sint8 nm_bus_init(void *pvinit) { struct hal_spi_settings cfg = { 0 }; /* * Add code to configure spi. */ if (!winc1500_spi_inited) { if (hal_gpio_init_out(WINC1500_SPI_SSN, 1)) { return M2M_ERR_BUS_FAIL; } cfg.data_mode = HAL_SPI_MODE0; cfg.data_order = HAL_SPI_MSB_FIRST; cfg.word_size = HAL_SPI_WORD_SIZE_8BIT; cfg.baudrate = WINC1500_SPI_SPEED; if (hal_spi_config(BSP_WINC1500_SPI_PORT, &cfg)) { return M2M_ERR_BUS_FAIL; } winc1500_spi_inited = 1; if (hal_spi_enable(BSP_WINC1500_SPI_PORT)) { return M2M_ERR_BUS_FAIL; } } nm_bsp_reset(); nm_bsp_sleep(1); return M2M_SUCCESS; }
void task1_handler(void *arg) { int rc; /* Set the led pin for the E407 devboard */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); spi_cb_arg = &spi_cb_obj; sblinky_spi_cfg(SPI_SLAVE_ID); hal_spi_enable(SPI_SLAVE_ID); /* Make the default character 0x77 */ hal_spi_slave_set_def_tx_val(SPI_SLAVE_ID, 0x77); /* * Fill buffer with 0x77 for first transfer. This should be a 0xdeadbeef * transfer from master to start things off */ memset(g_spi_tx_buf, 0x77, 32); rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf, 32); while (1) { /* Wait for semaphore from ISR */ os_sem_pend(&g_test_sem, OS_TIMEOUT_NEVER); if (g_spi_xfr_num == 0) { /* Since we dont know what master will send, we fill 0x88 */ memset(g_spi_tx_buf, 0x88, 32); rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf, 32); assert(rc == 0); } else { /* transmit back what we just received */ memcpy(prev_buf, g_spi_tx_buf, 32); memset(g_spi_tx_buf, 0xaa, 32); memcpy(g_spi_tx_buf, g_spi_rx_buf, spi_cb_obj.txlen); rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf, 32); assert(rc == 0); } ++g_spi_xfr_num; /* Toggle the LED */ hal_gpio_toggle(g_led_pin); } }
static int bq24040_configure_pin(struct bq24040_pin *pin) { if ((!pin) || (pin->bp_pin_num == -1)) { return 0; } if (pin->bp_pin_direction == HAL_GPIO_MODE_IN) { if (pin->bp_irq_trig != HAL_GPIO_TRIG_NONE) { assert(pin->bp_irq_fn != NULL); return hal_gpio_irq_init(pin->bp_pin_num, pin->bp_irq_fn, NULL, pin->bp_irq_trig, pin->bp_pull); } else { return hal_gpio_init_in(pin->bp_pin_num, pin->bp_pull); } } else if (pin->bp_pin_direction == HAL_GPIO_MODE_OUT) { return hal_gpio_init_out(pin->bp_pin_num, pin->bp_init_value); } return SYS_EINVAL; }
void task1_handler(void *arg) { struct os_task *t; int prev_pin_state, curr_pin_state; struct image_version ver; /* Set the led pin for the E407 devboard */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); if (imgr_my_version(&ver) == 0) { console_printf("\nSlinky_OIC %u.%u.%u.%u\n", ver.iv_major, ver.iv_minor, ver.iv_revision, (unsigned int)ver.iv_build_num); } else { console_printf("\nSlinky\n"); } while (1) { t = os_sched_get_current_task(); assert(t->t_func == task1_handler); ++g_task1_loops; /* Wait one second */ os_time_delay(OS_TICKS_PER_SEC); /* Toggle the LED */ prev_pin_state = hal_gpio_read(g_led_pin); curr_pin_state = hal_gpio_toggle(g_led_pin); LOG_INFO(&my_log, LOG_MODULE_DEFAULT, "GPIO toggle from %u to %u", prev_pin_state, curr_pin_state); STATS_INC(g_stats_gpio_toggle, toggles); /* Release semaphore to task 2 */ os_sem_release(&g_test_sem); } }
void task1_handler(void *arg) { int i; int rc; uint16_t rxval; uint8_t last_val; uint8_t spi_nb_cntr; uint8_t spi_b_cntr; /* Set the led pin for the E407 devboard */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); /* Use SS pin for testing */ hal_gpio_init_out(SPI_SS_PIN, 1); sblinky_spi_cfg(0); hal_spi_set_txrx_cb(0, NULL, NULL); hal_spi_enable(0); /* * Send some bytes in a non-blocking manner to SPI using tx val. The * slave should send back 0x77. */ g_spi_tx_buf[0] = 0xde; g_spi_tx_buf[1] = 0xad; g_spi_tx_buf[2] = 0xbe; g_spi_tx_buf[3] = 0xef; hal_gpio_write(SPI_SS_PIN, 0); for (i = 0; i < 4; ++i) { rxval = hal_spi_tx_val(0, g_spi_tx_buf[i]); assert(rxval == 0x77); g_spi_rx_buf[i] = (uint8_t)rxval; } hal_gpio_write(SPI_SS_PIN, 1); ++g_spi_xfr_num; /* Set up the callback to use when non-blocking API used */ hal_spi_disable(0); spi_cb_arg = &spi_cb_obj; spi_cb_obj.txlen = 32; hal_spi_set_txrx_cb(0, sblinky_spi_irqm_handler, spi_cb_arg); hal_spi_enable(0); spi_nb_cntr = 0; spi_b_cntr = 0; while (1) { /* Wait one second */ os_time_delay(OS_TICKS_PER_SEC); /* Toggle the LED */ hal_gpio_toggle(g_led_pin); /* Get random length to send */ g_last_tx_len = spi_cb_obj.txlen; spi_cb_obj.txlen = (rand() & 0x1F) + 1; memcpy(g_spi_last_tx_buf, g_spi_tx_buf, g_last_tx_len); last_val = g_spi_last_tx_buf[g_last_tx_len - 1]; for (i= 0; i < spi_cb_obj.txlen; ++i) { g_spi_tx_buf[i] = (uint8_t)(last_val + i); } if (g_spi_xfr_num & 1) { /* Send non-blocking */ ++spi_nb_cntr; assert(hal_gpio_read(SPI_SS_PIN) == 1); hal_gpio_write(SPI_SS_PIN, 0); #if 0 if (spi_nb_cntr == 7) { g_spi_null_rx = 1; rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, NULL, 32); } else { g_spi_null_rx = 0; rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf, 32); } assert(!rc); #else g_spi_null_rx = 0; rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf, spi_cb_obj.txlen); assert(!rc); console_printf("a transmitted: "); for (i = 0; i < spi_cb_obj.txlen; i++) { console_printf("%2x ", g_spi_tx_buf[i]); } console_printf("\n"); console_printf("received: "); for (i = 0; i < spi_cb_obj.txlen; i++) { console_printf("%2x ", g_spi_rx_buf[i]); } console_printf("\n"); #endif } else { /* Send blocking */ ++spi_b_cntr; assert(hal_gpio_read(SPI_SS_PIN) == 1); hal_gpio_write(SPI_SS_PIN, 0); #if 0 if (spi_b_cntr == 7) { g_spi_null_rx = 1; rc = hal_spi_txrx(0, g_spi_tx_buf, NULL, 32); spi_b_cntr = 0; } else { g_spi_null_rx = 0; rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, 32); } assert(!rc); hal_gpio_write(SPI_SS_PIN, 1); spitest_validate_last(spi_cb_obj.txlen); #else rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, spi_cb_obj.txlen); assert(!rc); hal_gpio_write(SPI_SS_PIN, 1); console_printf("b transmitted: "); for (i = 0; i < spi_cb_obj.txlen; i++) { console_printf("%2x ", g_spi_tx_buf[i]); } console_printf("\n"); console_printf("received: "); for (i = 0; i < spi_cb_obj.txlen; i++) { console_printf("%2x ", g_spi_rx_buf[i]); } console_printf("\n"); spitest_validate_last(spi_cb_obj.txlen); ++g_spi_xfr_num; #endif } } }
/** * Expects to be called back through os_dev_create(). * * @param The device object associated with this accelerometer * @param Argument passed to OS device init, unused * * @return 0 on success, non-zero error on failure. */ int lis2dw12_init(struct os_dev *dev, void *arg) { struct lis2dw12 *lis2dw12; struct sensor *sensor; int rc; if (!arg || !dev) { rc = SYS_ENODEV; goto err; } lis2dw12 = (struct lis2dw12 *) dev; lis2dw12->cfg.mask = SENSOR_TYPE_ALL; log_register(dev->od_name, &_log, &log_console_handler, NULL, LOG_SYSLEVEL); sensor = &lis2dw12->sensor; /* Initialise the stats entry */ rc = stats_init( STATS_HDR(g_lis2dw12stats), STATS_SIZE_INIT_PARMS(g_lis2dw12stats, STATS_SIZE_32), STATS_NAME_INIT_PARMS(lis2dw12_stat_section)); SYSINIT_PANIC_ASSERT(rc == 0); /* Register the entry with the stats registry */ rc = stats_register(dev->od_name, STATS_HDR(g_lis2dw12stats)); SYSINIT_PANIC_ASSERT(rc == 0); rc = sensor_init(sensor, dev); if (rc) { goto err; } /* Add the light driver */ rc = sensor_set_driver(sensor, SENSOR_TYPE_ACCELEROMETER, (struct sensor_driver *) &g_lis2dw12_sensor_driver); if (rc) { goto err; } /* Set the interface */ rc = sensor_set_interface(sensor, arg); if (rc) { goto err; } rc = sensor_mgr_register(sensor); if (rc) { goto err; } if (sensor->s_itf.si_type == SENSOR_ITF_SPI) { rc = hal_spi_disable(sensor->s_itf.si_num); if (rc) { goto err; } rc = hal_spi_config(sensor->s_itf.si_num, &spi_lis2dw12_settings); if (rc == EINVAL) { /* If spi is already enabled, for nrf52, it returns -1, We should not * fail if the spi is already enabled */ goto err; } rc = hal_spi_enable(sensor->s_itf.si_num); if (rc) { goto err; } rc = hal_gpio_init_out(sensor->s_itf.si_cs_pin, 1); if (rc) { goto err; } } init_interrupt(&lis2dw12->intr, lis2dw12->sensor.s_itf.si_ints); lis2dw12->pdd.notify_ctx.snec_sensor = sensor; lis2dw12->pdd.registered_mask = 0; lis2dw12->pdd.interrupt = NULL; rc = init_intpin(lis2dw12, lis2dw12_int_irq_handler, sensor); if (rc) { return rc; } return 0; err: return rc; }
/** * Expects to be called back through os_dev_create(). * * @param The device object associated with this accelerometer * @param Argument passed to OS device init, unused * * @return 0 on success, non-zero error on failure. */ int adxl345_init(struct os_dev *dev, void *arg) { struct adxl345 *adxl; struct sensor *sensor; int rc; if (!arg || !dev) { return SYS_ENODEV; } adxl = (struct adxl345 *) dev; adxl->cfg.mask = SENSOR_TYPE_ALL; log_register(dev->od_name, &_log, &log_console_handler, NULL, LOG_SYSLEVEL); sensor = &adxl->sensor; /* Initialise the stats entry */ rc = stats_init( STATS_HDR(g_adxl345stats), STATS_SIZE_INIT_PARMS(g_adxl345stats, STATS_SIZE_32), STATS_NAME_INIT_PARMS(adxl345_stat_section)); SYSINIT_PANIC_ASSERT(rc == 0); /* Register the entry with the stats registry */ rc = stats_register(dev->od_name, STATS_HDR(g_adxl345stats)); SYSINIT_PANIC_ASSERT(rc == 0); rc = sensor_init(sensor, dev); if (rc) { return rc; } /* Add the accelerometer/gyroscope driver */ rc = sensor_set_driver(sensor, SENSOR_TYPE_ACCELEROMETER, (struct sensor_driver *) &adxl345_sensor_driver); if (rc) { return rc; } rc = sensor_set_interface(sensor, arg); if (rc) { return rc; } rc = sensor_mgr_register(sensor); if (rc) { return rc; } if (sensor->s_itf.si_type == SENSOR_ITF_SPI) { rc = hal_spi_config(sensor->s_itf.si_num, &spi_adxl345_settings); if (rc == EINVAL) { return rc; } rc = hal_spi_enable(sensor->s_itf.si_num); if (rc) { return rc; } rc = hal_gpio_init_out(sensor->s_itf.si_cs_pin, 1); if (rc) { return rc; } } #if MYNEWT_VAL(ADXL345_INT_ENABLE) adxl->pdd.read_ctx.srec_sensor = sensor; adxl->pdd.notify_ctx.snec_sensor = sensor; rc = init_intpin(adxl, interrupt_handler, sensor); if (rc != 0) { return rc; } #endif return 0; }
/** * main * * The main task for the project. This function initializes the packages, * then starts serving events from default event queue. * * @return int NOTE: this function should never return! */ int main(void) { int rc; /* Initialize OS */ sysinit(); /* Dummy device address */ #if BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER g_dev_addr[0] = 0x00; g_dev_addr[1] = 0x00; g_dev_addr[2] = 0x00; g_dev_addr[3] = 0x88; g_dev_addr[4] = 0x88; g_dev_addr[5] = 0x08; g_bletest_cur_peer_addr[0] = 0x00; g_bletest_cur_peer_addr[1] = 0x00; g_bletest_cur_peer_addr[2] = 0x00; g_bletest_cur_peer_addr[3] = 0x99; g_bletest_cur_peer_addr[4] = 0x99; g_bletest_cur_peer_addr[5] = 0x09; #else g_dev_addr[0] = 0x00; g_dev_addr[1] = 0x00; g_dev_addr[2] = 0x00; g_dev_addr[3] = 0x99; g_dev_addr[4] = 0x99; g_dev_addr[5] = 0x09; g_bletest_cur_peer_addr[0] = 0x00; g_bletest_cur_peer_addr[1] = 0x00; g_bletest_cur_peer_addr[2] = 0x00; g_bletest_cur_peer_addr[3] = 0x88; g_bletest_cur_peer_addr[4] = 0x88; g_bletest_cur_peer_addr[5] = 0x08; #endif log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL, LOG_SYSLEVEL); /* Set the led pin as an output */ g_led_pin = LED_BLINK_PIN; hal_gpio_init_out(g_led_pin, 1); /* Initialize eventq for bletest task */ os_eventq_init(&g_bletest_evq); rc = os_task_init(&bletest_task, "bletest", bletest_task_handler, NULL, BLETEST_TASK_PRIO, OS_WAIT_FOREVER, bletest_stack, BLETEST_STACK_SIZE); assert(rc == 0); while (1) { os_eventq_run(os_eventq_dflt_get()); } /* Never returns */ /* os start should never return. If it does, this should be an error */ assert(0); return rc; }