Changeset - 021012fc2f31
[Not reviewed]
default
0 6 0
matthewreed - 7 years ago 2017-05-26 22:16:53

Updated protocol features including config and fixed CAN issue.
6 files changed with 69 insertions and 23 deletions:
0 comments (0 inline, 0 general)
inc/led.h
Show inline comments
 
#ifndef _LED_H_
 
#define _LED_H_
 
 
#include "stm32f0xx_hal.h"
 
#include <stdbool.h>
 
#include <string.h>
 
 
typedef enum {
 
    LED_STATUS = 0,
 
    LED_CAN,
 
    LED_ERROR,
 
} led_name_t;
 
 
void led_init(void);
 
void led_start(led_name_t led);
 
void led_start_time(led_name_t led, uint16_t time);
 
void led_blink(led_name_t led, uint16_t period);
 
void led_stop(led_name_t led);
 
void led_blink_once(led_name_t led, uint16_t period);
 
void led_update(led_name_t led);
 
void led_update_all(void);
 
void led_set(led_name_t led, bool value);
 
void led_toggle(led_name_t led);
 
bool led_set_brightness(uint8_t brightness);
 
 
bool gpio_set_led_brightness(uint8_t brightness);
 
bool gpio_set_led(led_name_t led, bool value);
 
bool gpio_toggle_led(led_name_t led);
 
 
#endif /* _LED_H_ */
inc/protocol.h
Show inline comments
 
@@ -39,36 +39,38 @@ typedef enum {
 
 
typedef enum {
 
    NONE = 0x0000,
 
    DIGITAL_INPUT = 0x0001,
 
    FREQ_INPUT = 0x0002,
 
    ANALOG_INPUT = 0x0003,
 
    DIGITAL_OUTPUT = 0x0004,
 
    FREQ_OUTPUT = 0x0005,
 
    ANALOG_OUTPUT = 0x0006,
 
    AIR_TEMP = 0x0007,
 
    AIR_HUMIDITY = 0x0008,
 
    AIR_PRESSURE = 0x0009,
 
    AMBIENT_LIGHT = 0x000A,
 
    WATER_TEMP = 0x000B,
 
    WATER_LEVEL = 0x000C,
 
    WATER_CONDUCTIVITY = 0x000D,
 
    WATER_PH = 0x000E,
 
    CAN_ID = 0x0100,
 
    DATA_RATE = 0x0101,
 
    GPIO = 0x0102,
 
    LED_BRIGHTNESS = 0x0103,
 
} protocol_data_key_t;
 
 
void protocol_init(protocol_device_t device);
 
flash_settings_t* protocol_get_settings(void);
 
bool protocol_receive_message(CanRxMsgTypeDef* can_message);
 
bool protocol_send_message(protocol_message_t* message);
 
bool protocol_process_message(protocol_message_t* message);
 
bool protocol_send_data(protocol_data_key_t key, uint8_t sensor, float data);
 
bool protocol_send_test();
 
bool _protocol_config(protocol_message_t* message);
 
 
bool protocol_estop(bool value);
 
bool protocol_set_output(protocol_message_t* message);
 
bool protocol_get_data(protocol_message_t* message);
 
bool protocol_config(protocol_message_t* message);
 
 
#endif //_PROTOCOL_H_
src/can.c
Show inline comments
 
@@ -104,35 +104,36 @@ void can_set_receive_id(uint32_t id)
 
    can_filter.FilterMode = CAN_FILTERMODE_IDMASK;
 
    can_filter.FilterScale = CAN_FILTERSCALE_32BIT;
 
    can_filter.FilterIdHigh = (id >> 16) & 0xFFFF;
 
    can_filter.FilterIdLow = id & 0xFFFF;
 
    can_filter.FilterActivation = ENABLE;
 
 
    HAL_CAN_ConfigFilter(&can_handle, &can_filter);
 
 
}
 
 
bool can_silence_bus(bool value)
 
{
 
    bool result = true;
 
 
    can_silenced = value;
 
 
    return result;
 
}
 
 
void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* hcan)
 
{
 
    if ((hcan->pRxMsg->StdId == can_rx_id) | (hcan->pRxMsg->StdId == can_broadcast_id))
 
    {
 
        protocol_receive_message(hcan->pRxMsg);
 
    }
 
 
    led_blink_once(LED_CAN, 500);
 
        HAL_CAN_Receive_IT(&can_handle, CAN_FIFO0);
 
    }
 
    led_start_time(LED_CAN, 500);
 
}
 
 
void HAL_CAN_TxCpltCallback(CAN_HandleTypeDef* hcan) {}
 
 
void HAL_CAN_ErrorCallback(CAN_HandleTypeDef* hcan)
 
{
 
    led_set(LED_ERROR, 1);
 
}
src/flash.c
Show inline comments
 
@@ -18,48 +18,50 @@ void flash_restore(flash_settings_t* tor
 
    uint16_t i;
 
    for(i = 0; i < (sizeof(flash_settings_t)/2); i++)
 
    {
 
        torestore->data[i] = eeprom[i];
 
        checksum ^= torestore->data[i];
 
    }
 
 
    //if checksum doesn't match, load default settings
 
    if((checksum ^ eeprom[i+1]) != 0) {
 
        flash_load_defaults(torestore);
 
    }
 
}
 
 
void flash_load_defaults(flash_settings_t* torestore)
 
{
 
 
    torestore->val.can_id = DEFAULT_CAN_ID;
 
    torestore->val.data_rate = DEFAULT_DATA_RATE;
 
    torestore->val.led_brightness = DEFAULT_LED_BRIGHTNESS;
 
 
}
 
 
static void __flash_write(flash_settings_t* tosave)
 
{
 
    //TODO: Check to see if anything has changed before saving
 
 
    // Erase mem
 
    HAL_FLASH_Unlock();
 
 
    // Erase the FLASH pages
 
    FLASH_EraseInitTypeDef erase;
 
    erase.TypeErase = TYPEERASE_PAGES;
 
    erase.PageAddress = (uint32_t) eeprom;
 
    erase.NbPages = 1;
 
    uint32_t SectorError = 0;
 
    HAL_FLASHEx_Erase(&erase, &SectorError);
 
    CLEAR_BIT(FLASH->CR, FLASH_CR_PER);
 
 
    // write to flash and calculate the checksum
 
    uint16_t checksum = MAGIC_NUMBER;
 
    uint16_t i;
 
    for(i = 0; i < (sizeof(flash_settings_t)/2); i++)
 
    {
 
        HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t)&eeprom[i], tosave->data[i]);
 
        checksum ^= tosave->data[i];
 
    }
 
 
    // write the checksum
 
    HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, (uint32_t)&eeprom[i+1], checksum);
 
src/led.c
Show inline comments
 
#include "led.h"
 
 
#define LED_CYCLE_STATUS 2000
 
#define LED_CYCLE_CAN 0
 
#define LED_CYCLE_ERROR 0
 
 
uint16_t led_timers[3];
 
uint16_t led_thresholds[3];
 
uint16_t led_cycles[3];
 
 
void led_init(void)
 
{
 
    memset(led_timers, 0, sizeof led_timers);
 
    memset(led_thresholds, 0, sizeof led_thresholds);
 
    memset(led_cycles, 0, sizeof led_cycles);
 
 
    led_cycles[LED_STATUS] = LED_CYCLE_STATUS;
 
    led_cycles[LED_CAN] = LED_CYCLE_CAN;
 
    led_cycles[LED_ERROR] = LED_CYCLE_ERROR;
 
    memset(led_timers, 0, sizeof(led_timers));
 
    memset(led_thresholds, 0, sizeof(led_thresholds));
 
    memset(led_cycles, 0, sizeof(led_cycles));
 
}
 
 
void led_start(led_name_t led)
 
void led_blink(led_name_t led, uint16_t period)
 
{
 
    led_timers[led] = led_cycles[led];
 
    led_thresholds[led] = led_cycles[led]/2;
 
    led_timers[led] = period;
 
    led_thresholds[led] = period/2;
 
    led_cycles[led] = period;
 
}
 
 
void led_start_time(led_name_t led, uint16_t time)
 
void led_stop(led_name_t led)
 
{
 
    led_timers[led] = 0;
 
    led_thresholds[led] = 0;
 
    led_cycles[led] = 0;
 
    led_set(led, 0);
 
}
 
 
void led_blink_once(led_name_t led, uint16_t period)
 
{
 
    if (led_timers[led] == 0)
 
    {
 
        led_timers[led] = time;
 
        led_thresholds[led] = time/2;
 
        led_timers[led] = period;
 
        led_thresholds[led] = period/2;
 
    }
 
}
 
 
void led_update(led_name_t led)
 
{
 
    if (led_timers[led] > led_thresholds[led])
 
    {
 
        led_set(led, 1);
 
        led_timers[led]--;
 
    }
 
    else if (led_timers[led] > 0)
 
    {
 
        led_set(led, 0);
 
        led_timers[led]--;
 
    }
 
    else if (led_cycles[led] > 0)
 
    {
 
        led_set(led, 0);
 
        led_timers[led] = led_cycles[led];
 
    }
 
}
 
 
void led_update_all(void)
 
{
 
    led_update(LED_STATUS);
 
    led_update(LED_CAN);
 
    led_update(LED_ERROR);
 
}
 
 
void led_set(led_name_t led, bool value)
 
{
 
    gpio_set_led(led, value);
 
}
 
 
void led_toggle(led_name_t led)
 
{
 
    gpio_toggle_led(led);
 
}
 
 
bool led_set_brightness(uint8_t brightness)
 
{
 
    return gpio_set_led_brightness(brightness);
 
}
 
 
__weak bool gpio_set_led(led_name_t led, bool value)
 
{
 
    return false;
 
}
 
 
__weak bool gpio_toggle_led(led_name_t led)
 
{
 
    return false;
 
}
 
 
__weak bool gpio_set_led_brightness(uint8_t brightness)
 
{
 
    return false;
 
}
 
src/protocol.c
Show inline comments
 
#include "protocol.h"
 
 
protocol_device_t protocol_device;
 
flash_settings_t protocol_settings;
 
 
void protocol_init(protocol_device_t device)
 
{
 
    protocol_device = device;
 
    flash_restore(&protocol_settings);
 
 
    can_init(protocol_settings.val.can_id, DEFAULT_BROADCAST_ID);
 
}
 
 
flash_settings_t* protocol_get_settings(void)
 
{
 
    return &protocol_settings;
 
}
 
 
bool protocol_send_test()
 
{
 
    bool result = true;
 
    can_send_test(protocol_settings.val.can_id | 0x00000001);
 
    return result;
 
}
 
 
bool protocol_receive_message(CanRxMsgTypeDef* can_message)
 
{
 
    bool result = true;
 
    
 
    protocol_message_t message;
 
    message.command = can_message->Data[0] & 0x80;
 
    message.id = can_message->Data[0] & 0x7F;
 
    message.key = (can_message->Data[1] << 8) | can_message->Data[2];
 
    message.sensor = can_message->Data[3];
 
    message.data.byte_data[0] = can_message->Data[4];
 
    message.data.byte_data[1] = can_message->Data[5];
 
    message.data.byte_data[2] = can_message->Data[6];
 
    message.data.byte_data[3] = can_message->Data[7];
 
    
 
    protocol_process_message(&message);
 
    
 
    return result;
 
@@ -69,72 +74,94 @@ bool protocol_process_message(protocol_m
 
                    //call estop weak function
 
                    result = protocol_estop(message->data.float_data!=0.0f);
 
                }
 
                break;
 
            case SILENCE_BUS:
 
                {
 
                    //silence can bus
 
                    result = can_silence_bus(message->data.float_data!=0.0f);
 
                }
 
                break;
 
            case SET_OUTPUT:
 
                {
 
                    //call set output weak function
 
                    result = protocol_set_output(message);
 
                }
 
                break;
 
            case GET_DATA:
 
                {
 
                    //call get data weak function
 
                    result = protocol_get_data(message);
 
                }
 
                break;
 
            case CONFIG:
 
                {
 
                    //call config weak function
 
                    result = protocol_config(message);
 
                    result = _protocol_config(message);
 
                }
 
                break;
 
            default:
 
                result = false;
 
                break;
 
        }
 
    }
 
    
 
    return result;
 
}
 
 
bool protocol_send_data(protocol_data_key_t key, uint8_t sensor, float data)
 
{
 
    bool result = true;
 
 
    protocol_message_t message;
 
    message.command = false;
 
    message.id = protocol_device;
 
    message.key = key;
 
    message.sensor = sensor;
 
    message.data.float_data = data;
 
 
    result = protocol_send_message(&message);
 
 
    return result;
 
}
 
 
bool _protocol_config(protocol_message_t* message)
 
{
 
    bool result = false;
 
 
    if (message->key == LED_BRIGHTNESS)
 
    {
 
        uint8_t brightness = (uint8_t)message->data.float_data;
 
        result = led_set_brightness(brightness);
 
        protocol_settings.val.led_brightness = brightness;
 
    }
 
    else if (message->key == DATA_RATE)
 
    {
 
        uint16_t data_rate = (uint16_t) message->data.float_data;
 
        protocol_settings.val.data_rate = data_rate;
 
    }
 
    else
 
    {
 
        //call config weak function
 
        result = protocol_config(message);
 
    }
 
    return result;
 
}
 
 
__weak bool protocol_estop(bool value)
 
{
 
    return false;
 
}
 
 
__weak bool protocol_set_output(protocol_message_t* message)
 
{
 
    return false;
 
}
 
 
__weak bool protocol_get_data(protocol_message_t* message)
 
{
 
    return false;
 
}
 
 
__weak bool protocol_config(protocol_message_t* message)
 
{
 
    return false;
 
}
0 comments (0 inline, 0 general)