-
Notifications
You must be signed in to change notification settings - Fork 0
Slave hack #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GuyBlumenthal
wants to merge
8
commits into
ryan_misc_changes
Choose a base branch
from
slave_hack
base: ryan_misc_changes
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Slave hack #28
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
045dda5
Initial commit of slave hack stuff
GuyBlumenthal 3546e93
Fixing build
GuyBlumenthal 22dbfc4
divded bms current by 2 lol
RyanJMah 8a450c5
Changed precharge voltage
GuyBlumenthal aa3a608
Added uart interrupt
GuyBlumenthal 33dcb91
UART for slave
GuyBlumenthal ffb075c
final build (hopefully)
RyanJMah 10a7a33
post CHC debug
RyanJMah File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
| #include <stddef.h> | ||
| #include "slave_thread.hpp" | ||
|
|
||
| #define CIRC_RX_BUFF_SIZE 16 | ||
|
|
||
| /* | ||
| Implementation of a circular CircQueue | ||
| */ | ||
|
|
||
| typedef enum { | ||
| CircQueue_GET_EMPTY = 0, | ||
| CircQueue_GET_OK = 1, | ||
| CircQueue_PUT_OK = 2, | ||
| CircQueue_PUT_WRAPAROUND = 3, | ||
| CircQueue_ERROR = 0 | ||
| } CircQueueStatus; | ||
|
|
||
| typedef struct { | ||
| size_t len; | ||
|
|
||
| SlavePkt _arr[CIRC_RX_BUFF_SIZE]; | ||
| size_t _head; | ||
| size_t _tail; | ||
| } CircQueue; | ||
|
|
||
| #define CIRC_INC_HEAD(self) { \ | ||
| if (self->_head == (CIRC_RX_BUFF_SIZE - 1)) { \ | ||
| self->_head = 0; \ | ||
| } \ | ||
| else { \ | ||
| self->_head += 1; \ | ||
| } \ | ||
| } | ||
| #define CIRC_INC_TAIL(self) { \ | ||
| if (self->_tail == (CIRC_RX_BUFF_SIZE - 1)) { \ | ||
| self->_tail = 0; \ | ||
| } \ | ||
| else { \ | ||
| self->_tail += 1; \ | ||
| } \ | ||
| } | ||
|
|
||
| CircQueue CircQueue_init(); | ||
| uint8_t CircQueue_empty(CircQueue* self); | ||
| CircQueueStatus CircQueue_put(CircQueue* self, SlavePkt* val); | ||
| CircQueueStatus CircQueue_get(CircQueue* self, SlavePkt* val); | ||
|
|
||
| void CircQueue_print(CircQueue* self); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| #pragma once | ||
|
|
||
| #include <stdint.h> | ||
| #include "util.hpp" | ||
|
|
||
| #define SLAVE_PKT_HEADER {0x46, 0x55, 0x43, 0x4b, 0x5f, 0x43, 0x48, 0x43} | ||
| #define SLAVE_PACKET_LEN 8 | ||
| #define PAYLOAD_LEN 4 | ||
|
|
||
| #define PACKED __attribute__((__packed__)) | ||
|
|
||
| #define ADDR_THERMISTOR1_TEMP 0x0C | ||
| #define ADDR_THERMISTOR2_TEMP 0x0D | ||
|
|
||
| #define MAX_ADDR 0x0D | ||
|
|
||
| #define FUCK_CHC {} | ||
|
|
||
| typedef struct { | ||
| uint8_t header[SLAVE_PACKET_LEN]; | ||
| uint8_t addr; | ||
| uint8_t payload[PAYLOAD_LEN]; | ||
| } PACKED SlavePkt; | ||
|
|
||
| class SlaveThread { | ||
| public: | ||
| static void initialize(); | ||
|
|
||
| static osThreadId_t getThreadId(); | ||
| private: | ||
| static RTOSThread thread; | ||
| static void runThread(void* args); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| #include <stdint.h> | ||
| #include "circ_queue.h" | ||
|
|
||
| CircQueue CircQueue_init() { | ||
| CircQueue ret = { | ||
| .len = 0, | ||
| ._arr = {}, | ||
| ._head = 0, | ||
| ._tail = 0 | ||
| }; | ||
| return ret; | ||
| } | ||
|
|
||
| uint8_t CircQueue_empty(CircQueue* self) { | ||
| return self->len == 0; | ||
| } | ||
|
|
||
| CircQueueStatus CircQueue_put(CircQueue* self, SlavePkt* val) { | ||
| if ( !(self->len == CIRC_RX_BUFF_SIZE) ) { | ||
| // regular insert condition | ||
| if (CircQueue_empty(self)) { | ||
| self->_arr[self->_head] = *val; | ||
| } | ||
| else { | ||
| CIRC_INC_TAIL(self); | ||
| self->_arr[self->_tail] = *val; | ||
| } | ||
| self->len += 1; | ||
| return CircQueue_PUT_OK; | ||
| } | ||
| else { | ||
| // overwrite condition | ||
| self->_arr[self->_head] = *val; | ||
| CIRC_INC_HEAD(self); | ||
| CIRC_INC_TAIL(self); | ||
| return CircQueue_PUT_WRAPAROUND; | ||
| } | ||
| } | ||
| CircQueueStatus CircQueue_get(CircQueue* self, SlavePkt* val) { | ||
| if (self->len == 0) { | ||
| return CircQueue_GET_EMPTY; | ||
| } | ||
| else { | ||
| SlavePkt ret = self->_arr[self->_head]; | ||
| CIRC_INC_HEAD(self); | ||
| if (!CircQueue_empty(self)) { | ||
| self->len -= 1; | ||
| } | ||
| *val = ret; | ||
| return CircQueue_GET_OK; | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #include "bms_entry.hpp" | ||
| #include "threads.hpp" | ||
| #include "main.h" | ||
| #include "can.h" | ||
| #include "circ_queue.h" | ||
|
|
||
| RTOSThread SlaveThread::thread; | ||
| static CircQueue _rx_CircQueue; | ||
| static uint8_t _rx_bytes[sizeof(SlavePkt)]; | ||
|
|
||
| void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { | ||
| SlavePkt* p_pkt = (SlavePkt*)_rx_bytes; | ||
| CircQueue_put(&_rx_CircQueue, p_pkt); | ||
|
|
||
| HAL_UART_Receive_IT(&huart1, _rx_bytes, sizeof(SlavePkt)); | ||
| } | ||
|
|
||
| void SlaveThread::initialize() { | ||
| thread = RTOSThread( | ||
| "slave_thread", | ||
| 1024*2, | ||
| SLAVE_THREAD_PRIORITY, | ||
| runThread | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| void SlaveThread::runThread(void* args) { | ||
| SlavePkt rqst_pkt = { | ||
| .header = SLAVE_PKT_HEADER, | ||
| .addr = 0, | ||
| .payload = {0, 0, 0, 0} | ||
| }; | ||
| SlavePkt pkt; | ||
|
|
||
| HAL_UART_Receive_IT(&huart1, _rx_bytes, sizeof(SlavePkt)); | ||
|
|
||
| while (1) { | ||
| // Send a request to the slave for data packets | ||
| HAL_UART_Transmit(&huart1, (uint8_t*)&rqst_pkt, sizeof(SlavePkt), 100); | ||
|
|
||
| for (uint8_t i = 0; i < 14; i++) { | ||
| HAL_UART_Receive(&huart1, (uint8_t*)&pkt, sizeof(SlavePkt), HAL_MAX_DELAY); | ||
|
|
||
| if (pkt.addr <= 12) { | ||
| // We are dealing with one of the 12 (2 batt * 6 cell/batt) LiPo cells | ||
| global_bms_data.battery.cells[pkt.addr].voltage = UINT_TO_FLOAT(*((uint32_t*)pkt.payload)); | ||
| } else if (pkt.addr <= 14) { | ||
| // We are dealing with one of the temp sensors. We will store them in the first two cell temperatures | ||
| global_bms_data.battery.cells[pkt.addr - 13].temp = UINT_TO_FLOAT(*((uint32_t*)pkt.payload)); | ||
| } else {} // Something went wrong, idk maybe do something? | ||
| } | ||
|
|
||
| osDelay(SLAVE_THREAD_PERIODICITY); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what the limitations of the perfboard slave device are but can we send an error message via uart? something like a printf statement would be sufficient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nushaab-Syed no