1 | /*
|
2 | / Description:
|
3 | / Date:
|
4 | / Filename: main.c
|
5 | */
|
6 | /* Includes */
|
7 | #include <stdio.h>
|
8 | #include <stdint.h>
|
9 | #include <inc/stm32f0xx.h>
|
10 | #include <inc/stm32f0xx_rtc.h>
|
11 | #include <inc/stm32f0xx_rcc.h>
|
12 | #include <inc/stm32f0xx_pwr.h>
|
13 | #include <inc/stm32f0xx_gpio.h>
|
14 |
|
15 | #include "inc/uart.h"
|
16 | #include "inc/i2c.h"
|
17 | #include "inc/clock.h"
|
18 | #include "inc/utility.h"
|
19 |
|
20 | /* Definitions */
|
21 | #define BSRR_VAL 0x0300
|
22 | #define WAIT_MS 500
|
23 | #define SYSTICK_RATE 1000
|
24 |
|
25 | /* Prototypes */
|
26 | void GPIO_init(void);
|
27 | void RTCInit(void);
|
28 | void setLEDs(void);
|
29 | void resetLEDs(void);
|
30 |
|
31 | /* Declarations */
|
32 | char buffer[16];
|
33 | uint8_t IRQ_REG;
|
34 | uint8_t btnRegister = 0;
|
35 | GPIO_InitTypeDef GPIO_InitStructure;
|
36 | RTC_TimeTypeDef RTC_ActualTimeTMP;
|
37 | NVIC_InitTypeDef NVIC_InitStructure;
|
38 | EXTI_InitTypeDef EXTI_InitStructure;
|
39 |
|
40 | /* Temporary */
|
41 | uint8_t x;
|
42 | uint8_t systickDelay = 0;
|
43 |
|
44 | /* The Mainroutine, initializes all peripherals and IOs */
|
45 | int main(void)
|
46 | {
|
47 | /**/
|
48 | SystemInit();
|
49 |
|
50 | /**/
|
51 | GPIO_init();
|
52 | printf("GPIO configured\n");
|
53 |
|
54 | /**/
|
55 | UartInit();
|
56 | printf("UART configured\n");
|
57 |
|
58 | /**/
|
59 | SystemCoreClockUpdate();
|
60 | SysTick_Config(SystemCoreClock/100);
|
61 | printf("SysteCoreClock is %d\n", SystemCoreClock);
|
62 |
|
63 | /**/
|
64 | I2CInit();
|
65 | printf("I2C configured\n");
|
66 |
|
67 | /**/
|
68 | RTC_Config();
|
69 | printf("RTC initialized\n");
|
70 |
|
71 | Clock_init();
|
72 | /**/
|
73 |
|
74 | //SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
|
75 |
|
76 | /* Configure the external interrupt */
|
77 | EXTI_ClearITPendingBit(EXTI_Line19|EXTI_Line0|EXTI_Line1|EXTI_Line2|EXTI_Line3);
|
78 | EXTI_InitStructure.EXTI_Line = EXTI_Line19;
|
79 | EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
|
80 | EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
|
81 | EXTI_InitStructure.EXTI_LineCmd = ENABLE;
|
82 | EXTI_InitStructure.EXTI_Line = EXTI_Line0;
|
83 | EXTI_Init(&EXTI_InitStructure);
|
84 | EXTI_InitStructure.EXTI_Line = EXTI_Line1;
|
85 | EXTI_Init(&EXTI_InitStructure);
|
86 | EXTI_InitStructure.EXTI_Line = EXTI_Line2;
|
87 | EXTI_Init(&EXTI_InitStructure);
|
88 | EXTI_InitStructure.EXTI_Line = EXTI_Line3;
|
89 | EXTI_Init(&EXTI_InitStructure);
|
90 |
|
91 | NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
|
92 | NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
|
93 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
94 | NVIC_Init(&NVIC_InitStructure);
|
95 | NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
|
96 | NVIC_Init(&NVIC_InitStructure);
|
97 | // SYSCFG_EXTILineConfig();
|
98 | I2C_SendSomething();
|
99 |
|
100 | // Test fading
|
101 |
|
102 | for(x = 0; x < 16; x++){
|
103 | setBrightness(16*x);
|
104 | wait_ms(100);
|
105 | }
|
106 |
|
107 | while (1){
|
108 | ClockStatemachine();
|
109 | printf("Maintask still running\n");
|
110 | wait_ms(1000);
|
111 |
|
112 | }
|
113 | }
|
114 |
|
115 |
|
116 | void GPIO_init(void)
|
117 | {
|
118 | //GPIOC Periph clock enable
|
119 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
120 |
|
121 | //Configure PC8 and PC9 in output pushpull mode
|
122 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
|
123 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
124 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
125 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
126 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
127 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
128 | }
|
129 |
|
130 | void setLEDs(void)
|
131 | {
|
132 | GPIOC->BSRR = BSRR_VAL;
|
133 | }
|
134 |
|
135 | void resetLEDs(void)
|
136 | {
|
137 | GPIOC->BRR = BSRR_VAL;
|
138 | }
|
139 |
|
140 |
|
141 | void SysTick_Handler(void)
|
142 | {
|
143 | //printf("systemTicker triggered\n");
|
144 | if(systickDelay == 100){
|
145 | systickDelay = 0;
|
146 | printf("systemTicker triggered\n");
|
147 | } else { systickDelay++;
|
148 | }
|
149 | //IRQ_REG &= 0x01;
|
150 | }
|
151 |
|
152 | void EXTI0_IRQHandler(void){
|
153 | if(EXTI_GetITStatus(EXTI_Line0) != RESET){
|
154 | printf("interrupt 0\n");
|
155 | EXTI_ClearITPendingBit(EXTI_Line0);
|
156 | }
|
157 |
|
158 | }
|