Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
stm32_libraries
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
humanoides
tools
stm32_libraries
Commits
9fa74025
Commit
9fa74025
authored
4 years ago
by
smartinezs
Browse files
Options
Downloads
Patches
Plain Diff
first commit
parent
d7d397f5
No related branches found
No related tags found
1 merge request
!13
Can devel
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
f1/can/include/can.h
+19
-0
19 additions, 0 deletions
f1/can/include/can.h
f1/can/include/can_common.h
+22
-0
22 additions, 0 deletions
f1/can/include/can_common.h
f1/can/src/can.c
+193
-0
193 additions, 0 deletions
f1/can/src/can.c
with
234 additions
and
0 deletions
f1/can/include/can.h
0 → 100644
+
19
−
0
View file @
9fa74025
#ifndef CAN_F1_H
#define CAN_F1_H
#include
"stm32f1xx_hal.h"
#include
"comm.h"
/* public functions */
void
can_init
(
TComm
*
comm_dev
,
CAN_InitTypeDef
*
conf
,
TCAN_IRQ_Priorities
*
priorities
);
void
can_config
(
TComm
*
comm_dev
,
CAN_InitTypeDef
*
conf
);
void
can_set_priorities
(
TComm
*
comm_dev
,
TCAN_IRQ_Priorities
*
priorities
);
void
can_set_bitrate
(
TComm
*
comm_dev
,
unsigned
int
bitrate
);
void
can_set_filter
(
TComm
*
comm_dev
,
CAN_FilterTypeDef
*
filter
)
/* IRQ functions */
unsigned
char
can_send_irq
(
unsigned
char
first_byte
);
unsigned
char
can_enable_tx_irq
(
void
);
unsigned
char
can_receive_irq
(
void
);
unsigned
char
can_cancel_receive_irq
(
void
);
#endif
This diff is collapsed.
Click to expand it.
f1/can/include/can_common.h
0 → 100644
+
22
−
0
View file @
9fa74025
#ifndef CAN_COMMON_F1_H
#define CAN_COMMON_F1_H
typedef
struct
{
unsigned
char
irq_priority
;
unsigned
char
irq_subpriority
;
}
TCAN_IRQ_Priorities
;
enum
CAN_SPEED
{
//only applies when APB1 = 36Mhz
CAN_10KBPS
,
CAN_20KBPS
,
CAN_50KBPS
,
CAN_83K3BPS
,
CAN_100KBPS
,
CAN_125KBPS
,
CAN_250KBPS
,
CAN_500KBPS
,
CAN_1000KBPS
};
#endif
This diff is collapsed.
Click to expand it.
f1/can/src/can.c
0 → 100644
+
193
−
0
View file @
9fa74025
#include
"can.h"
#define CAN CAN1
#define CAN_ENABLE_CLK __HAL_RCC_CAN1_CLK_ENABLE()
#define CAN_IRQnRX USB_LP_CAN1_RX0_IRQn
#define CAN_IRQnTX USB_HP_CAN1_TX_IRQn
#define CAN_IRQHandler USB_LP_CAN1_RX0CAN_IRQHandler
#define CAN_TX_PIN GPIO_PIN_9
#define CAN_TX_GPIO_PORT GPIOB
#define CAN_ENABLE_TX_GPIO_CLK __HAL_RCC_GPIOB_CLK_ENABLE()
#define CAN_RX_PIN GPIO_PIN_8
#define CAN_RX_GPIO_PORT GPIOB
#define CAN_ENABLE_RX_GPIO_CLK __HAL_RCC_GPIOB_CLK_ENABLE()
// private variables
CAN_HandleTypeDef
CANHandle
;
CanTxMsgTypeDef
CAN_txMessage
;
CanRxMsgTypeDef
CAN_rxMessage
;
TComm
*
can_comm_dev
;
void
can_init
(
TComm
*
comm_dev
,
CAN_InitTypeDef
*
conf
,
TCAN_IRQ_Priorities
*
priorities
)
{
GPIO_InitTypeDef
GPIO_InitStructure
;
/* Enable GPIO clock */
CAN_ENABLE_TX_GPIO_CLK
;
CAN_ENABLE_RX_GPIO_CLK
;
CAN_ENABLE_CLK
;
/**CAN GPIO Configuration
PB8 ------> CAN_RX
PB9 ------> CAN_TX
*/
/* Configure CAN Tx and Rx as alternate function push-pull */
GPIO_InitStructure
.
Pin
=
CAN_RX_PIN
;
GPIO_InitStructure
.
Pull
=
GPIO_PULLDOWN
;
GPIO_InitStructure
.
Mode
=
GPIO_MODE_INPUT
;
HAL_GPIO_Init
(
CAN_RX_GPIO_PORT
,
&
GPIO_InitStructure
);
GPIO_InitStructure
.
Pin
=
CAN_TX_PIN
;
GPIO_InitStructure
.
Mode
=
GPIO_MODE_AF_PP
;
GPIO_InitStructure
.
Speed
=
GPIO_SPEED_FREQ_HIGH
;
HAL_GPIO_Init
(
CAN_TX_GPIO_PORT
,
&
GPIO_InitStructure
);
_HAL_AFIO_REMAP_CAN1_2
();
CANHandle
.
Instance
=
CAN
;
can_config
(
comm_dev
,
conf
);
can_set_priorities
(
comm_dev
,
priorities
);
/* Initialize the comm structure */
comm_dev
->
send_irq
=
can_send_irq
;
comm_dev
->
enable_tx_irq
=
can_enable_tx_irq
;
comm_dev
->
receive_irq
=
can_receive_irq
;
comm_dev
->
cancel_receive_irq
=
can_cancel_receive_irq
;
comm_dev
->
irq_send_cb
=
0x00000000
;
comm_dev
->
irq_receive_cb
=
0x00000000
;
comm_dev
->
dma_send_cb
=
0x00000000
;
comm_dev
->
dma_receive_cb
=
0x00000000
;
/* initialize internal variables */
can_comm_dev
=
comm_dev
;
HAL_CAN_Start
(
CANHandle
);
}
void
can_config
(
TComm
*
comm_dev
,
CAN_InitTypeDef
*
conf
)
{
CANHandle
.
Init
.
Prescaler
=
conf
->
Prescaler
;
CANHandle
.
Init
.
Mode
=
conf
->
Mode
;
CANHandle
.
Init
.
SJW
=
conf
->
SJW
;
CANHandle
.
Init
.
BS1
=
conf
->
BS1
;
CANHandle
.
Init
.
BS2
=
conf
->
BS2
;
CANHandle
.
Init
.
TTCM
=
conf
->
TTCM
;
CANHandle
.
Init
.
ABOM
=
conf
->
ABOM
;
CANHandle
.
Init
.
AWUM
=
conf
->
AWUM
;
CANHandle
.
Init
.
NART
=
conf
->
NART
;
CANHandle
.
Init
.
RFLM
=
conf
->
RFLM
;
CANHandle
.
Init
.
TXFP
=
conf
->
TXFP
;
HAL_CAN_Init
(
&
CANHandle
);
}
void
can_set_priorities
(
TComm
*
comm_dev
,
TCAN_IRQ_Priorities
*
priorities
)
{
HAL_NVIC_SetPriority
(
CAN_IRQnRX
,
priorities
->
irq_priority
,
priorities
->
irq_subpriority
);
HAL_NVIC_EnableIRQ
(
CAN_IRQnRX
);
HAL_NVIC_SetPriority
(
CAN_IRQnTX
,
priorities
->
irq_priority
,
priorities
->
irq_subpriority
);
HAL_NVIC_EnableIRQ
(
CAN_IRQnTX
);
}
void
can_set_bitrate
(
TComm
*
comm_dev
,
CAN_SPEED
bitrate
)
{
switch
(
CAN_SPEED
)
{
case
CAN_10KBPS
:
CANHandle
.
Init
.
Prescaler
=
225
;
CANHandle
.
Init
.
TimeSeg1
=
13
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_20KBPS
:
CANHandle
.
Init
.
Prescaler
=
100
;
CANHandle
.
Init
.
TimeSeg1
=
15
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_50KBPS
:
CANHandle
.
Init
.
Prescaler
=
45
;
CANHandle
.
Init
.
TimeSeg1
=
13
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_83K3BPS
:
CANHandle
.
Init
.
Prescaler
=
27
;
CANHandle
.
Init
.
TimeSeg1
=
13
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_100KBPS
:
CANHandle
.
Init
.
Prescaler
=
20
;
CANHandle
.
Init
.
TimeSeg1
=
15
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_125KBPS
:
CANHandle
.
Init
.
Prescaler
=
18
;
CANHandle
.
Init
.
TimeSeg1
=
13
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_250KBPS
:
CANHandle
.
Init
.
Prescaler
=
9
;
CANHandle
.
Init
.
TimeSeg1
=
13
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_500KBPS
:
CANHandle
.
Init
.
Prescaler
=
4
;
CANHandle
.
Init
.
TimeSeg1
=
15
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
case
CAN_1000KBPS
:
CANHandle
.
Init
.
Prescaler
=
2
;
CANHandle
.
Init
.
TimeSeg1
=
15
;
CANHandle
.
Init
.
TimeSeg2
=
2
;
CANHandle
.
Init
.
SyncJumpWidth
=
1
;
break
;
}
}
void
can_set_filter
(
TComm
*
comm_dev
,
CAN_FilterTypeDef
*
filter
)
{
HAL_CAN_ConfigFilter
(
&
CANHandle
,
&
filter
);
HAL_CAN_Receive_IT
(
&
CANHandle
,
CAN_FIFO0
);
}
// interrupt handlers
void
USB_LP_CAN1_RX0_IRQHandler
(
void
)
{
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
HAL_PCD_IRQHandler
(
&
PCDHandle
);
/* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
/* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
}
void
USB_HP_CAN1_TX_IRQHandler
(
void
)
{
/* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 0 */
/* USER CODE END USB_HP_CAN1_TX_IRQn 0 */
HAL_CAN_IRQHandler
(
&
hcan
);
/* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 1 */
/* USER CODE END USB_HP_CAN1_TX_IRQn 1 */
}
void
usb_disconnect
(
void
)
{
HAL_PCD_DevDisconnect
(
&
PCDHandle
);
}
void
usb_connect
(
void
)
{
HAL_PCD_DevConnect
(
&
PCDHandle
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment