basic implemtation general purpose clock and tinyprintf
[cortex-from-scratch] / drivers / tsensor.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/8/4 - ROBIN KRENS       
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Temperature sensor
9  *
10  * */
11
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include <stdint.h>
15
16 #include <sys/mmap.h>
17 #include <sys/robsys.h>
18
19 #include <lib/regfunc.h>
20 #include <lib/string.h>
21 #include <lib/tinyprintf.h>
22
23 #include <drivers/tsensor.h>
24
25 /* void * update_handler() {
26
27         printf("HERE!");
28         rclrbit(TIM4_SR1, 0);
29 } */
30
31 void tsensor_init( ) {
32
33         int prescaler = 31; 
34
35         rsetbit(RCC_APB2ENR, 3); // GPIOB enable
36         rsetbit(RCC_APB1ENR, 2); // TIM4 enable
37         rsetbitsfrom(TIM4_CR1, 5, 0x00); // edge-aligned mode
38         rclrbit(TIM4_CR1, 4); // upcounter (clrbit!)
39
40         rwrite(TIM4_PSC, 0xFFFF); // 1 MHz: 23
41         rwrite(TIM4_ARR, 0xAB9); // preload register
42
43         rwrite(GPIOB_CRL, 0x4A444444); // PB6 for Channel 1 TIM4 alternate 
44
45         rwrite(TIM4_CCR1, 0x55C); // half of ARR
46         rwrite(TIM4_RCR, 0x0F); // repeat
47         rsetbit(TIM4_EGR, 0); // update generation
48         
49         rsetbitsfrom(TIM4_CCMR1, 4, 0x7); // PWM mode 1
50         
51         //rsetbit(TIM4_CCMR1, 3); // preload enable
52         //rsetbit(TIM4_CR1, 7); // buffered
53
54         //rsetbit(TIM4_CR1, 3); // one pulse mode
55
56         rsetbit(TIM4_CCER, 0); // enable output channel 1
57 //      rsetbit(TIM4_BDTR, 15); // main output 
58         rsetbit(TIM4_CR1, 0);
59
60         /* INTERRUPTS */        
61         //ivt_set_gate(41, update_handler, 0);
62
63         //rsetbit(TIM4_DIER, 0);
64         //rsetbit(NVIC_ISER0, 25); // interupt 41 - 32
65 }
66
67