diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index 521b98ebce6c50a493b207921a443d1d3317a1db..4a690c024642f050fe94ab68b982eb4e7cd6c072 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -164,7 +164,9 @@ endif ifneq (,$(filter io1_xplained,$(USEMODULE))) FEATURES_REQUIRED += periph_gpio + FEATURES_REQUIRED += periph_adc USEMODULE += at30tse75x + USEMODULE += sdcard_spi endif ifneq (,$(filter jc42,$(USEMODULE))) diff --git a/drivers/include/io1_xplained.h b/drivers/include/io1_xplained.h index 40f4c792dfdaf1328819b3edcb4566f1cd085c50..16d75ec8e85bfd4dcd527ebdaea3d38bd46f51c9 100644 --- a/drivers/include/io1_xplained.h +++ b/drivers/include/io1_xplained.h @@ -40,6 +40,7 @@ #include "saul.h" #include "at30tse75x.h" +#include "sdcard_spi.h" #ifdef __cplusplus extern "C" { @@ -51,11 +52,13 @@ extern "C" { enum { IO1_XPLAINED_OK = 0, /**< Initialization successful */ IO1_XPLAINED_NOTEMP, /**< Error during temperature sensor initialization */ + IO1_XPLAINED_NOSDCARD, /**< Error during sdcard initialization */ + IO1_XPLAINED_NOLIGHT, /**< Error during light sensor (ADC) initialization */ IO1_XPLAINED_NOLED, /**< Error during extension LED initialization */ IO1_XPLAINED_NOGPIO1, /**< Error during extension GPIO1 initialization */ IO1_XPLAINED_NOGPIO2, /**< Error during extension GPIO2 initialization */ - IO1_XPLAINED_READ_OK, /**< Temperature read successful */ - IO1_XPLAINED_READ_ERR /**< Error when reading temperature sensor */ + IO1_XPLAINED_READ_OK, /**< Light sensor read successful */ + IO1_XPLAINED_READ_ERR /**< Error when reading light sensor */ }; /** @@ -71,6 +74,7 @@ typedef struct { typedef struct { io1_xplained_params_t params; /**< Initialization parameters */ at30tse75x_t temp; /**< On-board temperature sensor */ + sdcard_spi_t sdcard; /**< On-board SD card */ } io1_xplained_t; /** @@ -81,6 +85,7 @@ typedef struct { * * @return IO1_XPLAINED_OK on success * @return -IO1_XPLAINED_NOTEMP if temperature sensor initialization failed + * @return -IO1_XPLAINED_NOSDCARD if sdcard initialization failed * @return -IO1_XPLAINED_NOLED if LED initialization failed * @return -IO1_XPLAINED_NOGPIO1 if GPIO1 initialization failed * @return -IO1_XPLAINED_NOGPIO2 if GPIO2 initialization failed @@ -88,30 +93,14 @@ typedef struct { int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params); /** - * @brief Read temperature value from the given IO1 Xplained extension, returned in °C + * @brief Read light sensor level on the IO1 Xplained extension * - * @param[in] dev Device descriptor of IO1 Xplained to read from - * @param[out] temperature Temperature in °C + * @param[out] light Light level value (between 0 and 1023) * * @return IO1_XPLAINED_READ_OK on success - * @return -IO1_XPLAINED_READ_ERR if temperature sensor read failed + * @return -IO1_XPLAINED_READ_ERR when the value cannot be read */ -int io1_xplained_read_temperature(const io1_xplained_t *dev, float *temperature); - -/** - * @brief Set the on-board led of the IO1 Xplained extension - */ -void io1_xplained_set_led(void); - -/** - * @brief Clear the on-board led of the IO1 Xplained extension - */ -void io1_xplained_clear_led(void); - -/** - * @brief Toggle the on-board led of the IO1 Xplained extension - */ -void io1_xplained_toggle_led(void); +int io1_xplained_read_light_level(uint16_t *light); #ifdef __cplusplus } diff --git a/drivers/io1_xplained/include/io1_xplained_internals.h b/drivers/io1_xplained/include/io1_xplained_internals.h index 912b85dd54eed4164d6668d05c69a213fa9b44f5..19998da08b42fdcefee48a5a4c601ba4bf1f3db6 100644 --- a/drivers/io1_xplained/include/io1_xplained_internals.h +++ b/drivers/io1_xplained/include/io1_xplained_internals.h @@ -22,7 +22,9 @@ #include "cpu.h" #include "periph_cpu.h" - +#include "periph/adc.h" +#include "periph/gpio.h" +#include "periph/spi.h" #ifdef __cplusplus extern "C" { @@ -32,8 +34,30 @@ extern "C" { * @name IO1 Xplained I2C addresses * @{ */ -#define TEMPERATURE_BASE_ADDR (0x48) -#define TEMPERATURE_DEFAULT_ADDR (0x07) +#define IO1_TEMPERATURE_BASE_ADDR (0x48) +#define IO1_TEMPERATURE_DEFAULT_ADDR (0x07) +/** @} */ + +/** + * @name IO1 Xplained SD Card configuration + * @{ + */ +#define IO1_SDCARD_SPI_PARAM_SPI SPI_DEV(1) +#define IO1_SDCARD_SPI_PARAM_CS GPIO_PIN(1,3) +#define IO1_SDCARD_SPI_PARAM_CLK GPIO_PIN(1,23) +#define IO1_SDCARD_SPI_PARAM_MOSI GPIO_PIN(1,22) +#define IO1_SDCARD_SPI_PARAM_MISO GPIO_PIN(1,2) +#define IO1_SDCARD_SPI_PARAM_POWER (GPIO_UNDEF) +#define IO1_SDCARD_SPI_PARAM_POWER_AH (true) +#define IO1_SDCARD_SPI_PARAM_DETECT GPIO_PIN(0,23) +/** @} */ + +/** + * @name IO1 Xplained Light sensor ADC configuration + * @{ + */ +#define IO1_LIGHT_ADC_LINE ADC_LINE(0) +#define IO1_LIGHT_ADC_RES ADC_RES_10BIT /** @} */ /** diff --git a/drivers/io1_xplained/io1_xplained.c b/drivers/io1_xplained/io1_xplained.c index c4aa592eca042d0f972b84944aaf728e2cc2f6c3..16c6d053a8b69343c6bb90dc613bb21516c4aaf5 100644 --- a/drivers/io1_xplained/io1_xplained.c +++ b/drivers/io1_xplained/io1_xplained.c @@ -18,13 +18,18 @@ * @} */ -#include "log.h" #include "io1_xplained.h" #include "io1_xplained_internals.h" + #include "at30tse75x.h" +#include "sdcard_spi.h" + #include "periph/i2c.h" #include "periph/gpio.h" +#define ENABLE_DEBUG (0) +#include "debug.h" + /*---------------------------------------------------------------------------* * IO1 Xplained Core API * *---------------------------------------------------------------------------*/ @@ -36,54 +41,73 @@ int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params) /* Initialize I2C interface */ if (at30tse75x_init(&dev->temp, I2C_DEV(0), - I2C_SPEED_NORMAL, (TEMPERATURE_BASE_ADDR | dev->params.addr)) < 0) { - LOG_ERROR("Cannot initialize temperature sensor.\n"); + I2C_SPEED_NORMAL, + (IO1_TEMPERATURE_BASE_ADDR | dev->params.addr)) < 0) { + DEBUG("[io1_xplained] Cannot initialize temperature sensor.\n"); return -IO1_XPLAINED_NOTEMP; } /* Use maximum resolution */ at30tse75x_set_resolution(&dev->temp, AT30TSE75X_RESOLUTION_12BIT); + if (gpio_init(IO1_SDCARD_SPI_PARAM_DETECT, GPIO_IN) < 0) { + DEBUG("[io1_xplained] SD Card detect initialization failed\n"); + return -IO1_XPLAINED_NOSDCARD; + } + + /* Card detect pin is inverted */ + if (!gpio_read(IO1_SDCARD_SPI_PARAM_DETECT)) { + /* Initialize the SD Card */ + sdcard_spi_params_t sdcard_params = { + .spi_dev = IO1_SDCARD_SPI_PARAM_SPI, + .cs = IO1_SDCARD_SPI_PARAM_CS, + .clk = IO1_SDCARD_SPI_PARAM_CLK, + .mosi = IO1_SDCARD_SPI_PARAM_MOSI, + .miso = IO1_SDCARD_SPI_PARAM_MISO, + .power = IO1_SDCARD_SPI_PARAM_POWER, + .power_act_high = IO1_SDCARD_SPI_PARAM_POWER_AH + }; + if (sdcard_spi_init(&dev->sdcard, &sdcard_params) != 0) { + DEBUG("[io1_xplained] SD Card initialization failed\n"); + return -IO1_XPLAINED_NOSDCARD; + } + } + + if (adc_init(IO1_LIGHT_ADC_LINE) < 0) { + DEBUG("[io1_xplained] Light sensor (ADC) initialization failed\n"); + return -IO1_XPLAINED_NOLIGHT; + } + if (gpio_init(IO1_LED_PIN, GPIO_OUT) < 0) { - LOG_ERROR("GPIO LED not enabled\n"); + DEBUG("[io1_xplained] LED initialization failed\n"); return -IO1_XPLAINED_NOLED; } if (gpio_init(IO1_GPIO1_PIN, GPIO_OUT) < 0) { - LOG_ERROR("GPIO1 not enabled\n"); + DEBUG("[io1_xplained] GPIO1 initialization failed\n"); return -IO1_XPLAINED_NOGPIO1; } if (gpio_init(IO1_GPIO2_PIN, GPIO_OUT) < 0) { - LOG_ERROR("GPIO2 not enabled\n"); + DEBUG("[io1_xplained] GPIO2 initialization failed\n"); return -IO1_XPLAINED_NOGPIO2; } - LOG_DEBUG("IO1 Xplained extension initialized!\n"); + DEBUG("IO1 Xplained extension initialized with success!\n"); return IO1_XPLAINED_OK; } -int io1_xplained_read_temperature(const io1_xplained_t *dev, float *temperature) +int io1_xplained_read_light_level(uint16_t *light) { - if (at30tse75x_get_temperature(&dev->temp, temperature) < 0) { - LOG_ERROR("Cannot read temperature sensor.\n"); + int sample = adc_sample(IO1_LIGHT_ADC_LINE, IO1_LIGHT_ADC_RES); + + if (sample < 0) { + DEBUG("[io1_xplained] Light sensor read failed\n"); return -IO1_XPLAINED_READ_ERR; } - return IO1_XPLAINED_READ_OK; -} -void io1_xplained_set_led(void) -{ - gpio_set(IO1_LED_PIN); -} + *light = (1023 - sample); -void io1_xplained_clear_led(void) -{ - gpio_clear(IO1_LED_PIN); -} - -void io1_xplained_toggle_led(void) -{ - gpio_toggle(IO1_LED_PIN); + return IO1_XPLAINED_READ_OK; } diff --git a/drivers/io1_xplained/io1_xplained_saul.c b/drivers/io1_xplained/io1_xplained_saul.c index ed56e621a0a8adae8b7a3c9e7aeae4be30e5e989..bf051df408f09b2bfefb078f6379cf8afd87099f 100644 --- a/drivers/io1_xplained/io1_xplained_saul.c +++ b/drivers/io1_xplained/io1_xplained_saul.c @@ -22,12 +22,14 @@ #include "saul.h" #include "io1_xplained.h" +#include "at30tse75x.h" static float temperature; static int read_temperature(const void *dev, phydat_t *res) { - io1_xplained_read_temperature((const io1_xplained_t *)dev, &temperature); + const io1_xplained_t *io1 = (const io1_xplained_t *)dev; + at30tse75x_get_temperature(&io1->temp, &temperature); res->val[0] = (int)(temperature * 100.0); res->unit = UNIT_TEMP_C; res->scale = -2; diff --git a/tests/driver_io1_xplained/main.c b/tests/driver_io1_xplained/main.c index 60e0f908243fccfcfa471a92568c696c3634ecdb..5ccf8b4ab1ad441eb94647d79febc332d0686689 100644 --- a/tests/driver_io1_xplained/main.c +++ b/tests/driver_io1_xplained/main.c @@ -21,16 +21,37 @@ #include <stdio.h> #include <inttypes.h> -#include "io1_xplained.h" -#include "io1_xplained_params.h" #include "xtimer.h" #include "board.h" -#define SLEEP_1S (1 * 1000 * 1000u) /* 1 seconds delay between each test */ +#include "periph/gpio.h" + +#include "at30tse75x.h" +#include "sdcard_spi.h" +#include "io1_xplained.h" +#include "io1_xplained_params.h" + +#define DELAY_1S (1U) /* 1 seconds delay between each test */ + +static io1_xplained_t dev; + +static void _sd_card_cid(void) +{ + puts("SD Card CID info:"); + printf("MID: %d\n", dev.sdcard.cid.MID); + printf("OID: %c%c\n", dev.sdcard.cid.OID[0], dev.sdcard.cid.OID[1]); + printf("PNM: %c%c%c%c%c\n", + dev.sdcard.cid.PNM[0], dev.sdcard.cid.PNM[1], dev.sdcard.cid.PNM[2], + dev.sdcard.cid.PNM[3], dev.sdcard.cid.PNM[4]); + printf("PRV: %d\n", dev.sdcard.cid.PRV); + printf("PSN: %lu\n", dev.sdcard.cid.PSN); + printf("MDT: %d\n", dev.sdcard.cid.MDT); + printf("CRC: %d\n", dev.sdcard.cid.CID_CRC); + puts("+----------------------------------------+\n"); +} int main(void) { - io1_xplained_t dev; float temperature; puts("IO1 Xplained extention test application\n"); @@ -45,27 +66,40 @@ int main(void) puts("\n+--------Starting tests --------+"); while (1) { /* Get temperature in degrees celsius */ - io1_xplained_read_temperature(&dev, &temperature); + at30tse75x_get_temperature(&dev.temp, &temperature); printf("Temperature [°C]: %.2f\n" - "\n+-------------------------------------+\n", + "+-------------------------------------+\n", temperature); - xtimer_usleep(SLEEP_1S); + xtimer_sleep(DELAY_1S); + + /* Card detect pin is inverted */ + if (!gpio_read(IO1_SDCARD_SPI_PARAM_DETECT)) { + _sd_card_cid(); + xtimer_sleep(DELAY_1S); + } + + uint16_t light; + io1_xplained_read_light_level(&light); + printf("Light level: %i\n" + "+-------------------------------------+\n", + light); + xtimer_sleep(DELAY_1S); /* set led */ - io1_xplained_set_led(); - xtimer_usleep(SLEEP_1S); + gpio_set(IO1_LED_PIN); + xtimer_sleep(DELAY_1S); /* clear led */ - io1_xplained_clear_led(); - xtimer_usleep(SLEEP_1S); + gpio_clear(IO1_LED_PIN); + xtimer_sleep(DELAY_1S); /* toggle led */ - io1_xplained_toggle_led(); - xtimer_usleep(SLEEP_1S); + gpio_toggle(IO1_LED_PIN); + xtimer_sleep(DELAY_1S); /* toggle led again */ - io1_xplained_toggle_led(); - xtimer_usleep(SLEEP_1S); + gpio_toggle(IO1_LED_PIN); + xtimer_sleep(DELAY_1S); } return 0;