/** * gpio toggle * * Toggles the specified pin * * @param pin Pin number to toggle * * @return current pin state int 0: low, 1: high */ int hal_gpio_toggle(int pin) { int pin_state = (hal_gpio_read(pin) == 0); hal_gpio_write(pin, pin_state); return pin_state; }
static void wait_interrupt(struct lis2dw12_int * interrupt, uint8_t int_num) { bool wait; OS_ENTER_CRITICAL(interrupt->lock); /* Check if we did not missed interrupt */ if (hal_gpio_read(interrupt->ints[int_num].host_pin) == interrupt->ints[int_num].active) { OS_EXIT_CRITICAL(interrupt->lock); return; } if (interrupt->active) { interrupt->active = false; wait = false; } else { interrupt->asleep = true; wait = true; } OS_EXIT_CRITICAL(interrupt->lock); if (wait) { os_error_t error; error = os_sem_pend(&interrupt->wait, -1); assert(error == OS_OK); } }
int bq24040_get_charging_status(struct bq24040 *bq24040, int *status) { int read_val; if (bq24040->b_cfg.bc_chg_pin->bp_pin_num != -1) { read_val = hal_gpio_read(bq24040->b_cfg.bc_chg_pin->bp_pin_num); *status = read_val == 0 ? 1 : 0; return 0; } return 1; }
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); } }
/* * GPIO interrupt when slave gets selected/deselected. */ static void spi_ss_isr(void *arg) { struct stm32_hal_spi *spi = (struct stm32_hal_spi *)arg; int ss; int len; uint16_t reg; spi_stat.ss_irq++; ss = hal_gpio_read(spi->cfg->ss_pin); if (ss == 0 && !spi->selected) { /* * We're now seeing chip select. Enable SPI, SPI interrupts. */ if (spi->tx_in_prog) { __HAL_SPI_ENABLE_IT(&spi->handle, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR); } else { __HAL_SPI_ENABLE_IT(&spi->handle, SPI_IT_TXE | SPI_IT_ERR); } reg = spi->handle.Instance->CR1; reg &= ~SPI_CR1_SSI; reg |= SPI_CR1_SPE; spi->handle.Instance->CR1 = reg; spi->selected = 1; } if (ss == 1 && spi->selected) { /* * Chip select done. Check whether there's pending data to RX. */ if (spi->handle.Instance->SR & SPI_SR_RXNE && spi->handle.RxISR) { spi->handle.RxISR(&spi->handle); } /* * Disable SPI. */ reg = spi->handle.Instance->CR1; reg &= ~SPI_CR1_SPE; reg |= SPI_CR1_SSI; spi->handle.Instance->CR1 = reg; __HAL_SPI_DISABLE_IT(&spi->handle, SPI_IT_RXNE|SPI_IT_TXE|SPI_IT_ERR); len = spi->handle.RxXferSize - spi->handle.RxXferCount; if (len) { /* * If some data was clocked out, reset to start sending * default data and call callback, if user was waiting for * data. */ spi->handle.State = HAL_SPI_STATE_READY; HAL_SPI_QueueTransmit(&spi->handle, spi->def_char, 2); if (spi->tx_in_prog) { spi->tx_in_prog = 0; if (spi->txrx_cb_func) { spi->txrx_cb_func(spi->txrx_cb_arg, len); } } } spi->selected = 0; } }
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 } } }