ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
c6_cam_camera.c File Reference

OV5640 hardware-JPEG camera backend for the C6 MJPEG example. More...

#include <stdint.h>
#include "c6_camera_server.h"
#include "ra8_attributes.h"
#include "ra8_board_ek_ra8d2.h"
#include "ra8_camera.h"
#include "ra8_camera_codec_passthrough.h"
#include "ra8_camera_source_ceu.h"
#include "ra8_ceu.h"
#include "ra8_ov5640.h"
#include "ra8_time.h"
Include dependency graph for c6_cam_camera.c:

Go to the source code of this file.

Enumerations

enum  sensor_jpeg_size_t : uint32_t {
  k_sensor_jpeg_buffer_bytes = 512U * 1024U ,
  k_sensor_jpeg_poll_ms = 2U ,
  k_sensor_jpeg_poll_tries = 2000U ,
  k_sensor_jpeg_xclk_hz = 24000000U ,
  k_sensor_jpeg_settle_ms = 100U
}
enum  sensor_jpeg_geometry_t : uint16_t {
  k_sensor_jpeg_width = 640U ,
  k_sensor_jpeg_height = 480U
}
enum  sensor_diagnostic_register_t : uint16_t {
  k_sensor_reg_system_reset00 = 0x3000U ,
  k_sensor_reg_system_reset02 = 0x3002U ,
  k_sensor_reg_clock_enable02 = 0x3006U ,
  k_sensor_reg_system_ctrl0 = 0x3008U ,
  k_sensor_reg_pll_ctrl0 = 0x3034U ,
  k_sensor_reg_pll_ctrl1 = 0x3035U ,
  k_sensor_reg_pll_ctrl2 = 0x3036U ,
  k_sensor_reg_pll_ctrl3 = 0x3037U ,
  k_sensor_reg_system_root = 0x3103U ,
  k_sensor_reg_clock_root = 0x3108U ,
  k_sensor_reg_pclk_divider = 0x3824U
}
 OV5640 clock and stream registers emitted on initialization failure. More...
enum  jpeg_marker_t : uint8_t {
  k_jpeg_marker_prefix = 0xFFU ,
  k_jpeg_marker_soi = 0xD8U ,
  k_jpeg_marker_eoi = 0xD9U
}
enum  sccb_operation_t : uint8_t {
  k_sccb_op_none = 0U ,
  k_sccb_op_read = 1U ,
  k_sccb_op_write = 2U
}
 Last SCCB operation retained for initialization diagnostics. More...

Functions

static ra8_err_t sensor_sccb_read (void *ctx, uint8_t address, uint16_t reg, uint8_t *out_value)
 Forward one SCCB read while retaining bounded failure diagnostics.
static ra8_err_t sensor_sccb_write (void *ctx, uint8_t address, uint16_t reg, uint8_t value)
 Forward one SCCB write while retaining bounded failure diagnostics.
static ra8_err_t sensor_bind (void)
 Bind the hardware-JPEG sensor instance to board SCCB callbacks.
static ra8_camera_source_ceu_cfg_t sensor_ceu_config (void)
 Build the CEU configuration for bounded hardware JPEG capture.
static void internal_reset_camera_diagnostics (void)
 Reset every camera diagnostic and lifecycle flag to its cold state.
static ra8_err_t internal_bring_up_sensor (void)
 Bring the OV5640 sensor up to streaming hardware-JPEG mode.
static ra8_err_t internal_bring_up_capture_pipeline (void)
 Route the parallel capture pins and bind the CEU source and codec.
ra8_err_t c6_cam_camera_init (void)
 Camera image and board-switch initialization, then one CEU capture.
static uint32_t jpeg_trim_to_eoi (const uint8_t *jpeg, uint32_t bytes)
 Locate the first complete JPEG payload in a captured span.
ra8_err_t c6_cam_camera_capture_jpeg (const uint8_t **out_jpeg, uint32_t *out_bytes, c6_cam_frame_timing_t *out_timing)
const char * c6_cam_camera_description (void)
 Human-readable description of the selected camera backend.
static void sensor_report_register (const char *label, uint16_t reg)
 Emit one named OV5640 register value when it remains readable.
static void internal_report_sccb_state (void)
 Report the most recent SCCB transfer direction, register, and result.
static void internal_report_sensor_registers (void)
 Report the OV5640 clock and stream registers when the sensor bound.
static void internal_report_capture_events (void)
 Report the CEU event mask observed by the most recent capture.
static void internal_report_jpeg_status_detail (void)
 Report the OV5640 hardware-JPEG status block from the last capture.
void c6_cam_camera_report_last_error (void)
 Emit backend-specific diagnostics for the last capture failure.

Variables

static uint8_t s_jpeg [k_sensor_jpeg_buffer_bytes]
static ra8_ov5640_t s_sensor
static ra8_camera_source_t s_source
static ra8_camera_source_ceu_state_t s_source_state
static ra8_camera_codec_t s_codec
static ra8_ov5640_jpeg_status_t s_last_jpeg_status
static ra8_err_t s_last_jpeg_status_err
static ra8_err_t s_last_sccb_err
static uint16_t s_last_sccb_reg
static sccb_operation_t s_last_sccb_op
static bool s_camera_initialized
static bool s_sensor_bound
static bool s_sensor_streaming

Detailed Description

OV5640 hardware-JPEG camera backend for the C6 MJPEG example.

Binds the reusable sensor and CEU source, captures one bounded JPEG, and exposes it through the shared C6 camera-server contract.

Tag
[Ring 6 / APP] {World: S}
Since
0.1.0

Definition in file c6_cam_camera.c.

Enumeration Type Documentation

◆ jpeg_marker_t

enum jpeg_marker_t : uint8_t
Enumerator
k_jpeg_marker_prefix 

JPEG marker prefix byte.

k_jpeg_marker_soi 

JPEG start-of-image marker byte.

k_jpeg_marker_eoi 

JPEG end-of-image marker byte.

Definition at line 55 of file c6_cam_camera.c.

◆ sccb_operation_t

enum sccb_operation_t : uint8_t

Last SCCB operation retained for initialization diagnostics.

Enumerator
k_sccb_op_none 

No SCCB transfer has run.

k_sccb_op_read 

Last transfer was a read.

k_sccb_op_write 

Last transfer was a write.

Definition at line 62 of file c6_cam_camera.c.

◆ sensor_diagnostic_register_t

OV5640 clock and stream registers emitted on initialization failure.

Enumerator
k_sensor_reg_system_reset00 

Internal MCU reset control.

k_sensor_reg_system_reset02 

JPEG reset control.

k_sensor_reg_clock_enable02 

JPEG clock enables.

k_sensor_reg_system_ctrl0 

Software stream state.

k_sensor_reg_pll_ctrl0 

PLL bit-mode control.

k_sensor_reg_pll_ctrl1 

PLL system divider.

k_sensor_reg_pll_ctrl2 

PLL multiplier.

k_sensor_reg_pll_ctrl3 

PLL pre-divider.

k_sensor_reg_system_root 

System-clock source.

k_sensor_reg_clock_root 

PCLK/SCLK root dividers.

k_sensor_reg_pclk_divider 

DVP pixel-clock divider.

Definition at line 41 of file c6_cam_camera.c.

◆ sensor_jpeg_geometry_t

enum sensor_jpeg_geometry_t : uint16_t
Enumerator
k_sensor_jpeg_width 

Encoded sensor JPEG width.

k_sensor_jpeg_height 

Encoded sensor JPEG height.

Definition at line 35 of file c6_cam_camera.c.

◆ sensor_jpeg_size_t

enum sensor_jpeg_size_t : uint32_t
Enumerator
k_sensor_jpeg_buffer_bytes 

Maximum gated frame span.

k_sensor_jpeg_poll_ms 

CEU completion poll period.

k_sensor_jpeg_poll_tries 

CEU completion poll limit.

k_sensor_jpeg_xclk_hz 

OV5640 input clock.

k_sensor_jpeg_settle_ms 

Sensor routing settle delay.

Definition at line 27 of file c6_cam_camera.c.

Function Documentation

◆ c6_cam_camera_capture_jpeg()

◆ c6_cam_camera_description()

const char * c6_cam_camera_description ( void )
nodiscard

Human-readable description of the selected camera backend.

Definition at line 389 of file c6_cam_camera.c.

Referenced by internal_c6_cam_prepare_camera().

◆ c6_cam_camera_init()

◆ c6_cam_camera_report_last_error()

void c6_cam_camera_report_last_error ( void )

Emit backend-specific diagnostics for the last capture failure.

Writes retained sensor or CEU status through the shared bounded console helpers.

Precondition
The board console is initialized.
A camera capture was attempted or the backend has reset diagnostics.
Postcondition
A backend-specific diagnostic line is queued.
Camera and capture state remain unchanged.
Note
Intended for failure paths; output content depends on the selected backend.
Since
0.1.0

Definition at line 535 of file c6_cam_camera.c.

References c6_cam_put_u32(), c6_cam_puts(), internal_report_capture_events(), internal_report_jpeg_status_detail(), internal_report_sccb_state(), internal_report_sensor_registers(), k_ra8_ok, ra8_camera_source_ceu_get_last_events(), ra8_err_to_str(), s_camera_initialized, s_last_jpeg_status_err, and s_source_state.

Referenced by internal_c6_cam_http_frame(), internal_c6_cam_http_stream(), and internal_c6_cam_prepare_camera().

◆ internal_bring_up_capture_pipeline()

ra8_err_t internal_bring_up_capture_pipeline ( void )
static

Route the parallel capture pins and bind the CEU source and codec.

Runs after the sensor is streaming: routes the DVP pins away from their bring-up owner, then binds the CEU capture source and the passthrough JPEG codec.

Returns
Repository error code from the first failed step, or success.
Return values
k_ra8_okThe capture pipeline is bound and ready to capture.
otherThe pin-routing, CEU, or codec step that failed reported this.
Precondition
internal_bring_up_sensor has already completed successfully.
s_source, s_source_state, and s_codec are not concurrently used.
Postcondition
On success s_source and s_codec are initialized for capture.
On any failure the function returns before later steps run.
Note
Not thread-safe with concurrent capture or re-initialization.
Since
0.1.0

Definition at line 289 of file c6_cam_camera.c.

References k_ra8_ok, ra8_board_camera_route_parallel_pins(), ra8_camera_codec_passthrough_init(), ra8_camera_source_ceu_init(), RA8_INTERNAL, s_codec, s_source, s_source_state, and sensor_ceu_config().

Referenced by c6_cam_camera_init().

◆ internal_bring_up_sensor()

ra8_err_t internal_bring_up_sensor ( void )
static

Bring the OV5640 sensor up to streaming hardware-JPEG mode.

Sequences clock start, board reset, SCCB bind, chip-id probe, and VGA-JPEG mode configuration, stopping at the first failure.

Returns
Repository error code from the first failed step, or success.
Return values
k_ra8_okThe sensor is bound, probed, and streaming VGA JPEG.
k_ra8_err_hw_init_failedThe probed chip id did not match OV5640.
otherThe board or sensor step that failed reported this code.
Precondition
The board camera pins and clock are not already owned by another user.
s_sensor is not concurrently accessed by another init or capture.
Postcondition
On success s_sensor_bound and s_sensor_streaming are both true.
On any failure the function returns before later steps run.
Note
Not thread-safe with concurrent sensor access.
Since
0.1.0

Definition at line 244 of file c6_cam_camera.c.

References k_ra8_err_hw_init_failed, k_ra8_ok, k_ra8_ov5640_chip_id, k_ra8_ov5640_mode_vga_jpeg, k_sensor_jpeg_settle_ms, k_sensor_jpeg_xclk_hz, ra8_board_camera_reset(), ra8_board_camera_select_parallel(), ra8_board_camera_xclk_start(), ra8_delay_ms(), RA8_INTERNAL, ra8_ov5640_configure(), ra8_ov5640_probe(), s_sensor, s_sensor_bound, s_sensor_streaming, and sensor_bind().

Referenced by c6_cam_camera_init().

◆ internal_report_capture_events()

void internal_report_capture_events ( void )
static

Report the CEU event mask observed by the most recent capture.

Silent when the CEU source cannot report an event mask, e.g. before any capture has run.

Precondition
The diagnostic console is initialized.
s_source_state was initialized by c6_cam_camera_init.
Postcondition
On success one ceu_events= field is emitted.
No static diagnostic state is modified.
Note
Not thread-safe with a concurrent capture.
Since
0.1.0

Definition at line 487 of file c6_cam_camera.c.

References c6_cam_put_u32(), c6_cam_puts(), k_ra8_ok, ra8_camera_source_ceu_get_last_events(), RA8_INTERNAL, and s_source_state.

Referenced by c6_cam_camera_report_last_error().

◆ internal_report_jpeg_status_detail()

void internal_report_jpeg_status_detail ( void )
static

Report the OV5640 hardware-JPEG status block from the last capture.

No-op unless the last ra8_ov5640_jpeg_status_get succeeded, since the status fields are undefined otherwise.

Precondition
The diagnostic console is initialized.
s_last_jpeg_status was populated by the most recent capture attempt.
Postcondition
When s_last_jpeg_status_err == k_ra8_ok, every field of s_last_jpeg_status is emitted once.
No static diagnostic state is modified.
Note
Not thread-safe with a concurrent capture.
Since
0.1.0

Definition at line 508 of file c6_cam_camera.c.

References c6_cam_put_u32(), c6_cam_puts(), k_ra8_ok, RA8_INTERNAL, s_last_jpeg_status, and s_last_jpeg_status_err.

Referenced by c6_cam_camera_report_last_error().

◆ internal_report_sccb_state()

void internal_report_sccb_state ( void )
static

Report the most recent SCCB transfer direction, register, and result.

Always emitted regardless of camera or sensor bring-up state, since a bring-up failure is exactly when this is most needed.

Precondition
The diagnostic console is initialized.
s_last_sccb_op, s_last_sccb_reg, and s_last_sccb_err describe one complete SCCB transfer attempt (or the cold-start "none" state).
Postcondition
One sccb_op=/sccb_reg=/sccb_err= triple is emitted.
No static diagnostic state is modified.
Note
Not thread-safe with a concurrent SCCB transfer.
Since
0.1.0

Definition at line 430 of file c6_cam_camera.c.

References c6_cam_put_u32(), c6_cam_puts(), k_sccb_op_read, k_sccb_op_write, ra8_err_to_str(), RA8_INTERNAL, s_last_sccb_err, s_last_sccb_op, and s_last_sccb_reg.

Referenced by c6_cam_camera_report_last_error().

◆ internal_report_sensor_registers()

void internal_report_sensor_registers ( void )
static

Report the OV5640 clock and stream registers when the sensor bound.

No-op when sensor_bind never succeeded, since the registers would not be readable.

Precondition
The diagnostic console is initialized.
s_sensor was bound by a prior internal_bring_up_sensor call.
Postcondition
When s_sensor_bound, every diagnostic register in sensor_diagnostic_register_t is emitted once.
No static diagnostic state is modified.
Note
Not thread-safe with concurrent sensor access.
Since
0.1.0

Definition at line 458 of file c6_cam_camera.c.

References k_sensor_reg_clock_enable02, k_sensor_reg_clock_root, k_sensor_reg_pclk_divider, k_sensor_reg_pll_ctrl0, k_sensor_reg_pll_ctrl1, k_sensor_reg_pll_ctrl2, k_sensor_reg_pll_ctrl3, k_sensor_reg_system_ctrl0, k_sensor_reg_system_reset00, k_sensor_reg_system_reset02, k_sensor_reg_system_root, RA8_INTERNAL, s_sensor_bound, and sensor_report_register().

Referenced by c6_cam_camera_report_last_error().

◆ internal_reset_camera_diagnostics()

void internal_reset_camera_diagnostics ( void )
static

Reset every camera diagnostic and lifecycle flag to its cold state.

Runs first in c6_cam_camera_init so a re-init after a partial failure never reports stale SCCB or streaming state.

Precondition
No capture or diagnostic report is concurrently in progress.
Static camera state was previously left in a defined state (init or a prior failed init).
Postcondition
Every static diagnostic and lifecycle flag holds its cold-start value.
No hardware register or bus transfer is touched.
Note
Not thread-safe with concurrent capture or diagnostic reporting.
Since
0.1.0

Definition at line 219 of file c6_cam_camera.c.

References k_ra8_ok, k_sccb_op_none, RA8_INTERNAL, s_camera_initialized, s_last_sccb_err, s_last_sccb_op, s_last_sccb_reg, s_sensor_bound, and s_sensor_streaming.

Referenced by c6_cam_camera_init().

◆ jpeg_trim_to_eoi()

uint32_t jpeg_trim_to_eoi ( const uint8_t * jpeg,
uint32_t bytes )
static

Locate the first complete JPEG payload in a captured span.

Validates the SOI prefix and returns the length through the first EOI marker.

Parameters
[in]jpegCaptured sensor byte span.
[in]bytesAvailable capture bytes.
Returns
Trimmed JPEG payload length.
Return values
0The span lacks a valid SOI or complete EOI.
Precondition
jpeg addresses at least bytes readable bytes.
bytes is bounded by the static JPEG capture capacity.
Postcondition
The input span remains unchanged.
A nonzero result includes both EOI marker bytes.
Note
Bytes after the first EOI are intentionally excluded.
Since
0.1.0

Definition at line 327 of file c6_cam_camera.c.

References k_jpeg_marker_eoi, k_jpeg_marker_prefix, and k_jpeg_marker_soi.

Referenced by c6_cam_camera_capture_jpeg().

◆ sensor_bind()

ra8_err_t sensor_bind ( void )
static

Bind the hardware-JPEG sensor instance to board SCCB callbacks.

Constructs a transport over the EK-RA8D2 camera bus and delay service.

Returns
Repository error code.
Return values
k_ra8_okThe sensor instance was initialized.
k_ra8_err_null_ptrA required transport dependency was absent.
Precondition
Board camera support is linked and its I2C channel can be initialized.
No capture concurrently uses s_sensor.
Postcondition
On success, s_sensor is initialized at the primary SCCB address.
No sensor register transfer occurs during binding.
Note
Probing is performed later by camera initialization.
Since
0.1.0

Definition at line 143 of file c6_cam_camera.c.

References ra8_board_camera_delay_ms(), ra8_ov5640_init(), s_sensor, sensor_sccb_read(), and sensor_sccb_write().

Referenced by internal_bring_up_sensor().

◆ sensor_ceu_config()

ra8_camera_source_ceu_cfg_t sensor_ceu_config ( void )
static

Build the CEU configuration for bounded hardware JPEG capture.

Selects data-enable capture into a fixed SDRAM buffer with VGA metadata.

Returns
Generic CEU source configuration.
Return values
ra8_camera_source_ceu_cfg_tComplete hardware-JPEG source configuration.
Precondition
The compile-time buffer capacity fits in the CEU image-area field.
Geometry constants match the OV5640 VGA JPEG mode.
Postcondition
Returned configuration references no temporary storage.
Static camera state remains unchanged.
Note
JPEG payload length is determined after capture by marker scanning.
Since
0.1.0

Definition at line 166 of file c6_cam_camera.c.

References k_ra8_camera_format_jpeg, k_ra8_ceu_burst_256, k_ra8_ceu_bus_8_bit, k_ra8_ceu_capture_single, k_ra8_ceu_edge_rising, k_ra8_ceu_field_immediate, k_ra8_ceu_fmt_data_enable, k_ra8_ceu_input_cb0_y0_cr0_y1, k_ra8_ceu_output_ycbcr_422, k_ra8_ceu_pol_high_active, k_sensor_jpeg_buffer_bytes, k_sensor_jpeg_height, k_sensor_jpeg_poll_ms, k_sensor_jpeg_poll_tries, and k_sensor_jpeg_width.

Referenced by internal_bring_up_capture_pipeline().

◆ sensor_report_register()

void sensor_report_register ( const char * label,
uint16_t reg )
static

Emit one named OV5640 register value when it remains readable.

Keeps failed JPEG bring-up diagnostics bounded and heap-free.

Parameters
[in]labelConsole label including its leading separator.
[in]regSensor register address.
Precondition
The diagnostic console is initialized.
The OV5640 transport was bound successfully.
Postcondition
A successful read emits the register value in decimal.
A failed read emits the repository error string.
Note
This helper intentionally updates the retained SCCB operation fields.
Since
0.1.0

Definition at line 406 of file c6_cam_camera.c.

References c6_cam_put_u32(), c6_cam_puts(), k_ra8_ok, ra8_err_to_str(), ra8_ov5640_read_reg(), and s_sensor.

Referenced by internal_report_sensor_registers().

◆ sensor_sccb_read()

ra8_err_t sensor_sccb_read ( void * ctx,
uint8_t address,
uint16_t reg,
uint8_t * out_value )
static

Forward one SCCB read while retaining bounded failure diagnostics.

Delegates transport ownership to the EK-RA8D2 board adapter.

Parameters
[in]ctxOpaque board context forwarded unchanged.
[in]addressSensor SCCB address.
[in]regSensor register address.
[out]out_valueDestination byte.
Returns
Repository error code from the board adapter.
Precondition
Camera SCCB pins and RIIC are initialized by the board adapter.
out_value points to writable storage.
Postcondition
Operation, register, and result are retained in static diagnostics.
On success out_value contains the sensor register byte.
Note
Not thread-safe with concurrent sensor access.
Since
0.1.0

Definition at line 97 of file c6_cam_camera.c.

References k_sccb_op_read, ra8_board_camera_sccb_read_reg(), s_last_sccb_err, s_last_sccb_op, and s_last_sccb_reg.

Referenced by sensor_bind().

◆ sensor_sccb_write()

ra8_err_t sensor_sccb_write ( void * ctx,
uint8_t address,
uint16_t reg,
uint8_t value )
static

Forward one SCCB write while retaining bounded failure diagnostics.

Delegates transport ownership to the EK-RA8D2 board adapter.

Parameters
[in]ctxOpaque board context forwarded unchanged.
[in]addressSensor SCCB address.
[in]regSensor register address.
[in]valueRegister byte to write.
Returns
Repository error code from the board adapter.
Precondition
Camera SCCB pins and RIIC are initialized by the board adapter.
No other context accesses the sensor concurrently.
Postcondition
Operation, register, and result are retained in static diagnostics.
On success the sensor accepted value for reg.
Note
Not thread-safe with concurrent sensor access.
Since
0.1.0

Definition at line 121 of file c6_cam_camera.c.

References k_sccb_op_write, ra8_board_camera_sccb_write_reg(), s_last_sccb_err, s_last_sccb_op, and s_last_sccb_reg.

Referenced by sensor_bind().

Variable Documentation

◆ s_camera_initialized

bool s_camera_initialized
static

◆ s_codec

ra8_camera_codec_t s_codec
static

Definition at line 72 of file c6_cam_camera.c.

◆ s_jpeg

uint8_t s_jpeg[k_sensor_jpeg_buffer_bytes]
static

Definition at line 68 of file c6_cam_camera.c.

◆ s_last_jpeg_status

ra8_ov5640_jpeg_status_t s_last_jpeg_status
static

◆ s_last_jpeg_status_err

ra8_err_t s_last_jpeg_status_err
static

◆ s_last_sccb_err

◆ s_last_sccb_op

◆ s_last_sccb_reg

uint16_t s_last_sccb_reg
static

◆ s_sensor

◆ s_sensor_bound

bool s_sensor_bound
static

◆ s_sensor_streaming

bool s_sensor_streaming
static

◆ s_source

ra8_camera_source_t s_source
static

Definition at line 70 of file c6_cam_camera.c.

◆ s_source_state

ra8_camera_source_ceu_state_t s_source_state
static

Definition at line 71 of file c6_cam_camera.c.