123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- /*
- 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"
- #include "boarddef.h"
- #include "onewire.h"
- /*
- ******************************************************************************
- * 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];
- static size_t devices_on_bus = 0;
- static onewire_error_t error_code;
- /*
- * 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,
- 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 onewireMeasure(void) {
- error_code = onewire_OK;
- int16_t tmp;
- uint8_t rombuf[24];
- 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;
- if (true == onewireReset(&OWD1)){
- memset(rombuf, 0x55, sizeof(rombuf));
- devices_on_bus = onewireSearchRom(&OWD1, rombuf, 3);
- if (devices_on_bus == 0) {
- error_code = onewire_No_Dev;
- return;
- }
- if (1 == devices_on_bus){
- /* test read rom command */
- presence = onewireReset(&OWD1);
- if (true != presence) {
- error_code = onewire_Dev_Lost;
- return;
- }
- testbuf[0] = ONEWIRE_CMD_READ_ROM;
- onewireWrite(&OWD1, testbuf, 1, 0);
- onewireRead(&OWD1, testbuf, 8);
- if (testbuf[7] != onewireCRC(testbuf, 7)) {
- error_code = onewire_CRC_Err;
- return;
- }
- if (0 != memcmp(rombuf, testbuf, 8)) {
- error_code = onewire_MemCmp_Err;
- return;
- }
- }
- /* start temperature measurement on all connected devices at once */
- presence = onewireReset(&OWD1);
- if (true != presence) {
- error_code = onewire_Dev_Lost;
- return;
- }
- testbuf[0] = ONEWIRE_CMD_SKIP_ROM;
- testbuf[1] = ONEWIRE_CMD_CONVERT_TEMP;
- #if ONEWIRE_USE_STRONG_PULLUP
- onewireWrite(&OWD1, testbuf, 2, TIME_MS2I(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);
- if (true != presence) {
- error_code = onewire_Dev_Lost;
- return;
- }
- 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);
- if (testbuf[8] != onewireCRC(testbuf, 8)) {
- error_code = onewire_CRC_Err;
- return;
- }
- memcpy(&tmp, &testbuf, 2);
- temperature[i] = ((int32_t)tmp * 625) / 10;
- }
- }
- else {
- devices_on_bus = 0;
- }
- osalThreadSleep(1); /* enforce ChibiOS's stack overflow check */
-
- onewireStop(&OWD1);
- }
- onewire_error_t onewireGetErrorCode(void) {
- return error_code;
- }
- uint8_t onewireGetDevicesNum(void) {
- return (uint8_t)devices_on_bus;
- }
- int32_t onewireGetTemperature(const size_t num) {
- if (num > devices_on_bus || num < devices_on_bus) {
- return -999;
- } else {
- return temperature[num];
- }
- }
|