lpc-field

Template project for programming NXP's LPC1768 MCUs
git clone git://git.mdnr.space/lpc-field
Log | Files | Refs | README | LICENSE

app.c (1934B)


      1 #include "LPC17xx.h"
      2 #include "vector.h"
      3 #include "lpc17xx_clkpwr.h"
      4 #include "lpc17xx_uart.h"
      5 #include "lpc17xx_pinsel.h"
      6 #include "lpc17xx_systick.h"
      7 
      8 volatile uint32_t tick;
      9 uint32_t get_tick(void);
     10 void delay(uint32_t ms_time);
     11 
     12 // Set p[0]:2 as uart tx
     13 PINSEL_CFG_Type pinsel_config_tx = {
     14     .Portnum = PINSEL_PORT_0,
     15     .Pinnum = PINSEL_PIN_2,
     16     .Funcnum = PINSEL_FUNC_1,
     17 };
     18 
     19 // Set p[0]:3 as uart rx
     20 PINSEL_CFG_Type pinsel_config_rx = {
     21     .Portnum = PINSEL_PORT_0,
     22     .Pinnum = PINSEL_PIN_3,
     23     .Funcnum = PINSEL_FUNC_1,
     24 };
     25 
     26 // Config uart as 8N1, 115200
     27 UART_CFG_Type uart_config = {
     28     .Baud_rate = 115200,
     29     .Parity = UART_PARITY_NONE,
     30     .Databits = UART_DATABIT_8,
     31     .Stopbits = UART_STOPBIT_1,
     32 };
     33 
     34 char *buffer = "hello, from lpc\r\n";
     35 
     36 int main(void)
     37 {
     38     // Clock source initialization
     39     SystemInit();
     40 
     41     // Configuring system tick
     42     SYSTICK_InternalInit(10); // 10ms interrupts
     43     SYSTICK_Cmd(ENABLE);
     44     SYSTICK_IntCmd(ENABLE);
     45     
     46     // Enable uart0 clock
     47     CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCUART0, ENABLE);
     48 
     49     UART_Init(LPC_UART0, &uart_config);
     50     
     51     // Set alternate functions for tx and rx pins
     52     PINSEL_ConfigPin(&pinsel_config_tx);
     53     PINSEL_ConfigPin(&pinsel_config_rx);
     54 
     55     // Enable uart tx pin
     56     UART_TxCmd(LPC_UART0, ENABLE);
     57 
     58     uint32_t now = get_tick();
     59     uint32_t delay_10ms = 200; // 2 sec delay
     60 
     61     while (1) {
     62         if ((get_tick() - now) > delay_10ms) {
     63             UART_Send(LPC_UART0, (uint8_t *)buffer, 17, BLOCKING);
     64             now = get_tick();
     65         }
     66     }
     67     return 0;
     68 }
     69 
     70 uint32_t get_tick(void)
     71 {
     72     return tick;
     73 }
     74 
     75 void delay(uint32_t ms_time)
     76 {
     77     // FIXME: This is not relaiable. Defuse the overflow
     78     uint32_t now = get_tick();
     79     if (ms_time < 10) ms_time = 10; // Minimum delay time is 10 ms
     80     ms_time /= 10;
     81     while((get_tick() - now) < ms_time) {
     82         __asm__("nop");
     83     }
     84 }
     85 
     86 void systick_handler(void)
     87 {
     88     tick++;
     89 }