Split into modules, add error handling
This commit is contained in:
parent
23be368b68
commit
e7532b40a7
14 changed files with 946 additions and 790 deletions
19
include/adc.h
Normal file
19
include/adc.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef __ADC_H__
|
||||
#define __ADC_H__
|
||||
|
||||
#include <global.h>
|
||||
|
||||
struct adc_state {
|
||||
uint8_t txbuf[12];
|
||||
uint8_t rxbuf[12];
|
||||
uint32_t dma_ccr3;
|
||||
int32_t data[2];
|
||||
bool has_adc;
|
||||
ErrorCode error;
|
||||
};
|
||||
|
||||
extern struct adc_state st_adc;
|
||||
|
||||
void adc_init(void);
|
||||
|
||||
#endif /* __ADC_H__ */
|
||||
94
include/adc_interface.h
Normal file
94
include/adc_interface.h
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#ifndef __ADC_INTERFACE_H__
|
||||
#define __ADC_INTERFACE_H__
|
||||
|
||||
enum adc_reg_addr {
|
||||
ADC_REG_ID = 0x00,
|
||||
ADC_REG_STATUS = 0x01,
|
||||
ADC_REG_MODE = 0x02,
|
||||
ADC_REG_CLOCK = 0x03,
|
||||
ADC_REG_GAIN = 0x04,
|
||||
ADC_REG_CFG = 0x06,
|
||||
ADC_REG_THRSHLD_MSB = 0x07,
|
||||
ADC_REG_THRSHLD_LSB = 0x08,
|
||||
ADC_REG_CH0_CFG = 0x09,
|
||||
ADC_REG_CH0_OCAL_MSB = 0x0a,
|
||||
ADC_REG_CH0_OCAL_LSB = 0x0b,
|
||||
ADC_REG_CH0_GCAL_MSB = 0x0c,
|
||||
ADC_REG_CH0_GCAL_LSB = 0x0d,
|
||||
ADC_REG_CH1_CFG = 0x0e,
|
||||
ADC_REG_CH1_OCAL_MSB = 0x0f,
|
||||
ADC_REG_CH1_OCAL_LSB = 0x10,
|
||||
ADC_REG_CH1_GCAL_MSB = 0x11,
|
||||
ADC_REG_CH1_GCAL_LSB = 0x12,
|
||||
ADC_REG_REGMAP_CRC = 0x3e,
|
||||
};
|
||||
|
||||
#define ADC_STATUS_LOCK (1<<15)
|
||||
#define ADC_STATUS_F_RESYNC (1<<14)
|
||||
#define ADC_STATUS_REG_MAP (1<<13)
|
||||
#define ADC_STATUS_CRC_ERR (1<<12)
|
||||
#define ADC_STATUS_CRC_TYPE (1<<11)
|
||||
#define ADC_STATUS_RESET (1<<10)
|
||||
#define ADC_STATUS_WLENGTH_Pos 8
|
||||
#define ADC_STATUS_DRDY1 (1<<1)
|
||||
#define ADC_STATUS_DRDY0 (1<<0)
|
||||
|
||||
#define ADC_MODE_REG_CRC_EN (1<<13)
|
||||
#define ADC_MODE_RX_CRC_EN (1<<12)
|
||||
#define ADC_MODE_CRC_TYPE (1<<11)
|
||||
#define ADC_MODE_RESET (1<<10)
|
||||
#define ADC_MODE_WLENGTH_Pos 8
|
||||
#define ADC_MODE_TIMEOUT (1<<4)
|
||||
#define ADC_MODE_DRDY_SEL_Pos 2
|
||||
#define ADC_MODE_DRDY_HiZ (1<<1)
|
||||
#define ADC_MODE_DRDY_FMT (1<<0)
|
||||
|
||||
#define ADC_CLOCK_CH1_EN (1<<9)
|
||||
#define ADC_CLOCK_CH2_EN (1<<8)
|
||||
#define ADC_CLOCK_TBM (1<<5)
|
||||
#define ADC_CLOCK_OSR_Pos 2
|
||||
#define ADC_CLOCK_PWR_Pos 0
|
||||
#define ADC_CLOCK_OSR_64 ADC_CLOCK_TBM
|
||||
#define ADC_CLOCK_OSR_128 (0<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_256 (1<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_512 (2<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_1024 (3<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_2048 (4<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_4096 (5<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_8192 (6<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_OSR_16384 (7<<ADC_CLOCK_OSR_Pos)
|
||||
#define ADC_CLOCK_PWR_VERY_LOW (0<<ADC_CLOCK_PWR_Pos)
|
||||
#define ADC_CLOCK_PWR_LOW (1<<ADC_CLOCK_PWR_Pos)
|
||||
#define ADC_CLOCK_PWR_HIRES (2<<ADC_CLOCK_PWR_Pos)
|
||||
|
||||
#define ADC_GAIN1_PGAGAIN1_1 (0<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_2 (1<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_4 (2<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_8 (3<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_16 (4<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_32 (5<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_64 (5<<4)
|
||||
#define ADC_GAIN1_PGAGAIN1_128 (7<<4)
|
||||
#define ADC_GAIN1_PGAGAIN0_1 (0<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_2 (1<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_4 (2<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_8 (3<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_16 (4<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_32 (5<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_64 (5<<0)
|
||||
#define ADC_GAIN1_PGAGAIN0_128 (7<<0)
|
||||
|
||||
#define ADC_CFG_GC_DLY_Pos 9
|
||||
#define ADC_CFG_GC_EN (1<<8)
|
||||
#define ADC_CFG_CD_ALLCH (1<<7)
|
||||
#define ADC_CFG_CD_NUM_Pos 4
|
||||
#define ADC_CFG_CD_LEN_Pos 1
|
||||
#define ADC_CFG_CD_EN (1<<0)
|
||||
|
||||
#define ADC_CHn_CFG_PHASE_Pos 6
|
||||
#define ADC_CHn_CFG_DCBLK_DIS (1<<2)
|
||||
#define ADC_CHn_CFG_MUX_Pos 0
|
||||
|
||||
|
||||
|
||||
#endif /* __ADC_INTERFACE_H__ */
|
||||
|
|
@ -17,4 +17,16 @@
|
|||
#define APB1_PRESC (1<<(APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1_Msk) >> RCC_CFGR_PPRE1_Pos]))
|
||||
#define AHB_PRESC (1<<(AHBPrescTable[(RCC->CFGR & RCC_CFGR_HPRE_Msk) >> RCC_CFGR_HPRE_Pos]))
|
||||
|
||||
enum ErrorCode {
|
||||
ERR_SUCCESS = 0,
|
||||
ERR_TIMEOUT,
|
||||
ERR_PHYSICAL_LAYER,
|
||||
ERR_PROTOCOL,
|
||||
ERR_DMA,
|
||||
};
|
||||
typedef enum ErrorCode ErrorCode;
|
||||
|
||||
|
||||
void delay_us(int duration_us);
|
||||
|
||||
#endif /* __GLOBAL_H__ */
|
||||
|
|
|
|||
21
include/lcd.h
Normal file
21
include/lcd.h
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef __LCD_H__
|
||||
#define __LCD_H__
|
||||
|
||||
void lcd_init(void);
|
||||
void lcd_write_dma(const char **buf);
|
||||
|
||||
#ifndef LCD_PEX_ADDR
|
||||
#define LCD_PEX_ADDR 0x4E
|
||||
#endif
|
||||
|
||||
struct lcd_state {
|
||||
uint8_t buf[512];
|
||||
uint32_t cr2_buf;
|
||||
bool led;
|
||||
bool has_lcd;
|
||||
ErrorCode error;
|
||||
};
|
||||
|
||||
extern struct lcd_state st_lcd;
|
||||
|
||||
#endif /* __LCD_H__ */
|
||||
60
include/lcd_interface.h
Normal file
60
include/lcd_interface.h
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#ifndef __LCD_INTERFACE_H__
|
||||
#define __LCD_INTERFACE_H__
|
||||
|
||||
/* https://www.lcd-module.de/eng/pdf/zubehoer/ks0066.pdf */
|
||||
enum LCD_Command {
|
||||
LCD_CMD_CLEAR = 0x01,
|
||||
LCD_CMD_HOME = 0x02,
|
||||
LCD_CMD_ENTRY_MODE = 0x04,
|
||||
LCD_CMD_ON_OFF = 0x08,
|
||||
LCD_CMD_SHIFT = 0x10,
|
||||
LCD_CMD_FUNCTION = 0x20,
|
||||
LCD_CMD_CGRAM_ADDR = 0x40,
|
||||
LCD_CMD_DDRAM_ADDR = 0x80,
|
||||
};
|
||||
|
||||
enum LCD_ENTRY_MODE_Cmd {
|
||||
LCD_ENTRY_CUR_RIGHT = 0x02,
|
||||
LCD_ENTRY_CUR_LEFT = 0x00,
|
||||
LCD_ENTRY_DSP_RIGHT = 0x03,
|
||||
LCD_ENTRY_DSP_LEFT = 0x01,
|
||||
};
|
||||
|
||||
enum LCD_ON_OFF_Cmd {
|
||||
LCD_DSP_ON = 0x04,
|
||||
LCD_DSP_OFF = 0x00,
|
||||
LCD_CUR_ON = 0x02,
|
||||
LCD_CUR_OFF = 0x00,
|
||||
LCD_BLK_ON = 0x01,
|
||||
LCD_BLK_OFF = 0x00,
|
||||
};
|
||||
|
||||
enum LCD_SHIFT_Cmd {
|
||||
LCD_SH_CUR_LEFT = 0x00,
|
||||
LCD_SH_CUR_RIGHT = 0x04,
|
||||
LCD_SH_DSP_LEFT = 0x08,
|
||||
LCD_SH_DSP_RIGHT = 0x0C,
|
||||
};
|
||||
|
||||
enum LCD_FUNCTION_Cmd {
|
||||
LCD_FN_4BIT = 0x00,
|
||||
LCD_FN_8BIT = 0x10,
|
||||
LCD_FN_1LINE = 0x00,
|
||||
LCD_FN_2LINE = 0x08,
|
||||
LCD_FN_5X8 = 0x00,
|
||||
LCD_FN_5X11 = 0x04,
|
||||
};
|
||||
|
||||
#define LCD_ENTER_8BIT 0x3
|
||||
#define LCD_ENTER_4BIT 0x2
|
||||
|
||||
enum LCD_Pins {
|
||||
B_LCD_RS = (1<<0),
|
||||
B_LCD_RW = (1<<1),
|
||||
B_LCD_E = (1<<2),
|
||||
B_LCD_LED = (1<<3),
|
||||
};
|
||||
|
||||
#define LCD_D_Pos 4
|
||||
|
||||
#endif /* __LCD_INTERFACE_H__ */
|
||||
24
include/led.h
Normal file
24
include/led.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef __LED_H__
|
||||
#define __LED_H__
|
||||
|
||||
#include <global.h>
|
||||
|
||||
struct apa102_pkt {
|
||||
uint8_t global;
|
||||
uint8_t b; /* each channel 8 bit 0-255 */
|
||||
uint8_t g;
|
||||
uint8_t r;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct led_state {
|
||||
uint32_t _zeros;
|
||||
struct apa102_pkt led[10];
|
||||
uint32_t _ones;
|
||||
} __attribute__((packed));
|
||||
|
||||
extern struct led_state st_led;
|
||||
|
||||
void led_init(void);
|
||||
void led_set_global_brightness(int brightness);
|
||||
|
||||
#endif /* __LED_H__ */
|
||||
2
include/stm32.h
Normal file
2
include/stm32.h
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* libusb_stm compatibility file */
|
||||
#include "stm32f072xb.h"
|
||||
8
include/usb_if.h
Normal file
8
include/usb_if.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#ifndef __USB_IF_H__
|
||||
#define __USB_IF_H__
|
||||
|
||||
#include <global.h>
|
||||
|
||||
void usb_init(void);
|
||||
|
||||
#endif /* __USB_IF_H__ */
|
||||
Loading…
Add table
Add a link
Reference in a new issue