From ca4c90b1a897f7e421aa691131da65e594ef3b73 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Thu, 23 Jul 2015 00:52:52 -0400 Subject: [PATCH] unify the "systems" functionality. Also, make the embeddedlinux tool more versatile. --- embeddedcommon/embeddedout.h | 5 +- embeddedlinux/Makefile | 4 +- embeddedlinux/embeddedcc.c | 20 +++-- embeddedstm32f303/lib/systems.c | 38 +++++++++- embeddedstm32f303/lib/systems.h | 32 ++++++-- embeddedstm32f407/lib/systems.c | 126 +++++++++++++++++++++++++------- embeddedstm32f407/lib/systems.h | 74 +++++++++++-------- 7 files changed, 224 insertions(+), 75 deletions(-) diff --git a/embeddedcommon/embeddedout.h b/embeddedcommon/embeddedout.h index 4bf9246..7bc6b8c 100644 --- a/embeddedcommon/embeddedout.h +++ b/embeddedcommon/embeddedout.h @@ -14,7 +14,10 @@ #define NUM_LIN_LEDS 296 #endif -#define LIN_WRAPAROUND 0 //Whether the output lights wrap around. +#ifndef LIN_WRAPAROUND +#define LIN_WRAPAROUND 0 //Whether the output lights wrap around. (Can't easily run on embedded systems) +#endif + #define SORT_NOTES 0 //Whether the notes will be sorted. extern uint8_t ledArray[]; diff --git a/embeddedlinux/Makefile b/embeddedlinux/Makefile index 28b9233..25e9ce8 100644 --- a/embeddedlinux/Makefile +++ b/embeddedlinux/Makefile @@ -1,13 +1,13 @@ all : embeddedcc -CFLAGS:=-Ofast -DCCEMBEDDED -I.. -flto -m32 -DDFREQ=11025 -I../embeddedcommon +CFLAGS:=-Ofast -DCCEMBEDDED -I.. -flto -m32 -DDFREQ=11025 -I../embeddedcommon -DNUM_LIN_LEDS=20 LDFLAGS:=-ffunction-sections -Wl,--gc-sections -fno-asynchronous-unwind-tables -Wl,--strip-all embeddedcc : ../embeddedcommon/embeddednf.c ../embeddedcommon/DFT32.c embeddedcc.c ../embeddedcommon/embeddedout.c gcc -o $@ $^ $(CFLAGS) $(LDFLAGS) runembedded : embeddedcc - parec --format=u8 --rate=11025 --channels=1 --device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor --latency=128 | ./embeddedcc + parec --format=u8 --rate=11025 --channels=1 --device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor --latency=128 | ./embeddedcc 192.168.4.1 0 clean : rm -rf embeddedcc *~ diff --git a/embeddedlinux/embeddedcc.c b/embeddedlinux/embeddedcc.c index 2b25c1e..a84f4eb 100644 --- a/embeddedlinux/embeddedcc.c +++ b/embeddedlinux/embeddedcc.c @@ -14,7 +14,9 @@ struct sockaddr_in servaddr; int sock; -#define expected_lights 296 +#define expected_lights NUM_LIN_LEDS + +int toskip = 1; void NewFrame() { @@ -30,24 +32,32 @@ void NewFrame() for( i = 0; i < expected_lights * 3; i++ ) { - buffer[i+3] = ledOut[i]; + buffer[i+toskip*3] = ledOut[i]; } int r = send(sock,buffer,expected_lights*3+3,0); } -int main() +int main( int argc, char ** argv ) { int wf = 0; int ci; + if( argc < 2 ) + { + fprintf( stderr, "Error: usage: [tool] [ip address] [num to skip, default 0]\n" ); + return -1; + } + + printf( "%d\n", argc ); + toskip = (argc > 2)?atoi(argv[2]):0; + sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); - printf( "%d\n", sock ); memset(&servaddr,0,sizeof(servaddr)); servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = inet_addr("192.168.0.245"); + servaddr.sin_addr.s_addr = inet_addr(argv[1]); servaddr.sin_port=htons(7777); connect( sock, (struct sockaddr *)&servaddr, sizeof(servaddr) ); diff --git a/embeddedstm32f303/lib/systems.c b/embeddedstm32f303/lib/systems.c index ce7481a..dc3a83a 100644 --- a/embeddedstm32f303/lib/systems.c +++ b/embeddedstm32f303/lib/systems.c @@ -1,9 +1,15 @@ #include #include "systems.h" #include +#ifdef STM32F30X #include #include #include +#elif defined( STM32F40_41xxx ) +#include +#include +#include +#endif #include #include @@ -72,7 +78,7 @@ void _delay_us(uint32_t us) { void ConfigureLED() { - ConfigureGPIO( GetGPIOFromString( "PB8" ), INOUT_OUT ); + ConfigureGPIO( LEDPIN, INOUT_OUT ); } uint8_t GetGPIOFromString( const char * str ) @@ -135,6 +141,8 @@ void ConfigureGPIO( uint8_t gpio, int parameters ) { GPIO_InitTypeDef GPIO_InitStructure; +#ifdef STM32F30X + /* Enable the GPIO_LED Clock */ RCC_AHBPeriphClockCmd( 1<<(17+(gpio>>4)), ENABLE); @@ -153,6 +161,32 @@ void ConfigureGPIO( uint8_t gpio, int parameters ) GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = (parameters&PUPD_FLAG)?( (parameters&PUPD_UP)?GPIO_PuPd_UP:GPIO_PuPd_DOWN ):GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; - GPIO_Init(GPIOB, &GPIO_InitStructure); + GPIO_Init(GPIOOf(gpio), &GPIO_InitStructure); + +#elif defined( STM32F40_41xxx ) + + + /* Enable the GPIO_LED Clock */ + RCC_AHB1PeriphClockCmd( 1<<((gpio>>4)), ENABLE); + + if( parameters & DEFAULT_VALUE_FLAG ) + { + GPIOOn( gpio ); + } + else + { + GPIOOff( gpio ); + } + + /* Configure the GPIO_LED pin */ + GPIO_InitStructure.GPIO_Pin = 1<<(gpio&0xf); + GPIO_InitStructure.GPIO_Mode = (parameters&INOUT_FLAG)?GPIO_Mode_OUT:GPIO_Mode_IN; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = (parameters&PUPD_FLAG)?( (parameters&PUPD_UP)?GPIO_PuPd_UP:GPIO_PuPd_DOWN ):GPIO_PuPd_NOPULL; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOOf(gpio), &GPIO_InitStructure); + +#endif + } diff --git a/embeddedstm32f303/lib/systems.h b/embeddedstm32f303/lib/systems.h index 574ccca..b193564 100644 --- a/embeddedstm32f303/lib/systems.h +++ b/embeddedstm32f303/lib/systems.h @@ -26,18 +26,38 @@ gpio GetGPIOFromString( const char * str ); void ConfigureGPIO( gpio gpio, int parameters ); + + +#ifdef STM32F30X #define GPIOOf(x) ((GPIO_TypeDef *) ((((x)>>4)<=6)?(AHB2PERIPH_BASE+0x400*((x)>>4)):0x60000000) ) +#elif defined( STM32F40_41xxx ) +#define GPIOOf(x) ((GPIO_TypeDef *) ((((x)>>4)<=6)?(AHB1PERIPH_BASE+0x400*((x)>>4)):0x60000000) ) +#endif + #define GPIOPin(x) ((1<<((x)&0x0f))) #define GPIOLatch(x) GPIOOf(x)->ODR -#define GPIOOff(x) GPIOOf(x)->BRR = (1<<((x)&0x0f)); -#define GPIOOn(x) GPIOOf(x)->BSRR = (1<<((x)&0x0f)); +#ifdef STM32F30X +#define GPIOOn(x) GPIOOf(x)->BSRR = (1<<((x)&0x0f)); +#define GPIOOff(x) GPIOOf(x)->BRR = (1<<((x)&0x0f)); +#elif defined( STM32F40_41xxx ) +#define GPIOOn(x) GPIOOf(x)->BSRRH = (1<<((x)&0x0f)); +#define GPIOOff(x) GPIOOf(x)->BSRRL = (1<<((x)&0x0f)); +#endif + + + +#ifdef STM32F30X +#define LEDPIN 0x18 +#elif defined( STM32F40_41xxx ) +#define LEDPIN 0x3f +#endif void ConfigureLED(); -#define LED_TOGGLE {GPIOB->ODR ^= GPIO_Pin_8;} -#define LED_ON {GPIOB->BSRR ^= GPIO_Pin_8;} -#define LED_OFF {GPIOB->BRR ^= GPIO_Pin_8;} -//General notes: +#define LED_TOGGLE {GPIOOf(LEDPIN)->ODR^=(1<<((LEDPIN)&0x0f));} +#define LED_ON GPIOOn(LEDPIN) +#define LED_OFF GPIOOff(LEDPIN) + #endif diff --git a/embeddedstm32f407/lib/systems.c b/embeddedstm32f407/lib/systems.c index 49291fb..b2c5fd7 100644 --- a/embeddedstm32f407/lib/systems.c +++ b/embeddedstm32f407/lib/systems.c @@ -1,8 +1,15 @@ #include #include "systems.h" #include - - +#ifdef STM32F30X +#include +#include +#include +#elif defined( STM32F40_41xxx ) +#include +#include +#include +#endif #include #include @@ -32,13 +39,13 @@ void send_text( const char * text ) send_openocd_command(0x05, m); } -int _write (int fd, const void *buf, size_t count) +int __attribute__((used)) _write (int fd, const void *buf, size_t count) { uint32_t m[] = { 2, (uint32_t)buf, count }; send_openocd_command(0x05, m); } -void * _sbrk(int incr) { +void __attribute__((used)) * _sbrk(int incr) { extern char _ebss; // Defined by the linker static char *heap_end; char *prev_heap_end; @@ -71,32 +78,97 @@ void _delay_us(uint32_t us) { void ConfigureLED() { - GPIO_InitTypeDef GPIO_InitStructure; - /* Enable the GPIO_LED Clock */ + ConfigureGPIO( LEDPIN, INOUT_OUT ); +} -#ifdef STM32F30X - RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); +uint8_t GetGPIOFromString( const char * str ) +{ + int mode = 0; + int port = -1; + int pin = -1; + const char * st = str; + for( ; *st; st++ ) + { + char c = *st; + if( mode == 0 ) + { + if( c >= 'A' && c <= 'F' ) + { + port = c - 'A'; + mode = 2; + } + else if( c >= 'a' && c <= 'f' ) + { + port = c - 'a'; + mode = 2; + } + } + else if( mode == 2 ) + { + if( c >= '0' && c <= '9' ) + { + pin = 0; + mode = 3; + } + } - /* Configure the GPIO_LED pin */ - GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //| GPIO_Pin_15 (15 = CTS) - GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; - GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; - GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; - GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; - GPIO_Init(GPIOB, &GPIO_InitStructure); -#elif defined( STM32F40_41xxx ) - - RCC_AHB1PeriphClockCmd( LED_AHB_PORT, ENABLE); - - /* Configure the GPIO_LED pin */ - GPIO_InitStructure.GPIO_Pin = (1<= '0' && c <= '9' ) + { + pin = pin * 10; + pin+= c - '0'; + } + else + { + break; + } + } + } + if( port > 0 && pin > 0 && port <= 6 && pin <= 15) + { + return (port<<4)|pin; + } + else + { + return 0xff; + } } +void ConfigureGPIO( uint8_t gpio, int parameters ) +{ + GPIO_InitTypeDef GPIO_InitStructure; + + /* Enable the GPIO_LED Clock */ +#ifdef STM32F30X + RCC_AHBPeriphClockCmd( 1<<(17+(gpio>>4)), ENABLE); +#elif defined( STM32F40_41xxx ) + RCC_AHB1PeriphClockCmd( 1<<((gpio>>4)), ENABLE); +#endif + + if( parameters & DEFAULT_VALUE_FLAG ) + { + GPIOOn( gpio ); + } + else + { + GPIOOff( gpio ); + } + + /* Configure the GPIO_LED pin */ + GPIO_InitStructure.GPIO_Pin = 1<<(gpio&0xf); + GPIO_InitStructure.GPIO_Mode = (parameters&INOUT_FLAG)?GPIO_Mode_OUT:GPIO_Mode_IN; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_PuPd = (parameters&PUPD_FLAG)?( (parameters&PUPD_UP)?GPIO_PuPd_UP:GPIO_PuPd_DOWN ):GPIO_PuPd_NOPULL; + +#ifdef STM32F30X + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; +#elif defined( STM32F40_41xxx ) + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; +#endif + + GPIO_Init(GPIOOf(gpio), &GPIO_InitStructure); +} + diff --git a/embeddedstm32f407/lib/systems.h b/embeddedstm32f407/lib/systems.h index 8c0850a..b193564 100644 --- a/embeddedstm32f407/lib/systems.h +++ b/embeddedstm32f407/lib/systems.h @@ -1,53 +1,63 @@ #ifndef _SYSTEMS_H #define _SYSTEMS_H - -#ifdef STM32F30X -#include -#include -#include - -#define LEDPORT GPIOB -#define LEDPIN 8 - -#elif defined( STM32F40_41xxx ) - -#include -#include -#include - -#define LED_AHB_PORT RCC_AHB1Periph_GPIOD -#define LEDPORT GPIOD -#define LEDPIN 15 -#else - -#error Unsupported device. - -#endif - void send_openocd_command(int command, void *message); void send_text( const char * text ); void _delay_us(uint32_t us); -void ConfigureLED(); + +typedef uint8_t gpio; + +gpio GetGPIOFromString( const char * str ); -#define LED_TOGGLE {LEDPORT->ODR ^= (1<BSRR = (1<BRR = (1<>4)<=6)?(AHB2PERIPH_BASE+0x400*((x)>>4)):0x60000000) ) #elif defined( STM32F40_41xxx ) +#define GPIOOf(x) ((GPIO_TypeDef *) ((((x)>>4)<=6)?(AHB1PERIPH_BASE+0x400*((x)>>4)):0x60000000) ) +#endif -#define LED_ON {LEDPORT->BSRRL = (1<BSRRH = (1<ODR +#ifdef STM32F30X +#define GPIOOn(x) GPIOOf(x)->BSRR = (1<<((x)&0x0f)); +#define GPIOOff(x) GPIOOf(x)->BRR = (1<<((x)&0x0f)); +#elif defined( STM32F40_41xxx ) +#define GPIOOn(x) GPIOOf(x)->BSRRH = (1<<((x)&0x0f)); +#define GPIOOff(x) GPIOOf(x)->BSRRL = (1<<((x)&0x0f)); #endif -//General notes: + +#ifdef STM32F30X +#define LEDPIN 0x18 +#elif defined( STM32F40_41xxx ) +#define LEDPIN 0x3f +#endif + +void ConfigureLED(); +#define LED_TOGGLE {GPIOOf(LEDPIN)->ODR^=(1<<((LEDPIN)&0x0f));} +#define LED_ON GPIOOn(LEDPIN) +#define LED_OFF GPIOOff(LEDPIN) + #endif