Formatting changes preparing for code overhaul

This commit is contained in:
Ethan Zonca 2016-08-11 21:52:05 -04:00
parent fb0f55fd3b
commit be7432a65f
5 changed files with 156 additions and 327 deletions

137
Src/can.c
View File

@ -7,7 +7,10 @@ CAN_FilterConfTypeDef filter;
uint32_t prescaler;
enum can_bus_state bus_state;
void can_init(void) {
// Initialize CAN peripheral settings, but don't actually start the peripheral
void can_init(void)
{
filter.FilterIdHigh = 0;
filter.FilterIdLow = 0;
filter.FilterMaskIdHigh = 0;
@ -25,19 +28,23 @@ void can_init(void) {
bus_state = OFF_BUS;
}
void can_enable(void) {
if (bus_state == OFF_BUS) {
hcan.Init.Prescaler = prescaler;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_4TQ;
hcan.Init.BS2 = CAN_BS2_3TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
// Start the CAN peripheral
void can_enable(void)
{
if (bus_state == OFF_BUS)
{
hcan.Init.Prescaler = prescaler;
hcan.Init.Mode = CAN_MODE_NORMAL;
hcan.Init.SJW = CAN_SJW_1TQ;
hcan.Init.BS1 = CAN_BS1_4TQ;
hcan.Init.BS2 = CAN_BS2_3TQ;
hcan.Init.TTCM = DISABLE;
hcan.Init.ABOM = DISABLE;
hcan.Init.AWUM = DISABLE;
hcan.Init.NART = DISABLE;
hcan.Init.RFLM = DISABLE;
hcan.Init.TXFP = DISABLE;
hcan.pTxMsg = NULL;
HAL_CAN_Init(&hcan);
HAL_CAN_ConfigFilter(&hcan, &filter);
@ -45,8 +52,12 @@ void can_enable(void) {
}
}
void can_disable(void) {
if (bus_state == ON_BUS) {
// Disable the CAN peripheral and go off-bus
void can_disable(void)
{
if (bus_state == ON_BUS)
{
// do a bxCAN reset (set RESET bit to 1)
hcan.Instance->MCR |= CAN_MCR_RESET;
bus_state = OFF_BUS;
@ -54,56 +65,69 @@ void can_disable(void) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
}
void can_set_bitrate(enum can_bitrate bitrate) {
if (bus_state == ON_BUS) {
// Set the bitrate of the CAN peripheral
void can_set_bitrate(enum can_bitrate bitrate)
{
if (bus_state == ON_BUS)
{
// cannot set bitrate while on bus
return;
}
switch (bitrate) {
case CAN_BITRATE_10K:
prescaler = 600;
break;
case CAN_BITRATE_20K:
prescaler = 300;
break;
case CAN_BITRATE_50K:
prescaler = 120;
break;
case CAN_BITRATE_100K:
prescaler = 60;
break;
case CAN_BITRATE_125K:
prescaler = 48;
break;
case CAN_BITRATE_250K:
prescaler = 24;
break;
case CAN_BITRATE_500K:
prescaler = 12;
break;
case CAN_BITRATE_750K:
prescaler = 8;
break;
case CAN_BITRATE_1000K:
prescaler = 6;
break;
switch (bitrate)
{
case CAN_BITRATE_10K:
prescaler = 600;
break;
case CAN_BITRATE_20K:
prescaler = 300;
break;
case CAN_BITRATE_50K:
prescaler = 120;
break;
case CAN_BITRATE_100K:
prescaler = 60;
break;
case CAN_BITRATE_125K:
prescaler = 48;
break;
case CAN_BITRATE_250K:
prescaler = 24;
break;
case CAN_BITRATE_500K:
prescaler = 12;
break;
case CAN_BITRATE_750K:
prescaler = 8;
break;
case CAN_BITRATE_1000K:
prescaler = 6;
break;
}
}
void can_set_silent(uint8_t silent) {
if (bus_state == ON_BUS) {
// Set CAN peripheral to silent mode
void can_set_silent(uint8_t silent)
{
if (bus_state == ON_BUS)
{
// cannot set silent mode while on bus
return;
}
if (silent) {
if (silent)
{
hcan.Init.Mode = CAN_MODE_SILENT;
} else {
hcan.Init.Mode = CAN_MODE_NORMAL;
}
}
uint32_t can_tx(CanTxMsgTypeDef *tx_msg, uint32_t timeout) {
// Send a message on the CAN bus (blocking)
uint32_t can_tx(CanTxMsgTypeDef *tx_msg, uint32_t timeout)
{
uint32_t status;
// transmit can frame
@ -114,7 +138,10 @@ uint32_t can_tx(CanTxMsgTypeDef *tx_msg, uint32_t timeout) {
return status;
}
uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout) {
// Receive a message from the CAN bus (blocking)
uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout)
{
uint32_t status;
hcan.pRxMsg = rx_msg;
@ -125,8 +152,12 @@ uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout) {
return status;
}
uint8_t is_can_msg_pending(uint8_t fifo) {
if (bus_state == OFF_BUS) {
// Check if a CAN message has been received and is waiting in the FIFO
uint8_t is_can_msg_pending(uint8_t fifo)
{
if (bus_state == OFF_BUS)
{
return 0;
}
return (__HAL_CAN_MSG_PENDING(&hcan, fifo) > 0);

View File

@ -1,80 +1,25 @@
/**
******************************************************************************
* File Name : main.c
* Date : 05/12/2014 20:22:28
* Description : Main program body
******************************************************************************
*
* COPYRIGHT(c) 2014 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
//
// CANable firmware - a fork of CANtact by Eric Evenchick
//
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
#include "usb_device.h"
/* USER CODE BEGIN Includes */
#include "usbd_cdc_if.h"
#include "can.h"
#include "slcan.h"
#include "led.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void led_init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
volatile int i=0;
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
@ -87,11 +32,6 @@ int main(void)
led_init();
MX_USB_DEVICE_Init();
/* USER CODE END 2 */
/* USER CODE BEGIN 3 */
// turn on green LED
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
@ -121,12 +61,8 @@ int main(void)
led_process();
}
/* USER CODE END 3 */
}
/** System Clock Configuration
*/
void SystemClock_Config(void)
{
@ -236,7 +172,8 @@ void MX_GPIO_Init(void)
}
/* USER CODE BEGIN 4 */
static void led_init() {
static void led_init()
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
@ -245,34 +182,3 @@ static void led_init() {
GPIO_InitStruct.Alternate = 0;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE END 4 */
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -1,18 +1,24 @@
//
// slcan: Parse incoming and generate outgoing slcan messages
#include "stm32f0xx_hal.h"
#include "can.h"
#include "slcan.h"
int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame)
{
uint8_t i = 0;
uint8_t id_len, j;
uint32_t tmp;
for (j=0; j < SLCAN_MTU; j++) {
for (j=0; j < SLCAN_MTU; j++)
{
buf[j] = '\0';
}
// add character for frame type
if (frame->RTR == CAN_RTR_DATA) {
if (frame->RTR == CAN_RTR_DATA)
{
buf[i] = 't';
} else if (frame->RTR == CAN_RTR_REMOTE) {
buf[i] = 'r';
@ -21,8 +27,10 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
// assume standard identifier
id_len = SLCAN_STD_ID_LEN;
tmp = frame->StdId;
// check if extended
if (frame->IDE == CAN_ID_EXT) {
if (frame->IDE == CAN_ID_EXT)
{
// convert first char to upper case for extended frame
buf[i] -= 32;
id_len = SLCAN_EXT_ID_LEN;
@ -31,7 +39,8 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
i++;
// add identifier to buffer
for(j=id_len; j > 0; j--) {
for(j=id_len; j > 0; j--)
{
// add nybble to buffer
buf[j] = (tmp & 0xF);
tmp = tmp >> 4;
@ -42,13 +51,15 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
buf[i++] = frame->DLC;
// add data bytes
for (j = 0; j < frame->DLC; j++) {
for (j = 0; j < frame->DLC; j++)
{
buf[i++] = (frame->Data[j] >> 4);
buf[i++] = (frame->Data[j] & 0x0F);
}
// convert to ASCII (2nd character to end)
for (j = 1; j < i; j++) {
for (j = 1; j < i; j++)
{
if (buf[j] < 0xA) {
buf[j] += 0x30;
} else {
@ -63,12 +74,15 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame) {
return i;
}
int8_t slcan_parse_str(uint8_t *buf, uint8_t len) {
int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
{
CanTxMsgTypeDef frame;
uint8_t i;
// convert from ASCII (2nd character to end)
for (i = 1; i < len; i++) {
for (i = 1; i < len; i++)
{
// lowercase letters
if(buf[i] >= 'a')
buf[i] = buf[i] - 'a' + 10;
@ -80,7 +94,8 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len) {
buf[i] = buf[i] - '0';
}
if (buf[0] == 'O') {
if (buf[0] == 'O')
{
// open channel command
can_enable();
return 0;
@ -92,43 +107,45 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len) {
} else if (buf[0] == 'S') {
// set bitrate command
switch(buf[1]) {
case 0:
can_set_bitrate(CAN_BITRATE_10K);
break;
case 1:
can_set_bitrate(CAN_BITRATE_20K);
break;
case 2:
can_set_bitrate(CAN_BITRATE_50K);
break;
case 3:
can_set_bitrate(CAN_BITRATE_100K);
break;
case 4:
can_set_bitrate(CAN_BITRATE_125K);
break;
case 5:
can_set_bitrate(CAN_BITRATE_250K);
break;
case 6:
can_set_bitrate(CAN_BITRATE_500K);
break;
case 7:
can_set_bitrate(CAN_BITRATE_750K);
break;
case 8:
can_set_bitrate(CAN_BITRATE_1000K);
break;
default:
// invalid setting
return -1;
switch(buf[1])
{
case 0:
can_set_bitrate(CAN_BITRATE_10K);
break;
case 1:
can_set_bitrate(CAN_BITRATE_20K);
break;
case 2:
can_set_bitrate(CAN_BITRATE_50K);
break;
case 3:
can_set_bitrate(CAN_BITRATE_100K);
break;
case 4:
can_set_bitrate(CAN_BITRATE_125K);
break;
case 5:
can_set_bitrate(CAN_BITRATE_250K);
break;
case 6:
can_set_bitrate(CAN_BITRATE_500K);
break;
case 7:
can_set_bitrate(CAN_BITRATE_750K);
break;
case 8:
can_set_bitrate(CAN_BITRATE_1000K);
break;
default:
// invalid setting
return -1;
}
return 0;
} else if (buf[0] == 'm' || buf[0] == 'M') {
// set mode command
if (buf[1] == 1) {
if (buf[1] == 1)
{
// mode 1: silent
can_set_silent(1);
} else {

View File

@ -1,60 +1,8 @@
/**
******************************************************************************
* File Name : stm32f0xx_hal_msp.c
* Date : 05/12/2014 20:22:27
* Description : This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
*
* COPYRIGHT(c) 2014 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f0xx_hal.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* Initializes the Global MSP.
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
/* System interrupt init*/
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
@ -63,9 +11,6 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
GPIO_InitTypeDef GPIO_InitStruct;
if(hcan->Instance==CAN)
{
/* USER CODE BEGIN CAN_MspInit 0 */
/* USER CODE END CAN_MspInit 0 */
/* Peripheral clock enable */
__CAN_CLK_ENABLE();
@ -80,9 +25,6 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN CAN_MspInit 1 */
/* USER CODE END CAN_MspInit 1 */
}
}
@ -92,9 +34,6 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
if(hcan->Instance==CAN)
{
/* USER CODE BEGIN CAN_MspDeInit 0 */
/* USER CODE END CAN_MspDeInit 0 */
/* Peripheral clock disable */
__CAN_CLK_DISABLE();
@ -104,14 +43,6 @@ void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan)
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9);
/* USER CODE BEGIN CAN_MspDeInit 1 */
/* USER CODE END CAN_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View File

@ -1,81 +1,25 @@
/**
******************************************************************************
* @file stm32f0xx_it.c
* @date 05/12/2014 20:22:27
* @brief Interrupt Service Routines.
******************************************************************************
*
* COPYRIGHT(c) 2014 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
//
// interrupts: handle global system interrupts
//
#include "stm32f0xx_hal.h"
#include "stm32f0xx.h"
#include "stm32f0xx_it.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern PCD_HandleTypeDef hpcd_USB_FS;
/******************************************************************************/
/* Cortex-M0 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles USB global Interrupt (combined with EXTI line 18).
*/
// Handle USB interrupts
void USB_IRQHandler(void)
{
/* USER CODE BEGIN USB_IRQn 0 */
/* USER CODE END USB_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_IRQn 1 */
/* USER CODE END USB_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
// Handle SysTick interrupt
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/