added silent mode, moved GPIO init

This commit is contained in:
Eric Evenchick 2015-05-14 17:41:28 -04:00
parent 6e7e7cad80
commit 0ab75d742d
4 changed files with 57 additions and 28 deletions

View File

@ -2,9 +2,15 @@
#define _CAN_H
enum can_bitrate {
CAN_BITRATE_1000K,
CAN_BITRATE_500K,
CAN_BITRATE_250K,
CAN_BITRATE_125K,
CAN_BITRATE_100K,
CAN_BITRATE_83K3,
CAN_BITRATE_62K5,
CAN_BITRATE_50K,
CAN_BITRATE_33K3,
};
enum can_bus_state {

View File

@ -33,7 +33,6 @@ void can_init(void) {
hcan.Init.TXFP = DISABLE;
bus_state = OFF_BUS;
//status = HAL_CAN_Init(&hcan);
}
void can_enable(void) {
@ -64,6 +63,9 @@ void can_set_bitrate(enum can_bitrate bitrate) {
}
switch (bitrate) {
case CAN_BITRATE_1000K:
// TODO
break;
case CAN_BITRATE_500K:
hcan.Init.Prescaler = 12;
break;
@ -71,8 +73,35 @@ void can_set_bitrate(enum can_bitrate bitrate) {
hcan.Init.Prescaler = 24;
break;
case CAN_BITRATE_125K:
hcan.Init.Prescaler = 48;
hcan.Init.Prescaler = 1;
break;
case CAN_BITRATE_100K:
// TODO
break;
case CAN_BITRATE_83K3:
// TODO
break;
case CAN_BITRATE_62K5:
// TODO
break;
case CAN_BITRATE_50K:
// TODO
break;
case CAN_BITRATE_33K3:
// TODO
break;
}
}
void can_set_silent(uint8_t silent) {
if (bus_state == ON_BUS) {
// cannot set silent mode while on bus
return;
}
if (silent) {
hcan.Init.Mode = CAN_MODE_SILENT;
} else {
hcan.Init.Mode = CAN_MODE_NORMAL;
}
}

View File

@ -81,12 +81,6 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN CAN_MspInit 1 */
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
GPIO_InitStruct.Alternate = 0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE END CAN_MspInit 1 */
}