4848#define ERROR_TX_RTS 0x1000
4949#define ERROR_TX_NORESP 0x2000
5050
51- static void IRAM_ATTR ps2_interrupt_handler (void * self_in );
52-
53- static void ps2_set_config (ps2io_ps2_obj_t * self ) {
54- // turn on falling edge interrupt
55- gpio_set_intr_type (self -> clk_pin , GPIO_INTR_NEGEDGE );
56- gpio_install_isr_service (ESP_INTR_FLAG_IRAM );
57- gpio_isr_handler_add (self -> clk_pin , ps2_interrupt_handler , (void * )self );
58- }
59-
60- static void disable_interrupt (ps2io_ps2_obj_t * self ) {
61- // turn off fallling edge interrupt
62- gpio_isr_handler_remove (self -> clk_pin );
63- }
64-
65- static void resume_interrupt (ps2io_ps2_obj_t * self ) {
66- self -> state = STATE_IDLE ;
67- gpio_isr_handler_add (self -> clk_pin , ps2_interrupt_handler , (void * )self );
68- }
69-
70- static void clk_hi (ps2io_ps2_obj_t * self ) {
71- // external pull-up
72- gpio_set_direction (self -> clk_pin , GPIO_MODE_INPUT );
73- gpio_pullup_dis (self -> clk_pin );
74- }
75-
76- static bool wait_clk_lo (ps2io_ps2_obj_t * self , uint32_t us ) {
77- clk_hi (self );
78- common_hal_mcu_delay_us (1 );
79- while (gpio_get_level (self -> clk_pin ) && us ) {
80- -- us ;
81- common_hal_mcu_delay_us (1 );
82- }
83- return us ;
84- }
85-
86- static bool wait_clk_hi (ps2io_ps2_obj_t * self , uint32_t us ) {
87- clk_hi (self );
88- common_hal_mcu_delay_us (1 );
89- while (!gpio_get_level (self -> clk_pin ) && us ) {
90- -- us ;
91- common_hal_mcu_delay_us (1 );
92- }
93- return us ;
94- }
95-
96- static void clk_lo (ps2io_ps2_obj_t * self ) {
97- gpio_pullup_dis (self -> clk_pin );
98- gpio_set_direction (self -> clk_pin , GPIO_MODE_OUTPUT );
99- gpio_set_level (self -> clk_pin , 0 );
100- }
101-
102- static void data_hi (ps2io_ps2_obj_t * self ) {
103- // external pull-up
104- gpio_set_direction (self -> data_pin , GPIO_MODE_INPUT );
105- gpio_pullup_dis (self -> data_pin );
106- }
107-
108- static bool wait_data_lo (ps2io_ps2_obj_t * self , uint32_t us ) {
109- data_hi (self );
110- common_hal_mcu_delay_us (1 );
111- while (gpio_get_level (self -> data_pin ) && us ) {
112- -- us ;
113- common_hal_mcu_delay_us (1 );
114- }
115- return us ;
116- }
117-
118- static bool wait_data_hi (ps2io_ps2_obj_t * self , uint32_t us ) {
119- data_hi (self );
120- common_hal_mcu_delay_us (1 );
121- while (!gpio_get_level (self -> data_pin ) && us ) {
122- -- us ;
123- common_hal_mcu_delay_us (1 );
124- }
125- return us ;
126- }
127-
128- static void data_lo (ps2io_ps2_obj_t * self ) {
129- gpio_pullup_dis (self -> data_pin );
130- gpio_set_direction (self -> data_pin , GPIO_MODE_OUTPUT );
131- gpio_set_level (self -> data_pin , 0 );
132- }
133-
134- static void idle (ps2io_ps2_obj_t * self ) {
135- clk_hi (self );
136- data_hi (self );
137- }
138-
139- static void inhibit (ps2io_ps2_obj_t * self ) {
140- clk_lo (self );
141- data_hi (self );
51+ void ps2_reset (void ) {
52+ gpio_uninstall_isr_service ();
14253}
14354
14455static void delay_us (uint32_t t ) {
14556 common_hal_mcu_delay_us (t );
14657}
14758
59+ /* interrupt handling */
60+
14861static void IRAM_ATTR ps2_interrupt_handler (void * self_in ) {
14962 // Grab the current time first.
15063 uint64_t current_tick = port_get_raw_ticks (NULL );
@@ -222,11 +135,99 @@ static void IRAM_ATTR ps2_interrupt_handler(void *self_in) {
222135 }
223136}
224137
225- void common_hal_ps2io_ps2_construct (ps2io_ps2_obj_t * self ,
226- const mcu_pin_obj_t * data_pin , const mcu_pin_obj_t * clk_pin ) {
138+ static void enable_interrupt (ps2io_ps2_obj_t * self ) {
139+ // turn on falling edge interrupt
140+ gpio_install_isr_service (ESP_INTR_FLAG_IRAM );
141+ gpio_set_intr_type (self -> clk_pin , GPIO_INTR_NEGEDGE );
142+ gpio_isr_handler_add (self -> clk_pin , ps2_interrupt_handler , (void * )self );
143+ }
144+
145+ static void disable_interrupt (ps2io_ps2_obj_t * self ) {
146+ // turn off fallling edge interrupt
147+ gpio_isr_handler_remove (self -> clk_pin );
148+ }
149+
150+ static void resume_interrupt (ps2io_ps2_obj_t * self ) {
151+ self -> state = STATE_IDLE ;
152+ gpio_isr_handler_add (self -> clk_pin , ps2_interrupt_handler , (void * )self );
153+ }
154+
155+ /* gpio handling */
156+
157+ static void clk_hi (ps2io_ps2_obj_t * self ) {
158+ // external pull-up
159+ gpio_set_direction (self -> clk_pin , GPIO_MODE_INPUT );
160+ }
161+
162+ static bool wait_clk_lo (ps2io_ps2_obj_t * self , uint32_t us ) {
163+ clk_hi (self );
164+ delay_us (1 );
165+ while (gpio_get_level (self -> clk_pin ) && us ) {
166+ -- us ;
167+ delay_us (1 );
168+ }
169+ return us ;
170+ }
171+
172+ static bool wait_clk_hi (ps2io_ps2_obj_t * self , uint32_t us ) {
173+ clk_hi (self );
174+ delay_us (1 );
175+ while (!gpio_get_level (self -> clk_pin ) && us ) {
176+ -- us ;
177+ delay_us (1 );
178+ }
179+ return us ;
180+ }
181+
182+ static void clk_lo (ps2io_ps2_obj_t * self ) {
183+ gpio_set_direction (self -> clk_pin , GPIO_MODE_OUTPUT );
184+ gpio_set_level (self -> clk_pin , 0 );
185+ }
186+
187+ static void data_hi (ps2io_ps2_obj_t * self ) {
188+ // external pull-up
189+ gpio_set_direction (self -> data_pin , GPIO_MODE_INPUT );
190+ }
191+
192+ static bool wait_data_lo (ps2io_ps2_obj_t * self , uint32_t us ) {
193+ data_hi (self );
194+ delay_us (1 );
195+ while (gpio_get_level (self -> data_pin ) && us ) {
196+ -- us ;
197+ delay_us (1 );
198+ }
199+ return us ;
200+ }
201+
202+ static bool wait_data_hi (ps2io_ps2_obj_t * self , uint32_t us ) {
203+ data_hi (self );
204+ delay_us (1 );
205+ while (!gpio_get_level (self -> data_pin ) && us ) {
206+ -- us ;
207+ delay_us (1 );
208+ }
209+ return us ;
210+ }
211+
212+ static void data_lo (ps2io_ps2_obj_t * self ) {
213+ gpio_set_direction (self -> data_pin , GPIO_MODE_OUTPUT );
214+ gpio_set_level (self -> data_pin , 0 );
215+ }
216+
217+ static void idle (ps2io_ps2_obj_t * self ) {
227218 clk_hi (self );
228219 data_hi (self );
220+ }
221+
222+ static void inhibit (ps2io_ps2_obj_t * self ) {
223+ clk_lo (self );
224+ data_hi (self );
225+ }
226+
227+ /* ps2io module functions */
229228
229+ void common_hal_ps2io_ps2_construct (ps2io_ps2_obj_t * self ,
230+ const mcu_pin_obj_t * data_pin , const mcu_pin_obj_t * clk_pin ) {
230231 self -> clk_pin = (gpio_num_t )clk_pin -> number ;
231232 self -> data_pin = (gpio_num_t )data_pin -> number ;
232233 self -> state = STATE_IDLE ;
@@ -235,11 +236,11 @@ void common_hal_ps2io_ps2_construct(ps2io_ps2_obj_t* self,
235236 self -> bufposw = 0 ;
236237 self -> waiting_cmd_response = false;
237238
239+ idle (self );
240+ enable_interrupt (self );
241+
238242 claim_pin (clk_pin );
239243 claim_pin (data_pin );
240-
241- // set config will enable the interrupt.
242- ps2_set_config (self );
243244}
244245
245246bool common_hal_ps2io_ps2_deinited (ps2io_ps2_obj_t * self ) {
@@ -257,10 +258,6 @@ void common_hal_ps2io_ps2_deinit(ps2io_ps2_obj_t* self) {
257258 self -> data_pin = GPIO_NUM_MAX ;
258259}
259260
260- void ps2_reset (void ) {
261- gpio_uninstall_isr_service ();
262- }
263-
264261uint16_t common_hal_ps2io_ps2_get_len (ps2io_ps2_obj_t * self ) {
265262 return self -> bufcount ;
266263}
@@ -291,6 +288,8 @@ uint16_t common_hal_ps2io_ps2_clear_errors(ps2io_ps2_obj_t* self) {
291288
292289int16_t common_hal_ps2io_ps2_sendcmd (ps2io_ps2_obj_t * self , uint8_t b ) {
293290 disable_interrupt (self );
291+
292+ /* terminate a transmission if we have */
294293 inhibit (self );
295294 delay_us (100 );
296295
@@ -321,6 +320,7 @@ int16_t common_hal_ps2io_ps2_sendcmd(ps2io_ps2_obj_t* self, uint8_t b) {
321320 }
322321 }
323322
323+ /* Parity bit */
324324 delay_us (15 );
325325 if (parity ) {
326326 data_hi (self );
0 commit comments