This repository has been archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
66 lines (61 loc) · 1.95 KB
/
serial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Serial code
#include "lpc17xx_uart.h" // Central include files
#include "lpc17xx_pinsel.h"
#include "lpc_types.h"
#include "serial.h" // Local functions
// Entry point for the program
void main(void)
{
serial_init();
write_usb_serial_blocking("USB test code\n\r",16);
while(1);
}
// Read options
int read_usb_serial_none_blocking(char *buf,int length)
{
return(UART_Receive((LPC_UART_TypeDef *)LPC_UART0, (uint8_t *)buf, length, NONE_BLOCKING));
}
// Write options
int write_usb_serial_blocking(char *buf,int length)
{
return(UART_Send((LPC_UART_TypeDef *)LPC_UART0,(uint8_t *)buf,length, BLOCKING));
}
// init code for the USB serial line
void serial_init(void)
{
UART_CFG_Type UARTConfigStruct; // UART Configuration structure variable
UART_FIFO_CFG_Type UARTFIFOConfigStruct; // UART FIFO configuration Struct variable
PINSEL_CFG_Type PinCfg; // Pin configuration for UART
/*
* Initialize UART pin connect
*/
PinCfg.Funcnum = 1;
PinCfg.OpenDrain = 0;
PinCfg.Pinmode = 0;
// USB serial first
PinCfg.Portnum = 0;
PinCfg.Pinnum = 2;
PINSEL_ConfigPin(&PinCfg);
PinCfg.Pinnum = 3;
PINSEL_ConfigPin(&PinCfg);
/* Initialize UART Configuration parameter structure to default state:
* - Baudrate = 9600bps
* - 8 data bit
* - 1 Stop bit
* - None parity
*/
UART_ConfigStructInit(&UARTConfigStruct);
/* Initialize FIFOConfigStruct to default state:
* - FIFO_DMAMode = DISABLE
* - FIFO_Level = UART_FIFO_TRGLEV0
* - FIFO_ResetRxBuf = ENABLE
* - FIFO_ResetTxBuf = ENABLE
* - FIFO_State = ENABLE
*/
UART_FIFOConfigStructInit(&UARTFIFOConfigStruct);
// Built the basic structures, lets start the devices/
// USB serial
UART_Init((LPC_UART_TypeDef *)LPC_UART0, &UARTConfigStruct); // Initialize UART0 peripheral with given to corresponding parameter
UART_FIFOConfig((LPC_UART_TypeDef *)LPC_UART0, &UARTFIFOConfigStruct); // Initialize FIFO for UART0 peripheral
UART_TxCmd((LPC_UART_TypeDef *)LPC_UART0, ENABLE); // Enable UART Transmit
}