summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_pima_event_set.c
blob: cc39809783fb6dc14710a4a6232b2e210f45e198 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Device PIMA Class                                                   */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_pima.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_pima_event_set                     PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sends an event to the pima class. It is processed     */
/*    asynchronously by the interrupt thread.                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    pima                                     Address of pima class      */
/*    event                                    Pointer of the event       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                   UX_SUCCESS if there is an  */
/*                                             event                      */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_semaphore_put                 Put semaphore              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    ThreadX                                                             */
/*                                                                        */
/**************************************************************************/
UINT  _ux_device_class_pima_event_set(UX_SLAVE_CLASS_PIMA *pima,
                                      UX_SLAVE_CLASS_PIMA_EVENT *pima_event)
{

UX_SLAVE_CLASS_PIMA_EVENT       *current_pima_event;
UX_SLAVE_CLASS_PIMA_EVENT       *next_pima_event;
UX_SLAVE_DEVICE                 *device;

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_EVENT_SET, pima, pima_event, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)

    /* Get the pointer to the device.  */
    device =  &_ux_system_slave -> ux_system_slave_device;

    /* Check the device state.  */
    if (device -> ux_slave_device_state !=  UX_DEVICE_CONFIGURED)
    {

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_DEVICE_HANDLE_UNKNOWN);
    }

    /* Check if interrupt endpoint is available.  */
    if (pima -> ux_device_class_pima_interrupt_endpoint == UX_NULL)
    {
        return (UX_FUNCTION_NOT_SUPPORTED);
    }

    /* Current position of the head.  */
    current_pima_event =  pima -> ux_device_class_pima_event_array_head;

    /* If the pointer is NULL, the round robin buffer has not been activated.  */
    if (current_pima_event == UX_NULL)
        return (UX_ERROR);

    /* Calculate the next position.  */
    if ((current_pima_event + 1) == pima -> ux_device_class_pima_event_array_end)

        /* We are at the end, go back to the beginning.  */
        next_pima_event =  pima -> ux_device_class_pima_event_array;

    else
        /* We are not at the end, increment the head position.  */
        next_pima_event = current_pima_event + 1;


    /* Any place left for this event ? */
    if (next_pima_event == pima -> ux_device_class_pima_event_array_tail)
        return (UX_ERROR);

    /* Update the head.  */
    pima -> ux_device_class_pima_event_array_head = next_pima_event;

    /* There is an event to report, get the current pointer to the event.  */
    current_pima_event =  pima -> ux_device_class_pima_event_array_tail;

    /* fill in the event structure from the user.  */
    current_pima_event -> ux_device_class_pima_event_code           = pima_event -> ux_device_class_pima_event_code;
    current_pima_event -> ux_device_class_pima_event_transaction_id = pima_event -> ux_device_class_pima_event_transaction_id;
    current_pima_event -> ux_device_class_pima_event_parameter_1    = pima_event -> ux_device_class_pima_event_parameter_1;
    current_pima_event -> ux_device_class_pima_event_parameter_2    = pima_event -> ux_device_class_pima_event_parameter_2;
    current_pima_event -> ux_device_class_pima_event_parameter_3    = pima_event -> ux_device_class_pima_event_parameter_3;

    /* Set a semaphore to wake up the interrupt thread.  */
    _ux_device_semaphore_put(&pima -> ux_device_class_pima_interrupt_thread_semaphore);

    /* Return event status to the user.  */
    return(UX_SUCCESS);
}