Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.73 KB)

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

5
  Company:
6
    Microchip Technology Inc.
7

8
  File Name:
9
    drv_spi_dynamic_ebm_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
int32_t DRV_SPI_MasterEBMSend8BitISR( struct DRV_SPI_DRIVER_OBJECT * pDrvInstance )
53
{
54
    register SPI_MODULE_ID spiId = pDrvInstance->spiId;
55
    register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
56

    
57
    /* Determine the maximum number of bytes we can send to the FIFO*/
58
        uint8_t symbolsInTransit = MAX(pDrvInstance->symbolsInProgress, PLIB_SPI_FIFOCountGet(spiId, SPI_FIFO_TYPE_TRANSMIT));
59
        uint8_t bufferBytes = PLIB_SPI_TX_8BIT_FIFO_SIZE(spiId) - symbolsInTransit;
60
    /* Figure out how much data we can send*/
61
    size_t dataUnits = MIN(currentJob->dataLeftToTx, bufferBytes);
62

    
63
    size_t counter;
64

    
65
    if (dataUnits != 0)
66
    {
67
        /* Adjust the maximum buffer size downwards based on how much data we'll be sending*/
68
        bufferBytes -= dataUnits;
69
        currentJob->dataLeftToTx -= dataUnits;
70
        /* Set the location in the buffer of where to start sending from*/
71
        uint8_t *bufferLoc = &(currentJob->txBuffer[currentJob->dataTxed]);
72
        for (counter = 0; counter < dataUnits; counter++)
73
        {
74
            /* Send a unit/symbol of data*/
75
            PLIB_SPI_BufferWrite(spiId, bufferLoc[counter]);
76
        }
77
        /* Update the number of bytes transmitted*/
78
        currentJob->dataTxed += dataUnits;
79
        /* Adjust the symbols in progress */
80
        pDrvInstance->symbolsInProgress += dataUnits;
81
    }
82
    size_t dummyUnits = MIN(currentJob->dummyLeftToTx, bufferBytes);
83
    if (dummyUnits != 0)
84
    {
85
        currentJob->dummyLeftToTx -= dummyUnits;
86
        /* Adjust the symbols in progress */
87
        pDrvInstance->symbolsInProgress += dummyUnits;
88
        for (counter = 0; counter < dummyUnits; counter++)
89
        {
90
            PLIB_SPI_BufferWrite(spiId, 0xff);
91
        }
92
    }
93
    if (currentJob->dataLeftToTx + currentJob->dummyLeftToTx == 0)
94
    {
95
        /* We have no more data to send, turn off the TX interrupt*/
96
        PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_TRANSMIT_BUFFER_IS_COMPLETELY_EMPTY);
97
        pDrvInstance->txEnabled = false;
98

    
99
        /* Turn on the RX Interrupt*/
100
        pDrvInstance->rxEnabled = true;
101
    }
102
    return 0;
103
}
104

    
105
int32_t DRV_SPI_MasterEBMReceive8BitISR( struct DRV_SPI_DRIVER_OBJECT * pDrvInstance )
106
{
107
    register SPI_MODULE_ID spiId = pDrvInstance->spiId;
108
    register DRV_SPI_JOB_OBJECT * currentJob = pDrvInstance->currentJob;
109

    
110
    /* Figure out how many bytes are waiting to be received."*/
111
    uint8_t bufferBytes = PLIB_SPI_FIFOCountGet(spiId, SPI_FIFO_TYPE_RECEIVE);
112
    /* Calculate the maximum number of data bytes that can be received*/
113
    size_t dataUnits = MIN(currentJob->dataLeftToRx, bufferBytes);
114
    size_t counter;
115

    
116
    if (dataUnits != 0)
117
    {
118
        bufferBytes -= dataUnits;
119
        currentJob->dataLeftToRx -= dataUnits;
120
        /* Set the buffer location to receive bytes from the SPI to*/
121
        uint8_t *bufferLoc = &(currentJob->rxBuffer[currentJob->dataRxed]);
122
        for (counter = 0; counter < dataUnits; counter++)
123
        {
124
            /* Receive the data from the SPI */
125
            bufferLoc[counter] = PLIB_SPI_BufferRead(spiId);
126
        }
127
        /* Adjust the amount of data that has been received */
128
        currentJob->dataRxed += dataUnits;
129
        /* Update the symbols in progress so we can send more units later */
130
        pDrvInstance->symbolsInProgress -= dataUnits;
131
    }
132

    
133
    /* Figure out the maximum number of dummy data to be received */
134
    size_t dummyUnits = MIN(currentJob->dummyLeftToRx, bufferBytes);
135
    if (dummyUnits != 0)
136
    {
137
        /* Lower the number of dummy bytes to be received */
138
        currentJob->dummyLeftToRx -= dummyUnits;
139
        pDrvInstance->symbolsInProgress -= dummyUnits;
140
        for (counter = 0; counter < dummyUnits; counter++)
141
        {
142
            /* Receive and throw away the byte.  Note: We cannot just clear the
143
               buffer because we have to keep track of how many symbols/units we
144
               have received, and the number may have increased since we checked
145
               how full the buffer is.*/
146
            PLIB_SPI_BufferRead(spiId);
147
        }
148
    }
149

    
150
    /* Figure out how many bytes are left to be received */
151
    size_t bytesLeft = currentJob->dataLeftToRx + currentJob->dummyLeftToRx;
152

    
153
    /* If the bytes left are smaller than the HW mark we have to change the interrupt mode */
154
    if (bytesLeft < PLIB_SPI_RX_8BIT_HW_MARK(spiId))
155
    {
156
        PLIB_SPI_FIFOInterruptModeSelect(spiId, SPI_FIFO_INTERRUPT_WHEN_RECEIVE_BUFFER_IS_NOT_EMPTY);
157
    }
158
    return 0;
159
}
160