Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
can_arch.c
Go to the documentation of this file.
1 /*
2  * $Id:$
3  *
4  * Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
25 #include <stdint.h>
26 #include <string.h>
27 
28 #include "mcu_periph/can_arch.h"
29 #include "mcu_periph/can.h"
30 
31 #include <stm32/rcc.h>
32 #include <stm32/gpio.h>
33 #include <stm32/flash.h>
34 #include <stm32/misc.h>
35 #include <stm32/can.h>
36 
37 #include "led.h"
38 
39 #define RCC_APB2Periph_GPIO_CAN RCC_APB2Periph_GPIOA
40 #define GPIO_CAN GPIOA
41 #define GPIO_Pin_CAN_RX GPIO_Pin_11
42 #define GPIO_Pin_CAN_TX GPIO_Pin_12
43 
44 CanTxMsg can_tx_msg;
45 CanRxMsg can_rx_msg;
46 
47 void _can_run_rx_callback(uint32_t id, uint8_t *buf, uint8_t len);
48 
49 void can_hw_init(void)
50 {
51  GPIO_InitTypeDef gpio;
52  NVIC_InitTypeDef nvic;
53  CAN_InitTypeDef can;
54  CAN_FilterInitTypeDef can_filter;
55 
56  /* Enable peripheral clocks */
57  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO |
58  RCC_APB2Periph_GPIOA, ENABLE);
59  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
60 
61  /* Configure CAN pin: RX */
62  gpio.GPIO_Pin = GPIO_Pin_11;
63  gpio.GPIO_Mode = GPIO_Mode_IPU;
64  GPIO_Init(GPIOA, &gpio);
65 
66  /* Configure CAN pin: TX */
67  gpio.GPIO_Pin = GPIO_Pin_12;
68  gpio.GPIO_Mode = GPIO_Mode_AF_PP;
69  gpio.GPIO_Speed = GPIO_Speed_50MHz;
70  GPIO_Init(GPIOA, &gpio);
71 
72  /* NVIC configuration */
73  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
74 
75  nvic.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
76  nvic.NVIC_IRQChannelPreemptionPriority = 0x00;
77  nvic.NVIC_IRQChannelSubPriority = 0x00;
78  nvic.NVIC_IRQChannelCmd = ENABLE;
79  NVIC_Init(&nvic);
80 
81  /* CAN register init */
82  CAN_DeInit(CAN1);
83  CAN_StructInit(&can);
84 
85  /* CAN cell init */
86  can.CAN_TTCM = DISABLE;
87  can.CAN_ABOM = CAN_ERR_RESUME;
88  can.CAN_AWUM = DISABLE;
89  can.CAN_NART = DISABLE;
90  can.CAN_RFLM = DISABLE;
91  can.CAN_TXFP = DISABLE;
92  can.CAN_Mode = CAN_Mode_Normal;
93  can.CAN_SJW = CAN_SJW_TQ;
94  can.CAN_BS1 = CAN_BS1_TQ;
95  can.CAN_BS2 = CAN_BS2_TQ;
96  can.CAN_Prescaler = CAN_PRESCALER;
97  can.CAN_ABOM = ENABLE;
98  CAN_Init(CAN1, &can);
99 
100  /* CAN filter init */
101  can_filter.CAN_FilterNumber = 0;
102  can_filter.CAN_FilterMode = CAN_FilterMode_IdMask;
103  can_filter.CAN_FilterScale = CAN_FilterScale_32bit;
104  can_filter.CAN_FilterIdHigh = 0x0000;
105  can_filter.CAN_FilterIdLow = 0x0000;
106  can_filter.CAN_FilterMaskIdHigh = 0x0000;
107  can_filter.CAN_FilterMaskIdLow = 0x0000;
108  can_filter.CAN_FilterFIFOAssignment = 0;
109  can_filter.CAN_FilterActivation = ENABLE;
110  CAN_FilterInit(&can_filter);
111 
112  /* transmit struct init */
113  can_tx_msg.StdId = 0x0;
114  can_tx_msg.ExtId = 0x0;
115  can_tx_msg.RTR = CAN_RTR_DATA;
116 #ifdef USE_CAN_EXT_ID
117  can_tx_msg.IDE = CAN_ID_EXT;
118 #else
119  can_tx_msg.IDE = CAN_ID_STD;
120 #endif
121  can_tx_msg.DLC = 1;
122 
123  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
124 }
125 
126 int can_hw_transmit(uint32_t id, const uint8_t *buf, uint8_t len)
127 {
128  if(len > 8){
129  return -1;
130  }
131 
132 #ifdef USE_CAN_EXT_ID
133  can_tx_msg.ExtId = id;
134 #else
135  can_tx_msg.StdId = id;
136 #endif
137  can_tx_msg.DLC = len;
138 
139  memcpy(can_tx_msg.Data, buf, len);
140 
141  CAN_Transmit(CAN1, &can_tx_msg);
142 
143  return 0;
144 }
145 
147 {
148  CAN_Receive(CAN1, CAN_FIFO0, &can_rx_msg);
149 #ifdef USE_CAN_EXT_ID
151 #else
153 #endif
154 }
155 
#define CAN1
Definition: LPC21xx.h:452
void _can_run_rx_callback(uint32_t id, uint8_t *buf, uint8_t len)
Definition: can.c:45
CanRxMsg can_rx_msg
Definition: can_arch.c:45
CanTxMsg can_tx_msg
Definition: can_arch.c:44
unsigned long uint32_t
Definition: types.h:18
int can_hw_transmit(uint32_t id, const uint8_t *buf, uint8_t len)
Definition: can_arch.c:126
unsigned char uint8_t
Definition: types.h:14
void usb_lp_can1_rx0_irq_handler(void)
Definition: can_arch.c:146
void can_hw_init(void)
Definition: can_arch.c:49
arch independent LED (Light Emitting Diodes) API