Updated sysless-common.
[LPC2xxx_and_RobotSpejbl.git] / app / shodiny / hodiny.c
blobd93e13fa62c2f1c838fe3695b69d029b797eb65f
1 /* ustredni hodiny Spejbla */
3 #include <types.h>
4 #include <lpc21xx.h>
5 #include <periph/can.h>
7 #define CANLOAD_ID (*((volatile unsigned long *) 0x40000120))
9 void timer_irq() __attribute__((interrupt));
11 void timer_init(uint32_t prescale, uint32_t period) {
12 T0PR = prescale - 1;
13 T0MR1 = period - 1;
14 T0MCR = 0x3<<3; /* interrupt and counter reset on match1 */
15 T0EMR = 0x1<<6 | 0x2; /* set MAT0.1 low on match and high now */
16 T0CCR = 0x0; /* no capture */
17 T0TCR |= 0x1; /* go! */
20 void timer_init_irq(unsigned irq_vect) {
21 /* set interrupt vector */
22 ((uint32_t*)&VICVectAddr0)[irq_vect] = (uint32_t)timer_irq;
23 ((uint32_t*)&VICVectCntl0)[irq_vect] = 0x20 | 4;
24 /* enable timer int */
25 VICIntEnable = 0x00000010;
28 can_msg_t msg = {.flags = 0, .dlc = 1};
29 uint8_t seq_num = 0;
31 void timer_irq() {
32 msg.data[0] = seq_num++;
33 while (can_tx_msg(&msg));
34 /* clear int flag */
35 T0IR = 0xffffffff;
36 /* int acknowledge */
37 VICVectAddr = 0;
40 int main() {
41 /* peripheral clock = CPU clock (10MHz) */
42 VPBDIV = 1;
43 /** map exception handling to RAM **/
44 MEMMAP = 0x2;
45 /* init Vector Interrupt Controller */
46 VICIntEnClr = 0xFFFFFFFF;
47 VICIntSelect = 0x00000000;
48 /* 10MHz VPB, 1000kb/s TSEG1=6, TSEG2=3, SJW= */
49 can_init(0x250000, 14, NULL);
50 msg.id = CANLOAD_ID;
51 /* * */
52 timer_init(10, 4000 /*250Hz*/);
53 timer_init_irq(13);
54 /* jamais fatigue */
55 for (;;);