1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _BOARD_H_
10 #define _BOARD_H_
11 
12 #include "clock_config.h"
13 #include "fsl_gpio.h"
14 
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 
19 /*! @brief The board name */
20 #define BOARD_NAME "TWR-KE18F"
21 
22 /*! @brief The UART to use for debug messages. */
23 #define BOARD_USE_UART
24 #define BOARD_DEBUG_UART_TYPE     kSerialPort_Uart
25 #define BOARD_DEBUG_UART_BASEADDR (uint32_t) LPUART0
26 #define BOARD_DEBUG_UART_INSTANCE 0U
27 #define BOARD_DEBUG_UART_CLKSRC   kCLOCK_ScgSysPllAsyncDiv2Clk
28 #define BOARD_DEBUG_UART_CLK_FREQ CLOCK_GetIpFreq(kCLOCK_Lpuart0)
29 
30 #define BOARD_ACCEL_I2C_BASEADDR   LPI2C0
31 #define BOARD_ACCEL_I2C_CLOCK_FREQ CLOCK_GetIpFreq(kCLOCK_Lpi2c0)
32 
33 #ifndef BOARD_DEBUG_UART_BAUDRATE
34 #define BOARD_DEBUG_UART_BAUDRATE 115200
35 #endif /* BOARD_DEBUG_UART_BAUDRATE */
36 
37 /*! @brief The lpi2c instance used for lpi2c connection by default */
38 #define BOARD_I2C_BASEADDR LPI2C0
39 
40 /*! @brief The rtc instance used for board. */
41 #define BOARD_RTC_FUNC_BASEADDR RTC
42 
43 /*! @brief Define the port interrupt number for the board switches */
44 #ifndef BOARD_SW2_GPIO
45 #define BOARD_SW2_GPIO GPIOD
46 #endif
47 #ifndef BOARD_SW2_PORT
48 #define BOARD_SW2_PORT PORTD
49 #endif
50 #ifndef BOARD_SW2_GPIO_PIN
51 #define BOARD_SW2_GPIO_PIN 3U
52 #endif
53 #define BOARD_SW2_IRQ         PORTD_IRQn
54 #define BOARD_SW2_IRQ_HANDLER PORTD_IRQHandler
55 #define BOARD_SW2_NAME        "SW2"
56 
57 #ifndef BOARD_SW3_GPIO
58 #define BOARD_SW3_GPIO GPIOD
59 #endif
60 #ifndef BOARD_SW3_PORT
61 #define BOARD_SW3_PORT PORTD
62 #endif
63 #ifndef BOARD_SW3_GPIO_PIN
64 #define BOARD_SW3_GPIO_PIN 6U
65 #endif
66 #define BOARD_SW3_IRQ         PORTD_IRQn
67 #define BOARD_SW3_IRQ_HANDLER PORTD_IRQHandler
68 #define BOARD_SW3_NAME        "SW3"
69 
70 /* Board led color mapping */
71 #define LOGIC_LED_ON               0U
72 #define LOGIC_LED_OFF              1U
73 #define BOARD_LED_RED1_GPIO        GPIOC
74 #define BOARD_LED_RED1_GPIO_PORT   PORTC
75 #define BOARD_LED_RED1_GPIO_PIN    10U
76 #define BOARD_LED_GREEN1_GPIO      GPIOC
77 #define BOARD_LED_GREEN1_GPIO_PORT PORTC
78 #define BOARD_LED_GREEN1_GPIO_PIN  11U
79 #ifndef BOARD_LED_YELLOW_GPIO
80 #define BOARD_LED_YELLOW_GPIO GPIOC
81 #endif
82 #define BOARD_LED_YELLOW_GPIO_PORT PORTC
83 #ifndef BOARD_LED_YELLOW_GPIO_PIN
84 #define BOARD_LED_YELLOW_GPIO_PIN 12U
85 #endif
86 #ifndef BOARD_LED_ORANGE_GPIO
87 #define BOARD_LED_ORANGE_GPIO GPIOC
88 #endif
89 #define BOARD_LED_ORANGE_GPIO_PORT PORTC
90 #ifndef BOARD_LED_ORANGE_GPIO_PIN
91 #define BOARD_LED_ORANGE_GPIO_PIN 13U
92 #endif
93 #define BOARD_LED_RED2_GPIO        GPIOD
94 #define BOARD_LED_RED2_GPIO_PORT   PORTD
95 #define BOARD_LED_RED2_GPIO_PIN    16U
96 #define BOARD_LED_GREEN2_GPIO      GPIOD
97 #define BOARD_LED_GREEN2_GPIO_PORT PORTD
98 #define BOARD_LED_GREEN2_GPIO_PIN  15U
99 #ifndef BOARD_LED_BLUE_GPIO
100 #define BOARD_LED_BLUE_GPIO GPIOB
101 #endif
102 #define BOARD_LED_BLUE_GPIO_PORT PORTB
103 #ifndef BOARD_LED_BLUE_GPIO_PIN
104 #define BOARD_LED_BLUE_GPIO_PIN 5U
105 #endif
106 
107 #define LED_RED1_INIT(output)                                            \
108     GPIO_PinWrite(BOARD_LED_RED1_GPIO, BOARD_LED_RED1_GPIO_PIN, output); \
109     BOARD_LED_RED1_GPIO->PDDR |= (1U << BOARD_LED_RED1_GPIO_PIN) /*!< Enable target LED_RED1 */
110 #define LED_RED1_ON()                                                                               \
111     GPIO_PortClear(BOARD_LED_RED1_GPIO, 1U << BOARD_LED_RED1_GPIO_PIN) /*!< Turn on target LED_RED1 \
112                                                                         */
113 #define LED_RED1_OFF()                                                                             \
114     GPIO_PortSet(BOARD_LED_RED1_GPIO, 1U << BOARD_LED_RED1_GPIO_PIN) /*!< Turn off target LED_RED1 \
115                                                                       */
116 #define LED_RED1_TOGGLE() \
117     GPIO_PortToggle(BOARD_LED_RED1_GPIO, 1U << BOARD_LED_RED1_GPIO_PIN) /*!< Toggle on target LED_RED1 */
118 
119 #define LED_GREEN1_INIT(output)                                              \
120     GPIO_PinWrite(BOARD_LED_GREEN1_GPIO, BOARD_LED_GREEN1_GPIO_PIN, output); \
121     BOARD_LED_GREEN1_GPIO->PDDR |= (1U << BOARD_LED_GREEN1_GPIO_PIN) /*!< Enable target LED_GREEN1 */
122 #define LED_GREEN1_ON() \
123     GPIO_PortClear(BOARD_LED_GREEN1_GPIO, 1U << BOARD_LED_GREEN1_GPIO_PIN) /*!< Turn on target LED_GREEN1 */
124 #define LED_GREEN1_OFF() \
125     GPIO_PortSet(BOARD_LED_GREEN1_GPIO, 1U << BOARD_LED_GREEN1_GPIO_PIN) /*!< Turn off target LED_GREEN1 */
126 #define LED_GREEN1_TOGGLE() \
127     GPIO_PortToggle(BOARD_LED_GREEN1_GPIO, 1U << BOARD_LED_GREEN1_GPIO_PIN) /*!< Toggle on target LED_GREEN1 */
128 
129 #define LED_YELLOW_INIT(output)                                              \
130     GPIO_PinWrite(BOARD_LED_YELLOW_GPIO, BOARD_LED_YELLOW_GPIO_PIN, output); \
131     BOARD_LED_YELLOW_GPIO->PDDR |= (1U << BOARD_LED_YELLOW_GPIO_PIN) /*!< Enable target LED_YELLOW */
132 #define LED_YELLOW_ON() \
133     GPIO_PortClear(BOARD_LED_YELLOW_GPIO, 1U << BOARD_LED_YELLOW_GPIO_PIN) /*!< Turn on target LED_YELLOW */
134 #define LED_YELLOW_OFF() \
135     GPIO_PortSet(BOARD_LED_YELLOW_GPIO, 1U << BOARD_LED_YELLOW_GPIO_PIN) /*!< Turn off target LED_YELLOW */
136 #define LED_YELLOW_TOGGLE() \
137     GPIO_PortToggle(BOARD_LED_YELLOW_GPIO, 1U << BOARD_LED_YELLOW_GPIO_PIN) /*!< Toggle on target LED_YELLOW */
138 
139 #define LED_ORANGE_INIT(output)                                              \
140     GPIO_PinWrite(BOARD_LED_ORANGE_GPIO, BOARD_LED_ORANGE_GPIO_PIN, output); \
141     BOARD_LED_ORANGE_GPIO->PDDR |= (1U << BOARD_LED_ORANGE_GPIO_PIN) /*!< Enable target LED_ORANGE */
142 #define LED_ORANGE_ON() \
143     GPIO_PortClear(BOARD_LED_ORANGE_GPIO, 1U << BOARD_LED_ORANGE_GPIO_PIN) /*!< Turn on target LED_ORANGE */
144 #define LED_ORANGE_OFF() \
145     GPIO_PortSet(BOARD_LED_ORANGE_GPIO, 1U << BOARD_LED_ORANGE_GPIO_PIN) /*!< Turn off target LED_ORANGE */
146 #define LED_ORANGE_TOGGLE() \
147     GPIO_PortToggle(BOARD_LED_ORANGE_GPIO, 1U << BOARD_LED_ORANGE_GPIO_PIN) /*!< Toggle on target LED_ORANGE */
148 
149 #define LED_RED2_INIT(output)                                            \
150     GPIO_PinWrite(BOARD_LED_RED2_GPIO, BOARD_LED_RED2_GPIO_PIN, output); \
151     BOARD_LED_RED2_GPIO->PDDR |= (1U << BOARD_LED_RED2_GPIO_PIN) /*!< Enable target LED_RED2 */
152 #define LED_RED2_ON()                                                                               \
153     GPIO_PortClear(BOARD_LED_RED2_GPIO, 1U << BOARD_LED_RED2_GPIO_PIN) /*!< Turn on target LED_RED2 \
154                                                                         */
155 #define LED_RED2_OFF()                                                                             \
156     GPIO_PortSet(BOARD_LED_RED2_GPIO, 1U << BOARD_LED_RED2_GPIO_PIN) /*!< Turn off target LED_RED2 \
157                                                                       */
158 #define LED_RED2_TOGGLE() \
159     GPIO_PortToggle(BOARD_LED_RED2_GPIO, 1U << BOARD_LED_RED2_GPIO_PIN) /*!< Toggle on target LED_RED2 */
160 
161 #define LED_GREEN2_INIT(output)                                              \
162     GPIO_PinWrite(BOARD_LED_GREEN2_GPIO, BOARD_LED_GREEN2_GPIO_PIN, output); \
163     BOARD_LED_GREEN2_GPIO->PDDR |= (1U << BOARD_LED_GREEN2_GPIO_PIN) /*!< Enable target LED_GREEN2 */
164 #define LED_GREEN2_ON() \
165     GPIO_PortClear(BOARD_LED_GREEN2_GPIO, 1U << BOARD_LED_GREEN2_GPIO_PIN) /*!< Turn on target LED_GREEN2 */
166 #define LED_GREEN2_OFF() \
167     GPIO_PortSet(BOARD_LED_GREEN2_GPIO, 1U << BOARD_LED_GREEN2_GPIO_PIN) /*!< Turn off target LED_GREEN2 */
168 #define LED_GREEN2_TOGGLE() \
169     GPIO_PortToggle(BOARD_LED_GREEN2_GPIO, 1U << BOARD_LED_GREEN2_GPIO_PIN) /*!< Toggle on target LED_GREEN2 */
170 
171 #define LED_BLUE_INIT(output)                                            \
172     GPIO_PinWrite(BOARD_LED_BLUE_GPIO, BOARD_LED_BLUE_GPIO_PIN, output); \
173     BOARD_LED_BLUE_GPIO->PDDR |= (1U << BOARD_LED_BLUE_GPIO_PIN) /*!< Enable target LED_BLUE */
174 #define LED_BLUE_ON()                                                                               \
175     GPIO_PortClear(BOARD_LED_BLUE_GPIO, 1U << BOARD_LED_BLUE_GPIO_PIN) /*!< Turn on target LED_BLUE \
176                                                                         */
177 #define LED_BLUE_OFF()                                                                             \
178     GPIO_PortSet(BOARD_LED_BLUE_GPIO, 1U << BOARD_LED_BLUE_GPIO_PIN) /*!< Turn off target LED_BLUE \
179                                                                       */
180 #define LED_BLUE_TOGGLE() \
181     GPIO_PortToggle(BOARD_LED_BLUE_GPIO, 1U << BOARD_LED_BLUE_GPIO_PIN) /*!< Toggle on target LED_BLUE */
182 
183 #if defined(__cplusplus)
184 extern "C" {
185 #endif /* __cplusplus */
186 
187 /*******************************************************************************
188  * API
189  ******************************************************************************/
190 
191 void BOARD_InitDebugConsole(void);
192 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
193 void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz);
194 status_t BOARD_LPI2C_Send(LPI2C_Type *base,
195                           uint8_t deviceAddress,
196                           uint32_t subAddress,
197                           uint8_t subaddressSize,
198                           uint8_t *txBuff,
199                           uint8_t txBuffSize);
200 status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
201                              uint8_t deviceAddress,
202                              uint32_t subAddress,
203                              uint8_t subaddressSize,
204                              uint8_t *rxBuff,
205                              uint8_t rxBuffSize);
206 void BOARD_Accel_I2C_Init(void);
207 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff);
208 status_t BOARD_Accel_I2C_Receive(
209     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize);
210 #endif /* SDK_I2C_BASED_COMPONENT_USED */
211 
212 #if defined(__cplusplus)
213 }
214 #endif /* __cplusplus */
215 
216 #endif /* _BOARD_H_ */
217