dev/driver #4
							
								
								
									
										34
									
								
								include/gpio.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								include/gpio.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
#ifndef __GPIO_H__
 | 
			
		||||
#define __GPIO_H__
 | 
			
		||||
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
 | 
			
		||||
#include "driver.h"
 | 
			
		||||
 | 
			
		||||
#define IOCTL_CMD_SET_DIRECTION 0
 | 
			
		||||
 | 
			
		||||
int gpio_open(const struct driver *drv);
 | 
			
		||||
int gpio_close(const struct driver *drv);
 | 
			
		||||
 | 
			
		||||
int gpio_read(const struct driver *drv, char *buffer, unsigned int len);
 | 
			
		||||
int gpio_write(const struct driver *drv, const char *buffer, unsigned int len);
 | 
			
		||||
 | 
			
		||||
enum direction {
 | 
			
		||||
    IN = 0,
 | 
			
		||||
    OUT
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct gpio {
 | 
			
		||||
    int pin;
 | 
			
		||||
    enum direction dir;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static const struct driver_fp gpio_fp = {
 | 
			
		||||
    .open = gpio_open,
 | 
			
		||||
    .close = gpio_close,
 | 
			
		||||
    .read = gpio_read,
 | 
			
		||||
    .write = gpio_write,
 | 
			
		||||
    .ioctl = NULL
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										63
									
								
								src/platform/nrf52/gpio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								src/platform/nrf52/gpio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stddef.h>
 | 
			
		||||
 | 
			
		||||
#include "gpio.h"
 | 
			
		||||
 | 
			
		||||
#include "nrf_gpio.h"
 | 
			
		||||
 | 
			
		||||
int gpio_open(const struct driver *drv)
 | 
			
		||||
{
 | 
			
		||||
    assert(NULL != drv);
 | 
			
		||||
 | 
			
		||||
    struct gpio *this = (struct gpio *)(drv->dev);
 | 
			
		||||
 | 
			
		||||
    if(this->dir == OUT) {
 | 
			
		||||
        nrf_gpio_cfg_output((uint32_t)(this->pin));
 | 
			
		||||
    nrf_gpio_pin_clear(this->pin);
 | 
			
		||||
    }
 | 
			
		||||
    // FIXME: implement input configuration
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int gpio_close(const struct driver *drv)
 | 
			
		||||
{
 | 
			
		||||
    assert(NULL != drv);
 | 
			
		||||
 | 
			
		||||
    struct gpio *this = (struct gpio *)(drv->dev);
 | 
			
		||||
    nrf_gpio_pin_clear(this->pin);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int gpio_read(const struct driver *drv, char *buffer, unsigned int len)
 | 
			
		||||
{
 | 
			
		||||
    assert(NULL != drv);
 | 
			
		||||
 | 
			
		||||
    if(len == 0) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct gpio *this = (struct gpio *)(drv->dev);
 | 
			
		||||
 | 
			
		||||
    uint32_t value = nrf_gpio_pin_read(this->pin);
 | 
			
		||||
    buffer[0] = value + 0x30;   // ascii convert
 | 
			
		||||
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int gpio_write(const struct driver *drv, const char *buffer, unsigned int len)
 | 
			
		||||
{
 | 
			
		||||
    assert((NULL != drv) && (NULL != buffer));
 | 
			
		||||
 | 
			
		||||
    if(len == 0) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    struct gpio *this = (struct gpio *)(drv->dev);
 | 
			
		||||
 | 
			
		||||
    nrf_gpio_pin_write(this->pin, buffer[0] - 0x30);
 | 
			
		||||
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user