* Improve uart dma ring buffer. Sending dma still missing

* Add digio module for controlling LEDs, Loudspeaker, and the Digital IOs
* General code improvements
This commit is contained in:
Mario Hüttel 2020-02-10 22:38:24 +01:00
parent 48fea3d36e
commit 69c00ff3d3
14 changed files with 340 additions and 96 deletions

View File

@ -10,7 +10,7 @@ INCLUDEPATH = -Icmsis -Iinclude
OBJDIR = obj OBJDIR = obj
target = reflow-controller target = reflow-controller
LIBRARYPATH = -L. -Lmathlib LIBRARYPATH = -L. -Lmathlib
LIBRARIES = # -larm_cortexM4lf_math LIBRARIES = -larm_cortexM4lf_math
DEFINES = -DSTM32F407xx -DSTM32F4XX -DARM_MATH_CM4 -DHSE_VALUE=8000000UL DEFINES = -DSTM32F407xx -DSTM32F4XX -DARM_MATH_CM4 -DHSE_VALUE=8000000UL
mapfile = memory-mapping mapfile = memory-mapping
@ -36,6 +36,10 @@ INCLUDEPATH += -Ishellmatta/api
CFILES += clock-enable-manager.c CFILES += clock-enable-manager.c
CFILES += uart/uart.c uart/dma-ring-buffer.c CFILES += uart/uart.c uart/dma-ring-buffer.c
CFILES += digio.c
DEFINES += -DDEBUGBUILD
#TODO #TODO
################################################################################### ###################################################################################

View File

@ -15,7 +15,7 @@ static volatile enum adc_pt1000_error pt1000_error;
static volatile uint8_t * volatile streaming_flag_ptr = NULL; static volatile uint8_t * volatile streaming_flag_ptr = NULL;
static uint32_t filter_startup_cnt; static uint32_t filter_startup_cnt;
static volatile float adc_pt1000_raw_reading_hf; static volatile float adc_pt1000_raw_reading_hf;
static volatile bool pt1000_measurement_active = false;
static volatile uint16_t dma_sample_buffer[ADC_PT1000_DMA_AVG_SAMPLES]; static volatile uint16_t dma_sample_buffer[ADC_PT1000_DMA_AVG_SAMPLES];
#define ADC_TO_RES(adc) ((float)(adc) / 4096.0f * 2500.0f) #define ADC_TO_RES(adc) ((float)(adc) / 4096.0f * 2500.0f)
@ -49,6 +49,8 @@ static inline void adc_pt1000_disable_adc()
ADC1->CR2 &= ~ADC_CR2_ADON; ADC1->CR2 &= ~ADC_CR2_ADON;
DMA2_Stream0->CR = 0; DMA2_Stream0->CR = 0;
pt1000_measurement_active = false;
rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_ADC1EN)); rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(RCC_APB2ENR_ADC1EN));
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ADC_PT1000_PORT_RCC_MASK)); rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(ADC_PT1000_PORT_RCC_MASK));
} }
@ -139,6 +141,8 @@ void adc_pt1000_setup_meas()
adc_pt1000_enable_dma_stream(); adc_pt1000_enable_dma_stream();
adc_pt1000_setup_sample_frequency_timer(); adc_pt1000_setup_sample_frequency_timer();
pt1000_measurement_active = true;
} }
void adc_pt1000_set_moving_average_filter_param(float alpha) void adc_pt1000_set_moving_average_filter_param(float alpha)
@ -197,12 +201,12 @@ return_value:
return ret_val; return ret_val;
} }
int adc_pt1000_stream_raw_value_to_memory(uint16_t *adc_array, uint32_t length, volatile uint8_t *flag_to_set) int adc_pt1000_stream_raw_value_to_memory(float *adc_array, uint32_t length, volatile uint8_t *flag_to_set)
{ {
} }
void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, uint16_t *raw_source, uint32_t count) void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, float *raw_source, uint32_t count)
{ {
} }

View File

@ -1,8 +1,7 @@
#include <clock-enable-manager.h> #include <clock-enable-manager.h>
#include <helper-macros/helper-macros.h>
#include <stdlib.h> #include <stdlib.h>
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
struct rcc_enable_count { struct rcc_enable_count {
volatile uint32_t *rcc_reg_addr; volatile uint32_t *rcc_reg_addr;
uint32_t enable_bit_cnt; uint32_t enable_bit_cnt;

120
stm-firmware/digio.c Normal file
View File

@ -0,0 +1,120 @@
#include <digio.h>
#include <stm32f4xx.h>
#include <clock-enable-manager.h>
#include <stm32-gpio-macros.h>
#include <helper-macros/helper-macros.h>
static const uint8_t digio_pins[] = {DIGIO_PINS};
static const uint8_t digio_default_io[] = {DIGIO_INOUT_DEFAULT};
static const uint8_t digio_default_altfunc[] = {DIGIO_ALTFUNC_DEFAULT};
static void digio_setup_pin_int(uint8_t bit_no, uint8_t in_out, uint8_t alt_func)
{
DIGIO_PORT->MODER &= MODER_DELETE(bit_no);
switch (in_out) {
case 2:
DIGIO_PORT->MODER |= ALTFUNC(bit_no);
SETAF(DIGIO_PORT, bit_no, alt_func);
break;
case 1:
DIGIO_PORT->MODER |= OUTPUT(bit_no);
break;
case 0: /* expected fallthrough */
default:
break;
}
}
void digio_setup_default_all()
{
unsigned int i;
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(DIGIO_RCC_MASK));
for (i = 0; i < COUNT_OF(digio_pins); i++) {
digio_setup_pin_int(digio_pins[i], digio_default_io[i], digio_default_altfunc[i]);
if (digio_default_io[i] == 1)
digio_set(i, 0);
}
}
void digio_setup_pin(uint8_t num, uint8_t in_out, uint8_t alt_func)
{
if (num >= COUNT_OF(digio_pins))
return;
digio_setup_pin_int(digio_pins[num], in_out, alt_func);
}
void digio_set(uint8_t num, int val)
{
if (num >= COUNT_OF(digio_pins))
return;
if (val)
DIGIO_PORT->ODR |= (1<<digio_pins[num]);
else
DIGIO_PORT->ODR &= ~(1<<digio_pins[num]);
}
int digio_get(uint8_t num)
{
if (num >= COUNT_OF(digio_pins))
return -1;
if ((DIGIO_PORT->MODER & (0x3<<digio_pins[num])) == OUTPUT(digio_pins[num]))
return (DIGIO_PORT->ODR & (1<<digio_pins[num]) ? 1 : 0);
else
return (DIGIO_PORT->IDR & (1<<digio_pins[num]) ? 1 : 0);
}
static const uint8_t led_pins[] = {LED_PINS};
void led_setup()
{
unsigned int i;
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LED_RCC_MASK));
for (i = 0; i < COUNT_OF(led_pins); i++) {
LED_PORT->MODER &= MODER_DELETE(led_pins[i]);
LED_PORT->MODER |= OUTPUT(led_pins[i]);
}
}
void led_set(uint8_t num, int val)
{
if (num >= COUNT_OF(led_pins))
return;
if (val)
LED_PORT->ODR |= (1<<led_pins[num]);
else
LED_PORT->ODR &= ~(1<<led_pins[num]);
}
int led_get(uint8_t num)
{
if (num >= COUNT_OF(led_pins))
return -1;
return ((LED_PORT->ODR & (1<<led_pins[num])) ? 1 : 0);
}
void loudspeaker_setup()
{
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(LOUDSPEAKER_RCC_MASK));
LOUDSPEAKER_PORT->MODER &= MODER_DELETE(LOUDSPEAKER_PIN);
LOUDSPEAKER_PORT->MODER |= OUTPUT(LOUDSPEAKER_PIN);
loudspeaker_set(0);
}
void loudspeaker_set(int val)
{
if (val)
LOUDSPEAKER_PORT->ODR |= (1<<LOUDSPEAKER_PIN);
else
LOUDSPEAKER_PORT->ODR &= ~(1<<LOUDSPEAKER_PIN);
}
int loudspeaker_get()
{
return ((LOUDSPEAKER_PORT->ODR & (1<<LOUDSPEAKER_PIN)) ? 1 : 0);
}

View File

@ -111,9 +111,9 @@ int adc_pt1000_get_current_resistance(float *resistance);
* @param flag_to_set This flag is set to 1 once the data has been measured and is transferred. A negative value indicates an error * @param flag_to_set This flag is set to 1 once the data has been measured and is transferred. A negative value indicates an error
* @return 0 if measurement could be started * @return 0 if measurement could be started
*/ */
int adc_pt1000_stream_raw_value_to_memory(uint16_t *adc_array, uint32_t length, volatile uint8_t *flag_to_set); int adc_pt1000_stream_raw_value_to_memory(float *adc_array, uint32_t length, volatile uint8_t *flag_to_set);
void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, uint16_t *raw_source, uint32_t count); void adc_pt1000_convert_raw_value_array_to_resistance(float *resistance_dest, float *raw_source, uint32_t count);
/** /**
* @brief Check if the ADC measurement experienced any kind of error (DMA, Analog Watchdog, etc...) * @brief Check if the ADC measurement experienced any kind of error (DMA, Analog Watchdog, etc...)

View File

@ -0,0 +1,47 @@
#ifndef __DIGIO_H__
#define __DIGIO_H__
#include <stdbool.h>
#include <stdint.h>
#include <helper-macros/helper-macros.h>
#define DIGIO_PORT GPIOB
#define DIGIO_RCC_MASK RCC_AHB1ENR_GPIOBEN
#define DIGIO_PINS 4,5,6,7
#ifdef DEBUGBUILD
#define DIGIO_INOUT_DEFAULT 0,0,0,0
#define DIGIO_ALTFUNC_DEFAULT 0,0,0,0
#else
#define DIGIO_INOUT_DEFAULT 0,0,2,2
#define DIGIO_ALTFUNC_DEFAULT 0,0,7,7
#endif
#define BEEPER_PORT GPIOB
#define BEEPER_RCC_MASK RCC_AHB1ENR_GPIOBEN
void digio_setup_default_all();
void digio_setup_pin(uint8_t num, uint8_t in_out, uint8_t alt_func);
void digio_set(uint8_t num, int val);
int digio_get(uint8_t num);
#define LED_PORT GPIOB
#define LED_RCC_MASK RCC_AHB1ENR_GPIOBEN
#define LED_PINS 2,3
void led_setup();
void led_set(uint8_t num, int val);
int led_get(uint8_t num);
#define LOUDSPEAKER_PORT GPIOB
#define LOUDSPEAKER_RCC_MASK RCC_AHB1ENR_GPIOBEN
#define LOUDSPEAKER_PIN 1
void loudspeaker_setup();
void loudspeaker_set(int val);
int loudspeaker_get();
#endif /* __DIGIO_H__ */

View File

@ -0,0 +1,14 @@
#ifndef __HELPER_MACROS_H__
#define __HELPER_MACROS_H__
#include <stddef.h>
#define xstr(x) str(x)
#define str(x) #x
#define CONCATX(x,y) CONCAT(x,y)
#define CONCAT(x,y) x##y
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#endif /* __HELPER_MACROS_H__ */

View File

@ -5,19 +5,18 @@
#include <stm32f4xx.h> #include <stm32f4xx.h>
#include <stddef.h> #include <stddef.h>
struct dma_ring_buffer { struct dma_ring_buffer_to_mem {
char *data_ptr; void *data_ptr;
size_t buffer_count; size_t buffer_count;
DMA_Stream_TypeDef *dma; DMA_Stream_TypeDef *dma;
size_t get_idx; size_t get_idx;
uint8_t base_dma_id; uint8_t base_dma_id;
size_t element_size;
}; };
int dma_ring_buffer_initialize(struct dma_ring_buffer *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, char *data_buffer, void *src_reg, uint8_t dma_trigger_channel); int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size, void *data_buffer, void *src_reg, uint8_t dma_trigger_channel);
int dma_ring_buffer_get_data(struct dma_ring_buffer *buff, const char **data_buff, size_t *len); int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const void **data_buff, size_t *len);
void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff);
void dma_ring_buffer_stop(struct dma_ring_buffer *buff);
#endif /* __DMA_RING_BUFFER_H__ */ #endif /* __DMA_RING_BUFFER_H__ */

View File

@ -4,21 +4,24 @@
#ifndef UART_UART_H_ #ifndef UART_UART_H_
#define UART_UART_H_ #define UART_UART_H_
#define UART_RECEIVE_DMA_STREAM #define UART_RECEIVE_DMA_STREAM DMA2_Stream5
#define UART_SEND_DMA_STREAM #define UART_SEND_DMA_STREAM
#define UART_PORT_RCC_MASK RCC_AHB1ENR_GPIOAEN
#define UART_PORT GPIOA
#define UART_PERIPH USART1 #define UART_PERIPH USART1
#define UART_RCC_MASK RCC_APB2ENR_USART1EN #define UART_RCC_MASK RCC_APB2ENR_USART1EN
#ifdef DEBUGBUILD
#define UART_PORT GPIOA
#define UART_PORT_RCC_MASK RCC_AHB1ENR_GPIOAEN
#define UART_RX_PIN 10 #define UART_RX_PIN 10
#define UART_TX_PIN 9 #define UART_TX_PIN 9
#define UART_RX_PIN_ALTFUNC 7 #define UART_RX_PIN_ALTFUNC 7
#define UART_TX_PIN_ALTFUNC 7 #define UART_TX_PIN_ALTFUNC 7
#else
#endif
/* UART_DIV is 45.5625 => 115200 @ 84 MHz */ /* UART_DIV is 45.5625 => 115200 @ 84 MHz */
#define UART_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */ #define UART_DIV_FRACTION 9U /* Equals 9/16 = 0.5625 */

View File

@ -15,6 +15,7 @@
#include <clock-enable-manager.h> #include <clock-enable-manager.h>
#include <uart/uart.h> #include <uart/uart.h>
#include <shell.h> #include <shell.h>
#include <digio.h>
static void setup_nvic_priorities() static void setup_nvic_priorities()
{ {
@ -35,18 +36,16 @@ int main()
size_t uart_input_len; size_t uart_input_len;
shellmatta_handle_t shell_handle; shellmatta_handle_t shell_handle;
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_GPIOBEN));
__DSB();
GPIOB->MODER = OUTPUT(2) | OUTPUT(3);
GPIOB->ODR |= (1<<2);
setup_nvic_priorities(); setup_nvic_priorities();
systick_setup(); systick_setup();
//setup_dma(&adc_results, 3); //setup_dma(&adc_results, 3);
adc_pt1000_setup_meas(); adc_pt1000_setup_meas();
digio_setup_default_all();
led_setup();
loudspeaker_setup();
uart_init_with_dma(); uart_init_with_dma();
shell_handle = shell_init(); shell_handle = shell_init();

View File

@ -2,9 +2,9 @@
#include <uart/uart.h> #include <uart/uart.h>
#include <string.h> #include <string.h>
#include <adc-meas.h> #include <adc-meas.h>
#include <digio.h>
#define xstr(x) str(x) #include <stdlib.h>
#define str(x) #x #include <helper-macros/helper-macros.h>
#ifndef GIT_VER #ifndef GIT_VER
#define GIT_VER "VERSION NOT SET" #define GIT_VER "VERSION NOT SET"
@ -33,6 +33,59 @@ static shellmatta_retCode_t shell_cmd_ver(const shellmatta_handle_t handle,
return SHELLMATTA_OK; return SHELLMATTA_OK;
} }
static shellmatta_retCode_t shell_cmd_digio_get(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
(void)arguments;
(void)length;
shellmatta_printf(handle,
"DIGIO0 DIGIO1 DIGIO2 DIGIO3 LED0 LED1 LS\r\n"
"%d %d %d %d %d %d %d\r\n",
digio_get(0), digio_get(1), digio_get(2), digio_get(3),
led_get(0), led_get(1), loudspeaker_get());
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_digio_set(const shellmatta_handle_t handle,
const char *arguments,
uint32_t length)
{
int num = 100;
int state;
(void)length;
char buff[64];
char *curr_token;
strncpy(buff, arguments, sizeof(buff));
curr_token = strtok(buff, " ");
curr_token = strtok(NULL, " ");
if (!curr_token)
return SHELLMATTA_ERROR;
num = atoi(curr_token);
if (!curr_token)
return SHELLMATTA_ERROR;
curr_token = strtok(NULL, " ");
state = atoi(curr_token);
if (num < 4 && num >= 0)
digio_set(num, state);
else if (num >= 4 && num <= 5)
led_set(num - 4, state);
else if (num == 6)
loudspeaker_set(state);
else
return SHELLMATTA_ERROR;
return SHELLMATTA_OK;
}
static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t handle, static shellmatta_retCode_t shell_cmd_pt1000_res(const shellmatta_handle_t handle,
const char *arguments, const char *arguments,
uint32_t length) uint32_t length)
@ -81,7 +134,7 @@ static shellmatta_retCode_t shell_cmd_clear_error_status(const shellmatta_handle
// struct shellmatta_cmd *next; /**< pointer to next command or NULL */ // struct shellmatta_cmd *next; /**< pointer to next command or NULL */
//} shellmatta_cmd_t; //} shellmatta_cmd_t;
static shellmatta_cmd_t cmd[3] = { static shellmatta_cmd_t cmd[5] = {
{ {
.cmd = "version", .cmd = "version",
.cmdAlias = "ver", .cmdAlias = "ver",
@ -104,6 +157,22 @@ static shellmatta_cmd_t cmd[3] = {
.helpText = "Clear error status of PT1000 reading", .helpText = "Clear error status of PT1000 reading",
.usageText = NULL, .usageText = NULL,
.cmdFct = shell_cmd_clear_error_status, .cmdFct = shell_cmd_clear_error_status,
.next = &cmd[3],
},
{
.cmd = "digio-get",
.cmdAlias = "dg",
.helpText = "Read all digital input/output ports",
.usageText = NULL,
.cmdFct = shell_cmd_digio_get,
.next = &cmd[4],
},
{
.cmd = "digio-set",
.cmdAlias = "ds",
.helpText = "Set DIGIO Port",
.usageText = "digio-set <num> <state>",
.cmdFct = shell_cmd_digio_set,
.next = NULL, .next = NULL,
}, },
}; };

View File

@ -36,6 +36,6 @@ int _read(void) {
return 0; return 0;
} }
int _write(int fd, const void *buf, int count) { int _write(int fd, const void *buf, int count) {
sendString((char*)buf, count); uart_send_array((char*)buf, count);
return count; return count;
} }

View File

@ -1,33 +1,56 @@
#include <uart/dma-ring-buffer.h> #include <uart/dma-ring-buffer.h>
#include <clock-enable-manager.h> #include <clock-enable-manager.h>
#include <stdbool.h>
int dma_ring_buffer_initialize(struct dma_ring_buffer *dma_buffer, uint8_t base_dma_id, DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, char *data_buffer, void* src_reg, uint8_t dma_trigger_channel) static int dma_ring_buffer_switch_clock_enable(uint8_t base_dma, bool clk_en)
{ {
int ret_val;
int (*clk_func)(volatile uint32_t *, uint8_t);
if (clk_en)
clk_func = rcc_manager_enable_clock;
else
clk_func = rcc_manager_disable_clock;
switch (base_dma) {
case 1:
ret_val = clk_func(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA1EN));
break;
case 2:
ret_val = clk_func(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA2EN));
break;
default:
ret_val = -1000;
break;
}
return ret_val;
}
int dma_ring_buffer_periph_to_mem_initialize(struct dma_ring_buffer_to_mem *dma_buffer, uint8_t base_dma_id,
DMA_Stream_TypeDef *dma_stream, size_t buffer_element_count, size_t element_size,
void *data_buffer, void* src_reg, uint8_t dma_trigger_channel)
{
int ret_val = 0;
if (!dma_buffer || !dma_stream || !data_buffer || !src_reg) if (!dma_buffer || !dma_stream || !data_buffer || !src_reg)
return -1; return -1000;
if (dma_trigger_channel > 7) if (dma_trigger_channel > 7)
return -3; return -1007;
dma_buffer->base_dma_id = base_dma_id; dma_buffer->base_dma_id = base_dma_id;
switch (base_dma_id) { ret_val = dma_ring_buffer_switch_clock_enable(base_dma_id, true);
case 1: if (ret_val)
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA1EN)); return ret_val;
break;
case 2:
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA2EN));
break;
default:
return -2;
break;
}
dma_buffer->dma = dma_stream; dma_buffer->dma = dma_stream;
dma_buffer->get_idx = 0; dma_buffer->get_idx = 0;
dma_buffer->buffer_count = buffer_element_count; dma_buffer->buffer_count = buffer_element_count;
dma_buffer->data_ptr = data_buffer; dma_buffer->data_ptr = data_buffer;
dma_buffer->element_size = element_size;
dma_stream->PAR = (uint32_t)src_reg; dma_stream->PAR = (uint32_t)src_reg;
dma_stream->M0AR = (uint32_t)data_buffer; dma_stream->M0AR = (uint32_t)data_buffer;
@ -39,7 +62,7 @@ int dma_ring_buffer_initialize(struct dma_ring_buffer *dma_buffer, uint8_t base_
return 0; return 0;
} }
int dma_ring_buffer_get_data(struct dma_ring_buffer *buff, const char **data_buff, size_t *len) int dma_ring_buffer_periph_to_mem_get_data(struct dma_ring_buffer_to_mem *buff, const void **data_buff, size_t *len)
{ {
int ret_code = 0; int ret_code = 0;
uint32_t ndtr; uint32_t ndtr;
@ -54,12 +77,12 @@ int dma_ring_buffer_get_data(struct dma_ring_buffer *buff, const char **data_buf
/* Check if wrap around */ /* Check if wrap around */
if (put_idx < buff->get_idx) { if (put_idx < buff->get_idx) {
*data_buff = &(buff->data_ptr[buff->get_idx]); *data_buff = &(((char *)buff->data_ptr)[buff->get_idx * buff->element_size]);
*len = buff->buffer_count - buff->get_idx; *len = buff->buffer_count - buff->get_idx;
buff->get_idx = 0; buff->get_idx = 0;
ret_code = 1; ret_code = 1;
} else if (put_idx > buff->get_idx) { } else if (put_idx > buff->get_idx) {
*data_buff = &(buff->data_ptr[buff->get_idx]); *data_buff = &(((char *)buff->data_ptr)[buff->get_idx * buff->element_size]);
*len = put_idx - buff->get_idx; *len = put_idx - buff->get_idx;
buff->get_idx += *len; buff->get_idx += *len;
} else { } else {
@ -70,7 +93,7 @@ int dma_ring_buffer_get_data(struct dma_ring_buffer *buff, const char **data_buf
return ret_code; return ret_code;
} }
void dma_ring_buffer_stop(struct dma_ring_buffer *buff) void dma_ring_buffer_periph_to_mem_stop(struct dma_ring_buffer_to_mem *buff)
{ {
if (!buff || !buff->dma) if (!buff || !buff->dma)
return; return;
@ -80,14 +103,5 @@ void dma_ring_buffer_stop(struct dma_ring_buffer *buff)
buff->dma->M1AR = 0; buff->dma->M1AR = 0;
buff->dma->FCR = 0; buff->dma->FCR = 0;
switch (buff->base_dma_id) { dma_ring_buffer_switch_clock_enable(buff->base_dma_id, false);
case 1:
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA1EN));
break;
case 2:
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(RCC_AHB1ENR_DMA2EN));
break;
default:
break;
}
} }

View File

@ -14,42 +14,11 @@
#include <stm32-gpio-macros.h> #include <stm32-gpio-macros.h>
#include <uart/dma-ring-buffer.h> #include <uart/dma-ring-buffer.h>
static struct dma_ring_buffer ring_buff; static struct dma_ring_buffer_to_mem ring_buff_rx;
static char uart_rx_buffer[64]; static char uart_rx_buffer[64];
void initUART() {
__DSB();
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
__DSB();
GPIOA->MODER |= (1<<5);
GPIOA->AFR[0] |= (7<<8); //Enable Clock
GPIOA->MODER |= (1<<5);
GPIOA->AFR[0] |= (7<<8);
asm("nop");
asm("nop");
asm("nop");
USART2->BRR = 0x1117; //Baudrate 273.4375=>0x1117 9600baud bei 42MHz Periph
USART2->CR1 = USART_CR1_UE | USART_CR1_TE;
}
void sendChar(char c) {
while(!(USART2->SR & USART_SR_TXE));
USART2->DR = c;
}
void sendString(char* s, int count) {
int i = 0;
for (i = 0; i < count; i++,s++)
{
if (!(*s))
break;
sendChar(*s);
}
}
#ifdef DEBUGBUILD
static inline void uart_gpio_config() static inline void uart_gpio_config()
{ {
rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK)); rcc_manager_enable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK));
@ -58,17 +27,20 @@ static inline void uart_gpio_config()
SETAF(UART_PORT, UART_RX_PIN, UART_RX_PIN_ALTFUNC); SETAF(UART_PORT, UART_RX_PIN, UART_RX_PIN_ALTFUNC);
SETAF(UART_PORT, UART_TX_PIN, UART_TX_PIN_ALTFUNC); SETAF(UART_PORT, UART_TX_PIN, UART_TX_PIN_ALTFUNC);
} }
#endif
void uart_init_with_dma() void uart_init_with_dma()
{ {
rcc_manager_enable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK)); rcc_manager_enable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK));
#ifdef DEBUGBUILD
uart_gpio_config(); uart_gpio_config();
#endif
UART_PERIPH->BRR = UART_BRR_REG_VALUE; UART_PERIPH->BRR = UART_BRR_REG_VALUE;
UART_PERIPH->CR3 = USART_CR3_DMAR | USART_CR3_DMAT; UART_PERIPH->CR3 = USART_CR3_DMAR | USART_CR3_DMAT;
UART_PERIPH->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; UART_PERIPH->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
dma_ring_buffer_initialize(&ring_buff, 2, DMA2_Stream5, sizeof(uart_rx_buffer), uart_rx_buffer, (char *)&UART_PERIPH->DR, 4); dma_ring_buffer_periph_to_mem_initialize(&ring_buff_rx, 2, UART_RECEIVE_DMA_STREAM, sizeof(uart_rx_buffer), 1U,
uart_rx_buffer, (char *)&UART_PERIPH->DR, 4);
} }
void uart_disable() void uart_disable()
@ -76,7 +48,7 @@ void uart_disable()
UART_PERIPH->CR1 = 0; UART_PERIPH->CR1 = 0;
UART_PERIPH->CR2 = 0; UART_PERIPH->CR2 = 0;
UART_PERIPH->CR3 = 0; UART_PERIPH->CR3 = 0;
dma_ring_buffer_stop(&ring_buff); dma_ring_buffer_periph_to_mem_stop(&ring_buff_rx);
rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK)); rcc_manager_disable_clock(&RCC->AHB1ENR, BITMASK_TO_BITNO(UART_PORT_RCC_MASK));
rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK)); rcc_manager_disable_clock(&RCC->APB2ENR, BITMASK_TO_BITNO(UART_RCC_MASK));
@ -110,5 +82,5 @@ void uart_send_string_with_dma(char *string);
int uart_receive_data_with_dma(const char **data, size_t *len) int uart_receive_data_with_dma(const char **data, size_t *len)
{ {
return dma_ring_buffer_get_data(&ring_buff, data, len); return dma_ring_buffer_periph_to_mem_get_data(&ring_buff_rx, (const void **)data, len);
} }