From: Nicolas D. <Ba...@us...> - 2011-05-30 21:59:11
|
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "krobot". The branch, master has been updated via 2ee1763a0f7bd92e6e37a25b1e4bf17c0351290d (commit) via 900dfc8c3c7319040141e12d7f896831d604dfd2 (commit) via d79681660ec191abf8c383634f6e937bf06c4acf (commit) via 722916138bde625e4a56e8c77bf7d11a9b34cadc (commit) from 7ad67e53ff536078ecef7132f604d5957472f782 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 2ee1763a0f7bd92e6e37a25b1e4bf17c0351290d Author: Nicolas Dandrimont <Nic...@cr...> Date: Mon May 30 23:58:19 2011 +0200 [USB_CAN] Move battery monitoring to the Sensor_Actuator board commit 900dfc8c3c7319040141e12d7f896831d604dfd2 Author: Nicolas Dandrimont <Nic...@cr...> Date: Mon May 30 23:53:44 2011 +0200 [Sensor_Actuator] Add ADC management commit d79681660ec191abf8c383634f6e937bf06c4acf Author: Nicolas Dandrimont <Nic...@cr...> Date: Mon May 30 23:51:25 2011 +0200 [Sensor_Actuator] Silence SAM3X warnings commit 722916138bde625e4a56e8c77bf7d11a9b34cadc Author: Nicolas Dandrimont <Nic...@cr...> Date: Mon May 30 23:50:38 2011 +0200 [Sensor_Actuator] Enhance switches management ----------------------------------------------------------------------- Changes: diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.c new file mode 100644 index 0000000..434790f --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.c @@ -0,0 +1,35 @@ +#include "adc.h" + +void sa_adc_init(void) { + + // Initialize the pins in the Analog IN mode + // (should be the default, but anyway...) + stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE, + BV(0) | BV(1) | BV(2) | BV(3), + GPIO_MODE_AIN, + GPIO_SPEED_50MHZ); + + stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, + BV(0) | BV(1) | BV(2) | BV(3), + GPIO_MODE_AIN, + GPIO_SPEED_50MHZ); + + + // BeRTOS ADC initialization + adc_init(); + + // Yep, that's all folks... +} + +void get_adc_values(adc_values *pkt1, adc_values *pkt2) { + pkt1->p.val1 = adc_read(10); + pkt1->p.val2 = adc_read(11); + pkt1->p.val3 = adc_read(12); + pkt1->p.val4 = adc_read(13); + + pkt2->p.val1 = adc_read(0); + pkt2->p.val2 = adc_read(1); + pkt2->p.val3 = adc_read(2); + pkt2->p.val4 = adc_read(3); + +} diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.h new file mode 100644 index 0000000..44dba3e --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/adc/adc.h @@ -0,0 +1,47 @@ +/** + * Sensor and Actuator Board : ADC Interfacing + * + * This file contains the interface for the ADCs + * + * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> + * Authors: Nicolas Dandrimont <ol...@cr...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifndef ADC_H__ +#define ADC_H__ + +#include <stdlib.h> +#include <stdint.h> + +#include <drv/adc.h> +#include <drv/clock_stm32.h> +#include <drv/gpio_stm32.h> + +#include "../can/can_messages.h" + +/** + * Initialize the ADCs + */ +void sa_adc_init(void); + +/** + * Fetch the ADC data in the CAN packets + */ +void get_adc_values(adc_values *pkt1, adc_values *pkt2); + + +#endif diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.c new file mode 100644 index 0000000..031ff29 --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.c @@ -0,0 +1,25 @@ +/* + * Wrapper to use the ADS7828 analog to digital converter + * Olivier Bichler + */ + +#include "ads7828.h" + +unsigned int adc7828_measure(I2c* i2c, unsigned char addr, unsigned char ch) { + unsigned char value[2] = {0}; + unsigned char cmd = ADS7828_CMD_PD0 + | ADS7828_CMD_PD1 + | ((ch/2) << 4) + | ADS7828_CMD_SD; + + if (ch % 2) + cmd|= ADS7828_CMD_C2; + + i2c_start_w(i2c, addr, 1, I2C_STOP); + i2c_write(i2c, &cmd, 1); + i2c_start_r(i2c, addr, 2, I2C_STOP); + i2c_read(i2c, &value, 2); + + return ((value[0] << 8) | value[1]); +} + diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.h new file mode 100644 index 0000000..5622fa8 --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/ads7828.h @@ -0,0 +1,29 @@ +/* + * Wrapper to use the ADS7828 analog to digital converter + * Olivier Bichler + */ + +#ifndef HEADER__ADS7828 +#define HEADER__ADS7828 + +#define ADS7828_VREF 2.5 +#define ADS7828_RES 12 +#define ADS7828_LSB (ADS7828_VREF/((1 << ADS7828_RES) - 1.0)) + +#define ADS7828_ADDR_BASE 0x90 +#define ADS7828_ADDR_A0 0x02 +#define ADS7828_ADDR_A1 0x04 + +#define ADS7828_CMD_PD0 0x04 +#define ADS7828_CMD_PD1 0x08 +#define ADS7828_CMD_C0 0x10 +#define ADS7828_CMD_C1 0x20 +#define ADS7828_CMD_C2 0x40 +#define ADS7828_CMD_SD 0x80 + +#include <drv/i2c.h> + +unsigned int adc7828_measure(I2c* i2c, unsigned char addr, unsigned char ch); + +#endif + diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.c new file mode 100644 index 0000000..31f5175 --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.c @@ -0,0 +1,82 @@ +/** + * Battery Monitoring wrapper for the USB CAN Board + * + * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> + * Authors: Nicolas Dandrimont <ol...@cr...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "battery_monitoring.h" + +PROC_DEFINE_STACK(stack_battery_monitoring, KERN_MINSTACKSIZE * 16); + +static battery_monitoring_ctx _ctx; +battery_monitoring_ctx *ctx = &_ctx; + +static void NORETURN battery_monitoring_process(void) { + + can_tx_frame txf; + can_rx_frame rxf; + + battery_status b; + + uint8_t ch; + uint16_t value = 42; + + txf.ide = rxf.ide = 1; + txf.rtr = rxf.rtr = 0; + txf.dlc = rxf.dlc = sizeof(b.p); + + for (;;) { + // Battery 1 + for (ch = 0; ch < 4; ++ch) { + value = adc7828_measure(ctx->i2c, ADS7828_ADDR_BASE, ch); + b.p.elem[ch] = (uint16_t)(2.0*value*ADS7828_LSB * 10000.); + } + + txf.eid = rxf.eid = 401; + txf.data32[0] = rxf.data32[0] = b.d[0]; + txf.data32[1] = rxf.data32[1] = b.d[1]; + + usb_can_emit(ctx->usbcan, &rxf); + can_transmit(ctx->usbcan->can, &txf, ms_to_ticks(10)); + + // Battery 2 + for (ch = 0; ch < 4; ++ch) { + value = adc7828_measure(ctx->i2c, ADS7828_ADDR_BASE + 2, ch); + b.p.elem[ch] = (uint16_t)(2.0*value*ADS7828_LSB * 10000.); + } + + txf.eid = rxf.eid = 402; + txf.data32[0] = rxf.data32[0] = b.d[0]; + txf.data32[1] = rxf.data32[1] = b.d[1]; + + usb_can_emit(ctx->usbcan, &rxf); + can_transmit(ctx->usbcan->can, &txf, ms_to_ticks(10)); + + timer_delay(100); + } +} + +battery_monitoring_ctx *battery_monitoring_init(usb_can *usbcan, I2c *i2c) { + ctx->usbcan = usbcan; + ctx->i2c = i2c; + + proc_new(battery_monitoring_process, NULL, sizeof(stack_battery_monitoring), stack_battery_monitoring); + + + return ctx; +} diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.h new file mode 100644 index 0000000..6075c1e --- /dev/null +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/battery_monitoring/battery_monitoring.h @@ -0,0 +1,47 @@ +/** + * Battery Monitoring wrapper for the USB CAN Board + * + * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> + * Authors: Nicolas Dandrimont <ol...@cr...> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +#ifndef BATTERY_MONITORING_H +#define BATTERY_MONITORING_H + +#include <drv/can.h> +#include <drv/i2c.h> + +#include "../usb_can/usb_can.h" +#include "ads7828.h" + +typedef struct { + usb_can *usbcan; + I2c *i2c; +} battery_monitoring_ctx; + +struct battery_status_pkt { + uint16_t elem[4] __attribute__((packed)); // in 1/10000th volts [0; 6.5536[ +}; + +typedef union { + struct battery_status_pkt p; + uint32_t d[2]; +} battery_status; + +battery_monitoring_ctx *battery_monitoring_init(usb_can *usbcan, I2c *i2c); + +#endif diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_messages.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_messages.h index 9dac070..512077e 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_messages.h +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_messages.h @@ -38,6 +38,14 @@ #define CAN_SWITCH_STATUS_1 311 // switch_status #define CAN_SWITCH_STATUS_2 312 // switch_status +#define CAN_SWITCH_SET 313 // switch_request + + +// ADC + +#define CAN_ADC_VALUES_1 321 // adc_values +#define CAN_ADC_VALUES_2 322 // adc_values + /****************************************************************************/ @@ -90,6 +98,23 @@ struct switch_status_pkt { uint8_t sw8; }; +// Switch request +struct switch_request_pkt { + uint8_t num; + uint8_t state; +}; + +/** + * ADC messages + */ +// ADC Values +struct adc_values_pkt { + uint16_t val1; + uint16_t val2; + uint16_t val3; + uint16_t val4; +} __attribute__((packed)); + /****************************************************************************/ /** @@ -116,4 +141,14 @@ typedef union { uint32_t d[2]; } switch_status; +typedef union { + struct switch_request_pkt p; + uint32_t d[2]; +} switch_request; + +typedef union { + struct adc_values_pkt p; + uint32_t d[2]; +} adc_values; + #endif diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.c index c777fe4..80bab34 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.c +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.c @@ -26,6 +26,29 @@ PROC_DEFINE_STACK(stack_send, KERN_MINSTACKSIZE * 4) PROC_DEFINE_STACK(stack_recv, KERN_MINSTACKSIZE * 4) +static void NORETURN can_sender_process(void); +static void NORETURN can_receiver_process(void); + +void can_processes_init(void) { + can_config cfg; + + cfg.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP; + /* 1 Mbit by default (FIXME: O'RLY?) */ + cfg.btr = CAN_BTR_SJW(0) | CAN_BTR_TS1(8) | CAN_BTR_TS2(1) | CAN_BTR_BRP(6); + + cfg.n_filters = 0; + cfg.filters = NULL; + + /* Initialize CAN driver */ + can_init(); + + can_start(CAND1, &cfg); + + proc_new(can_sender_process, NULL, sizeof(stack_send), stack_send); + proc_new(can_receiver_process, NULL, sizeof(stack_recv), stack_recv); + +} + #define SET_PACKET(f, id, pk) do { \ f.eid = id; \ f.dlc = sizeof(pk.p); \ @@ -44,6 +67,8 @@ static void NORETURN can_sender_process(void) { switch_status st1, st2; + adc_values adc1, adc2; + /* Initialize can frame */ f.ide = 1; @@ -67,7 +92,7 @@ static void NORETURN can_sender_process(void) { /* Switches */ - get_switch_state(&st1, &st2); + get_switch_status(&st1, &st2); SET_PACKET(f, CAN_SWITCH_STATUS_1, st1); can_transmit(CAND1, &f, ms_to_ticks(10)); @@ -75,35 +100,45 @@ static void NORETURN can_sender_process(void) { SET_PACKET(f, CAN_SWITCH_STATUS_2, st2); can_transmit(CAND1, &f, ms_to_ticks(10)); - timer_waitEvent(&timer_send); - } + /* ADC */ -} + get_adc_values(&adc1, &adc2); -static void NORETURN can_receiver_process(void) { + SET_PACKET(f, CAN_ADC_VALUES_1, adc1); + can_transmit(CAND1, &f, ms_to_ticks(10)); - for (;;) { - timer_delay(500); + SET_PACKET(f, CAN_ADC_VALUES_2, adc2); + can_transmit(CAND1, &f, ms_to_ticks(10)); + + + timer_waitEvent(&timer_send); } } -void can_processes_init(void) { - can_config cfg; - - cfg.mcr = CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP; - /* 1 Mbit by default (FIXME: O'RLY?) */ - cfg.btr = CAN_BTR_SJW(0) | CAN_BTR_TS1(8) | CAN_BTR_TS2(1) | CAN_BTR_BRP(6); +#define GET_PACKET(type, name, frame) type name; name.d[0] = frame.data32[0]; name.d[1] = frame.data32[1] - cfg.n_filters = 0; - cfg.filters = NULL; +static void NORETURN can_receiver_process(void) { - /* Initialize CAN driver */ - can_init(); + can_rx_frame f; - can_start(CAND1, &cfg); + int ret; - proc_new(can_sender_process, NULL, sizeof(stack_send), stack_send); - proc_new(can_receiver_process, NULL, sizeof(stack_recv), stack_recv); + for (;;) { + ret = can_receive(CAND1, &f, ms_to_ticks(200)); + if (!ret || f.ide != 1) + continue; + + switch (f.eid) { + case CAN_SWITCH_SET: + do { + GET_PACKET(switch_request, req, f); + set_switch(&req); + } while (0); + break; + default: + break; + } + } } diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.h index 4bab22c..757e3a4 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.h +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/can/can_monitor.h @@ -31,6 +31,7 @@ #include "can_messages.h" +#include "../adc/adc.h" #include "../beacon/beacon.h" #include "../switch/switch.h" diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/main.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/main.c index 9e861b8..8ba8ccb 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/main.c +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/main.c @@ -37,9 +37,10 @@ #include <kern/proc.h> #include <io/kfile.h> +#include "adc/adc.h" #include "beacon/beacon.h" -#include "switch/switch.h" #include "can/can_monitor.h" +#include "switch/switch.h" PROC_DEFINE_STACK(stack_blinky, KERN_MINSTACKSIZE * 2); @@ -68,6 +69,9 @@ static void init(void) // Initialize the switches switch_init(); + // Initialize the ADCs + sa_adc_init(); + // Initialize the CAN bus processing can_processes_init(); diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/sensor_actuator_user.mk b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/sensor_actuator_user.mk index 79cac29..ec821cb 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/sensor_actuator_user.mk +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/sensor_actuator_user.mk @@ -10,10 +10,11 @@ sensor_actuator_PROGRAMMER_PORT = none # Files included by the user. sensor_actuator_USER_CSRC = \ $(sensor_actuator_SRC_PATH)/main.c \ + $(sensor_actuator_SRC_PATH)/adc/adc.c \ $(sensor_actuator_SRC_PATH)/beacon/stm32lib/stm32f10x_tim.c \ $(sensor_actuator_SRC_PATH)/beacon/beacon.c \ - $(sensor_actuator_SRC_PATH)/switch/switch.c \ $(sensor_actuator_SRC_PATH)/can/can_monitor.c \ + $(sensor_actuator_SRC_PATH)/switch/switch.c \ # $(sensor_actuator_SRC_PATH)/ax12/serial.c \ # $(sensor_actuator_SRC_PATH)/ax12/ax12.c \ # @@ -46,4 +47,5 @@ sensor_actuator_USER_CPPAFLAGS = \ sensor_actuator_USER_CPPFLAGS = \ -fno-strict-aliasing \ -fwrapv \ + -DCPU_CM3_SAM3X=0 \ # diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.c b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.c index 180efd4..0dabba9 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.c +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.c @@ -23,38 +23,71 @@ #include "switch.h" +static uint32_t pins[][2] = { + //{GPIOC_BASE, 4}, + //{GPIOC_BASE, 5}, + //{GPIOB_BASE, 0}, + //{GPIOB_BASE, 1}, + //{GPIOB_BASE, 12}, + //{GPIOB_BASE, 15}, + //{GPIOC_BASE, 6}, + //{GPIOC_BASE, 7}, + //{GPIOC_BASE, 8}, + //{GPIOC_BASE, 9}, + //{GPIOA_BASE, 8}, + //{GPIOC_BASE, 12}, + {GPIOA_BASE, 4}, + {GPIOA_BASE, 5}, + {GPIOA_BASE, 6}, + {GPIOA_BASE, 7}, + {GPIOB_BASE, 5}, +}; + void switch_init(void) { // Clock the GPIOs. RCC->APB2ENR |= RCC_APB2_GPIOA | RCC_APB2_GPIOB | RCC_APB2_GPIOC; - // Initialize D1_1 PC(4) and D1_2 PC(5) + // Initialize D1_1 PC(4) and D1_2 PC(5) as inputs stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE, BV(4) | BV(5), GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); - // Initialize D1_3 PB(0), D1_4 PB(1), D1_5 PB(12), D1_6 PB(15) + // Initialize D1_3 PB(0), D1_4 PB(1), D1_5 PB(12), D1_6 PB(15) as inputs stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, BV(0) | BV(1) | BV(12) | BV(15), GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); - // Initialize D2_1 PC(6), D2_2 PC(7), D2_3 PC(8), D2_4 PC(9), D2_6 PC(12) + // Initialize D2_1 PC(6), D2_2 PC(7), D2_3 PC(8), D2_4 PC(9), D2_6 PC(12) as inputs stm32_gpioPinConfig((struct stm32_gpio *)GPIOC_BASE, BV(6) | BV(7) | BV(8) | BV(9) | BV(12), GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); - // Initialize D2_5 PA(8) + // Initialize D2_5 PA(8) as input stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, BV(8), GPIO_MODE_IN_FLOATING, GPIO_SPEED_50MHZ); + + // Initialize SW1..4 PA(4..7) as outputs open drain + stm32_gpioPinConfig((struct stm32_gpio *)GPIOA_BASE, + BV(4) | BV(5) | BV(6) | BV(7), + GPIO_MODE_OUT_OD, + GPIO_SPEED_50MHZ); + + // Initialize BUZ PB(5) as output push-pull + stm32_gpioPinConfig((struct stm32_gpio *)GPIOB_BASE, + BV(5), + GPIO_MODE_OUT_PP, + GPIO_SPEED_50MHZ); + } #define BOOL(a) ((a)?1:0) -void get_switch_state(switch_status *pkt1, switch_status *pkt2) { +void get_switch_status(switch_status *pkt1, switch_status *pkt2) { pkt1->p.sw1 = BOOL(stm32_gpioPinRead((struct stm32_gpio *)GPIOC_BASE, BV(4))); pkt1->p.sw2 = BOOL(stm32_gpioPinRead((struct stm32_gpio *)GPIOC_BASE, BV(5))); @@ -71,3 +104,17 @@ void get_switch_state(switch_status *pkt1, switch_status *pkt2) { pkt2->p.sw6 = BOOL(stm32_gpioPinRead((struct stm32_gpio *)GPIOC_BASE, BV(12))); } + +void set_switch(switch_request *pkt) { + + struct stm32_gpio *base = NULL; + int32_t pin; + + if(pkt->p.num >= sizeof(pins)/sizeof(pins[0])) + return; + + base = (struct stm32_gpio *)pins[pkt->p.num][0]; + pin = BV(pins[pkt->p.num][1]); + + stm32_gpioPinWrite(base, pin, pkt->p.state); +} diff --git a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.h b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.h index b5cb78b..b4eae68 100644 --- a/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.h +++ b/elec/boards/Sensor_Actuator/Firmware/sensor_actuator/sensor_actuator/switch/switch.h @@ -36,6 +36,11 @@ void switch_init(void); /** * Get the switch CAN packet */ -void get_switch_state(switch_status *pkt1, switch_status *pkt2); +void get_switch_status(switch_status *pkt1, switch_status *pkt2); + +/** + * Set the switch state from the CAN packet + */ +void set_switch(switch_request *pkt); #endif diff --git a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.c b/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.c deleted file mode 100644 index 031ff29..0000000 --- a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.c +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Wrapper to use the ADS7828 analog to digital converter - * Olivier Bichler - */ - -#include "ads7828.h" - -unsigned int adc7828_measure(I2c* i2c, unsigned char addr, unsigned char ch) { - unsigned char value[2] = {0}; - unsigned char cmd = ADS7828_CMD_PD0 - | ADS7828_CMD_PD1 - | ((ch/2) << 4) - | ADS7828_CMD_SD; - - if (ch % 2) - cmd|= ADS7828_CMD_C2; - - i2c_start_w(i2c, addr, 1, I2C_STOP); - i2c_write(i2c, &cmd, 1); - i2c_start_r(i2c, addr, 2, I2C_STOP); - i2c_read(i2c, &value, 2); - - return ((value[0] << 8) | value[1]); -} - diff --git a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.h b/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.h deleted file mode 100644 index 5622fa8..0000000 --- a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/ads7828.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Wrapper to use the ADS7828 analog to digital converter - * Olivier Bichler - */ - -#ifndef HEADER__ADS7828 -#define HEADER__ADS7828 - -#define ADS7828_VREF 2.5 -#define ADS7828_RES 12 -#define ADS7828_LSB (ADS7828_VREF/((1 << ADS7828_RES) - 1.0)) - -#define ADS7828_ADDR_BASE 0x90 -#define ADS7828_ADDR_A0 0x02 -#define ADS7828_ADDR_A1 0x04 - -#define ADS7828_CMD_PD0 0x04 -#define ADS7828_CMD_PD1 0x08 -#define ADS7828_CMD_C0 0x10 -#define ADS7828_CMD_C1 0x20 -#define ADS7828_CMD_C2 0x40 -#define ADS7828_CMD_SD 0x80 - -#include <drv/i2c.h> - -unsigned int adc7828_measure(I2c* i2c, unsigned char addr, unsigned char ch); - -#endif - diff --git a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.c b/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.c deleted file mode 100644 index 31f5175..0000000 --- a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.c +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Battery Monitoring wrapper for the USB CAN Board - * - * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> - * Authors: Nicolas Dandrimont <ol...@cr...> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -#include "battery_monitoring.h" - -PROC_DEFINE_STACK(stack_battery_monitoring, KERN_MINSTACKSIZE * 16); - -static battery_monitoring_ctx _ctx; -battery_monitoring_ctx *ctx = &_ctx; - -static void NORETURN battery_monitoring_process(void) { - - can_tx_frame txf; - can_rx_frame rxf; - - battery_status b; - - uint8_t ch; - uint16_t value = 42; - - txf.ide = rxf.ide = 1; - txf.rtr = rxf.rtr = 0; - txf.dlc = rxf.dlc = sizeof(b.p); - - for (;;) { - // Battery 1 - for (ch = 0; ch < 4; ++ch) { - value = adc7828_measure(ctx->i2c, ADS7828_ADDR_BASE, ch); - b.p.elem[ch] = (uint16_t)(2.0*value*ADS7828_LSB * 10000.); - } - - txf.eid = rxf.eid = 401; - txf.data32[0] = rxf.data32[0] = b.d[0]; - txf.data32[1] = rxf.data32[1] = b.d[1]; - - usb_can_emit(ctx->usbcan, &rxf); - can_transmit(ctx->usbcan->can, &txf, ms_to_ticks(10)); - - // Battery 2 - for (ch = 0; ch < 4; ++ch) { - value = adc7828_measure(ctx->i2c, ADS7828_ADDR_BASE + 2, ch); - b.p.elem[ch] = (uint16_t)(2.0*value*ADS7828_LSB * 10000.); - } - - txf.eid = rxf.eid = 402; - txf.data32[0] = rxf.data32[0] = b.d[0]; - txf.data32[1] = rxf.data32[1] = b.d[1]; - - usb_can_emit(ctx->usbcan, &rxf); - can_transmit(ctx->usbcan->can, &txf, ms_to_ticks(10)); - - timer_delay(100); - } -} - -battery_monitoring_ctx *battery_monitoring_init(usb_can *usbcan, I2c *i2c) { - ctx->usbcan = usbcan; - ctx->i2c = i2c; - - proc_new(battery_monitoring_process, NULL, sizeof(stack_battery_monitoring), stack_battery_monitoring); - - - return ctx; -} diff --git a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.h b/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.h deleted file mode 100644 index 6075c1e..0000000 --- a/elec/boards/USB_CAN/Firmware/usb_can/battery_monitoring/battery_monitoring.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Battery Monitoring wrapper for the USB CAN Board - * - * Copyright © 2011 Nicolas Dandrimont <ol...@cr...> - * Authors: Nicolas Dandrimont <ol...@cr...> - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -#ifndef BATTERY_MONITORING_H -#define BATTERY_MONITORING_H - -#include <drv/can.h> -#include <drv/i2c.h> - -#include "../usb_can/usb_can.h" -#include "ads7828.h" - -typedef struct { - usb_can *usbcan; - I2c *i2c; -} battery_monitoring_ctx; - -struct battery_status_pkt { - uint16_t elem[4] __attribute__((packed)); // in 1/10000th volts [0; 6.5536[ -}; - -typedef union { - struct battery_status_pkt p; - uint32_t d[2]; -} battery_status; - -battery_monitoring_ctx *battery_monitoring_init(usb_can *usbcan, I2c *i2c); - -#endif diff --git a/elec/boards/USB_CAN/Firmware/usb_can/main.c b/elec/boards/USB_CAN/Firmware/usb_can/main.c index 4f984b8..a5a2aa7 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/main.c +++ b/elec/boards/USB_CAN/Firmware/usb_can/main.c @@ -32,7 +32,6 @@ #include <drv/gpio_stm32.h> #include <drv/can.h> -#include <drv/i2c.h> #include <drv/timer.h> #include <kern/monitor.h> @@ -47,8 +46,6 @@ PROC_DEFINE_STACK(stack_blinky, KERN_MINSTACKSIZE * 2); -static I2c i2c; - static void init(void) { can_config cfg; @@ -88,9 +85,6 @@ static void init(void) /* Initialize Serial driver */ serial_init(SERIAL_BAUDRATE); - /* Initialize I2c interface */ - i2c_init(&i2c, 0, CONFIG_I2C_FREQ); - /* * Kernel initialization: processes (allow to create and dispatch * processes using proc_new()). @@ -99,9 +93,6 @@ static void init(void) /* Initialize USB-CAN logic */ usbcan = usb_can_init(CAND1); - - /* Initialize battery monitoring */ - battery_monitoring_init(usbcan, &i2c); } static void NORETURN blinky_process(void) { diff --git a/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk b/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk index d6bff03..6831017 100644 --- a/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk +++ b/elec/boards/USB_CAN/Firmware/usb_can/usb_can_user.mk @@ -9,8 +9,6 @@ usb_can_PROGRAMMER_PORT = none # Files included by the user. usb_can_USER_CSRC = \ - $(usb_can_SRC_PATH)/battery_monitoring/ads7828.c \ - $(usb_can_SRC_PATH)/battery_monitoring/battery_monitoring.c \ $(usb_can_SRC_PATH)/usb_can/serial.c \ $(usb_can_SRC_PATH)/usb_can/usb_can.c \ $(usb_can_SRC_PATH)/main.c \ hooks/post-receive -- krobot |