aboutsummaryrefslogtreecommitdiffstats
path: root/Projects/TempDataLogger/Config/AppConfig.h
blob: 5ac7011f0ab0483a4c19a017ef4cb859ee7d794b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
             LUFA Library
     Copyright (C) Dean Camera, 2014.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2014  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

/** \file
 *  \brief Application Configuration Header File
 *
 *  This is a header file which is be used to configure some of
 *  the application's compile time options, as an alternative to
 *  specifying the compile time constants supplied through a
 *  makefile or build system.
 *
 *  For information on what each token does, refer to the
 *  \ref Sec_Options section of the application documentation.
 */

#ifndef _APP_CONFIG_H_
#define _APP_CONFIG_H_

	#define DUMMY_RTC

#endif
cial */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ch.h"
#include "hal.h"

#if HAL_USE_I2C || defined(__DOXYGEN__)

/**
 * @brief I2C Driver initialization.
 */
void i2cInit(void) {

  i2c_lld_init();
}

/**
 * @brief Initializes the standard part of a @p I2CDriver structure.
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 */
void i2cObjectInit(I2CDriver *i2cp) {
  chEvtInit(&i2cp->sevent);
  i2cp->errors = I2CD_NO_ERROR;
  i2cp->state = I2C_STOP;
//  i2cp->i2cd_config = NULL;
#if I2C_USE_WAIT
  i2cp->thread = NULL;
#endif /* I2C_USE_WAIT */
#if I2C_USE_MUTUAL_EXCLUSION
#if CH_USE_MUTEXES
  chMtxInit(&i2cp->mutex);
#elif CH_USE_SEMAPHORES
  chSemInit(&i2cp->semaphore, 1);
#endif
#endif /* I2C_USE_MUTUAL_EXCLUSION */
#if defined(I2C_DRIVER_EXT_INIT_HOOK)
  I2C_DRIVER_EXT_INIT_HOOK(i2cp);
#endif
}

/**
 * @brief Configures and activates the I2C peripheral.
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 * @param[in] config    pointer to the @p I2CConfig object
 */
void i2cStart(I2CDriver *i2cp, const I2CConfig *config) {

  chDbgCheck((i2cp != NULL) && (config != NULL), "i2cStart");

  chSysLock();
  chDbgAssert((i2cp->state == I2C_STOP)||(i2cp->state == I2C_READY),
              "i2cStart(), #1", "invalid state");

  i2cp->nbit_address = config->nBitAddress;
  i2c_lld_start(i2cp);
  i2c_lld_set_clock(i2cp, config->ClockSpeed, config->FastModeDutyCycle);
  i2c_lld_set_opmode(i2cp, config->opMode);
  i2c_lld_set_own_address(i2cp, config->OwnAddress1, config->nBitAddress);
  i2cp->state = I2C_READY;
  chSysUnlock();
}

/**
 * @brief Deactivates the I2C peripheral.
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 */
void i2cStop(I2CDriver *i2cp) {

  chDbgCheck(i2cp != NULL, "i2cStop");

  chSysLock();
  chDbgAssert((i2cp->state == I2C_STOP) || (i2cp->state == I2C_READY),
              "i2cStop(), #1", "invalid state");
  i2c_lld_stop(i2cp);
  i2cp->state = I2C_STOP;
  chSysUnlock();
}

/**
 * @brief Sends data ever the I2C bus.
 *
 * @param[in] i2cp           pointer to the @p I2CDriver object
 * @param[in] slave_addr     7-bit or 10-bit address of the slave
 * @param[in] n              number of words to send
 * @param[in] txbuf          the pointer to the transmit buffer
 *
 */
void i2cMasterTransmit(I2CDriver *i2cp, uint16_t slave_addr, size_t n, void *txbuf) {

  chDbgCheck((i2cp != NULL) && (n > 0) && (txbuf != NULL),
             "i2cMasterTransmit");

#if I2C_USE_WAIT
  i2c_lld_wait_bus_free(i2cp);
  if(i2c_lld_bus_is_busy(i2cp)) {
#ifdef PRINTTRACE
    print("I2C Bus busy!\n");
#endif
    return;
  };
#endif

  chSysLock();
  chDbgAssert(i2cp->state == I2C_READY,
              "i2cMasterTransmit(), #1", "not ready");

  i2cp->state = I2C_ACTIVE;
  i2c_lld_master_transmit(i2cp, slave_addr, n, txbuf);
  _i2c_wait_s(i2cp);
#if !I2C_USE_WAIT
  i2c_lld_wait_bus_free(i2cp);
#endif
  if (i2cp->state == I2C_COMPLETE)
    i2cp->state = I2C_READY;
  chSysUnlock();
}

/**
 * @brief Receives data from the I2C bus.
 *
 * @param[in] i2cp          pointer to the @p I2CDriver object
 * @param[in] slave_addr    7-bit or 10-bit address of the slave
 * @param[in] n             number of bytes to receive
 * @param[out] rxbuf        the pointer to the receive buffer
 *
 */
void i2cMasterReceive(I2CDriver *i2cp, uint16_t slave_addr, size_t n, void *rxbuf) {

  chDbgCheck((i2cp != NULL) && (n > 0) && (rxbuf != NULL),
             "i2cMasterReceive");

#if I2C_USE_WAIT
  i2c_lld_wait_bus_free(i2cp);
  if(i2c_lld_bus_is_busy(i2cp)) {
#ifdef PRINTTRACE
    print("I2C Bus busy!\n");
#endif
    return;
  };
#endif

  chSysLock();
  chDbgAssert(i2cp->state == I2C_READY,
              "i2cMasterReceive(), #1", "not ready");

  i2cp->state = I2C_ACTIVE;
  i2c_lld_master_receive(i2cp, slave_addr, n, rxbuf);
  _i2c_wait_s(i2cp);
#if !I2C_USE_WAIT
  i2c_lld_wait_bus_free(i2cp);
#endif
  if (i2cp->state == I2C_COMPLETE)
    i2cp->state = I2C_READY;
  chSysUnlock();
}

uint16_t i2cSMBusAlertResponse(I2CDriver *i2cp) {
  uint16_t slv_addr;

  i2cMasterReceive(i2cp, 0x0C, 2, &slv_addr);
  return slv_addr;
}


/**
 * @brief   Handles communication events/errors.
 * @details Must be called from the I/O interrupt service routine in order to
 *          notify I/O conditions as errors, signals change etc.
 *
 * @param[in] i2cp      pointer to a @p I2CDriver structure
 * @param[in] mask      condition flags to be added to the mask
 *
 * @iclass
 */
void i2cAddFlagsI(I2CDriver *i2cp, i2cflags_t mask) {

  chDbgCheck(i2cp != NULL, "i2cAddFlagsI");

  i2cp->errors |= mask;
  chEvtBroadcastI(&i2cp->sevent);
}

/**
 * @brief   Returns and clears the errors mask associated to the driver.
 *
 * @param[in] i2cp      pointer to a @p I2CDriver structure
 * @return              The condition flags modified since last time this
 *                      function was invoked.
 *
 * @api
 */
i2cflags_t i2cGetAndClearFlags(I2CDriver *i2cp) {
  i2cflags_t mask;

  chDbgCheck(i2cp != NULL, "i2cGetAndClearFlags");

  chSysLock();
  mask = i2cp->errors;
  i2cp->errors = I2CD_NO_ERROR;
  chSysUnlock();
  return mask;
}



#if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
/**
 * @brief Gains exclusive access to the I2C bus.
 * @details This function tries to gain ownership to the I2C bus, if the bus
 *          is already being used then the invoking thread is queued.
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 *
 * @note This function is only available when the @p I2C_USE_MUTUAL_EXCLUSION
 *       option is set to @p TRUE.
 */
void i2cAcquireBus(I2CDriver *i2cp) {

  chDbgCheck(i2cp != NULL, "i2cAcquireBus");

#if CH_USE_MUTEXES
  chMtxLock(&i2cp->mutex);
#elif CH_USE_SEMAPHORES
  chSemWait(&i2cp->semaphore);
#endif
}

/**
 * @brief Releases exclusive access to the I2C bus.
 *
 * @param[in] i2cp      pointer to the @p I2CDriver object
 *
 * @note This function is only available when the @p I2C_USE_MUTUAL_EXCLUSION
 *       option is set to @p TRUE.
 */
void i2cReleaseBus(I2CDriver *i2cp) {

  chDbgCheck(i2cp != NULL, "i2cReleaseBus");

#if CH_USE_MUTEXES
  (void)i2cp;
  chMtxUnlock();
#elif CH_USE_SEMAPHORES
  chSemSignal(&i2cp->semaphore);
#endif
}
#endif /* I2C_USE_MUTUAL_EXCLUSION */

#endif /* CH_HAL_USE_I2C */