Skip to content

Commit

Permalink
Implemented PortIn, PortOut and PortInOut API #8
Browse files Browse the repository at this point in the history
Followingt test cases have been passed:
* PortOut (#24)
* PortOut PortIn (#9)
* PortInOut (#8)
  • Loading branch information
toyowata committed Jul 24, 2013
1 parent 9f47511 commit a125a25
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 21 deletions.
6 changes: 3 additions & 3 deletions libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#ifndef MBED_DEVICE_H
#define MBED_DEVICE_H

#define DEVICE_PORTIN 0
#define DEVICE_PORTOUT 0
#define DEVICE_PORTINOUT 0
#define DEVICE_PORTIN 1
#define DEVICE_PORTOUT 1
#define DEVICE_PORTINOUT 1

#define DEVICE_INTERRUPTIN 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct gpio_irq_s {

struct port_s {
__IO uint32_t *reg_dir;
__IO uint32_t *reg_mpin;
__IO uint32_t *reg_data;
PortName port;
uint32_t mask;
};
Expand Down
35 changes: 19 additions & 16 deletions libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX/port_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,35 @@
* limitations under the License.
*/

#warning TODO(@toyowata) This platform doesn't support PortIn, PortOut and PortInOut
#if 0

#include "port_api.h"
#include "pinmap.h"
#include "gpio_api.h"

PinName port_pin(PortName port, int pin_n) {
return (PinName)((port << PORT_SHIFT) | pin_n);
// LPC114 IOCON offset table [port][pin]

static uint8_t iocon_offset[4][12] = {
{0x0c,0x10,0x1c,0x2c,0x30,0x34,0x4c,0x50,0x60,0x64,0x68,0x74}, // PORT 0
{0x78,0x7c,0x80,0x90,0x94,0xa0,0xa4,0xa8,0x14,0x38,0x6c,0x98}, // PORT 1
{0x08,0x28,0x5c,0x8c,0x40,0x44,0x00,0x20,0x24,0x54,0x58,0x70}, // PORT 2
{0x84,0x88,0x9c,0xac,0x3c,0x48} // PORT 3
};

static PinName port_pin(PortName port, int pin) {
return (PinName)((port << PORT_SHIFT) | (pin << PIN_SHIFT) | (uint32_t)iocon_offset[port][pin]);
}

void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
obj->port = port;
obj->mask = mask;

LPC_GPIO_TypeDef *port_reg = (LPC_GPIO_TypeDef *)(LPC_GPIO0_BASE + ((int)port * 0x20));
LPC_GPIO_TypeDef *port_reg = ((LPC_GPIO_TypeDef *) (LPC_GPIO0_BASE + (port * 0x10000)));

port_reg->MASK = ~mask;

obj->reg_out = &port_reg->PIN;
obj->reg_in = &port_reg->PIN;
obj->reg_dir = &port_reg->DIR;
obj->reg_data = &port_reg->DATA;
obj->reg_dir = &port_reg->DIR;

uint32_t i;
// The function is set per pin: reuse gpio logic
for (i=0; i<32; i++) {
for (i=0; i<12; i++) {
if (obj->mask & (1<<i)) {
gpio_set(port_pin(obj->port, i));
}
Expand All @@ -51,7 +54,7 @@ void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
void port_mode(port_t *obj, PinMode mode) {
uint32_t i;
// The mode is set per pin: reuse pinmap logic
for (i=0; i<32; i++) {
for (i=0; i<12; i++) {
if (obj->mask & (1<<i)) {
pin_mode(port_pin(obj->port, i), mode);
}
Expand All @@ -66,10 +69,10 @@ void port_dir(port_t *obj, PinDirection dir) {
}

void port_write(port_t *obj, int value) {
*obj->reg_mpin = value;
*obj->reg_data = (value & obj->mask);
}

int port_read(port_t *obj) {
return (*obj->reg_mpin);
return (*obj->reg_data & obj->mask);
}
#endif

9 changes: 9 additions & 0 deletions libraries/tests/mbed/portinout/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
#define P2_2 (1 << 0) // p2.0 -> p26
#define PORT_2 Port2

#elif defined(TARGET_LPC1114)
#define P1_1 (1 << 9) // p0.9
#define P1_2 (1 << 8) // p0.8
#define PORT_1 Port0

#define P2_1 (1 << 1) // p1.1
#define P2_2 (1 << 0) // p1.0
#define PORT_2 Port1

#elif defined(TARGET_KL25Z)
#define P1_1 (1 << 1) // PTA1
#define P1_2 (1 << 2) // PTA2
Expand Down
2 changes: 1 addition & 1 deletion libraries/tests/mbed/portout/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# define LED2 (1 << 20) // P1.20
# define LED3 (1 << 21) // P1.21
# define LED4 (1 << 23) // P1.23
# elif defined(TARGET_LPC11U24)
# elif defined(TARGET_LPC11U24) || defined(TARGET_LPC1114)
# define LED1 (1 << 8) // P1.8
# define LED2 (1 << 9) // P1.9
# define LED3 (1 << 10) // P1.10
Expand Down
9 changes: 9 additions & 0 deletions libraries/tests/mbed/portout_portin/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
#define P2_2 (1 << 0) // p2.0 -> p26
#define PORT_2 Port2

#elif defined(TARGET_LPC1114)
#define P1_1 (1 << 9) // p0.9
#define P1_2 (1 << 8) // p0.8
#define PORT_1 Port0

#define P2_1 (1 << 1) // p1.1
#define P2_2 (1 << 0) // p1.0
#define PORT_2 Port1

#elif defined(TARGET_KL25Z)
#define P1_1 (1 << 1) // PTA1
#define P1_2 (1 << 2) // PTA2
Expand Down

0 comments on commit a125a25

Please sign in to comment.