summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src
diff options
context:
space:
mode:
Diffstat (limited to 'digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src')
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoFT245RL.c290
-rw-r--r--digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoVirtualUsart.c250
2 files changed, 540 insertions, 0 deletions
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoFT245RL.c b/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoFT245RL.c
new file mode 100644
index 00000000..8ba0d9c5
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoFT245RL.c
@@ -0,0 +1,290 @@
+/**************************************************************************//**
+\file usbFifoFT245RL.c
+
+\brief Implementation of FT245RL driver.
+
+\author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+\internal
+ History:
+ 12.07.11 A. Khromykh - Created
+*******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <usbFifoVirtualUsart.h>
+#include <usart.h>
+#include <atomic.h>
+#include <irq.h>
+
+/******************************************************************************
+ Define(s) section
+******************************************************************************/
+#if defined(ATMEGA128RFA1)
+// data port
+#define USB_DATA_PORT PORTB
+#define USB_DATA_DDR DDRB
+#define USB_DATA_PIN PINB
+
+#elif defined(ATMEGA1281) || defined(ATMEGA2561)
+// data port
+#define USB_DATA_PORT PORTA
+#define USB_DATA_DDR DDRA
+#define USB_DATA_PIN PINA
+
+#endif
+
+#define HANDLERS_GET(A, I) memcpy_P(A, &usbfifoHandlers[I], sizeof(UsbfifoTask_t))
+
+/******************************************************************************
+ Types section
+******************************************************************************/
+typedef volatile uint8_t UsbfifoTaskBitMask_t;
+typedef void (* UsbfifoTask_t)(void);
+
+// USB FIFO task IDs.
+typedef enum
+{
+ USB_FIFO_TASK_RX,
+ USB_FIFO_TASK_TX,
+ USB_FIFO_TASKS_NUMBER
+} UsbfifoTaskId_t;
+
+/******************************************************************************
+ Prototypes section
+******************************************************************************/
+static void usbfifoRxBufferFiller(void);
+static void usbfifoTxBufferCleaner(void);
+static void usbfifoPostTask(UsbfifoTaskId_t taskId);
+static void usbfifoTxComplete(void);
+static void usbfifoRxComplete(void);
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+// pointer to application uart descriptor
+extern HAL_UsartDescriptor_t *usbfifoPointDescrip;
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+static volatile UsbfifoTaskBitMask_t usbfifoTaskBitMask = 0; // USB FIFO tasks' bit mask.
+static const UsbfifoTask_t PROGMEM_DECLARE(usbfifoHandlers[USB_FIFO_TASKS_NUMBER]) =
+{
+ usbfifoRxComplete,
+ usbfifoTxComplete
+}; // List Of possible USB FIFO tasks.
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/**************************************************************************//**
+\brief Startup initialization.
+******************************************************************************/
+void usbfifoInit(void)
+{
+ // init decoder input pins
+ GPIO_PC6_make_out();
+ GPIO_PC7_make_out();
+ // set pins to zero to select usb FIFO
+ GPIO_PC6_clr();
+ GPIO_PC7_clr();
+
+ // setup default value
+ GPIO_RD_set();
+ GPIO_WR_set();
+ // init R\W pins
+ GPIO_RD_make_out();
+ GPIO_WR_make_out();
+
+ // register RXF interrupt
+ HAL_RegisterIrq(IRQ_7, IRQ_LOW_LEVEL, usbfifoRxBufferFiller);
+ // register TXE interrupt
+ HAL_RegisterIrq(IRQ_6, IRQ_LOW_LEVEL, usbfifoTxBufferCleaner);
+
+ USB_DATA_DDR = 0;
+}
+
+/**************************************************************************//**
+\brief Clear startup initialization parameters
+******************************************************************************/
+void usbfifoUnInit(void)
+{
+ GPIO_PC6_make_in();
+ GPIO_PC7_make_in();
+
+ GPIO_RD_make_in();
+ GPIO_WR_make_in();
+
+ HAL_DisableIrq(IRQ_7);
+ HAL_UnregisterIrq(IRQ_7);
+
+ HAL_DisableIrq(IRQ_6);
+ HAL_UnregisterIrq(IRQ_6);
+}
+
+/**************************************************************************//**
+\brief Puts the byte received to the cyclic buffer.
+******************************************************************************/
+static void usbfifoRxBufferFiller(void)
+{
+ uint16_t old;
+ uint16_t poW;
+ uint16_t poR;
+ uint8_t *buffer;
+ HalUsartService_t *control;
+
+ if (!usbfifoPointDescrip)
+ { // unregistered intrrupt is occurred
+ HAL_DisableIrq(IRQ_7);
+ return;
+ }
+
+ control = &usbfifoPointDescrip->service;
+ poW = control->rxPointOfWrite;
+ poR = control->rxPointOfRead;
+ buffer = usbfifoPointDescrip->rxBuffer;
+
+ if (!buffer)
+ {
+ HAL_DisableIrq(IRQ_7);
+ return;
+ }
+
+ old = poW;
+
+ if (++poW == usbfifoPointDescrip->rxBufferLength)
+ poW = 0;
+
+ if (poW == poR)
+ { // Buffer full.
+ HAL_DisableIrq(IRQ_7);
+ return;
+ } // Buffer full.
+
+ control->rxPointOfWrite = poW;
+ GPIO_RD_clr();
+ NOP;
+ buffer[old] = USB_DATA_PIN;
+ GPIO_RD_set();
+ control->rxBytesInBuffer++;
+ usbfifoPostTask(USB_FIFO_TASK_RX);
+}
+
+/**************************************************************************//**
+\brief Reads byte from tx buffer and sends it to fifo.
+******************************************************************************/
+static void usbfifoTxBufferCleaner(void)
+{
+ HalUsartService_t *control;
+ uint16_t poW;
+ uint16_t poR;
+
+ if (!usbfifoPointDescrip)
+ { // unregistered intrrupt is occurred
+ HAL_DisableIrq(IRQ_6);
+ return;
+ }
+
+ control = &usbfifoPointDescrip->service;
+ poW = control->txPointOfWrite;
+ poR = control->txPointOfRead;
+
+ if (poW != poR)
+ {
+ // set port as output
+ USB_DATA_DDR = 0xFF;
+ NOP;
+ USB_DATA_PORT = usbfifoPointDescrip->txBuffer[poR++];
+ GPIO_WR_clr();
+ GPIO_WR_set();
+ USB_DATA_DDR = 0;
+
+ if (poR == usbfifoPointDescrip->txBufferLength)
+ poR = 0;
+
+ control->txPointOfRead = poR;
+ }
+ else
+ { // tx buffer is empty
+ HAL_DisableIrq(IRQ_6);
+ usbfifoPostTask(USB_FIFO_TASK_TX);
+ }
+}
+
+/**************************************************************************//**
+\brief Transmitting complete handler
+******************************************************************************/
+static void usbfifoTxComplete(void)
+{
+ if (NULL == usbfifoPointDescrip)
+ return;
+
+ if (0 == usbfifoPointDescrip->txBufferLength)
+ usbfifoPointDescrip->txBuffer = NULL; // nulling pointer for callback mode
+
+ if (usbfifoPointDescrip->txCallback)
+ usbfifoPointDescrip->txCallback();
+}
+
+/**************************************************************************//**
+\brief Byte is received handler
+******************************************************************************/
+static void usbfifoRxComplete(void)
+{
+ HalUsartService_t *control;
+ uint16_t number;
+
+ if (NULL == usbfifoPointDescrip)
+ return;
+
+ control = &usbfifoPointDescrip->service;
+ ATOMIC_SECTION_ENTER
+ number = control->rxBytesInBuffer;
+ ATOMIC_SECTION_LEAVE
+
+ if (number)
+ if (NULL != usbfifoPointDescrip->rxCallback)
+ usbfifoPointDescrip->rxCallback(number);
+}
+
+/**************************************************************************//**
+\brief USB FIFO driver task handler.
+******************************************************************************/
+void usbfifoHandler(void)
+{
+ UsbfifoTask_t handler;
+ uint8_t mask = 1;
+ UsbfifoTaskId_t index = 0;
+
+ for ( ; index < USB_FIFO_TASKS_NUMBER; index++, mask <<= 1)
+ {
+ if (usbfifoTaskBitMask & mask)
+ {
+ ATOMIC_SECTION_ENTER
+ usbfifoTaskBitMask ^= mask;
+ ATOMIC_SECTION_LEAVE
+ HANDLERS_GET(&handler, index);
+ handler();
+ }
+ }
+}
+
+/**************************************************************************//**
+\brief Posts specific USART task.
+
+\param[in]
+ taskId - unique identifier of the task to be posted.
+******************************************************************************/
+static void usbfifoPostTask(UsbfifoTaskId_t taskId)
+{
+ usbfifoTaskBitMask |= (UsbfifoTaskBitMask_t)1 << taskId;
+ halPostTask4(HAL_EXT_HANDLER);
+}
+
+// eof usbFifoFT245RL.c
diff --git a/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoVirtualUsart.c b/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoVirtualUsart.c
new file mode 100644
index 00000000..1a909d38
--- /dev/null
+++ b/digital/zigbit/bitcloud/stack/Components/HAL/drivers/USBFIFO/src/usbFifoVirtualUsart.c
@@ -0,0 +1,250 @@
+/****************************************************************************//**
+ \file usbFifoVirtualUart.c
+
+ \brief Implementation of virtual uart API.
+
+ \author
+ Atmel Corporation: http://www.atmel.com \n
+ Support email: avr@atmel.com
+
+ Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
+ Licensed under Atmel's Limited License Agreement (BitCloudTM).
+
+ \internal
+ History:
+ 11/09/08 A. Khromykh - Created
+*******************************************************************************/
+/******************************************************************************
+ Includes section
+******************************************************************************/
+#include <usbFifoVirtualUsart.h>
+#include <usbFifoUsart.h>
+#include <atomic.h>
+#include <irq.h>
+
+/******************************************************************************
+ External global variables section
+******************************************************************************/
+extern void (* extHandler)(void);
+
+/******************************************************************************
+ Global variables section
+******************************************************************************/
+// pointer to application uart descriptor
+HAL_UsartDescriptor_t *usbfifoPointDescrip = NULL;
+
+/******************************************************************************
+ Implementations section
+******************************************************************************/
+/******************************************************************************
+Open virtual com port and register uart's event handlers.
+
+Parameters:
+ descriptor - pointer to HAL_UartDescriptor_t structure
+
+Returns:
+ Returns positive uart descriptor on success or -1 in cases:
+ - bad uart channel;
+ - there are not enough resources;
+ - receiving buffer is less bulk endpoint size;
+******************************************************************************/
+int USBFIFO_OpenUsart(HAL_UsartDescriptor_t *descriptor)
+{
+ if (NULL == descriptor)
+ return -1;
+
+ if (USART_CHANNEL_USBFIFO != descriptor->tty)
+ return -1;
+
+ if (NULL != usbfifoPointDescrip)
+ return -1; /* source was opened */
+
+ extHandler = usbfifoHandler;
+ usbfifoPointDescrip = descriptor;
+
+ usbfifoPointDescrip->service.rxPointOfRead = 0;
+ usbfifoPointDescrip->service.rxPointOfWrite = 0;
+ usbfifoPointDescrip->service.txPointOfRead = 0;
+ usbfifoPointDescrip->service.txPointOfWrite = 0;
+ usbfifoInit();
+ // enable receiver
+ HAL_EnableIrq(IRQ_7);
+
+ return (int)descriptor->tty;
+}
+
+/******************************************************************************
+Frees the virtual uart channel.
+Parameters:
+ descriptor - the uart descriptor.
+Returns:
+ Returns 0 on success or -1 if bad descriptor.
+******************************************************************************/
+int USBFIFO_CloseUsart(HAL_UsartDescriptor_t *descriptor)
+{
+ if (NULL == descriptor)
+ return -1;
+
+ if (usbfifoPointDescrip != descriptor)
+ return -1;
+
+ usbfifoPointDescrip = NULL;
+ extHandler = NULL;
+ usbfifoUnInit();
+
+ return 0;
+}
+
+/******************************************************************************
+Writes a number of bytes to a virtual uart channel.
+txCallback function will be used to notify when the transmission is finished.
+Parameters:
+ descriptor - pointer to HAL_UartDescriptor_t structure;
+ buffer - pointer to the application data buffer;
+ length - number of bytes to transfer;
+Returns:
+ -1 - bad descriptor;
+ Number of bytes placed to the buffer - success.
+******************************************************************************/
+int USBFIFO_WriteUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ uint16_t poW;
+ uint16_t poR;
+ uint16_t old;
+ uint16_t wasWrote = 0;
+ HalUsartService_t *control;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (usbfifoPointDescrip != descriptor)
+ return -1;
+
+ if (!buffer || !length)
+ return -1;
+
+ control = &descriptor->service;
+ ATOMIC_SECTION_ENTER
+ poW = control->txPointOfWrite;
+ poR = control->txPointOfRead;
+ ATOMIC_SECTION_LEAVE
+
+ if (0 == descriptor->txBufferLength)
+ { // Callback mode
+ if (poW != poR)
+ return -1; // there is unsent data
+ descriptor->txBuffer = buffer;
+ control->txPointOfWrite = length;
+ control->txPointOfRead = 0;
+ wasWrote = length;
+ } // Callback mode.
+ else
+ { // Polling mode.
+ while (wasWrote < length)
+ {
+ old = poW;
+
+ if (++poW == descriptor->txBufferLength)
+ poW = 0;
+
+ if (poW == poR)
+ { // Buffer full.
+ poW = old;
+ break;
+ } // Buffer full.
+
+ descriptor->txBuffer[old] = buffer[wasWrote++];
+ }
+
+ ATOMIC_SECTION_ENTER
+ control->txPointOfWrite = poW;
+ ATOMIC_SECTION_LEAVE
+ } // Polling mode
+
+ HAL_EnableIrq(IRQ_6);
+
+ return wasWrote;
+}
+
+/*****************************************************************************
+Reads length bytes from uart and places ones to buffer.
+Parameters:
+ descriptor - uart descriptor;
+ buffer - pointer to a application buffer;
+ length - the number of bytes which should be placed to buffer
+Returns:
+ -1 - bad descriptor, bad number to read;
+ number of bytes that were placed to buffer.
+*****************************************************************************/
+int USBFIFO_ReadUsart(HAL_UsartDescriptor_t *descriptor, uint8_t *buffer, uint16_t length)
+{
+ uint16_t wasRead = 0;
+ uint16_t poW;
+ uint16_t poR;
+ HalUsartService_t *control;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (!buffer || !length)
+ return -1;
+
+ if (descriptor != usbfifoPointDescrip)
+ return -1; // Channel is not opened.
+
+ control = &usbfifoPointDescrip->service;
+ ATOMIC_SECTION_ENTER
+ poW = control->rxPointOfWrite;
+ poR = control->rxPointOfRead;
+ ATOMIC_SECTION_LEAVE
+
+ while ((poR != poW) && (wasRead < length))
+ {
+ buffer[wasRead] = descriptor->rxBuffer[poR];
+ if (++poR == descriptor->rxBufferLength)
+ poR = 0;
+ wasRead++;
+ }
+
+ ATOMIC_SECTION_ENTER
+ control->rxPointOfRead = poR;
+ control->rxBytesInBuffer -= wasRead;
+ ATOMIC_SECTION_LEAVE
+
+ HAL_EnableIrq(IRQ_7);
+
+ return wasRead;
+}
+
+/**************************************************************************//**
+\brief Checks the status of tx buffer.
+
+\param[in] descriptor - pointer to HAL_UsartDescriptor_t structure;
+\return -1 - bad descriptor, no tx buffer; \n
+ 1 - tx buffer is empty; \n
+ 0 - tx buffer is not empty;
+******************************************************************************/
+int USBFIFO_IsTxEmpty(HAL_UsartDescriptor_t *descriptor)
+{
+ HalUsartService_t *control;
+ uint16_t poW;
+ uint16_t poR;
+
+ if (NULL == descriptor)
+ return -1;
+
+ if (descriptor != usbfifoPointDescrip)
+ return -1; // Channel is not opened.
+
+ control = &usbfifoPointDescrip->service;
+ ATOMIC_SECTION_ENTER
+ poW = control->txPointOfWrite;
+ poR = control->txPointOfRead;
+ ATOMIC_SECTION_LEAVE
+ if (poW == poR)
+ return 1;
+
+ return 0;
+}
+
+// eof usbFifoVirtualUart.c