From a45423299cc24a6238cd3b57e30c09988cf04e7e Mon Sep 17 00:00:00 2001 From: Jan Michel Date: Mon, 6 Jan 2014 18:56:39 +0100 Subject: [PATCH] added DAC control & its registers --- firmware/src/CB_functions.c | 32 +++++++++++++++++- firmware/src/Makefile | 1 + firmware/src/dac.c | 67 +++++++++++++++++++++++++++++++++++++ firmware/src/dac.h | 15 +++++++++ firmware/src/main.c | 2 ++ firmware/src/misc_utils.c | 14 ++++++++ firmware/src/misc_utils.h | 6 +++- 7 files changed, 135 insertions(+), 2 deletions(-) create mode 100644 firmware/src/dac.c create mode 100644 firmware/src/dac.h diff --git a/firmware/src/CB_functions.c b/firmware/src/CB_functions.c index 55958bd..e563b35 100644 --- a/firmware/src/CB_functions.c +++ b/firmware/src/CB_functions.c @@ -9,12 +9,25 @@ #include "CB_functions.h" #include "periph_conf.h" #include "stm32f10x_conf.h" +#include "misc_utils.h" uint16_t uC_regs[UC_NO_REGS]; - +void uart_word_to_dac(uint16_t word){ + while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); + CB_GPIO_Out_Lo(DAC_CS); + + USART_SendData(USART3, reverse_8bit(0x30 | ((word>>12)&0xF))); + while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); + USART_SendData(USART3, reverse_8bit((word>>4)&0xFF)); + while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); + USART_SendData(USART3, reverse_8bit((word<<4)&0xFF)); + delay_ms(1); + while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); + CB_GPIO_Out_Hi(DAC_CS); +} void uart_byte_to_fpga(uint8_t byte){ while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); @@ -61,6 +74,13 @@ void decode_register(uint8_t addr) { CB_GPIO_Out(SENSOREN0,thisRegister & 1<<1); CB_GPIO_Out(JTAGEN0,thisRegister & 1<<0); break; + case 0x2: //DAC CurLimA 0 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (0<<12)); + case 0x3: //DAC CurLimD 0 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (1<<12)); + case 0x4: //DAC VClp 0 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (2<<12)); + case 0x6: // Switches1 CB_GPIO_Out(ENAA1,thisRegister & 1<<5); CB_GPIO_Out(DISA1,thisRegister & 1<<4); @@ -69,6 +89,16 @@ void decode_register(uint8_t addr) { CB_GPIO_Out(SENSOREN1,thisRegister & 1<<1); CB_GPIO_Out(JTAGEN1,thisRegister & 1<<0); break; + case 0x7: //DAC CurLimA 1 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (4<<12)); + case 0x8: //DAC CurLimD 1 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (5<<12)); + case 0x9: //DAC VClp 1 + uart_word_to_dac(((thisRegister&0xFFFF)>>4) | (6<<12)); + + case 0xF: //SPI Debug + uart_word_to_dac( (uint16_t) (thisRegister&0xFFFF)); + break; case 0x11: // LEDs if(thisRegister & 1<<7) { diff --git a/firmware/src/Makefile b/firmware/src/Makefile index 5cf462e..769a8a6 100644 --- a/firmware/src/Makefile +++ b/firmware/src/Makefile @@ -9,6 +9,7 @@ OBJS+=CB_functions.o OBJS+=usart1.o OBJS+=spi1.o OBJS+=spi2.o +OBJS+=dac.o OBJS+=misc_utils.o # OBJS+=keypins.o diff --git a/firmware/src/dac.c b/firmware/src/dac.c new file mode 100644 index 0000000..ea5e5e6 --- /dev/null +++ b/firmware/src/dac.c @@ -0,0 +1,67 @@ +/** + ******************************************************** + * + * USART3 (DAC) + * + ******************************************************** + */ + +#include "dac.h" + + +void init_USART3() { + GPIO_InitTypeDef GPIO_InitStructure; + USART_InitTypeDef USART_InitStructure; + USART_ClockInitTypeDef USART_ClkInitStructure; + + /* Clock configuration -------------------------------------------------------*/ + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); + RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); + + /* Configure the GPIO ports( USART3 Transmit and Clock Lines) */ + /* Configure as Alternate function remapped Push-Pull */ + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); + GPIO_PinRemapConfig(GPIO_PartialRemap_USART3,ENABLE); + + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_Init(GPIOC, &GPIO_InitStructure); + + + /* USART1 configuration ------------------------------------------------------*/ + /* USART1 configured as follow: + - BaudRate = 115200 baud + - Word Length = 8 Bits + - One Stop Bit + - No parity + - Hardware flow control disabled (RTS and CTS signals) + - Receive and transmit enabled + */ + USART_InitStructure.USART_BaudRate = 1000000; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_Parity = USART_Parity_No; + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; + USART_InitStructure.USART_Mode = USART_Mode_Tx; + + /* Configure the USART3 */ + USART_Init(USART3, &USART_InitStructure); + + USART_ClkInitStructure.USART_Clock= USART_Clock_Enable; + USART_ClkInitStructure.USART_CPOL = USART_CPOL_Low; + USART_ClkInitStructure.USART_CPHA = USART_CPHA_1Edge; + USART_ClkInitStructure.USART_LastBit=USART_LastBit_Enable; + USART_ClockInit(USART3, &USART_ClkInitStructure); + + /* Enable USART3 interrupt */ + //USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); + //USART_ITConfig(USART1,USART_IT_TXE,ENABLE); + + + /* Enable the USART3 */ + USART_Cmd(USART3, ENABLE); + } \ No newline at end of file diff --git a/firmware/src/dac.h b/firmware/src/dac.h new file mode 100644 index 0000000..3c49a29 --- /dev/null +++ b/firmware/src/dac.h @@ -0,0 +1,15 @@ +/** + ******************************************************** + * + * DAC SPI (headers) + * + ******************************************************** + */ + + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f10x.h" +#include "stm32f10x_conf.h" + + +void init_USART3(void); \ No newline at end of file diff --git a/firmware/src/main.c b/firmware/src/main.c index dc52a17..2babe91 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -16,6 +16,7 @@ #include "CB_functions.h" #include "usart1.h" #include "spi2.h" +#include "dac.h" #include "periph_conf.h" #include "misc_utils.h" @@ -67,6 +68,7 @@ int main(int argc, char *argv[]) { init_USART1(); + init_USART3(); delay_ms(1000); CB_GPIO_Out_Hi(LED4); diff --git a/firmware/src/misc_utils.c b/firmware/src/misc_utils.c index 71c5d6b..e1d84c2 100644 --- a/firmware/src/misc_utils.c +++ b/firmware/src/misc_utils.c @@ -54,4 +54,18 @@ uint8_t BufferOut(uint8_t *pByte) { *pByte = buffer.data[buffer.read]; buffer.read = (buffer.read + 1) & BUFFER_MASK; return SUCCESS; +} + + + + +uint8_t reverse_8bit(uint8_t value) { + uint32_t t = __RBIT(value); + return t >> 24; +} + + +uint16_t reverse_16bit(uint16_t value) { + uint32_t t = __RBIT(value); + return t >> 16; } \ No newline at end of file diff --git a/firmware/src/misc_utils.h b/firmware/src/misc_utils.h index c3ffeab..9172eb9 100644 --- a/firmware/src/misc_utils.h +++ b/firmware/src/misc_utils.h @@ -28,4 +28,8 @@ void SysTick_Handler(void); void delay_ms(uint16_t time_ms); uint8_t BufferIn(uint8_t byte); -uint8_t BufferOut(uint8_t *pByte); \ No newline at end of file +uint8_t BufferOut(uint8_t *pByte); + +//Reverse value +uint8_t reverse_8bit(uint8_t value); +uint16_t reverse_16bit(uint16_t value); \ No newline at end of file -- 2.43.0