From 555f301da2f276cc353a8e2d0a283a488bd8eeff Mon Sep 17 00:00:00 2001 From: barthess Date: Tue, 18 Oct 2016 12:02:55 +0300 Subject: 1-Wire. Duplicated code from testhal apllications moved to separate directory. --- testhal/hw_abstracted/onewire/real_roms.txt | 27 ++ testhal/hw_abstracted/onewire/synth_searchrom.c | 370 ++++++++++++++++++++++++ testhal/hw_abstracted/onewire/testhal_onewire.c | 228 +++++++++++++++ testhal/hw_abstracted/onewire/testhal_onewire.h | 28 ++ 4 files changed, 653 insertions(+) create mode 100644 testhal/hw_abstracted/onewire/real_roms.txt create mode 100644 testhal/hw_abstracted/onewire/synth_searchrom.c create mode 100644 testhal/hw_abstracted/onewire/testhal_onewire.c create mode 100644 testhal/hw_abstracted/onewire/testhal_onewire.h (limited to 'testhal/hw_abstracted/onewire') diff --git a/testhal/hw_abstracted/onewire/real_roms.txt b/testhal/hw_abstracted/onewire/real_roms.txt new file mode 100644 index 0000000..ea19c1a --- /dev/null +++ b/testhal/hw_abstracted/onewire/real_roms.txt @@ -0,0 +1,27 @@ +rombuf[0] 0x28 +rombuf[1] 0xec +rombuf[2] 0xf5 +rombuf[3] 0x67 +rombuf[4] 0x5 +rombuf[5] 0x0 +rombuf[6] 0x0 +rombuf[7] 0x1d + +rombuf[8] 0x28 +rombuf[9] 0xbd +rombuf[10] 0x1a +rombuf[11] 0x60 +rombuf[12] 0x5 +rombuf[13] 0x0 +rombuf[14] 0x0 +rombuf[15] 0x37 + +rombuf[16] 0x28 +rombuf[17] 0x83 +rombuf[18] 0x7d +rombuf[19] 0x67 +rombuf[20] 0x5 +rombuf[21] 0x0 +rombuf[22] 0x0 +rombuf[23] 0xf + diff --git a/testhal/hw_abstracted/onewire/synth_searchrom.c b/testhal/hw_abstracted/onewire/synth_searchrom.c new file mode 100644 index 0000000..53d4a30 --- /dev/null +++ b/testhal/hw_abstracted/onewire/synth_searchrom.c @@ -0,0 +1,370 @@ +/* + 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 + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +/* do not set it more than 64 because of some fill_pattern functions + will be broken.*/ +#define SYNTH_DEVICES_MAX 64 + +/* + * synthetic device + */ +typedef struct { + bool active; + uint64_t id; +} OWSynthDevice; + +/* + * synthetic bus + */ +typedef struct { + OWSynthDevice devices[SYNTH_DEVICES_MAX]; + size_t dev_present; + bool complement_bit; + ioline_t rom_bit; +} OWSynthBus; + +/* + ****************************************************************************** + * EXTERNS + ****************************************************************************** + */ + +/* + ****************************************************************************** + * PROTOTYPES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * GLOBAL VARIABLES + ****************************************************************************** + */ + +static OWSynthBus synth_bus; + +/* + * local buffer for discovered ROMs + */ +static uint64_t detected_devices[SYNTH_DEVICES_MAX]; + +/* + ****************************************************************************** + ****************************************************************************** + * LOCAL FUNCTIONS + ****************************************************************************** + ****************************************************************************** + */ + +/* + ****************************************************************************** + * EXPORTED FUNCTIONS + ****************************************************************************** + */ + +/* + * + */ +void _synth_ow_write_bit(onewireDriver *owp, ioline_t bit) { + (void)owp; + size_t i; + + for (i=0; i> synth_bus.rom_bit) & 1U) != bit) { + synth_bus.devices[i].active = false; + } + } + synth_bus.rom_bit++; +} + +/* + * + */ +ioline_t _synth_ow_read_bit(void) { + ioline_t ret = 0xFF; + size_t i; + ioline_t bit; + + for (i=0; i> synth_bus.rom_bit) & 1U; + if (synth_bus.complement_bit){ + bit ^= 1U; + } + if (0xFF == ret) + ret = bit; + else + ret &= bit; + } + } + synth_bus.complement_bit = !synth_bus.complement_bit; + return ret; +} + +/* + * + */ +static void synth_reset_pulse(void){ + size_t i; + + for (i=0; isearch_rom); + + do { + /* initialize buffer to store result */ + if (owp->search_rom.reg.devices_found >= max_rom_cnt) + owp->search_rom.retbuf = result + 8*(max_rom_cnt-1); + else + owp->search_rom.retbuf = result + 8*owp->search_rom.reg.devices_found; + memset(owp->search_rom.retbuf, 0, 8); + + /* clean iteration state */ + search_clean_iteration(&owp->search_rom); + + /**/ + synth_reset_pulse(); + synth_bus.rom_bit = 0; + synth_bus.complement_bit = false; + for (i=0; i<64*3 - 1; i++){ + ow_search_rom_cb(NULL, owp); + } + + if (ONEWIRE_SEARCH_ROM_ERROR != owp->search_rom.reg.result) { + /* store cached result for usage in next iteration */ + memcpy(owp->search_rom.prev_path, owp->search_rom.retbuf, 8); + } + } + while (ONEWIRE_SEARCH_ROM_SUCCESS == owp->search_rom.reg.result); + + /**/ + if (ONEWIRE_SEARCH_ROM_ERROR == owp->search_rom.reg.result) + return 0; + else + return owp->search_rom.reg.devices_found; +} + +/* + * + */ +static void fill_pattern_real_devices(void) { + size_t i; + + for (i=0; i> i); + } +} + +/* + * Random pattern helper + */ +static bool is_id_uniq(const OWSynthDevice *dev, size_t n, uint64_t id) { + size_t i; + + for (i=0; i + +#include "hal.h" +#include "boarddef.h" + +/* + ****************************************************************************** + * ERROR CHECKS + ****************************************************************************** + */ + +#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 + +/* + ****************************************************************************** + * DEFINES + ****************************************************************************** + */ + +/* + ****************************************************************************** + * 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]; + +/* stores 3 temperature values in millicelsius */ +static int32_t temperature[3]; + +/* + * Config for underlying PWM driver. + * Note! It is NOT constant because 1-wire driver needs to change them + * during functioning. + */ +static PWMConfig pwm_cfg = { + 0, + 0, + NULL, + { + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_DISABLED, NULL} + }, + 0, +#if STM32_PWM_USE_ADVANCED + 0, +#endif + 0 +}; + +/* + * + */ +static const onewireConfig ow_cfg = { + &PWMD3, + &pwm_cfg, + PWM_OUTPUT_ACTIVE_LOW, + 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(ONEWIRE_PORT, ONEWIRE_PIN, PAL_MODE_STM32_ALTERNATE_PUSHPULL); +} + +/** + * + */ +static void strong_pullup_release(void) { + palSetPadMode(ONEWIRE_PORT, ONEWIRE_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); +} +#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