Skip to content

Commit

Permalink
CurrSour: cli for current source
Browse files Browse the repository at this point in the history
  • Loading branch information
epccs committed Apr 30, 2018
1 parent 4958674 commit 1a357a4
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 17,7 @@ env:
- APP_PATH="Digital"
- APP_PATH="Pwm"
- APP_PATH="i2c-debug"
- APP_PATH="CurrSour"
- APP_PATH="AmpHr"

before_install:
Expand Down
2 changes: 1 addition & 1 deletion Adc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 33,7 @@ With a serial port connection (set the BOOT_PORT in Makefile) and optiboot insta

```
git clone https://github.com/epccs/RPUlux/
cd /RPUux/Adc
cd /RPUlux/Adc
make bootload
...
avrdude done. Thank you.
Expand Down
2 changes: 1 addition & 1 deletion BlinkLED/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 14,7 @@ With a serial port connection (set the BOOT_PORT in Makefile) and optiboot insta

```
git clone https://github.com/epccs/RPUlux/
cd /RPUux/BlinkLED
cd /RPUlux/BlinkLED
make bootload
...
avrdude done. Thank you.
Expand Down
71 changes: 71 additions & 0 deletions CurrSour/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 1,71 @@
# Makefile
TARGET = CurrSour
LIBDIR = ../lib
OBJECTS = main.o \
cs.o \
status.o \
../Uart/id.o \
$(LIBDIR)/timers.o \
$(LIBDIR)/uart.o \
$(LIBDIR)/twi.o \
$(LIBDIR)/rpu_mgr.o \
$(LIBDIR)/parse.o

## Chip and project-specific global definitions
MCU = atmega328p
F_CPU = 16000000UL
BAUD = 38400UL
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I.

## Cross-compilation
CC = avr-gcc
OBJCOPY = avr-objcopy
SIZE = avr-size

# FTDI's USB to serial bridge shows as /dev/ttyUSB0,
# Uno's serial bridge (an ATmega16U2) shows as /dev/ttyACM0 (a modem,?)
# Pi Zero on chip hardware serial shows as /dev/ttyAMA0 (hardware UART on a Linux system)
detect_PORT := $(shell sh -c 'ls /dev/ttyAMA0 2>/dev/null || echo not')
ifeq ($(detect_PORT),/dev/ttyAMA0)
BOOTLOAD_PORT = /dev/ttyAMA0
endif
detect_PORT := $(shell sh -c 'ls /dev/ttyUSB0 2>/dev/null || echo not')
ifeq ($(detect_PORT),/dev/ttyUSB0)
BOOTLOAD_PORT = /dev/ttyUSB0
endif

## Compiler/linker options
CFLAGS = -Os -g -std=gnu99 -Wall
# CFLAGS = -funsigned-char -funsigned-bitfields
# CFLAGS = -fpack-struct -fshort-enums
CFLAGS = -ffunction-sections -fdata-sections

TARGET_ARCH = -mmcu=$(MCU)

LDFLAGS = -Wl,-Map,$(TARGET).map
LDFLAGS = -Wl,--gc-sections

## printf() is one of the vfprintf() family of functions which does not implement floating point conversion by default
## warning this takes about 2k of program space
LDFLAGS = -Wl,-u,vfprintf -lprintf_flt -lm

all: $(TARGET).hex

$(TARGET): $(TARGET).hex

$(TARGET).hex: $(TARGET).elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
rm -f $(TARGET).elf

## works with optiboot which erases flash without being told (e.g. -e )
bootload: $(TARGET).hex
avrdude -v -p $(MCU) -c arduino -P $(BOOTLOAD_PORT) -b 115200 -U flash:w:$(TARGET).hex

$(TARGET).elf: $(OBJECTS)
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ -o $@
$(SIZE) -C --mcu=$(MCU) $@
rm -f $(TARGET).o $(OBJECTS)

clean:
rm -f $(TARGET).hex $(TARGET).map

71 changes: 71 additions & 0 deletions CurrSour/README.md
Original file line number Diff line number Diff line change
@@ -0,0 1,71 @@
# Current Source

## Overview

There are two 22mA current sources CS0..CS1 controlled with ATmega328p pins PB4, PB5.


# Firmware Upload

With a serial port connection (set the BOOT_PORT in Makefile) and optiboot installed on the RPUno run 'make bootload' and it should compile and then flash the MCU.

```
git clone https://github.com/epccs/RPUlux/
cd /RPUlux/CurrSour
make bootload
...
avrdude done. Thank you.
```

Now connect with picocom (or ilk).


```
#exit is C-a, C-x
picocom -b 38400 /dev/ttyUSB0
```


# Commands

Commands are interactive over the serial interface at 38400 baud rate. The echo will start after the second character of a new line.


## /\[rpu_address\]/\[command \[arg\]\]

rpu_address is taken from the I2C address 0x29 (e.g. get_Rpu_address() which is included form ../lib/rpu_mgr.h). The value of rpu_address is used as a character in a string, which means don't use a null value (C strings are null terminated) as an adddress. The ASCII value for '1' (0x31) is easy and looks nice, though I fear it will cause some confusion when it is discovered that the actual address value is 49.

Commands and their arguments follow.


## /0/id? \[name|desc|avr-gcc\]

identify

```
/1/id?
{"id":{"name":"CurrSour","desc":"RPUlux (17323^2) Board /w atmega328p","avr-gcc":"5.4.0"}}
```

## /0/cs 0..1,ON|OFF

Turn on (or off) 22mA current sources CS0..CS3

```
/1/cs 1,ON
{"PB5":"ON"}
/1/cs 1,OFF
{"PB5":"OFF"}
```


## /0/showstat ON|OFF

A current source can be used to show status, but it is not ON by default.

```
/1/showstat ON
{"SHOW":"ON"}
/1/showstat OFF
{"SHOW":"OFF"}
```
112 changes: 112 additions & 0 deletions CurrSour/cs.c
Original file line number Diff line number Diff line change
@@ -0,0 1,112 @@
/*
cs is part of CurrSour, a serial command interface to control current sources.
Copyright (C) 2018 Ronald Sutherland
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 2
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.
For a copy of the GNU General Public License use
http://www.gnu.org/licenses/gpl-2.0.html
*/
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include "../lib/parse.h"
#include "../lib/timers.h"
#include "../lib/pin_num.h"
#include "../lib/pins_board.h"
#include "cs.h"

#define SERIAL_PRINT_DELAY_MILSEC 10000
static unsigned long serial_print_started_at;

// show the current source pin number used
void echo_cs_pin_in_json_rply(uint8_t cs)
{
switch(cs)
{
case 0 :
printf_P(PSTR("PD5")); // CS0
break;
case 1 :
printf_P(PSTR("PD6")); // CS1
break;
case 2 :
printf_P(PSTR("PD3")); // CS2
break;
case 3 :
printf_P(PSTR("PD4")); // CS3
break;
}
}

// pinMode( arg[0], arg[1] )
void CurrSour(void)
{
if ( (command_done == 10) )
{
// check that arg[0] is a digit
if ( ( !( isdigit(arg[0][0]) ) ) )
{
printf_P(PSTR("{\"err\":\"CSarg0NaN\"}\r\n"));
initCommandBuffer();
return;
}
// and arg[0] value is 0..3
uint8_t cs = atoi(arg[0]);
if ( ( cs < 0) || (cs > 3) )
{
printf_P(PSTR("{\"err\":\"CSarg0OutOfRng\"}\r\n"));
initCommandBuffer();
return;
}
// also arg[1] is not ('INPUT' or 'OUTPUT')
if ( !( (strcmp_P( arg[1], PSTR("ON")) == 0) || (strcmp_P( arg[1], PSTR("OFF")) == 0) ) )
{
printf_P(PSTR("{\"err\":\"CSarg1NaMode\"}\r\n"));
initCommandBuffer();
return;
}
serial_print_started_at = millis();
printf_P(PSTR("{\""));
if (strcmp_P( arg[1], PSTR("ON")) == 0 )
{
pinMode(cs_pin_map[cs].pin, OUTPUT);
digitalWrite(cs_pin_map[cs].pin, HIGH);
}
else
{
pinMode(cs_pin_map[cs].pin, OUTPUT);
digitalWrite(cs_pin_map[cs].pin, LOW);
}
command_done = 11;
}
else if ( (command_done == 11) )
{
uint8_t cs = atoi(arg[0]);
echo_cs_pin_in_json_rply( cs );
printf_P(PSTR("\":\""));
command_done = 12;
}
else if ( (command_done == 12) )
{
printf( arg[1] );
printf_P(PSTR("\"}\r\n"));
initCommandBuffer();
}
else
{
printf_P(PSTR("{\"err\":\"CSCmdDnWTF\"}\r\n"));
initCommandBuffer();
}
}
17 changes: 17 additions & 0 deletions CurrSour/cs.h
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
#ifndef CS_H
#define CS_H

#define NUM_CS_PINS 2

typedef struct {
uint8_t pin;
} CS_Map;

static const CS_Map cs_pin_map[NUM_CS_PINS] = {
[0] = { .pin=12 }, // number is defined in ../lib/pin_num.h
[1] = { .pin=13 }
};

extern void CurrSour(void);

#endif // CS_H
Loading

0 comments on commit 1a357a4

Please sign in to comment.