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 rt_Mailbox.c
Pawel Zarembski 0:01f31e923fe2 3 * @brief
Pawel Zarembski 0:01f31e923fe2 4 *
Pawel Zarembski 0:01f31e923fe2 5 * DAPLink Interface Firmware
Pawel Zarembski 0:01f31e923fe2 6 * Copyright (c) 2009-2016, 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 "rt_TypeDef.h"
Pawel Zarembski 0:01f31e923fe2 23 #include "RTX_Config.h"
Pawel Zarembski 0:01f31e923fe2 24 #include "rt_System.h"
Pawel Zarembski 0:01f31e923fe2 25 #include "rt_List.h"
Pawel Zarembski 0:01f31e923fe2 26 #include "rt_Mailbox.h"
Pawel Zarembski 0:01f31e923fe2 27 #include "rt_Task.h"
Pawel Zarembski 0:01f31e923fe2 28 #include "rt_HAL_CM.h"
Pawel Zarembski 0:01f31e923fe2 29
Pawel Zarembski 0:01f31e923fe2 30
Pawel Zarembski 0:01f31e923fe2 31 /*----------------------------------------------------------------------------
Pawel Zarembski 0:01f31e923fe2 32 * Functions
Pawel Zarembski 0:01f31e923fe2 33 *---------------------------------------------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 34
Pawel Zarembski 0:01f31e923fe2 35
Pawel Zarembski 0:01f31e923fe2 36 /*--------------------------- rt_mbx_init -----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 37
Pawel Zarembski 0:01f31e923fe2 38 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
Pawel Zarembski 0:01f31e923fe2 39 /* Initialize a mailbox */
Pawel Zarembski 0:01f31e923fe2 40 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 41
Pawel Zarembski 0:01f31e923fe2 42 p_MCB->cb_type = MCB;
Pawel Zarembski 0:01f31e923fe2 43 p_MCB->isr_st = 0;
Pawel Zarembski 0:01f31e923fe2 44 p_MCB->p_lnk = NULL;
Pawel Zarembski 0:01f31e923fe2 45 p_MCB->first = 0;
Pawel Zarembski 0:01f31e923fe2 46 p_MCB->last = 0;
Pawel Zarembski 0:01f31e923fe2 47 p_MCB->count = 0;
Pawel Zarembski 0:01f31e923fe2 48 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
Pawel Zarembski 0:01f31e923fe2 49 (U32)sizeof (void *);
Pawel Zarembski 0:01f31e923fe2 50 }
Pawel Zarembski 0:01f31e923fe2 51
Pawel Zarembski 0:01f31e923fe2 52
Pawel Zarembski 0:01f31e923fe2 53 /*--------------------------- rt_mbx_send -----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 54
Pawel Zarembski 0:01f31e923fe2 55 OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) {
Pawel Zarembski 0:01f31e923fe2 56 /* Send message to a mailbox */
Pawel Zarembski 0:01f31e923fe2 57 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 58 P_TCB p_TCB;
Pawel Zarembski 0:01f31e923fe2 59
Pawel Zarembski 0:01f31e923fe2 60 if (p_MCB->p_lnk != NULL && p_MCB->count == 0) {
Pawel Zarembski 0:01f31e923fe2 61 /* A task is waiting for message */
Pawel Zarembski 0:01f31e923fe2 62 p_TCB = rt_get_first ((P_XCB)p_MCB);
Pawel Zarembski 0:01f31e923fe2 63 *p_TCB->msg = p_msg;
Pawel Zarembski 0:01f31e923fe2 64 p_TCB->ret_val = OS_R_MBX;
Pawel Zarembski 0:01f31e923fe2 65 rt_rmv_dly (p_TCB);
Pawel Zarembski 0:01f31e923fe2 66 rt_dispatch (p_TCB);
Pawel Zarembski 0:01f31e923fe2 67 os_tsk.run->ret_val = OS_R_OK;
Pawel Zarembski 0:01f31e923fe2 68 }
Pawel Zarembski 0:01f31e923fe2 69 else {
Pawel Zarembski 0:01f31e923fe2 70 /* Store message in mailbox queue */
Pawel Zarembski 0:01f31e923fe2 71 if (p_MCB->count == p_MCB->size) {
Pawel Zarembski 0:01f31e923fe2 72 /* No free message entry, wait for one. If message queue is full, */
Pawel Zarembski 0:01f31e923fe2 73 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
Pawel Zarembski 0:01f31e923fe2 74 /* pointer can now be reused for send message waits task list. */
Pawel Zarembski 0:01f31e923fe2 75 if (timeout == 0) {
Pawel Zarembski 0:01f31e923fe2 76 return (OS_R_TMO);
Pawel Zarembski 0:01f31e923fe2 77 }
Pawel Zarembski 0:01f31e923fe2 78 if (p_MCB->p_lnk != NULL) {
Pawel Zarembski 0:01f31e923fe2 79 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
Pawel Zarembski 0:01f31e923fe2 80 }
Pawel Zarembski 0:01f31e923fe2 81 else {
Pawel Zarembski 0:01f31e923fe2 82 p_MCB->p_lnk = os_tsk.run;
Pawel Zarembski 0:01f31e923fe2 83 os_tsk.run->p_lnk = NULL;
Pawel Zarembski 0:01f31e923fe2 84 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
Pawel Zarembski 0:01f31e923fe2 85 /* Signal the 'isr_mbx_receive ()' that the task is waiting */
Pawel Zarembski 0:01f31e923fe2 86 /* to send a message */
Pawel Zarembski 0:01f31e923fe2 87 p_MCB->isr_st = 1;
Pawel Zarembski 0:01f31e923fe2 88 }
Pawel Zarembski 0:01f31e923fe2 89 os_tsk.run->msg = p_msg;
Pawel Zarembski 0:01f31e923fe2 90 rt_block (timeout, WAIT_MBX);
Pawel Zarembski 0:01f31e923fe2 91 return (OS_R_TMO);
Pawel Zarembski 0:01f31e923fe2 92 }
Pawel Zarembski 0:01f31e923fe2 93 /* Yes, there is a free entry in a mailbox. */
Pawel Zarembski 0:01f31e923fe2 94 p_MCB->msg[p_MCB->first] = p_msg;
Pawel Zarembski 0:01f31e923fe2 95 rt_inc (&p_MCB->count);
Pawel Zarembski 0:01f31e923fe2 96 if (++p_MCB->first == p_MCB->size) {
Pawel Zarembski 0:01f31e923fe2 97 p_MCB->first = 0;
Pawel Zarembski 0:01f31e923fe2 98 }
Pawel Zarembski 0:01f31e923fe2 99 }
Pawel Zarembski 0:01f31e923fe2 100 return (OS_R_OK);
Pawel Zarembski 0:01f31e923fe2 101 }
Pawel Zarembski 0:01f31e923fe2 102
Pawel Zarembski 0:01f31e923fe2 103
Pawel Zarembski 0:01f31e923fe2 104 /*--------------------------- rt_mbx_wait -----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 105
Pawel Zarembski 0:01f31e923fe2 106 OS_RESULT rt_mbx_wait (OS_ID mailbox, void **message, U16 timeout) {
Pawel Zarembski 0:01f31e923fe2 107 /* Receive a message; possibly wait for it */
Pawel Zarembski 0:01f31e923fe2 108 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 109 P_TCB p_TCB;
Pawel Zarembski 0:01f31e923fe2 110
Pawel Zarembski 0:01f31e923fe2 111 /* If a message is available in the fifo buffer */
Pawel Zarembski 0:01f31e923fe2 112 /* remove it from the fifo buffer and return. */
Pawel Zarembski 0:01f31e923fe2 113 if (p_MCB->count) {
Pawel Zarembski 0:01f31e923fe2 114 *message = p_MCB->msg[p_MCB->last];
Pawel Zarembski 0:01f31e923fe2 115 if (++p_MCB->last == p_MCB->size) {
Pawel Zarembski 0:01f31e923fe2 116 p_MCB->last = 0;
Pawel Zarembski 0:01f31e923fe2 117 }
Pawel Zarembski 0:01f31e923fe2 118 if (p_MCB->p_lnk != NULL) {
Pawel Zarembski 0:01f31e923fe2 119 /* A task is waiting to send message */
Pawel Zarembski 0:01f31e923fe2 120 p_TCB = rt_get_first ((P_XCB)p_MCB);
Pawel Zarembski 0:01f31e923fe2 121 p_TCB->ret_val = OS_R_OK;
Pawel Zarembski 0:01f31e923fe2 122 p_MCB->msg[p_MCB->first] = p_TCB->msg;
Pawel Zarembski 0:01f31e923fe2 123 if (++p_MCB->first == p_MCB->size) {
Pawel Zarembski 0:01f31e923fe2 124 p_MCB->first = 0;
Pawel Zarembski 0:01f31e923fe2 125 }
Pawel Zarembski 0:01f31e923fe2 126 rt_rmv_dly (p_TCB);
Pawel Zarembski 0:01f31e923fe2 127 rt_dispatch (p_TCB);
Pawel Zarembski 0:01f31e923fe2 128 os_tsk.run->ret_val = OS_R_OK;
Pawel Zarembski 0:01f31e923fe2 129 }
Pawel Zarembski 0:01f31e923fe2 130 else {
Pawel Zarembski 0:01f31e923fe2 131 rt_dec (&p_MCB->count);
Pawel Zarembski 0:01f31e923fe2 132 }
Pawel Zarembski 0:01f31e923fe2 133 return (OS_R_OK);
Pawel Zarembski 0:01f31e923fe2 134 }
Pawel Zarembski 0:01f31e923fe2 135 /* No message available: wait for one */
Pawel Zarembski 0:01f31e923fe2 136 if (timeout == 0) {
Pawel Zarembski 0:01f31e923fe2 137 return (OS_R_TMO);
Pawel Zarembski 0:01f31e923fe2 138 }
Pawel Zarembski 0:01f31e923fe2 139 if (p_MCB->p_lnk != NULL) {
Pawel Zarembski 0:01f31e923fe2 140 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
Pawel Zarembski 0:01f31e923fe2 141 }
Pawel Zarembski 0:01f31e923fe2 142 else {
Pawel Zarembski 0:01f31e923fe2 143 p_MCB->p_lnk = os_tsk.run;
Pawel Zarembski 0:01f31e923fe2 144 os_tsk.run->p_lnk = NULL;
Pawel Zarembski 0:01f31e923fe2 145 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
Pawel Zarembski 0:01f31e923fe2 146 }
Pawel Zarembski 0:01f31e923fe2 147 rt_block(timeout, WAIT_MBX);
Pawel Zarembski 0:01f31e923fe2 148 os_tsk.run->msg = message;
Pawel Zarembski 0:01f31e923fe2 149 return (OS_R_TMO);
Pawel Zarembski 0:01f31e923fe2 150 }
Pawel Zarembski 0:01f31e923fe2 151
Pawel Zarembski 0:01f31e923fe2 152
Pawel Zarembski 0:01f31e923fe2 153 /*--------------------------- rt_mbx_check ----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 154
Pawel Zarembski 0:01f31e923fe2 155 OS_RESULT rt_mbx_check (OS_ID mailbox) {
Pawel Zarembski 0:01f31e923fe2 156 /* Check for free space in a mailbox. Returns the number of messages */
Pawel Zarembski 0:01f31e923fe2 157 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
Pawel Zarembski 0:01f31e923fe2 158 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 159
Pawel Zarembski 0:01f31e923fe2 160 return (p_MCB->size - p_MCB->count);
Pawel Zarembski 0:01f31e923fe2 161 }
Pawel Zarembski 0:01f31e923fe2 162
Pawel Zarembski 0:01f31e923fe2 163
Pawel Zarembski 0:01f31e923fe2 164 /*--------------------------- isr_mbx_send ----------------------------------*/
Pawel Zarembski 0:01f31e923fe2 165
Pawel Zarembski 0:01f31e923fe2 166 void isr_mbx_send (OS_ID mailbox, void *p_msg) {
Pawel Zarembski 0:01f31e923fe2 167 /* Same function as "os_mbx_send", but to be called by ISRs. */
Pawel Zarembski 0:01f31e923fe2 168 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 169
Pawel Zarembski 0:01f31e923fe2 170 rt_psq_enq (p_MCB, (U32)p_msg);
Pawel Zarembski 0:01f31e923fe2 171 rt_psh_req ();
Pawel Zarembski 0:01f31e923fe2 172 }
Pawel Zarembski 0:01f31e923fe2 173
Pawel Zarembski 0:01f31e923fe2 174
Pawel Zarembski 0:01f31e923fe2 175 /*--------------------------- isr_mbx_receive -------------------------------*/
Pawel Zarembski 0:01f31e923fe2 176
Pawel Zarembski 0:01f31e923fe2 177 OS_RESULT isr_mbx_receive (OS_ID mailbox, void **message) {
Pawel Zarembski 0:01f31e923fe2 178 /* Receive a message in the interrupt function. The interrupt function */
Pawel Zarembski 0:01f31e923fe2 179 /* should not wait for a message since this would block the rtx os. */
Pawel Zarembski 0:01f31e923fe2 180 P_MCB p_MCB = mailbox;
Pawel Zarembski 0:01f31e923fe2 181
Pawel Zarembski 0:01f31e923fe2 182 if (p_MCB->count) {
Pawel Zarembski 0:01f31e923fe2 183 /* A message is available in the fifo buffer. */
Pawel Zarembski 0:01f31e923fe2 184 *message = p_MCB->msg[p_MCB->last];
Pawel Zarembski 0:01f31e923fe2 185 if (p_MCB->isr_st == 1) {
Pawel Zarembski 0:01f31e923fe2 186 /* A task is locked waiting to send message */
Pawel Zarembski 0:01f31e923fe2 187 p_MCB->isr_st = 2;
Pawel Zarembski 0:01f31e923fe2 188 rt_psq_enq (p_MCB, 0);
Pawel Zarembski 0:01f31e923fe2 189 rt_psh_req ();
Pawel Zarembski 0:01f31e923fe2 190 }
Pawel Zarembski 0:01f31e923fe2 191 rt_dec (&p_MCB->count);
Pawel Zarembski 0:01f31e923fe2 192 if (++p_MCB->last == p_MCB->size) {
Pawel Zarembski 0:01f31e923fe2 193 p_MCB->last = 0;
Pawel Zarembski 0:01f31e923fe2 194 }
Pawel Zarembski 0:01f31e923fe2 195 return (OS_R_MBX);
Pawel Zarembski 0:01f31e923fe2 196 }
Pawel Zarembski 0:01f31e923fe2 197 return (OS_R_OK);
Pawel Zarembski 0:01f31e923fe2 198 }
Pawel Zarembski 0:01f31e923fe2 199
Pawel Zarembski 0:01f31e923fe2 200
Pawel Zarembski 0:01f31e923fe2 201 /*--------------------------- rt_mbx_psh ------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 202
Pawel Zarembski 0:01f31e923fe2 203 void rt_mbx_psh (P_MCB p_CB, void *p_msg) {
Pawel Zarembski 0:01f31e923fe2 204 /* Store the message to the mailbox queue or pass it to task directly. */
Pawel Zarembski 0:01f31e923fe2 205 P_TCB p_TCB;
Pawel Zarembski 0:01f31e923fe2 206
Pawel Zarembski 0:01f31e923fe2 207 /* Check if this was an 'isr_mbx_receive ()' post service request. */
Pawel Zarembski 0:01f31e923fe2 208 if (p_CB->p_lnk != NULL && p_CB->isr_st == 2) {
Pawel Zarembski 0:01f31e923fe2 209 /* A task is waiting to send message, remove it from the waiting list. */
Pawel Zarembski 0:01f31e923fe2 210 p_CB->isr_st = 0;
Pawel Zarembski 0:01f31e923fe2 211 p_TCB = rt_get_first ((P_XCB)p_CB);
Pawel Zarembski 0:01f31e923fe2 212 p_TCB->ret_val = OS_R_OK;
Pawel Zarembski 0:01f31e923fe2 213 /* Store the message to the mailbox queue. */
Pawel Zarembski 0:01f31e923fe2 214 p_CB->msg[p_CB->first] = p_TCB->msg;
Pawel Zarembski 0:01f31e923fe2 215 rt_inc (&p_CB->count);
Pawel Zarembski 0:01f31e923fe2 216 if (++p_CB->first == p_CB->size) {
Pawel Zarembski 0:01f31e923fe2 217 p_CB->first = 0;
Pawel Zarembski 0:01f31e923fe2 218 }
Pawel Zarembski 0:01f31e923fe2 219 goto rdy;
Pawel Zarembski 0:01f31e923fe2 220 }
Pawel Zarembski 0:01f31e923fe2 221 /* A task is waiting for message, pass the message to task directly. */
Pawel Zarembski 0:01f31e923fe2 222 if (p_CB->p_lnk != NULL && p_CB->count == 0) {
Pawel Zarembski 0:01f31e923fe2 223 p_TCB = rt_get_first ((P_XCB)p_CB);
Pawel Zarembski 0:01f31e923fe2 224 *p_TCB->msg = p_msg;
Pawel Zarembski 0:01f31e923fe2 225 p_TCB->ret_val = OS_R_MBX;
Pawel Zarembski 0:01f31e923fe2 226 rdy:p_TCB->state = READY;
Pawel Zarembski 0:01f31e923fe2 227 rt_rmv_dly (p_TCB);
Pawel Zarembski 0:01f31e923fe2 228 rt_put_prio (&os_rdy, p_TCB);
Pawel Zarembski 0:01f31e923fe2 229 }
Pawel Zarembski 0:01f31e923fe2 230 else {
Pawel Zarembski 0:01f31e923fe2 231 /* No task is waiting for message, store the message to the mailbox queue.*/
Pawel Zarembski 0:01f31e923fe2 232 if (p_CB->count < p_CB->size) {
Pawel Zarembski 0:01f31e923fe2 233 p_CB->msg[p_CB->first] = p_msg;
Pawel Zarembski 0:01f31e923fe2 234 rt_inc (&p_CB->count);
Pawel Zarembski 0:01f31e923fe2 235 if (++p_CB->first == p_CB->size) {
Pawel Zarembski 0:01f31e923fe2 236 p_CB->first = 0;
Pawel Zarembski 0:01f31e923fe2 237 }
Pawel Zarembski 0:01f31e923fe2 238 }
Pawel Zarembski 0:01f31e923fe2 239 else {
Pawel Zarembski 0:01f31e923fe2 240 os_error (OS_ERR_MBX_OVF);
Pawel Zarembski 0:01f31e923fe2 241 }
Pawel Zarembski 0:01f31e923fe2 242 }
Pawel Zarembski 0:01f31e923fe2 243 }
Pawel Zarembski 0:01f31e923fe2 244
Pawel Zarembski 0:01f31e923fe2 245 /*----------------------------------------------------------------------------
Pawel Zarembski 0:01f31e923fe2 246 * end of file
Pawel Zarembski 0:01f31e923fe2 247 *---------------------------------------------------------------------------*/
Pawel Zarembski 0:01f31e923fe2 248