Statistics
| Branch: | Tag: | Revision:

mongoose / examples / PIC32 / http_server / firmware / src / system_config / pic32mx_eth_sk2_encx24j600 / framework / driver / spi / dynamic / drv_spi_master_dma_tasks.c @ eaef5bd1

History | View | Annotate | Download (7.95 KB)

1
/* clang-format off */
2
/*******************************************************************************
3
  SPI Driver Functions for Dynamic Standard Buffer Driver Tasks Functions
4

5
  Company:
6
    Microchip Technology Inc.
7

8
  File Name:
9
    drv_spi_dynamic_master_dma_tasks.c
10

11
  Summary:
12
    SPI driver tasks functions
13

14
  Description:
15
    The SPI device driver provides a simple interface to manage the SPI
16
    modules on Microchip microcontrollers. This file contains implemenation
17
    for the SPI driver.
18

19
  Remarks:
20
  This file is generated from framework/driver/spi/template/drv_spi_dynamic_ebm_tasks.c.ftl
21
*******************************************************************************/
22

    
23
//DOM-IGNORE-BEGIN
24
/*******************************************************************************
25
Copyright (c) 2013 released Microchip Technology Inc.  All rights reserved.
26

27
Microchip licenses to you the right to use, modify, copy and distribute
28
Software only when embedded on a Microchip microcontroller or digital signal
29
controller that is integrated into your product or third party product
30
(pursuant to the sublicense terms in the accompanying license agreement).
31

32
You should refer to the license agreement accompanying this Software for
33
additional information regarding your rights and obligations.
34

35
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
36
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
37
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
38
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
39
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
40
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
41
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
42
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
43
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
44
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
45
*******************************************************************************/
46
//DOM-IGNORE-END
47

    
48
#include "driver/spi/src/dynamic/drv_spi_internal.h"
49
#include <stdbool.h>
50

    
51

    
52

    
53

    
54

    
55
void DRV_SPI_MasterDMASendDummy8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
56
{
57
    if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
58
    {
59
        // Ignore for now
60
        return;
61
    }
62
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
63
    DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
64
    if (currentJob->dummyLeftToTx != 0)
65
    {
66
            uint8_t * ptr = sDrvSpiTxDummy;
67
            uint32_t len = MIN(MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dummyLeftToTx), DRV_SPI_DMA_TXFER_SIZE), DRV_SPI_DMA_DUMMY_BUFFER_SIZE);
68
            void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
69
            SYS_DMA_ChannelTransferAdd(pDrvInstance->txDmaChannelHandle, ptr, len, spiPtr, 1, 1);
70
            currentJob->txDMAProgressStage = DRV_SPI_DMA_DUMMY_INPROGRESS;
71
            currentJob->dummyLeftToTx -= len;
72
    }
73
    else
74
    {
75
        // Job is done
76
        currentJob->txDMAProgressStage = DRV_SPI_DMA_COMPLETE;
77
    }
78

    
79
}
80

    
81

    
82
void DRV_SPI_MasterDMASendData8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
83
{
84
    if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
85
    {
86
        // Ignore for now
87
        return;
88
    }
89
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
90
    DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
91
    if (currentJob->dataLeftToTx != 0)
92
    {
93
            uint8_t * ptr = &(currentJob->txBuffer[currentJob->dataTxed]);
94
            uint32_t len = MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dataLeftToTx), DRV_SPI_DMA_TXFER_SIZE);
95
            void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
96
            SYS_DMA_ChannelTransferAdd(pDrvInstance->txDmaChannelHandle, ptr, len, spiPtr, 1, 1);
97
            currentJob->txDMAProgressStage = DRV_SPI_DMA_DATA_INPROGRESS;
98
            currentJob->dataLeftToTx -= len;
99
            currentJob->dataTxed += len;
100
    }
101
    else
102
    {
103
        // Job is done
104
        currentJob->txDMAProgressStage = DRV_SPI_DMA_COMPLETE;
105
    }
106
}
107

    
108

    
109
void DRV_SPI_ISRDMAMasterSendEventHandler8bit(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
110
{
111
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT * )contextHandle;
112
    register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
113

    
114
    switch(currentJob->txDMAProgressStage)
115
    {
116
        case DRV_SPI_DMA_DATA_INPROGRESS:
117
            DRV_SPI_MasterDMASendData8BitISR(event, handle, contextHandle);
118
            break;
119
        case DRV_SPI_DMA_DUMMY_INPROGRESS:
120
            DRV_SPI_MasterDMASendDummy8BitISR(event, handle, contextHandle);
121
            break;
122
        default:
123
            break;
124
    }
125
}
126
void DRV_SPI_MasterDMAReceiveDummy8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
127
{
128
    if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
129
    {
130
        // Ignore for now
131
        return;
132
    }
133
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
134
    DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
135
    if (currentJob->dummyLeftToRx != 0)
136
    {
137
            uint8_t * ptr = sDrvSpiRxDummy;
138
            uint32_t len = MIN(MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dummyLeftToRx), DRV_SPI_DMA_TXFER_SIZE), DRV_SPI_DMA_DUMMY_BUFFER_SIZE);
139
            void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
140
            SYS_DMA_ChannelTransferAdd(pDrvInstance->rxDmaChannelHandle, spiPtr, 1, ptr, len, 1);
141
            currentJob->rxDMAProgressStage = DRV_SPI_DMA_DUMMY_INPROGRESS;
142
            currentJob->dummyLeftToRx -= len;
143
    }
144
    else
145
    {
146
        // Job is done
147
        currentJob->rxDMAProgressStage = DRV_SPI_DMA_COMPLETE;
148
        SYS_INT_SourceEnable(pDrvInstance->rxInterruptSource);
149
    }
150
}
151

    
152
void DRV_SPI_MasterDMAReceiveData8BitISR(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
153
{
154
    if (event != SYS_DMA_TRANSFER_EVENT_COMPLETE)
155
    {
156
        // Ignore for now
157
        return;
158
    }
159
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT *)contextHandle;
160
    DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
161
    if (currentJob->dataLeftToRx != 0)
162
    {
163
            uint8_t * ptr = &(currentJob->rxBuffer[currentJob->dataRxed]);
164
            uint32_t len = MIN(MIN(PLIB_DMA_MAX_TRF_SIZE, currentJob->dataLeftToRx), DRV_SPI_DMA_TXFER_SIZE);
165
            void * spiPtr = PLIB_SPI_BufferAddressGet(pDrvInstance->spiId);
166
            SYS_DMA_ChannelTransferAdd(pDrvInstance->rxDmaChannelHandle, spiPtr, 1, ptr, len, 1);
167
            currentJob->rxDMAProgressStage = DRV_SPI_DMA_DATA_INPROGRESS;
168
            currentJob->dataLeftToRx -= len;
169
            currentJob->dataRxed += len;
170
    }
171
    else
172
    {
173
        // Job is done
174
        currentJob->rxDMAProgressStage = DRV_SPI_DMA_COMPLETE;
175
        SYS_INT_SourceEnable(pDrvInstance->rxInterruptSource);
176
    }
177
}
178

    
179
void DRV_SPI_ISRDMAMasterReceiveEventHandler8bit(SYS_DMA_TRANSFER_EVENT event, SYS_DMA_CHANNEL_HANDLE handle, uintptr_t contextHandle)
180
{
181
    struct DRV_SPI_DRIVER_OBJECT * pDrvInstance = (struct DRV_SPI_DRIVER_OBJECT * )contextHandle;
182
    register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
183

    
184
    switch(currentJob->rxDMAProgressStage)
185
    {
186
        case DRV_SPI_DMA_DATA_INPROGRESS:
187
            DRV_SPI_MasterDMAReceiveData8BitISR(event, handle, contextHandle);
188
            break;
189
        case DRV_SPI_DMA_DUMMY_INPROGRESS:
190
            DRV_SPI_MasterDMAReceiveDummy8BitISR(event, handle, contextHandle);
191
            break;
192
        default:
193
            break;
194
    }
195
}
196