mirror of https://github.com/ARMmbed/mbed-os.git
* updated with port driver apis. Tested and working.
parent
5d0a913591
commit
c47f60bb23
|
@ -22,7 +22,8 @@ extern "C" {
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
PortA = 0,
|
PortA = 0,
|
||||||
PortB = 1
|
PortB = 1,
|
||||||
|
PortC = 2
|
||||||
} PortName;
|
} PortName;
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
|
|
|
@ -43,6 +43,9 @@ struct port_s {
|
||||||
|
|
||||||
PortName port;
|
PortName port;
|
||||||
uint32_t mask;
|
uint32_t mask;
|
||||||
|
uint8_t powersave;
|
||||||
|
uint8_t mode;
|
||||||
|
uint8_t direction;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct serial_s {
|
struct serial_s {
|
||||||
|
@ -66,28 +69,7 @@ struct analogin_s {
|
||||||
};
|
};
|
||||||
/*
|
/*
|
||||||
struct pwmout_s {
|
struct pwmout_s {
|
||||||
__IO uint32_t *MR;
|
};*/
|
||||||
PWMName pwm;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct serial_s {
|
|
||||||
LPC_UART_TypeDef *uart;
|
|
||||||
int index;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct dac_s {
|
|
||||||
DACName dac;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct can_s {
|
|
||||||
LPC_CAN_TypeDef *dev;
|
|
||||||
int index;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct i2c_s {
|
|
||||||
LPC_I2C_TypeDef *i2c;
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
struct spi_s {
|
struct spi_s {
|
||||||
Sercom *spi;
|
Sercom *spi;
|
||||||
|
|
|
@ -16,24 +16,148 @@
|
||||||
#include "port_api.h"
|
#include "port_api.h"
|
||||||
#include "pinmap.h"
|
#include "pinmap.h"
|
||||||
#include "gpio_api.h"
|
#include "gpio_api.h"
|
||||||
|
#include "port.h"
|
||||||
|
|
||||||
|
#define PORTA_MASK 0xDBDFFFF3 // mask for available pins in Port A
|
||||||
|
#define PORTB_MASK 0xC0C3C30D // mask for available pins in Port B
|
||||||
|
#define PORTC_MASK 0x000D0000 // mask for available pins in Port C
|
||||||
|
|
||||||
|
#define PORTA_START 0 // 32 pins in a port
|
||||||
|
#define PORTB_START 32
|
||||||
|
#define PORTC_START 64
|
||||||
|
|
||||||
|
uint32_t start_pin(PortName port)
|
||||||
|
{
|
||||||
|
switch (port) {
|
||||||
|
case PortA:
|
||||||
|
return PORTA_START;
|
||||||
|
case PortB:
|
||||||
|
return PORTB_START;
|
||||||
|
case PortC:
|
||||||
|
return PORTC_START;
|
||||||
|
default:
|
||||||
|
return NC;
|
||||||
|
}
|
||||||
|
}
|
||||||
void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
|
void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
|
||||||
{
|
{
|
||||||
|
MBED_ASSERT(obj);
|
||||||
|
struct port_config pin_conf;
|
||||||
|
int i, j;
|
||||||
|
uint32_t start;
|
||||||
|
|
||||||
|
PortGroup *const port_base = (PortGroup*)port_get_group_from_gpio_pin(port * 32); // 32 pins in port // function reused to get the port base
|
||||||
|
switch (port) {
|
||||||
|
case PortA:
|
||||||
|
obj->mask = (uint32_t)mask & PORTA_MASK;
|
||||||
|
break;
|
||||||
|
case PortB:
|
||||||
|
obj->mask = (uint32_t)mask & PORTB_MASK;
|
||||||
|
break;
|
||||||
|
case PortC:
|
||||||
|
obj->mask = (uint32_t)mask & PORTC_MASK;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
start = start_pin(port);
|
||||||
|
if(start == NC)
|
||||||
|
return;
|
||||||
|
obj->port = port;
|
||||||
|
obj->direction = dir;
|
||||||
|
port_get_config_defaults(&pin_conf);
|
||||||
|
obj->powersave = pin_conf.powersave;
|
||||||
|
obj->mode = PORT_PIN_PULL_UP;
|
||||||
|
switch (dir) {
|
||||||
|
case PIN_INPUT :
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_INPUT;
|
||||||
|
break;
|
||||||
|
case PIN_OUTPUT:
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
|
||||||
|
break;
|
||||||
|
case PIN_INPUT_OUTPUT:
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_OUTPUT_WTH_READBACK;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (i = start, j = 0; i < (start + 32); i++, j++) {
|
||||||
|
if (obj->mask & (1<<j)) {
|
||||||
|
port_pin_set_config((PinName)i, &pin_conf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj->OUTCLR = &port_base->OUTCLR.reg;
|
||||||
|
obj->OUTSET = &port_base->OUTSET.reg;
|
||||||
|
obj->IN = &port_base->IN.reg;
|
||||||
|
obj->OUT = &port_base->OUT.reg;
|
||||||
}
|
}
|
||||||
|
|
||||||
void port_mode(port_t *obj, PinMode mode)
|
void port_mode(port_t *obj, PinMode mode)
|
||||||
{
|
{
|
||||||
|
MBED_ASSERT(obj);
|
||||||
|
int i, j;
|
||||||
|
uint32_t start;
|
||||||
|
start = start_pin(obj->port);
|
||||||
|
if(start == NC)
|
||||||
|
return;
|
||||||
|
for (i = start, j = 0; i < (start + 32); i++, j++) {
|
||||||
|
if (obj->mask & (1<<j)) {
|
||||||
|
pin_mode((PinName)i , mode);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void port_dir(port_t *obj, PinDirection dir)
|
void port_dir(port_t *obj, PinDirection dir)
|
||||||
{
|
{
|
||||||
|
MBED_ASSERT(obj);
|
||||||
|
struct port_config pin_conf;
|
||||||
|
int i, j;
|
||||||
|
uint32_t start;
|
||||||
|
start = start_pin(obj->port);
|
||||||
|
if(start == NC)
|
||||||
|
return;
|
||||||
|
obj->direction = dir;
|
||||||
|
pin_conf.input_pull = obj->mode;
|
||||||
|
pin_conf.powersave = obj->powersave;
|
||||||
|
switch (dir) {
|
||||||
|
case PIN_INPUT :
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_INPUT;
|
||||||
|
break;
|
||||||
|
case PIN_OUTPUT:
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_OUTPUT;
|
||||||
|
break;
|
||||||
|
case PIN_INPUT_OUTPUT:
|
||||||
|
pin_conf.direction = PORT_PIN_DIR_OUTPUT_WTH_READBACK;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for (i = start, j = 0; i < (start + 32); i++, j++) {
|
||||||
|
if (obj->mask & (1<<j)) {
|
||||||
|
port_pin_set_config((PinName)i, &pin_conf);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void port_write(port_t *obj, int value)
|
void port_write(port_t *obj, int value)
|
||||||
{
|
{
|
||||||
|
MBED_ASSERT(obj);
|
||||||
|
int i;
|
||||||
|
uint32_t start;
|
||||||
|
start = start_pin(obj->port);
|
||||||
|
if(start == NC)
|
||||||
|
return;
|
||||||
|
for (i = 0; i < 32 ; i++) {
|
||||||
|
if (obj->mask & (1<<i)) {
|
||||||
|
if (value & (1<<i)) {
|
||||||
|
*obj->OUTSET = 1 << i;
|
||||||
|
} else {
|
||||||
|
*obj->OUTCLR = 1 << i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int port_read(port_t *obj)
|
int port_read(port_t *obj)
|
||||||
{
|
{
|
||||||
return 0;
|
MBED_ASSERT(obj);
|
||||||
|
if (obj->direction == PIN_INPUT) {
|
||||||
|
return (*obj->IN & obj->mask);
|
||||||
|
} else {
|
||||||
|
return (*obj->OUT & obj->mask);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue