--- /dev/null
+#include <system_def.h>
+#include <cpu_def.h>
+#include <LPC17xx.h>
+
+#ifndef PORT_PIN
+#warning PORT_PIN not defined, do not compile hal_gpio.c
+#else
+
+#include <hal_gpio.h>
+
+int hal_pin_conf_fnc(unsigned gpio, int fnc)
+{
+ __IO uint32_t *p = &(PINCON->PINSEL0);
+ uint32_t mask;
+
+ if(fnc & PORT_CONF_MODE_MASK)
+ fnc = __mfld2val(fnc, PORT_CONF_MODE_MASK);
+
+ p += hal_gpio_get_port_num(gpio)*2;
+ if(gpio & 0x10)
+ p++;
+ gpio &= 0x0f;
+ mask = 3 << gpio;
+
+ *p = (*p & ~ mask) | ((fnc << gpio) & mask);
+
+ return 0;
+}
+
+int hal_pin_conf_mode(unsigned gpio, int mode)
+{
+ __IO uint32_t *p = &(PINCON->PINSEL0);
+ uint32_t mask;
+
+ if(mode & PORT_CONF_MODE_MASK)
+ mode = __mfld2val(mode, PORT_CONF_MODE_MASK);
+
+ p += hal_gpio_get_port_num(gpio)*2;
+ if(gpio & 0x10)
+ p++;
+ gpio &= 0x0f;
+ mask = 3 << gpio;
+
+ *p = (*p & ~ mask) | ((mode << gpio) & mask);
+
+ return 0;
+}
+
+int hal_pin_conf_od(unsigned gpio, int od)
+{
+ uint32_t mask = 1 << (gpio & 0x1f);
+
+ if(od)
+ (&(PINCON->PINMODE_OD0))[hal_gpio_get_port_num(gpio)] |= mask;
+ else
+ (&(PINCON->PINMODE_OD0))[hal_gpio_get_port_num(gpio)] &= ~mask;
+
+ return 0;
+}
+
+int hal_pin_conf_set(unsigned gpio, int conf)
+{
+ gpio &= ~PORT_CONF_MASK;
+ hal_pin_conf_mode(gpio, conf & PORT_CONF_MODE_MASK);
+ hal_pin_conf_od(gpio, conf & PORT_CONF_OD_MASK);
+ if(conf & PORT_CONF_SET_DIR) {
+ if((conf & PORT_CONF_DIR_MASK) == (PORT_CONF_DIR_IN & PORT_CONF_DIR_MASK))
+ hal_gpio_direction_input(gpio);
+ else
+ hal_gpio_direction_output(gpio, conf & PORT_CONF_INIT_HIGH);
+ }
+ hal_pin_conf_fnc(gpio, conf & PORT_CONF_FNC_MASK);
+
+ return 0;
+}
+
+#endif
\ No newline at end of file
--- /dev/null
+#ifndef _HAL_GPIO_H_
+#define _HAL_GPIO_H_
+
+#include <system_def.h>
+#include <cpu_def.h>
+#include <LPC17xx.h>
+
+#define HAL_GPIO_PORT_BITS 3
+
+static inline
+GPIO_TypeDef *hal_gpio_get_port_base(unsigned port)
+{
+ char *p = (char*)GPIO0_BASE;
+ p += ((char*)GPIO1_BASE - (char*)GPIO0_BASE) * port;
+ return (GPIO_TypeDef *)p;
+}
+
+static inline
+unsigned hal_gpio_get_port_num(unsigned gpio)
+{
+ gpio >>= PORT_SHIFT;
+ return gpio & ((1 << HAL_GPIO_PORT_BITS) - 1);
+}
+
+static inline
+GPIO_TypeDef *hal_gpio_get_base(unsigned gpio)
+{
+ return hal_gpio_get_port_base(hal_gpio_get_port_num(gpio));
+}
+
+static inline
+int hal_gpio_get_value(unsigned gpio)
+{
+ return ((hal_gpio_get_base(gpio)->FIOPIN) >> (gpio & 0x1f)) & 1;
+}
+
+static inline
+void hal_gpio_set_value(unsigned gpio, int value)
+{
+ if(value)
+ hal_gpio_get_base(gpio)->FIOSET = 1 << (gpio & 0x1f);
+ else
+ hal_gpio_get_base(gpio)->FIOCLR = 1 << (gpio & 0x1f);
+}
+
+static inline
+int hal_gpio_direction_input(unsigned gpio)
+{
+ hal_gpio_get_base(gpio)->FIODIR &= ~(1 << (gpio & 0x1f));
+ return 0;
+}
+
+static inline
+int hal_gpio_direction_output(unsigned gpio, int value)
+{
+ hal_gpio_set_value(gpio, value);
+ hal_gpio_get_base(gpio)->FIODIR |= (1 << (gpio & 0x1f));
+ return 0;
+}
+
+int hal_pin_conf_fnc(unsigned gpio, int fnc);
+
+int hal_pin_conf_mode(unsigned gpio, int mode);
+
+int hal_pin_conf_od(unsigned gpio, int od);
+
+int hal_pin_conf_set(unsigned gpio, int conf);
+
+static inline
+int hal_pin_conf(unsigned gpio)
+{
+ return hal_pin_conf_set(gpio, gpio);
+}
+
+#endif /*_HAL_GPIO_H_*/
#define BIT(n) (1 << (n))
#endif
+#ifndef PORT_SHIFT
+#define PORT_SHIFT 5
+#endif
+#ifndef PORT_PIN
+#define PORT_PIN(p,n,conf) (((p)<<PORT_SHIFT) | (n) | (conf))
+#define PORT_CONF_MASK 0xff000000
+#endif
+
+#define PORT_CONF_DIR_MASK 0x01000000
+#define PORT_CONF_DIR_IN (0x00000000 | PORT_CONF_SET_DIR)
+#define PORT_CONF_DIR_OUT (0x01000000 | PORT_CONF_SET_DIR)
+
+#define PORT_CONF_INIT_MASK 0x02000000
+#define PORT_CONF_INIT_LOW 0x00000000
+#define PORT_CONF_INIT_HIGH 0x02000000
+
+#define PORT_CONF_OD_MASK 0x04000000
+#define PORT_CONF_OD_OFF 0x00000000
+#define PORT_CONF_OD_ON 0x04000000
+
+#define PORT_CONF_SET_DIR 0x08000000
+
+#define PORT_CONF_MODE_MASK 0x30000000
+#define PORT_CONF_MODE_PU 0x00000000
+#define PORT_CONF_MODE_REP 0x10000000
+#define PORT_CONF_MODE_NORM 0x20000000
+#define PORT_CONF_MODE_PD 0x30000000
+
+#define PORT_CONF_FNC_MASK 0xc0000000
+#define PORT_CONF_FNC_GPIO 0x00000000
+#define PORT_CONF_FNC_0 0x00000000
+#define PORT_CONF_FNC_1 0x40000000
+#define PORT_CONF_FNC_2 0x80000000
+#define PORT_CONF_FNC_3 0xc0000000
+
+#define PORT_CONF_GPIO_IN (PORT_CONF_DIR_IN | PORT_CONF_FNC_GPIO | PORT_CONF_MODE_NORM)
+#define PORT_CONF_GPIO_IN_PU (PORT_CONF_DIR_IN | PORT_CONF_FNC_GPIO | PORT_CONF_MODE_PU)
+#define PORT_CONF_GPIO_IN_PD (PORT_CONF_DIR_IN | PORT_CONF_FNC_GPIO | PORT_CONF_MODE_PD)
+#define PORT_CONF_GPIO_IN_REP (PORT_CONF_DIR_IN | PORT_CONF_FNC_GPIO | PORT_CONF_MODE_REP)
+
+#define PORT_CONF_GPIO_OUT_LO (PORT_CONF_DIR_OUT | PORT_CONF_FNC_GPIO | PORT_CONF_INIT_LOW)
+#define PORT_CONF_GPIO_OUT_HI (PORT_CONF_DIR_OUT | PORT_CONF_FNC_GPIO | PORT_CONF_INIT_HIGH)
+#define PORT_CONF_GPIO_OUT_LO_OD (PORT_CONF_GPIO_OUT_LO | PORT_CONF_OD_ON)
+#define PORT_CONF_GPIO_OUT_HI_OD (PORT_CONF_GPIO_OUT_LO | PORT_CONF_OD_ON)
+
+#define PORT_CONF_OUT_LO_NORM (PORT_CONF_DIR_OUT | PORT_CONF_INIT_LOW | PORT_CONF_MODE_NORM)
+#define PORT_CONF_IN_PU (PORT_CONF_DIR_IN | PORT_CONF_MODE_PU)
+
// Port Bit Definitions & Macros: Description - initial conditions
#define CAN1_RX_BIT BIT(0) // CAN1 RX
#define CAN1_TX_BIT BIT(1) // CAN1 TX
#define P1_14_UNUSED_BIT BIT(14) // P1.14 unused - low output
#define P1_15_UNUSED_BIT BIT(15) // P1.15 unused - low output
#define PWM1_BIT BIT(18) // motor pwm 0 / ADC0
+#define PWM1_PIN PORT_PIN(1,18,PORT_CONF_FNC_2|PORT_CONF_OUT_LO_NORM)
#define BLDC_HAL_A_BIT BIT(19) // motor HAL input A
+#define BLDC_HAL_A_PIN PORT_PIN(1,19,PORT_CONF_GPIO_IN_PU)
#define IRC_A_BIT BIT(20) // motor IRC channel A (MCI0)
#define IRC_M_BIT BIT(21) // motor IRC channel mark (GPIO)
#define BLDC_HAL_B_BIT BIT(22) // motor HAL input B
+#define BLDC_HAL_B_PIN PORT_PIN(1,22,PORT_CONF_GPIO_IN_PU)
#define IRC_B_BIT BIT(23) // motor IRC channel B (MCI1)
#define IRC_I_BIT BIT(24) // motor IRC index (MCI2)
#define BLDC_HAL_C_BIT BIT(25) // motor HAL input C
+#define BLDC_HAL_C_PIN PORT_PIN(1,25,PORT_CONF_GPIO_IN_PU)
#define PWM1_EN_BIT BIT(26) // motor pwm 0 enable
+#define PWM1_EN_PIN PORT_PIN(1,26,PORT_CONF_GPIO_OUT_LO)
#define PWM2_EN_BIT BIT(27) // motor pwm 1 enable
+#define PWM2_EN_PIN PORT_PIN(1,27,PORT_CONF_GPIO_OUT_LO)
#define PWM4_EN_BIT BIT(28) // motor pwm 2 enable
+#define PWM4_EN_PIN PORT_PIN(1,28,PORT_CONF_GPIO_OUT_LO)
#define PWM6_EN_BIT BIT(29) // motor pwm 3 enable
+#define PWM6_EN_PIN PORT_PIN(1,29,PORT_CONF_GPIO_OUT_LO)
#define ADC4_BIT BIT(30) // ADC4 tensometer
#define ADC5_BIT BIT(31) // ADC5 external input
#define RXD1_BIT BIT(1) // P2.1 RXD
#define CTS1_BIT BIT(2) // P2.2 CTS connected to RXD1
#define PWM4_BIT BIT(3) // P2.3 motor pwm 2 / ADC2
+#define PWM4_PIN PORT_PIN(2,3,PORT_CONF_FNC_1|PORT_CONF_OUT_LO_NORM)
#define DSR1_BIT BIT(4) // P2.4 DSR connected to TXD1
#define PWM6_BIT BIT(5) // P2.5 motor pwm 3 / ADC3
+#define PWM6_PIN PORT_PIN(2,5,PORT_CONF_FNC_1|PORT_CONF_OUT_LO_NORM)
#define LED1_BIT BIT(6) // P2.6 LED1 - error
#define RTS1_BIT BIT(7) // P2.7 RTS1 used as DIR1
#define AUX_OUT2_BIT BIT(8) // P2.8 auxual TLL port
// Port Bit Definitions & Macros: Description - initial conditions
#define PWM2_BIT BIT(25) // P3.25 motor pwm 1 / ADC1
+#define PWM2_PIN PORT_PIN(3,25,PORT_CONF_FNC_3|PORT_CONF_OUT_LO_NORM)
#define AUX_OUT1_BIT BIT(26) // P3.26 auxual TLL port
// Port Bit Definitions & Macros: Description - initial conditions