From 3d94c5f72ea39a9fd6593fac402b75ec572cc611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sun, 14 Jan 2018 03:30:07 +0100 Subject: [PATCH] working on stm32 implementation --- eigener_ctrl/stm32f407-olimex/Makefile | 3 +- .../os/various/lwip_bindings/lwipthread.h | 4 +- eigener_ctrl/stm32f407-olimex/dfi.c | 177 ++++++++++++++ eigener_ctrl/stm32f407-olimex/dfi.h | 27 +++ eigener_ctrl/stm32f407-olimex/main.c | 221 +---------------- eigener_ctrl/stm32f407-olimex/ozone.jdebug | 226 ++++++++++++++++++ .../stm32f407-olimex/ozone.jdebug.user | 9 + eigener_ctrl/stm32f407-olimex/web/web.h | 1 + 8 files changed, 452 insertions(+), 216 deletions(-) create mode 100644 eigener_ctrl/stm32f407-olimex/dfi.c create mode 100644 eigener_ctrl/stm32f407-olimex/dfi.h create mode 100644 eigener_ctrl/stm32f407-olimex/ozone.jdebug create mode 100644 eigener_ctrl/stm32f407-olimex/ozone.jdebug.user diff --git a/eigener_ctrl/stm32f407-olimex/Makefile b/eigener_ctrl/stm32f407-olimex/Makefile index 79dca77..c98d4ea 100755 --- a/eigener_ctrl/stm32f407-olimex/Makefile +++ b/eigener_ctrl/stm32f407-olimex/Makefile @@ -103,7 +103,7 @@ include $(CHIBIOS)/test/rt/test.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk include $(CHIBIOS)/os/various/shell/shell.mk include $(CHIBIOS)/os/various/lwip_bindings/lwip.mk -include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk +#include $(CHIBIOS)/os/various/fatfs_bindings/fatfs.mk # Define linker script file here LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld @@ -123,6 +123,7 @@ CSRC = $(STARTUPSRC) \ $(STREAMSSRC) \ $(SHELLSRC) \ $(CHIBIOS)/os/various/evtimer.c \ + dfi.c \ web/web.c usbcfg.c main.c # C++ sources that can be compiled in ARM or THUMB mode depending on the global diff --git a/eigener_ctrl/stm32f407-olimex/chibios/os/various/lwip_bindings/lwipthread.h b/eigener_ctrl/stm32f407-olimex/chibios/os/various/lwip_bindings/lwipthread.h index fd222f6..4080d9b 100755 --- a/eigener_ctrl/stm32f407-olimex/chibios/os/various/lwip_bindings/lwipthread.h +++ b/eigener_ctrl/stm32f407-olimex/chibios/os/various/lwip_bindings/lwipthread.h @@ -51,14 +51,14 @@ * @brief IP Address. */ #if !defined(LWIP_IPADDR) || defined(__DOXYGEN__) -#define LWIP_IPADDR(p) IP4_ADDR(p, 192, 168, 1, 10) +#define LWIP_IPADDR(p) IP4_ADDR(p, 195, 160, 169, 111) #endif /** * @brief IP Gateway. */ #if !defined(LWIP_GATEWAY) || defined(__DOXYGEN__) -#define LWIP_GATEWAY(p) IP4_ADDR(p, 192, 168, 1, 1) +#define LWIP_GATEWAY(p) IP4_ADDR(p, 192, 160, 169, 1) #endif /** diff --git a/eigener_ctrl/stm32f407-olimex/dfi.c b/eigener_ctrl/stm32f407-olimex/dfi.c new file mode 100644 index 0000000..7c63ce4 --- /dev/null +++ b/eigener_ctrl/stm32f407-olimex/dfi.c @@ -0,0 +1,177 @@ +#include "ch.h" +#include "hal.h" + +#include "dfi.h" + + +#define FET_COUNT 8 +#define BYTE_PER_FET 6 + +volatile uint8_t data[FET_COUNT][BYTE_PER_FET]; +static virtual_timer_t mosfet_timer; + +// prototypes +void initializeSRData(void); +static void refreshDisplay(void *arg); +void shiftOut(uint8_t); + + +THD_WORKING_AREA(wa_dfiFunc, DFI_THREAD_STACK_SIZE); +THD_FUNCTION(dfiFunc, p) { + (void)p; + chRegSetThreadName("dfi"); + + static systime_t last_time_simul = 0; + static uint8_t counterFet = 0; + static uint8_t counterDigit = 0; + static uint8_t counterBit = 0; + chVTObjectInit(&mosfet_timer); + chVTSet(&mosfet_timer, MS2ST(10), refreshDisplay, NULL); + + while(true) { + + if(ST2MS(chVTTimeElapsedSinceX(last_time_simul)) > 100) { + + for(int fet=0; fet 7) { + counterBit = 0; + counterDigit++; + + if(counterDigit > 5) { + counterFet++; + counterDigit = 0; + counterFet %= FET_COUNT; + } + } + + last_time_simul = chVTGetSystemTimeX(); + palTogglePad(GPIOC, GPIOC_LED); + } + } +} + + +void initializeSRData() { + palSetPad(GPIOE, GPIOE_PIN1); //Tells all SRs that uController is sending data + + for(int digit = 0; digit < BYTE_PER_FET; digit++) { + shiftOut(0); + } + + for(int fet=0; fet> i; + /* + if(bitbit == 1) { + palSetPad(GPIOE, GPIOE_PIN2); + } else { + palClearPad(GPIOE, GPIOE_PIN2); + }*/ + palWritePad(GPIOE, GPIOE_PIN2, bitbit); + + palSetPad(GPIOE, GPIOE_PIN0); //clock + for (int w = 0; w < 1000; w++); + palClearPad(GPIOE, GPIOE_PIN0); //clock + for (int w = 0; w < 1000; w++); + } +} + + + +void init_hw() { + + initializeSRData(); + + palSetGroupMode(GPIOD, 0xff00, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); + palSetGroupMode(GPIOE, 0x00ff, 0, PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST); + /* + // enable clock + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOE, ENABLE); + + GPIO_InitTypeDef GPIO_InitStructure; + + GPIO_InitStructure.GPIO_Pin = GPIOD_PIN8 | GPIOD_PIN9 | GPIOD_PIN10 | + GPIOD_PIN11 | GPIOD_PIN12 | GPIOD_PIN13 | GPIOD_PIN14 | GPIOD_PIN15; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_Init(GPIOD, &GPIO_InitStructure); + + + GPIO_InitStructure.GPIO_Pin = GPIOE_PIN0 | GPIOE_PIN1 | GPIOE_PIN2 | + GPIOE_PIN3 | GPIOE_PIN4 | GPIOE_PIN5 | GPIOE_PIN6 | GPIOE_PIN7; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; + GPIO_Init(GPIOE, &GPIO_InitStructure); + +*/ + +} diff --git a/eigener_ctrl/stm32f407-olimex/dfi.h b/eigener_ctrl/stm32f407-olimex/dfi.h new file mode 100644 index 0000000..c117a37 --- /dev/null +++ b/eigener_ctrl/stm32f407-olimex/dfi.h @@ -0,0 +1,27 @@ +#ifndef _DFI_H +#define _DFI_H + + +#ifndef DFI_THREAD_STACK_SIZE +#define DFI_THREAD_STACK_SIZE 1024 +#endif + +#ifndef DFI_THREAD_PRIORITY +#define DFI_THREAD_PRIORITY (LOWPRIO + 2) +#endif + +extern THD_WORKING_AREA(wa_dfiFunc, DFI_THREAD_STACK_SIZE); + +#ifdef __cplusplus +extern "C" { +#endif + THD_FUNCTION(dfiFunc, p); + void init_hw(void); +#ifdef __cplusplus +} +#endif + + + + +#endif diff --git a/eigener_ctrl/stm32f407-olimex/main.c b/eigener_ctrl/stm32f407-olimex/main.c index f329b13..3fed6f8 100755 --- a/eigener_ctrl/stm32f407-olimex/main.c +++ b/eigener_ctrl/stm32f407-olimex/main.c @@ -20,130 +20,12 @@ #include "ch.h" #include "hal.h" #include "ch_test.h" - #include "chprintf.h" #include "shell.h" - #include "lwipthread.h" #include "web/web.h" - -#include "ff.h" - #include "usbcfg.h" - -/*===========================================================================*/ -/* Card insertion monitor. */ -/*===========================================================================*/ - -#define POLLING_INTERVAL 10 -#define POLLING_DELAY 10 - -/** - * @brief Card monitor timer. - */ -static virtual_timer_t tmr; - -/** - * @brief Debounce counter. - */ -static unsigned cnt; - -/** - * @brief Card event sources. - */ -static event_source_t inserted_event, removed_event; - -/** - * @brief Insertion monitor timer callback function. - * - * @param[in] p pointer to the @p BaseBlockDevice object - * - * @notapi - */ -static void tmrfunc(void *p) { - BaseBlockDevice *bbdp = p; - - chSysLockFromISR(); - if (cnt > 0) { - if (blkIsInserted(bbdp)) { - if (--cnt == 0) { - chEvtBroadcastI(&inserted_event); - } - } - else - cnt = POLLING_INTERVAL; - } - else { - if (!blkIsInserted(bbdp)) { - cnt = POLLING_INTERVAL; - chEvtBroadcastI(&removed_event); - } - } - chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, bbdp); - chSysUnlockFromISR(); -} - -/** - * @brief Polling monitor start. - * - * @param[in] p pointer to an object implementing @p BaseBlockDevice - * - * @notapi - */ -static void tmr_init(void *p) { - - chEvtObjectInit(&inserted_event); - chEvtObjectInit(&removed_event); - chSysLock(); - cnt = POLLING_INTERVAL; - chVTSetI(&tmr, MS2ST(POLLING_DELAY), tmrfunc, p); - chSysUnlock(); -} - -/*===========================================================================*/ -/* FatFs related. */ -/*===========================================================================*/ - -/** - * @brief FS object. - */ -static FATFS SDC_FS; - -/* FS mounted and ready.*/ -static bool fs_ready = FALSE; - -/* Generic large buffer.*/ -static uint8_t fbuff[1024]; - -static FRESULT scan_files(BaseSequentialStream *chp, char *path) { - static FILINFO fno; - FRESULT res; - DIR dir; - size_t i; - char *fn; - - res = f_opendir(&dir, path); - if (res == FR_OK) { - i = strlen(path); - while (((res = f_readdir(&dir, &fno)) == FR_OK) && fno.fname[0]) { - if (FF_FS_RPATH && fno.fname[0] == '.') - continue; - fn = fno.fname; - if (fno.fattrib & AM_DIR) { - *(path + i) = '/'; - strcpy(path + i + 1, fn); - res = scan_files(chp, path); - *(path + i) = '\0'; - if (res != FR_OK) - break; - } - else { - chprintf(chp, "%s/%s\r\n", path, fn); - } - } - } - return res; -} +#include "dfi.h" /*===========================================================================*/ /* Command line related. */ @@ -151,39 +33,7 @@ static FRESULT scan_files(BaseSequentialStream *chp, char *path) { #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) -static void cmd_tree(BaseSequentialStream *chp, int argc, char *argv[]) { - FRESULT err; - uint32_t fre_clust, fre_sect, tot_sect; - FATFS *fsp; - - (void)argv; - if (argc > 0) { - chprintf(chp, "Usage: tree\r\n"); - return; - } - if (!fs_ready) { - chprintf(chp, "File System not mounted\r\n"); - return; - } - err = f_getfree("/", &fre_clust, &fsp); - if (err != FR_OK) { - chprintf(chp, "FS: f_getfree() failed\r\n"); - return; - } - chprintf(chp, - "FS: %lu free clusters with %lu sectors (%lu bytes) per cluster\r\n", - fre_clust, (uint32_t)fsp->csize, (uint32_t)fsp->csize * 512); - chprintf(chp, - " %lu bytes (%lu MB) free of %lu MB\r\n", - fre_sect * 512, - fre_sect / 2 / 1024, - tot_sect / 2 / 1024); - fbuff[0] = 0; - scan_files(chp, (char *)fbuff); -} - static const ShellCommand commands[] = { - {"tree", cmd_tree}, {NULL, NULL} }; @@ -192,48 +42,13 @@ static const ShellConfig shell_cfg1 = { commands }; -/*===========================================================================*/ -/* Main and generic code. */ -/*===========================================================================*/ - static thread_t *shelltp = NULL; -/* - * Card insertion event. - */ -static void InsertHandler(eventid_t id) { - FRESULT err; - - (void)id; - /* - * On insertion SDC initialization and FS mount. - */ - if (sdcConnect(&SDCD1)) - return; - - err = f_mount(&SDC_FS, "/", 1); - if (err != FR_OK) { - sdcDisconnect(&SDCD1); - return; - } - fs_ready = TRUE; -} - -/* - * Card removal event. - */ -static void RemoveHandler(eventid_t id) { - - (void)id; - sdcDisconnect(&SDCD1); - fs_ready = FALSE; -} /* * Shell exit event. */ static void ShellHandler(eventid_t id) { - (void)id; if (chThdTerminatedX(shelltp)) { chThdWait(shelltp); /* Returning memory to heap. */ @@ -246,12 +61,11 @@ static void ShellHandler(eventid_t id) { */ static THD_WORKING_AREA(waThread1, 128); static THD_FUNCTION(Thread1, arg) { - (void)arg; chRegSetThreadName("blinker"); while (true) { - palTogglePad(GPIOC, GPIOC_LED); - chThdSleepMilliseconds(fs_ready ? 125 : 500); + //palTogglePad(GPIOC, GPIOC_LED); + chThdSleepMilliseconds(100); } } @@ -260,11 +74,9 @@ static THD_FUNCTION(Thread1, arg) { */ int main(void) { static const evhandler_t evhndl[] = { - InsertHandler, - RemoveHandler, ShellHandler }; - event_listener_t el0, el1, el2; + event_listener_t el2; /* * System initializations. @@ -299,35 +111,18 @@ int main(void) { */ shellInit(); - /* - * Activates the serial driver 6 and SDC driver 1 using default - * configuration. - */ - sdStart(&SD6, NULL); - sdcStart(&SDCD1, NULL); - /* - * Activates the card insertion monitor. - */ - tmr_init(&SDCD1); - /* - * Creates the blinker thread. - */ + init_hw(); + chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); - - /* - * Creates the HTTP thread (it changes priority internally). - */ - chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1, - http_server, NULL); + chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1, http_server, NULL); + chThdCreateStatic(wa_dfiFunc, sizeof(wa_dfiFunc), NORMALPRIO + 1, dfiFunc, NULL); /* * Normal main() thread activity, handling SD card events and shell * start/exit. */ - chEvtRegister(&inserted_event, &el0, 0); - chEvtRegister(&removed_event, &el1, 1); chEvtRegister(&shell_terminated, &el2, 2); while (true) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) { diff --git a/eigener_ctrl/stm32f407-olimex/ozone.jdebug b/eigener_ctrl/stm32f407-olimex/ozone.jdebug new file mode 100644 index 0000000..ffc4e80 --- /dev/null +++ b/eigener_ctrl/stm32f407-olimex/ozone.jdebug @@ -0,0 +1,226 @@ + +/********************************************************************* +* +* OnProjectLoad +* +* Function description +* Project load routine. Required. +* +********************************************************************** +*/ +void OnProjectLoad (void) { + // + // Dialog-generated settings + // + Project.SetDevice ("STM32F407VG"); + Project.SetHostIF ("USB", ""); + Project.SetTargetIF ("JTAG"); + Project.SetTIFSpeed ("1 MHz"); + Project.AddSvdFile ("Cortex-M4F.svd"); + Project.AddSvdFile ("$(InstallDir)/Config/Peripherals/ARMv7M.svd"); + // + // User settings + // + File.Open ("/home/lucas/ctdo/repos/dfi-led-matrix/eigener_ctrl/stm32f407-olimex/build/dfi.elf"); +} + +/********************************************************************* +* +* TargetReset +* +* Function description +* Replaces the default target device reset routine. Optional. +* +* Notes +* This example demonstrates the usage when +* debugging a RAM program on a Cortex-M target device +* +********************************************************************** +*/ +//void TargetReset (void) { +// +// unsigned int SP; +// unsigned int PC; +// unsigned int VectorTableAddr; +// +// Exec.Reset(); +// +// VectorTableAddr = Elf.GetBaseAddr(); +// +// if (VectorTableAddr != 0xFFFFFFFF) { +// +// Util.Log("Resetting Program."); +// +// SP = Target.ReadU32(VectorTableAddr); +// Target.SetReg("SP", SP); +// +// PC = Target.ReadU32(VectorTableAddr + 4); +// Target.SetReg("PC", PC); +// } +//} + +/********************************************************************* +* +* BeforeTargetReset +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void BeforeTargetReset (void) { +//} + +/********************************************************************* +* +* AfterTargetReset +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void AfterTargetReset (void) { +//} + +/********************************************************************* +* +* DebugStart +* +* Function description +* Replaces the default debug session startup routine. Optional. +* +********************************************************************** +*/ +//void DebugStart (void) { +//} + +/********************************************************************* +* +* TargetConnect +* +* Function description +* Replaces the default target IF connection routine. Optional. +* +********************************************************************** +*/ +//void TargetConnect (void) { +//} + +/********************************************************************* +* +* BeforeTargetConnect +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void BeforeTargetConnect (void) { +//} + +/********************************************************************* +* +* AfterTargetConnect +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void AfterTargetConnect (void) { +//} + +/********************************************************************* +* +* TargetDownload +* +* Function description +* Replaces the default program download routine. Optional. +* +********************************************************************** +*/ +//void TargetDownload (void) { +//} + +/********************************************************************* +* +* BeforeTargetDownload +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void BeforeTargetDownload (void) { +//} + +/********************************************************************* +* +* AfterTargetDownload +* +* Function description +* Event handler routine. +* +* Notes +* This sample implementation demonstrates the application +* initialization on a Cortex-M target. +* If no initialization can be done, Target.Reset() may be called. +* +********************************************************************** +*/ +//void AfterTargetDownload (void) { +// +// unsigned int SP; +// unsigned int PC; +// unsigned int VectorTableAddr; +// +// VectorTableAddr = Elf.GetBaseAddr(); +// +// if (VectorTableAddr != 0xFFFFFFFF) { +// +// Util.Log("Initializing PC and SP."); +// +// SP = Target.ReadU32(VectorTableAddr); +// Target.SetReg("SP", SP); +// +// PC = Target.ReadU32(VectorTableAddr + 4); +// Target.SetReg("PC", PC); +// } +//} + +/********************************************************************* +* +* BeforeTargetDisconnect +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void BeforeTargetDisconnect (void) { +//} + +/********************************************************************* +* +* AfterTargetDisconnect +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void AfterTargetDisconnect (void) { +//} + +/********************************************************************* +* +* AfterTargetHalt +* +* Function description +* Event handler routine. Optional. +* +********************************************************************** +*/ +//void AfterTargetHalt (void) { +//} diff --git a/eigener_ctrl/stm32f407-olimex/ozone.jdebug.user b/eigener_ctrl/stm32f407-olimex/ozone.jdebug.user new file mode 100644 index 0000000..8827b27 --- /dev/null +++ b/eigener_ctrl/stm32f407-olimex/ozone.jdebug.user @@ -0,0 +1,9 @@ +OpenWindow="Memory1", DockArea=BOTTOM, x=0, y=0, w=972, h=261, FilterBarShown=0, +OpenWindow="Disassembly", DockArea=RIGHT, x=0, y=0, w=289, h=338, FilterBarShown=0, ExecCountersShown=0 +OpenWindow="Registers", DockArea=RIGHT, x=0, y=1, w=289, h=338, FilterBarShown=0, +OpenWindow="Console", DockArea=BOTTOM, x=1, y=0, w=942, h=261, FilterBarShown=0, +OpenWindow="Functions", DockArea=LEFT, x=0, y=0, w=465, h=338, FilterBarShown=1, +OpenWindow="Memory Usage", DockArea=LEFT, x=0, y=1, w=465, h=338, FilterBarShown=0, +OpenToolbar="Debug", Floating=0, x=0, y=0 +TableHeader="Registers", SortCol="Name", SortOrder="ASCENDING", VisibleCols=["Name";"Value";"Description"], ColWidths=[125;115;252] +TableHeader="Functions", SortCol="Name", SortOrder="ASCENDING", VisibleCols=["Name";"Address";"Size";"#Insts";"Source"], ColWidths=[170;75;48;57;100] diff --git a/eigener_ctrl/stm32f407-olimex/web/web.h b/eigener_ctrl/stm32f407-olimex/web/web.h index 67f136e..6ddc0c7 100755 --- a/eigener_ctrl/stm32f407-olimex/web/web.h +++ b/eigener_ctrl/stm32f407-olimex/web/web.h @@ -24,6 +24,7 @@ #ifndef WEB_H #define WEB_H + #ifndef WEB_THREAD_STACK_SIZE #define WEB_THREAD_STACK_SIZE 1024 #endif