1 | #include "stm32f10x.h"
|
2 | #include "FreeRTOSSource/FreeRTOS.h"
|
3 | #include "FreeRTOSSource/task.h"
|
4 | #include "lib/can.h"
|
5 | #include "lib/outpin.h"
|
6 | #include "lib/canids.h"
|
7 | #include "lib/i2c.h"
|
8 | #include "Std_lib/misc.h"
|
9 | #include "Std_lib/stm32f10x_it.h"
|
10 | #include "Std_lib/stm32f10x_rcc.h"
|
11 | #include "Std_lib/stm32f10x_dma.h"
|
12 | #include "lib/SPI.H"
|
13 |
|
14 | #include "lib/enc28j60.h"
|
15 |
|
16 |
|
17 | const unsigned char enc28j60_MAC[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
|
18 | /**
|
19 | * FreeRTOS task prototypes.
|
20 | */
|
21 |
|
22 | portTASK_FUNCTION(goEther, param);
|
23 |
|
24 |
|
25 | portTASK_FUNCTION(vQueryCAN, param);
|
26 |
|
27 | portTASK_FUNCTION(vBlinkLEDs, param);
|
28 |
|
29 | /* Private define ------------------------------------------------------------*/
|
30 |
|
31 |
|
32 | /* Private macro -------------------------------------------------------------*/
|
33 | /* Private variables ---------------------------------------------------------*/
|
34 |
|
35 | DMA_InitTypeDef DMA_InitStructure;
|
36 |
|
37 |
|
38 | volatile portTickType lastErrorTimeStamp;
|
39 | outpin_t led_ready, led_error, led_3,led_4, ready,sck;
|
40 |
|
41 |
|
42 | /**
|
43 | * @brief Configures NVIC and Vector Table base location.
|
44 | * @param None
|
45 | * @retval : None
|
46 | */
|
47 | void NVIC_Configuration(void)
|
48 | {
|
49 |
|
50 | /* 1 bit for pre-emption priority, 3 bits for subpriority */
|
51 | // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
|
52 |
|
53 | NVIC_SetPriority(I2C1_EV_IRQn, 0x00);
|
54 | NVIC_EnableIRQ(I2C1_EV_IRQn);
|
55 |
|
56 | NVIC_SetPriority(I2C1_ER_IRQn, 0x01);
|
57 | NVIC_EnableIRQ(I2C1_ER_IRQn);
|
58 |
|
59 |
|
60 | NVIC_SetPriority(I2C2_EV_IRQn, 0x00);
|
61 | NVIC_EnableIRQ(I2C2_EV_IRQn);
|
62 |
|
63 | NVIC_SetPriority(I2C2_ER_IRQn, 0x01);
|
64 | NVIC_EnableIRQ(I2C2_ER_IRQn);
|
65 |
|
66 | }
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | ///////////////////////////////////////////////////////////////////////////////
|
72 | // MAIN
|
73 | ///////////////////////////////////////////////////////////////////////////////
|
74 | int main(void) {
|
75 |
|
76 | outpin_init(&led_ready, 'C', 0);
|
77 | outpin_init(&ready, 'C', 14);
|
78 | outpin_init(&led_3, 'C', 2);
|
79 | outpin_init(&led_4, 'C', 3);
|
80 | outpin_init(&led_error, 'C', 1);
|
81 |
|
82 |
|
83 | // this sets up the oscilator
|
84 | SystemInit();
|
85 | // Initialization
|
86 | NVIC_Configuration();
|
87 | CAN_inits();
|
88 | SPI2_Init();
|
89 | I2C_config();
|
90 |
|
91 |
|
92 | //zu testzwecken
|
93 | outpin_enable(&led_3);
|
94 | outpin_enable(&led_4);
|
95 | outpin_enable(&led_error);
|
96 |
|
97 |
|
98 | enc28j60Init((unsigned char *)enc28j60_MAC);
|
99 |
|
100 |
|
101 |
|
102 | enc_powerdown();
|
103 |
|
104 | //initialization terminated
|
105 | outpin_enable(&ready);
|
106 |
|
107 | // launch CAN query task one priority level below
|
108 | xTaskCreate(vQueryCAN, (const int8_t*)"QCAN", 100, NULL, tskIDLE_PRIORITY, NULL);
|
109 | // launch blink
|
110 | xTaskCreate(vBlinkLEDs, (const int8_t*)"BLINK", 100, NULL, tskIDLE_PRIORITY, NULL);
|
111 |
|
112 |
|
113 | xTaskCreate(goEther, (const int8_t*)"Ether", 200, NULL, tskIDLE_PRIORITY, NULL);
|
114 |
|
115 | vTaskStartScheduler();
|
116 | return 0;
|
117 | }
|
118 |
|
119 |
|
120 | portTASK_FUNCTION(goEther, param) {
|
121 | outpin_t CS_Ether, MOSI, SCK;
|
122 | outpin_init(&CS_Ether, 'B', 12); // SS Ethernetmodul
|
123 |
|
124 | uint8_t buffer[16] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
|
125 |
|
126 | int i;
|
127 |
|
128 | for (i = 0;i<=15;i++)
|
129 | {
|
130 | int a = 1000;
|
131 | while(--a);
|
132 | I2C_Master_Transmit_Byte(I2C1,0x00,buffer[i]);
|
133 | }
|
134 |
|
135 | while (1) {
|
136 |
|
137 |
|
138 | }
|
139 |
|
140 | }
|
141 |
|
142 |
|
143 | /**
|
144 | * This task grabs actor messages from the CAN bus and forwards their
|
145 | * content.
|
146 | * ERROR messages are also evaluated.
|
147 | */portTASK_FUNCTION(vQueryCAN, param) {
|
148 | // Register all CAN ids that could contain interesting
|
149 | // messages.
|
150 |
|
151 | CAN_subscribeId(CANID_ERROR);
|
152 |
|
153 | uint32_t message;
|
154 | static float norm;
|
155 | static uint8_t brake;
|
156 | static portTickType timeStamp;
|
157 |
|
158 | while (1) {
|
159 | timeStamp = xTaskGetTickCount();
|
160 |
|
161 | if (CAN_receive(CANID_ERROR, &message)) {
|
162 | if (message == ERRORID_PWM_FREQUENCY || message
|
163 | == ERRORID_PWM_OUTOFBOUNDS || message
|
164 | == ERRORID_RECEIVER_NOSIGNAL)
|
165 | lastErrorTimeStamp = timeStamp;
|
166 | }
|
167 | // and sleep for 10ms before trying to fetch new messages again
|
168 | vTaskDelay(10 / portTICK_RATE_MS);
|
169 | }
|
170 | }
|
171 | /**
|
172 | * Task to blink the "RUNS" LED
|
173 | * Slow blinking for normal operation, fast for indicating some error.
|
174 | */portTASK_FUNCTION(vBlinkLEDs, param) {
|
175 |
|
176 |
|
177 | //outpin_init(&led_ready, 'C', 0);
|
178 | while (1) {
|
179 | outpin_toggle(&led_ready);
|
180 | if (xTaskGetTickCount() - lastErrorTimeStamp > 1000 / portTICK_RATE_MS) {
|
181 | // blink slow if ok
|
182 | vTaskDelay(250 / portTICK_RATE_MS);
|
183 | //for(int i=0;i<0xffff;i++){
|
184 |
|
185 |
|
186 | } else {
|
187 | // hectic blink if error present now or in the last second
|
188 | //for(int i=0;i<0xffff;i++);
|
189 | vTaskDelay(75 / portTICK_RATE_MS);
|
190 |
|
191 | }
|
192 | }
|
193 | }
|