uint8_t spiflash_read_jedec_id(struct spiflash_dev *dev, uint8_t *manufacturer, uint8_t *memory_type, uint8_t *capacity) { uint8_t cmd[4] = { SPIFLASH_READ_JEDEC_ID, 0, 0, 0 }; spiflash_cs_activate(dev); hal_spi_txrx(dev->spi_num, cmd, cmd, sizeof cmd); spiflash_cs_deactivate(dev); if (manufacturer) { *manufacturer = cmd[1]; } if (memory_type) { *memory_type = cmd[2]; } if (capacity) { *capacity = cmd[3]; } return 0; }
static sint8 nm_spi_rw(uint8 *pu8Mosi, uint8 *pu8Miso, uint16 u16Sz) { int rc = M2M_SUCCESS; uint8_t tx = 0; uint8_t rx; /* chip select */ hal_gpio_write(WINC1500_SPI_SSN, 0); while (u16Sz) { if (pu8Mosi) { tx = *pu8Mosi; pu8Mosi++; } //rx = hal_spi_tx_val(BSP_WINC1500_SPI_PORT, tx); rc = hal_spi_txrx(BSP_WINC1500_SPI_PORT, &tx, &rx, 1); if (rc != 0) { rc = M2M_ERR_BUS_FAIL; break; } if (pu8Miso) { *pu8Miso = rx; pu8Miso++; } u16Sz--; } /* chip deselect */ hal_gpio_write(WINC1500_SPI_SSN, 1); return rc; }
uint8_t spiflash_release_power_down(struct spiflash_dev *dev, uint8_t *id) { uint8_t cmd[5] = { SPIFLASH_RELEASE_POWER_DOWN, 0xFF, 0xFF, 0xFF, 0 }; spiflash_cs_activate(dev); hal_spi_txrx(dev->spi_num, cmd, cmd, sizeof cmd); spiflash_cs_deactivate(dev); if (id) { *id = cmd[4]; } return 0; }
/** * tlc5971 write * * Send the 224 bits to the device. Note that the device must be opened * prior to calling this function * * @param dev Pointer to tlc5971 device * * @return int 0: success; -1 error */ int tlc5971_write(struct tlc5971_dev *dev) { int rc; os_sr_t sr; if (!dev->is_enabled) { return -1; } /* * XXX: for now, disable interrupts around write as it is possible that * too long a gap will cause mis-program of device. */ tlc5971_construct_packet(dev); OS_ENTER_CRITICAL(sr); rc = hal_spi_txrx(dev->tlc_itf.tpi_spi_num, dev->data_packet, NULL, TLC5971_PACKET_LENGTH); OS_EXIT_CRITICAL(sr); return rc; }
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 } } }