main.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. use ctimer to do a LED blink
  3. */
  4. #include <LPC8xx.h>
  5. #include <iocon.h>
  6. #include <syscon.h>
  7. #include <gpio.h>
  8. #include <swm.h>
  9. #include <ctimer.h>
  10. #include <delay.h>
  11. /*=======================================================================*/
  12. /* Configuration */
  13. #define SYS_TICK_PERIOD_IN_MS 100
  14. /*=======================================================================*/
  15. /* system procedures and sys tick master task */
  16. volatile uint32_t sys_tick_irq_cnt=0;
  17. void __attribute__ ((interrupt)) SysTick_Handler(void)
  18. {
  19. sys_tick_irq_cnt++;
  20. }
  21. /*=======================================================================*/
  22. /*
  23. replacement for ConfigSWM(uint32_t func, uint32_t port_pin)
  24. Args:
  25. fn: A function number, e.g. T0_MAT0, see swm.h
  26. port: A port number for the GPIO port (0..30)
  27. */
  28. void mapFunctionToPort(uint32_t fn, uint32_t port)
  29. {
  30. /* first reset the pin assignment to 0xff (this is also the reset value */
  31. LPC_SWM->PINASSIGN[fn/4] |= ((0xffUL)<<(8*(fn%4)));
  32. /* then write the destination pin to it */
  33. LPC_SWM->PINASSIGN[fn/4] &= ~((port^255UL)<<(8*(fn%4)));
  34. }
  35. /*=======================================================================*/
  36. /*
  37. setup the hardware and start interrupts.
  38. called by "Reset_Handler"
  39. */
  40. int __attribute__ ((noinline)) main(void)
  41. {
  42. /* call to the lpc lib setup procedure. This will set the IRC as clk src and main clk to 24 MHz */
  43. SystemInit();
  44. /* if the clock or PLL has been changed, also update the global variable SystemCoreClock */
  45. SystemCoreClockUpdate();
  46. /* set systick and start systick interrupt */
  47. SysTick_Config(main_clk/1000UL*(unsigned long)SYS_TICK_PERIOD_IN_MS);
  48. /* init GPIOs */
  49. GPIOInit();
  50. /* enable clock for several subsystems */
  51. Enable_Periph_Clock(CLK_IOCON);
  52. Enable_Periph_Clock(CLK_SWM);
  53. Enable_Periph_Clock(CLK_CTIMER0);
  54. GPIOSetDir( PORT0, 15, OUTPUT);
  55. LPC_CTIMER0->PR = 0; /* no prescale */
  56. LPC_CTIMER0->MR[3] = 15000000; /* PWM cycle length: one second with 15MHz AHB clock */
  57. LPC_CTIMER0->MCR |= 1<<MR3R; /* Use MR3 value to reset the counter: MR3 is the upper value and sets the PWM cycle */
  58. LPC_CTIMER0->MR[0] = 14000000; /* PWM duty cycle in MR0 */
  59. LPC_CTIMER0->PWMC |= 1<<PWMEN0; /* PWM mode for MR0 */
  60. /* invert output, will only work with PIO0_30, not with PIO0_15 */
  61. LPC_IOCON->PIO0_30 |= 1<<IOCON_INV;
  62. /* connect subsystems to the GPIOs */
  63. /* Just for testing: The signal is routed via PIN0_30 */
  64. mapFunctionToPort(T0_MAT0, 30);
  65. mapFunctionToPort(LVLSHFT_IN0, 30);
  66. mapFunctionToPort(LVLSHFT_OUT0, 15);
  67. /* enable the timer */
  68. LPC_CTIMER0->TCR |= 1<<CEN;
  69. }