This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:multiasm:paarm:chapter_5_9 [2025/12/04 14:22] – eriks.klavins | en:multiasm:paarm:chapter_5_9 [2025/12/04 14:59] (current) – [Pulse Width Modulation] eriks.klavins | ||
|---|---|---|---|
| Line 133: | Line 133: | ||
| Overall, working with any peripheral requires studying its documentation to identify its base addresses and the offset addresses needed to control it. | Overall, working with any peripheral requires studying its documentation to identify its base addresses and the offset addresses needed to control it. | ||
| - | ===== General Purpose Inputs | + | ===== Communication interfaces ===== |
| + | |||
| + | The hardware can be used to send and receive a byte through I2C. The hardware itself performs all the control over digital signals. Everything that is needed again, find the base addresses for the hardware. Unfortunately, | ||
| + | |||
| + | In chapter 2 of the [[https:// | ||
| + | |||
| + | First, it is necessary to determine which interfaces are available on the Raspberry Pi and, of course, to find the device addresses. Another option is to use Linux. To use any communication interface, it must be enabled in the OS. The easiest way is to use the standard Linux i.e. i2c-dev interface, | ||
| + | * open("/ | ||
| + | * write(fd, & | ||
| + | * read(fd, & | ||
| + | |||
| + | In the assembler code, the I2C device must be opened by calling the OS system function “'' | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | path_i2c: | ||
| + | .asciz "/ | ||
| + | .section .text | ||
| + | .global _start | ||
| + | _start: | ||
| + | MOV X0, #-100 @ #AT_FDCWD | ||
| + | LDR X1, =path_i2c | ||
| + | MOV X2, #2 @ O_RDWR - read and write | ||
| + | MOV X3, #0 | ||
| + | MOV X8, #56 @ # | ||
| + | SVC #0 | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | After the system call is executed, the ''< | ||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | I2Cbyte: | ||
| + | .byte 0xAB | ||
| + | MOV X0, X19 | ||
| + | LDR X1, =I2Cbyte | ||
| + | MOV X2, #1 @ nuber of bytes | ||
| + | MOV X8, # | ||
| + | SVC #0 | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | The code sends the data; the device address is not set in these examples. The system call with number ''< | ||
| + | |||
| + | To unlock the full potential of any communication interface on Raspberry Pi, it will take a lot of effort to find the register addresses, digital lines, and their parameters, and even then, something will be missing. For example, the same code that works on Raspberry Pi version 3 or 4 will not work on version 5. The hardware addresses differ, and it seems the Operating System is translating them into 32-bit addresses. The communication interface depends heavily on the Operating System kernel version, kernel modules, and configuration. It is recommended to use system calls for other communication interfaces as well, as experimenting with the hardware without complete documentation is not recommended. As a result, the code may take harmful actions, damaging the Raspberry Pi. | ||
| + | |||
| + | ===== Pulse Width Modulation ===== | ||
| + | |||
| + | Basically, with a single | ||
| + | |||
| + | In Raspberry Pi 5, the RP1 chip documentation contains much more information on pulse-width modulation than on basic communication interfaces. PWM registers are on the internal peripheral bus; the base addresses for PWM0 and PWM1 are '' | ||
| + | |||
| + | Before proceeding, the base addresses must be checked at least three times (**NO JOKES**) and, if needed, replaced. The PWM0_BASE and IO_BANK0_BASE addresses are already mapped and known for the Raspberry Pi 5. In the example, the GPIO line 18 will be used. | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | .equ PWM0_BASE, | ||
| + | .equ IO_BANK0_BASE, | ||
| + | .equ PWM_CHAN2_CTRL, | ||
| + | .equ PWM_CHAN2_RANGE, | ||
| + | .equ PWM_CHAN2_DUTY, | ||
| + | .equ GPIO18_CTRL, | ||
| + | .equ FUNC_PWM0_2, | ||
| + | </ | ||
| + | </ | ||
| + | These are the constants used later on in the code. Note that three constants are holding dummy values – these values depend on the hardware. The rest of the code will control GPIO18 and generate a pulse at the specified frequency and duty cycle. The frequency and duty cycle parameters can be passed to the code. The ''< | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | .global pwm_init_chan2 | ||
| + | pwm_init_chan2: | ||
| + | LDR | ||
| + | ADD X2, X2, # | ||
| + | LDR W3, [X2] @ read current CTRL | ||
| + | BIC W3, W3, #0x1f @ clear FUNCSEL bits [4:0] | ||
| + | ORR W3, W3, # | ||
| + | STR W3, [X2] | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | At this moment, the GPIO line is ready and internally connected to the PWM generator. The following code lines provide the PWM generator with all the necessary parameters: period and duty cycle. | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | LDR | ||
| + | ADD X5, X4, # | ||
| + | STR W0, [X5] @ low 32 bits used | ||
| + | ADD X5, X4, # | ||
| + | STR W1, [X5] | ||
| + | </ | ||
| + | </ | ||
| + | Note that the code uses only 32-bit values of the ''< | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | ADD X5, X4, # | ||
| + | LDR W6, [X5] | ||
| + | @ Check the datasheet -> clear existing MODE bits (example) | ||
| + | BIC W6, W6, #(0x7) | ||
| + | @ set mode to trailing-edge | ||
| + | ORR W6, W6, #0x1 | ||
| + | @ set enable bit (placeholder) check datasheet | ||
| + | ORR W6, W6, #(1 << 8) | ||
| + | STR W6, [X5] | ||
| + | RET | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | This code can be used as a kernel module, but it cannot be executed directly from a regular user program on Pi OS. That’s because the mapping is involved, and with that, the kernel is also involved (because of the PCIe mapping). | ||
| + | |||
| + | ** The second approach ** | ||
| + | |||
| + | The second approach is similar to I2C communication: | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | @ void write_file(const char *path, const char *str) | ||
| + | write_file: | ||
| + | @ x0 = path, x1 = string | ||
| + | @ openat(AT_FDCWD, | ||
| + | MOV X2, #O_WRONLY | ||
| + | MOV X3, #0 | ||
| + | MOV X8, # | ||
| + | MOV X4, #AT_FDCWD | ||
| + | MOV X0, X4 @ AT_FDCWD | ||
| + | @ x1 already holds path | ||
| + | SVC # | ||
| + | MOV X19, X0 @ save fd | ||
| + | @ find string length | ||
| + | MOV X0, X1 @ pointer to string | ||
| + | 1: LDRB W2, [X0], #1 | ||
| + | CBZ W2, 2f @ jump to label 2 (f means forward) | ||
| + | B | ||
| + | 2: | ||
| + | @ now X0 points past NUL; length = (X0 - 1) - str | ||
| + | SUB X2, X0, X1 @ remove the NUL as it is not needed | ||
| + | SUB X2, X2, #1 | ||
| + | @ write(fd, str, len) | ||
| + | MOV X0, X19 | ||
| + | MOV X8, # | ||
| + | SVC #0 | ||
| + | @ close(fd) | ||
| + | MOV X0, X19 | ||
| + | MOV X8, # | ||
| + | SVC #0 | ||
| + | RET | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | Now, the code to activate the PWM generator sets the period and duty. It is necessary to know which file to edit and which values to write to the files. Required constants for this example are: | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | .equ SYS_openat, 56 | ||
| + | .equ SYS_write, | ||
| + | .equ SYS_close, | ||
| + | .equ SYS_exit, | ||
| + | .equ AT_FDCWD, | ||
| + | .equ O_WRONLY, | ||
| + | |||
| + | .section .rodata | ||
| + | |||
| + | path_export: | ||
| + | .asciz "/ | ||
| + | path_period: | ||
| + | .asciz "/ | ||
| + | path_duty: | ||
| + | .asciz "/ | ||
| + | path_enable: | ||
| + | .asciz "/ | ||
| + | str_chan0: | ||
| + | .asciz " | ||
| + | str_period: | ||
| + | .asciz " | ||
| + | str_duty: | ||
| + | .asciz " | ||
| + | str_enable: | ||
| + | .asciz " | ||
| + | </ | ||
| + | </ | ||
| + | And the main code, which sets all values: | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | LDR X0, =path_export | ||
| + | LDR X1, =str_chan0 | ||
| + | BL write_file | ||
| + | |||
| + | LDR X0, =path_period | ||
| + | LDR X1, =str_period | ||
| + | BL write_file | ||
| + | |||
| + | LDR X0, =path_duty | ||
| + | LDR X1, =str_duty | ||
| + | BL write_file | ||
| + | |||
| + | LDR X0, =path_enable | ||
| + | LDR X1, =str_enable | ||
| + | BL write_file | ||
| + | |||
| + | MOV X0, #0 @ exit | ||
| + | MOV X8, #SYS_exit | ||
| + | SVC #0 | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | The code can be upgraded to accept the arguments: period and duty cycle. This would be similar to the write_file function, which takes two arguments. | ||