СИНТАКСИС
#include <hal.h>
bool hal_port_read(hal_port_t port, char* dest, unsigned count);
bool hal_port_peek(hal_port_t port, char* dest, unsigned count);
bool hal_port_peek_commit(hal_port_t port, unsigned count);
unsigned hal_port_readable(hal_port_t port);
void hal_port_clear(hal_port_t port);
bool hal_port_write(hal_port_t port, const char* src, unsigned count);
unsigned hal_port_writable(hal_port_t port);
unsigned hal_port_buffer_size(hal_port_t port);
#ifdef ULAPI
void hal_port_wait_readable(hal_port_t** port, unsigned count, sig_atomic_t* stop);
void hal_port_wait_writable(hal_port_t** port, unsigned count, sig_atomic_t* stop);
#endif
ОПИС
Порт HAL — це контакт HAL, який діє як односторонній байтовий потік даних у режимі реального часу. Вихідний порт будь-якого компонента може бути підключений до вхідного порту будь-якого іншого компонента за допомогою сигналу. Дані, записані на вихідний контакт, стають доступними для вхідного контакту. Сигнал порту HAL може з’єднувати тільки одного записувача та одного зчитувача.
Порт також буферизує дані. Користувачі повинні визначити правильний розмір буфера залежно від передбачуваного застосування.
- hal_port_read()
-
Reads count bytes from the port into destination buffer dest. A call to hal_port_read() will read count bytes if and only if count bytes are available for reading and otherwise it will leave the port unaffected. Returns true if count bytes were read and false otherwise. This function should only be called by the component that owns the IN PORT pin.
- hal_port_peek()
-
Behaves the same as hal_port_read(), however it does not consume bytes from the HAL port. Repeated calls to hal_port_peek() will return the same data. Returns true if count bytes were read and false otherwise. This function should only be called by the component that owns the IN PORT pin.
- hal_port_peek_commit()
-
Advances the read position in the port buffer by count bytes. A hal_port_peek() followed by a hal_port_peek_commit() would function equivalently to hal_port_read() given the same count value. Returns true if count readable bytes were skipped and are no longer accessible and false if no bytes wer skipped. This function should only be called by the component that owns the IN PORT pin.
- hal_port_readable()
-
Returns the number of bytes available for reading from port. It is safe to call this function from any component.
- hal_port_clear()
-
Очищає вказаний порт від усіх даних. hal_port_clear має викликатися лише компонентом, якому належить вивід IN PORT.
- hal_port_write()
-
Записує значення count байтів з src у порт. Повертає значення true, якщо значення count байтів було записано, і false в іншому випадку. Цю функцію слід викликати лише компонентом, якому належить вивід OUT PORT.
- hal_port_writable()
-
Returns the number of bytes that can be written into the port. It is safe to call this function from any component.
- hal_port_buffer_size()
-
Returns the maximum number of bytes that a port can buffer. It is safe to call this function from any component.
- hal_port_wait_readable()
-
Waits until the port has count bytes or more available for reading or the stop flag is set.
This function cannot be called from realtime code. - hal_port_wait_writable()
-
Waits until the port has count bytes or more available for writing or the stop flag is set.
This function cannot be called from realtime code.
АРГУМЕНТИ
- hal_port_t
-
Дескриптор об’єкта порту. Створено користувачем hal_pin_new.
- dest
-
Масив байтів, у який hal_port_read та hal_port_peek копіюватимуть дані. Він має бути виділений викликаючою функцією та мати довжину щонайменше count байтів.
- рахувати
-
Кількість байтів, які hal_port_read, hal_port_peek та hal_port_write скопіюють до dest або з src.
- src
-
Масив байтів, з якого hal_port_write копіюватиме дані в буфер порту. Розмір масиву має бути не менше 1000 байтів.
- стій
-
Вказівник на значення, яке контролюється під час очікування. Якщо воно не дорівнює нулю, операція очікування повертається раніше. Це дозволяє безпечно завершити виклик очікування у разі сигналу.
ЗРАЗОК КОДУ
У дереві вихідного коду під src/hal/components/raster.comp знаходиться компонент реального часу, призначений для керування лазером. src/tests/raster — це тестова програма, яка також програмує растровий компонент з Python.
МІРКУВАННЯ В РЕАЛЬНОМУ ЧАСІ
Most hal_port_*() functions can be called from realtime code.
The exceptions are hal_port_wait_writable() and hal_port_wait_readable(), which may be only called from ULAPI code.