BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
serial.h
Go to the documentation of this file.
1 /* Copyright 2012-2014, Peter A. Bigot
2  *
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the software nor the names of its contributors may be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
46 #ifndef BSP430_SERIAL_H
47 #define BSP430_SERIAL_H
48 
49 #include <bsp430/core.h>
50 #include <bsp430/periph.h>
51 
52 /* NOTE TO MAINTAINER: You will be tempted to refactor the boolean
53  * configBSP430_SERIAL_USE_FOO flag to a valued
54  * configBSP430_SERIAL_USE_VARIANT==BSP430_SERIAL_HAL_HPL_VARIANT_FOO
55  * flag. Don't. There are MCUs (such as msp430fg4618) that provide
56  * serial capabilities through multiple modules (USART+USCI) where SPI
57  * might come from one and UART from another. */
58 
66 #ifndef configBSP430_SERIAL_USE_USCI
67 #define configBSP430_SERIAL_USE_USCI defined(__MSP430_HAS_USCI__)
68 #endif /* configBSP430_SERIAL_USE_USCI */
69 
77 #ifndef configBSP430_SERIAL_USE_USCI5
78 #define configBSP430_SERIAL_USE_USCI5 defined(__MSP430_HAS_USCI_A0__)
79 #endif /* configBSP430_SERIAL_USE_USCI5 */
80 
88 #ifndef configBSP430_SERIAL_USE_EUSCI
89 #define configBSP430_SERIAL_USE_EUSCI (defined(__MSP430_HAS_EUSCI_A0__) || defined(__MSP430_HAS_EUSCI_B0__))
90 #endif /* configBSP430_SERIAL_USE_EUSCI */
91 
103 #ifndef configBSP430_SERIAL_ENABLE_UART
104 #define configBSP430_SERIAL_ENABLE_UART (configBSP430_CONSOLE - 0)
105 #endif /* configBSP430_SERIAL_ENABLE_UART */
106 
118 #ifndef configBSP430_SERIAL_ENABLE_SPI
119 #define configBSP430_SERIAL_ENABLE_SPI 0
120 #endif /* configBSP430_SERIAL_ENABLE_SPI */
121 
134 #ifndef BSP430_SERIAL_SPI_BUS_SPEED_HZ
135 #define BSP430_SERIAL_SPI_BUS_SPEED_HZ 4000000UL
136 #endif /* BSP430_SERIAL_SPI_BUS_SPEED_HZ */
137 
149 #ifndef configBSP430_SERIAL_ENABLE_I2C
150 #define configBSP430_SERIAL_ENABLE_I2C 0
151 #endif /* configBSP430_SERIAL_ENABLE_I2C */
152 
167 #ifndef BSP430_SERIAL_I2C_BUS_SPEED_HZ
168 #define BSP430_SERIAL_I2C_BUS_SPEED_HZ 400000UL
169 #endif /* BSP430_SERIAL_SPI_BUS_SPEED_HZ */
170 
177 #define BSP430_SERIAL \
178  ((configBSP430_SERIAL_ENABLE_UART - 0) \
179  || (configBSP430_SERIAL_ENABLE_SPI - 0) \
180  || (configBSP430_SERIAL_ENABLE_I2C - 0))
181 
182 #include <bsp430/serial_.h>
183 
184 #if defined(BSP430_DOXYGEN) || (BSP430_SERIAL - 0)
185 
204 #define BSP430_SERIAL_ADJUST_CTL0_INITIALIZER(_i) (((_i) >= 0x100) ? ((_i) >> 8) : (_i))
205 
220 unsigned int uiBSP430serialSMCLKPrescaler (unsigned long freq_Hz);
221 
222 #if defined(BSP430_DOXYGEN) || (configBSP430_SERIAL_ENABLE_UART - 0)
223 
258 static BSP430_CORE_INLINE
260  unsigned char ctl0_byte,
261  unsigned char ctl1_byte,
262  unsigned long baud)
263 {
264  return hal->dispatch->openUART(hal, ctl0_byte, ctl1_byte, baud);
265 }
266 
279 static BSP430_CORE_INLINE
281 {
282  return hal->dispatch->uartRxByte_rh(hal);
283 }
284 
299 static BSP430_CORE_INLINE
301 {
302  return hal->dispatch->uartTxByte_rh(hal, c);
303 }
304 
321 static BSP430_CORE_INLINE
323  const uint8_t * data,
324  size_t len)
325 {
326  return hal->dispatch->uartTxData_rh(hal, data, len);
327 }
328 
345 static BSP430_CORE_INLINE
346 int iBSP430uartTxASCIIZ_rh (hBSP430halSERIAL hal, const char * str)
347 {
348  return hal->dispatch->uartTxASCIIZ_rh(hal, str);
349 }
350 #endif /* configBSP430_SERIAL_ENABLE_UART */
351 
352 #if defined(BSP430_DOXYGEN) || (configBSP430_SERIAL_ENABLE_SPI - 0)
353 
399 static BSP430_CORE_INLINE
401  unsigned char ctl0_byte,
402  unsigned char ctl1_byte,
403  unsigned int prescaler)
404 {
405  return hal->dispatch->openSPI(hal, ctl0_byte, ctl1_byte, prescaler);
406 }
407 
446 static BSP430_CORE_INLINE
448  const uint8_t * tx_data,
449  size_t tx_len,
450  size_t rx_len,
451  uint8_t * rx_data)
452 {
453  return hal->dispatch->spiTxRx_rh(hal, tx_data, tx_len, rx_len, rx_data);
454 }
455 
456 #endif /* configBSP430_SERIAL_ENABLE_SPI */
457 
468 #ifndef BSP430_I2C_SPIN_LIMIT
469 #define BSP430_I2C_SPIN_LIMIT 65536
470 #endif /* BSP430_I2C_SPIN_LIMIT */
471 
484 #define BSP430_I2C_ERRFLAG_PROTOCOL 0x4000
485 
499 #define BSP430_I2C_ERRFLAG_SPINLIMIT 0x2000
500 
501 #if defined(BSP430_DOXYGEN) || (configBSP430_SERIAL_ENABLE_I2C - 0)
502 
538 static BSP430_CORE_INLINE
540  unsigned char ctl0_byte,
541  unsigned char ctl1_byte,
542  unsigned int prescaler)
543 {
544  return hal->dispatch->openI2C(hal, ctl0_byte, ctl1_byte, prescaler);
545 }
546 
567 static BSP430_CORE_INLINE
569  int own_address,
570  int slave_address)
571 {
572  return hal->dispatch->i2cSetAddresses_rh(hal, own_address, slave_address);
573 }
574 
605 static BSP430_CORE_INLINE
607  const uint8_t * tx_data,
608  size_t tx_len)
609 {
610  return hal->dispatch->i2cTxData_rh(hal, tx_data, tx_len);
611 }
612 
644 static BSP430_CORE_INLINE
646  uint8_t * rx_data,
647  size_t rx_len)
648 {
649  return hal->dispatch->i2cRxData_rh(hal, rx_data, rx_len);
650 }
651 #endif /* configBSP430_SERIAL_ENABLE_I2C */
652 
702 static BSP430_CORE_INLINE
704  int resetp)
705 {
706  return hal->dispatch->setReset_rh(hal, resetp);
707 }
708 
733 static BSP430_CORE_INLINE
735  int holdp)
736 {
737  return hal->dispatch->setHold_rh(hal, holdp);
738 }
739 
751 static BSP430_CORE_INLINE
753 {
754  return hal->dispatch->close(hal);
755 }
756 
771 static BSP430_CORE_INLINE
773 {
774  hal->dispatch->wakeupTransmit_rh(hal);
775 }
776 
796 static BSP430_CORE_INLINE
798 {
799  hal->dispatch->flush_ni(hal);
800 }
801 
811 static BSP430_CORE_INLINE
813 {
814  return hal->dispatch->rate(hal);
815 }
816 
817 #endif /* BSP430_SERIAL - 0 */
818 
819 #if (configBSP430_SERIAL_USE_USCI - 0)
820 #include <bsp430/periph/usci.h>
821 #endif /* configBSP430_SERIAL_USE_USCI */
822 #if (configBSP430_SERIAL_USE_USCI5 - 0)
823 #include <bsp430/periph/usci5.h>
824 #endif /* configBSP430_SERIAL_USE_USCI5 */
825 #if (configBSP430_SERIAL_USE_EUSCI - 0)
826 #include <bsp430/periph/eusci.h>
827 #endif /* configBSP430_SERIAL_USE_EUSCI */
828 
837 static BSP430_CORE_INLINE
839 {
840  hBSP430halSERIAL rv = NULL;
841 #if (configBSP430_SERIAL_USE_USCI - 0)
842  if (NULL == rv) {
843  rv = hBSP430usciLookup(periph);
844  }
845 #endif /* configBSP430_SERIAL_USE_USCI */
846 #if (configBSP430_SERIAL_USE_USCI5 - 0)
847  if (NULL == rv) {
848  rv = hBSP430usci5Lookup(periph);
849  }
850 #endif /* configBSP430_SERIAL_USE_USCI5 */
851 #if (configBSP430_SERIAL_USE_EUSCI - 0)
852  if (NULL == rv) {
853  rv = hBSP430eusciLookup(periph);
854  }
855 #endif /* configBSP430_SERIAL_USE_EUSCI */
856  return rv;
857 }
858 
865 const char * xBSP430serialName (tBSP430periphHandle periph);
866 
867 #endif /* BSP430_SERIAL_H */
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430serialOpenSPI(hBSP430halSERIAL hal, unsigned char ctl0_byte, unsigned char ctl1_byte, unsigned int prescaler)
Definition: serial.h:400
static BSP430_CORE_INLINE int iBSP430uartTxASCIIZ_rh(hBSP430halSERIAL hal, const char *str)
Definition: serial.h:346
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430usciLookup(tBSP430periphHandle periph)
Definition: usci.h:701
static BSP430_CORE_INLINE int iBSP430serialClose(hBSP430halSERIAL hal)
Definition: serial.h:752
static BSP430_CORE_INLINE int iBSP430i2cTxData_rh(hBSP430halSERIAL hal, const uint8_t *tx_data, size_t tx_len)
Definition: serial.h:606
const char * xBSP430serialName(tBSP430periphHandle periph)
static BSP430_CORE_INLINE int iBSP430i2cRxData_rh(hBSP430halSERIAL hal, uint8_t *rx_data, size_t rx_len)
Definition: serial.h:645
static BSP430_CORE_INLINE unsigned long ulBSP430serialRate(hBSP430halSERIAL hal)
Definition: serial.h:812
static BSP430_CORE_INLINE int iBSP430serialSetReset_rh(hBSP430halSERIAL hal, int resetp)
Definition: serial.h:703
static BSP430_CORE_INLINE void vBSP430serialFlush_ni(hBSP430halSERIAL hal)
Definition: serial.h:797
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430serialOpenI2C(hBSP430halSERIAL hal, unsigned char ctl0_byte, unsigned char ctl1_byte, unsigned int prescaler)
Definition: serial.h:539
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430serialOpenUART(hBSP430halSERIAL hal, unsigned char ctl0_byte, unsigned char ctl1_byte, unsigned long baud)
Definition: serial.h:259
Common header included by all BSP430 leaf headers.
Definition: serial_.h:193
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430serialLookup(tBSP430periphHandle periph)
Definition: serial.h:838
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430usci5Lookup(tBSP430periphHandle periph)
Definition: usci5.h:1169
static BSP430_CORE_INLINE int iBSP430uartTxByte_rh(hBSP430halSERIAL hal, uint8_t c)
Definition: serial.h:300
static BSP430_CORE_INLINE int iBSP430spiTxRx_rh(hBSP430halSERIAL hal, const uint8_t *tx_data, size_t tx_len, size_t rx_len, uint8_t *rx_data)
Definition: serial.h:447
static BSP430_CORE_INLINE int iBSP430serialSetHold_rh(hBSP430halSERIAL hal, int holdp)
Definition: serial.h:734
#define BSP430_CORE_INLINE
Definition: core.h:439
Generic peripheral support for MSP430 MCUs.
unsigned int uiBSP430serialSMCLKPrescaler(unsigned long freq_Hz)
static BSP430_CORE_INLINE int iBSP430uartTxData_rh(hBSP430halSERIAL hal, const uint8_t *data, size_t len)
Definition: serial.h:322
static BSP430_CORE_INLINE hBSP430halSERIAL hBSP430eusciLookup(tBSP430periphHandle periph)
Definition: eusci.h:1018
Hardware presentation/abstraction for USCI_A/USCI_B on 2xx/4xx devices.
static BSP430_CORE_INLINE void vBSP430serialWakeupTransmit_rh(hBSP430halSERIAL hal)
Definition: serial.h:772
int tBSP430periphHandle
Definition: periph.h:106
static BSP430_CORE_INLINE int iBSP430i2cSetAddresses_rh(hBSP430halSERIAL hal, int own_address, int slave_address)
Definition: serial.h:568
Hardware presentation/abstraction for eUSCI.
Structures for abstracted serial interface.
Hardware presentation/abstraction for USCI_A/USCI_B on 5xx/6xx devices.
static BSP430_CORE_INLINE int iBSP430uartRxByte_rh(hBSP430halSERIAL hal)
Definition: serial.h:280