1 /*
2 * Copyright (c) 2013-2018 Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include "Driver_USART.h"
20
21 #include "cmsis.h"
22 #include "stm32hal.h"
23 /* board configuration */
24 #include "board.h"
25 #ifndef ARG_UNUSED
26 #define ARG_UNUSED(arg) (void)arg
27 #endif /* ARG_UNUSED */
28 #define USART_DRV_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2, 2)
29
30 /* Driver Version */
31 static const ARM_DRIVER_VERSION DriverVersion =
32 {
33 ARM_USART_API_VERSION,
34 USART_DRV_VERSION
35 };
36
37 /* Driver Capabilities */
38 static const ARM_USART_CAPABILITIES DriverCapabilities =
39 {
40 1, /* supports UART (Asynchronous) mode */
41 0, /* supports Synchronous Master mode */
42 0, /* supports Synchronous Slave mode */
43 0, /* supports UART Single-wire mode */
44 0, /* supports UART IrDA mode */
45 0, /* supports UART Smart Card mode */
46 0, /* Smart Card Clock generator available */
47 0, /* RTS Flow Control available */
48 0, /* CTS Flow Control available */
49 0, /* Transmit completed event: \ref USARTx_EVENT_TX_COMPLETE */
50 0, /* Signal receive character timeout event: \ref USARTx_EVENT_RX_TIMEOUT */
51 0, /* RTS Line: 0=not available, 1=available */
52 0, /* CTS Line: 0=not available, 1=available */
53 0, /* DTR Line: 0=not available, 1=available */
54 0, /* DSR Line: 0=not available, 1=available */
55 0, /* DCD Line: 0=not available, 1=available */
56 0, /* RI Line: 0=not available, 1=available */
57 0, /* Signal CTS change event: \ref USARTx_EVENT_CTS */
58 0, /* Signal DSR change event: \ref USARTx_EVENT_DSR */
59 0, /* Signal DCD change event: \ref USARTx_EVENT_DCD */
60 0, /* Signal RI change event: \ref USARTx_EVENT_RI */
61 0 /* Reserved */
62 };
63
USART_GetVersion(void)64 static ARM_DRIVER_VERSION USART_GetVersion(void)
65 {
66 return DriverVersion;
67 }
68
USART_GetCapabilities(void)69 static ARM_USART_CAPABILITIES USART_GetCapabilities(void)
70 {
71 return DriverCapabilities;
72 }
73
74 static UART_HandleTypeDef uart_device;
USART0_Initialize(ARM_USART_SignalEvent_t cb_event)75 static int32_t USART0_Initialize(ARM_USART_SignalEvent_t cb_event)
76 {
77
78 #if !defined(__DOMAIN_NS)
79 GPIO_InitTypeDef GPIO_Init;
80 /* Configure COM Tx as alternate function */
81 COM_TX_GPIO_CLK_ENABLE();
82 COM_RX_GPIO_CLK_ENABLE();
83 COM_CLK_ENABLE();
84 GPIO_Init.Pin = COM_TX_PIN;
85 GPIO_Init.Mode = GPIO_MODE_AF_PP;
86 GPIO_Init.Speed = GPIO_SPEED_FREQ_HIGH;
87 GPIO_Init.Pull = GPIO_PULLUP;
88 GPIO_Init.Alternate = COM_TX_AF;
89 HAL_GPIO_Init(COM_TX_GPIO_PORT, &GPIO_Init);
90
91 /* Configure COM Rx as alternate function */
92 GPIO_Init.Pin = COM_RX_PIN;
93 GPIO_Init.Alternate = COM_RX_AF;
94 HAL_GPIO_Init(COM_RX_GPIO_PORT, &GPIO_Init);
95 #endif /* __DOMAIN_NS */
96 uart_device.Instance = COM_INSTANCE;
97 uart_device.Init.BaudRate = 115200;
98 uart_device.Init.WordLength = UART_WORDLENGTH_8B;
99 uart_device.Init.StopBits = UART_STOPBITS_1;
100 uart_device.Init.Parity = UART_PARITY_NONE;
101 uart_device.Init.Mode = UART_MODE_TX_RX;
102 uart_device.Init.HwFlowCtl = UART_HWCONTROL_NONE;
103 uart_device.Init.OverSampling = UART_OVERSAMPLING_8;
104 uart_device.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
105 uart_device.Init.ClockPrescaler = UART_PRESCALER_DIV1;
106
107 /* Initialize COM */
108 if (HAL_UART_Init(&uart_device) != HAL_OK)
109 {
110 return ARM_DRIVER_ERROR;
111 }
112 return ARM_DRIVER_OK;
113 }
114
USART0_Uninitialize(void)115 static int32_t USART0_Uninitialize(void)
116 {
117 /* Nothing to be done */
118 return ARM_DRIVER_OK;
119 }
120
USART0_PowerControl(ARM_POWER_STATE state)121 static int32_t USART0_PowerControl(ARM_POWER_STATE state)
122 {
123 return ARM_DRIVER_OK;
124 }
125
USART0_Send(const void * data,uint32_t num)126 static int32_t USART0_Send(const void *data, uint32_t num)
127 {
128 if ((data == NULL) || (num == 0U))
129 {
130 /* Invalid parameters */
131 return ARM_DRIVER_ERROR_PARAMETER;
132 }
133 HAL_UART_Transmit(&uart_device, (uint8_t *) data, num, 1000);
134
135 return ARM_DRIVER_OK;
136 }
137
USART0_Receive(void * data,uint32_t num)138 static int32_t USART0_Receive(void *data, uint32_t num)
139 {
140 if ((data == NULL) || (num == 0U))
141 {
142 /* Invalid parameters */
143 return ARM_DRIVER_ERROR_PARAMETER;
144 }
145 HAL_UART_Receive_IT(&uart_device, data, num);
146
147 return num;
148 }
149
USART0_Transfer(const void * data_out,void * data_in,uint32_t num)150 static int32_t USART0_Transfer(const void *data_out, void *data_in,
151 uint32_t num)
152 {
153 ARG_UNUSED(data_out);
154 ARG_UNUSED(data_in);
155 ARG_UNUSED(num);
156
157 return ARM_DRIVER_ERROR_UNSUPPORTED;
158 }
159
USART0_GetTxCount(void)160 static uint32_t USART0_GetTxCount(void)
161 {
162 return 0;
163 }
164
USART0_GetRxCount(void)165 static uint32_t USART0_GetRxCount(void)
166 {
167 return 0;
168 }
USART0_Control(uint32_t control,uint32_t arg)169 static int32_t USART0_Control(uint32_t control, uint32_t arg)
170 {
171 return ARM_DRIVER_OK;
172 }
173
USART0_GetStatus(void)174 static ARM_USART_STATUS USART0_GetStatus(void)
175 {
176 ARM_USART_STATUS status = {0, 0, 0, 0, 0, 0, 0, 0};
177 return status;
178 }
179
USART0_SetModemControl(ARM_USART_MODEM_CONTROL control)180 static int32_t USART0_SetModemControl(ARM_USART_MODEM_CONTROL control)
181 {
182 ARG_UNUSED(control);
183 return ARM_DRIVER_ERROR_UNSUPPORTED;
184 }
185
USART0_GetModemStatus(void)186 static ARM_USART_MODEM_STATUS USART0_GetModemStatus(void)
187 {
188 ARM_USART_MODEM_STATUS modem_status = {0, 0, 0, 0, 0};
189 return modem_status;
190 }
191
192 ARM_DRIVER_USART Driver_USART0 =
193 {
194 USART_GetVersion,
195 USART_GetCapabilities,
196 USART0_Initialize,
197 USART0_Uninitialize,
198 USART0_PowerControl,
199 USART0_Send,
200 USART0_Receive,
201 USART0_Transfer,
202 USART0_GetTxCount,
203 USART0_GetRxCount,
204 USART0_Control,
205 USART0_GetStatus,
206 USART0_SetModemControl,
207 USART0_GetModemStatus
208 };
209