From 7f193c251e48a0ab8b8dcdc3872c9d5045268dcd Mon Sep 17 00:00:00 2001 From: jaseg Date: Sun, 7 Oct 2012 20:37:45 +0200 Subject: [PATCH] Back-ported the working version from avr to msp. This is NOT YET TESTED. --- avr/comm.c | 9 ++++-- avr/generate.py | 2 +- {avr => msp}/comm.c | 17 +++++++---- msp/comm.h | 10 +++---- msp/config.h | 2 +- msp/generate.py | 81 ++++++++++++++++++++++++++++++-------------------- msp/timer-pwm-rgb.c.tp | 6 ++-- 7 files changed, 75 insertions(+), 52 deletions(-) copy {avr => msp}/comm.c (83%) diff --git a/avr/comm.c b/avr/comm.c index c07059d..0a7abfc 100644 --- a/avr/comm.c +++ b/avr/comm.c @@ -1,3 +1,10 @@ +/* + Copyright (C) 2012 jaseg + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 3 as published by the Free Software Foundation. + */ #include #include @@ -74,8 +81,6 @@ void comm_loop(){ //FIXME add crc checking } if(funcid < num_callbacks){ - //HACK: The argument buffer is currently passed as the response buffer for ram efficiency. - //Only write to the response buffer *after* you are done reading from it. comm_callbacks[funcid](arglen, argbuf); }else{ //FIXME error handling diff --git a/avr/generate.py b/avr/generate.py index cf397a8..9d4c707 100644 --- a/avr/generate.py +++ b/avr/generate.py @@ -121,7 +121,7 @@ def generate(dev, devicename, builddate): register_callback("callback_get_descriptor_auto") - def generate_accessors(name, ctype): + def generate_accessors(name, ctype, aval=1): return Template(accessor_callbacks).render_unicode(name=name, bsize="({}*sizeof({}))".format(aval, ctype)); for mname, member in members.items(): diff --git a/avr/comm.c b/msp/comm.c similarity index 83% copy from avr/comm.c copy to msp/comm.c index c07059d..173cdd5 100644 --- a/avr/comm.c +++ b/msp/comm.c @@ -1,9 +1,16 @@ +/* + Copyright (C) 2012 jaseg + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + version 3 as published by the Free Software Foundation. + */ -#include #include #include +#include #include "comm.h" -#include "uart.h" +#include "uart_io.h" void comm_loop(){ static uint8_t escape_state = 0; @@ -14,12 +21,12 @@ void comm_loop(){ static uint8_t argbuf[ARGBUF_SIZE]; static uint16_t arglen = 0; - uint16_t v = uart_getc(); + uint16_t v = uart_getc_nonblocking(); uint8_t c = v&0xff; v &= 0xff00; if(!v){ //a character was successfully received if(escape_state){ - if(c == '#'){ + if(c == '#'){ //FIXME pos = 0; state = 0; return; @@ -74,8 +81,6 @@ void comm_loop(){ //FIXME add crc checking } if(funcid < num_callbacks){ - //HACK: The argument buffer is currently passed as the response buffer for ram efficiency. - //Only write to the response buffer *after* you are done reading from it. comm_callbacks[funcid](arglen, argbuf); }else{ //FIXME error handling diff --git a/msp/comm.h b/msp/comm.h index a4113ac..925b638 100644 --- a/msp/comm.h +++ b/msp/comm.h @@ -1,4 +1,3 @@ - #ifndef __COMM_H__ #define __COMM_H__ @@ -11,18 +10,17 @@ void putc_escaped(char c); *uint8_t callback(uint8_t arglen, uint8_t* argbuf); * arglen: length of the argument string passed to the function * argbuf: buffer containing the argument string + * respbuflen: length of the response buffer (maximum response length) + * respbuf: response buffer (write your return value *here*) * * return value: amount of data written to respbuf (in bytes) */ typedef void (*comm_callback)(uint16_t, uint8_t*); extern comm_callback comm_callbacks[]; - -#ifndef NUM_CALLBACKS -#define NUM_CALLBACKS 0 -#endif//NUM_CALLBACKS +extern const uint16_t num_callbacks; #ifndef ARGBUF_SIZE -#define ARGBUF_SIZE 32 +#define ARGBUF_SIZE 12 #endif//ARGBUF_SIZE #endif//__COMM_H__ diff --git a/msp/config.h b/msp/config.h index b0f8000..721595d 100644 --- a/msp/config.h +++ b/msp/config.h @@ -2,4 +2,4 @@ */ extern unsigned int auto_config_descriptor_length; -extern char auto_config_descriptor[]; +extern const char auto_config_descriptor[]; diff --git a/msp/generate.py b/msp/generate.py index 186ec7d..91f993b 100644 --- a/msp/generate.py +++ b/msp/generate.py @@ -8,7 +8,6 @@ import subprocess import os.path import struct from mako.template import Template -from datetime import datetime import binascii import json import pylzma @@ -27,6 +26,7 @@ autocode_stub = """\ #include #include "autocode.h" #include "comm.h" +#include "uart_io.h" """ autoglue = """ @@ -38,6 +38,8 @@ comm_callback comm_callbacks[] = { % endfor }; +const uint16_t num_callbacks = ${len(callbacks)}; + void init_auto(){ % for initfunc in init_functions: ${initfunc}(); @@ -52,25 +54,25 @@ void loop_auto(){ void callback_get_descriptor_auto(uint16_t alen, uint8_t* argbuf){ //FIXME - putc_escaped(auto_config_descriptor_length >> 8); - putc_escaped(auto_config_descriptor_length & 0xFF); - for(char* i=auto_config_descriptor; i < auto_config_descriptor+auto_config_descriptor_length; i++){ - putc_escaped(*i); + uart_putc(auto_config_descriptor_length >> 8); + uart_putc(auto_config_descriptor_length & 0xFF); + for(const char* i=auto_config_descriptor; i < auto_config_descriptor+auto_config_descriptor_length; i++){ + uart_putc(*i); } //FIXME add crc generation - putc_escaped(0x00); - putc_escaped(0x00); + uart_putc(0x00); + uart_putc(0x00); } """ accessor_callbacks = """ void callback_set_${name}(uint16_t alen, uint8_t* argbuf){ - if(! sizeof(${ctype}) == alen){ + if(! ${bsize} == alen){ //FIXME error handling return; } - memcpy(&${name}, argbuf, sizeof(${ctype})); + memcpy(&${name}, argbuf, ${bsize}); } void callback_get_${name}(uint16_t alen, uint8_t* argbuf){ @@ -78,14 +80,14 @@ void callback_get_${name}(uint16_t alen, uint8_t* argbuf){ //FIXME error handling return; } - putc_escaped(sizeof(${ctype})>>8); - putc_escaped(sizeof(${ctype})&0xFF); - for(char* i=((char*)&${name}); i<((char*)&${name})+sizeof(${ctype}); i++){ - putc_escaped(*i); + uart_putc(${bsize}>>8); + uart_putc(${bsize}&0xFF); + for(char* i=((char*)&${name}); i<((char*)&${name})+${bsize}; i++){ + uart_putc(*i); } //FIXME add crc generation - putc_escaped(0x00); - putc_escaped(0x00); + uart_putc(0x00); + uart_putc(0x00); } """ @@ -97,14 +99,13 @@ config_c_template = """\ */ #include "config.h" unsigned int auto_config_descriptor_length = ${desc_len}; -char auto_config_descriptor[] = {${desc}}; +const char auto_config_descriptor[] = {${desc}}; """ -def generate(dev, devicename): +def generate(dev, devicename, builddate): members = dev["members"] seqnum = 23 current_id = 0 - builddate = datetime.now() dev["builddate"] = str(builddate) autocode = Template(autocode_stub).render_unicode(devname=devicename, version=dev["version"], builddate=builddate) init_functions = [] @@ -114,12 +115,14 @@ def generate(dev, devicename): def register_callback(name): nonlocal current_id callbacks.append((name, current_id)) + old_id = current_id current_id += 1 + return old_id register_callback("callback_get_descriptor_auto") - def generate_accessors(name, ctype): - return Template(accessor_callbacks).render_unicode(name=name, ctype=ctype); + def generate_accessors(name, ctype, aval=1): + return Template(accessor_callbacks).render_unicode(name=name, bsize="({}*sizeof({}))".format(aval, ctype)); for mname, member in members.items(): mfile = member["type"] @@ -129,36 +132,48 @@ def generate(dev, devicename): # print("Cannot find a handler for member type " + dev["type"]) # exit(1) - init_function = "init_{}_{}".format(mtype, seqnum) - init_functions.append(init_function) - loop_function = "loop_{}_{}".format(mtype, seqnum) - loop_functions.append(loop_function) + def init_function(): + fun = "init_{}_{}".format(mtype, seqnum) + init_functions.append(fun) + return fun + def loop_function(): + fun = "loop_{}_{}".format(mtype, seqnum) + loop_functions.append(fun) + return fun member["properties"] = {} member["functions"] = {} accessors = "" - def modulevar(name, ctype=None, fmt=None): + def modulevar(name, ctype=None, fmt=None, array=False): varname = "modvar_{}_{}_{}".format(mtype, seqnum, name) if fmt is not None and ctype is not None: nonlocal accessors - accessors += generate_accessors(varname, ctype) + aval = 1 + if array != False: + aval = array + accessors += generate_accessors(varname, ctype, aval) - register_callback("callback_get_" + varname) + accid = register_callback("callback_get_" + varname) + register_callback("callback_set_" + varname) member["properties"][name] = { "size": struct.calcsize(fmt), - "id": current_id, + "id": accid, "format": fmt} - register_callback("callback_set_" + varname) - return "{} {}".format(ctype, varname) + array_component = "" + if array == True: + array_component = "[]" + elif array: + array_component = "[{}]".format(array) + return "{} {}{}".format(ctype, varname, array_component) return varname def module_callback(name, argformat="", retformat=""): cbname = "callback_{}_{}_{}".format(mtype, seqnum, name) - register_callback(cbname) + cbid = register_callback(cbname) member["functions"][name] = { - "id": current_id, + "id": cbid, "args": argformat, "returns": retformat} return cbname @@ -170,7 +185,7 @@ def generate(dev, devicename): loop_function=loop_function, modulevar=modulevar, module_callback=module_callback, - dev=dev) + member=member) autocode += accessors autocode += Template(autoglue).render_unicode(init_functions=init_functions, loop_functions=loop_functions, callbacks=callbacks) diff --git a/msp/timer-pwm-rgb.c.tp b/msp/timer-pwm-rgb.c.tp index 9233a37..2b792d3 100644 --- a/msp/timer-pwm-rgb.c.tp +++ b/msp/timer-pwm-rgb.c.tp @@ -69,11 +69,11 @@ rgbval hsv2rgb(hsvval in){ #endif//_TIMER_PWM_RGB_STATIC_ -void ${init_function} (){ +void ${init_function()} (){ //PWM Timer setup (uses both TIMER_As) TA0CTL = MC_2 | TASSEL_2; //Continous with SMCLK TA1CTL = MC_2 | TASSEL_2; //Continous with SMCLK -% if "pwm_inverted" in dev and dev["pwm_inverted"]: +% if "pwm_inverted" in member and member["pwm_inverted"]: TA0CCTL1 = OUTMOD_7; TA1CCTL1 = OUTMOD_7; TA1CCTL2 = OUTMOD_7; @@ -91,7 +91,7 @@ void ${init_function} (){ ${modulevar("col", "hsvval", "fff")} = {0.0, 1.0, 1.0}; -void ${loop_function} (){ +void ${loop_function()} (){ rgbval rgb = hsv2rgb( ${modulevar("col")} ); TA1CCR2 = gammatable[rgb.r]; // red TA0CCR1 = gammatable[rgb.g]; // green -- 2.11.4.GIT