|
|
@@ -23,7 +23,11 @@ static uint8_t const crc8x_table[] = {
|
|
|
};
|
|
|
|
|
|
|
|
|
-uint8_t crc8x_fast(void const *mem, uint16_t len) {
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+/* ------------------ PRIVATE FUNCTIONS-------------------- */
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+
|
|
|
+static uint8_t crc8x_fast(void const *mem, uint16_t len) {
|
|
|
uint8_t crc = CRC8_INIT_VALUE;
|
|
|
uint8_t const *data = (uint8_t*)mem;
|
|
|
if (data == NULL)
|
|
|
@@ -34,27 +38,13 @@ uint8_t crc8x_fast(void const *mem, uint16_t len) {
|
|
|
return crc;
|
|
|
}
|
|
|
|
|
|
-void nbus_init(Peripheral_typeDef *periph){
|
|
|
- nBus.periph = periph;
|
|
|
-
|
|
|
- HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
|
|
|
- HAL_TIM_RegisterCallback(nBus.periph->measure_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed_base);
|
|
|
- HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
|
|
|
|
|
|
- nBus.rx_length = 0;
|
|
|
- nBus.data_timebase = 0;
|
|
|
- nBus.measure_active = MEASURE_STOPPED;
|
|
|
|
|
|
- // init restart timer
|
|
|
- nBus.uart_state = UART_RX_1B;
|
|
|
- HAL_TIM_Base_Start_IT(periph->uart_timer);
|
|
|
- HAL_TIM_Base_Stop_IT(periph->uart_timer);
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-inline void receiveOneByte(){
|
|
|
+inline static void receiveOneByte(){
|
|
|
+ HAL_UART_AbortReceive_IT(nBus.periph->huart);
|
|
|
HAL_UART_Receive_IT(nBus.periph->huart, nBus.rx_buffer, 1);
|
|
|
nBus.uart_state = UART_RX_1B;
|
|
|
+ nBus.rx_length = 1;
|
|
|
}
|
|
|
|
|
|
inline static void receiveNBytes(uint8_t n){
|
|
|
@@ -69,43 +59,14 @@ inline static void send_response(){
|
|
|
#if USE_USART_DMA_TX == 1
|
|
|
HAL_UART_Transmit_DMA(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length);
|
|
|
#else
|
|
|
- HAL_UART_Transmi(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length, 10);
|
|
|
+ HAL_UART_Transmit(nBus.periph->huart, nBus.tx_buffer, nBus.tx_length, 10);
|
|
|
#endif
|
|
|
}
|
|
|
+ HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
|
|
|
}
|
|
|
|
|
|
-void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
|
|
|
- if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
- nBus.uart_state = UART_RECEIVED;
|
|
|
- HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
|
|
|
- HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
- }
|
|
|
-
|
|
|
- if(nBus.uart_state == UART_RX_1B) {
|
|
|
- HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
|
|
|
- receiveNBytes(nBus.rx_buffer[0]);
|
|
|
-
|
|
|
- HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
- nBus.periph->uart_timer->Instance->CNT = 1;
|
|
|
- HAL_TIM_Base_Start_IT(nBus.periph->uart_timer);
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
-void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim) {
|
|
|
- HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
|
|
|
-}
|
|
|
-
|
|
|
-void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
|
|
|
- if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
- HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
- HAL_UART_AbortReceive_IT(nBus.periph->huart);
|
|
|
- receiveOneByte();
|
|
|
- }
|
|
|
-}
|
|
|
|
|
|
static nBusCommandType_t get_request_type(){
|
|
|
-
|
|
|
nBus.addressModule = nBus.rx_buffer[0];
|
|
|
nBus.sensorInfo = *(const nBus_sensorByte_t*)&nBus.rx_buffer[1];
|
|
|
nBus.function_code = *(const nBus_functionCode_t*)&nBus.rx_buffer[2];
|
|
|
@@ -129,9 +90,8 @@ static nBusCommandType_t get_request_type(){
|
|
|
return BROADCAST_GLOBAL;
|
|
|
}
|
|
|
|
|
|
-void process_request(){
|
|
|
+static void process_request(){
|
|
|
nBusCommandType_t request_type = get_request_type();
|
|
|
- nBus_functionCode_t *fc = &nBus.function_code;
|
|
|
nBus.send_response = SEND_RESPONSE;
|
|
|
|
|
|
nBus.tx_buffer[0] = 0;
|
|
|
@@ -156,28 +116,98 @@ void process_request(){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- fc->error = 0;
|
|
|
+ nBus.function_code.error = 0;
|
|
|
nBus.tx_length = META_SIZE;
|
|
|
|
|
|
if(nBus.function_code.notReadWrite == REQUEST_GET) {
|
|
|
if(request_type == UNICAST_TO_SENSOR) {
|
|
|
- fc = nbus_unicastToSensor(&nBus, fc);
|
|
|
+ nbus_unicastToSensorGet(&nBus);
|
|
|
}
|
|
|
|
|
|
if(request_type == UNICAST_TO_MODULE) {
|
|
|
- fc = nbus_unicastToModule(&nBus, fc);
|
|
|
+ nbus_unicastToModuleGet(&nBus);
|
|
|
}
|
|
|
|
|
|
- nBus.tx_buffer[3] = *(uint8_t*)&fc;
|
|
|
- nBus.tx_buffer[nBus.tx_length-1] = crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length-2);
|
|
|
- nBus.tx_buffer[0] = nBus.tx_length;
|
|
|
+ } else {
|
|
|
+ // else request is REQUEST_SET
|
|
|
+ if(request_type == UNICAST_TO_SENSOR) {
|
|
|
+ nbus_unicastToSensorSet(&nBus);
|
|
|
+ }
|
|
|
|
|
|
- } else { // else request if REQUEST_SET
|
|
|
+ if(request_type == UNICAST_TO_MODULE) {
|
|
|
+ nbus_unicastToModuleSet(&nBus);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ nBus.tx_buffer[3] = *(uint8_t*)&nBus.function_code;
|
|
|
+ nBus.tx_buffer[nBus.tx_length-1] = crc8x_fast(&nBus.tx_buffer[1], nBus.tx_length-2);
|
|
|
+ nBus.tx_buffer[0] = nBus.tx_length;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+/* ----------------------- CALLBACKS----------------------- */
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+static void nbus_cb_UART_RX(UART_HandleTypeDef *huart){
|
|
|
+ if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
+ nBus.uart_state = UART_RECEIVED;
|
|
|
+ HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(nBus.uart_state == UART_RX_1B) {
|
|
|
+ HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_SET);
|
|
|
+ if(nBus.rx_buffer[0] > 0) {
|
|
|
+ receiveNBytes(nBus.rx_buffer[0]);
|
|
|
+
|
|
|
+ HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
+ nBus.periph->uart_timer->Instance->CNT = 1;
|
|
|
+ nBus.periph->uart_timer->Instance->ARR = 40*nBus.rx_buffer[0] + 400;
|
|
|
+ HAL_TIM_Base_Start_IT(nBus.periph->uart_timer);
|
|
|
+ } else {
|
|
|
+ receiveOneByte();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
+static void nbus_cb_TIM_periodElapsed_base(TIM_HandleTypeDef *htim) {
|
|
|
+ HAL_GPIO_TogglePin(nBus.periph->led->port, nBus.periph->led->pin);
|
|
|
+}
|
|
|
+
|
|
|
+static void nbus_cb_TIM_periodElapsed(TIM_HandleTypeDef *htim) {
|
|
|
+ if(nBus.uart_state == UART_RX_PAYLOAD) {
|
|
|
+ HAL_GPIO_WritePin(nBus.periph->led->port, nBus.periph->led->pin, GPIO_PIN_RESET);
|
|
|
+ HAL_TIM_Base_Stop_IT(nBus.periph->uart_timer);
|
|
|
+ HAL_UART_AbortReceive_IT(nBus.periph->huart);
|
|
|
+ receiveOneByte();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+/* ------------------ PUBLIC FUNCTIONS-------------------- */
|
|
|
+/* -------------------------------------------------------- */
|
|
|
+
|
|
|
+void nbus_init(Peripheral_typeDef *periph){
|
|
|
+ nBus.periph = periph;
|
|
|
+
|
|
|
+ HAL_TIM_RegisterCallback(nBus.periph->uart_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed);
|
|
|
+ HAL_TIM_RegisterCallback(nBus.periph->measure_timer, HAL_TIM_PERIOD_ELAPSED_CB_ID, nbus_cb_TIM_periodElapsed_base);
|
|
|
+ HAL_UART_RegisterCallback(nBus.periph->huart, HAL_UART_RX_COMPLETE_CB_ID, nbus_cb_UART_RX);
|
|
|
+
|
|
|
+ nBus.rx_length = 0;
|
|
|
+ nBus.data_timebase = 0;
|
|
|
+ nBus.measure_active = MEASURE_STOPPED;
|
|
|
+
|
|
|
+ // init restart timer
|
|
|
+ nBus.uart_state = UART_RX_1B;
|
|
|
+ HAL_TIM_Base_Start_IT(periph->uart_timer);
|
|
|
+ HAL_TIM_Base_Stop_IT(periph->uart_timer);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
void nbus_stack(void){
|
|
|
receiveOneByte();
|
|
|
|