t
Fork of mbed-dev by
targets/TARGET_Atmel/TARGET_SAM_CortexM0P/sercom_dma.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/sercom_dma.c@64:41a834223ea3
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 15:a81a8d6c1dfe | 1 | /* mbed Microcontroller Library |
mbed_official | 15:a81a8d6c1dfe | 2 | * Copyright (c) 2006-2015 ARM Limited |
mbed_official | 15:a81a8d6c1dfe | 3 | * |
mbed_official | 15:a81a8d6c1dfe | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 15:a81a8d6c1dfe | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 15:a81a8d6c1dfe | 6 | * You may obtain a copy of the License at |
mbed_official | 15:a81a8d6c1dfe | 7 | * |
mbed_official | 15:a81a8d6c1dfe | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 15:a81a8d6c1dfe | 9 | * |
mbed_official | 15:a81a8d6c1dfe | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 15:a81a8d6c1dfe | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 15:a81a8d6c1dfe | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 15:a81a8d6c1dfe | 13 | * See the License for the specific language governing permissions and |
mbed_official | 15:a81a8d6c1dfe | 14 | * limitations under the License. |
mbed_official | 15:a81a8d6c1dfe | 15 | */ |
mbed_official | 15:a81a8d6c1dfe | 16 | #include "mbed_assert.h" |
mbed_official | 15:a81a8d6c1dfe | 17 | |
mbed_official | 15:a81a8d6c1dfe | 18 | #include "cmsis.h" |
mbed_official | 15:a81a8d6c1dfe | 19 | #include "sercom.h" |
mbed_official | 15:a81a8d6c1dfe | 20 | #include "dma_api.h" |
mbed_official | 15:a81a8d6c1dfe | 21 | #include "dma_api_HAL.h" |
mbed_official | 15:a81a8d6c1dfe | 22 | #include "sercom_dma.h" |
mbed_official | 15:a81a8d6c1dfe | 23 | |
mbed_official | 15:a81a8d6c1dfe | 24 | static struct sercom_dma sercom_channels[SERCOM_INST_NUM] = {{0}}; |
mbed_official | 15:a81a8d6c1dfe | 25 | |
mbed_official | 15:a81a8d6c1dfe | 26 | /** Allocate a channel for TX |
mbed_official | 15:a81a8d6c1dfe | 27 | * |
mbed_official | 15:a81a8d6c1dfe | 28 | * @param[in] sercom_index index of sercom instance |
mbed_official | 15:a81a8d6c1dfe | 29 | * @param[out] tx_id pointer to channel id |
mbed_official | 15:a81a8d6c1dfe | 30 | * @return allocated channel id |
mbed_official | 15:a81a8d6c1dfe | 31 | */ |
mbed_official | 15:a81a8d6c1dfe | 32 | uint8_t sercom_setup_tx_channel(uint8_t sercom_index, uint8_t *tx_id) |
mbed_official | 15:a81a8d6c1dfe | 33 | { |
mbed_official | 15:a81a8d6c1dfe | 34 | if (sercom_channels[sercom_index].tx_status & DMA_ALLOCATED) { |
mbed_official | 15:a81a8d6c1dfe | 35 | *tx_id = sercom_channels[sercom_index].tx_channel_id; |
mbed_official | 15:a81a8d6c1dfe | 36 | } else { |
mbed_official | 15:a81a8d6c1dfe | 37 | /* Try to allocate a channel */ |
mbed_official | 15:a81a8d6c1dfe | 38 | sercom_channels[sercom_index].tx_channel_id = dma_channel_allocate(DMA_CAP_NONE); |
mbed_official | 15:a81a8d6c1dfe | 39 | if (sercom_channels[sercom_index].tx_channel_id != (uint8_t)DMA_ERROR_OUT_OF_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 40 | *tx_id = sercom_channels[sercom_index].tx_channel_id; |
mbed_official | 15:a81a8d6c1dfe | 41 | sercom_channels[sercom_index].tx_status = DMA_ALLOCATED; |
mbed_official | 15:a81a8d6c1dfe | 42 | } else { |
mbed_official | 15:a81a8d6c1dfe | 43 | /* Couldn't find a channel */ |
mbed_official | 64:41a834223ea3 | 44 | return (uint8_t)DMA_ERROR_OUT_OF_CHANNELS; |
mbed_official | 15:a81a8d6c1dfe | 45 | } |
mbed_official | 15:a81a8d6c1dfe | 46 | } |
mbed_official | 15:a81a8d6c1dfe | 47 | return *tx_id; |
mbed_official | 15:a81a8d6c1dfe | 48 | } |
mbed_official | 15:a81a8d6c1dfe | 49 | |
mbed_official | 15:a81a8d6c1dfe | 50 | /** Allocate a channel for RX |
mbed_official | 15:a81a8d6c1dfe | 51 | * |
mbed_official | 15:a81a8d6c1dfe | 52 | * @param[in] sercom_index index of sercom instance |
mbed_official | 15:a81a8d6c1dfe | 53 | * @param[out] rx_id pointer to channel id |
mbed_official | 15:a81a8d6c1dfe | 54 | * @return allocated channel id |
mbed_official | 15:a81a8d6c1dfe | 55 | */ |
mbed_official | 15:a81a8d6c1dfe | 56 | uint8_t sercom_setup_rx_channel(uint8_t sercom_index, uint8_t *rx_id) |
mbed_official | 15:a81a8d6c1dfe | 57 | { |
mbed_official | 15:a81a8d6c1dfe | 58 | if (sercom_channels[sercom_index].rx_status & DMA_ALLOCATED) { |
mbed_official | 15:a81a8d6c1dfe | 59 | *rx_id = sercom_channels[sercom_index].rx_channel_id; |
mbed_official | 15:a81a8d6c1dfe | 60 | } else { |
mbed_official | 15:a81a8d6c1dfe | 61 | /* Try to allocate a channel */ |
mbed_official | 15:a81a8d6c1dfe | 62 | sercom_channels[sercom_index].rx_channel_id = dma_channel_allocate(DMA_CAP_NONE); |
mbed_official | 15:a81a8d6c1dfe | 63 | if (sercom_channels[sercom_index].rx_channel_id != (uint8_t)DMA_ERROR_OUT_OF_CHANNELS) { |
mbed_official | 15:a81a8d6c1dfe | 64 | *rx_id = sercom_channels[sercom_index].rx_channel_id; |
mbed_official | 15:a81a8d6c1dfe | 65 | sercom_channels[sercom_index].rx_status = DMA_ALLOCATED; |
mbed_official | 15:a81a8d6c1dfe | 66 | } else { |
mbed_official | 15:a81a8d6c1dfe | 67 | /* Couldn't find a channel */ |
mbed_official | 64:41a834223ea3 | 68 | return (uint8_t)DMA_ERROR_OUT_OF_CHANNELS; |
mbed_official | 15:a81a8d6c1dfe | 69 | } |
mbed_official | 15:a81a8d6c1dfe | 70 | } |
mbed_official | 15:a81a8d6c1dfe | 71 | return *rx_id; |
mbed_official | 15:a81a8d6c1dfe | 72 | } |
mbed_official | 15:a81a8d6c1dfe | 73 | |
mbed_official | 15:a81a8d6c1dfe | 74 | /** Release DMA channels if allocated |
mbed_official | 15:a81a8d6c1dfe | 75 | * |
mbed_official | 15:a81a8d6c1dfe | 76 | * @param[in] sercom_index index of sercom instance |
mbed_official | 15:a81a8d6c1dfe | 77 | * @return void |
mbed_official | 15:a81a8d6c1dfe | 78 | */ |
mbed_official | 15:a81a8d6c1dfe | 79 | void sercom_release_channel(uint8_t sercom_index) |
mbed_official | 15:a81a8d6c1dfe | 80 | { |
mbed_official | 15:a81a8d6c1dfe | 81 | if (sercom_channels[sercom_index].rx_status & DMA_ALLOCATED) { |
mbed_official | 15:a81a8d6c1dfe | 82 | if (0 == dma_channel_free(sercom_channels[sercom_index].rx_channel_id)) { |
mbed_official | 15:a81a8d6c1dfe | 83 | sercom_channels[sercom_index].rx_status = DMA_NOT_USED; |
mbed_official | 15:a81a8d6c1dfe | 84 | } |
mbed_official | 15:a81a8d6c1dfe | 85 | } |
mbed_official | 15:a81a8d6c1dfe | 86 | if (sercom_channels[sercom_index].tx_status & DMA_ALLOCATED) { |
mbed_official | 15:a81a8d6c1dfe | 87 | if (0 == dma_channel_free(sercom_channels[sercom_index].tx_channel_id)) { |
mbed_official | 15:a81a8d6c1dfe | 88 | sercom_channels[sercom_index].tx_status = DMA_NOT_USED; |
mbed_official | 15:a81a8d6c1dfe | 89 | } |
mbed_official | 15:a81a8d6c1dfe | 90 | } |
mbed_official | 15:a81a8d6c1dfe | 91 | } |