Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/daplink/circ_buf.c@0:01f31e923fe2, 2020-04-07 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file circular_buffer.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief Implementation of a circular buffer |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2016-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 "circ_buf.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "cortex_m.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "util.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | |
Pawel Zarembski |
0:01f31e923fe2 | 27 | void circ_buf_init(circ_buf_t *circ_buf, uint8_t *buffer, uint32_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 28 | { |
Pawel Zarembski |
0:01f31e923fe2 | 29 | cortex_int_state_t state; |
Pawel Zarembski |
0:01f31e923fe2 | 30 | state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 31 | |
Pawel Zarembski |
0:01f31e923fe2 | 32 | circ_buf->buf = buffer; |
Pawel Zarembski |
0:01f31e923fe2 | 33 | circ_buf->size = size; |
Pawel Zarembski |
0:01f31e923fe2 | 34 | circ_buf->head = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 35 | circ_buf->tail = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 36 | |
Pawel Zarembski |
0:01f31e923fe2 | 37 | cortex_int_restore(state); |
Pawel Zarembski |
0:01f31e923fe2 | 38 | } |
Pawel Zarembski |
0:01f31e923fe2 | 39 | |
Pawel Zarembski |
0:01f31e923fe2 | 40 | void circ_buf_push(circ_buf_t *circ_buf, uint8_t data) |
Pawel Zarembski |
0:01f31e923fe2 | 41 | { |
Pawel Zarembski |
0:01f31e923fe2 | 42 | cortex_int_state_t state; |
Pawel Zarembski |
0:01f31e923fe2 | 43 | state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 44 | |
Pawel Zarembski |
0:01f31e923fe2 | 45 | circ_buf->buf[circ_buf->tail] = data; |
Pawel Zarembski |
0:01f31e923fe2 | 46 | circ_buf->tail += 1; |
Pawel Zarembski |
0:01f31e923fe2 | 47 | if (circ_buf->tail >= circ_buf->size) { |
Pawel Zarembski |
0:01f31e923fe2 | 48 | util_assert(circ_buf->tail == circ_buf->size); |
Pawel Zarembski |
0:01f31e923fe2 | 49 | circ_buf->tail = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 50 | } |
Pawel Zarembski |
0:01f31e923fe2 | 51 | |
Pawel Zarembski |
0:01f31e923fe2 | 52 | // Assert no overflow |
Pawel Zarembski |
0:01f31e923fe2 | 53 | util_assert(circ_buf->head != circ_buf->tail); |
Pawel Zarembski |
0:01f31e923fe2 | 54 | |
Pawel Zarembski |
0:01f31e923fe2 | 55 | cortex_int_restore(state); |
Pawel Zarembski |
0:01f31e923fe2 | 56 | } |
Pawel Zarembski |
0:01f31e923fe2 | 57 | |
Pawel Zarembski |
0:01f31e923fe2 | 58 | uint8_t circ_buf_pop(circ_buf_t *circ_buf) |
Pawel Zarembski |
0:01f31e923fe2 | 59 | { |
Pawel Zarembski |
0:01f31e923fe2 | 60 | uint8_t data; |
Pawel Zarembski |
0:01f31e923fe2 | 61 | cortex_int_state_t state; |
Pawel Zarembski |
0:01f31e923fe2 | 62 | |
Pawel Zarembski |
0:01f31e923fe2 | 63 | state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 64 | |
Pawel Zarembski |
0:01f31e923fe2 | 65 | // Assert buffer isn't empty |
Pawel Zarembski |
0:01f31e923fe2 | 66 | util_assert(circ_buf->head != circ_buf->tail); |
Pawel Zarembski |
0:01f31e923fe2 | 67 | |
Pawel Zarembski |
0:01f31e923fe2 | 68 | data = circ_buf->buf[circ_buf->head]; |
Pawel Zarembski |
0:01f31e923fe2 | 69 | circ_buf->head += 1; |
Pawel Zarembski |
0:01f31e923fe2 | 70 | if (circ_buf->head >= circ_buf->size) { |
Pawel Zarembski |
0:01f31e923fe2 | 71 | util_assert(circ_buf->head == circ_buf->size); |
Pawel Zarembski |
0:01f31e923fe2 | 72 | circ_buf->head = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 73 | } |
Pawel Zarembski |
0:01f31e923fe2 | 74 | |
Pawel Zarembski |
0:01f31e923fe2 | 75 | cortex_int_restore(state); |
Pawel Zarembski |
0:01f31e923fe2 | 76 | |
Pawel Zarembski |
0:01f31e923fe2 | 77 | return data; |
Pawel Zarembski |
0:01f31e923fe2 | 78 | } |
Pawel Zarembski |
0:01f31e923fe2 | 79 | |
Pawel Zarembski |
0:01f31e923fe2 | 80 | uint32_t circ_buf_count_used(circ_buf_t *circ_buf) |
Pawel Zarembski |
0:01f31e923fe2 | 81 | { |
Pawel Zarembski |
0:01f31e923fe2 | 82 | uint32_t cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 83 | cortex_int_state_t state; |
Pawel Zarembski |
0:01f31e923fe2 | 84 | |
Pawel Zarembski |
0:01f31e923fe2 | 85 | state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 86 | |
Pawel Zarembski |
0:01f31e923fe2 | 87 | if (circ_buf->tail >= circ_buf->head) { |
Pawel Zarembski |
0:01f31e923fe2 | 88 | cnt = circ_buf->tail - circ_buf->head; |
Pawel Zarembski |
0:01f31e923fe2 | 89 | } else { |
Pawel Zarembski |
0:01f31e923fe2 | 90 | cnt = circ_buf->tail + circ_buf->size - circ_buf->head; |
Pawel Zarembski |
0:01f31e923fe2 | 91 | } |
Pawel Zarembski |
0:01f31e923fe2 | 92 | |
Pawel Zarembski |
0:01f31e923fe2 | 93 | cortex_int_restore(state); |
Pawel Zarembski |
0:01f31e923fe2 | 94 | return cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 95 | } |
Pawel Zarembski |
0:01f31e923fe2 | 96 | |
Pawel Zarembski |
0:01f31e923fe2 | 97 | uint32_t circ_buf_count_free(circ_buf_t *circ_buf) |
Pawel Zarembski |
0:01f31e923fe2 | 98 | { |
Pawel Zarembski |
0:01f31e923fe2 | 99 | uint32_t cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 100 | cortex_int_state_t state; |
Pawel Zarembski |
0:01f31e923fe2 | 101 | |
Pawel Zarembski |
0:01f31e923fe2 | 102 | state = cortex_int_get_and_disable(); |
Pawel Zarembski |
0:01f31e923fe2 | 103 | |
Pawel Zarembski |
0:01f31e923fe2 | 104 | cnt = circ_buf->size - circ_buf_count_used(circ_buf) - 1; |
Pawel Zarembski |
0:01f31e923fe2 | 105 | |
Pawel Zarembski |
0:01f31e923fe2 | 106 | cortex_int_restore(state); |
Pawel Zarembski |
0:01f31e923fe2 | 107 | return cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 108 | } |
Pawel Zarembski |
0:01f31e923fe2 | 109 | |
Pawel Zarembski |
0:01f31e923fe2 | 110 | uint32_t circ_buf_read(circ_buf_t *circ_buf, uint8_t *data, uint32_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 111 | { |
Pawel Zarembski |
0:01f31e923fe2 | 112 | uint32_t cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 113 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 114 | |
Pawel Zarembski |
0:01f31e923fe2 | 115 | cnt = circ_buf_count_used(circ_buf); |
Pawel Zarembski |
0:01f31e923fe2 | 116 | cnt = MIN(size, cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 117 | for (i = 0; i < cnt; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 118 | data[i] = circ_buf_pop(circ_buf); |
Pawel Zarembski |
0:01f31e923fe2 | 119 | } |
Pawel Zarembski |
0:01f31e923fe2 | 120 | |
Pawel Zarembski |
0:01f31e923fe2 | 121 | return cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 122 | } |
Pawel Zarembski |
0:01f31e923fe2 | 123 | |
Pawel Zarembski |
0:01f31e923fe2 | 124 | uint32_t circ_buf_write(circ_buf_t *circ_buf, const uint8_t *data, uint32_t size) |
Pawel Zarembski |
0:01f31e923fe2 | 125 | { |
Pawel Zarembski |
0:01f31e923fe2 | 126 | uint32_t cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 127 | uint32_t i; |
Pawel Zarembski |
0:01f31e923fe2 | 128 | |
Pawel Zarembski |
0:01f31e923fe2 | 129 | cnt = circ_buf_count_free(circ_buf); |
Pawel Zarembski |
0:01f31e923fe2 | 130 | cnt = MIN(size, cnt); |
Pawel Zarembski |
0:01f31e923fe2 | 131 | for (i = 0; i < cnt; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 132 | circ_buf_push(circ_buf, data[i]); |
Pawel Zarembski |
0:01f31e923fe2 | 133 | } |
Pawel Zarembski |
0:01f31e923fe2 | 134 | |
Pawel Zarembski |
0:01f31e923fe2 | 135 | return cnt; |
Pawel Zarembski |
0:01f31e923fe2 | 136 | } |