Skip to content
This repository was archived by the owner on Nov 19, 2022. It is now read-only.

Commit 2fa485a

Browse files
committed
Reimplement GPIO functions
1 parent 845e6c6 commit 2fa485a

File tree

7 files changed

+161
-215
lines changed

7 files changed

+161
-215
lines changed

cores/scirocco/WInterrupts.c

Lines changed: 0 additions & 76 deletions
This file was deleted.

cores/scirocco/WInterrupts.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define _WIRING_INTERRUPTS_
2121

2222
#include <stdint.h>
23+
#include "wiring_private.h"
2324

2425
#ifdef __cplusplus
2526
extern "C" {
@@ -40,15 +41,21 @@ typedef void (*voidFuncPtr)(void);
4041
* \brief Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs.
4142
* Replaces any previous function that was attached to the interrupt.
4243
*/
43-
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode);
44+
inline void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode) { w_attach_interrupt(pin, callback, mode); }
4445

4546
/*
4647
* \brief Turns off the given interrupt.
4748
*/
48-
void detachInterrupt(uint32_t pin);
49+
inline void detachInterrupt(uint32_t pin) { w_detach_interrupt(pin); }
4950

5051
#ifdef __cplusplus
5152
}
53+
54+
inline void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode, uint32_t pinmode, uint32_t extraflag=0)
55+
{
56+
w_configure_gpio_interrupt(pin, callback, mode, pinmode, extraflag);
57+
}
58+
5259
#endif
5360

5461
#endif

cores/scirocco/wiring_digital.c

Lines changed: 0 additions & 67 deletions
This file was deleted.

cores/scirocco/wiring_digital.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#ifndef _WIRING_DIGITAL_
2020
#define _WIRING_DIGITAL_
2121

22+
#include "wiring_private.h"
23+
2224
#ifdef __cplusplus
2325
extern "C" {
2426
#endif
@@ -31,7 +33,7 @@
3133
* \param ulPin The number of the pin whose mode you wish to set
3234
* \param ulMode Can be INPUT, OUTPUT, INPUT_PULLUP or INPUT_PULLDOWN
3335
*/
34-
extern void pinMode( uint32_t dwPin, uint32_t dwMode ) ;
36+
inline void pinMode( uint32_t dwPin, uint32_t dwMode ) { w_configure_gpio_interrupt(dwPin, NULL, -1, dwMode, 0); }
3537

3638
/**
3739
* \brief Write a HIGH or a LOW value to a digital pin.
@@ -53,7 +55,7 @@ extern void pinMode( uint32_t dwPin, uint32_t dwMode ) ;
5355
* \param dwPin the pin number
5456
* \param dwVal HIGH or LOW
5557
*/
56-
extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ;
58+
inline void digitalWrite( uint32_t dwPin, uint32_t dwVal ) { w_digital_write(dwPin, dwVal); }
5759

5860
/**
5961
* \brief Reads the value from a specified digital pin, either HIGH or LOW.
@@ -62,7 +64,7 @@ extern void digitalWrite( uint32_t dwPin, uint32_t dwVal ) ;
6264
*
6365
* \return HIGH or LOW
6466
*/
65-
extern int digitalRead( uint32_t ulPin ) ;
67+
inline int digitalRead( uint32_t ulPin ) { return w_digital_read(ulPin); }
6668

6769
#ifdef __cplusplus
6870
}

cores/scirocco/wiring_private.c

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,102 @@
11
#include <zephyr.h>
22
#include <gpio.h>
33
#include <variant.h>
4+
#include "wiring_digital.h"
5+
#include "wiring_private.h"
6+
#include "WInterrupts.h"
7+
8+
int w_pin_mode[W_GPIO_PIN_NUM] = { 0 };
9+
struct w_gpio_callbacks w_callbacks[W_GPIO_PIN_NUM] = { 0 };
10+
11+
static void gpio_handler(struct device *port, struct gpio_callback *cb, u32_t pins)
12+
{
13+
for(int i=0; i<W_GPIO_PIN_NUM; i++)
14+
{
15+
if( (&w_callbacks[i].z_callback) == cb) {
16+
w_callbacks[i].callback();
17+
}
18+
}
19+
}
20+
21+
void w_configure_gpio_interrupt(uint32_t pin, voidFuncPtr callback, uint32_t intmode, uint32_t pinmode, uint32_t extraflag)
22+
{
23+
int pinconf = 0;
24+
25+
switch(pinmode)
26+
{
27+
case INPUT:
28+
pinconf = GPIO_DIR_IN;
29+
break;
30+
case OUTPUT:
31+
pinconf = GPIO_DIR_OUT;
32+
break;
33+
case INPUT_PULLUP:
34+
pinconf = GPIO_DIR_IN | GPIO_PUD_PULL_UP;
35+
break;
36+
case INPUT_PULLDOWN:
37+
pinconf = GPIO_DIR_IN | GPIO_PUD_PULL_DOWN;
38+
break;
39+
}
40+
41+
if(callback) {
42+
switch(intmode)
43+
{
44+
case LOW:
45+
pinconf |= (GPIO_INT | GPIO_INT_LEVEL | GPIO_INT_ACTIVE_LOW);
46+
break;
47+
case HIGH:
48+
pinconf |= (GPIO_INT | GPIO_INT_LEVEL | GPIO_INT_ACTIVE_HIGH);
49+
break;
50+
case CHANGE:
51+
pinconf |= (GPIO_INT | GPIO_INT_EDGE | GPIO_INT_DOUBLE_EDGE);
52+
break;
53+
case FALLING:
54+
pinconf |= (GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_LOW);
55+
break;
56+
case RISING:
57+
pinconf |= (GPIO_INT | GPIO_INT_EDGE | GPIO_INT_ACTIVE_HIGH);
58+
break;
59+
}
60+
}
61+
62+
pinconf |= extraflag;
63+
64+
w_pin_mode[pin] = pinmode;
65+
gpio_pin_configure(device_get_binding(W_PIN2PORT(pin)), W_PIN2PORTPIN(pin), pinconf);
66+
67+
if(callback) {
68+
w_callbacks[pin].callback = callback;
69+
gpio_init_callback(&w_callbacks[pin].z_callback, gpio_handler, BIT(W_PIN2PORTPIN(pin)));
70+
gpio_add_callback(device_get_binding(W_PIN2PORT(pin)), &w_callbacks[pin].z_callback);
71+
gpio_pin_enable_callback(device_get_binding(W_PIN2PORT(pin)), pin);
72+
}
73+
else {
74+
w_detach_interrupt(pin);
75+
}
76+
77+
}
78+
79+
void w_attach_interrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode)
80+
{
81+
uint32_t pinmode = w_pin_mode[pin];
82+
w_configure_gpio_interrupt(pin, callback, mode, pinmode, 0);
83+
}
84+
85+
void w_detach_interrupt(uint32_t pin)
86+
{
87+
gpio_pin_disable_callback(device_get_binding(W_PIN2PORT(pin)), pin);
88+
gpio_remove_callback(device_get_binding(W_PIN2PORT(pin)), &w_callbacks[pin].z_callback);
89+
}
90+
91+
void w_digital_write( uint32_t ulPin, uint32_t ulVal )
92+
{
93+
gpio_pin_write(device_get_binding(W_PIN2PORT(ulPin)), W_PIN2PORTPIN(ulPin), ulVal ? 1 : 0);
94+
}
95+
96+
int w_digital_read( uint32_t ulPin )
97+
{
98+
u32_t value;
99+
gpio_pin_read(device_get_binding(W_PIN2PORT(ulPin)), W_PIN2PORTPIN(ulPin), &value);
100+
return value ? 1 : 0;
101+
}
4102

5-
int gpio_configs[GPIO_PIN_NO] = { 0xF000 };
6-
struct gpio_callback gpio_cb[GPIO_PIN_NO];

cores/scirocco/wiring_private.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,39 @@
2525
#include <stdint.h>
2626
#include <stdbool.h>
2727

28-
#include "WInterrupts.h"
2928
#include "wiring_constants.h"
3029
#include "variant.h"
3130

3231
#ifdef __cplusplus
3332
extern "C" {
3433
#endif
35-
extern int gpio_configs[GPIO_PIN_NO];
36-
37-
extern struct gpio_callback gpio_cb[GPIO_PIN_NO];
38-
39-
struct uart_device {
40-
int (*input)( uint8_t c);
41-
void (*init)(void*, uint32_t, uint8_t, uint8_t, uint8_t, uint8_t);
42-
void (*set_input)(void*, int (*input)(unsigned char));
43-
void (*writeb)(void*, unsigned char);
44-
uint8_t (*busy)(void*);
45-
int (*txbuffer_availables)(void*);
46-
void (*deinit)(void*);
47-
void* portinfo;
48-
uint8_t received;
34+
35+
#define W_GPIO_PINS_PER_PORT 32
36+
#define W_GPIO_PORT_NUM (sizeof(gpio_port_names)/sizeof(const char*))
37+
#define W_PIN2PORT(x) (gpio_port_names[x / W_GPIO_PINS_PER_PORT])
38+
#define W_PIN2PORTPIN(x) (x % W_GPIO_PINS_PER_PORT)
39+
#define W_GPIO_PIN_NUM (W_GPIO_PINS_PER_PORT * W_GPIO_PORT_NUM)
40+
41+
typedef void (*voidFuncPtr)(void);
42+
43+
struct w_gpio_callbacks {
44+
struct gpio_callback z_callback;
45+
voidFuncPtr callback;
4946
};
5047

48+
static const char* gpio_port_names[] = GPIO_PORT_NAMES;
49+
50+
extern int w_pin_mode[W_GPIO_PIN_NUM];
51+
extern struct w_gpio_callbacks w_callbacks[W_GPIO_PIN_NUM];
52+
53+
extern void w_digital_write( uint32_t ulPin, uint32_t ulVal );
54+
extern int w_digital_read( uint32_t ulPin );
55+
extern void w_attach_interrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode);
56+
extern void w_detach_interrupt(uint32_t pin);
57+
58+
extern void w_configure_gpio_interrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode, uint32_t pinmode, uint32_t extraflag);
59+
60+
5161
struct spi_device {
5262
int (*init)(void*);
5363
int (*configure)(void*, bool, bool, bool, uint32_t);

0 commit comments

Comments
 (0)