Skip to content

Openofdm tx.c

Function reg_read(u32 reg)

reg_read: The function of reg_read is to read a 32-bit value from a specified register address.

parameters: The parameters of this Function. · parameter1: u32 reg - This parameter represents the offset of the register from the base address.

Code Description: The reg_read function is defined as a static inline function, which means it is intended for use only within the file it is defined in and may be optimized by the compiler for performance. The function takes a single parameter, reg, which is an unsigned 32-bit integer representing the offset of the register to be read. Inside the function, the ioread32 function is called with the expression (base_addr + reg) as its argument. Here, base_addr is a predefined pointer that points to the base address of the memory-mapped I/O region where the registers are located. The ioread32 function is responsible for reading a 32-bit value from the specified memory address, which is calculated by adding the reg offset to the base address. The result of this read operation is then returned by the reg_read function.

Note: It is important to ensure that the base_addr is correctly initialized and points to a valid memory-mapped I/O region before calling this function. Additionally, the reg parameter should be within the valid range of offsets for the registers being accessed to avoid undefined behavior.

Output Example: If the base_addr is set to 0x1000 and reg is 0x04, the function call reg_read(0x04) would read the value from the memory address 0x1004 and return it as a 32-bit unsigned integer. For instance, if the value at that address is 0xDEADBEEF, the function would return 0xDEADBEEF.

graph TD
    A[Start reg_read function] --> B[Receive register address]
    B --> C[Calculate effective address by adding base address]
    C --> D[Read value from memory using ioread32]
    D --> E[Return read value]
    E --> F[End reg_read function]

Function reg_write(u32 reg, u32 value)

reg_write: The function of reg_write is to write a specified value to a given hardware register.

parameters: The parameters of this Function. · parameter1: u32 reg - This parameter represents the offset of the register to which the value will be written. It is an unsigned 32-bit integer that specifies the location of the register in the device's memory-mapped I/O space.
· parameter2: u32 value - This parameter is the value to be written to the specified register. It is also an unsigned 32-bit integer that contains the data intended for the hardware register.

Code Description: The reg_write function is defined as a static inline function, which means it is intended to be used only within the file it is defined in and may be optimized by the compiler for performance. The function takes two parameters: 'reg' and 'value'. Inside the function, the iowrite32 function is called, which is responsible for writing a 32-bit value to a specific memory address. The address is calculated by adding the 'reg' offset to a base address, 'base_addr', which is assumed to be defined elsewhere in the code. This operation effectively writes the 'value' to the hardware register located at the computed address. The use of inline functions can improve performance by reducing function call overhead, especially in scenarios where this function is called frequently.

Note: It is important to ensure that the 'base_addr' is properly initialized before calling this function, as writing to an uninitialized or incorrect address may lead to undefined behavior or hardware malfunctions. Additionally, the function assumes that the hardware supports 32-bit register access, and care should be taken to ensure that the value being written is appropriate for the specific register being targeted.

graph TD
    A[Start reg_write function] --> B[Receive register address reg]
    B --> C[Receive value to write]
    C --> D[Calculate target address by adding base_addr and reg]
    D --> E[Write value to calculated address using iowrite32]
    E --> F[End reg_write function]

Function OPENOFDM_TX_REG_MULTI_RST_write(u32 Data)

OPENOFDM_TX_REG_MULTI_RST_write: The function of OPENOFDM_TX_REG_MULTI_RST_write is to write a specified value to the OPENOFDM_TX_REG_MULTI_RST register.

parameters: The parameters of this Function. · Data: A 32-bit unsigned integer that represents the value to be written to the register.

Code Description: The function OPENOFDM_TX_REG_MULTI_RST_write is defined as a static inline function, which means it is intended to be used only within the file it is defined in and may be optimized by the compiler for performance. The function takes a single parameter, Data, which is a 32-bit unsigned integer. Inside the function, the reg_write function is called with two arguments: OPENOFDM_TX_REG_MULTI_RST_ADDR and Data.

OPENOFDM_TX_REG_MULTI_RST_ADDR is a predefined constant that specifies the memory address of the OPENOFDM_TX_REG_MULTI_RST register. The reg_write function is responsible for writing the value of Data to the specified address. This operation is typically used in hardware programming to configure or reset specific hardware components associated with the OPENOFDM transmitter.

Note: It is important to ensure that the value being written to the register is valid and appropriate for the specific hardware context. Improper values may lead to undefined behavior or hardware malfunctions. Additionally, since this function is defined as inline, it is recommended to use it in performance-critical sections of code where register access is frequent.

graph TD
    A[Start] --> B[Call OPENOFDM_TX_REG_MULTI_RST_write with Data]
    B --> C[Execute reg_write function]
    C --> D[Write Data to OPENOFDM_TX_REG_MULTI_RST_ADDR]
    D --> E[End]

Function OPENOFDM_TX_REG_INIT_PILOT_STATE_write(u32 Data)

OPENOFDM_TX_REG_INIT_PILOT_STATE_write: The function of OPENOFDM_TX_REG_INIT_PILOT_STATE_write is to write a specified data value to the OPENOFDM_TX_REG_INIT_PILOT_STATE register.

parameters: The parameters of this Function. · Data: A 32-bit unsigned integer (u32) that represents the value to be written to the register.

Code Description: The OPENOFDM_TX_REG_INIT_PILOT_STATE_write function is defined as a static inline function, which means it is intended for use only within the file it is defined in and may be optimized by the compiler for performance. The function takes a single parameter, Data, which is expected to be a 32-bit unsigned integer.

Inside the function, the reg_write function is called with two arguments: the address of the OPENOFDM_TX_REG_INIT_PILOT_STATE register (defined as OPENOFDM_TX_REG_INIT_PILOT_STATE_ADDR) and the Data value. The reg_write function is responsible for performing the actual write operation to the specified register address with the provided data. This operation is crucial for initializing or configuring the pilot state in the Open OFDM transmitter.

Note: It is important to ensure that the Data value being written is valid and appropriate for the intended operation of the OPENOFDM_TX_REG_INIT_PILOT_STATE register. Improper values may lead to undefined behavior or incorrect operation of the transmitter. Additionally, since this function is static inline, it should be used in contexts where performance is critical, as it may reduce function call overhead.

graph TD
    A[Start] --> B[Receive Data]
    B --> C[Call reg_write function]
    C --> D[Write Data to OPENOFDM_TX_REG_INIT_PILOT_STATE_ADDR]
    D --> E[End]

Function OPENOFDM_TX_REG_INIT_DATA_STATE_write(u32 Data)

OPENOFDM_TX_REG_INIT_DATA_STATE_write: The function of OPENOFDM_TX_REG_INIT_DATA_STATE_write is to write a specified data value to the OPENOFDM_TX_REG_INIT_DATA_STATE register.

parameters: The parameters of this Function. · Data: A 32-bit unsigned integer (u32) that represents the data value to be written to the register.

Code Description: The OPENOFDM_TX_REG_INIT_DATA_STATE_write function is defined as a static inline function, which means it is intended for use only within the file it is defined in and may be optimized by the compiler for performance. The function takes a single parameter, Data, which is a 32-bit unsigned integer. Inside the function, the reg_write function is called with two arguments: the address of the OPENOFDM_TX_REG_INIT_DATA_STATE register (OPENOFDM_TX_REG_INIT_DATA_STATE_ADDR) and the Data value. This operation effectively writes the provided Data value to the specified register address, allowing for the configuration or initialization of the register's state.

Note: It is important to ensure that the Data value being written is valid and appropriate for the specific use case of the OPENOFDM_TX_REG_INIT_DATA_STATE register. Improper values may lead to undefined behavior or incorrect operation of the system. Additionally, since this function is static inline, it should be used in contexts where performance is critical, as it may reduce function call overhead.

graph TD
    A[Start] --> B[Call OPENOFDM_TX_REG_INIT_DATA_STATE_write]
    B --> C[Receive Data parameter]
    C --> D[Execute reg_write function]
    D --> E[Write Data to OPENOFDM_TX_REG_INIT_DATA_STATE_ADDR]
    E --> F[End]

Function hw_init(enum openofdm_tx_mode mode)

hw_init: The function of hw_init is to initialize the hardware for the OpenOFDM transmitter based on the specified mode.

parameters: The parameters of this Function. · mode: An enumeration value of type openofdm_tx_mode that specifies the initialization mode. It can be either OPENOFDM_TX_TEST or OPENOFDM_TX_NORMAL.

Code Description: The hw_init function begins by logging the initialization mode using the printk function, which outputs a formatted string to the kernel log. It checks the provided mode against predefined cases: OPENOFDM_TX_TEST and OPENOFDM_TX_NORMAL. If the mode is valid, it logs the corresponding message; if the mode is invalid, it logs an error message and sets the error variable err to 1.

The function then proceeds to reset the hardware registers. It performs a series of writes to the OPENOFDM_TX_REG_MULTI_RST register, first writing 0 eight times, then writing 0xFFFFFFFF thirty-two times, and finally writing 0 eight more times. This sequence is intended to ensure that the hardware is properly reset.

After the reset operations, the function initializes two specific registers: OPENOFDM_TX_REG_INIT_PILOT_STATE and OPENOFDM_TX_REG_INIT_DATA_STATE, both set to the value 0x7F. This step configures the initial state of the pilot and data signals for the transmitter.

Finally, the function logs the value of the error variable err before returning it. A return value of 0 indicates successful initialization, while a return value of 1 indicates an error due to an invalid mode.

Note: It is important to ensure that the mode passed to the hw_init function is valid to avoid erroneous behavior. The function is designed to be called in contexts where hardware initialization is required, and proper error handling should be implemented based on the return value.

Output Example: If the function is called with OPENOFDM_TX_TEST, the expected return value would be 0, indicating successful initialization. If called with an invalid mode, such as OPENOFDM_TX_INVALID, the return value would be 1, indicating an error.

graph TD
    A[Start hw_init function] --> B[Print hw_init mode]
    B --> C{Check mode}
    C -->|OPENOFDM_TX_TEST| D[Print hw_init mode OPENOFDM_TX_TEST]
    C -->|OPENOFDM_TX_NORMAL| E[Print hw_init mode OPENOFDM_TX_NORMAL]
    C -->|Default| F[Print hw_init mode is wrong]
    F --> G[Set err to 1]
    D --> H[Reset registers]
    E --> H
    G --> H
    H --> I[Write 0 to OPENOFDM_TX_REG_MULTI_RST 8 times]
    I --> J[Write 0xFFFFFFFF to OPENOFDM_TX_REG_MULTI_RST 32 times]
    J --> K[Write 0 to OPENOFDM_TX_REG_MULTI_RST 8 times]
    K --> L[Write 0x7F to OPENOFDM_TX_REG_INIT_PILOT_STATE]
    L --> M[Write 0x7F to OPENOFDM_TX_REG_INIT_DATA_STATE]
    M --> N[Print hw_init err]
    N --> O[Return err]
    O --> P[End hw_init function]

Function dev_probe(struct platform_device *pdev)

dev_probe: The function of dev_probe is to initialize the device driver for the OpenOFDM transmitter.

parameters: The parameters of this Function. · pdev: A pointer to the platform_device structure representing the device being probed.

Code Description: The dev_probe function is responsible for initializing the OpenOFDM transmitter device driver when the device is detected by the system. It begins by retrieving the device node associated with the platform device (pdev) and initializes an error variable (err) to 1, indicating failure by default.

The function checks if the device node (np) is valid. If it is, it attempts to match the device node with a predefined list of compatible device IDs (dev_of_ids). If a match is found, it logs a success message and sets the error variable (err) to 0, indicating that the probe was successful.

If no match is found, the function returns the error variable, which remains set to 1. If the probe is successful, the function proceeds to initialize various function pointers in the openofdm_tx_api structure, linking them to their respective hardware interaction functions such as hw_init, reg_read, and reg_write.

Next, the function requests and maps the I/O memory for the device by calling platform_get_resource to retrieve the memory resource and devm_ioremap_resource to map it. If the mapping fails (indicated by IS_ERR), the function returns the error code obtained from PTR_ERR.

The function then logs detailed information about the I/O memory, including its start and end addresses, name, flags, and description. It also logs the base address of the mapped I/O memory and the addresses of the driver API instance and the API structure itself.

Finally, the function logs a success message and calls the hw_init function with the parameter OPENOFDM_TX_NORMAL to perform any necessary hardware initialization. The return value of hw_init is then returned as the final result of the dev_probe function.

Note: It is important to ensure that the device node is properly matched with the compatible device IDs to avoid initialization failures. Additionally, proper error handling should be implemented to manage potential failures during memory mapping.

Output Example: A successful return value from dev_probe could be 0, indicating that the device was successfully probed and initialized. If there is an error, the function may return 1 or another error code indicating the specific failure encountered during the probe process.

graph TD
    A[Start dev_probe function] --> B[Get device node from platform device]
    B --> C{Check if device node exists}
    C -->|Yes| D[Match device node with device IDs]
    C -->|No| E[Return error 1]
    D --> F{Check if match found}
    F -->|Yes| G[Print match message]
    F -->|No| H[Return error 1]
    G --> I[Set hardware initialization function]
    I --> J[Set register read and write functions]
    J --> K[Request and map I/O memory]
    K --> L[Check if mapping is successful]
    L -->|Yes| M[Print I/O memory details]
    M --> N[Print base address and driver API instance details]
    N --> O[Print success message]
    O --> P[Call hardware initialization function]
    L -->|No| Q[Return error from mapping]
    P --> R[Return result from hardware initialization]
    E --> R
    Q --> R

Function dev_remove(struct platform_device *pdev)

dev_remove: The function of dev_remove is to handle the removal of a device from the platform.

parameters: The parameters of this Function. · parameter1: struct platform_device *pdev - A pointer to the platform device structure representing the device to be removed.

Code Description: The dev_remove function is a static function that is invoked when a device is being removed from the platform. It takes a single parameter, pdev, which is a pointer to the platform_device structure associated with the device being removed.

Upon invocation, the function begins by printing a newline character to the console for formatting purposes. It then proceeds to log several pieces of information related to the device being removed. The first log statement outputs the base address of the device, which is cast to a 32-bit unsigned integer for display purposes. This is followed by logging the address of the openofdm_tx_driver_api_inst instance, which is also cast to a 32-bit unsigned integer. Finally, the function logs the address of the openofdm_tx_api.

Each log statement includes a prefix that identifies the context of the log, specifically using the openofdm_tx_compatible_str variable, which likely contains a string that identifies the driver or device type. After logging the relevant information, the function concludes by printing a success message indicating that the device removal process has completed successfully. The function then returns 0, which typically indicates success in C programming.

Note: It is important to ensure that any resources allocated to the device are properly released in conjunction with this function, as this implementation does not handle resource cleanup. Additionally, this function should be called in the appropriate context where the device is no longer needed.

Output Example: A possible appearance of the code's return value could be:

openofdm_tx: dev_remove base_addr 0x00000000
openofdm_tx: dev_remove openofdm_tx_driver_api_inst 0x12345678
openofdm_tx: dev_remove             openofdm_tx_api 0x87654321
openofdm_tx: dev_remove succeed!

graph TD
    A[Start dev_remove function] --> B[Print newline]
    B --> C[Print base_addr with openofdm_tx_compatible_str]
    C --> D[Print openofdm_tx_driver_api_inst with openofdm_tx_compatible_str]
    D --> E[Print openofdm_tx_api with openofdm_tx_compatible_str]
    E --> F[Print success message with openofdm_tx_compatible_str]
    F --> G[Return 0]
    G --> H[End dev_remove function]