|
| 1 | +/** |
| 2 | + * @brief Implement SPI section of miscellaneous/testing_plan. |
| 3 | + * @author Francois Berder |
| 4 | + * @date 2016 |
| 5 | + * @copyright 3-clause BSD |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +#include <linux/spi/spidev.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <stdlib.h> |
| 12 | +#include <string.h> |
| 13 | +#include "common.h" |
| 14 | +#include "core/common.h" |
| 15 | +#include "core/spi.h" |
| 16 | + |
| 17 | +#define ADXL345_DEVICE_ID_REG (0x00) |
| 18 | +#define ADXL345_DEVICE_ID (0xE5) |
| 19 | + |
| 20 | +static bool test_spi_set_mode_before_init(void) |
| 21 | +{ |
| 22 | + return spi_set_mode(MIKROBUS_1, SPI_MODE_0) == -1 |
| 23 | + && spi_set_mode(MIKROBUS_2, SPI_MODE_0) == -1; |
| 24 | +} |
| 25 | + |
| 26 | +static bool test_spi_set_speed_before_init(void) |
| 27 | +{ |
| 28 | + return spi_set_mode(MIKROBUS_1, 1000000) == -1 |
| 29 | + && spi_set_mode(MIKROBUS_2, 1000000) == -1; |
| 30 | +} |
| 31 | + |
| 32 | +static bool test_spi_transfer_before_init(void) |
| 33 | +{ |
| 34 | + uint8_t tx_buffer = 0, rx_buffer = 0; |
| 35 | + |
| 36 | + return spi_transfer(tx_buffer, rx_buffer, 1) == -1; |
| 37 | +} |
| 38 | + |
| 39 | +static bool test_spi_init(void) |
| 40 | +{ |
| 41 | + if (spi_init() < 0) |
| 42 | + return false; |
| 43 | + |
| 44 | + if (spi_init() < 0) |
| 45 | + return false; |
| 46 | + |
| 47 | + return spi_get_current_bus() == MIKROBUS_1; |
| 48 | +} |
| 49 | + |
| 50 | +static bool test_spi_transfer_zero_byte(void) |
| 51 | +{ |
| 52 | + return spi_transfer(NULL, NULL, 0) == 0; |
| 53 | +} |
| 54 | + |
| 55 | +static bool test_spi_transfer_null_buffers(void) |
| 56 | +{ |
| 57 | + return spi_transfer(NULL, NULL, 1) == -1; |
| 58 | +} |
| 59 | + |
| 60 | +static bool read_accel_product_id(uint8_t mikrobus_index) |
| 61 | +{ |
| 62 | + int ret = -1; |
| 63 | + uint8_t tx_buffer[2], rx_buffer[2]; |
| 64 | + |
| 65 | + spi_select_bus(mikrobus_index); |
| 66 | + if (spi_get_current_bus() != mikrobus_index) |
| 67 | + return false; |
| 68 | + |
| 69 | + ret = ask_question("Do you have an Accel Click (SPI) ?", 15); |
| 70 | + if (ret < 0) |
| 71 | + return false; |
| 72 | + else if (ret == 2) |
| 73 | + return true; |
| 74 | + |
| 75 | + printf("Insert your Accel Click in mikrobus %u\n", mikrobus_index+1); |
| 76 | + printf("Press a switch when ready.\n"); |
| 77 | + if (wait_for_switch(10) < 0) |
| 78 | + return false; |
| 79 | + |
| 80 | + tx_buffer[0] = 0x80 | ADXL345_DEVICE_ID_REG; |
| 81 | + tx_buffer[1] = 0; |
| 82 | + if (spi_transfer(tx_buffer, rx_buffer, 2) < 0) |
| 83 | + return false; |
| 84 | + |
| 85 | + return rx_buffer[1] == ADXL345_DEVICE_ID; |
| 86 | +} |
| 87 | + |
| 88 | +static bool test_spi_read_id_mikrobus_1(void) |
| 89 | +{ |
| 90 | + return read_accel_product_id(MIKROBUS_1); |
| 91 | +} |
| 92 | + |
| 93 | +static bool test_spi_read_id_mikrobus_2(void) |
| 94 | +{ |
| 95 | + return read_accel_product_id(MIKROBUS_2); |
| 96 | +} |
| 97 | + |
| 98 | +static bool test_spi_release(void) |
| 99 | +{ |
| 100 | + return spi_release() == 0 |
| 101 | + && spi_release() == 0; |
| 102 | +} |
| 103 | + |
| 104 | +int main(void) |
| 105 | +{ |
| 106 | + int ret = -1; |
| 107 | + |
| 108 | + CREATE_TEST(spi, 9); |
| 109 | + ADD_TEST_CASE(spi, set_mode_before_init); |
| 110 | + ADD_TEST_CASE(spi, set_speed_before_init); |
| 111 | + ADD_TEST_CASE(spi, transfer_before_init); |
| 112 | + ADD_TEST_CASE(spi, init); |
| 113 | + ADD_TEST_CASE(spi, transfer_zero_byte); |
| 114 | + ADD_TEST_CASE(spi, transfer_null_buffers); |
| 115 | + ADD_TEST_CASE(spi, read_id_mikrobus_1); |
| 116 | + ADD_TEST_CASE(spi, read_id_mikrobus_2); |
| 117 | + ADD_TEST_CASE(spi, release); |
| 118 | + |
| 119 | + ret = run_test(test_spi); |
| 120 | + free(test_spi.cases); |
| 121 | + |
| 122 | + return ret; |
| 123 | +} |
0 commit comments