cueOS  2.4
cueOS - Universal Show Control OS for ARM
DMX512_driver.c
1/***============================================================================================================================
2 * Depedencies inclusion
3 * Necessary dependencies should be declared here. Header file should contain as little dependecies declarations as possible
4 *=============================================================================================================================*/
5
6#include <string.h>
7#include "DMX512_driver.h"
8#include "cmsis_os.h"
9
10
11/***============================================================================================================================
12 * Private variables definitions
13 * These variables are only accessible from within the file's scope
14 *=============================================================================================================================*/
15
17static osThreadId_t DMX512_driver_ThreadId = NULL;
18
19
20/***============================================================================================================================
21 * Private functions definitions
22 * These functions are only accessible from within the file's scope
23 *=============================================================================================================================*/
24
31static void _DMX512_driver_send_frame(void){
32 this.uart.Init.BaudRate = DMX512_FRAME_BAUDRATE;
33 this.uart.Init.StopBits = UART_STOPBITS_2;
34 HAL_UART_Init(&this.uart);
35 HAL_UART_Transmit(&this.uart, this.value_buffer, DMX512_FRAME_BYTESIZE, HAL_MAX_DELAY);
36}
37
47static void _DMX512_driver_send_breakmab(void){
48 this.uart.Init.BaudRate = DMX512_BREAKMAB_BAUDRATE;
49 this.uart.Init.StopBits = UART_STOPBITS_1;
50 HAL_UART_Init(&this.uart);
51 HAL_UART_Transmit(&this.uart, DMX512_BREAKMAB_BYTEDATA, DMX512_BREAKMAB_BYTESIZE, 0x0001);
52}
53
60static void _DMX512_driver_thread(void *arg){
61 for(;;){
62 _DMX512_driver_send_breakmab();
63 _DMX512_driver_send_frame();
64 osDelay(23);
65 }
66}
67
74uint8_t _DMX512_driver_address_check(uint16_t address){
75 return (address < DMX512_CHANNEL_ADDRESS_MIN || address > DMX512_CHANNEL_ADDRESS_MAX) ? 0 : 1;
76}
77
82void _DMX512_driver_GPIO_init(void){
83
84 __USART1_CLK_ENABLE();
85 __GPIOB_CLK_ENABLE();
86
87 GPIO_InitTypeDef GPIO_InitStructure;
88
89 GPIO_InitStructure.Pin = GPIO_PIN_6;
90 GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
91 GPIO_InitStructure.Alternate = GPIO_AF7_USART1;
92 GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
93 GPIO_InitStructure.Pull = GPIO_NOPULL;
94 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
95
96 GPIO_InitStructure.Pin = GPIO_PIN_7;
97 GPIO_InitStructure.Mode = GPIO_MODE_AF_OD;
98 HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
99
100}
101
107void _DMX512_driver_UART_init(void){
108
109 this.uart.Instance = USART1;
110 this.uart.Init.WordLength = UART_WORDLENGTH_8B;
111 this.uart.Init.StopBits = UART_STOPBITS_2;
112 this.uart.Init.Parity = UART_PARITY_NONE;
113 this.uart.Init.Mode = UART_MODE_TX_RX;
114 this.uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
115 this.uart.Init.OverSampling = UART_OVERSAMPLING_16;
116
117 HAL_UART_Init(&this.uart);
118
119}
120
121/***============================================================================================================================
122 * Public functions definitions
123 * These functions can be accessed outside of the file's scope
124 * @see DMX512_driver.h for declarations
125 *=============================================================================================================================*/
126
134 if(this.status == DMX512_DRIVER_UNINITIALISED){
135 _DMX512_driver_GPIO_init();
136 _DMX512_driver_UART_init();
137 this.status = DMX512_DRIVER_INITIALISED;
138 }
139}
140
148 return this.status;
149}
150
158
159 const osThreadAttr_t DMX512_driver_thread_attr = {
160 .priority = osPriorityNormal,
161 .stack_size = configMINIMAL_STACK_SIZE*4
162 };
163
164 DMX512_driver_ThreadId = osThreadNew(_DMX512_driver_thread, NULL, &DMX512_driver_thread_attr);
165 return DMX512_driver_ThreadId != NULL ? DMX512_DRIVER_OK : DMX512_DRIVER_THREAD_ERR;
166
167}
168
176 return osThreadTerminate(DMX512_driver_ThreadId) == osOK ? DMX512_DRIVER_OK : DMX512_DRIVER_THREAD_ERR;
177}
178
187DMX512_driver_err_e DMX512_driver_set_single(uint16_t address, uint8_t value){
188
189 DMX512_driver_err_e err = DMX512_DRIVER_OK;
190
191 if(!_DMX512_driver_address_check(address)){
192 err = DMX512_DRIVER_ADDRESS_OUT_OF_BOUNDS;
193 }else{
194 this.value_buffer[address] = value;
195 }
196
197 return err;
198}
#define DMX512_CHANNEL_ADDRESS_MAX
DMX512 max channel address.
Definition: DMX512_defs.h:13
DMX512_driver_err_e DMX512_driver_set_single(uint16_t address, uint8_t value)
Sets a buffer's address to a given value.
#define DEFAULT_DMX512_DRIVER
DMX512 driver object default values.
Definition: DMX512_driver.h:29
#define DMX512_BREAKMAB_BYTESIZE
Definition: DMX512_driver.h:18
DMX512_driver_err_e
QLSF DMX512 driver error constants.
Definition: DMX512_driver.h:39
DMX512_driver_status_e DMX512_driver_get_status(void)
Returns the status of the DMX512 driver.
#define DMX512_FRAME_BYTESIZE
Definition: DMX512_driver.h:19
#define DMX512_BREAKMAB_BYTEDATA
Definition: DMX512_driver.h:20
#define DMX512_FRAME_BAUDRATE
Definition: DMX512_driver.h:17
#define DMX512_BREAKMAB_BAUDRATE
Definition: DMX512_driver.h:16
DMX512_driver_status_e
Status of the DMX512 driver instance.
Definition: DMX512_driver.h:52
DMX512_driver_err_e DMX512_driver_start(void)
Starts the DMX512 driver thread.
void DMX512_driver_init(void)
Initialises DMX512 driver periphals.
DMX512_driver_err_e DMX512_driver_stop(void)
Terminates the DMX512 driver thread.
Defines a DMX512 driver instance.
Definition: DMX512_driver.h:66