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

View File

@ -9,6 +9,7 @@
extern PCD_HandleTypeDef hpcd_USB_FS; extern PCD_HandleTypeDef hpcd_USB_FS;
// Handle USB interrupts // Handle USB interrupts
void USB_IRQHandler(void) void USB_IRQHandler(void)
{ {

View File

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

View File

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

View File

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