X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=src%2Fstm-demo%2Fao_demo.c;h=db432c2ae91a6f257a6a02847aa88f02c868e7ff;hp=5ff2b32a94eeded531e50b6db878e4d4e6034dd0;hb=1085ec5d57e0ed5d132f2bbdac1a0b6a32c0ab4a;hpb=2db6b0f58811ffc44a468c8fbcacc08d37edd26c diff --git a/src/stm-demo/ao_demo.c b/src/stm-demo/ao_demo.c index 5ff2b32a..db432c2a 100644 --- a/src/stm-demo/ao_demo.c +++ b/src/stm-demo/ao_demo.c @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -16,6 +17,11 @@ */ #include "ao.h" +#include +#include +#include +#include +#include struct ao_task demo_task; @@ -51,13 +57,15 @@ void _exit () { } void _read () { } void _fstat() { } +#define AO_DMA_TEST_INDEX STM_DMA_INDEX(4) + static void ao_dma_test(void) { static char src[20] = "hello, world"; static char dst[20]; dst[0] = '\0'; - ao_dma_set_transfer(STM_DMA_INDEX(1), dst, src, 13, + ao_dma_set_transfer(AO_DMA_TEST_INDEX, dst, src, 13, (1 << STM_DMA_CCR_MEM2MEM) | (STM_DMA_CCR_PL_LOW << STM_DMA_CCR_PL) | (STM_DMA_CCR_MSIZE_8 << STM_DMA_CCR_MSIZE) | @@ -66,11 +74,12 @@ ao_dma_test(void) { (1 << STM_DMA_CCR_PINC) | (0 << STM_DMA_CCR_CIRC) | (STM_DMA_CCR_DIR_MEM_TO_PER << STM_DMA_CCR_DIR)); - ao_dma_start(STM_DMA_INDEX(1)); - cli(); - while (!ao_dma_done[STM_DMA_INDEX(1)]) - ao_sleep(&ao_dma_done[STM_DMA_INDEX(1)]); - sei(); + ao_dma_start(AO_DMA_TEST_INDEX); + ao_arch_critical( + while (!ao_dma_done[AO_DMA_TEST_INDEX]) + ao_sleep(&ao_dma_done[AO_DMA_TEST_INDEX]); + ); + ao_dma_done_transfer(AO_DMA_TEST_INDEX); printf ("copied %s\n", dst); } @@ -80,7 +89,7 @@ ao_spi_write(void) { int i; for (i = 0; i < 10; i++) { - ao_spi_get(0); + ao_spi_get(0, AO_SPI_SPEED_FAST); stm_gpio_set(&stm_gpioc, 12, 0); ao_spi_send(data, 4, 0); stm_gpio_set(&stm_gpioc, 12, 1); @@ -97,7 +106,7 @@ ao_spi_read(void) { int i; for (i = 0; i < 10; i++) { - ao_spi_get(0); + ao_spi_get(0, AO_SPI_SPEED_FAST); stm_gpio_set(&stm_gpioc, 12, 0); ao_spi_recv(data, 4, 0); stm_gpio_set(&stm_gpioc, 12, 1); @@ -108,10 +117,94 @@ ao_spi_read(void) { } } +static void +ao_i2c_write(void) { + unsigned char data[] = { 0x55, 0xaa, 0xff, 0x00 }; + int i; + + for (i = 0; i < 10; i++) { + ao_i2c_get(0); + if (ao_i2c_start(0, 0x55)) + ao_i2c_send(data, 4, 0, TRUE); + else { + printf ("i2c start failed\n"); + ao_i2c_put(0); + break; + } + ao_i2c_put(0); + printf("."); + flush(); + ao_delay(100); + } +} + +static void +ao_temp (void) +{ + struct ao_data packet; + int temp; + + ao_data_get(&packet); + + /* + * r = (110 - 25) / (ts_cal_hot - ts_cal_cold) + * 25 + (110 - 25) * (temp - ts_cal_cold) / (ts_cal_hot - ts_cal_cold) + */ + temp = 25 + (110 - 25) * (packet.adc.temp - stm_temp_cal.ts_cal_cold) / (stm_temp_cal.ts_cal_hot - stm_temp_cal.ts_cal_cold); + printf ("temp: %d\n", temp); +} + +#if 0 +static void +ao_event(void) +{ + struct ao_event event; + + for (;;) { + flush(); + ao_event_get(&event); + printf ("type %1d unit %1d tick %5u value %ld\n", + event.type, event.unit, event.tick, event.value); + if (event.value == 100) + break; + } + +} +#endif + +static uint8_t ao_blinking = 0; + +static void +ao_blink(void) +{ + for (;;) { + while (!ao_blinking) + ao_sleep(&ao_blinking); + while (ao_blinking) { + ao_led_toggle(AO_LED_BLUE|AO_LED_GREEN); + ao_delay(AO_MS_TO_TICKS(500)); + } + } +} + +static struct ao_task ao_blink_task; + +static void +ao_blink_toggle(void) +{ + ao_blinking = !ao_blinking; + ao_wakeup(&ao_blinking); +} + + __code struct ao_cmds ao_demo_cmds[] = { { ao_dma_test, "D\0DMA test" }, { ao_spi_write, "W\0SPI write" }, { ao_spi_read, "R\0SPI read" }, + { ao_i2c_write, "i\0I2C write" }, + { ao_temp, "t\0Show temp" }, + { ao_blink_toggle, "b\0Toggle LED blinking" }, +/* { ao_event, "e\0Monitor event queue" }, */ { 0, NULL } }; @@ -120,21 +213,30 @@ main(void) { ao_clock_init(); - ao_serial_init(); + ao_task_init(); + + ao_led_init(LEDS_AVAILABLE); + ao_led_on(AO_LED_GREEN); + ao_led_off(AO_LED_BLUE); ao_timer_init(); ao_dma_init(); ao_cmd_init(); // ao_lcd_stm_init(); // ao_lcd_font_init(); - ao_spi_init(); +// ao_spi_init(); +// ao_i2c_init(); +// ao_exti_init(); +// ao_quadrature_init(); +// ao_button_init(); + +// ao_timer_set_adc_interval(100); +// ao_adc_init(); + ao_usb_init(); + + ao_add_task(&ao_blink_task, ao_blink, "blink"); ao_cmd_register(&ao_demo_cmds[0]); - stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_GPIOCEN); - stm_gpio_set(&stm_gpioc, 12, 1); - stm_moder_set(&stm_gpioc, 12, STM_MODER_OUTPUT); - stm_otyper_set(&stm_gpioc, 12, STM_OTYPER_PUSH_PULL); - ao_start_scheduler(); return 0; }