aboutsummaryrefslogtreecommitdiffstats
path: root/testhal/STM32/STM32F0xx/onewire/onewire_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'testhal/STM32/STM32F0xx/onewire/onewire_test.c')
-rw-r--r--testhal/STM32/STM32F0xx/onewire/onewire_test.c235
1 files changed, 235 insertions, 0 deletions
diff --git a/testhal/STM32/STM32F0xx/onewire/onewire_test.c b/testhal/STM32/STM32F0xx/onewire/onewire_test.c
new file mode 100644
index 0000000..682ed71
--- /dev/null
+++ b/testhal/STM32/STM32F0xx/onewire/onewire_test.c
@@ -0,0 +1,235 @@
+/*
+ ChibiOS/RT - Copyright (C) 2014 Uladzimir Pylinsky aka barthess
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include <string.h>
+
+#include "hal.h"
+
+/*
+ ******************************************************************************
+ * DEFINES
+ ******************************************************************************
+ */
+
+#if defined(BOARD_ST_STM32F4_DISCOVERY) || \
+ defined(BOARD_ST_STM32F0_DISCOVERY) || \
+ defined(BOARD_ST_STM32F0308_DISCOVERY)
+ #if ONEWIRE_USE_STRONG_PULLUP
+ #error "This board has not enough voltage for this feature"
+ #endif
+#endif
+
+#if defined(BOARD_ST_STM32F0308_DISCOVERY)
+ #define ONEWIRE_PORT GPIOB
+ #define ONEWIRE_PIN GPIOB_PIN0
+ #define ONEWIRE_PAD_MODE_ACTIVE (PAL_MODE_ALTERNATE(1) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUDR_PULLUP)
+ #define search_led_off() (palClearPad(GPIOC, GPIOC_LED4))
+ #define search_led_on() (palSetPad(GPIOC, GPIOC_LED4))
+ #define ONEWIRE_MASTER_CHANNEL 2
+ #define ONEWIRE_SAMPLE_CHANNEL 3
+#elif defined(BOARD_ST_STM32F4_DISCOVERY)
+ #define ONEWIRE_PORT GPIOB
+ #define ONEWIRE_PIN GPIOB_PIN0
+ #define ONEWIRE_PAD_MODE_ACTIVE (PAL_MODE_ALTERNATE(2) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUDR_PULLUP)
+ #define search_led_off() (palClearPad(GPIOD, GPIOD_LED4))
+ #define search_led_on() (palSetPad(GPIOD, GPIOD_LED4))
+ #define ONEWIRE_MASTER_CHANNEL 2
+ #define ONEWIRE_SAMPLE_CHANNEL 3
+#elif defined(BOARD_OLIMEX_STM32_103STK)
+ #define ONEWIRE_PORT GPIOB
+ #define ONEWIRE_PIN 0
+ #define ONEWIRE_PAD_MODE_IDLE PAL_MODE_INPUT
+ #define ONEWIRE_PAD_MODE_ACTIVE PAL_MODE_STM32_ALTERNATE_OPENDRAIN
+ #define search_led_on() (palClearPad(GPIOC, GPIOC_LED))
+ #define search_led_off() (palSetPad(GPIOC, GPIOC_LED))
+ #define ONEWIRE_MASTER_CHANNEL 2
+ #define ONEWIRE_SAMPLE_CHANNEL 3
+#else
+ #define ONEWIRE_PORT GPIOB
+ #define GPIOB_ONEWIRE GPIOB_TACHOMETER
+ #include "pads.h"
+ #define ONEWIRE_PAD_MODE_ACTIVE (PAL_MODE_ALTERNATE(2) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUDR_PULLUP)
+ #define search_led_on red_led_on
+ #define search_led_off red_led_off
+ #define ONEWIRE_MASTER_CHANNEL 2
+ #define ONEWIRE_SAMPLE_CHANNEL 3
+#endif
+
+/*
+ ******************************************************************************
+ * EXTERNS
+ ******************************************************************************
+ */
+
+/*
+ ******************************************************************************
+ * PROTOTYPES
+ ******************************************************************************
+ */
+/*
+ * Forward declarations
+ */
+#if ONEWIRE_USE_STRONG_PULLUP
+static void strong_pullup_assert(void);
+static void strong_pullup_release(void);
+#endif
+
+/*
+ ******************************************************************************
+ * GLOBAL VARIABLES
+ ******************************************************************************
+ */
+
+static uint8_t testbuf[12];
+
+static int32_t temperature[3];
+
+/*
+ *
+ */
+static const onewireConfig ow_cfg = {
+ &PWMD3,
+ ONEWIRE_MASTER_CHANNEL,
+ ONEWIRE_SAMPLE_CHANNEL,
+ ONEWIRE_PORT,
+ ONEWIRE_PIN,
+#if defined(STM32F1XX)
+ ONEWIRE_PAD_MODE_IDLE,
+#endif
+ ONEWIRE_PAD_MODE_ACTIVE,
+#if ONEWIRE_USE_STRONG_PULLUP
+ strong_pullup_assert,
+ strong_pullup_release
+#endif
+};
+
+/*
+ ******************************************************************************
+ ******************************************************************************
+ * LOCAL FUNCTIONS
+ ******************************************************************************
+ ******************************************************************************
+ */
+
+#if ONEWIRE_USE_STRONG_PULLUP
+/**
+ *
+ */
+static void strong_pullup_assert(void) {
+ palSetPadMode(GPIOB, GPIOB_ONEWIRE, PAL_MODE_ALTERNATE(2) |
+ PAL_STM32_OTYPE_PUSHPULL | PAL_STM32_PUDR_PULLUP);
+}
+
+/**
+ *
+ */
+static void strong_pullup_release(void) {
+ palSetPadMode(GPIOB, GPIOB_ONEWIRE, PAL_MODE_ALTERNATE(2) |
+ PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUDR_PULLUP);
+}
+#endif /* ONEWIRE_USE_STRONG_PULLUP */
+
+/*
+ ******************************************************************************
+ * EXPORTED FUNCTIONS
+ ******************************************************************************
+ */
+
+/**
+ *
+ */
+void onewireTest(void) {
+
+ int16_t tmp;
+ uint8_t rombuf[24];
+ size_t devices_on_bus = 0;
+ size_t i = 0;
+ bool presence;
+
+ onewireObjectInit(&OWD1);
+ onewireStart(&OWD1, &ow_cfg);
+
+#if ONEWIRE_SYNTH_SEARCH_TEST
+ synthSearchRomTest(&OWD1);
+#endif
+
+ for (i=0; i<3; i++)
+ temperature[i] = -666;
+
+ while (true) {
+ if (true == onewireReset(&OWD1)){
+
+ memset(rombuf, 0x55, sizeof(rombuf));
+ search_led_on();
+ devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
+ search_led_off();
+ osalDbgCheck(devices_on_bus <= 3);
+ osalDbgCheck(devices_on_bus > 0);
+
+ if (1 == devices_on_bus){
+ /* test read rom command */
+ presence = onewireReset(&OWD1);
+ osalDbgCheck(true == presence);
+ testbuf[0] = ONEWIRE_CMD_READ_ROM;
+ onewireWrite(&OWD1, testbuf, 1, 0);
+ onewireRead(&OWD1, testbuf, 8);
+ osalDbgCheck(testbuf[7] == onewireCRC(testbuf, 7));
+ osalDbgCheck(0 == memcmp(rombuf, testbuf, 8));
+ }
+
+ /* start temperature measurement on all connected devices at once */
+ presence = onewireReset(&OWD1);
+ osalDbgCheck(true == presence);
+ testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
+ testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP;
+
+#if ONEWIRE_USE_STRONG_PULLUP
+ onewireWrite(&OWD1, testbuf, 2, MS2ST(750));
+#else
+ onewireWrite(&OWD1, testbuf, 2, 0);
+ /* poll bus waiting ready signal from all connected devices */
+ testbuf[0] = 0;
+ while (testbuf[0] == 0){
+ osalThreadSleepMilliseconds(50);
+ onewireRead(&OWD1, testbuf, 1);
+ }
+#endif
+
+ for (i=0; i<devices_on_bus; i++) {
+ /* read temperature device by device from their scratchpads */
+ presence = onewireReset(&OWD1);
+ osalDbgCheck(true == presence);
+
+ testbuf[0] = ONEWIRE_CMD_MATCH_ROM;
+ memcpy(&testbuf[1], &rombuf[i*8], 8);
+ testbuf[9] = ONEWIRE_CMD_READ_SCRATCHPAD;
+ onewireWrite(&OWD1, testbuf, 10, 0);
+
+ onewireRead(&OWD1, testbuf, 9);
+ osalDbgCheck(testbuf[8] == onewireCRC(testbuf, 8));
+ tmp = 0;
+ tmp |= (testbuf[1] << 8) | testbuf[0];
+ temperature[i] = (tmp * 625) / 10;
+ }
+ }
+ else {
+ osalSysHalt("No devices found");
+ }
+ osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
+ }
+
+ onewireStop(&OWD1);
+}