Описание библиотеки микросхемы 5023ВС016 процессора "Спутник"
can_test.c
См. документацию.
1 
29 #include "can_test.h"
30 
31 
36 
39 #define BUF_LEN 10
41 unsigned int buf_pos_rx = 0;
42 unsigned int buf_pos_tx = 0;
43 unsigned int rx_flag[2] = {0, 0};
44 
45 
46 void irq_handler(int index)
47 {
48  for (unsigned int wait = 0; wait < 100; wait++);
49 
50  if (basic_can[index]->INTERRUPT&0x1 == 1)
51  {
52  rx_flag[index] = 1;
53  //копируем содержимое регистров fifo в программный буфер
54  if (basic_can[index]->Clock_divider & (1<<7)) //extended режим
55  {
57  receive_buffer[buf_pos_rx][1] = peli_can[index]->ID_1_Acceptance_code_1;
58  receive_buffer[buf_pos_rx][2] = peli_can[index]->ID_2_Acceptance_code_2;
64  receive_buffer[buf_pos_rx][8] = peli_can[index]->Data6_Data4;
65  receive_buffer[buf_pos_rx][9] = peli_can[index]->Data7_Data5;
66  receive_buffer[buf_pos_rx][10] = peli_can[index]->Data8_Data6;
67  receive_buffer[buf_pos_rx][11] = peli_can[index]->FIFO1_Data7;
68  receive_buffer[buf_pos_rx][12] = peli_can[index]->FIFO2_Data8;
69  }
70  else //basic режим
71  {
72  receive_buffer[buf_pos_rx][0] = basic_can[index]->RX_id1;
73  receive_buffer[buf_pos_rx][1] = basic_can[index]->RX_id2_rtr_dlc;
74  receive_buffer[buf_pos_rx][2] = basic_can[index]->RX_data_byte_1;
75  receive_buffer[buf_pos_rx][3] = basic_can[index]->RX_data_byte_2;
76  receive_buffer[buf_pos_rx][4] = basic_can[index]->RX_data_byte_3;
77  receive_buffer[buf_pos_rx][5] = basic_can[index]->RX_data_byte_4;
78  receive_buffer[buf_pos_rx][6] = basic_can[index]->RX_data_byte_5;
79  receive_buffer[buf_pos_rx][7] = basic_can[index]->RX_data_byte_6;
80  receive_buffer[buf_pos_rx][8] = basic_can[index]->RX_data_byte_7;
81  receive_buffer[buf_pos_rx][9] = basic_can[index]->RX_data_byte_8;
82  receive_buffer[buf_pos_rx][10] = 0x00;
83  }
85  basic_can[index]->COMMAND = (1 << 2); //реализуем приемный буффер
86  receive_buffer[0][10]=0;
87  PRINT(">>> Принято сообщение по CAN: %s", &receive_buffer[0][2]);
88  }
89 }
90 
91 
92 void CAN1_IRQ()
93 {
94  irq_handler(0);
95 }
96 
97 void CAN2_IRQ()
98 {
99  irq_handler(1);
100 }
101 
102 //настройка портов для CAN для одного из режимов альтернативных функций
103 void Init_ports_CAN(unsigned int mode)
104 {
105  if (mode == 0) //altfunc 1
106  {
107  GPIO_E->ALTFUNCCLR = (1 << 14) | (1 << 15);
108  GPIO_F->ALTFUNCCLR = (1 << 0) | (1 << 1);
109  GPIO_G->ALTFUNCCLR = (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
110  GPIO_E->OUTENCLR = (1 << 14) | (1 << 15);
111  GPIO_F->OUTENCLR = (1 << 0) | (1 << 1);
112  GPIO_G->OUTENCLR = (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
113  GPIO_H->ALTFUNCSET = (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
114  }
115  else if (mode == 1) //atlfunc 2
116  {
117  GPIO_H->ALTFUNCCLR |= (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
118  GPIO_G->ALTFUNCCLR |= (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
119  GPIO_H->OUTENCLR |= (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
120  GPIO_G->OUTENCLR |= (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
121  CMN_REG->ALT_FUNCTION_CTRL[4] |= 0x50000000;
122  CMN_REG->ALT_FUNCTION_CTRL[5] |= 0x00000005;
123  GPIO_E->ALTFUNCSET |= (1 << 14) | (1 << 15);
124  GPIO_F->ALTFUNCSET |= (1 << 0) | (1 << 1);
125  }
126  else if (mode == 2) //atlfunc 3
127  {
128  GPIO_H->ALTFUNCCLR |= (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
129  GPIO_E->ALTFUNCCLR |= (1 << 14) | (1 << 15);
130  GPIO_F->ALTFUNCCLR |= (1 << 0) | (1 << 1);
131  GPIO_G->ALTFUNCCLR |= (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
132  GPIO_E->OUTENCLR |= (1 << 14) | (1 << 15);
133  GPIO_F->OUTENCLR |= (1 << 0) | (1 << 1);
134  GPIO_G->OUTENCLR |= (1 << 12) | (1 << 13) | (1 << 14) | (1 << 15);
135  GPIO_H->OUTENCLR |= (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13);
136  CMN_REG->ALT_FUNCTION_CTRL[6] |= 0xAA000000;
137  GPIO_G->ALTFUNCSET |= (1<<12)|(1<<13)|(1 << 14) | (1 << 15);
138  }
139 }
140 
141 void testFilter(int index,int id)
142 {
143  basic_can[index]->ACEPTANCE_CODE = id;
144  basic_can[index]->ACEPTANCE_MASK = 0x00;
145 }
146 
147 #define BASIC 0
148 #define EXTENDED 1
149 #define CAN_MODE BASIC // EXTENDED
151 {
153  //NVIC_EnableIRQ(CAN2_NUM,20);
154 
155  CMN_REG->PWR_CTRL_CLK |= (1<<20); //CAN1
156  CMN_REG->PWR_CTRL_RST |= (1<<20);
157 
158  //CMN_REG->PWR_CTRL_CLK |= (1<<21); //CAN2
159  //CMN_REG->PWR_CTRL_RST |= (1<<21);
160 
161  basic_can[0]->CONTROL = 1; //reset ВКЛ
162 // 1 Mbit speed 0.5 Mbit 0.1 Mbit //0.05Mbit
163 #define BRP 7 //7 //15 //39 57
164 #define TSEG1 1 //1 //1 //3 6
165 #define TSEG2 1 //1 //1 //4 5
166 
167  basic_can[0]->BUS_TIMING_0 = (BRP << 0) | (1 << 6);
168  basic_can[0]->BUS_TIMING_1 = (TSEG1 << 0) | (TSEG2 << 4) | (0 << 7);
169 
170  basic_can[0]->Clock_divider = 0; //basic режим
171  basic_can[0]->ACEPTANCE_CODE = 0x00;
172  basic_can[0]->ACEPTANCE_MASK = 0xFF;
173  basic_can[0]->CONTROL = 0x1E;
174 
175  basic_can[0]->TX_id1 = 0x55;
176  basic_can[0]->TX_id2_rtr_dlc = (3 << 5) | (0 << 4) | (8 << 0);
177  basic_can[0]->TX_data_byte_1 = 0xaa;
178  basic_can[0]->TX_data_byte_2 = 0xaa;
179  basic_can[0]->TX_data_byte_3 = 0xaa;
180  basic_can[0]->TX_data_byte_4 = 0xaa;
181  basic_can[0]->TX_data_byte_5 = 0xaa;
182  basic_can[0]->TX_data_byte_6 = 0xaa;
183  basic_can[0]->TX_data_byte_7 = 0xbb;
184  basic_can[0]->TX_data_byte_8 = 0xbb;
185 
186  basic_can[0]->COMMAND = 1;
187 
188  for (unsigned int index = 0; index < 2; index++)
189  {
190  basic_can[index]->CONTROL = 1; //reset ВКЛ
191 #define BRP 7
192 #define TSEG1 1
193 #define TSEG2 1
194  basic_can[index]->BUS_TIMING_0 = (BRP << 0) | (1 << 6);
195  basic_can[index]->BUS_TIMING_1 = (TSEG1 << 0) | (TSEG2 << 4) | (0 << 7);
196 
197 #if (CAN_MODE == BASIC)
198  basic_can[index]->Clock_divider = 0; //basic режим
199  basic_can[index]->ACEPTANCE_CODE = 0x00;
200  basic_can[index]->ACEPTANCE_MASK = 0xFF;
201 
202 
203  // testFilter(index,0x05); // Тест Фильтрации - при включении - перестает принимать сообщения!все верно
204 
205 
206  basic_can[index]->CONTROL = 0x1E; //reset off, all irqs enabled
207 #else
208  peli_can[index]->Clock_divider = (1 << 7); //extended режим
209  peli_can[index]->FI_SFF_FI_EFF_Acceptance_code_0 = 0x00;
210  peli_can[index]->ID_1_Acceptance_code_1 = 0x00;
211  peli_can[index]->ID_2_Acceptance_code_2 = 0x00;
212  peli_can[index]->Data1_ID_3_Acceptance_code_3 = 0x00;
213  peli_can[index]->Data2_ID_4_Acceptance_mask_0 = 0xFF;
214  peli_can[index]->Data3_Data1_Acceptance_mask_1 = 0xFF;
215  peli_can[index]->Data4_Data2_Acceptance_mask_2 = 0xFF;
216  peli_can[index]->Data5_Data3_Acceptance_mask_3 = 0xFF;
217 
218  peli_can[index]->Interrupt_enable = 0xFF;
219  peli_can[index]->Mode = 0;
220 #endif
221  rx_flag[index] = 0;
222  }
223  //Отправка обратно
224  unsigned int ID = 0;
225  unsigned int length;
226  unsigned int FF;
227  unsigned int RTR;
228  while(1)
229  {
230  for (unsigned int index = 0; index < 1; index++)
231  if (rx_flag[index]) //данные приняты
232  {
233  rx_flag[index] = 0;
234 #if (CAN_MODE == BASIC)
235  ID = (receive_buffer[buf_pos_tx][0] << 3) | (receive_buffer[buf_pos_tx][1] >> 5);
236  length = receive_buffer[buf_pos_tx][1] & 0xF;
237  //Отправка обратно
238  basic_can[index]->TX_id1 = receive_buffer[buf_pos_tx][0];//ID >> 3; //id[10:3]
239  basic_can[index]->TX_id2_rtr_dlc = receive_buffer[buf_pos_tx][1];//((ID & 0x7) << 5) | (0 << 4) | (length << 0); //id[2:0] = 3, rtr = 0, length = 7
240  basic_can[index]->TX_data_byte_1 = receive_buffer[buf_pos_tx][2];
241  basic_can[index]->TX_data_byte_2 = receive_buffer[buf_pos_tx][3];
242  basic_can[index]->TX_data_byte_3 = receive_buffer[buf_pos_tx][4];
243  basic_can[index]->TX_data_byte_4 = receive_buffer[buf_pos_tx][5];
244  basic_can[index]->TX_data_byte_5 = receive_buffer[buf_pos_tx][6];
245  basic_can[index]->TX_data_byte_6 = receive_buffer[buf_pos_tx][7];
246  basic_can[index]->TX_data_byte_7 = receive_buffer[buf_pos_tx][8];
247  basic_can[index]->TX_data_byte_8 = receive_buffer[buf_pos_tx][9];
248  basic_can[index]->COMMAND = 1;
249 #endif
250  buf_pos_tx++;
251  buf_pos_tx %= BUF_LEN;
252  }
253  }
254 }
__RW uint32_t ACEPTANCE_CODE
Definition: can.h:45
__RW uint32_t Clock_divider
Definition: can.h:71
__RW uint32_t TX_data_byte_6
Definition: can.h:57
#define PRINT(...)
Макросы для использования отладочного выхода
Definition: debug_uart.h:48
__RW uint32_t ID_1_Acceptance_code_1
Definition: can.h:163
__RW uint32_t Clock_divider
Definition: can.h:177
#define BRP
char receive_buffer[BUF_LEN][13]
Definition: can_test.c:40
__RW uint32_t Data2_ID_4_Acceptance_mask_0
Definition: can.h:166
CAN_T * basic_can[2]
Definition: can_test.c:37
void NVIC_EnableIRQ(IRQn_T IRQn, uint32_t vec)
Разрешение прерывания
Definition: system.c:74
__RW uint32_t TX_data_byte_4
Definition: can.h:55
__RO uint32_t RX_id2_rtr_dlc
Definition: can.h:61
unsigned int rx_flag[2]
Definition: can_test.c:43
__RW uint32_t TX_id1
Definition: can.h:50
CAN_T * can0
Definition: can_test.c:32
__RW uint32_t Data8_Data6
Definition: can.h:172
void CAN1_IRQ()
Definition: can_test.c:92
__RW uint32_t FIFO2_Data8
Definition: can.h:174
__RO uint32_t RX_data_byte_8
Definition: can.h:69
__RW uint32_t TX_data_byte_5
Definition: can.h:56
__RW uint32_t BUS_TIMING_0
Definition: can.h:47
__RO uint32_t RX_data_byte_2
Definition: can.h:63
__RO uint32_t RX_data_byte_6
Definition: can.h:67
#define GPIO_G
Указатель на структуру GPIO_G.
#define BUF_LEN
Definition: can_test.c:39
#define BasicCAN_1
Definition: can_test.h:37
__RW uint32_t Data1_ID_3_Acceptance_code_3
Definition: can.h:165
__RW uint32_t Data3_Data1_Acceptance_mask_1
Definition: can.h:167
__RW uint32_t Data4_Data2_Acceptance_mask_2
Definition: can.h:168
__RW uint32_t TX_data_byte_1
Definition: can.h:52
unsigned int buf_pos_tx
Definition: can_test.c:42
__RW uint32_t BUS_TIMING_1
Definition: can.h:48
Этот файл содержит структуры, макросы и функции необходимые необходимые для тестовой программы для мо...
__WO uint32_t COMMAND
Definition: can.h:42
__RO uint32_t RX_data_byte_1
Definition: can.h:62
__RO uint32_t RX_data_byte_5
Definition: can.h:66
__RO uint32_t RX_id1
Definition: can.h:60
__RW uint32_t Data5_Data3_Acceptance_mask_3
Definition: can.h:169
__RW uint32_t Interrupt_enable
Definition: can.h:146
CAN_EXTANDED_T * peli_can[2]
Definition: can_test.c:38
__RW uint32_t TX_data_byte_8
Definition: can.h:59
__RW uint32_t TX_data_byte_3
Definition: can.h:54
CAN_EXTANDED_T * pelican1
Definition: can_test.c:35
void CAN2_IRQ()
Definition: can_test.c:97
__RO uint32_t RX_data_byte_7
Definition: can.h:68
__RW uint32_t Data7_Data5
Definition: can.h:171
#define PeliCAN_1
Definition: can_test.h:39
__RW uint32_t FIFO1_Data7
Definition: can.h:173
__RW uint32_t FI_SFF_FI_EFF_Acceptance_code_0
Definition: can.h:162
#define BasicCAN_0
Definition: can_test.h:36
__RW uint32_t ID_2_Acceptance_code_2
Definition: can.h:164
__RW uint32_t Mode
Definition: can.h:142
__RW uint32_t Data6_Data4
Definition: can.h:170
#define PeliCAN_0
Definition: can_test.h:38
void Init_ports_CAN(unsigned int mode)
Definition: can_test.c:103
#define TSEG1
CAN_EXTANDED_T * pelican0
Definition: can_test.c:34
Структура для доступа к регистрам контроллера CAN в базовом режиме.
Definition: can.h:39
#define TSEG2
__RW uint32_t CONTROL
Definition: can.h:41
#define GPIO_E
Указатель на структуру GPIO_E.
unsigned int buf_pos_rx
Definition: can_test.c:41
void testFilter(int index, int id)
Definition: can_test.c:141
__RW uint32_t TX_id2_rtr_dlc
Definition: can.h:51
__RW uint32_t ACEPTANCE_MASK
Definition: can.h:46
__RO uint32_t RX_data_byte_4
Definition: can.h:65
#define CMN_REG
Указатель на структуру CMN_REG.
__RO uint32_t RX_data_byte_3
Definition: can.h:64
void irq_handler(int index)
Definition: can_test.c:46
Структура для доступа к регистрам контроллера CAN в расширеном режим режиме.
Definition: can.h:140
#define GPIO_H
Указатель на структуру GPIO_H.
void CAN_to_USB_Test()
Definition: can_test.c:150
#define GPIO_F
Указатель на структуру GPIO_F.
__RW uint32_t TX_data_byte_2
Definition: can.h:53
CAN_T * can1
Definition: can_test.c:33
__RW uint32_t TX_data_byte_7
Definition: can.h:58