working on stm32 implementation

This commit is contained in:
Lucas Pleß 2018-01-14 03:30:07 +01:00
parent fee8f342eb
commit 3d94c5f72e
8 changed files with 452 additions and 216 deletions

View file

@ -103,7 +103,7 @@ include $(CHIBIOS)/test/rt/test.mk
include $(CHIBIOS)/os/hal/lib/streams/streams.mk include $(CHIBIOS)/os/hal/lib/streams/streams.mk
include $(CHIBIOS)/os/various/shell/shell.mk include $(CHIBIOS)/os/various/shell/shell.mk
include $(CHIBIOS)/os/various/lwip_bindings/lwip.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 # Define linker script file here
LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld LDSCRIPT= $(STARTUPLD)/STM32F407xG.ld
@ -123,6 +123,7 @@ CSRC = $(STARTUPSRC) \
$(STREAMSSRC) \ $(STREAMSSRC) \
$(SHELLSRC) \ $(SHELLSRC) \
$(CHIBIOS)/os/various/evtimer.c \ $(CHIBIOS)/os/various/evtimer.c \
dfi.c \
web/web.c usbcfg.c main.c web/web.c usbcfg.c main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global # C++ sources that can be compiled in ARM or THUMB mode depending on the global

View file

@ -51,14 +51,14 @@
* @brief IP Address. * @brief IP Address.
*/ */
#if !defined(LWIP_IPADDR) || defined(__DOXYGEN__) #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 #endif
/** /**
* @brief IP Gateway. * @brief IP Gateway.
*/ */
#if !defined(LWIP_GATEWAY) || defined(__DOXYGEN__) #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 #endif
/** /**

View file

@ -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<FET_COUNT; fet++) {
for(int digit=0; digit<BYTE_PER_FET; digit++) {
if(fet == counterFet && digit == counterDigit) {
data[fet][digit] = 0xff; //(1 << counterBit);
} else {
data[fet][digit] = 0xff;
}
}
}
counterBit++;
if(counterBit > 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<FET_COUNT; fet++) {
for(int digit=0; digit<BYTE_PER_FET; digit++) {
data[fet][digit] = 0xff;
}
}
palClearPad(GPIOE, GPIOE_PIN1); //Tells all SRs that uController is done sending data
}
static void refreshDisplay(void *arg) {
(void)arg;
chSysLockFromISR();
static int fet = 0;
fet++;
fet %= FET_COUNT;
// switch fets
for(int digit = 0; digit < BYTE_PER_FET; digit++) {
shiftOut(data[fet][digit]);
}
palClearPort(GPIOD, 0xff00);
palSetPad(GPIOE, GPIOE_PIN1); // latch
for (int w = 0; w < 1000; w++);
palClearPad(GPIOE, GPIOE_PIN1); // latch
// switch fets
//palSetPort(GPIOD, (1 << (fet+8)));
palWritePad(GPIOD, GPIOD_PIN8, (fet == 0) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN9, (fet == 1) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN10, (fet == 2) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN11, (fet == 3) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN12, (fet == 4) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN13, (fet == 5) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN14, (fet == 6) ? 1 : 0);
palWritePad(GPIOD, GPIOD_PIN15, (fet == 7) ? 1 : 0);
//palClearPort(GPIOD, 0xff00);
chVTSetI(&mosfet_timer, MS2ST(2), refreshDisplay, NULL);
chSysUnlockFromISR();
}
void shiftOut(uint8_t val) {
uint8_t i;
uint8_t bitbit = false;
for (i = 0; i < 8; i++) {
//bitbit = !!((val & (1 << i)));
bitbit = (val & (1 << i)) >> 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);
*/
}

View file

@ -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

View file

@ -20,130 +20,12 @@
#include "ch.h" #include "ch.h"
#include "hal.h" #include "hal.h"
#include "ch_test.h" #include "ch_test.h"
#include "chprintf.h" #include "chprintf.h"
#include "shell.h" #include "shell.h"
#include "lwipthread.h" #include "lwipthread.h"
#include "web/web.h" #include "web/web.h"
#include "ff.h"
#include "usbcfg.h" #include "usbcfg.h"
#include "dfi.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;
}
/*===========================================================================*/ /*===========================================================================*/
/* Command line related. */ /* Command line related. */
@ -151,39 +33,7 @@ static FRESULT scan_files(BaseSequentialStream *chp, char *path) {
#define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048) #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[] = { static const ShellCommand commands[] = {
{"tree", cmd_tree},
{NULL, NULL} {NULL, NULL}
}; };
@ -192,48 +42,13 @@ static const ShellConfig shell_cfg1 = {
commands commands
}; };
/*===========================================================================*/
/* Main and generic code. */
/*===========================================================================*/
static thread_t *shelltp = NULL; 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. * Shell exit event.
*/ */
static void ShellHandler(eventid_t id) { static void ShellHandler(eventid_t id) {
(void)id; (void)id;
if (chThdTerminatedX(shelltp)) { if (chThdTerminatedX(shelltp)) {
chThdWait(shelltp); /* Returning memory to heap. */ chThdWait(shelltp); /* Returning memory to heap. */
@ -246,12 +61,11 @@ static void ShellHandler(eventid_t id) {
*/ */
static THD_WORKING_AREA(waThread1, 128); static THD_WORKING_AREA(waThread1, 128);
static THD_FUNCTION(Thread1, arg) { static THD_FUNCTION(Thread1, arg) {
(void)arg; (void)arg;
chRegSetThreadName("blinker"); chRegSetThreadName("blinker");
while (true) { while (true) {
palTogglePad(GPIOC, GPIOC_LED); //palTogglePad(GPIOC, GPIOC_LED);
chThdSleepMilliseconds(fs_ready ? 125 : 500); chThdSleepMilliseconds(100);
} }
} }
@ -260,11 +74,9 @@ static THD_FUNCTION(Thread1, arg) {
*/ */
int main(void) { int main(void) {
static const evhandler_t evhndl[] = { static const evhandler_t evhndl[] = {
InsertHandler,
RemoveHandler,
ShellHandler ShellHandler
}; };
event_listener_t el0, el1, el2; event_listener_t el2;
/* /*
* System initializations. * System initializations.
@ -299,35 +111,18 @@ int main(void) {
*/ */
shellInit(); 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);
/* init_hw();
* Creates the blinker thread.
*/
chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1, http_server, NULL);
/* chThdCreateStatic(wa_dfiFunc, sizeof(wa_dfiFunc), NORMALPRIO + 1, dfiFunc, NULL);
* Creates the HTTP thread (it changes priority internally).
*/
chThdCreateStatic(wa_http_server, sizeof(wa_http_server), NORMALPRIO + 1,
http_server, NULL);
/* /*
* Normal main() thread activity, handling SD card events and shell * Normal main() thread activity, handling SD card events and shell
* start/exit. * start/exit.
*/ */
chEvtRegister(&inserted_event, &el0, 0);
chEvtRegister(&removed_event, &el1, 1);
chEvtRegister(&shell_terminated, &el2, 2); chEvtRegister(&shell_terminated, &el2, 2);
while (true) { while (true) {
if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) { if (!shelltp && (SDU1.config->usbp->state == USB_ACTIVE)) {

View file

@ -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) {
//}

View file

@ -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]

View file

@ -24,6 +24,7 @@
#ifndef WEB_H #ifndef WEB_H
#define WEB_H #define WEB_H
#ifndef WEB_THREAD_STACK_SIZE #ifndef WEB_THREAD_STACK_SIZE
#define WEB_THREAD_STACK_SIZE 1024 #define WEB_THREAD_STACK_SIZE 1024
#endif #endif