Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.

Upstream: https://github.com/ARMmbed/DAPLink

Committer:
Pawel Zarembski
Date:
Tue Apr 07 12:55:42 2020 +0200
Revision:
0:01f31e923fe2
hani: DAPLink with reset workaround

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Pawel Zarembski 0:01f31e923fe2 1 /**
Pawel Zarembski 0:01f31e923fe2 2 * @file usbd_user_cdc_acm.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief implementation of USB CDC ACM middleware
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2019, ARM Limited, All Rights Reserved
Pawel Zarembski 0:01f31e923fe2 7 * SPDX-License-Identifier: Apache-2.0
Pawel Zarembski 0:01f31e923fe2 8 *
Pawel Zarembski 0:01f31e923fe2 9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Pawel Zarembski 0:01f31e923fe2 10 * not use this file except in compliance with the License.
Pawel Zarembski 0:01f31e923fe2 11 * You may obtain a copy of the License at
Pawel Zarembski 0:01f31e923fe2 12 *
Pawel Zarembski 0:01f31e923fe2 13 * http://www.apache.org/licenses/LICENSE-2.0
Pawel Zarembski 0:01f31e923fe2 14 *
Pawel Zarembski 0:01f31e923fe2 15 * Unless required by applicable law or agreed to in writing, software
Pawel Zarembski 0:01f31e923fe2 16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Pawel Zarembski 0:01f31e923fe2 17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Pawel Zarembski 0:01f31e923fe2 18 * See the License for the specific language governing permissions and
Pawel Zarembski 0:01f31e923fe2 19 * limitations under the License.
Pawel Zarembski 0:01f31e923fe2 20 */
Pawel Zarembski 0:01f31e923fe2 21
Pawel Zarembski 0:01f31e923fe2 22 #include "cmsis_os2.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "rl_usb.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "main.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "uart.h"
Pawel Zarembski 0:01f31e923fe2 26 #ifdef DRAG_N_DROP_SUPPORT
Pawel Zarembski 0:01f31e923fe2 27 #include "flash_intf.h"
Pawel Zarembski 0:01f31e923fe2 28 #endif
Pawel Zarembski 0:01f31e923fe2 29 #include "target_family.h"
Pawel Zarembski 0:01f31e923fe2 30
Pawel Zarembski 0:01f31e923fe2 31 UART_Configuration UART_Config;
Pawel Zarembski 0:01f31e923fe2 32
Pawel Zarembski 0:01f31e923fe2 33 /** @brief Vitual COM Port initialization
Pawel Zarembski 0:01f31e923fe2 34 *
Pawel Zarembski 0:01f31e923fe2 35 * The function inititalizes the hardware resources of the port used as
Pawel Zarembski 0:01f31e923fe2 36 * the Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 37 *
Pawel Zarembski 0:01f31e923fe2 38 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 39 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 40 */
Pawel Zarembski 0:01f31e923fe2 41 int32_t USBD_CDC_ACM_PortInitialize(void)
Pawel Zarembski 0:01f31e923fe2 42 {
Pawel Zarembski 0:01f31e923fe2 43 uart_initialize();
Pawel Zarembski 0:01f31e923fe2 44 main_cdc_send_event();
Pawel Zarembski 0:01f31e923fe2 45 return 1;
Pawel Zarembski 0:01f31e923fe2 46 }
Pawel Zarembski 0:01f31e923fe2 47
Pawel Zarembski 0:01f31e923fe2 48 /** @brief Vitual COM Port uninitialization
Pawel Zarembski 0:01f31e923fe2 49 *
Pawel Zarembski 0:01f31e923fe2 50 * The function uninititalizes/releases the hardware resources of the port used
Pawel Zarembski 0:01f31e923fe2 51 * as the Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 52 *
Pawel Zarembski 0:01f31e923fe2 53 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 54 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 55 */
Pawel Zarembski 0:01f31e923fe2 56 int32_t USBD_CDC_ACM_PortUninitialize(void)
Pawel Zarembski 0:01f31e923fe2 57 {
Pawel Zarembski 0:01f31e923fe2 58 uart_uninitialize();
Pawel Zarembski 0:01f31e923fe2 59 return 1;
Pawel Zarembski 0:01f31e923fe2 60 }
Pawel Zarembski 0:01f31e923fe2 61
Pawel Zarembski 0:01f31e923fe2 62 /** @brief Vitual COM Port reset
Pawel Zarembski 0:01f31e923fe2 63 *
Pawel Zarembski 0:01f31e923fe2 64 * The function resets the internal states of the port used
Pawel Zarembski 0:01f31e923fe2 65 * as the Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 66 *
Pawel Zarembski 0:01f31e923fe2 67 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 68 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 69 */
Pawel Zarembski 0:01f31e923fe2 70 int32_t USBD_CDC_ACM_PortReset(void)
Pawel Zarembski 0:01f31e923fe2 71 {
Pawel Zarembski 0:01f31e923fe2 72 uart_reset();
Pawel Zarembski 0:01f31e923fe2 73 return 1;
Pawel Zarembski 0:01f31e923fe2 74 }
Pawel Zarembski 0:01f31e923fe2 75
Pawel Zarembski 0:01f31e923fe2 76 /** @brief Virtual COM Port change communication settings
Pawel Zarembski 0:01f31e923fe2 77 *
Pawel Zarembski 0:01f31e923fe2 78 * The function changes communication settings of the port used as the
Pawel Zarembski 0:01f31e923fe2 79 * Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 80 *
Pawel Zarembski 0:01f31e923fe2 81 * @param [in] line_coding Pointer to the loaded CDC_LINE_CODING structure.
Pawel Zarembski 0:01f31e923fe2 82 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 83 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 84 */
Pawel Zarembski 0:01f31e923fe2 85 int32_t USBD_CDC_ACM_PortSetLineCoding(CDC_LINE_CODING *line_coding)
Pawel Zarembski 0:01f31e923fe2 86 {
Pawel Zarembski 0:01f31e923fe2 87 UART_Config.Baudrate = line_coding->dwDTERate;
Pawel Zarembski 0:01f31e923fe2 88 UART_Config.DataBits = (UART_DataBits) line_coding->bDataBits;
Pawel Zarembski 0:01f31e923fe2 89 UART_Config.Parity = (UART_Parity) line_coding->bParityType;
Pawel Zarembski 0:01f31e923fe2 90 UART_Config.StopBits = (UART_StopBits) line_coding->bCharFormat;
Pawel Zarembski 0:01f31e923fe2 91 UART_Config.FlowControl = UART_FLOW_CONTROL_NONE;
Pawel Zarembski 0:01f31e923fe2 92 return uart_set_configuration(&UART_Config);
Pawel Zarembski 0:01f31e923fe2 93 }
Pawel Zarembski 0:01f31e923fe2 94
Pawel Zarembski 0:01f31e923fe2 95 /** @brief Vitual COM Port retrieve communication settings
Pawel Zarembski 0:01f31e923fe2 96 *
Pawel Zarembski 0:01f31e923fe2 97 * The function retrieves communication settings of the port used as the
Pawel Zarembski 0:01f31e923fe2 98 * Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 99 *
Pawel Zarembski 0:01f31e923fe2 100 * @param [in] line_coding Pointer to the CDC_LINE_CODING structure.
Pawel Zarembski 0:01f31e923fe2 101 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 102 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 103 */
Pawel Zarembski 0:01f31e923fe2 104 int32_t USBD_CDC_ACM_PortGetLineCoding(CDC_LINE_CODING *line_coding)
Pawel Zarembski 0:01f31e923fe2 105 {
Pawel Zarembski 0:01f31e923fe2 106 line_coding->dwDTERate = UART_Config.Baudrate;
Pawel Zarembski 0:01f31e923fe2 107 line_coding->bDataBits = UART_Config.DataBits;
Pawel Zarembski 0:01f31e923fe2 108 line_coding->bParityType = UART_Config.Parity;
Pawel Zarembski 0:01f31e923fe2 109 line_coding->bCharFormat = UART_Config.StopBits;
Pawel Zarembski 0:01f31e923fe2 110 return (1);
Pawel Zarembski 0:01f31e923fe2 111 }
Pawel Zarembski 0:01f31e923fe2 112
Pawel Zarembski 0:01f31e923fe2 113 static U32 start_break_time = 0;
Pawel Zarembski 0:01f31e923fe2 114 int32_t USBD_CDC_ACM_SendBreak(uint16_t dur)
Pawel Zarembski 0:01f31e923fe2 115 {
Pawel Zarembski 0:01f31e923fe2 116 uint32_t end_break_time;
Pawel Zarembski 0:01f31e923fe2 117 #ifdef DRAG_N_DROP_SUPPORT
Pawel Zarembski 0:01f31e923fe2 118 if (!flash_intf_target->flash_busy())
Pawel Zarembski 0:01f31e923fe2 119 #endif
Pawel Zarembski 0:01f31e923fe2 120 { //added checking if flashing on target is in progress
Pawel Zarembski 0:01f31e923fe2 121 // reset and send the unique id over CDC
Pawel Zarembski 0:01f31e923fe2 122 if (dur != 0) {
Pawel Zarembski 0:01f31e923fe2 123 start_break_time = osKernelGetSysTimerCount();
Pawel Zarembski 0:01f31e923fe2 124 target_set_state(RESET_HOLD);
Pawel Zarembski 0:01f31e923fe2 125 } else {
Pawel Zarembski 0:01f31e923fe2 126 end_break_time = osKernelGetSysTimerCount();
Pawel Zarembski 0:01f31e923fe2 127
Pawel Zarembski 0:01f31e923fe2 128 // long reset -> send uID over serial (300 -> break > 3s)
Pawel Zarembski 0:01f31e923fe2 129 if ((end_break_time - start_break_time) >= (300)) {
Pawel Zarembski 0:01f31e923fe2 130 main_reset_target(1);
Pawel Zarembski 0:01f31e923fe2 131 } else {
Pawel Zarembski 0:01f31e923fe2 132 main_reset_target(0);
Pawel Zarembski 0:01f31e923fe2 133 }
Pawel Zarembski 0:01f31e923fe2 134 }
Pawel Zarembski 0:01f31e923fe2 135 }
Pawel Zarembski 0:01f31e923fe2 136 return (1);
Pawel Zarembski 0:01f31e923fe2 137 }
Pawel Zarembski 0:01f31e923fe2 138
Pawel Zarembski 0:01f31e923fe2 139 /** @brief Virtual COM Port set control line state
Pawel Zarembski 0:01f31e923fe2 140 *
Pawel Zarembski 0:01f31e923fe2 141 * The function sets control line state on the port used as the
Pawel Zarembski 0:01f31e923fe2 142 * Virtual COM Port.
Pawel Zarembski 0:01f31e923fe2 143 *
Pawel Zarembski 0:01f31e923fe2 144 * @param [in] ctrl_bmp Control line settings
Pawel Zarembski 0:01f31e923fe2 145 * bitmap (0. bit - DTR state, 1. bit - RTS state).
Pawel Zarembski 0:01f31e923fe2 146 * @return 0 Function failed.
Pawel Zarembski 0:01f31e923fe2 147 * @return 1 Function succeeded.
Pawel Zarembski 0:01f31e923fe2 148 */
Pawel Zarembski 0:01f31e923fe2 149 int32_t USBD_CDC_ACM_PortSetControlLineState(uint16_t ctrl_bmp)
Pawel Zarembski 0:01f31e923fe2 150 {
Pawel Zarembski 0:01f31e923fe2 151 return (1);
Pawel Zarembski 0:01f31e923fe2 152 }
Pawel Zarembski 0:01f31e923fe2 153
Pawel Zarembski 0:01f31e923fe2 154 void cdc_process_event()
Pawel Zarembski 0:01f31e923fe2 155 {
Pawel Zarembski 0:01f31e923fe2 156 int32_t len_data = 0;
Pawel Zarembski 0:01f31e923fe2 157 uint8_t data[64];
Pawel Zarembski 0:01f31e923fe2 158
Pawel Zarembski 0:01f31e923fe2 159 len_data = USBD_CDC_ACM_DataFree();
Pawel Zarembski 0:01f31e923fe2 160
Pawel Zarembski 0:01f31e923fe2 161 if (len_data > sizeof(data)) {
Pawel Zarembski 0:01f31e923fe2 162 len_data = sizeof(data);
Pawel Zarembski 0:01f31e923fe2 163 }
Pawel Zarembski 0:01f31e923fe2 164
Pawel Zarembski 0:01f31e923fe2 165 if (len_data) {
Pawel Zarembski 0:01f31e923fe2 166 len_data = uart_read_data(data, len_data);
Pawel Zarembski 0:01f31e923fe2 167 }
Pawel Zarembski 0:01f31e923fe2 168
Pawel Zarembski 0:01f31e923fe2 169 if (len_data) {
Pawel Zarembski 0:01f31e923fe2 170 if (USBD_CDC_ACM_DataSend(data , len_data)) {
Pawel Zarembski 0:01f31e923fe2 171 main_blink_cdc_led(MAIN_LED_FLASH);
Pawel Zarembski 0:01f31e923fe2 172 }
Pawel Zarembski 0:01f31e923fe2 173 }
Pawel Zarembski 0:01f31e923fe2 174
Pawel Zarembski 0:01f31e923fe2 175 len_data = uart_write_free();
Pawel Zarembski 0:01f31e923fe2 176
Pawel Zarembski 0:01f31e923fe2 177 if (len_data > sizeof(data)) {
Pawel Zarembski 0:01f31e923fe2 178 len_data = sizeof(data);
Pawel Zarembski 0:01f31e923fe2 179 }
Pawel Zarembski 0:01f31e923fe2 180
Pawel Zarembski 0:01f31e923fe2 181 if (len_data) {
Pawel Zarembski 0:01f31e923fe2 182 len_data = USBD_CDC_ACM_DataRead(data, len_data);
Pawel Zarembski 0:01f31e923fe2 183 }
Pawel Zarembski 0:01f31e923fe2 184
Pawel Zarembski 0:01f31e923fe2 185 if (len_data) {
Pawel Zarembski 0:01f31e923fe2 186 if (uart_write_data(data, len_data)) {
Pawel Zarembski 0:01f31e923fe2 187 main_blink_cdc_led(MAIN_LED_FLASH);
Pawel Zarembski 0:01f31e923fe2 188 }
Pawel Zarembski 0:01f31e923fe2 189 }
Pawel Zarembski 0:01f31e923fe2 190
Pawel Zarembski 0:01f31e923fe2 191 // Always process events
Pawel Zarembski 0:01f31e923fe2 192 main_cdc_send_event();
Pawel Zarembski 0:01f31e923fe2 193 }