1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #ifndef LIBRARIES_PERIPHDRIVERS_SOURCE_SKBD_SKBD_REVA_H_
22 #define LIBRARIES_PERIPHDRIVERS_SOURCE_SKBD_SKBD_REVA_H_
23 
24 #include <stddef.h>
25 #include "mxc_device.h"
26 #include "mxc_assert.h"
27 #include "mxc_pins.h"
28 #include "mxc_lock.h"
29 #include "mxc_delay.h"
30 #include "mxc_errors.h"
31 #include "nvic_table.h"
32 #include "skbd.h"
33 #include "skbd_reva_regs.h"
34 
35 #define xstr(s) str(s)
36 #define str(s) #s
37 #define MXC_SKBD_REVA_VERS_MAJOR <VERSMAJ>
38 #define MXC_SKBD_REVA_VERS_MINOR <VERSMIN>
39 #define MXC_SKBD_REVA_VERS_PATCH <VERSPAT>
40 #define MXC_SKBD_REVA_VERSION_STRING                                                \
41     "v" xstr(MXC_SKBD_REVA_VERS_MAJOR) "." xstr(MXC_SKBD_REVA_VERS_MINOR) "." xstr( \
42         MXC_SKBD_REVA_VERS_PATCH)
43 
44 /* COBRA adaptation */
45 #define MXC_KEYPAD_REVA_BASE_ERR 0 //COBRA_KEYPAD_BASE_ERR
46 /* Number of key registers present in the keypad interface */
47 #define MXC_SKBD_REVA_TOTAL_KEY_REGS 4
48 
49 /**
50  * @brief   Keypad errors list
51  *
52  */
53 typedef enum {
54     MXC_SKBD_REVA_ERR_MIN = MXC_KEYPAD_REVA_BASE_ERR,
55     MXC_SKBD_REVA_ERR_NOT_INITIALIZED, ///< Error Code: Keypad not initialized
56     MXC_SKBD_REVA_ERR_ALREAD_INITIALIZED, ///< Error Code: Keypad already initialized
57     MXC_SKBD_REVA_ERR_INVALID_OPERATION, ///< Error Code: Invalid keypad operation
58     MXC_SKBD_REVA_ERR_OUT_OF_RANGE, ///< Error Code: Invalid parameter or value
59     MXC_SKBD_REVA_ERR_OVERRUN, ///< Error Code: Keypad Over run error
60     MXC_SKBD_REVA_ERR_IRQ, ///< Error Code: IRQ setup error
61     MXC_SKBD_REVA_ERR_IRQ_NULL, ///< Error Code: NULL IRQ handler
62     MXC_SKBD_REVA_ERR_INVALID_PIN_CONFIGURATION, ///< Error Code: One or more keypad I/O pins are overlapped or  input/output pin configurations are invalid
63     MXC_SKBD_REVA_ERR_BUSY, ///< Error Code: Keypad is busy
64     MXC_SKBD_REVA_ERR_UNKNOWN, ///< Error Code: Generic error for unknown behavior
65     MXC_SKBD_REVA_ERR_MAX = MXC_SKBD_REVA_ERR_UNKNOWN
66 } mxc_skbd_reva_errors_t;
67 
68 /**
69  * @brief   Keypad initialization state FSM
70  *
71  */
72 typedef enum {
73     MXC_SKBD_REVA_STATE_MIN = 0,
74     MXC_SKBD_REVA_STATE_NOT_INITIALIZED = MXC_SKBD_REVA_STATE_MIN, ///< State not initialized
75     MXC_SKBD_REVA_STATE_INITIALIZED, ///< State initialized
76     MXC_SKBD_REVA_STATE_CLOSED, ///< State closed
77     MXC_SKBD_REVA_STATE_MAX = MXC_SKBD_REVA_STATE_CLOSED,
78     MXC_SKBD_REVA_STATE_COUNT
79 } mxc_skbd_reva_state_t;
80 
81 /**
82  * @brief   Keypad events
83  *
84  */
85 typedef enum {
86     MXC_SKBD_REVA_EVENT_PUSH = MXC_F_SKBD_REVA_IER_PUSHIE, ///< Push Event
87     MXC_SKBD_REVA_EVENT_RELEASE = MXC_F_SKBD_REVA_IER_RELEASEIE, ///< Release Event
88     MXC_SKBD_REVA_EVENT_OVERRUN = MXC_F_SKBD_REVA_IER_OVERIE ///< Overrun Event
89 } mxc_skbd_reva_events_t;
90 
91 /**
92  * @brief   Keypad Interrupt Status
93  *
94  */
95 typedef enum {
96     MXC_SKBD_REVA_INTERRUPT_STATUS_PUSHIS = MXC_F_SKBD_REVA_ISR_PUSHIS, ///< Push Interupt flag
97     MXC_SKBD_REVA_INTERRUPT_STATUS_RELEASEIS =
98         MXC_F_SKBD_REVA_ISR_RELEASEIS, ///< Release Interupt flag
99     MXC_SKBD_REVA_INTERRUPT_STATUS_OVERIS = MXC_F_SKBD_REVA_ISR_OVERIS ///< Overrun Interupt flag
100 } mxc_skbd_reva_interrupt_status_t;
101 
102 /**
103  * @brief   Keypad I/O's IOSEL
104  *
105  */
106 typedef enum {
107     MXC_SKBD_REVA_KBDIO0 = (0x01 << 0), ///< SKBD pin 0
108     MXC_SKBD_REVA_KBDIO1 = (0x01 << 1), ///< SKBD pin 1
109     MXC_SKBD_REVA_KBDIO2 = (0x01 << 2), ///< SKBD pin 2
110     MXC_SKBD_REVA_KBDIO3 = (0x01 << 3), ///< SKBD pin 3
111     MXC_SKBD_REVA_KBDIO4 = (0x01 << 4), ///< SKBD pin 4
112     MXC_SKBD_REVA_KBDIO5 = (0x01 << 5), ///< SKBD pin 5
113     MXC_SKBD_REVA_KBDIO6 = (0x01 << 6), ///< SKBD pin 6
114     MXC_SKBD_REVA_KBDIO7 = (0x01 << 7), ///< SKBD pin 7
115     MXC_SKBD_REVA_KBDIO8 = (0x01 << 8), ///< SKBD pin 8
116     MXC_SKBD_REVA_KBDIO9 = (0x01 << 9), ///< SKBD pin 9
117 } mxc_skbd_reva_io_pins_t;
118 
119 /**
120  * @brief   Keypad configuration structure
121  *
122  */
123 typedef struct {
124     uint16_t ioselect; ///< I/O pin direction selection for the corresponding keypad pins
125     unsigned int reg_erase; ///< key register erase flag on key is released
126     int outputs; ///< Specifies the keypad pins to be configured as output
127     int inputs; ///< Specifies the keypad pins to be configured as input
128     uint32_t debounce; ///< Keypad Debouncing Time
129     irq_handler_t irq_handler; ///< IRQ handler
130 } mxc_skbd_reva_config_t;
131 
132 /**
133  * @brief   Keypad channel context information
134  *
135  */
136 typedef struct {
137     unsigned int first_init; ///< 1 - initialize
138     unsigned int irq; ///< Interrupt request(IRQ) number
139     irq_handler_t irq_handler; ///< IRQ handler
140     mxc_skbd_reva_state_t state; ///< keypad initialization state
141 } mxc_skbd_reva_req_t;
142 
143 /**
144  * @brief   Keyboard Key's scan codes
145  *
146  */
147 typedef struct {
148     /*
149      * key scan code format as follows
150      *      key(x) bits[3-0] : Input scan code
151      *      key(x) bits[7-4] : Output scan code
152      *      key(x) bit[8]    : Next Key Flag
153      */
154     uint16_t key0_reva; ///< Key0 scan code
155     uint16_t key1_reva; ///< Key1 scan code
156     uint16_t key2_reva; ///< Key2 scan code
157     uint16_t key3_reva; ///< Key3 scan code
158 } mxc_skbd_reva_keys_t;
159 
160 int MXC_SKBD_RevA_PreInit(void);
161 
162 int MXC_SKBD_RevA_Init(mxc_skbd_reva_regs_t *skbd, mxc_skbd_config_t config);
163 
164 int MXC_SKBD_RevA_EnableInterruptEvents(mxc_skbd_reva_regs_t *skbd, unsigned int events);
165 
166 int MXC_SKBD_RevA_DisableInterruptEvents(mxc_skbd_reva_regs_t *skbd, unsigned int events);
167 
168 int MXC_SKBD_RevA_ClearInterruptStatus(mxc_skbd_reva_regs_t *skbd, unsigned int status);
169 
170 int MXC_SKBD_RevA_InterruptStatus(mxc_skbd_reva_regs_t *skbd, unsigned int *status);
171 
172 int MXC_SKBD_RevA_ReadKeys(mxc_skbd_reva_regs_t *skbd, mxc_skbd_reva_keys_t *keys);
173 
174 int MXC_SKBD_RevA_Close(void);
175 
176 #endif // LIBRARIES_PERIPHDRIVERS_SOURCE_SKBD_SKBD_REVA_H_
177