Compare commits

...

6 Commits

  1. 2
      Makefile
  2. 4
      include/buzzer.h
  3. 4
      include/console.h
  4. 2
      include/led.h
  5. 1
      include/rtc.h
  6. 2
      source/core/Makefile
  7. 29
      source/main.c
  8. 46
      source/platform/usart.c
  9. 64
      source/platform/virtual/alarm.c
  10. 61
      source/platform/virtual/buzzer.c
  11. 53
      source/platform/virtual/gpio.c
  12. 40
      source/platform/virtual/led.c
  13. 25
      source/platform/virtual/rcc.c
  14. 22
      source/platform/virtual/rtc.c
  15. 150
      source/platform/virtual/spi.c
  16. 2
      source/shell/alarm.c
  17. 1
      source/shell/buzzer.c
  18. 2
      source/shell/led.c

@ -1,5 +1,5 @@
TARGET ?= host
TARGET := $(filter host stm32f0 stm32f1,${TARGET})
TARGET := $(filter host stm32f0 stm32f1 virtual,${TARGET})
ifeq (${TARGET},)
$(error No support available for the target)

@ -1,10 +1,14 @@
#pragma once
/*
#include <libopencm3/stm32/timer.h>
*/
struct buzzer {
uint32_t timer;
/*
enum tim_oc_id channel;
*/
};
void buzzer_init(struct buzzer *buzzer);

@ -1,16 +1,20 @@
#pragma once
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
struct console {
FILE *read_fp;
FILE *fp;
};
struct console *console_init(unsigned dev_id);
struct console *console_init_fd(unsigned dev_id, FILE* rfd, FILE* wfd);
int console_peek(char *c, struct console *console, int block);
int console_getc(char *c, struct console *console);
int console_getline(struct console *console, char *buf, size_t n);

@ -1,5 +1,7 @@
#pragma once
#include <stdint.h>
struct led {
uint32_t port;
uint32_t pin;

@ -1,6 +1,7 @@
#pragma once
#include <time.h>
#include <stdint.h>
#define SECS_PER_DAY (24 * 60 * 60)

@ -1 +1 @@
tbm-obj-y += source/core/flash.o
tbm-obj-y += source/core/flash.o source/core/spi.o

@ -2,6 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <buzzer.h>
#include <console.h>
@ -79,7 +80,8 @@ struct cmd admin_cmds[] = {
{ NULL, NULL },
};
int main(void)
//int main(void)
int main(int argc, char **argv)
{
size_t i;
@ -91,8 +93,18 @@ int main(void)
buzzer_init(buzzers + i);
}
user_con = console_init(1);
admin_con = console_init(0);
FILE *admin_out = fopen(argv[1], "w");
if (admin_out == NULL) {
perror("admin_out cannot be opened");
return 1;
}
FILE *admin_in = fopen(argv[1], "r");
if (admin_in == NULL) {
perror("admin_in cannot be opened");
return 1;
}
//user_con = console_init_fd(1, user_in, user_out);
admin_con = console_init_fd(0, admin_in, admin_out);
if ((flash = flash_probe())) {
flash = ftl_mount(flash);
@ -101,16 +113,19 @@ int main(void)
fprintf(admin_con->fp, "error: unable to probe flash chip.\n");
}
fprintf(user_con->fp, "TBM-dev (built on " BUILD_VERSION ")\n");
fprintf(stderr, "fds opened\n");
//fprintf(user_con->fp, "TBM-dev (built on " BUILD_VERSION ")\n");
fprintf(admin_con->fp, "TBM-dev (built on " BUILD_VERSION ")\n");
//fflush(user_con->fp);
fflush(admin_con->fp);
shell_init(&user_shell, user_cmds, user_con, "tbm $",
SHELL_SHOW_EXIT_CODE);
//shell_init(&user_shell, user_cmds, user_con, "tbm $",
// SHELL_SHOW_EXIT_CODE);
shell_init(&admin_shell, admin_cmds, admin_con, "tbm #",
SHELL_SHOW_EXIT_CODE);
while (1) {
shell_parse(&user_shell);
//shell_parse(&user_shell);
shell_parse(&admin_shell);
}

@ -1,7 +1,11 @@
#define _GNU_SOURCE
#include <stdint.h>
/*
#include <libopencm3/stm32/usart.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/scb.h>
*/
#include <ctype.h>
#include <stdarg.h>
@ -30,6 +34,7 @@ struct usart_console {
static struct usart_console usart[2];
#if 0
#ifdef STM32F0
#define USART_STOPBITS_1 USART_CR2_STOP_1_0BIT
#endif
@ -37,9 +42,15 @@ static struct usart_console usart[2];
#ifdef STM32F1
#define USART_ISR_RXNE USART_SR_RXNE
#endif
#endif
void usart_send_blocking(uint32_t dev, char c) {
return;
}
static void usart_isr(struct usart_console *console)
{
#if 0
size_t i;
if (!console)
@ -53,6 +64,7 @@ static void usart_isr(struct usart_console *console)
if (i != console->cur)
console->next = i;
}
#endif
}
void usart1_isr(void)
@ -82,6 +94,14 @@ int console_peek(char *c, struct console *console_, int block)
int console_getc(char *c, struct console *console_)
{
*c = fgetc(console_->read_fp);
if (*c == EOF) {
return -1;
}
fprintf(stderr, "fgetc on %d gives %x\n", fileno(console_->read_fp), *c);
return 0;
#if 0
struct usart_console *console = container_of(console_,
struct usart_console, console);
@ -94,6 +114,7 @@ int console_getc(char *c, struct console *console_)
}
return 0;
#endif
}
int console_getline(struct console *console_, char *buf, size_t n)
@ -147,6 +168,8 @@ int console_getline(struct console *console_, char *buf, size_t n)
ssize_t console_read(struct console *console, char *buf, size_t n)
{
return fread(buf, n, 1, console->read_fp);
#if 0
char *p = buf;
char c;
@ -166,6 +189,7 @@ ssize_t console_read(struct console *console, char *buf, size_t n)
}
return p - buf;
#endif
}
static ssize_t usart_write(void *cookie, const char *buf, size_t n)
@ -192,17 +216,20 @@ static int usart_get_irq_no(unsigned *irq_no, unsigned dev)
if (!irq_no)
return -1;
#if 0
switch (dev) {
case USART1: *irq_no = NVIC_USART1_IRQ; break;
case USART2: *irq_no = NVIC_USART2_IRQ; break;
default: return -1;
}
#endif
return 0;
}
static int usart_init(struct usart_console *console)
{
#if 0
if (usart_get_irq_no(&console->irq_no, console->dev) < 0)
return -1;
@ -216,35 +243,54 @@ static int usart_init(struct usart_console *console)
nvic_enable_irq(console->irq_no);
usart_enable(console->dev);
usart_enable_rx_interrupt(console->dev);
#endif
return 0;
}
struct console *console_init_fd(unsigned dev_id, FILE* rfd, FILE* wfd) {
struct usart_console *console;
console = usart + dev_id;
console->console.read_fp = rfd;
console->console.fp = wfd;
setvbuf(console->console.fp, NULL, _IONBF, 0);
return &console->console;
}
struct console *console_init(unsigned dev_id)
{
#if 0
cookie_io_functions_t fops = {
.read = NULL,
.write = usart_write,
.seek = NULL,
.close = NULL,
};
#endif
struct usart_console *console;
console = usart + dev_id;
#if 0
switch (dev_id) {
case 0: console->dev = USART1; break;
case 1: console->dev = USART2; break;
default: return NULL;
}
#endif
console->cur = 0;
console->next = 0;
usart_init(console);
#if 0
console->console.fp = fopencookie(console, "w", fops);
setvbuf(console->console.fp, NULL, _IONBF, 0);
#endif
return &console->console;
}

@ -0,0 +1,64 @@
/*
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rtc.h>
*/
#include <rtc.h>
static int reset = 0;
void rtc_alarm_isr(void)
{
/*
if (rtc_check_flag(RTC_ALR)) {
rtc_clear_flag(RTC_ALR);
rtc_disable_alarm();
exti_reset_request(EXTI17);
if (!reset) {
reset = 1;
gpio_set(GPIOA, GPIO1);
rtc_set_alarm_time(rtc_get_counter_val() + 5);
rtc_enable_alarm();
} else {
reset = 0;
gpio_set(GPIOA, GPIO1);
alarm_disable();
}
}
*/
}
int alarm_enable(uint32_t timeout)
{
/*
rtc_enable_alarm();
rtc_set_alarm_time(rtc_get_counter_val() + timeout);
exti_enable_request(EXTI17);
exti_set_trigger(EXTI17, EXTI_TRIGGER_RISING);
nvic_enable_irq(NVIC_RTC_ALARM_IRQ);
nvic_set_priority(NVIC_RTC_ALARM_IRQ, 1);
rtc_interrupt_enable(RTC_ALR);
*/
return 0;
}
int alarm_disable(void)
{
/*
exti_disable_request(EXTI17);
nvic_disable_irq(NVIC_RTC_ALARM_IRQ);
rtc_interrupt_disable(RTC_ALR);
rtc_disable_alarm();
*/
return 0;
}

@ -0,0 +1,61 @@
/*
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/timer.h>
*/
#include <sys/types.h>
#include <stdint.h>
#include <buzzer.h>
#include <macros.h>
struct buzzer buzzers[] = {
{ },
};
size_t nbuzzers = count_of(buzzers);
void buzzer_init(struct buzzer *buzzer)
{
/*
rcc_periph_reset_pulse(buzzer->timer);
timer_set_mode(buzzer->timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE,
TIM_CR1_DIR_UP);
timer_set_prescaler(buzzer->timer, 72);
timer_set_repetition_counter(buzzer->timer, 0);
timer_enable_preload(buzzer->timer);
timer_enable_break_main_output(buzzer->timer);
timer_continuous_mode(buzzer->timer);
timer_disable_oc_output(buzzer->timer, buzzer->channel);
timer_set_oc_mode(buzzer->timer, buzzer->channel, TIM_OCM_PWM1);
timer_set_oc_value(buzzer->timer, buzzer->channel, 0);
timer_enable_oc_output(buzzer->timer, buzzer->channel);
*/
}
void buzzer_enable(struct buzzer *buzzer)
{
/*
timer_enable_counter(buzzer->timer);
*/
}
void buzzer_disable(struct buzzer *buzzer)
{
/*
timer_disable_counter(buzzer->timer);
*/
}
void buzzer_set_freq(struct buzzer *buzzer, uint32_t freq)
{
/*
uint32_t period = 1000000 / freq;
timer_set_period(buzzer->timer, period);
timer_set_oc_value(buzzer->timer, buzzer->channel, period / 2);
*/
}

@ -0,0 +1,53 @@
/*
#include <libopencm3/stm32/gpio.h>
*/
#include <gpio.h>
int gpio_init(void)
{
#if 0
/* Set up GPIOs for SPI 1 */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, GPIO_SPI1_NSS);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_SPI1_SCK |
GPIO_SPI1_MOSI);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
GPIO_SPI1_MISO);
/* Set up GPIOs for user console (USART 1) */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
/* Set up GPIOs for admin console (USART 2) */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
/* Set up GPIOs for reset. */
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN,
GPIO1);
/* Set up GPIOs for timers. */
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON,
AFIO_MAPR_TIM2_REMAP_PARTIAL_REMAP1);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM2_PR1_CH1_ETR);
/* Set up GPIO for LED. */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0);
gpio_clear(GPIOB, GPIO0);
gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
gpio_set(GPIOC, GPIO10);
#endif
return 0;
}
void gpio_cleanup(void)
{
}

@ -0,0 +1,40 @@
/*
#include <libopencm3/stm32/gpio.h>
*/
#include <stdint.h>
#include <sys/types.h>
#include <led.h>
#include <macros.h>
struct led leds[] = {
{ },
};
size_t nleds = count_of(leds);
void led_enable(struct led *led)
{
/*
if (!led)
return;
if (led->invert)
gpio_clear(led->port, led->pin);
else
gpio_set(led->port, led->pin);
*/
}
void led_disable(struct led *led)
{
/*
if (!led)
return;
if (led->invert)
gpio_set(led->port, led->pin);
else
gpio_clear(led->port, led->pin);
*/
}

@ -0,0 +1,25 @@
/*
#include <libopencm3/stm32/rcc.h>
*/
#include <rcc.h>
int rcc_init(void)
{
/*
rcc_periph_clock_enable(RCC_AFIO);
rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOC);
rcc_periph_clock_enable(RCC_SPI1);
rcc_periph_clock_enable(RCC_USART1);
rcc_periph_clock_enable(RCC_USART2);
rcc_periph_clock_enable(RCC_TIM2);
*/
return 0;
}
void rcc_cleanup(void)
{
}

@ -0,0 +1,22 @@
#include <macros.h>
#include <rtc.h>
int rtc_get_time(struct tm *time)
{
return 0;
}
static int rtc_set_time(struct tm *time)
{
return 0;
}
int rtc_init(struct tm *time)
{
/*
rtc_set_time(time);
*/
return 0;
}

@ -0,0 +1,150 @@
#define _GNU_SOURCE
#include <stdint.h>
#include <stdlib.h>
#include <spi.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <spi_flash.h>
// XXX: 16 is used in other places in this file too
#define FLASH_SIZE (1 << 16)
static int virtual_spi_tx_rx(struct spi_dev *dev, void *rx_buf, size_t rx_len,
const void *tx_buf, size_t tx_len);
static struct spi_ops virtual_spi_ops = {
.tx_rx = virtual_spi_tx_rx,
};
// XXX: make part of dev struct
static unsigned char *flash_map;
static int virtual_spi_init(void)
{
/* TODO: close this fd */
FILE *flash_fd;
int exists = 0;
exists = access("flash", F_OK) != -1;
/* XXX: treat not existing separately from existing file, since we want to
* null one that doesn't exist yet */
flash_fd = fopen("flash", "ab+");
if (!exists) {
fprintf(stderr,
"flash doesn't exist yet, creating and filling now\n");
char buf[FLASH_SIZE];
int ret = fwrite(buf, sizeof(char), FLASH_SIZE, flash_fd);
fprintf(stderr, "fwrite ret: %d\n", ret);
}
if (flash_fd == NULL) {
perror("flash_open");
fprintf(stderr, "spi init failed\n");
return 1;
}
flash_map =
mmap(NULL, FLASH_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
fileno(flash_fd), 0);
fprintf(stderr, "flash: %p\n", flash_map);
return 0;
}
struct spi_dev *spi_probe(void)
{
struct spi_dev *dev;
if (!(dev = malloc(sizeof *dev)))
return NULL;
dev->ops = &virtual_spi_ops;
/*
dev->dev_id = SPI1;
*/
if (virtual_spi_init()) {
free(dev);
return NULL;
}
return dev;
}
void spi_release(struct spi_dev *dev)
{
free(dev);
/* TODO: close fd, close mmap */
}
static int virtual_spi_tx_rx(struct spi_dev *dev, void *rx_buf, size_t rx_len,
const void *tx_buf, size_t tx_len)
{
unsigned char *rx_buf_c = (unsigned char *)rx_buf;
unsigned char *tx_buf_c = (unsigned char *)tx_buf;
unsigned char cmd = tx_buf_c[0];
fprintf(stderr, "? RX len: %ld\n", rx_len);
if (tx_buf != NULL) {
fprintf(stderr, "? TX Command: %d\n", cmd);
fprintf(stderr, "? TX len: %ld\n", tx_len);
if (cmd == SPI_FLASH_JEDEC_ID) {
if (rx_len != 3) {
fprintf(stderr, "UNEXPECTED RX_LEN\n");
}
fprintf(stderr, "SPI_FLASH_JEDEC_ID\n");
rx_buf_c[0] = 0x1;
rx_buf_c[1] = 0x1;
rx_buf_c[2] = 16;
}
if (cmd == SPI_FLASH_READ) {
fprintf(stderr, "SPI_FLASH_READ\n");
unsigned int addr = (tx_buf_c[1] << 16) +
(tx_buf_c[2] << 8) + tx_buf_c[3];
fprintf(stderr, "addr: %d\n", addr);
fprintf(stderr, "memmove: %d\n", addr);
memmove(rx_buf_c, flash_map + addr, rx_len);
}
if (cmd == SPI_FLASH_PAGE_PROGRAM) {
fprintf(stderr, "SPI_FLASH_PAGE_PROGRAM\n");
unsigned int addr = (tx_buf_c[1] << 16) +
(tx_buf_c[2] << 8) + tx_buf_c[3];
fprintf(stderr, "addr: %d\n", addr);
fprintf(stderr, "memmove: %d\n", addr);
memmove(flash_map + addr, tx_buf_c + 4, tx_len - 4);
}
if (cmd == SPI_FLASH_ERASE_4K) {
fprintf(stderr, "SPI_FLASH_ERASE_4K\n");
unsigned int addr = (tx_buf_c[1] << 16) +
(tx_buf_c[2] << 8) + tx_buf_c[3];
fprintf(stderr, "addr: %d\n", addr);
memset(flash_map + addr, 0, 4096);
}
if (cmd == SPI_FLASH_READ_SR1) {
fprintf(stderr, "SPI_FLASH_READ_SR1\n");
/* Just report read/write/erase as done, we are never busy with mmap */
rx_buf_c[0] = 0;
}
if (cmd == SPI_FLASH_WRITE_ENABLE) {
fprintf(stderr, "SPI_FLASH_WRITE_ENABLE\n");
/* These can be stubs */
}
if (cmd == SPI_FLASH_WRITE_DISABLE) {
fprintf(stderr, "SPI_FLASH_WRITE_DISABLE\n");
/* These can be stubs */
}
}
return 0;
}

@ -2,7 +2,9 @@
#include <stdlib.h>
#include <string.h>
#if 0
#include <libopencm3/stm32/gpio.h>
#endif
#include <console.h>
#include <rtc.h>

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <buzzer.h>
#include <console.h>

@ -2,7 +2,9 @@
#include <stdlib.h>
#include <string.h>
#if 0
#include <libopencm3/stm32/gpio.h>
#endif
#include <console.h>
#include <led.h>

Loading…
Cancel
Save