Indentation and commenting, no functional changes

This commit is contained in:
Ethan Zonca 2019-01-01 12:24:33 -05:00
parent 430db8223f
commit f6a60edd48
5 changed files with 63 additions and 68 deletions

View File

@ -9,19 +9,13 @@
#include "led.h"
// Private variables
static CAN_HandleTypeDef can_handle;
static CAN_FilterConfTypeDef filter;
static uint32_t prescaler;
enum can_bus_state bus_state;
static volatile uint8_t process_recv = 0;
static volatile uint8_t process_tx = 0;
static uint8_t can_nart = DISABLE;
static CanRxMsgTypeDef can_rx_msg;
static CanTxMsgTypeDef can_tx_msg;
// Initialize CAN peripheral settings, but don't actually start the peripheral
void can_init(void)
@ -65,10 +59,6 @@ void can_enable(void)
{
if (bus_state == OFF_BUS)
{
//HAL_NVIC_SetPriority(CEC_CAN_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(CEC_CAN_IRQn);
can_handle.Init.Prescaler = prescaler;
can_handle.Init.Mode = CAN_MODE_NORMAL;
can_handle.Init.SJW = CAN_SJW_1TQ;
@ -80,16 +70,12 @@ void can_enable(void)
can_handle.Init.NART = can_nart;
can_handle.Init.RFLM = DISABLE;
can_handle.Init.TXFP = DISABLE;
can_handle.pTxMsg = NULL; //&can_tx_msg;
//can_handle.pRxMsg = &can_rx_msg;
can_handle.pTxMsg = NULL;
HAL_CAN_Init(&can_handle);
HAL_CAN_ConfigFilter(&can_handle, &filter);
bus_state = ON_BUS;
led_blue_on();
// HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0);
}
}
@ -99,13 +85,10 @@ void can_disable(void)
{
if (bus_state == ON_BUS)
{
// do a bxCAN reset (set RESET bit to 1)
// Do a bxCAN reset (set RESET bit to 1)
can_handle.Instance->MCR |= CAN_MCR_RESET;
bus_state = OFF_BUS;
// HAL_NVIC_DisableIRQ(CEC_CAN_IRQn);
// HAL_CAN_DeInit(&can_handle);
led_green_on();
}
}
@ -179,7 +162,7 @@ void can_set_autoretransmit(uint8_t autoretransmit)
{
if (bus_state == ON_BUS)
{
// cannot set silent mode while on bus
// Cannot set autoretransmission while on bus
return;
}
if (autoretransmit)
@ -198,15 +181,16 @@ uint32_t can_tx(CanTxMsgTypeDef *tx_msg)
{
uint32_t status;
// transmit can frame
// Transmit can frame
can_handle.pTxMsg = tx_msg;
// status = HAL_CAN_Transmit_IT(&can_handle);
status = HAL_CAN_Transmit(&can_handle, 10);
// led_blue_on();
led_green_on();
return status;
}
// Receive message from the CAN bus (blocking)
uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout)
{
uint32_t status;

View File

@ -9,18 +9,19 @@
extern PCD_HandleTypeDef hpcd_USB_FS;
// Handle USB interrupts
void USB_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd_USB_FS);
HAL_PCD_IRQHandler(&hpcd_USB_FS);
}
// Handle SysTick interrupt
void SysTick_Handler(void)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}

View File

@ -5,6 +5,8 @@
#include "stm32f0xx_hal.h"
#include "led.h"
// Private variables
static volatile uint32_t led_blue_laston = 0;
static volatile uint32_t led_green_laston = 0;
static uint32_t led_blue_lastoff = 0;
@ -61,6 +63,7 @@ void led_blue_blink(uint8_t numblinks)
}
}
// Attempt to turn on status LED
void led_blue_on(void)
{
@ -84,6 +87,7 @@ void led_process(void)
led_blue_laston = 0;
led_blue_lastoff = HAL_GetTick();
}
// If LED has been on for long enough, turn it off
if(led_green_laston > 0 && HAL_GetTick() - led_green_laston > LED_DURATION)
{

View File

@ -14,8 +14,8 @@
int main(void)
{
// Initialize peripherals
HAL_Init();
system_init();
can_init();
led_init();
@ -23,34 +23,34 @@ int main(void)
led_blue_blink(2);
// loop forever
// Storage for status and recieved message buffer
CanRxMsgTypeDef rx_msg;
uint32_t status;
uint8_t msg_buf[SLCAN_MTU];
uint16_t msg_len = 0;
while(1)
{
// Block until a CAN message is recieved
while (!is_can_msg_pending(CAN_FIFO0))
led_process();
status = can_rx(&rx_msg, 3);
if (status == HAL_OK) {
msg_len = slcan_parse_frame((uint8_t *)&msg_buf, &rx_msg);
// If message received from bus, parse the frame
if (status == HAL_OK)
{
msg_len = slcan_parse_frame((uint8_t *)&msg_buf, &rx_msg);
if(msg_len)
{
CDC_Transmit_FS(msg_buf, msg_len);
}
// Transmit message via USB-CDC
if(msg_len)
{
CDC_Transmit_FS(msg_buf, msg_len);
}
}
led_process();
//can_process();
//usb_process();
}
}

View File

@ -1,10 +1,13 @@
//
// slcan: Parse incoming and generate outgoing slcan messages
//
#include "stm32f0xx_hal.h"
#include "can.h"
#include "slcan.h"
// Parse an incoming CAN frame into an outgoing slcan message
int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame)
{
uint8_t i = 0;
@ -16,7 +19,7 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame)
buf[j] = '\0';
}
// add character for frame type
// Add character for frame type
if (frame->RTR == CAN_RTR_DATA)
{
buf[i] = 't';
@ -24,40 +27,40 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame)
buf[i] = 'r';
}
// assume standard identifier
// Assume standard identifier
id_len = SLCAN_STD_ID_LEN;
tmp = frame->StdId;
// check if extended
// Check if extended
if (frame->IDE == CAN_ID_EXT)
{
// convert first char to upper case for extended frame
// Convert first char to upper case for extended frame
buf[i] -= 32;
id_len = SLCAN_EXT_ID_LEN;
tmp = frame->ExtId;
}
i++;
// add identifier to buffer
// Add identifier to buffer
for(j=id_len; j > 0; j--)
{
// add nybble to buffer
// Add nybble to buffer
buf[j] = (tmp & 0xF);
tmp = tmp >> 4;
i++;
}
// add DLC to buffer
// Add DLC to buffer
buf[i++] = frame->DLC;
// add data bytes
// Add data bytes
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)
// Convert to ASCII (2nd character to end)
for (j = 1; j < i; j++)
{
if (buf[j] < 0xA) {
@ -67,46 +70,47 @@ int8_t slcan_parse_frame(uint8_t *buf, CanRxMsgTypeDef *frame)
}
}
// add carrage return (slcan EOL)
// Add carrage return (slcan EOL)
buf[i++] = '\r';
// return number of bytes in string
// Return number of bytes in string
return i;
}
// Parse an incoming slcan command from the USB CDC port
int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
{
CanTxMsgTypeDef frame;
uint8_t i;
// convert from ASCII (2nd character to end)
// Convert from ASCII (2nd character to end)
for (i = 1; i < len; i++)
{
// lowercase letters
// Lowercase letters
if(buf[i] >= 'a')
buf[i] = buf[i] - 'a' + 10;
// uppercase letters
// Uppercase letters
else if(buf[i] >= 'A')
buf[i] = buf[i] - 'A' + 10;
// numbers
// Numbers
else
buf[i] = buf[i] - '0';
}
if (buf[0] == 'O')
{
// open channel command
// Open channel command
can_enable();
return 0;
} else if (buf[0] == 'C') {
// close channel command
// Close channel command
can_disable();
return 0;
} else if (buf[0] == 'S') {
// set bitrate command
// Set bitrate command
switch(buf[1])
{
case 0:
@ -143,42 +147,43 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
return 0;
} else if (buf[0] == 'm' || buf[0] == 'M') {
// set mode command
// Set mode command
if (buf[1] == 1)
{
// mode 1: silent
// Mode 1: silent
can_set_silent(1);
} else {
// default to normal mode
// Default to normal mode
can_set_silent(0);
}
return 0;
} else if (buf[0] == 'a' || buf[0] == 'A') {
// set autoretry command
// Set autoretry command
if (buf[1] == 1)
{
// mode 1: autoretry enabled (default)
// Mode 1: autoretry enabled (default)
can_set_autoretransmit(1);
} else {
// mode 0: autoretry disabled
// Mode 0: autoretry disabled
can_set_autoretransmit(0);
}
return 0;
} else if (buf[0] == 't' || buf[0] == 'T') {
// transmit data frame command
// Transmit data frame command
frame.RTR = CAN_RTR_DATA;
} else if (buf[0] == 'r' || buf[0] == 'R') {
// transmit remote frame command
// Transmit remote frame command
frame.RTR = CAN_RTR_REMOTE;
} else {
// error, unknown command
// Error, unknown command
return -1;
}
// Check for extended or standard frame (set by case)
if (buf[0] == 't' || buf[0] == 'r') {
frame.IDE = CAN_ID_STD;
} else if (buf[0] == 'T' || buf[0] == 'R') {
@ -208,6 +213,7 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
}
// Attempt to parse DLC and check sanity
frame.DLC = buf[i++];
if (frame.DLC < 0 || frame.DLC > 8) {
return -1;
@ -219,9 +225,9 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
i += 2;
}
// send the message
// can_preptx(&frame);
// Transmit the message
can_tx(&frame);
return 0;
}