0cdda5a3ffb1013a43b1e992df52c036da709e9b
[cortex-from-scratch] / drivers / led.c
1 /* (CC-BY-NC-SA) ROBIN KRENS - ROBIN @ ROBINKRENS.NL
2  * 
3  * $LOG$
4  * 2019/7/25 - ROBIN KRENS      
5  * Initial version 
6  * 
7  * $DESCRIPTION$
8  * Hardly a driver, but a driver nonetheless. Enables clock on 
9  * a GPIOx range and sets the GPIOx pin in push-pull output mode. 
10  * Basic on - off function (writing a 1 or zero 0) 
11  * 
12  * $USAGE$
13  * In the code below we enable GPIOC and use PC0 as output. But 
14  * basically any GPIO range or pin could be used. Note that the 
15  * output register is only word accessible.
16  *
17  * */
18
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include <stdint.h>
22
23 #include <sys/mmap.h>
24 #include <sys/robsys.h>
25
26 #include <lib/regfunc.h>
27 #include <lib/string.h>
28
29 #include <drivers/led.h>
30
31
32 void led_init() {
33
34         regw_u8(RCC_APB2ENR, 0x1, 4, SETBIT); // enable GPIOC
35         regw_u32(GPIOC_CRL, 0x44444442, 0, OWRITE); // set PC0 pin to output mode
36         *GPIOC_ODR = 0xFFFF; // only writable in word mode
37
38 }
39
40 void led_on() {
41         *GPIOC_ODR = 0x0001;
42 }
43
44 void led_off() {
45         *GPIOC_ODR = 0x0000;
46 }
47