<commit_before>/**
 * @file    uart.c
 * @brief
 *
 * DAPLink Interface Firmware
 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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.
 */

#include "string.h"

#include "stm32h7xx.h"
#include "uart.h"
#include "gpio.h"
#include "util.h"
#include "circ_buf.h"
#include "IO_Config.h"

//#include "stm32h7xx_hal.h"  //elee: porting to h7

/*
// UDB: UART4, on the AUX connector (for bringup).
// pd1=UART4_TX pd0=UART4_RX
#define CDC_UART                     UART4
#define CDC_UART_ENABLE()            __HAL_RCC_UART4_CLK_ENABLE()
#define CDC_UART_DISABLE()           __HAL_RCC_UART4_CLK_DISABLE()
#define CDC_UART_IRQn                UART4_IRQn
#define CDC_UART_IRQn_Handler        UART4_IRQHandler

#define UART_PINS_PORT_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
#define UART_PINS_PORT_DISABLE()     __HAL_RCC_GPIOD_CLK_DISABLE()

#define UART_ALTFUNC                 GPIO_AF8_UART4  //Select the correct alt func

#define UART_TX_PORT                 GPIOD
#define UART_TX_PIN                  GPIO_PIN_1

#define UART_RX_PORT                 GPIOD
#define UART_RX_PIN                  GPIO_PIN_0
*/


// UDB: USART3, UART0_MCU_UDC_TXD or UART0_UDC_TXD
// pd8=USART3_TX pd9=USART3_RX
#define CDC_UART                     USART3
#define CDC_UART_ENABLE()            __HAL_RCC_USART3_CLK_ENABLE()
#define CDC_UART_DISABLE()           __HAL_RCC_USART3_CLK_DISABLE()
#define CDC_UART_IRQn                USART3_IRQn
#define CDC_UART_IRQn_Handler        USART3_IRQHandler

#define UART_PINS_PORT_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
#define UART_PINS_PORT_DISABLE()     __HAL_RCC_GPIOD_CLK_DISABLE()

#define UART_ALTFUNC                 GPIO_AF7_USART3  //Select the correct alt func

#define UART_TX_PORT                 GPIOD
#define UART_TX_PIN                  GPIO_PIN_8

#define UART_RX_PORT                 GPIOD
#define UART_RX_PIN                  GPIO_PIN_9

//ToDo(elee): CTS/RTS not used on main UDC uarts.  How to ensure they are not used?  Just don't set the Alternate function?
// Just choose one of the possible pins for CTS/RTS for usart3 for now.
#define UART_CTS_PORT                GPIOD
#define UART_CTS_PIN                 GPIO_PIN_11

#define UART_RTS_PORT                GPIOD
#define UART_RTS_PIN                 GPIO_PIN_12


#define RX_OVRF_MSG         "<DAPLink:Overflow>\n"
#define RX_OVRF_MSG_SIZE    (sizeof(RX_OVRF_MSG) - 1)
#define BUFFER_SIZE         (512)

circ_buf_t write_buffer;
uint8_t write_buffer_data[BUFFER_SIZE];
circ_buf_t read_buffer;
uint8_t read_buffer_data[BUFFER_SIZE];

static UART_Configuration configuration = {
    .Baudrate = 9600,
    .DataBits = UART_DATA_BITS_8,
    .Parity = UART_PARITY_NONE_DAPLINK,
    .StopBits = UART_STOP_BITS_1,
    .FlowControl = UART_FLOW_CONTROL_NONE,
};

extern uint32_t SystemCoreClock;



static void clear_buffers(void)
{
    circ_buf_init(&write_buffer, write_buffer_data, sizeof(write_buffer_data));
    circ_buf_init(&read_buffer, read_buffer_data, sizeof(read_buffer_data));
}

int32_t uart_initialize(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    CDC_UART->CR1 &= ~(USART_ISR_TXE_TXFNF | USART_ISR_RXNE_RXFNE);
    clear_buffers();<commit_msg>Make each UART's read/write buffers static<commit_after>/**
 * @file    uart.c
 * @brief
 *
 * DAPLink Interface Firmware
 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved
 * SPDX-License-Identifier: Apache-2.0
 *
 * 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.
 */

#include "string.h"

#include "stm32h7xx.h"
#include "uart.h"
#include "gpio.h"
#include "util.h"
#include "circ_buf.h"
#include "IO_Config.h"

//#include "stm32h7xx_hal.h"  //elee: porting to h7

/*
// UDB: UART4, on the AUX connector (for bringup).
// pd1=UART4_TX pd0=UART4_RX
#define CDC_UART                     UART4
#define CDC_UART_ENABLE()            __HAL_RCC_UART4_CLK_ENABLE()
#define CDC_UART_DISABLE()           __HAL_RCC_UART4_CLK_DISABLE()
#define CDC_UART_IRQn                UART4_IRQn
#define CDC_UART_IRQn_Handler        UART4_IRQHandler

#define UART_PINS_PORT_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
#define UART_PINS_PORT_DISABLE()     __HAL_RCC_GPIOD_CLK_DISABLE()

#define UART_ALTFUNC                 GPIO_AF8_UART4  //Select the correct alt func

#define UART_TX_PORT                 GPIOD
#define UART_TX_PIN                  GPIO_PIN_1

#define UART_RX_PORT                 GPIOD
#define UART_RX_PIN                  GPIO_PIN_0
*/


// UDB: USART3, UART0_MCU_UDC_TXD or UART0_UDC_TXD
// pd8=USART3_TX pd9=USART3_RX
#define CDC_UART                     USART3
#define CDC_UART_ENABLE()            __HAL_RCC_USART3_CLK_ENABLE()
#define CDC_UART_DISABLE()           __HAL_RCC_USART3_CLK_DISABLE()
#define CDC_UART_IRQn                USART3_IRQn
#define CDC_UART_IRQn_Handler        USART3_IRQHandler

#define UART_PINS_PORT_ENABLE()      __HAL_RCC_GPIOD_CLK_ENABLE()
#define UART_PINS_PORT_DISABLE()     __HAL_RCC_GPIOD_CLK_DISABLE()

#define UART_ALTFUNC                 GPIO_AF7_USART3  //Select the correct alt func

#define UART_TX_PORT                 GPIOD
#define UART_TX_PIN                  GPIO_PIN_8

#define UART_RX_PORT                 GPIOD
#define UART_RX_PIN                  GPIO_PIN_9

//ToDo(elee): CTS/RTS not used on main UDC uarts.  How to ensure they are not used?  Just don't set the Alternate function?
// Just choose one of the possible pins for CTS/RTS for usart3 for now.
#define UART_CTS_PORT                GPIOD
#define UART_CTS_PIN                 GPIO_PIN_11

#define UART_RTS_PORT                GPIOD
#define UART_RTS_PIN                 GPIO_PIN_12


#define RX_OVRF_MSG         "<DAPLink:Overflow>\n"
#define RX_OVRF_MSG_SIZE    (sizeof(RX_OVRF_MSG) - 1)
#define BUFFER_SIZE         (512)

static circ_buf_t write_buffer;
static uint8_t write_buffer_data[BUFFER_SIZE];
static circ_buf_t read_buffer;
static uint8_t read_buffer_data[BUFFER_SIZE];

static UART_Configuration configuration = {
    .Baudrate = 9600,
    .DataBits = UART_DATA_BITS_8,
    .Parity = UART_PARITY_NONE_DAPLINK,
    .StopBits = UART_STOP_BITS_1,
    .FlowControl = UART_FLOW_CONTROL_NONE,
};

extern uint32_t SystemCoreClock;



static void clear_buffers(void)
{
    circ_buf_init(&write_buffer, write_buffer_data, sizeof(write_buffer_data));
    circ_buf_init(&read_buffer, read_buffer_data, sizeof(read_buffer_data));
}

int32_t uart_initialize(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    CDC_UART->CR1 &= ~(USART_ISR_TXE_TXFNF | USART_ISR_RXNE_RXFNE);
    clear_buffers();
{'fraction_non_alphanumeric': 0.07461304405344622, 'fraction_numerical': 0.01931472416986374, 'mean_word_length': 3.197667962243198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6632516', 'n_tokens_mistral': 2773, 'n_tokens_neox': 2613, 'n_words': 630}