Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
libisp.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/mman.h>
9 #include <sys/ioctl.h>
10 
11 #include "libisp.h"
12 
13 #define AVI_BASE 0x400000
14 #define AVI_SIZE 0x100000
15 #define AVI_MASK (AVI_SIZE - 1)
16 
23 };
24 
25 /* IOCTL implemented in AVI drivers */
26 #define AVI_ISP_IOGET_OFFSETS _IOR('F', 0x33, struct avi_isp_offsets)
27 
28 /* Raw accesses */
29 #define readl(_addr) (*((volatile uint32_t *)(_addr)))
30 #define writel(_val, _addr) (*((volatile uint32_t *)(_addr)) = _val)
31 
32 /* ISP context */
33 static struct libisp_context isp_ctx = {
34  .devmem = -1
35 };
36 
37 static const unsigned isp_bases[] = {
42  AVI_ISP_GREEN_IMBALANCE + AVI_ISP_GREEN_IMBALANCE_GREEN_RED_COEFF_MEM,
43  AVI_ISP_GREEN_IMBALANCE + AVI_ISP_GREEN_IMBALANCE_GREEN_BLUE_COEFF_MEM,
49  AVI_ISP_LENS_SHADING_CORRECTION + AVI_ISP_LENS_SHADING_CORRECTION_RED_COEFF_MEM,
50  AVI_ISP_LENS_SHADING_CORRECTION + AVI_ISP_LENS_SHADING_CORRECTION_GREEN_COEFF_MEM,
51  AVI_ISP_LENS_SHADING_CORRECTION + AVI_ISP_LENS_SHADING_CORRECTION_BLUE_COEFF_MEM,
56  0, /* GAMMA conf */
60  0, /* CHROMA */
61  0, /* STATS YUV*/
70 };
71 
76 static int avi_isp_get_offsets_fd(int fd, struct avi_isp_offsets *off)
77 {
78  if (ioctl(fd, AVI_ISP_IOGET_OFFSETS, off) < 0) {
79  printf("sizeof: %d, %X\n", sizeof(struct avi_isp_offsets), AVI_ISP_IOGET_OFFSETS);
80  perror("ioctl(AVI_ISP_IOGET_OFFSETS) failed");
81  return -1;
82  }
83 
84  return 0;
85 }
86 
91 static int open_isp_fd(struct libisp_context *ctx, int fd)
92 {
93  struct avi_isp_offsets off;
94  int i;
95 
96  ctx->devmem = open("/dev/mem", O_RDWR);
97 
98  if (ctx->devmem < 0) {
99  perror("Can't open /dev/mem");
100  goto open_failed;
101  }
102 
103  ctx->avi_base = (unsigned long) mmap(NULL, AVI_SIZE,
104  PROT_READ | PROT_WRITE,
105  MAP_SHARED, ctx->devmem, AVI_BASE & ~AVI_MASK);
106 
107  if (ctx->avi_base == (unsigned long) MAP_FAILED) {
108  perror("mmap failed");
109  goto mmap_failed;
110  }
111 
112  if (avi_isp_get_offsets_fd(fd, &off) < 0) {
113  goto get_offsets_failed;
114  }
115 
116  /* Compute all the sub-modules offsets */
117  /* Chain Bayer */
118  for (i = chain_bayer_inter ; i < gamma_corrector ; i++) {
119  ctx->offsets[i] = ctx->avi_base + isp_bases[i] + off.chain_bayer;
120  }
121 
122  ctx->offsets[gamma_corrector] = ctx->avi_base + isp_bases[i++] + off.gamma_corrector;
123  ctx->offsets[gamma_corrector_ry_lut] = ctx->avi_base + isp_bases[i++] + off.gamma_corrector;
124  ctx->offsets[gamma_corrector_gu_lut] = ctx->avi_base + isp_bases[i++] + off.gamma_corrector;
125  ctx->offsets[gamma_corrector_bv_lut] = ctx->avi_base + isp_bases[i++] + off.gamma_corrector;
126  ctx->offsets[chroma] = ctx->avi_base + isp_bases[i++] + off.chroma;
127  ctx->offsets[statistics_yuv] = ctx->avi_base + isp_bases[i++] + off.statistics_yuv;
128  ctx->offsets[statistics_yuv_ae_histogram_y] = ctx->avi_base + isp_bases[i++] + off.statistics_yuv;
129 
130  /* Chain YUV */
131  for (i = chain_yuv_inter ; i < ISP_NODE_NR ; i++) {
132  ctx->offsets[i] = ctx->avi_base + isp_bases[i] + off.chain_yuv;
133  }
134 
135  return 0;
136 
137 get_offsets_failed:
138  munmap((void *) ctx->avi_base, AVI_SIZE);
139 
140 mmap_failed:
141  close(ctx->devmem);
142  ctx->devmem = -1;
143 
144 open_failed:
145  return -1;
146 }
147 
148 /*static int close_isp(struct libisp_context *ctx)
149 {
150  int ret = 0;
151 
152  if (munmap((void *) ctx->avi_base, AVI_SIZE) == -1) {
153  perror("munmap failed");
154  ret = -1;
155  }
156 
157  close(ctx->devmem);
158  ctx->devmem = -1;
159 
160  return ret;
161 }*/
162 
164 {
165  if (open_isp_fd(&isp_ctx, dev->fd) < 0) {
166  return -1;
167  }
168 
169  avi_isp_vlformat_32to40_set_registers(&isp_ctx, &isp_config.vlformat_32to40);
170  avi_isp_chain_bayer_inter_set_registers(&isp_ctx, &isp_config.bayer_inter);
171  avi_isp_pedestal_set_registers(&isp_ctx, &isp_config.pedestal);
172  avi_isp_green_imbalance_set_registers(&isp_ctx, &isp_config.green_imbalance);
173  avi_isp_green_imbalance_green_red_coeff_mem_set_registers(&isp_ctx, &isp_config.grim_gr);
174  avi_isp_green_imbalance_green_blue_coeff_mem_set_registers(&isp_ctx, &isp_config.grim_gb);
175  avi_isp_dead_pixel_correction_set_registers(&isp_ctx, &isp_config.dead_pixel_correction);
176  avi_isp_denoising_set_registers(&isp_ctx, &isp_config.denoising);
177  avi_isp_statistics_bayer_set_registers(&isp_ctx, &isp_config.statistics_bayer);
178  avi_isp_lens_shading_correction_set_registers(&isp_ctx, &isp_config.lens_shading_correction);
179  avi_isp_lens_shading_correction_red_coeff_mem_set_registers(&isp_ctx, &isp_config.lsc_red_coeffs);
180  avi_isp_lens_shading_correction_green_coeff_mem_set_registers(&isp_ctx, &isp_config.lsc_green_coeffs);
181  avi_isp_lens_shading_correction_blue_coeff_mem_set_registers(&isp_ctx, &isp_config.lsc_blue_coeffs);
182  avi_isp_bayer_set_registers(&isp_ctx, &isp_config.bayer);
183  avi_isp_color_correction_set_registers(&isp_ctx, &isp_config.color_correction);
184  avi_isp_vlformat_40to32_set_registers(&isp_ctx, &isp_config.vlformat_40to32);
185  avi_isp_gamma_corrector_set_registers(&isp_ctx, &isp_config.gamma_corrector);
186  avi_isp_gamma_corrector_ry_lut_set_registers(&isp_ctx, &isp_config.gc_ry_lut);
187  avi_isp_gamma_corrector_gu_lut_set_registers(&isp_ctx, &isp_config.gc_gu_lut);
188  avi_isp_gamma_corrector_bv_lut_set_registers(&isp_ctx, &isp_config.gc_bv_lut);
189  avi_isp_chroma_set_registers(&isp_ctx, &isp_config.chroma);
190  avi_isp_statistics_yuv_set_registers(&isp_ctx, &isp_config.statistics_yuv);
191  avi_isp_edge_enhancement_color_reduction_filter_set_registers(&isp_ctx, &isp_config.eecrf);
192  avi_isp_edge_enhancement_color_reduction_filter_ee_lut_set_registers(&isp_ctx, &isp_config.eecrf_lut);
193  avi_isp_chain_yuv_inter_get_registers(&isp_ctx, &isp_config.chain_yuv_inter);
194 
195  //close_isp(&isp_ctx);
196 
197  return 0;
198 }
199 
200 /* Get YUV statistics */
202 {
203  uint16_t i;
204 
205  if (isp_ctx.devmem < 0) {
206  return -1;
207  }
208 
209  struct avi_isp_statistics_yuv_regs stats_yuv;
210  avi_isp_statistics_yuv_get_registers(&isp_ctx, &stats_yuv);
211 
212  yuv_stats->awb_sum_Y = stats_yuv.awb_sum_y.awb_sum_y;
213  yuv_stats->awb_sum_U = stats_yuv.awb_sum_u.awb_sum_u;
214  yuv_stats->awb_sum_V = stats_yuv.awb_sum_v.awb_sum_v;
215  yuv_stats->awb_nb_grey_pixels = stats_yuv.awb_nb_grey_pixels.nb_grey_pixels;
216  yuv_stats->nb_valid_Y = stats_yuv.ae_nb_valid_y.nb_valid_y;
217 
218  // Histogram
220  avi_isp_statistics_yuv_ae_histogram_y_get_registers(&isp_ctx, &histogram);
221 
222  for (i = 0; i < 256; ++i) {
223  yuv_stats->ae_histogram_Y[i] = histogram.ae_histogram_y[i].histogram_y;
224  }
225 
226  avi_isp_statistics_yuv_set_registers(&isp_ctx, &isp_config.statistics_yuv);
227  return 0;
228 }
229 
230 static inline void memcpy_to_registers(unsigned long addr,
231  const void *reg_base,
232  size_t s)
233 {
234  const uint32_t *reg = reg_base;
235  unsigned i;
236 
237  s /= sizeof(uint32_t); /* we write one register at a time */
238 
239  for (i = 0; i < s; i++) {
240  writel(reg[i], addr + i * sizeof(uint32_t));
241  }
242 }
243 
244 static inline void memcpy_from_registers(void *reg_base,
245  unsigned long addr,
246  size_t s)
247 {
248  uint32_t *reg = reg_base;
249  unsigned i;
250 
251  s /= sizeof(uint32_t); /* we read one register at a time */
252 
253  for (i = 0; i < s; i++) {
254  reg[i] = readl(addr + i * sizeof(uint32_t));
255  }
256 }
257 
258 #define EXPAND_AS_FUNCTION(_node) \
259  void avi_isp_ ## _node ## _set_registers(struct libisp_context *c, \
260  struct avi_isp_ ## _node ## _regs const *regs) \
261  { \
262  memcpy_to_registers(c->offsets[_node], regs, sizeof(*regs)); \
263  } \
264  \
265  void avi_isp_ ## _node ## _get_registers(struct libisp_context *c, \
266  struct avi_isp_ ## _node ## _regs *regs) \
267  { \
268  memcpy_from_registers(regs, c->offsets[_node], sizeof(*regs)); \
269  }
270 
#define AVI_ISP_GREEN_IMBALANCE_GREEN_RED_COEFF_MEM
#define AVI_ISP_DROP
Definition: reg_avi.h:49
uint32_t chain_yuv
Definition: libisp.c:22
unsigned short uint16_t
Definition: types.h:16
struct avi_isp_gamma_corrector_bv_lut_regs gc_bv_lut
Gamma corrector BV lut.
Definition: libisp.h:84
struct avi_isp_green_imbalance_green_blue_coeff_mem_regs grim_gb
Green imbalance GB coefficients.
Definition: libisp.h:66
struct avi_isp_pedestal_regs pedestal
Pedestral parameters (substract from pixels)
Definition: libisp.h:63
uint32_t nb_valid_Y
Definition: libisp.h:99
static const unsigned isp_bases[]
Definition: libisp.c:37
struct avi_isp_dead_pixel_correction_regs dead_pixel_correction
Dead pixel correction (disabled)
Definition: libisp.h:67
#define AVI_ISP_EDGE_ENHANCEMENT_COLOR_REDUCTION_FILTER_EE_KERNEL_COEFF
#define AVI_ISP_STATISTICS_BAYER
Definition: reg_avi.h:38
uint32_t awb_sum_Y
Definition: libisp.h:95
#define AVI_ISP_LENS_SHADING_CORRECTION_BLUE_COEFF_MEM
unsigned long avi_base
Definition: libisp.h:55
#define AVI_ISP_LENS_SHADING_CORRECTION_RED_COEFF_MEM
int devmem
Definition: libisp.h:54
struct avi_isp_gamma_corrector_ry_lut_regs gc_ry_lut
Gamma corrector RY lut.
Definition: libisp.h:82
struct avi_isp_chain_yuv_inter_regs chain_yuv_inter
YUV chain bypass configuration (enable/disable features)
Definition: libisp.h:90
static void memcpy_to_registers(unsigned long addr, const void *reg_base, size_t s)
Definition: libisp.c:230
struct avi_isp_green_imbalance_green_red_coeff_mem_regs grim_gr
Green imbalance GR coefficients.
Definition: libisp.h:65
#define AVI_ISP_CHAIN_BAYER_INTER
Definition: reg_avi.h:32
struct avi_isp_lens_shading_correction_red_coeff_mem_regs lsc_red_coeffs
Lens shade correction red coefficients.
Definition: libisp.h:71
#define AVI_ISP_VLFORMAT_32TO40
Definition: reg_avi.h:33
#define AVI_ISP_CHAIN_YUV_INTER
Definition: reg_avi.h:46
static int avi_isp_get_offsets_fd(int fd, struct avi_isp_offsets *off)
This is taken from libisp.
Definition: libisp.c:76
int isp_get_statistics_yuv(struct isp_yuv_stats_t *yuv_stats)
Definition: libisp.c:201
union avi_isp_statistics_yuv_awb_sum_u awb_sum_u
struct avi_isp_lens_shading_correction_regs lens_shading_correction
Lens shade correction.
Definition: libisp.h:70
uint32_t chroma
Definition: libisp.c:20
uint32_t awb_nb_grey_pixels
Definition: libisp.h:98
union avi_isp_statistics_yuv_ae_nb_valid_y ae_nb_valid_y
#define AVI_BASE
Definition: libisp.c:13
#define AVI_ISP_DENOISING
Definition: reg_avi.h:37
struct libisp_config isp_config
Definition: libisp_config.c:4
struct avi_isp_edge_enhancement_color_reduction_filter_ee_lut_regs eecrf_lut
Edge enhancement + Color correction lut.
Definition: libisp.h:88
int configure_isp(struct v4l2_device *dev)
Definition: libisp.c:163
struct avi_isp_vlformat_40to32_regs vlformat_40to32
Conversion factor (10bit to 10bit default)
Definition: libisp.h:80
#define AVI_ISP_EDGE_ENHANCEMENT_COLOR_REDUCTION_FILTER
Definition: reg_avi.h:47
union avi_isp_statistics_yuv_awb_sum_y awb_sum_y
struct avi_isp_vlformat_32to40_regs vlformat_32to40
Conversion factor (10bit to 10bit default)
Definition: libisp.h:61
#define AVI_ISP_DEAD_PIXEL_CORRECTION_LIST_MEM
#define AVI_ISP_EDGE_ENHANCEMENT_COLOR_REDUCTION_FILTER_EE_LUT
#define AVI_ISP_IOGET_OFFSETS
Definition: libisp.c:26
struct avi_isp_edge_enhancement_color_reduction_filter_regs eecrf
Edge enhancement + Color reduction.
Definition: libisp.h:87
static struct libisp_context isp_ctx
Definition: libisp.c:33
#define AVI_ISP_GREEN_IMBALANCE_GREEN_BLUE_COEFF_MEM
unsigned long uint32_t
Definition: types.h:18
uint32_t chain_bayer
Definition: libisp.c:18
#define AVI_ISP_DEAD_PIXEL_CORRECTION
Definition: reg_avi.h:36
#define AVI_ISP_DEAD_PIXEL_CORRECTION_CFA
uint32_t awb_sum_V
Definition: libisp.h:97
uint32_t awb_sum_U
Definition: libisp.h:96
struct avi_isp_color_correction_regs color_correction
Color correction parameters.
Definition: libisp.h:79
static void memcpy_from_registers(void *reg_base, unsigned long addr, size_t s)
Definition: libisp.c:244
struct avi_isp_chain_bayer_inter_regs bayer_inter
Enable or disable bayer ISP functions by bypassing them.
Definition: libisp.h:62
#define writel(_val, _addr)
Definition: libisp.c:30
#define AVI_ISP_CHROMATIC_ABERRATION
Definition: reg_avi.h:40
struct avi_isp_bayer_regs bayer
< Chromatic abberation (Disabled for now)
Definition: libisp.h:78
unsigned offsets[ISP_NODE_NR]
Definition: libisp.h:56
#define AVI_ISP_GAMMA_CORRECTOR_GU_LUT
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
uint32_t statistics_yuv
Definition: libisp.c:21
struct avi_isp_lens_shading_correction_blue_coeff_mem_regs lsc_blue_coeffs
Lens shade correction blue coefficients.
Definition: libisp.h:75
struct avi_isp_gamma_corrector_regs gamma_corrector
Gamma corrector (Curves)
Definition: libisp.h:81
union avi_isp_statistics_yuv_awb_nb_grey_pixels awb_nb_grey_pixels
struct avi_isp_statistics_yuv_regs statistics_yuv
YUV statistics parameters.
Definition: libisp.h:86
uint32_t gamma_corrector
Definition: libisp.c:19
#define AVI_ISP_GAMMA_CORRECTOR_BV_LUT
int fd
Definition: serial.c:26
#define AVI_SIZE
Definition: libisp.c:14
struct avi_isp_gamma_corrector_gu_lut_regs gc_gu_lut
Gamma corrector GU lut.
Definition: libisp.h:83
union avi_isp_statistics_yuv_awb_sum_v awb_sum_v
struct avi_isp_chroma_regs chroma
Color space conversion.
Definition: libisp.h:85
struct avi_isp_statistics_bayer_regs statistics_bayer
Statistics bayer parameters.
Definition: libisp.h:69
#define AVI_ISP_COLOR_CORRECTION
Definition: reg_avi.h:42
#define AVI_ISP_I3D_LUT
Definition: reg_avi.h:48
static int open_isp_fd(struct libisp_context *ctx, int fd)
This is taken from libisp.
Definition: libisp.c:91
struct avi_isp_denoising_regs denoising
Denoising parameters.
Definition: libisp.h:68
#define AVI_DEFINE_NODE(EXPANDER)
Definition: libisp.h:10
#define AVI_ISP_BAYER
Definition: reg_avi.h:41
#define AVI_MASK
Definition: libisp.c:15
#define AVI_ISP_I3D_LUT_CLIP_MODE
struct avi_isp_green_imbalance_regs green_imbalance
Green imbalance correction.
Definition: libisp.h:64
union avi_isp_statistics_yuv_ae_histogram_y ae_histogram_y[256]
#define AVI_ISP_LENS_SHADING_CORRECTION_GREEN_COEFF_MEM
#define AVI_ISP_LENS_SHADING_CORRECTION
Definition: reg_avi.h:39
#define AVI_ISP_PEDESTAL
Definition: reg_avi.h:34
#define AVI_ISP_VLFORMAT_40TO32
Definition: reg_avi.h:43
uint32_t ae_histogram_Y[256]
Definition: libisp.h:100
#define AVI_ISP_I3D_LUT_LUT_OUTSIDE
struct avi_isp_lens_shading_correction_green_coeff_mem_regs lsc_green_coeffs
Lens shade correction green coefficients.
Definition: libisp.h:73
#define AVI_ISP_GREEN_IMBALANCE
Definition: reg_avi.h:35
#define EXPAND_AS_FUNCTION(_node)
Definition: libisp.c:258
#define readl(_addr)
Definition: libisp.c:29
#define AVI_ISP_STATISTICS_YUV_AE_HISTOGRAM_Y
#define AVI_ISP_I3D_LUT_LUT_INSIDE
#define AVI_ISP_GAMMA_CORRECTOR_RY_LUT
int fd
The file pointer to the device.
Definition: v4l2.h:51