Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving HICs to use CMSIS UART Drivers #887

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from
1 change: 1 addition & 0 deletions records/hic_hal/kl27z.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ common:
- source/hic_hal/freescale
- source/hic_hal/freescale/kl27z
- source/hic_hal/freescale/kl27z/MKL27Z4
- source/hic_hal/cmsis-driver/uart
fsl_flash_driver:
- source/hic_hal/freescale/iap

Expand Down
1 change: 1 addition & 0 deletions records/hic_hal/lpc55s26.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common:
- source/hic_hal/nxp/lpc55xx
- source/hic_hal/nxp/lpc55xx/drivers
- source/hic_hal/nxp/lpc55xx/LPC55S26
- source/hic_hal/cmsis-driver/uart

tool_specific:
gcc_arm:
Expand Down
1 change: 1 addition & 0 deletions records/hic_hal/lpc55s69.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ common:
- source/hic_hal/nxp/lpc55xx
- source/hic_hal/nxp/lpc55xx/drivers
- source/hic_hal/nxp/lpc55xx/LPC55S69
- source/hic_hal/cmsis-driver/uart

tool_specific:
uvision:
Expand Down
1 change: 1 addition & 0 deletions records/hic_hal/nrf52820.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ common:
- source/hic_hal/nordic/nrf52820
- source/hic_hal/nordic/nrf52820/cmsis
- source/hic_hal/nordic/nrf52820/nrfx/drivers
- source/hic_hal/cmsis-driver/uart

tool_specific:
uvision:
Expand Down
2 changes: 2 additions & 0 deletions records/hic_hal/stm32f103xb.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ common:
- INTERFACE_STM32F103XB
- USE_HAL_DRIVER
- STM32F103xB
- STM32F10X_MD
- DAPLINK_HIC_ID=0x97969908 # DAPLINK_HIC_ID_STM32F103XB
- __packed=__packed # Prevent redefinition of __packed with ARMCC
- DAPLINK_NO_ASSERT_FILENAMES
Expand All @@ -21,6 +22,7 @@ common:
- source/hic_hal/stm32/stm32f103xb
- source/hic_hal/stm32/stm32f103xb/cmsis
- source/hic_hal/stm32/stm32f103xb/STM32F1xx_HAL_Driver/Src
- source/hic_hal/cmsis-driver/uart/uart.c

tool_specific:
uvision:
Expand Down
1 change: 1 addition & 0 deletions source/board/microbitv2/i2c_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "storage.h"
#include "gpio_extra.h"
#include "microbitv2.h"
#include "util.h"


extern uint16_t board_id_hex;
Expand Down
22 changes: 11 additions & 11 deletions source/board/microbitv2/kl27z/IO_Config_Override.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_KL27Z);
// Target Running LED Not available

// UART
#define UART_PORT PORTA
#define UART_NUM (1)
// RX PTA18
#define PIN_UART_RX_GPIO PTA

// UART RX PTA18
#define PIN_UART_RX_PORT PORTA
#define PIN_UART_RX_BIT (18)
#define PIN_UART_RX (1<<PIN_UART_RX_BIT)
#define PIN_UART_RX_MUX_ALT (3)
// TX PTA19
#define PIN_UART_TX_GPIO PTA
#define PIN_UART_RX_MUX_ALT (kPORT_MuxAlt3)
// UART TX PTA19
#define PIN_UART_TX_PORT PORTA
#define PIN_UART_TX_BIT (19)
#define PIN_UART_TX (1<<PIN_UART_TX_BIT)
#define PIN_UART_TX_MUX_ALT (3)
#define PIN_UART_TX_MUX_ALT (kPORT_MuxAlt3)

#define UART LPUART1
#define UART_RX_TX_IRQn LPUART1_IRQn
#define UART_RX_TX_IRQHandler LPUART1_IRQHandler
// cmsis-driver/uart/uart.c configuration
#include "fsl_lpuart_cmsis.h"
#define CMSIS_UART_INSTANCE (Driver_USART1)
#define CMSIS_UART_IRQ (LPUART1_IRQn)

#endif
10 changes: 5 additions & 5 deletions source/board/microbitv2/kl27z/power.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ static void power_pre_switch_hook(smc_power_state_t originPowerState, app_power_
// and consider waiting for the write buffer to empty too, but as this is our only call to uart_uninitialize()
// and we never get here with USB connected it may be unnecessary.
/* Wait for debug console output finished. */
while (!(LPUART_STAT_TC_MASK & UART->STAT))
while (!(LPUART_STAT_TC_MASK & LPUART1->STAT))
{
}
uart_uninitialize();

/* Disable pins to lower current leakage */
PORT_SetPinMux(UART_PORT, PIN_UART_RX_BIT, kPORT_PinDisabledOrAnalog);
PORT_SetPinMux(UART_PORT, PIN_UART_TX_BIT, kPORT_PinDisabledOrAnalog);
PORT_SetPinMux(PIN_UART_RX_PORT, PIN_UART_RX_BIT, kPORT_PinDisabledOrAnalog);
PORT_SetPinMux(PIN_UART_TX_PORT, PIN_UART_TX_BIT, kPORT_PinDisabledOrAnalog);
PORT_SetPinMux(PIN_HID_LED_PORT, PIN_HID_LED_BIT, kPORT_PinDisabledOrAnalog);

/* Disable I/O pin SWCLK */
Expand Down Expand Up @@ -254,8 +254,8 @@ static void power_post_switch_hook(smc_power_state_t originPowerState, app_power
PORT_PCR_PS_MASK; /* Pull-up */

/* re-configure pinmux of disabled pins */
PORT_SetPinMux(UART_PORT, PIN_UART_RX_BIT, (port_mux_t)PIN_UART_RX_MUX_ALT);
PORT_SetPinMux(UART_PORT, PIN_UART_TX_BIT, (port_mux_t)PIN_UART_TX_MUX_ALT);
PORT_SetPinMux(PIN_UART_RX_PORT, PIN_UART_RX_BIT, PIN_UART_RX_MUX_ALT);
PORT_SetPinMux(PIN_UART_TX_PORT, PIN_UART_TX_BIT, PIN_UART_TX_MUX_ALT);

uart_initialize();
// TODO: Check if this is necessary, when we have time to test. This has always been in the V2 code.
Expand Down
5 changes: 5 additions & 0 deletions source/board/override_nm180410/IO_Config_Override.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ COMPILER_ASSERT((DAPLINK_HIC_ID == DAPLINK_HIC_ID_LPC55XX) ||
#define PIN_UART_TX (30U)
#define PIN_UART_TX_MASK (1U << PIN_UART_TX)

// cmsis-driver/uart/uart.c configuration
#include "fsl_usart_cmsis.h"
#define CMSIS_UART_INSTANCE (Driver_USART0)
#define CMSIS_UART_IRQ (FLEXCOMM0_IRQn)

// LED_A: LED_CDC PIO1_13
// LED_B: LED_MSC PIO1_20
// LED_C: LED_WEBHID PIO1_9
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file uart.c
* @brief
* @brief UART Function for HIC using CMSIS-Driver
*
* DAPLink Interface Firmware
* Copyright (c) 2020 Arm Limited, All Rights Reserved
Expand All @@ -20,18 +20,13 @@
*/

#include "string.h"
#include "fsl_device_registers.h"
#include "fsl_usart_cmsis.h"
#include "uart.h"
#include "util.h"
#include "cortex_m.h"
#include "circ_buf.h"
#include "settings.h" // for config_get_overflow_detect

#define USART_INSTANCE (Driver_USART0)
#define USART_IRQ (FLEXCOMM0_IRQn)

extern uint32_t SystemCoreClock;
#include "Driver_USART.h"
#include "IO_Config.h"

static void clear_buffers(void);

Expand All @@ -43,6 +38,9 @@ circ_buf_t write_buffer;
uint8_t write_buffer_data[BUFFER_SIZE];
circ_buf_t read_buffer;
uint8_t read_buffer_data[BUFFER_SIZE];
uint16_t cur_line_state;
uint32_t cur_control;
uint32_t cur_baud;

struct {
// Number of bytes pending to be transferred. This is 0 if there is no
Expand All @@ -64,18 +62,21 @@ int32_t uart_initialize(void)
{
clear_buffers();
cb_buf.tx_size = 0;
USART_INSTANCE.Initialize(uart_handler);
USART_INSTANCE.PowerControl(ARM_POWER_FULL);
CMSIS_UART_INSTANCE.Initialize(uart_handler);
CMSIS_UART_INSTANCE.PowerControl(ARM_POWER_FULL);
cur_line_state = 0;
cur_control = 0;
cur_baud = 0;

return 1;
}

int32_t uart_uninitialize(void)
{
USART_INSTANCE.Control(ARM_USART_CONTROL_RX, 0);
USART_INSTANCE.Control(ARM_USART_ABORT_RECEIVE, 0U);
USART_INSTANCE.PowerControl(ARM_POWER_OFF);
USART_INSTANCE.Uninitialize();
CMSIS_UART_INSTANCE.Control(ARM_USART_CONTROL_RX, 0);
CMSIS_UART_INSTANCE.Control(ARM_USART_ABORT_RECEIVE, 0U);
CMSIS_UART_INSTANCE.PowerControl(ARM_POWER_OFF);
CMSIS_UART_INSTANCE.Uninitialize();
clear_buffers();
cb_buf.tx_size = 0;

Expand All @@ -85,14 +86,14 @@ int32_t uart_uninitialize(void)
int32_t uart_reset(void)
{
// disable interrupt
NVIC_DisableIRQ(USART_IRQ);
NVIC_DisableIRQ(CMSIS_UART_IRQ);
clear_buffers();
if (cb_buf.tx_size != 0) {
USART_INSTANCE.Control(ARM_USART_ABORT_SEND, 0U);
CMSIS_UART_INSTANCE.Control(ARM_USART_ABORT_SEND, 0U);
cb_buf.tx_size = 0;
}
// enable interrupt
NVIC_EnableIRQ(USART_IRQ);
NVIC_EnableIRQ(CMSIS_UART_IRQ);

return 1;
}
Expand Down Expand Up @@ -161,27 +162,35 @@ int32_t uart_set_configuration(UART_Configuration *config)
break;
}

NVIC_DisableIRQ(USART_IRQ);
if ((control == cur_control) && (config->Baudrate == cur_baud)) {
return 1;
}
cur_control = control;
cur_baud = config->Baudrate;

NVIC_DisableIRQ(CMSIS_UART_IRQ);
clear_buffers();
if (cb_buf.tx_size != 0) {
USART_INSTANCE.Control(ARM_USART_ABORT_SEND, 0U);
CMSIS_UART_INSTANCE.Control(ARM_USART_ABORT_SEND, 0U);
cb_buf.tx_size = 0;
}

// If there was no Receive() call in progress aborting it is harmless.
USART_INSTANCE.Control(ARM_USART_CONTROL_RX, 0U);
USART_INSTANCE.Control(ARM_USART_ABORT_RECEIVE, 0U);
CMSIS_UART_INSTANCE.Control(ARM_USART_CONTROL_RX, 0U);
CMSIS_UART_INSTANCE.Control(ARM_USART_ABORT_RECEIVE, 0U);

uint32_t r = USART_INSTANCE.Control(control, config->Baudrate);
uint32_t r = CMSIS_UART_INSTANCE.Control(control, config->Baudrate);
if (r != ARM_DRIVER_OK) {
return 0;
}
USART_INSTANCE.Control(ARM_USART_CONTROL_TX, 1);
USART_INSTANCE.Control(ARM_USART_CONTROL_RX, 1);
USART_INSTANCE.Receive(&(cb_buf.rx), 1);
CMSIS_UART_INSTANCE.Control(ARM_USART_CONTROL_TX, 1);
CMSIS_UART_INSTANCE.Control(ARM_USART_CONTROL_RX, 1);
CMSIS_UART_INSTANCE.Receive(&(cb_buf.rx), 1);

NVIC_ClearPendingIRQ(CMSIS_UART_IRQ);
NVIC_EnableIRQ(CMSIS_UART_IRQ);

NVIC_ClearPendingIRQ(USART_IRQ);
NVIC_EnableIRQ(USART_IRQ);
uart_reset();

return 1;
}
Expand All @@ -193,6 +202,10 @@ int32_t uart_get_configuration(UART_Configuration *config)

void uart_set_control_line_state(uint16_t ctrl_bmp)
{
if (ctrl_bmp != cur_line_state) {
uart_reset();
cur_line_state = ctrl_bmp;
}
}

int32_t uart_write_free(void)
Expand All @@ -215,7 +228,7 @@ static void uart_start_tx_transfer() {
}
cb_buf.tx_size = tx_size;
if (tx_size) {
USART_INSTANCE.Send(buf, tx_size);
CMSIS_UART_INSTANCE.Send(buf, tx_size);
}
}

Expand Down Expand Up @@ -254,7 +267,7 @@ void uart_handler(uint32_t event) {
} else {
// Drop character
}
USART_INSTANCE.Receive(&(cb_buf.rx), 1);
CMSIS_UART_INSTANCE.Receive(&(cb_buf.rx), 1);
}

if (event & ARM_USART_EVENT_SEND_COMPLETE) {
Expand Down
18 changes: 9 additions & 9 deletions source/hic_hal/freescale/kl27z/IO_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_KL27Z);
#define SW_RESET_NOT_PRESSED (1)

// UART
#define UART_PORT PORTA
#define UART_NUM (1)
// RX PTA18
#define PIN_UART_RX_GPIO PTA

// UART RX PTA18
#define PIN_UART_RX_PORT PORTA
#define PIN_UART_RX_BIT (18)
#define PIN_UART_RX (1<<PIN_UART_RX_BIT)
#define PIN_UART_RX_MUX_ALT (3)
// TX PTA19
#define PIN_UART_TX_GPIO PTA
// UART TX PTA19
#define PIN_UART_TX_PORT PORTA
#define PIN_UART_TX_BIT (19)
#define PIN_UART_TX (1<<PIN_UART_TX_BIT)
#define PIN_UART_TX_MUX_ALT (3)

#define UART LPUART1
#define UART_RX_TX_IRQn LPUART1_IRQn
#define UART_RX_TX_IRQHandler LPUART1_IRQHandler
// cmsis-driver/uart/uart.c configuration
#include "fsl_lpuart_cmsis.h"
#define CMSIS_UART_INSTANCE (Driver_USART1)
#define CMSIS_UART_IRQ (LPUART1_IRQn)

#endif
Loading
Loading