W7500 mbed SDK Porting Guide
Serial Porting
예제 코드
#include "mbed.h"
int main()
{
Serial pc(USBTX, USBRX);
pc.baud(115200);
pc.printf("TEST Start\r\n");
while(1){
pc.printf("LED ON\r\n");
wait(1);
pc.printf("LED OFF\r\n");
wait(1);
}
}
수정할 필요 없는 코드
api 폴더, common 폴더, hal 폴더에 있는 내용을 수정하면 호환성 문제가 발생 할 수 있으므로 수정하지 않는 것을 권한다.
GPIO Porting시 사용하는 아래 파일들은 수정할 필요가 없다.
- api/Serial.h
- api/SerialBase.h
- common/Serial.cpp
- common/SerialBase.cpp
targets/hal/TARGET_WIZNET/TARGET_W7500x/TARGET_WIZwiki_W7500/PeripheralPins.c
- UART 사용 시 필요한 RX, TX Pin 정보를 저장
- 상위 어플리케이션에서는 PA_14라는 Pin 정보만 줘도 아래 PinMap으로 UART0의 TX Pin을 설정 할 수 있음
const PinMap PinMap_UART_TX[] = {
{PA_14, UART_0, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
{PC_2, UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
{NC, NC, 0}
};
const PinMap PinMap_UART_RX[] = {
{PA_13, UART_0, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
{PC_3, UART_1, WIZ_PIN_DATA(WIZ_MODE_AF, WIZ_GPIO_PULLUP, Px_AFSR_AF0)},
{NC, NC, 0}
};
targets/hal/TARGET_WIZNET/TARGET_W7500x/serial_api.c
- Serial 객체 생성 및 사용 시, Serial.cpp와 SerialBase.cpp를 통해 아래 코드 수행
static void init_uart(serial_t *obj)
{
UART = (UART_TypeDef *)(obj->uart);
UART_InitStructure.UART_BaudRate = obj->baudrate;
UART_InitStructure.UART_WordLength = obj->databits;
UART_InitStructure.UART_StopBits = obj->stopbits;
UART_InitStructure.UART_Parity = obj->parity;
UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
if (obj->pin_rx == NC) {
UART_InitStructure.UART_Mode = UART_Mode_Tx;
} else if (obj->pin_tx == NC) {
UART_InitStructure.UART_Mode = UART_Mode_Rx;
} else {
UART_InitStructure.UART_Mode = (UART_Mode_Rx | UART_Mode_Tx);
}
UART_Init(UART,&UART_InitStructure);
}
void serial_init(serial_t *obj, PinName tx, PinName rx)
{
UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX);
UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX);
obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx);
MBED_ASSERT(obj->uart != (UARTName)NC);
if (obj->uart == UART_0) {
obj->index = 0;
}
if (obj->uart == UART_1) {
obj->index = 1;
}
pinmap_pinout(tx, PinMap_UART_TX);
pinmap_pinout(rx, PinMap_UART_RX);
if (tx != NC) {
pin_mode(tx, PullUp);
}
if (rx != NC) {
pin_mode(rx, PullUp);
}
obj->baudrate = 9600;
obj->databits = UART_WordLength_8b;
obj->stopbits = UART_StopBits_1;
obj->parity = UART_Parity_No;
obj->pin_tx = tx;
obj->pin_rx = rx;
init_uart(obj);
if (obj->uart == STDIO_UART) {
stdio_uart_inited = 1;
memcpy(&stdio_uart, obj, sizeof(serial_t));
}
}
void serial_baud(serial_t *obj, int baudrate)
{
obj->baudrate = baudrate;
init_uart(obj);
}
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
{
if (data_bits == 7) {
obj->databits = UART_WordLength_7b;
} else {
obj->databits = UART_WordLength_8b;
}
switch (parity) {
case ParityOdd:
case ParityForced0:
obj->parity = UART_Parity_Odd;
break;
case ParityEven:
case ParityForced1:
obj->parity = UART_Parity_Even;
break;
default:
obj->parity = UART_Parity_No;
break;
}
if (stop_bits == 2) {
obj->stopbits = UART_StopBits_2;
} else {
obj->stopbits = UART_StopBits_1;
}
init_uart(obj);
}
int serial_getc(serial_t *obj)
{
UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
while(uart->FR & UART_FR_RXFE);
return (uart->DR & 0xFF);
}
void serial_putc(serial_t *obj, int c)
{
UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
uart->DR = (uint32_t)(c & (uint16_t)0xFF);
while(uart->FR & UART_FR_BUSY);
}
int serial_readable(serial_t *obj)
{
int status;
UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
status = ((uart->FR & UART_FR_RXFE) ? 0: 1);
return status;
}
int serial_writable(serial_t *obj)
{
int status;
UART_TypeDef *uart = (UART_TypeDef *)(obj->uart);
status = ((uart->FR & UART_FR_BUSY) ? 0: 1);
return status;
}