static bool iotjs_spi_open(iotjs_spi_t* spi) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_spi_t, spi); struct iotbus_spi_config_s cfg = {.bits_per_word = _this->bits_per_word, .chip_select = _this->chip_select == kSpiCsNone ? 0 : 1, .frequency = _this->max_speed }; switch (_this->mode) { case kSpiMode_0: cfg.mode = IOTBUS_SPI_MODE0; break; case kSpiMode_1: cfg.mode = IOTBUS_SPI_MODE1; break; case kSpiMode_2: cfg.mode = IOTBUS_SPI_MODE2; break; case kSpiMode_3: cfg.mode = IOTBUS_SPI_MODE3; break; default: cfg.mode = IOTBUS_SPI_MODE0; } _this->hSpi = iotbus_spi_open(_this->bus, &cfg); if (_this->hSpi == NULL) { return false; } DDLOG( "SPI Options \n mode: %d\n chipSelect: %d\n bitOrder: %d\n " "maxSpeed: %d\n bitPerWord: %d\n loopback: %d", _this->mode, _this->chip_select, _this->bit_order, _this->max_speed, _this->bits_per_word, _this->loopback); return true; } bool iotjs_spi_transfer(iotjs_spi_t* spi) { IOTJS_VALIDATED_STRUCT_METHOD(iotjs_spi_t, spi); int err = iotbus_spi_transfer_buf(_this->hSpi, (unsigned char*)_this->tx_buf_data, (unsigned char*)_this->rx_buf_data, _this->buf_len); if (err != 0) { DDLOG("%s - transfer failed: %d", __func__, err); return false; } return true; }
/****************************** SPI TEST ******************************/ int systemio_test_spi(char *failstr) { int result = SYSIO_RESULT_FAIL; int fail_flag = 0; int ret; iotbus_spi_context_h hSpi; unsigned int bus = 0; unsigned char txbuf[64] = { 0, }; unsigned char rxbuf[64] = { 0, };; struct iotbus_spi_config_s config = { (char)8, 0, 0, 12000000, 0, }; hSpi = iotbus_spi_open(bus, &config); if (!hSpi) { SYSIO_DEBUG("[IOTAPI] iotbus_spi_open() fail \n"); REGISTER_FAIL_REASON("iotbus_spi_open() fail", &fail_flag); goto done; } else { SYSIO_DEBUG("[IOTAPI] iotbus_spi_open() success \n"); } ret = iotbus_spi_write(hSpi, txbuf, 8); if (ret != 0) { SYSIO_DEBUG("[IOTAPI] iotbus_spi_write() fail \n"); REGISTER_FAIL_REASON("iotbus_spi_write() fail", &fail_flag); } else { SYSIO_DEBUG("[IOTAPI] iotbus_spi_write() success \n"); } ret = iotbus_spi_recv(hSpi, rxbuf, 8); if (ret != 0) { SYSIO_DEBUG("[IOTAPI] iotbus_spi_recv() fail \n"); REGISTER_FAIL_REASON("iotbus_spi_recv() fail", &fail_flag); } else { SYSIO_DEBUG("[IOTAPI] iotbus_spi_recv() success \n"); } #if 0 ret = iotbus_spi_transfer_buf(hSpi, txbuf, rxbuf, 16); if (ret != 0) { SYSIO_DEBUG("[IOTAPI] iotbus_spi_transfer_buf() fail \n"); REGISTER_FAIL_REASON("iotbus_spi_transfer_buf() fail"); } else { SYSIO_DEBUG("[IOTAPI] iotbus_spi_transfer_buf() success \n"); } #endif ret = iotbus_spi_close(hSpi); if (ret != 0) { SYSIO_DEBUG("[IOTAPI] iotbus_spi_close() fail \n"); REGISTER_FAIL_REASON("iotbus_spi_close() fail", &fail_flag); } else { SYSIO_DEBUG("[IOTAPI] iotbus_spi_close() success \n"); } done: /* check if succeed */ if (fail_flag == 0) { result = SYSIO_RESULT_SUCCESS; } return result; }