Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
mt9f002.c File Reference

Initialization of MT9F002 chip and options to change settings. More...

#include "std.h"
#include "mt9f002.h"
#include "mt9f002_regs.h"
#include "isp/libisp.h"
#include "math/pprz_algebra_int.h"
#include "peripherals/video_device.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <linux/videodev2.h>
#include "generated/airframe.h"
#include "boards/bebop.h"
Include dependency graph for mt9f002.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  blanking_t

Macros

#define MT9F002_I2C_TRIES   3
#define PRINT(string, ...)
#define VERBOSE_PRINT(...)
#define MT9F002_TARGET_EXPOSURE   30
 Exposure of the front camera of the bebop.
#define MT9F002_GAIN_GREEN1   2.0
#define MT9F002_GAIN_GREEN2   2.0
#define MT9F002_GAIN_RED   1.4
#define MT9F002_GAIN_BLUE   2.7

Functions

static bool i2c_checked (struct i2c_periph *p, struct i2c_transaction *t, uint8_t slave, uint16_t reg, uint8_t len_w, uint16_t len_r, float timeout, const char *tag)
 Blocking I2C transaction with result check and retry.
static void write_reg (struct mt9f002_t *mt, uint16_t addr, uint32_t val, uint8_t len)
 Write multiple bytes to a single register.
static uint32_t read_reg (struct mt9f002_t *mt, uint16_t addr, uint8_t len)
 Read multiple bytes from a register.
static void mt9f002_mipi_stage1 (struct mt9f002_t *mt)
 Configure stage 1 for both MiPi and HiSPi connection.
static void mt9f002_mipi_stage2 (struct mt9f002_t *mt)
 Configure stage 2 for both MiPi and HiSPi connection.
static void mt9f002_mipi_stage3 (struct mt9f002_t *mt)
 Configure stage 3 for both MiPi and HiSPi connection.
static void mt9f002_parallel_stage1 (struct mt9f002_t *mt)
 Configure stage 1 for parallel connection.
static void mt9f002_parallel_stage2 (struct mt9f002_t *mt)
 Configure stage 2 for parallel connection.
static void mt9f002_set_pll (struct mt9f002_t *mt)
 Set the PLL registers based on config.
static void mt9f002_set_blanking (struct mt9f002_t *mt)
void mt9f002_set_exposure (struct mt9f002_t *mt)
 Set the exposure configuration Depends on the blanking (and therefore the FPS).
static uint16_t mt9f002_calc_gain (float gain)
 Calculate the gain based on value of 1.0 -> 63.50.
void mt9f002_set_gains (struct mt9f002_t *mt)
 Sets the GreenR, Blue, Red and GreenB gains.
static void mt9f002_calc_resolution (struct mt9f002_t *mt)
static void mt9f002_set_resolution (struct mt9f002_t *mt)
void mt9f002_init (struct mt9f002_t *mt)
 Initialisation of the Aptina MT9F002 CMOS sensor (front camera).
void mt9f002_reset_color (struct mt9f002_t *mt)
void mt9f002_reset_exposure (struct mt9f002_t *mt)
void mt9f002_setting_update_resolution (float in UNUSED)
void mt9f002_setting_update_color (float in UNUSED)
void mt9f002_setting_update_exposure (float in UNUSED)

Variables

struct video_config_t front_camera
struct mt9f002_t mt9f002
struct blanking_t mt9f002_blanking
float mt9f002_send_resolution
float mt9f002_send_color
float mt9f002_send_exposure

Detailed Description

Initialization of MT9F002 chip and options to change settings.

Runs onboard AND in NPS (included by mt9f002_nps.c). NPS has no sensor: no I2C is done and every register reads as 0. So never configure from values read from the sensor (e.g. ask it for its resolution and store that in the struct): it would work onboard and break NPS.

Definition in file mt9f002.c.


Data Structure Documentation

◆ blanking_t

struct blanking_t

Definition at line 210 of file mt9f002.c.

Data Fields
uint16_t fine_integration_time_max_margin
uint16_t fine_integration_time_min
uint16_t min_line_blanking_pck
uint16_t min_line_fifo_pck
uint16_t min_line_length_pck

Macro Definition Documentation

◆ MT9F002_GAIN_BLUE

#define MT9F002_GAIN_BLUE   2.7

Definition at line 131 of file mt9f002.c.

Referenced by mt9f002_reset_color().

◆ MT9F002_GAIN_GREEN1

#define MT9F002_GAIN_GREEN1   2.0

Definition at line 119 of file mt9f002.c.

Referenced by mt9f002_reset_color().

◆ MT9F002_GAIN_GREEN2

#define MT9F002_GAIN_GREEN2   2.0

Definition at line 123 of file mt9f002.c.

Referenced by mt9f002_reset_color().

◆ MT9F002_GAIN_RED

#define MT9F002_GAIN_RED   1.4

Definition at line 127 of file mt9f002.c.

Referenced by mt9f002_reset_color().

◆ MT9F002_I2C_TRIES

#define MT9F002_I2C_TRIES   3

Definition at line 42 of file mt9f002.c.

Referenced by i2c_checked().

◆ MT9F002_TARGET_EXPOSURE

#define MT9F002_TARGET_EXPOSURE   30

Exposure of the front camera of the bebop.

Experimental values: Outside: 15 Inside well lit: 30 Inside poorly lit: 60

Definition at line 114 of file mt9f002.c.

Referenced by mt9f002_reset_exposure().

◆ PRINT

#define PRINT ( string,
... )
Value:
fprintf(stderr, "[MT9F002->%s()] " string,__FUNCTION__ , ##__VA_ARGS__)

Definition at line 93 of file mt9f002.c.

Referenced by write_reg().

◆ VERBOSE_PRINT

#define VERBOSE_PRINT ( ...)

Definition at line 98 of file mt9f002.c.

Referenced by mt9f002_calc_resolution(), mt9f002_init(), and mt9f002_set_blanking().

Function Documentation

◆ i2c_checked()

bool i2c_checked ( struct i2c_periph * p,
struct i2c_transaction * t,
uint8_t slave,
uint16_t reg,
uint8_t len_w,
uint16_t len_r,
float timeout,
const char * tag )
static

Blocking I2C transaction with result check and retry.

Never resubmits while the previous transaction is still queued, and sets the register address again before each read (a failed read can overwrite it).

Returns
true when the transaction succeeded

Definition at line 51 of file mt9f002.c.

References i2c_transaction::buf, i2c_blocking_transceive(), i2c_blocking_transmit(), I2CTransPending, I2CTransRunning, I2CTransSuccess, MT9F002_I2C_TRIES, p, s, i2c_transaction::status, and timeout.

Referenced by read_reg(), and write_reg().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_calc_gain()

uint16_t mt9f002_calc_gain ( float gain)
static

Calculate the gain based on value of 1.0 -> 63.50.

Definition at line 905 of file mt9f002.c.

Referenced by mt9f002_set_gains().

Here is the caller graph for this function:

◆ mt9f002_calc_resolution()

◆ mt9f002_init()

◆ mt9f002_mipi_stage1()

void mt9f002_mipi_stage1 ( struct mt9f002_t * mt)
inlinestatic

Configure stage 1 for both MiPi and HiSPi connection.

Definition at line 267 of file mt9f002.c.

References mt9f002_t::interface, MT9F002_CPP_DATA_FORMAT, MT9F002_HiSPi, MT9F002_MFR_3176, MT9F002_MFR_317C, MT9F002_MFR_3D00, MT9F002_MFR_3D02, MT9F002_MFR_3D04, MT9F002_MFR_3D06, MT9F002_MFR_3D08, MT9F002_MFR_3D0A, MT9F002_MFR_3D0C, MT9F002_MFR_3D0E, MT9F002_MFR_3D10, MT9F002_MFR_3D12, MT9F002_MFR_3D14, MT9F002_MFR_3D16, MT9F002_MFR_3D18, MT9F002_MFR_3D1A, MT9F002_MFR_3D1C, MT9F002_MFR_3D1E, MT9F002_MFR_3D20, MT9F002_MFR_3D22, MT9F002_MFR_3D24, MT9F002_MFR_3D26, MT9F002_MFR_3D28, MT9F002_MFR_3D2A, MT9F002_MFR_3D2C, MT9F002_MFR_3D2E, MT9F002_MFR_3D30, MT9F002_MFR_3D32, MT9F002_MFR_3D34, MT9F002_MFR_3D36, MT9F002_MFR_3D38, MT9F002_MFR_3D3A, MT9F002_MFR_3D3C, MT9F002_MFR_3D3E, MT9F002_MFR_3D40, MT9F002_MFR_3D42, MT9F002_MFR_3D44, MT9F002_MFR_3D46, MT9F002_MFR_3D48, MT9F002_MFR_3D4A, MT9F002_MFR_3D4C, MT9F002_MFR_3D4E, MT9F002_MFR_3D50, MT9F002_MFR_3D52, MT9F002_MFR_3D54, MT9F002_MFR_3D56, MT9F002_MFR_3D58, MT9F002_MFR_3D5A, MT9F002_MFR_3D5C, MT9F002_MFR_3D5E, MT9F002_MFR_3D60, MT9F002_MFR_3D62, MT9F002_MFR_3D64, MT9F002_MFR_3D66, MT9F002_MFR_3D68, MT9F002_MFR_3D6A, MT9F002_MFR_3D6C, MT9F002_MFR_3D6E, MT9F002_MFR_3D70, MT9F002_MFR_3D72, MT9F002_MFR_3D74, MT9F002_MFR_3D76, MT9F002_MFR_3D78, MT9F002_MFR_3D7A, MT9F002_MFR_3D7C, MT9F002_MFR_3D7E, MT9F002_MFR_3D80, MT9F002_MFR_3D82, MT9F002_MFR_3D84, MT9F002_MFR_3D86, MT9F002_MFR_3D88, MT9F002_MFR_3D8A, MT9F002_MFR_3D8C, MT9F002_MFR_3D8E, MT9F002_MFR_3D90, MT9F002_MFR_3D92, MT9F002_MFR_3D94, MT9F002_MFR_3D96, MT9F002_MFR_3D98, MT9F002_MFR_3D9A, MT9F002_MFR_3D9C, MT9F002_MFR_3D9E, MT9F002_MFR_3DA0, MT9F002_MFR_3DA2, MT9F002_MFR_3DA4, MT9F002_MFR_3DA6, MT9F002_MFR_3DA8, MT9F002_MFR_3DAA, MT9F002_MFR_3DAC, MT9F002_MFR_3DAE, MT9F002_MFR_3DB0, MT9F002_MFR_3DB2, MT9F002_MFR_3DB4, MT9F002_MFR_3DB6, MT9F002_MFR_3DB8, MT9F002_MFR_3DBA, MT9F002_MFR_3DBC, MT9F002_MFR_3DBE, MT9F002_MFR_3DC0, MT9F002_MFR_3DC2, MT9F002_MFR_3DC4, MT9F002_MFR_3DC6, MT9F002_MFR_3DC8, MT9F002_MFR_3DCA, MT9F002_MFR_3DCC, MT9F002_MFR_3DCE, MT9F002_MFR_3DD0, MT9F002_MFR_3DD2, MT9F002_MFR_3DD4, MT9F002_MFR_3DD6, MT9F002_MFR_3DD8, MT9F002_MFR_3DDA, MT9F002_MFR_3DDC, MT9F002_MFR_3DDE, MT9F002_MFR_3DE0, MT9F002_MFR_3DE2, MT9F002_MFR_3DE4, MT9F002_MFR_3DE6, MT9F002_MFR_3DE8, MT9F002_MFR_3DEA, MT9F002_MFR_3DEC, MT9F002_MFR_3DEE, MT9F002_MFR_3DF0, MT9F002_MFR_3DF2, MT9F002_MFR_3DF4, MT9F002_MFR_3DF6, MT9F002_MFR_3DF8, MT9F002_MFR_3DFA, MT9F002_MFR_3DFC, MT9F002_MFR_3DFE, MT9F002_MFR_3E00, MT9F002_MFR_3E02, MT9F002_MFR_3E04, MT9F002_MFR_3E06, MT9F002_MFR_3E08, MT9F002_MFR_3E0A, MT9F002_MFR_3E0C, MT9F002_MFR_3E0E, MT9F002_MFR_3E10, MT9F002_MFR_3E12, MT9F002_MFR_3E14, MT9F002_MFR_3E16, MT9F002_MFR_3E18, MT9F002_MFR_3E1A, MT9F002_MFR_3E1C, MT9F002_MFR_3E1E, MT9F002_MFR_3E20, MT9F002_MFR_3E22, MT9F002_MFR_3E24, MT9F002_MFR_3E26, MT9F002_MFR_3E28, MT9F002_MFR_3E2A, MT9F002_MFR_3E2C, MT9F002_MFR_3E2E, MT9F002_MFR_3E30, MT9F002_MFR_3E32, MT9F002_MFR_3E34, MT9F002_MFR_3E36, MT9F002_MFR_3E38, MT9F002_MFR_3E3A, MT9F002_MFR_3E3C, MT9F002_MFR_3E3E, MT9F002_MFR_3E40, MT9F002_MFR_3E42, MT9F002_MFR_3E44, MT9F002_MFR_3E46, MT9F002_MFR_3E48, MT9F002_MFR_3E4A, MT9F002_MFR_3E4C, MT9F002_MFR_3E4E, MT9F002_MFR_3E50, MT9F002_MFR_3E52, MT9F002_MFR_3E54, MT9F002_MFR_3E56, MT9F002_MFR_3E58, MT9F002_MFR_3E5A, MT9F002_MFR_3E5C, MT9F002_MFR_3E5E, MT9F002_MFR_3E60, MT9F002_MFR_3E62, MT9F002_MFR_3E64, MT9F002_MFR_3E66, MT9F002_MFR_3E68, MT9F002_MFR_3E6A, MT9F002_MFR_3E6C, MT9F002_MFR_3E6E, MT9F002_MFR_3E70, MT9F002_MFR_3E72, MT9F002_MFR_3E74, MT9F002_MFR_3E76, MT9F002_MFR_3E78, MT9F002_MFR_3E7A, MT9F002_MFR_3E7C, MT9F002_MFR_3E7E, MT9F002_MFR_3E80, MT9F002_MFR_3E82, MT9F002_MFR_3E84, MT9F002_MFR_3E86, MT9F002_MFR_3E88, MT9F002_MFR_3E8A, MT9F002_MFR_3E8C, MT9F002_MFR_3E8E, MT9F002_MFR_3E90, MT9F002_MFR_3E92, MT9F002_MFR_3E94, MT9F002_MFR_3E96, MT9F002_MFR_3E98, MT9F002_MFR_3E9A, MT9F002_MFR_3E9C, MT9F002_MFR_3E9E, MT9F002_MFR_3EA0, MT9F002_MFR_3EA2, MT9F002_MFR_3EA4, MT9F002_MFR_3EA6, MT9F002_MFR_3EA8, MT9F002_MFR_3EAA, MT9F002_MFR_3EAC, MT9F002_MFR_3EAE, MT9F002_MFR_3EB0, MT9F002_MFR_3EB2, MT9F002_MFR_3EB4, MT9F002_MFR_3EB6, MT9F002_MFR_3EB8, MT9F002_MFR_3EBA, MT9F002_MFR_3EBC, MT9F002_MFR_3EBE, MT9F002_MFR_3EC0, MT9F002_MFR_3EC2, MT9F002_MFR_3EC4, MT9F002_MFR_3EC6, MT9F002_MFR_3EC8, MT9F002_MFR_3ECA, MT9F002_MFR_3ED8, MT9F002_MFR_3EE6, MT9F002_MFR_3EE8, MT9F002_MODE_SELECT, MT9F002_RESET_REGISTER, MT9F002_SERIAL_FORMAT, MT9F002_SMIA_TEST, and write_reg().

Referenced by mt9f002_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_mipi_stage2()

void mt9f002_mipi_stage2 ( struct mt9f002_t * mt)
inlinestatic

Configure stage 2 for both MiPi and HiSPi connection.

Definition at line 523 of file mt9f002.c.

References MT9F002_SMIA_TEST, and write_reg().

Referenced by mt9f002_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_mipi_stage3()

void mt9f002_mipi_stage3 ( struct mt9f002_t * mt)
inlinestatic

Configure stage 3 for both MiPi and HiSPi connection.

Definition at line 531 of file mt9f002.c.

References MT9F002_EXTRA_DELAY, MT9F002_MFR_3EDC, MT9F002_MFR_3EE2, MT9F002_RESET_REGISTER, and write_reg().

Referenced by mt9f002_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_parallel_stage1()

void mt9f002_parallel_stage1 ( struct mt9f002_t * mt)
inlinestatic

Configure stage 1 for parallel connection.

Definition at line 542 of file mt9f002.c.

References MT9F002_ANALOG_CONTROL4, MT9F002_CTX_CONTROL_REG, MT9F002_CTX_WR_DATA_REG, MT9F002_DAC_LD_14_15, MT9F002_DATA_PEDESTAL_, MT9F002_DATAPATH_SELECT, MT9F002_GLOBAL_GAIN, MT9F002_RESET_REGISTER, MT9F002_SERIAL_FORMAT, MT9F002_SMIA_TEST, and write_reg().

Referenced by mt9f002_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_parallel_stage2()

void mt9f002_parallel_stage2 ( struct mt9f002_t * mt)
inlinestatic

◆ mt9f002_reset_color()

void mt9f002_reset_color ( struct mt9f002_t * mt)

◆ mt9f002_reset_exposure()

void mt9f002_reset_exposure ( struct mt9f002_t * mt)

Definition at line 1153 of file mt9f002.c.

References mt9f002_set_exposure(), MT9F002_TARGET_EXPOSURE, and mt9f002_t::target_exposure.

Here is the call graph for this function:

◆ mt9f002_set_blanking()

◆ mt9f002_set_exposure()

void mt9f002_set_exposure ( struct mt9f002_t * mt)

Set the exposure configuration Depends on the blanking (and therefore the FPS).

Definition at line 844 of file mt9f002.c.

References mt9f002_t::frame_length, mt9f002_t::line_length, mt9f002_blanking, MT9F002_COARSE_INTEGRATION_TIME, MT9F002_COARSE_INTEGRATION_TIME_MAX_MARGIN, MT9F002_COARSE_INTEGRATION_TIME_MIN, MT9F002_FINE_INTEGRATION_TIME_, read_reg(), mt9f002_t::real_exposure, mt9f002_t::target_exposure, mt9f002_t::vt_pix_clk, and write_reg().

Referenced by mt9f002_init(), mt9f002_reset_exposure(), mt9f002_setting_update_exposure(), and update_ae_awb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_set_gains()

void mt9f002_set_gains ( struct mt9f002_t * mt)

Sets the GreenR, Blue, Red and GreenB gains.

Definition at line 957 of file mt9f002.c.

References mt9f002_t::gain_blue, mt9f002_t::gain_green1, mt9f002_t::gain_green2, mt9f002_t::gain_red, MT9F002_BLUE_GAIN, mt9f002_calc_gain(), MT9F002_GREEN1_GAIN, MT9F002_GREEN2_GAIN, MT9F002_RED_GAIN, and write_reg().

Referenced by mt9f002_init(), mt9f002_reset_color(), and mt9f002_setting_update_color().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mt9f002_set_pll()

◆ mt9f002_set_resolution()

◆ mt9f002_setting_update_color()

void mt9f002_setting_update_color ( float in UNUSED)

Definition at line 1184 of file mt9f002.c.

References mt9f002, mt9f002_set_gains(), and UNUSED.

Here is the call graph for this function:

◆ mt9f002_setting_update_exposure()

void mt9f002_setting_update_exposure ( float in UNUSED)

Definition at line 1189 of file mt9f002.c.

References mt9f002, mt9f002_set_exposure(), and UNUSED.

Here is the call graph for this function:

◆ mt9f002_setting_update_resolution()

void mt9f002_setting_update_resolution ( float in UNUSED)

◆ read_reg()

uint32_t read_reg ( struct mt9f002_t * mt,
uint16_t addr,
uint8_t len )
static

Read multiple bytes from a register.

Definition at line 248 of file mt9f002.c.

References i2c_transaction::buf, i2c_checked(), mt9f002_t::i2c_periph, mt9f002_t::i2c_trans, and MT9F002_ADDRESS.

Referenced by mt9f002_set_blanking(), mt9f002_set_exposure(), and mt9f002_set_pll().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_reg()

void write_reg ( struct mt9f002_t * mt,
uint16_t addr,
uint32_t val,
uint8_t len )
static

Write multiple bytes to a single register.

Definition at line 221 of file mt9f002.c.

References i2c_transaction::buf, i2c_checked(), mt9f002_t::i2c_periph, mt9f002_t::i2c_trans, MT9F002_ADDRESS, PRINT, and val.

Referenced by mt9f002_init(), mt9f002_mipi_stage1(), mt9f002_mipi_stage2(), mt9f002_mipi_stage3(), mt9f002_parallel_stage1(), mt9f002_parallel_stage2(), mt9f002_set_blanking(), mt9f002_set_exposure(), mt9f002_set_gains(), mt9f002_set_pll(), and mt9f002_set_resolution().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ front_camera

struct video_config_t front_camera

Definition at line 135 of file mt9f002.c.

◆ mt9f002

◆ mt9f002_blanking

struct blanking_t mt9f002_blanking

◆ mt9f002_send_color

float mt9f002_send_color

Definition at line 950 of file mt9f002.c.

◆ mt9f002_send_exposure

float mt9f002_send_exposure

Definition at line 951 of file mt9f002.c.

◆ mt9f002_send_resolution

float mt9f002_send_resolution

Definition at line 949 of file mt9f002.c.