W7500 mbed porting Guide ( 2/6 )
2015. 10. 8. 15:00ㆍ2018년 이전 관심사/mbed
반응형
W7500 mbed SDK Porting Guide
GPIO Porting
예제 코드
#include "mbed.h"
////MBED Example 1 LED CONTROL AND Serial Output
int main()
{
DigitalOut myled(LED1);
myled = 0;
while(1){
myled = 1; // LED is ON
wait(1); // 1 sec
myled = 0; // LED is OFF
wait(1); // 1 sec
}
}
수정할 필요 없는 코드
api 폴더, common 폴더, hal 폴더에 있는 내용을 수정하면 호환성 문제가 발생 할 수 있으므로 수정하지 않는 것을 권한다.
GPIO Porting시 사용하는 아래 파일들은 수정할 필요가 없다.
- api/DigitalOut.h
- common/gpio.c
- hal/gpio_api.h
targets/hal/TARGET_WIZNET/TARGET_W7500x/TARGET_WIZwiki_W7500/PortNames.h
typedef enum {
PortA = 0,
PortB = 1,
PortC = 2,
PortD = 3,
} PortName;
targets/hal/TARGET_WIZNET/TARGET_W7500x/TARGET_WIZwiki_W7500/PinNames.h
- W7500 Chip의 Pin 정보를 mbed SDK를 위한 형태로 변경
- PinName 규칙 (AF[9:8] + PORT[5:4] + PIN[3:0])
- ex) PC_10 = 0x32A, // 0xx:U_TXD2, 1xx:GPIOC_10, 2xx:PWM2, 3xx:AIN5
// See W7500x_hal_gpio.h for values of MODE, PUPD and AFNUM
#define WIZ_PIN_DATA(MODE, PUPD, AFNUM) ((int)(((AFNUM) << 8) | ((PUPD) << 4) | ((MODE) << 0)))
#define WIZ_PIN_MODE(X) (((X) >> 0) & 0x0F)
#define WIZ_PIN_PUPD(X) (((X) >> 4) & 0x0F)
#define WIZ_PIN_AFNUM(X) (((X) >> 8) & 0x0F)
#define WIZ_MODE_INPUT (0)
#define WIZ_MODE_OUTPUT (1)
#define WIZ_MODE_AF (2)
#define WIZ_GPIO_NOPULL (0) /*!< No Pull-up or Pull-down activation */
#define WIZ_GPIO_PULLDOWN (1) /*!< Pull-down activation */
#define WIZ_GPIO_PULLUP (2) /*!< Pull-up activation */
#define WIZ_GPIO_OPEN_DRAIN (3) /*!< Open Drain activation */
#define WIZ_AFNUM(X)(((uint32_t)(X) >> 8) & 0xF) // AF number (0=AF0, 1=AF1, 2=AF2, 3=AF3)
#define WIZ_PORT(X) (((uint32_t)(X) >> 4) & 0xF) // port number (0=A, 1=B, 2=C, 3=D)
#define WIZ_PIN(X) ((uint32_t)(X) & 0xF) // pin number
typedef enum {
PIN_INPUT,
PIN_OUTPUT
} PinDirection;
typedef enum {
// W7500x Pin Names (AF[9:8] + PORT[5:4] + PIN[3:0])
PA_0 = 0x000,
PA_1 = 0x001,
PA_2 = 0x002,
PA_3 = 0x003,
PA_4 = 0x004,
PA_5 = 0x005,
PA_6 = 0x006,
PA_7 = 0x007,
PA_8 = 0x008,
PA_9 = 0x009,
PA_10 = 0x00A,
PA_11 = 0x00B,
PA_12 = 0x00C,
PA_13 = 0x00D,
PA_14 = 0x00E,
PA_15 = 0x00F,
PB_0 = 0x010, //SSEL1/SD_SEL
PB_1 = 0x011, //SCLK1/SD_CLK
PB_2 = 0x012, //MISO1/SD_MISO
PB_3 = 0x013, //MOSI1/SD_MOSI
PB_4 = 0x014,
PB_5 = 0x015,
PB_6 = 0x016,
PB_7 = 0x017,
PB_8 = 0x018,
PB_9 = 0x019,
PB_10 = 0x01A,
PB_11 = 0x01B,
PB_12 = 0x01C,
PB_13 = 0x01D,
PB_14 = 0x01E,
PB_15 = 0x01F,
PC_0 = 0x120, // 0xx:U_CTS1, 1xx:GPIOC_0, 2xx:PWM0
PC_1 = 0x121, // 0xx:U_RTS1, 1xx:GPIOC_1, 2xx:PWM1
PC_2 = 0x022,
PC_3 = 0x023,
PC_4 = 0x124, // 0xx:SDA1, 1xx:GPIOC_4, 2xx:PWM4
PC_5 = 0x025,
PC_6 = 0x026,
PC_7 = 0x027,
PC_8 = 0x128, // 0xx:PWM0, 1xx:GPIOC_8, 2xx:SCL0, 3xx:AIN7
PC_9 = 0x129, // 0xx:PWM1, 1xx:GPIOC_9, 2xx:SDA0, 3xx:AIN6
PC_10 = 0x32A, // 0xx:U_TXD2, 1xx:GPIOC_10, 2xx:PWM2, 3xx:AIN5
PC_11 = 0x32B, // 0xx:U_RXD2, 1xx:GPIOC_11, 2xx:PWM3, 3xx:AIN4
PC_12 = 0x32C, // 0xx:AIN3, 1xx:GPIOC_12, 2xx:SSEL0, 3xx:AIN3
PC_13 = 0x32D, // 0xx:AIN2, 1xx:GPIOC_13, 2xx:SCLK0, 3xx:AIN2
PC_14 = 0x32E, // 0xx:AIN1, 1xx:GPIOC_14, 2xx:MISO0, 3xx:AIN1
PC_15 = 0x32F, // 0xx:AIN0, 1xx:GPIOC_15, 2xx:MOSI0, 3xx:AIN0
PD_0 = 0x030,
PD_1 = 0x031,
PD_2 = 0x032,
PD_3 = 0x033,
PD_4 = 0x034,
// Arduino connector namings
A0 = PC_15, // AIN0
A1 = PC_14, // AIN1
A2 = PC_13, // AIN2
A3 = PC_12, // AIN3
A4 = PC_11, // AIN4
A5 = PC_10, // AIN5
D0 = PA_14, // U_TXD0
D1 = PA_13, // U_RXD0
D2 = PC_1, // GPIOC_1, EXTI
D3 = PC_0, // GPIOC_0, EXTI
D4 = PA_2, // GPIOA_2,
D5 = PA_1, // GPIOA_1,
D6 = PA_0, // GPIOA_0,
D7 = PC_6, // GPIOC_6, EXTI
D8 = PC_7, // GPIOC_7, EXTI
D9 = PC_4, // GPIOC_4, EXTI
D10 = PA_5, // SSEL0
D11 = PA_8, // MOSI0
D12 = PA_7, // MISO0
D13 = PA_6, // SCLK0
D14 = PA_10, // SDA0
D15 = PA_9, // SCL0
// Generic signals namings
LED_RED = PC_8,
LED_GREEN = PC_9,
LED_BLUE = PC_5,
LED1 = LED_RED,
LED2 = LED_GREEN,
LED3 = LED_BLUE,
// USER_BUTTON = PC_13,
SERIAL_TX = PC_2, // U_TXD1
SERIAL_RX = PC_3, // U_RXD1
USBTX = SERIAL_TX,
USBRX = SERIAL_RX,
I2C_SCL = PA_9, // SCL0
I2C_SDA = PA_10, // SDA0
SPI_MOSI = PA_8, // MISO0
SPI_MISO = PA_7, // MOSI0
SPI_SCK = PA_9, // SCL0
SPI_CS = PA_5, // SSEL0
SD_SEL = PB_0, // SSEL1
SD_CLK = PB_1, // SCL1
SD_MISO = PB_2, // MOSI1
SD_MOSI = PB_3, // MISO1
// PWM_OUT = PC_7,
// Not connected
NC = (int)0xFFFFFFFF
} PinName;
targets/hal/TARGET_WIZNET/TARGET_W7500x/gpio_object.h
- PinNames.h에 정의한 Pin 정보들을 이용하여 GPIO pin을 read, write 하기 위한 Function 구현
typedef struct {
PinName pin;
uint32_t mask;
__IO uint32_t *reg_data_in;
} gpio_t;
extern uint32_t Get_GPIO_BaseAddress(uint32_t port_idx);
static inline void gpio_write(gpio_t *obj, int value) {
MBED_ASSERT(obj->pin != (PinName)NC);
uint32_t port_index = WIZ_PORT(obj->pin);
uint32_t pin_index = WIZ_PIN(obj->pin);
uint32_t gpio_add = Get_GPIO_BaseAddress(port_index);
GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
if (value)
{
HAL_GPIO_SetBits(gpio,(0x01 << pin_index));
}
else
{
HAL_GPIO_ResetBits(gpio,(0x01 << pin_index));
}
}
static inline int gpio_read(gpio_t *obj) {
MBED_ASSERT(obj->pin != (PinName)NC);
return ((*obj->reg_data_in & obj->mask) ? 1 : 0);
}
static inline int gpio_is_connected(const gpio_t *obj) {
return obj->pin != (PinName)NC;
}
targets/hal/TARGET_WIZNET/TARGET_W7500x/pinmap.c
- Pin들을 설정하기 위한 함수 구현
- Output,Input,Alternative Fuction을 설정 할 수 있음.
- Pin Mode 설정을 위한 함수 구현 —> Pull up, Pull down, Open Drain
- 실질적인 pin을 Control 하기 위한 기능들이 구현되어 있음
// GPIO mode look-up table
// It have to same with PinMode index in "PinNames.h"
static const uint32_t gpio_pupd[4] = {
GPIO_NO_PUPD, // PullNone
GPIO_PuPd_DOWN, // PullDown
GPIO_PuPd_UP, // PullUp
GPIO_OD // OpenDrain
};
uint32_t Get_GPIO_BaseAddress(uint32_t port_idx)
{
uint32_t gpio_add = 0;
switch(port_idx) {
case PortA:
gpio_add = GPIOA_BASE;
break;
case PortB:
gpio_add = GPIOB_BASE;
break;
case PortC:
gpio_add = GPIOC_BASE;
break;
case PortD:
gpio_add = GPIOD_BASE;
break;
default:
error("Pinmap error: wrong port number.");
break;
}
return gpio_add;
}
/**
* Configure pin (input, output, alternate function or analog) + output speed + AF
*/
void pin_function(PinName pin, int data) {
MBED_ASSERT(pin != (PinName)NC);
// Get the pin informations
uint32_t mode = WIZ_PIN_MODE(data);
uint32_t pupd = WIZ_PIN_PUPD(data);
uint32_t afnum;
if( mode == WIZ_MODE_AF )
afnum = WIZ_PIN_AFNUM(data);
else
afnum = WIZ_AFNUM(pin);
uint32_t port_index = WIZ_PORT(pin);
uint32_t pin_index = WIZ_PIN(pin);
uint32_t gpio_add = Get_GPIO_BaseAddress(port_index);
GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add;
// Configure Alternate Function
// Warning: Must be done before the GPIO is initialized
switch (afnum) {
case 0:
HAL_PAD_AFConfig(port_index,(uint32_t)(1 << pin_index),Px_AFSR_AF0);
break;
case 1:
HAL_PAD_AFConfig(port_index,(uint32_t)(1 << pin_index),Px_AFSR_AF1);
break;
case 2:
HAL_PAD_AFConfig(port_index,(uint32_t)(1 << pin_index),Px_AFSR_AF2);
break;
case 3:
HAL_PAD_AFConfig(port_index,(uint32_t)(1 << pin_index),Px_AFSR_AF3);
break;
default:
break;
}
if(mode == WIZ_MODE_AF)
return;
// Configure GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = (uint32_t)(1 << pin_index);
GPIO_InitStructure.GPIO_Mode = mode;
GPIO_InitStructure.GPIO_Pad = gpio_pupd[pupd];
HAL_GPIO_Init(gpio, &GPIO_InitStructure);
}
/**
* Configure pin pull-up/pull-down
*/
void pin_mode(PinName pin, PinMode pupd)
{
MBED_ASSERT(pin != (PinName)NC);
P_Port_Def *px_pcr;
uint32_t port_index = WIZ_PORT(pin);
switch(port_index) {
case PortA:
px_pcr = PA_PCR;
break;
case PortB:
px_pcr = PB_PCR;
break;
case PortC:
px_pcr = PC_PCR;
break;
case PortD:
px_pcr = (P_Port_Def*)PD_PCR;
break;
default:
error("Pinmap error: wrong port number.");
return;
}
px_pcr->Port[port_index] &= ~(Px_PCR_PUPD_DOWN|Px_PCR_PUPD_UP|Px_PCR_DS_HIGH| \
Px_PCR_OD | Px_PCR_IE | Px_PCR_CS_SUMMIT);
px_pcr->Port[port_index] |= gpio_pupd[pupd];
}
targets/hal/TARGET_WIZNET/TARGET_W7500x/gpio_api.c
- pinmap.c에 구현한 함수를 이용하여 gpio 설정을 변경
extern uint32_t Get_GPIO_BaseAddress(uint32_t port_idx);
uint32_t gpio_set(PinName pin)
{
MBED_ASSERT(pin != (PinName)NC);
pin_function(pin, WIZ_PIN_DATA(WIZ_MODE_INPUT, WIZ_GPIO_NOPULL, 0));
return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
}
void gpio_init(gpio_t *obj, PinName pin)
{
obj->pin = pin;
if (pin == (PinName)NC) {
return;
}
uint32_t port_index = WIZ_PORT(pin);
GPIO_TypeDef *gpio = (GPIO_TypeDef *)Get_GPIO_BaseAddress(port_index);
// Fill GPIO object structure for future use
obj->mask = gpio_set(pin);
obj->reg_data_in = &gpio->DATA;
}
void gpio_mode(gpio_t *obj, PinMode mode)
{
pin_mode(obj->pin, mode);
}
void gpio_dir(gpio_t *obj, PinDirection direction)
{
MBED_ASSERT(obj->pin != (PinName)NC);
if (direction == PIN_OUTPUT) {
pin_function(obj->pin, WIZ_PIN_DATA(WIZ_MODE_OUTPUT, WIZ_GPIO_NOPULL, 0));
} else { // PIN_INPUT
pin_function(obj->pin, WIZ_PIN_DATA(WIZ_MODE_INPUT, WIZ_GPIO_NOPULL, 0));
}
}
반응형
'2018년 이전 관심사 > mbed' 카테고리의 다른 글
W7500 mbed SDK Porting Guide (4/6) (0) | 2015.10.08 |
---|---|
W7500 mbed porting Guide ( 3/6 ) (0) | 2015.10.08 |
W7500 mbed porting Guide ( 1/6 ) (0) | 2015.10.08 |
mbed rpc interfacing with Python (0) | 2015.10.08 |
mbed RPC Library porting guide for WIZwiki-W7500 (0) | 2015.10.08 |