Statistics
| Branch: | Tag: | Revision:

mongoose / examples / PIC32 / mqtt_client / firmware / src / system_config / mx795_CC3100_e16 / framework / driver / usart / src / drv_usart_static.c @ eaef5bd1

History | View | Annotate | Download (10.9 KB)

1 eaef5bd1 Alexander Alashkin
/* clang-format off */
2
/*******************************************************************************
3
  USART Driver Static implementation
4

5
  Company:
6
    Microchip Technology Inc.
7

8
  File Name:
9
    drv_usart_static.c
10

11
  Summary:
12
    Source code for the USART driver static implementation.
13

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

19
  Remarks:
20
    Static interfaces incorporate the driver instance number within the names
21
    of the routines, eliminating the need for an object ID or object handle.
22

23
    Static single-open interfaces also eliminate the need for the open handle.
24
*******************************************************************************/
25
26
//DOM-IGNORE-BEGIN
27
/*******************************************************************************
28
Copyright (c) 2015 released Microchip Technology Inc.  All rights reserved.
29

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

35
You should refer to the license agreement accompanying this Software for
36
additional information regarding your rights and obligations.
37

38
SOFTWARE AND DOCUMENTATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND,
39
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
40
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
41
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
42
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
43
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
44
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
45
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
46
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
47
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
48
*******************************************************************************/
49
//DOM-IGNORE-END
50
51
// *****************************************************************************
52
// *****************************************************************************
53
// Section: Included Files
54
// *****************************************************************************
55
// *****************************************************************************
56
57
#include "system_config.h"
58
#include "system_definitions.h"
59
60
61
// *****************************************************************************
62
// *****************************************************************************
63
// Section: Global Data
64
// *****************************************************************************
65
// *****************************************************************************
66
67
/* This is the driver static object . */
68
DRV_USART_OBJ  gDrvUSART0Obj ;
69
70
// *****************************************************************************
71
// *****************************************************************************
72
// Section: Instance 0 static driver functions
73
// *****************************************************************************
74
// *****************************************************************************
75
76
SYS_MODULE_OBJ DRV_USART0_Initialize(void)
77
{
78
    uint32_t clockSource;
79
80
    /* Disable the USART module to configure it*/
81
    PLIB_USART_Disable (USART_ID_2);
82
83
    /* Initialize the USART based on configuration settings */
84
    PLIB_USART_InitializeModeGeneral(USART_ID_2,
85
            false,  /*Auto baud*/
86
            false,  /*LoopBack mode*/
87
            false,  /*Auto wakeup on start*/
88
            false,  /*IRDA mode*/
89
            false);  /*Stop In Idle mode*/
90
91
    /* Set the line control mode */
92
    PLIB_USART_LineControlModeSelect(USART_ID_2, DRV_USART_LINE_CONTROL_8NONE1);
93
94
    /* We set the receive interrupt mode to receive an interrupt whenever FIFO
95
       is not empty */
96
    PLIB_USART_InitializeOperation(USART_ID_2,
97
            USART_RECEIVE_FIFO_ONE_CHAR,
98
            USART_TRANSMIT_FIFO_IDLE,
99
            USART_ENABLE_TX_RX_USED);
100
101
    /* Get the USART clock source value*/
102
    clockSource = SYS_CLK_PeripheralFrequencyGet ( CLK_BUS_PERIPHERAL_1 );
103
104
    /* Set the baud rate and enable the USART */
105
    PLIB_USART_BaudSetAndEnable(USART_ID_2,
106
            clockSource,
107
            115200);  /*Desired Baud rate value*/
108
109
    /* Clear the interrupts to be on the safer side*/
110
    SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_TRANSMIT);
111
    SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_RECEIVE);
112
    SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_ERROR);
113
114
    /* Enable the error interrupt source */
115
    SYS_INT_SourceEnable(INT_SOURCE_USART_2_ERROR);
116
117
    /* Enable the Receive interrupt source */
118
    SYS_INT_SourceEnable(INT_SOURCE_USART_2_RECEIVE);
119
120
    /* Return the driver instance value*/
121
    return (SYS_MODULE_OBJ)DRV_USART_INDEX_0;
122
}
123
124
void  DRV_USART0_Deinitialize(void)
125
{
126
    bool status;
127
128
    /* Disable the interrupts */
129
    status = SYS_INT_SourceDisable(INT_SOURCE_USART_2_TRANSMIT) ;
130
    status = SYS_INT_SourceDisable(INT_SOURCE_USART_2_RECEIVE) ;
131
    status = SYS_INT_SourceDisable(INT_SOURCE_USART_2_ERROR);
132
    /* Ignore the warning */
133
    (void)status;
134
135
    /* Disable USART module */
136
    PLIB_USART_Disable (USART_ID_2);
137
138
}
139
140
141
SYS_STATUS DRV_USART0_Status(void)
142
{
143
    /* Return the status as ready always */
144
    return SYS_STATUS_READY;
145
}
146
147
148
void DRV_USART0_TasksTransmit(void)
149
{
150
    /* This is the USART Driver Transmit tasks routine.
151
       In this function, the driver checks if a transmit
152
       interrupt is active and performs respective action*/
153
154
    /* Reading the transmit interrupt flag */
155
    if(SYS_INT_SourceStatusGet(INT_SOURCE_USART_2_TRANSMIT))
156
    {
157
        /* Disable the interrupt, to avoid calling ISR continuously*/
158
        SYS_INT_SourceDisable(INT_SOURCE_USART_2_TRANSMIT);
159
160
        /* Clear up the interrupt flag */
161
        SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_TRANSMIT);
162
    }
163
}
164
165
void DRV_USART0_TasksReceive(void)
166
{
167
    /* This is the USART Driver Receive tasks routine. If the receive
168
       interrupt flag is set, the tasks routines are executed.
169
     */
170
171
    /* Reading the receive interrupt flag */
172
    if(SYS_INT_SourceStatusGet(INT_SOURCE_USART_2_RECEIVE))
173
    {
174
175
        /* Clear up the interrupt flag */
176
        SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_RECEIVE);
177
    }
178
}
179
180
181
void DRV_USART0_TasksError(void)
182
{
183
    /* This is the USART Driver Error tasks routine. In this function, the
184
     * driver checks if an error interrupt has occurred. If so the error
185
     * condition is cleared.  */
186
187
    /* Reading the error interrupt flag */
188
    if(SYS_INT_SourceStatusGet(INT_SOURCE_USART_2_ERROR))
189
    {
190
        /* This means an error has occurred */
191
        if(PLIB_USART_ReceiverOverrunHasOccurred(USART_ID_2))
192
        {
193
            PLIB_USART_ReceiverOverrunErrorClear(USART_ID_2);
194
        }
195
196
        /* Clear up the error interrupt flag */
197
        SYS_INT_SourceStatusClear(INT_SOURCE_USART_2_ERROR);
198
    }
199
}
200
201
DRV_HANDLE DRV_USART0_Open(const SYS_MODULE_INDEX index, const DRV_IO_INTENT ioIntent)
202
{
203
204
    /* Return the driver instance value*/
205
    return ((DRV_HANDLE)DRV_USART_INDEX_0 );
206
}
207
208
void DRV_USART0_Close(void)
209
{
210
    return;
211
}
212
213
DRV_USART_CLIENT_STATUS DRV_USART0_ClientStatus(void)
214
{
215
    /* Return the status as ready always*/
216
    return DRV_USART_CLIENT_STATUS_READY;
217
}
218
219
DRV_USART_TRANSFER_STATUS DRV_USART0_TransferStatus( void )
220
{
221
    DRV_USART_TRANSFER_STATUS result = 0;
222
223
    /* Check if RX data available */
224
    if(PLIB_USART_ReceiverDataIsAvailable(USART_ID_2))
225
    {
226
        result|= DRV_USART_TRANSFER_STATUS_RECEIVER_DATA_PRESENT;
227
    }
228
    else
229
    {
230
        result|= DRV_USART_TRANSFER_STATUS_RECEIVER_EMPTY;
231
    }
232
233
    /* Check if TX Buffer is empty */
234
    if(PLIB_USART_TransmitterIsEmpty(USART_ID_2))
235
    {
236
        result|= DRV_USART_TRANSFER_STATUS_TRANSMIT_EMPTY;
237
    }
238
239
    /* Check if the TX buffer is full */
240
    if(PLIB_USART_TransmitterBufferIsFull(USART_ID_2))
241
    {
242
        result|= DRV_USART_TRANSFER_STATUS_TRANSMIT_FULL;
243
    }
244
245
    return(result);
246
}
247
248
249
250
251
252
DRV_USART_BAUD_SET_RESULT DRV_USART0_BaudSet(uint32_t baud)
253
{
254
    uint32_t clockSource;
255
    int32_t brgValueLow=0;
256
    int32_t brgValueHigh=0;
257
    DRV_USART_BAUD_SET_RESULT retVal = DRV_USART_BAUD_SET_SUCCESS;
258
#if defined (PLIB_USART_ExistsModuleBusyStatus)
259
    bool isEnabled = false;
260
#endif
261
262
    /* Get the USART clock source value*/
263
    clockSource = SYS_CLK_PeripheralFrequencyGet ( CLK_BUS_PERIPHERAL_1 );
264
265
    /* Calculate low and high baud values */
266
    brgValueLow  = ( (clockSource/baud) >> 4 ) - 1;
267
    brgValueHigh = ( (clockSource/baud) >> 2 ) - 1;
268
269
#if defined (PLIB_USART_ExistsModuleBusyStatus)
270
        isEnabled = PLIB_USART_ModuleIsBusy (USART_ID_2);
271
        if (isEnabled)
272
        {
273
            PLIB_USART_Disable (USART_ID_2);
274
            while (PLIB_USART_ModuleIsBusy (USART_ID_2));
275
        }
276
#endif
277
278
    /* Check if the baud value can be set with high baud settings */
279
    if ((brgValueHigh >= 0) && (brgValueHigh <= UINT16_MAX))
280
    {
281
        PLIB_USART_BaudRateHighEnable(USART_ID_2);
282
        PLIB_USART_BaudRateHighSet(USART_ID_2,clockSource,baud);
283
    }
284
    
285
    /* Check if the baud value can be set with low baud settings */
286
    else if ((brgValueLow >= 0) && (brgValueLow <= UINT16_MAX))
287
    {
288
        PLIB_USART_BaudRateHighDisable(USART_ID_2);
289
        PLIB_USART_BaudRateSet(USART_ID_2, clockSource, baud);
290
    }
291
    else
292
    {
293
            retVal = DRV_USART_BAUD_SET_ERROR;
294
    }
295
296
#if defined (PLIB_USART_ExistsModuleBusyStatus)
297
    if (isEnabled)
298
    {
299
        PLIB_USART_Enable (USART_ID_2);
300
    }
301
#endif
302
303
    return retVal;
304
}
305
306
307
DRV_USART_LINE_CONTROL_SET_RESULT DRV_USART0_LineControlSet(DRV_USART_LINE_CONTROL lineControlMode)
308
{
309
#if defined (PLIB_USART_ExistsModuleBusyStatus)
310
    bool isEnabled = false;
311
#endif
312
#if defined (PLIB_USART_ExistsModuleBusyStatus)
313
        isEnabled = PLIB_USART_ModuleIsBusy (USART_ID_2);
314
        if (isEnabled)
315
        {
316
            PLIB_USART_Disable (USART_ID_2);
317
            while (PLIB_USART_ModuleIsBusy (USART_ID_2));
318
        }
319
#endif
320
321
    /* Set the Line Control Mode */
322
    PLIB_USART_LineControlModeSelect(USART_ID_2, lineControlMode);
323
    
324
#if defined (PLIB_USART_ExistsModuleBusyStatus)
325
        if (isEnabled)
326
        {
327
            PLIB_USART_Enable (USART_ID_2);
328
        }
329
#endif
330
331
    /* Return success */
332
    return(DRV_USART_LINE_CONTROL_SET_SUCCESS);
333
}
334
335
/*******************************************************************************
336
 End of File
337
*/