diff options
author | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2013-06-15 15:58:20 +0000 |
---|---|---|
committer | gdisirio <gdisirio@35acf78f-673a-0410-8e92-d51de3d6d3f4> | 2013-06-15 15:58:20 +0000 |
commit | 7c68ef157d009f9932ac47ba21ba5d74e321623f (patch) | |
tree | 6c2442ca44cd090656ec3059ffb959f3d747e18d /os/hal/platforms/AVR | |
parent | 076746af63d317f8e96766b9137a65679f60463f (diff) | |
parent | e0d850113610f3efa0c0ac4946901f683e5e7332 (diff) | |
download | ChibiOS-7c68ef157d009f9932ac47ba21ba5d74e321623f.tar.gz ChibiOS-7c68ef157d009f9932ac47ba21ba5d74e321623f.tar.bz2 ChibiOS-7c68ef157d009f9932ac47ba21ba5d74e321623f.zip |
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@5854 35acf78f-673a-0410-8e92-d51de3d6d3f4
Diffstat (limited to 'os/hal/platforms/AVR')
-rw-r--r-- | os/hal/platforms/AVR/hal_lld.c | 57 | ||||
-rw-r--r-- | os/hal/platforms/AVR/hal_lld.h | 72 | ||||
-rw-r--r-- | os/hal/platforms/AVR/i2c_lld.c | 289 | ||||
-rw-r--r-- | os/hal/platforms/AVR/i2c_lld.h | 224 | ||||
-rw-r--r-- | os/hal/platforms/AVR/pal_lld.c | 157 | ||||
-rw-r--r-- | os/hal/platforms/AVR/pal_lld.h | 329 | ||||
-rw-r--r-- | os/hal/platforms/AVR/platform.dox | 110 | ||||
-rw-r--r-- | os/hal/platforms/AVR/platform.mk | 8 | ||||
-rw-r--r-- | os/hal/platforms/AVR/serial_lld.c | 360 | ||||
-rw-r--r-- | os/hal/platforms/AVR/serial_lld.h | 158 |
10 files changed, 1764 insertions, 0 deletions
diff --git a/os/hal/platforms/AVR/hal_lld.c b/os/hal/platforms/AVR/hal_lld.c new file mode 100644 index 000000000..e01e291d9 --- /dev/null +++ b/os/hal/platforms/AVR/hal_lld.c @@ -0,0 +1,57 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/hal_lld.c
+ * @brief AVR HAL subsystem low level driver code.
+ *
+ * @addtogroup HAL
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level HAL driver initialization.
+ *
+ * @notapi
+ */
+void hal_lld_init(void) {
+
+}
+
+/** @} */
diff --git a/os/hal/platforms/AVR/hal_lld.h b/os/hal/platforms/AVR/hal_lld.h new file mode 100644 index 000000000..fd99a70aa --- /dev/null +++ b/os/hal/platforms/AVR/hal_lld.h @@ -0,0 +1,72 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/hal_lld.h
+ * @brief AVR HAL subsystem low level driver header.
+ *
+ * @addtogroup HAL
+ * @{
+ */
+
+#ifndef _HAL_LLD_H_
+#define _HAL_LLD_H_
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/**
+ * @brief Defines the support for realtime counters in the HAL.
+ */
+#define HAL_IMPLEMENTS_COUNTERS FALSE
+
+/**
+ * @brief Platform name.
+ */
+#define PLATFORM_NAME "ATmega128"
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void hal_lld_init(void);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _HAL_LLD_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/i2c_lld.c b/os/hal/platforms/AVR/i2c_lld.c new file mode 100644 index 000000000..8db158fbd --- /dev/null +++ b/os/hal/platforms/AVR/i2c_lld.c @@ -0,0 +1,289 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/i2c_lld.c
+ * @brief AVR I2C subsystem low level driver source.
+ *
+ * @addtogroup I2C
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#if HAL_USE_I2C || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver local definitions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/** @brief I2C driver identifier.*/
+#if USE_AVR_I2C || defined(__DOXYGEN__)
+I2CDriver I2CD;
+#endif
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Wakes up the waiting thread.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ * @param[in] msg wakeup message
+ *
+ * @notapi
+ */
+#define wakeup_isr(i2cp, msg) { \
+ chSysLockFromIsr(); \
+ if ((i2cp)->thread != NULL) { \
+ Thread *tp = (i2cp)->thread; \
+ (i2cp)->thread = NULL; \
+ tp->p_u.rdymsg = (msg); \
+ chSchReadyI(tp); \
+ } \
+ chSysUnlockFromIsr(); \
+}
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+#if USE_AVR_I2C || defined(__DOXYGEN__)
+/**
+ * @brief I2C event interrupt handler.
+ *
+ * @notapi
+ */
+CH_IRQ_HANDLER(TWI_vect) {
+ CH_IRQ_PROLOGUE();
+
+ I2CDriver *i2cp = &I2CD;
+
+ switch (TWSR & 0xF8) {
+ case TWI_START:
+ case TWI_REPEAT_START:
+ TWDR = (i2cp->addr << 1);
+ if ((i2cp->txbuf == NULL) || (i2cp->txbytes == 0) || (i2cp->txidx == i2cp->txbytes)) {
+ TWDR |= 0x01;
+ }
+ TWCR = ((1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ break;
+ case TWI_MASTER_TX_ADDR_ACK:
+ case TWI_MASTER_TX_DATA_ACK:
+ if (i2cp->txidx < i2cp->txbytes) {
+ TWDR = i2cp->txbuf[i2cp->txidx++];
+ TWCR = ((1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ } else {
+ if (i2cp->rxbuf && i2cp->rxbytes) {
+ TWCR = ((1 << TWSTA) | (1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ } else {
+ TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
+ wakeup_isr(i2cp, RDY_OK);
+ }
+ }
+ break;
+ case TWI_MASTER_RX_ADDR_ACK:
+ if (i2cp->rxidx == (i2cp->rxbytes - 1)) {
+ TWCR = ((1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ } else {
+ TWCR = ((1 << TWEA) | (1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ }
+ break;
+ case TWI_MASTER_RX_DATA_ACK:
+ i2cp->rxbuf[i2cp->rxidx++] = TWDR;
+ if (i2cp->rxidx == (i2cp->rxbytes - 1)) {
+ TWCR = ((1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ } else {
+ TWCR = ((1 << TWEA) | (1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+ }
+ break;
+ case TWI_MASTER_RX_DATA_NACK:
+ i2cp->rxbuf[i2cp->rxidx] = TWDR;
+ TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
+ wakeup_isr(i2cp, RDY_OK);
+ case TWI_MASTER_TX_ADDR_NACK:
+ case TWI_MASTER_TX_DATA_NACK:
+ case TWI_MASTER_RX_ADDR_NACK:
+ i2cp->errors |= I2CD_ACK_FAILURE;
+ break;
+ case TWI_ARBITRATION_LOST:
+ i2cp->errors |= I2CD_ARBITRATION_LOST;
+ break;
+ case TWI_BUS_ERROR:
+ i2cp->errors |= I2CD_BUS_ERROR;
+ break;
+ default:
+ /* FIXME: only gets here if there are other MASTERs in the bus */
+ TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
+ wakeup_isr(i2cp, RDY_RESET);
+ }
+
+ if (i2cp->errors != I2CD_NO_ERROR) {
+ TWCR = ((1 << TWSTO) | (1 << TWINT) | (1 << TWEN));
+ wakeup_isr(i2cp, RDY_RESET);
+ }
+
+ CH_IRQ_EPILOGUE();
+}
+#endif /* USE_AVR_I2C */
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level I2C driver initialization.
+ *
+ * @notapi
+ */
+void i2c_lld_init(void) {
+ i2cObjectInit(&I2CD);
+}
+
+/**
+ * @brief Configures and activates the I2C peripheral.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ *
+ * @notapi
+ */
+void i2c_lld_start(I2CDriver *i2cp) {
+
+ /* TODO: Test TWI without external pull-ups (use internal) */
+
+ /* Configure prescaler to 1 */
+ TWSR &= 0xF8;
+
+ /* Configure baudrate */
+ TWBR = ((F_CPU / i2cp->config->clock_speed) - 16) / 2;
+}
+
+/**
+ * @brief Deactivates the I2C peripheral.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ *
+ * @notapi
+ */
+void i2c_lld_stop(I2CDriver *i2cp) {
+
+ if (i2cp->state != I2C_STOP) {
+ /* Disable TWI subsystem and stop all operations */
+ TWCR &= ~(1 << TWEN);
+ }
+}
+
+/**
+ * @brief Receives data via the I2C bus as master.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ * @param[in] addr slave device address
+ * @param[out] rxbuf pointer to the receive buffer
+ * @param[in] rxbytes number of bytes to be received
+ * @param[in] timeout the number of ticks before the operation timeouts,
+ * the following special values are allowed:
+ * - @a TIME_INFINITE no timeout.
+ * .
+ * @return The operation status.
+ * @retval RDY_OK if the function succeeded.
+ * @retval RDY_RESET if one or more I2C errors occurred, the errors can
+ * be retrieved using @p i2cGetErrors().
+ * @retval RDY_TIMEOUT if a timeout occurred before operation end. <b>After a
+ * timeout the driver must be stopped and restarted
+ * because the bus is in an uncertain state</b>.
+ *
+ * @notapi
+ */
+msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
+ uint8_t *rxbuf, size_t rxbytes,
+ systime_t timeout) {
+ i2cp->addr = addr;
+ i2cp->txbuf = NULL;
+ i2cp->txbytes = 0;
+ i2cp->txidx = 0;
+ i2cp->rxbuf = rxbuf;
+ i2cp->rxbytes = rxbytes;
+ i2cp->rxidx = 0;
+
+ /* Send START */
+ TWCR = ((1 << TWSTA) | (1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+
+ chSysLock();
+ i2cp->thread = chThdSelf();
+ chSchGoSleepS(THD_STATE_SUSPENDED);
+ chSysUnlock();
+
+ return chThdSelf()->p_u.rdymsg;
+}
+
+/**
+ * @brief Transmits data via the I2C bus as master.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ * @param[in] addr slave device address
+ * @param[in] txbuf pointer to the transmit buffer
+ * @param[in] txbytes number of bytes to be transmitted
+ * @param[out] rxbuf pointer to the receive buffer
+ * @param[in] rxbytes number of bytes to be received
+ * @param[in] timeout the number of ticks before the operation timeouts,
+ * the following special values are allowed:
+ * - @a TIME_INFINITE no timeout.
+ * .
+ * @return The operation status.
+ * @retval RDY_OK if the function succeeded.
+ * @retval RDY_RESET if one or more I2C errors occurred, the errors can
+ * be retrieved using @p i2cGetErrors().
+ * @retval RDY_TIMEOUT if a timeout occurred before operation end. <b>After a
+ * timeout the driver must be stopped and restarted
+ * because the bus is in an uncertain state</b>.
+ *
+ * @notapi
+ */
+msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
+ const uint8_t *txbuf, size_t txbytes,
+ uint8_t *rxbuf, size_t rxbytes,
+ systime_t timeout) {
+ i2cp->addr = addr;
+ i2cp->txbuf = txbuf;
+ i2cp->txbytes = txbytes;
+ i2cp->txidx = 0;
+ i2cp->rxbuf = rxbuf;
+ i2cp->rxbytes = rxbytes;
+ i2cp->rxidx = 0;
+
+ TWCR = ((1 << TWSTA) | (1 << TWINT) | (1 << TWEN) | (1 << TWIE));
+
+ chSysLock();
+ i2cp->thread = chThdSelf();
+ chSchGoSleepS(THD_STATE_SUSPENDED);
+ chSysUnlock();
+
+ return chThdSelf()->p_u.rdymsg;
+}
+
+#endif /* HAL_USE_I2C */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/i2c_lld.h b/os/hal/platforms/AVR/i2c_lld.h new file mode 100644 index 000000000..9e49cefbe --- /dev/null +++ b/os/hal/platforms/AVR/i2c_lld.h @@ -0,0 +1,224 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/i2c_lld.h
+ * @brief AVR I2C subsystem low level driver header.
+ *
+ * @addtogroup I2C
+ * @{
+ */
+
+#ifndef _I2C_LLD_H_
+#define _I2C_LLD_H_
+
+#if HAL_USE_I2C || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/** @brief START transmitted.*/
+#define TWI_START 0x08
+/** @brief Repeated START transmitted.*/
+#define TWI_REPEAT_START 0x10
+/** @brief Arbitration Lost.*/
+#define TWI_ARBITRATION_LOST 0x38
+/** @brief Bus errors.*/
+#define TWI_BUS_ERROR 0x00
+
+/** @brief SLA+W transmitted with ACK response.*/
+#define TWI_MASTER_TX_ADDR_ACK 0x18
+/** @brief SLA+W transmitted with NACK response.*/
+#define TWI_MASTER_TX_ADDR_NACK 0x20
+/** @brief DATA transmitted with ACK response.*/
+#define TWI_MASTER_TX_DATA_ACK 0x28
+/** @brief DATA transmitted with NACK response.*/
+#define TWI_MASTER_TX_DATA_NACK 0x30
+
+/** @brief SLA+R transmitted with ACK response.*/
+#define TWI_MASTER_RX_ADDR_ACK 0x40
+/** @brief SLA+R transmitted with NACK response.*/
+#define TWI_MASTER_RX_ADDR_NACK 0x48
+/** @brief DATA received with ACK response.*/
+#define TWI_MASTER_RX_DATA_ACK 0x50
+/** @brief DATA received with NACK response.*/
+#define TWI_MASTER_RX_DATA_NACK 0x58
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @name Configuration options
+ * @{
+ */
+/**
+ * @brief I2C driver enable switch.
+ * @details If set to @p TRUE the support for I2C is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(USE_AVR_I2C) || defined(__DOXYGEN__)
+#define USE_AVR_I2C FALSE
+#endif
+/** @} */
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Type representing I2C address.
+ */
+typedef uint8_t i2caddr_t;
+
+/**
+ * @brief I2C Driver condition flags type.
+ */
+typedef uint8_t i2cflags_t;
+
+/**
+ * @brief Driver configuration structure.
+ * @note Implementations may extend this structure to contain more,
+ * architecture dependent, fields.
+ */
+typedef struct {
+
+ /**
+ * @brief Specifies the I2C clock frequency.
+ */
+ uint32_t clock_speed;
+
+} I2CConfig;
+
+/**
+ * @brief Structure representing an I2C driver.
+ */
+struct I2CDriver {
+ /**
+ * @brief Driver state.
+ */
+ i2cstate_t state;
+ /**
+ * @brief Current configuration data.
+ */
+ const I2CConfig *config;
+ /**
+ * @brief Error flags.
+ */
+ i2cflags_t errors;
+#if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
+#if CH_USE_MUTEXES || defined(__DOXYGEN__)
+ /**
+ * @brief Mutex protecting the bus.
+ */
+ Mutex mutex;
+#elif CH_USE_SEMAPHORES
+ Semaphore semaphore;
+#endif
+#endif /* I2C_USE_MUTUAL_EXCLUSION */
+#if defined(I2C_DRIVER_EXT_FIELDS)
+ I2C_DRIVER_EXT_FIELDS
+#endif
+ /* End of the mandatory fields.*/
+ /**
+ * @brief Thread waiting for I/O completion.
+ */
+ Thread *thread;
+ /**
+ * @brief Address of slave device.
+ */
+ i2caddr_t addr;
+ /**
+ * @brief Pointer to the buffer with data to send.
+ */
+ const uint8_t *txbuf;
+ /**
+ * @brief Number of bytes of data to send.
+ */
+ size_t txbytes;
+ /**
+ * @brief Current index in buffer when sending data.
+ */
+ size_t txidx;
+ /**
+ * @brief Pointer to the buffer to put received data.
+ */
+ uint8_t *rxbuf;
+ /**
+ * @brief Number of bytes of data to receive.
+ */
+ size_t rxbytes;
+ /**
+ * @brief Current index in buffer when receiving data.
+ */
+ size_t rxidx;
+};
+
+/**
+ * @brief Type of a structure representing an I2C driver.
+ */
+typedef struct I2CDriver I2CDriver;
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @brief Get errors from I2C driver.
+ *
+ * @param[in] i2cp pointer to the @p I2CDriver object
+ *
+ * @notapi
+ */
+#define i2c_lld_get_errors(i2cp) ((i2cp)->errors)
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if !defined(__DOXYGEN__)
+#if USE_AVR_I2C
+extern I2CDriver I2CD;
+#endif
+#endif /* !defined(__DOXYGEN__) */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void i2c_lld_init(void);
+ void i2c_lld_start(I2CDriver *i2cp);
+ void i2c_lld_stop(I2CDriver *i2cp);
+ msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
+ const uint8_t *txbuf, size_t txbytes,
+ uint8_t *rxbuf, size_t rxbytes,
+ systime_t timeout);
+ msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
+ uint8_t *rxbuf, size_t rxbytes,
+ systime_t timeout);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_I2C */
+
+#endif /* _I2C_LLD_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/pal_lld.c b/os/hal/platforms/AVR/pal_lld.c new file mode 100644 index 000000000..def4bd430 --- /dev/null +++ b/os/hal/platforms/AVR/pal_lld.c @@ -0,0 +1,157 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/pal_lld.c
+ * @brief AVR GPIO low level driver code.
+ *
+ * @addtogroup PAL
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#if HAL_USE_PAL || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief AVR GPIO ports configuration.
+ * @details GPIO registers initialization.
+ *
+ * @param[in] config the AVR ports configuration
+ *
+ * @notapi
+ */
+void _pal_lld_init(const PALConfig *config) {
+
+#if defined(PORTA) || defined(__DOXYGEN__)
+ PORTA = config->porta.out;
+ DDRA = config->porta.dir;
+#endif
+
+#if defined(PORTB) || defined(__DOXYGEN__)
+ PORTB = config->portb.out;
+ DDRB = config->portb.dir;
+#endif
+
+#if defined(PORTC) || defined(__DOXYGEN__)
+ PORTC = config->portc.out;
+ DDRC = config->portc.dir;
+#endif
+
+#if defined(PORTD) || defined(__DOXYGEN__)
+ PORTD = config->portd.out;
+ DDRD = config->portd.dir;
+#endif
+
+#if defined(PORTE) || defined(__DOXYGEN__)
+ PORTE = config->porte.out;
+ DDRE = config->porte.dir;
+#endif
+
+#if defined(PORTF) || defined(__DOXYGEN__)
+ PORTF = config->portf.out;
+ DDRF = config->portf.dir;
+#endif
+
+#if defined(PORTG) || defined(__DOXYGEN__)
+ PORTG = config->portg.out;
+ DDRG = config->portg.dir;
+#endif
+
+#if defined(PORTH) || defined(__DOXYGEN__)
+ PORTH = config->porth.out;
+ DDRH = config->porth.dir;
+#endif
+
+#if defined(PORTJ) || defined(__DOXYGEN__)
+ PORTJ = config->portj.out;
+ DDRJ = config->portj.dir;
+#endif
+
+#if defined(PORTK) || defined(__DOXYGEN__)
+ PORTK = config->portk.out;
+ DDRK = config->portk.dir;
+#endif
+
+#if defined(PORTL) || defined(__DOXYGEN__)
+ PORTL = config->portl.out;
+ DDRL = config->portl.dir;
+#endif
+}
+
+/**
+ * @brief Pads mode setup.
+ * @details This function programs a pads group belonging to the same port
+ * with the specified mode.
+ *
+ * @param[in] port the port identifier
+ * @param[in] mask the group mask
+ * @param[in] mode the mode
+ *
+ * @note This function is not meant to be invoked directly by the application
+ * code.
+ * @note @p PAL_MODE_UNCONNECTED is implemented as output as recommended by
+ * the AVR Family User's Guide. Unconnected pads are set to input
+ * with pull-up by default.
+ *
+ * @notapi
+ */
+void _pal_lld_setgroupmode(ioportid_t port,
+ ioportmask_t mask,
+ iomode_t mode) {
+
+ switch (mode) {
+ case PAL_MODE_RESET:
+ case PAL_MODE_INPUT:
+ case PAL_MODE_INPUT_ANALOG:
+ port->dir &= ~mask;
+ port->out &= ~mask;
+ break;
+ case PAL_MODE_UNCONNECTED:
+ case PAL_MODE_INPUT_PULLUP:
+ port->dir &= ~mask;
+ port->out |= mask;
+ break;
+ case PAL_MODE_OUTPUT_PUSHPULL:
+ port->dir |= mask;
+ break;
+ }
+}
+
+#endif /* HAL_USE_PAL */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/pal_lld.h b/os/hal/platforms/AVR/pal_lld.h new file mode 100644 index 000000000..f48b51ec3 --- /dev/null +++ b/os/hal/platforms/AVR/pal_lld.h @@ -0,0 +1,329 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/pal_lld.h
+ * @brief AVR GPIO low level driver header.
+ *
+ * @addtogroup PAL
+ * @{
+ */
+
+#ifndef _PAL_LLD_H_
+#define _PAL_LLD_H_
+
+#if HAL_USE_PAL || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Unsupported modes and specific modes */
+/*===========================================================================*/
+
+#undef PAL_MODE_INPUT_PULLDOWN
+#undef PAL_MODE_OUTPUT_OPENDRAIN
+
+/*===========================================================================*/
+/* I/O Ports Types and constants. */
+/*===========================================================================*/
+
+/**
+ * @brief Width, in bits, of an I/O port.
+ */
+#define PAL_IOPORTS_WIDTH 8
+
+/**
+ * @brief Whole port mask.
+ * @details This macro specifies all the valid bits into a port.
+ */
+#define PAL_WHOLE_PORT ((ioportmask_t)0xFF)
+
+/**
+ * @brief AVR setup registers.
+ */
+typedef struct {
+ uint8_t out;
+ uint8_t dir;
+} avr_gpio_setup_t;
+
+/**
+ * @brief AVR registers block.
+ * @note On some devices registers do not follow this layout on some
+ * ports, the ports with abnormal layout cannot be used through
+ * PAL driver. Example: PORT F on Mega128.
+ */
+typedef struct {
+ volatile uint8_t in;
+ volatile uint8_t dir;
+ volatile uint8_t out;
+} avr_gpio_registers_t;
+
+/**
+ * @brief Generic I/O ports static initializer.
+ * @details An instance of this structure must be passed to @p palInit() at
+ * system startup time in order to initialized the digital I/O
+ * subsystem. This represents only the initial setup, specific pads
+ * or whole ports can be reprogrammed at later time.
+ */
+typedef struct {
+#if defined(PORTA) || defined(__DOXYGEN__)
+ avr_gpio_setup_t porta;
+#endif
+#if defined(PORTB) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portb;
+#endif
+#if defined(PORTC) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portc;
+#endif
+#if defined(PORTD) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portd;
+#endif
+#if defined(PORTE) || defined(__DOXYGEN__)
+ avr_gpio_setup_t porte;
+#endif
+#if defined(PORTF) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portf;
+#endif
+#if defined(PORTG) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portg;
+#endif
+#if defined(PORTH) || defined(__DOXYGEN__)
+ avr_gpio_setup_t porth;
+#endif
+#if defined(PORTJ) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portj;
+#endif
+#if defined(PORTK) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portk;
+#endif
+#if defined(PORTL) || defined(__DOXYGEN__)
+ avr_gpio_setup_t portl;
+#endif
+} PALConfig;
+
+/**
+ * @brief Digital I/O port sized unsigned type.
+ */
+typedef uint8_t ioportmask_t;
+
+/**
+ * @brief Digital I/O modes.
+ */
+typedef uint8_t iomode_t;
+
+/**
+ * @brief Port Identifier.
+ * @details This type can be a scalar or some kind of pointer, do not make
+ * any assumption about it, use the provided macros when populating
+ * variables of this type.
+ */
+typedef avr_gpio_registers_t *ioportid_t;
+
+/*===========================================================================*/
+/* I/O Ports Identifiers. */
+/*===========================================================================*/
+
+#if defined(PORTA) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port A identifier.
+ */
+#define IOPORT1 ((volatile avr_gpio_registers_t *)&PINA)
+#endif
+
+#if defined(PORTB) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port B identifier.
+ */
+#define IOPORT2 ((volatile avr_gpio_registers_t *)&PINB)
+#endif
+
+#if defined(PORTC) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port C identifier.
+ */
+#define IOPORT3 ((volatile avr_gpio_registers_t *)&PINC)
+#endif
+
+#if defined(PORTD) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port D identifier.
+ */
+#define IOPORT4 ((volatile avr_gpio_registers_t *)&PIND)
+#endif
+
+#if defined(PORTE) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port E identifier.
+ */
+#define IOPORT5 ((volatile avr_gpio_registers_t *)&PINE)
+#endif
+
+#if defined(PORTF) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port F identifier.
+ */
+#define IOPORT6 ((volatile avr_gpio_registers_t *)&PINF)
+#endif
+
+#if defined(PORTG) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port G identifier.
+ */
+#define IOPORT7 ((volatile avr_gpio_registers_t *)&PING)
+#endif
+
+#if defined(PORTH) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port H identifier.
+ */
+#define IOPORT8 ((volatile avr_gpio_registers_t *)&PINH)
+#endif
+
+#if defined(PORTJ) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port J identifier.
+ */
+#define IOPORT9 ((volatile avr_gpio_registers_t *)&PINJ)
+#endif
+
+#if defined(PORTK) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port K identifier.
+ */
+#define IOPORT10 ((volatile avr_gpio_registers_t *)&PINK)
+#endif
+
+#if defined(PORTL) || defined(__DOXYGEN__)
+/**
+ * @brief GPIO port L identifier.
+ */
+#define IOPORT11 ((volatile avr_gpio_registers_t *)&PINL)
+#endif
+
+/*===========================================================================*/
+/* Implementation, some of the following macros could be implemented as */
+/* functions, if so please put them in pal_lld.c. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level PAL subsystem initialization.
+ *
+ * @param[in] config the architecture-dependent ports configuration
+ *
+ * @notapi
+ */
+#define pal_lld_init(config) _pal_lld_init(config)
+
+/**
+ * @brief Reads the physical I/O port states.
+ *
+ * @param[in] port port identifier
+ * @return The port bits.
+ *
+ * @notapi
+ */
+#define pal_lld_readport(port) ((port)->in)
+
+/**
+ * @brief Reads the output latch.
+ * @details The purpose of this function is to read back the latched output
+ * value.
+ *
+ * @param[in] port port identifier
+ * @return The latched logical states.
+ *
+ * @notapi
+ */
+#define pal_lld_readlatch(port) ((port)->out)
+
+/**
+ * @brief Writes a bits mask on a I/O port.
+ *
+ * @param[in] port port identifier
+ * @param[in] bits bits to be written on the specified port
+ *
+ * @notapi
+ */
+#define pal_lld_writeport(port, bits) ((port)->out = bits)
+
+/**
+ * @brief Pads group mode setup.
+ * @details This function programs a pads group belonging to the same port
+ * with the specified mode.
+ * @note Programming an unknown or unsupported mode is silently ignored.
+ *
+ * @param[in] port port identifier
+ * @param[in] mask group mask
+ * @param[in] offset group bit offset within the port
+ * @param[in] mode group mode
+ *
+ * @notapi
+ */
+#define pal_lld_setgroupmode(port, mask, offset, mode) \
+ _pal_lld_setgroupmode(port, mask << offset, mode)
+
+/**
+ * @brief Sets a pad logical state to @p PAL_HIGH.
+ *
+ * @param[in] port port identifier
+ * @param[in] pad pad number within the port
+ *
+ * @notapi
+ */
+#define pal_lld_setpad(port, pad) \
+__asm__ __volatile__ \
+( \
+ "sbi %0,%1\n\t" \
+ : \
+ : "I" (_SFR_IO_ADDR(port->out)), \
+ "I" (pad) \
+ \
+)
+
+/**
+ * @brief Clears a pad logical state to @p PAL_LOW.
+ *
+ * @param[in] port port identifier
+ * @param[in] pad pad number within the port
+ *
+ * @notapi
+ */
+#define pal_lld_clearpad(port, pad) \
+__asm__ __volatile__ \
+( \
+ "cbi %0,%1\n\t" \
+ : \
+ : "I" (_SFR_IO_ADDR(port->out)), \
+ "I" (pad) \
+ \
+)
+
+extern ROMCONST PALConfig pal_default_config;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void _pal_lld_init(const PALConfig *config);
+ void _pal_lld_setgroupmode(ioportid_t port,
+ ioportmask_t mask,
+ iomode_t mode);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_PAL */
+
+#endif /* _PAL_LLD_H_ */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/platform.dox b/os/hal/platforms/AVR/platform.dox new file mode 100644 index 000000000..bd6af1b1d --- /dev/null +++ b/os/hal/platforms/AVR/platform.dox @@ -0,0 +1,110 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @defgroup AVR_DRIVERS AVR Drivers
+ * @details This section describes all the supported drivers on the AVR
+ * platform and the implementation details of the single drivers.
+ *
+ * @ingroup platforms
+ */
+
+/**
+ * @defgroup AVR_HAL AVR Initialization Support
+ * @details On the AVR platform the HAL driver is a stub and does not perform
+ * any platform-specific initialization, it still performs the
+ * initialization of the other drivers.
+ *
+ * @ingroup AVR_DRIVERS
+ */
+
+/**
+ * @defgroup AVR_PAL AVR PAL Support
+ * @details The AVR PAL driver uses the PORT peripherals.
+ *
+ * @section avr_pal_1 Supported HW resources
+ * - PORTA.
+ * - PORTB.
+ * - PORTC.
+ * - PORTD.
+ * - PORTE.
+ * - PORTF.
+ * - PORTG.
+ * .
+ * @section avr_pal_2 AVR PAL driver implementation features
+ * The AVR PAL driver implementation fully supports the following hardware
+ * capabilities:
+ * - 8 bits wide ports.
+ * - Atomic set/reset functions.
+ * - Output latched regardless of the pad setting.
+ * - Direct read of input pads regardless of the pad setting.
+ * .
+ * @section avr_pal_3 Supported PAL setup modes
+ * The AVR PAL driver supports the following I/O modes:
+ * - @p PAL_MODE_RESET.
+ * - @p PAL_MODE_UNCONNECTED.
+ * - @p PAL_MODE_INPUT.
+ * - @p PAL_MODE_INPUT_PULLUP.
+ * - @p PAL_MODE_INPUT_ANALOG.
+ * - @p PAL_MODE_OUTPUT_PUSHPULL.
+ * .
+ * Any attempt to setup an invalid mode is ignored.
+ *
+ * @section avr_pal_4 Suboptimal behavior
+ * The AVR PORT is less than optimal in several areas, the limitations
+ * should be taken in account while using the PAL driver:
+ * - Pad/port toggling operations are not atomic.
+ * - Pad/group mode setup is not atomic.
+ * - Group set+reset function is not atomic.
+ * - Writing on pads/groups/ports programmed as input with pull-up
+ * resistor changes the resistor setting because the output latch is
+ * used for resistor selection.
+ * - The PORT registers layout on some devices is not regular (it does
+ * not have contiguous PIN, DDR, PORT registers in this order), such
+ * ports cannot be accessed using the PAL driver. For example, PORT F
+ * on ATmega128. Verify the user manual of your device.
+ * .
+ * @ingroup AVR_DRIVERS
+ */
+
+/**
+ * @defgroup AVR_SERIAL AVR Serial Support
+ * @details The AVR Serial driver uses the USART peripherals in a
+ * buffered, interrupt driven, implementation.
+ *
+ * @section avr_serial_1 Supported HW resources
+ * The serial driver can support any of the following hardware resources:
+ * - USART0.
+ * - USART1.
+ * .
+ * @section avr_serial_2 AVR Serial driver implementation features
+ * - Each USART can be independently enabled and programmed.
+ * - Fully interrupt driven.
+ * .
+ * @ingroup AVR_DRIVERS
+ */
+
+/**
+ * @defgroup AVR_I2C AVR I2C Support
+ * @details The AVR I2C driver uses the TWI peripheral in an interrupt
+ * driven, implementation.
+ *
+ * @section avr_i2c Supported HW resources
+ * The i2c driver can support the following hardware resource:
+ * - I2C.
+ * .
+ * @ingroup AVR_DRIVERS
+ */
diff --git a/os/hal/platforms/AVR/platform.mk b/os/hal/platforms/AVR/platform.mk new file mode 100644 index 000000000..86a2c2eba --- /dev/null +++ b/os/hal/platforms/AVR/platform.mk @@ -0,0 +1,8 @@ +# List of all the AVR platform files.
+PLATFORMSRC = ${CHIBIOS}/os/hal/platforms/AVR/hal_lld.c \
+ ${CHIBIOS}/os/hal/platforms/AVR/pal_lld.c \
+ ${CHIBIOS}/os/hal/platforms/AVR/serial_lld.c \
+ ${CHIBIOS}/os/hal/platforms/AVR/i2c_lld.c
+
+# Required include directories
+PLATFORMINC = ${CHIBIOS}/os/hal/platforms/AVR
diff --git a/os/hal/platforms/AVR/serial_lld.c b/os/hal/platforms/AVR/serial_lld.c new file mode 100644 index 000000000..0b6127bb6 --- /dev/null +++ b/os/hal/platforms/AVR/serial_lld.c @@ -0,0 +1,360 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/serial_lld.c
+ * @brief AVR low level serial driver code.
+ *
+ * @addtogroup SERIAL
+ * @{
+ */
+
+#include "ch.h"
+#include "hal.h"
+
+#if HAL_USE_SERIAL || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver exported variables. */
+/*===========================================================================*/
+
+/**
+ * @brief USART0 serial driver identifier.
+ * @note The name does not follow the convention used in the other ports
+ * (COMn) because a name conflict with the AVR headers.
+ */
+#if USE_AVR_USART0 || defined(__DOXYGEN__)
+SerialDriver SD1;
+#endif
+
+/**
+ * @brief USART1 serial driver identifier.
+ * @note The name does not follow the convention used in the other ports
+ * (COMn) because a name conflict with the AVR headers.
+ */
+#if USE_AVR_USART1 || defined(__DOXYGEN__)
+SerialDriver SD2;
+#endif
+
+/*===========================================================================*/
+/* Driver local variables and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Driver default configuration.
+ */
+static const SerialConfig default_config = {
+ UBRR(SERIAL_DEFAULT_BITRATE),
+ USART_CHAR_SIZE_8
+};
+
+/*===========================================================================*/
+/* Driver local functions. */
+/*===========================================================================*/
+
+static void set_error(uint8_t sra, SerialDriver *sdp) {
+ flagsmask_t sts = 0;
+ uint8_t dor = 0;
+ uint8_t upe = 0;
+ uint8_t fe = 0;
+
+#if USE_AVR_USART0
+ if (&SD1 == sdp) {
+ dor = (1 << DOR0);
+ upe = (1 << UPE0);
+ fe = (1 << FE0);
+ }
+#endif
+
+#if USE_AVR_USART1
+ if (&SD2 == sdp) {
+ dor = (1 << DOR1);
+ upe = (1 << UPE1);
+ fe = (1 << FE1);
+ }
+#endif
+
+ if (sra & dor)
+ sts |= SD_OVERRUN_ERROR;
+ if (sra & upe)
+ sts |= SD_PARITY_ERROR;
+ if (sra & fe)
+ sts |= SD_FRAMING_ERROR;
+ chSysLockFromIsr();
+ chnAddFlagsI(sdp, sts);
+ chSysUnlockFromIsr();
+}
+
+#if USE_AVR_USART0 || defined(__DOXYGEN__)
+static void notify1(GenericQueue *qp) {
+
+ (void)qp;
+ UCSR0B |= (1 << UDRIE0);
+}
+
+/**
+ * @brief USART0 initialization.
+ *
+ * @param[in] config the architecture-dependent serial driver configuration
+ */
+static void usart0_init(const SerialConfig *config) {
+
+ UBRR0L = config->sc_brr;
+ UBRR0H = config->sc_brr >> 8;
+ UCSR0A = 0;
+ UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
+ switch (config->sc_bits_per_char) {
+ case USART_CHAR_SIZE_5:
+ UCSR0C = 0;
+ break;
+ case USART_CHAR_SIZE_6:
+ UCSR0C = (1 << UCSZ00);
+ break;
+ case USART_CHAR_SIZE_7:
+ UCSR0C = (1 << UCSZ01);
+ break;
+ case USART_CHAR_SIZE_9:
+ UCSR0B |= (1 << UCSZ02);
+ UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);
+ break;
+ case USART_CHAR_SIZE_8:
+ default:
+ UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);
+ }
+}
+
+/**
+ * @brief USART0 de-initialization.
+ */
+static void usart0_deinit(void) {
+
+ UCSR0A = 0;
+ UCSR0B = 0;
+ UCSR0C = 0;
+}
+#endif
+
+#if USE_AVR_USART1 || defined(__DOXYGEN__)
+static void notify2(GenericQueue *qp) {
+
+ (void)qp;
+ UCSR1B |= (1 << UDRIE1);
+}
+
+/**
+ * @brief USART1 initialization.
+ *
+ * @param[in] config the architecture-dependent serial driver configuration
+ */
+static void usart1_init(const SerialConfig *config) {
+
+ UBRR1L = config->sc_brr;
+ UBRR1H = config->sc_brr >> 8;
+ UCSR1A = 0;
+ UCSR1B = (1 << RXEN1) | (1 << TXEN1) | (1 << RXCIE1);
+ switch (config->sc_bits_per_char) {
+ case USART_CHAR_SIZE_5:
+ UCSR1C = 0;
+ break;
+ case USART_CHAR_SIZE_6:
+ UCSR1C = (1 << UCSZ10);
+ break;
+ case USART_CHAR_SIZE_7:
+ UCSR1C = (1 << UCSZ11);
+ break;
+ case USART_CHAR_SIZE_9:
+ UCSR1B |= (1 << UCSZ12);
+ UCSR1C = (1 << UCSZ10) | (1 << UCSZ11);
+ break;
+ case USART_CHAR_SIZE_8:
+ default:
+ UCSR1C = (1 << UCSZ10) | (1 << UCSZ11);
+ }
+}
+
+/**
+ * @brief USART1 de-initialization.
+ */
+static void usart1_deinit(void) {
+
+ UCSR1A = 0;
+ UCSR1B = 0;
+ UCSR1C = 0;
+}
+#endif
+
+/*===========================================================================*/
+/* Driver interrupt handlers. */
+/*===========================================================================*/
+
+#if USE_AVR_USART0 || defined(__DOXYGEN__)
+/**
+ * @brief USART0 RX interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USART0_RX_vect) {
+ uint8_t sra;
+
+ CH_IRQ_PROLOGUE();
+
+ sra = UCSR0A;
+ if (sra & ((1 << DOR0) | (1 << UPE0) | (1 << FE0)))
+ set_error(sra, &SD1);
+ chSysLockFromIsr();
+ sdIncomingDataI(&SD1, UDR0);
+ chSysUnlockFromIsr();
+
+ CH_IRQ_EPILOGUE();
+}
+
+/**
+ * @brief USART0 TX interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USART0_UDRE_vect) {
+ msg_t b;
+
+ CH_IRQ_PROLOGUE();
+
+ chSysLockFromIsr();
+ b = sdRequestDataI(&SD1);
+ chSysUnlockFromIsr();
+ if (b < Q_OK)
+ UCSR0B &= ~(1 << UDRIE0);
+ else
+ UDR0 = b;
+
+ CH_IRQ_EPILOGUE();
+}
+#endif /* USE_AVR_USART0 */
+
+#if USE_AVR_USART1 || defined(__DOXYGEN__)
+/**
+ * @brief USART1 RX interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USART1_RX_vect) {
+ uint8_t sra;
+
+ CH_IRQ_PROLOGUE();
+
+ sra = UCSR1A;
+ if (sra & ((1 << DOR1) | (1 << UPE1) | (1 << FE1)))
+ set_error(sra, &SD2);
+ chSysLockFromIsr();
+ sdIncomingDataI(&SD2, UDR1);
+ chSysUnlockFromIsr();
+
+ CH_IRQ_EPILOGUE();
+}
+
+/**
+ * @brief USART1 TX interrupt handler.
+ *
+ * @isr
+ */
+CH_IRQ_HANDLER(USART1_UDRE_vect) {
+ msg_t b;
+
+ CH_IRQ_PROLOGUE();
+
+ chSysLockFromIsr();
+ b = sdRequestDataI(&SD2);
+ chSysUnlockFromIsr();
+ if (b < Q_OK)
+ UCSR1B &= ~(1 << UDRIE1);
+ else
+ UDR1 = b;
+
+ CH_IRQ_EPILOGUE();
+}
+#endif /* USE_AVR_USART1 */
+
+/*===========================================================================*/
+/* Driver exported functions. */
+/*===========================================================================*/
+
+/**
+ * @brief Low level serial driver initialization.
+ *
+ * @notapi
+ */
+void sd_lld_init(void) {
+
+#if USE_AVR_USART0
+ sdObjectInit(&SD1, NULL, notify1);
+#endif
+#if USE_AVR_USART1
+ sdObjectInit(&SD2, NULL, notify2);
+#endif
+}
+
+/**
+ * @brief Low level serial driver configuration and (re)start.
+ *
+ * @param[in] sdp pointer to a @p SerialDriver object
+ * @param[in] config the architecture-dependent serial driver configuration.
+ * If this parameter is set to @p NULL then a default
+ * configuration is used.
+ *
+ * @notapi
+ */
+void sd_lld_start(SerialDriver *sdp, const SerialConfig *config) {
+
+ if (config == NULL)
+ config = &default_config;
+
+#if USE_AVR_USART0
+ if (&SD1 == sdp) {
+ usart0_init(config);
+ return;
+ }
+#endif
+#if USE_AVR_USART1
+ if (&SD2 == sdp) {
+ usart1_init(config);
+ return;
+ }
+#endif
+}
+
+/**
+ * @brief Low level serial driver stop.
+ * @details De-initializes the USART, stops the associated clock, resets the
+ * interrupt vector.
+ *
+ * @param[in] sdp pointer to a @p SerialDriver object
+ *
+ * @notapi
+ */
+void sd_lld_stop(SerialDriver *sdp) {
+
+#if USE_AVR_USART0
+ if (&SD1 == sdp)
+ usart0_deinit();
+#endif
+#if USE_AVR_USART1
+ if (&SD2 == sdp)
+ usart1_deinit();
+#endif
+}
+
+#endif /* HAL_USE_SERIAL */
+
+/** @} */
diff --git a/os/hal/platforms/AVR/serial_lld.h b/os/hal/platforms/AVR/serial_lld.h new file mode 100644 index 000000000..5a19befc7 --- /dev/null +++ b/os/hal/platforms/AVR/serial_lld.h @@ -0,0 +1,158 @@ +/*
+ ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
+
+ 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.
+*/
+
+/**
+ * @file AVR/serial_lld.h
+ * @brief AVR low level serial driver header.
+ *
+ * @addtogroup SERIAL
+ * @{
+ */
+
+#ifndef _SERIAL_LLD_H_
+#define _SERIAL_LLD_H_
+
+#if HAL_USE_SERIAL || defined(__DOXYGEN__)
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/**
+ * @brief USART0 driver enable switch.
+ * @details If set to @p TRUE the support for USART0 is included.
+ * @note The default is @p FALSE.
+ */
+#if !defined(USE_AVR_USART0) || defined(__DOXYGEN__)
+#define USE_AVR_USART0 TRUE
+#endif
+
+/**
+ * @brief USART1 driver enable switch.
+ * @details If set to @p TRUE the support for USART1 is included.
+ * @note The default is @p TRUE.
+ */
+#if !defined(USE_AVR_USART1) || defined(__DOXYGEN__)
+#define USE_AVR_USART1 TRUE
+#endif
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief AVR Serial Driver configuration structure.
+ * @details An instance of this structure must be passed to @p sdStart()
+ * in order to configure and start a serial driver operations.
+ */
+typedef struct {
+ /**
+ * @brief Initialization value for the BRR register.
+ */
+ uint16_t sc_brr;
+ /**
+ * @brief Number of bits per character (USART_CHAR_SIZE_5 to USART_CHAR_SIZE_9).
+ */
+ uint8_t sc_bits_per_char;
+} SerialConfig;
+
+/**
+ * @brief @p SerialDriver specific data.
+ */
+#define _serial_driver_data \
+ _base_asynchronous_channel_data \
+ /* Driver state.*/ \
+ sdstate_t state; \
+ /* Input queue.*/ \
+ InputQueue iqueue; \
+ /* Output queue.*/ \
+ OutputQueue oqueue; \
+ /* Input circular buffer.*/ \
+ uint8_t ib[SERIAL_BUFFERS_SIZE]; \
+ /* Output circular buffer.*/ \
+ uint8_t ob[SERIAL_BUFFERS_SIZE]; \
+ /* End of the mandatory fields.*/
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @brief Macro for baud rate computation.
+ * @note Make sure the final baud rate is within tolerance.
+ */
+#define UBRR(b) (((F_CPU / b) >> 4) - 1)
+
+/**
+ * @brief Macro for baud rate computation when U2Xn == 1.
+ * @note Make sure the final baud rate is within tolerance.
+ */
+#define UBRR2x(b) (((F_CPU / b) >> 3) - 1)
+
+/**
+* @brief Macro for baud rate computation.
+* @note Make sure the final baud rate is within tolerance.
+* @note This version uses floating point math for greater accuracy.
+*/
+#define UBRR_F(b) ((((double) F_CPU / (double) b) / 16.0) - 0.5)
+
+/**
+* @brief Macro for baud rate computation when U2Xn == 1.
+* @note Make sure the final baud rate is within tolerance.
+* @note This version uses floating point math for greater accuracy.
+*/
+#define UBRR2x_F(b) ((((double) F_CPU / (double) b) / 8.0) - 0.5)
+
+#define USART_CHAR_SIZE_5 0
+#define USART_CHAR_SIZE_6 1
+#define USART_CHAR_SIZE_7 2
+#define USART_CHAR_SIZE_8 3
+#define USART_CHAR_SIZE_9 4
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#if USE_AVR_USART0 && !defined(__DOXYGEN__)
+extern SerialDriver SD1;
+#endif
+#if USE_AVR_USART1 && !defined(__DOXYGEN__)
+extern SerialDriver SD2;
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ void sd_lld_init(void);
+ void sd_lld_start(SerialDriver *sdp, const SerialConfig *config);
+ void sd_lld_stop(SerialDriver *sdp);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_USE_SERIAL */
+
+#endif /* _SERIAL_LLD_H_ */
+
+/** @} */
|