CO_LSSslave.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * CANopen LSS Slave protocol.
  3. *
  4. * @file CO_LSSslave.c
  5. * @ingroup CO_LSS
  6. * @author Martin Wagner
  7. * @author Janez Paternoster
  8. * @copyright 2017 - 2020 Neuberger Gebaeudeautomation GmbH
  9. *
  10. *
  11. * This file is part of CANopenNode, an opensource CANopen Stack.
  12. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  13. * For more information on CANopen see <http://www.can-cia.org/>.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. #include "305/CO_LSSslave.h"
  28. #if (CO_CONFIG_LSS) & CO_CONFIG_LSS_SLAVE
  29. #include <string.h>
  30. /*
  31. * Read received message from CAN module.
  32. *
  33. * Function will be called (by CAN receive interrupt) every time, when CAN
  34. * message with correct identifier will be received. For more information and
  35. * description of parameters see file CO_driver.h.
  36. */
  37. static void CO_LSSslave_receive(void *object, void *msg)
  38. {
  39. CO_LSSslave_t *LSSslave = (CO_LSSslave_t*)object;
  40. uint8_t DLC = CO_CANrxMsg_readDLC(msg);
  41. if (DLC == 8U && !CO_FLAG_READ(LSSslave->sendResponse)) {
  42. bool_t request_LSSslave_process = false;
  43. uint8_t *data = CO_CANrxMsg_readData(msg);
  44. CO_LSS_cs_t cs = (CO_LSS_cs_t) data[0];
  45. if (cs == CO_LSS_SWITCH_STATE_GLOBAL) {
  46. uint8_t mode = data[1];
  47. switch (mode) {
  48. case CO_LSS_STATE_WAITING:
  49. if (LSSslave->lssState == CO_LSS_STATE_CONFIGURATION &&
  50. LSSslave->activeNodeID == CO_LSS_NODE_ID_ASSIGNMENT &&
  51. *LSSslave->pendingNodeID != CO_LSS_NODE_ID_ASSIGNMENT)
  52. {
  53. /* Slave process function will request NMT Reset comm.*/
  54. LSSslave->service = cs;
  55. request_LSSslave_process = true;
  56. }
  57. LSSslave->lssState = CO_LSS_STATE_WAITING;
  58. memset(&LSSslave->lssSelect, 0,
  59. sizeof(LSSslave->lssSelect));
  60. break;
  61. case CO_LSS_STATE_CONFIGURATION:
  62. LSSslave->lssState = CO_LSS_STATE_CONFIGURATION;
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68. else if (LSSslave->lssState == CO_LSS_STATE_WAITING) {
  69. switch (cs) {
  70. case CO_LSS_SWITCH_STATE_SEL_VENDOR: {
  71. uint32_t valSw;
  72. memcpy(&valSw, &data[1], sizeof(valSw));
  73. LSSslave->lssSelect.identity.vendorID = CO_SWAP_32(valSw);
  74. break;
  75. }
  76. case CO_LSS_SWITCH_STATE_SEL_PRODUCT: {
  77. uint32_t valSw;
  78. memcpy(&valSw, &data[1], sizeof(valSw));
  79. LSSslave->lssSelect.identity.productCode = CO_SWAP_32(valSw);
  80. break;
  81. }
  82. case CO_LSS_SWITCH_STATE_SEL_REV: {
  83. uint32_t valSw;
  84. memcpy(&valSw, &data[1], sizeof(valSw));
  85. LSSslave->lssSelect.identity.revisionNumber = CO_SWAP_32(valSw);
  86. break;
  87. }
  88. case CO_LSS_SWITCH_STATE_SEL_SERIAL: {
  89. uint32_t valSw;
  90. memcpy(&valSw, &data[1], sizeof(valSw));
  91. LSSslave->lssSelect.identity.serialNumber = CO_SWAP_32(valSw);
  92. if (CO_LSS_ADDRESS_EQUAL(LSSslave->lssAddress,
  93. LSSslave->lssSelect)
  94. ) {
  95. LSSslave->lssState = CO_LSS_STATE_CONFIGURATION;
  96. LSSslave->service = cs;
  97. request_LSSslave_process = true;
  98. }
  99. break;
  100. }
  101. case CO_LSS_IDENT_FASTSCAN: {
  102. /* fastscan is only active on unconfigured nodes */
  103. if (*LSSslave->pendingNodeID == CO_LSS_NODE_ID_ASSIGNMENT &&
  104. LSSslave->activeNodeID == CO_LSS_NODE_ID_ASSIGNMENT)
  105. {
  106. uint8_t bitCheck = data[5];
  107. uint8_t lssSub = data[6];
  108. uint8_t lssNext = data[7];
  109. uint32_t valSw;
  110. uint32_t idNumber;
  111. bool_t ack;
  112. if (!CO_LSS_FASTSCAN_BITCHECK_VALID(bitCheck) ||
  113. !CO_LSS_FASTSCAN_LSS_SUB_NEXT_VALID(lssSub) ||
  114. !CO_LSS_FASTSCAN_LSS_SUB_NEXT_VALID(lssNext)) {
  115. /* Invalid request */
  116. break;
  117. }
  118. memcpy(&valSw, &data[1], sizeof(valSw));
  119. idNumber = CO_SWAP_32(valSw);
  120. ack = false;
  121. if (bitCheck == CO_LSS_FASTSCAN_CONFIRM) {
  122. /* Confirm, Reset */
  123. ack = true;
  124. LSSslave->fastscanPos = CO_LSS_FASTSCAN_VENDOR_ID;
  125. memset(&LSSslave->lssFastscan, 0,
  126. sizeof(LSSslave->lssFastscan));
  127. }
  128. else if (LSSslave->fastscanPos == lssSub) {
  129. uint32_t mask = 0xFFFFFFFF << bitCheck;
  130. if ((LSSslave->lssAddress.addr[lssSub] & mask)
  131. == (idNumber & mask))
  132. {
  133. /* all requested bits match */
  134. ack = true;
  135. LSSslave->fastscanPos = lssNext;
  136. if (bitCheck == 0 && lssNext < lssSub) {
  137. /* complete match, enter configuration state */
  138. LSSslave->lssState = CO_LSS_STATE_CONFIGURATION;
  139. }
  140. }
  141. }
  142. if (ack) {
  143. #if (CO_CONFIG_LSS) & CO_CONFIG_LSS_SLAVE_FASTSCAN_DIRECT_RESPOND
  144. LSSslave->TXbuff->data[0] = CO_LSS_IDENT_SLAVE;
  145. memset(&LSSslave->TXbuff->data[1], 0,
  146. sizeof(LSSslave->TXbuff->data) - 1);
  147. CO_CANsend(LSSslave->CANdevTx, LSSslave->TXbuff);
  148. #else
  149. LSSslave->service = cs;
  150. request_LSSslave_process = true;
  151. #endif
  152. }
  153. }
  154. break;
  155. }
  156. default: {
  157. break;
  158. }
  159. }
  160. }
  161. else { /* LSSslave->lssState == CO_LSS_STATE_CONFIGURATION */
  162. memcpy(&LSSslave->CANdata, &data[0], sizeof(LSSslave->CANdata));
  163. LSSslave->service = cs;
  164. request_LSSslave_process = true;
  165. }
  166. if (request_LSSslave_process) {
  167. CO_FLAG_SET(LSSslave->sendResponse);
  168. #if (CO_CONFIG_LSS) & CO_CONFIG_FLAG_CALLBACK_PRE
  169. /* Optional signal to RTOS, which can resume task,
  170. * which handles further processing. */
  171. if (LSSslave->pFunctSignalPre != NULL) {
  172. LSSslave->pFunctSignalPre(LSSslave->functSignalObjectPre);
  173. }
  174. #endif
  175. }
  176. }
  177. }
  178. /******************************************************************************/
  179. CO_ReturnError_t CO_LSSslave_init(
  180. CO_LSSslave_t *LSSslave,
  181. CO_LSS_address_t *lssAddress,
  182. uint16_t *pendingBitRate,
  183. uint8_t *pendingNodeID,
  184. CO_CANmodule_t *CANdevRx,
  185. uint16_t CANdevRxIdx,
  186. uint32_t CANidLssMaster,
  187. CO_CANmodule_t *CANdevTx,
  188. uint16_t CANdevTxIdx,
  189. uint32_t CANidLssSlave)
  190. {
  191. CO_ReturnError_t ret = CO_ERROR_NO;
  192. /* verify arguments */
  193. if (LSSslave==NULL || pendingBitRate == NULL || pendingNodeID == NULL ||
  194. CANdevRx==NULL || CANdevTx==NULL ||
  195. !CO_LSS_NODE_ID_VALID(*pendingNodeID)
  196. ) {
  197. return CO_ERROR_ILLEGAL_ARGUMENT;
  198. }
  199. /* Application must make sure that lssAddress is filled with data. */
  200. /* clear the object */
  201. memset(LSSslave, 0, sizeof(CO_LSSslave_t));
  202. /* Configure object variables */
  203. memcpy(&LSSslave->lssAddress, lssAddress, sizeof(LSSslave->lssAddress));
  204. LSSslave->lssState = CO_LSS_STATE_WAITING;
  205. LSSslave->fastscanPos = CO_LSS_FASTSCAN_VENDOR_ID;
  206. LSSslave->pendingBitRate = pendingBitRate;
  207. LSSslave->pendingNodeID = pendingNodeID;
  208. LSSslave->activeNodeID = *pendingNodeID;
  209. CO_FLAG_CLEAR(LSSslave->sendResponse);
  210. /* configure LSS CAN Master message reception */
  211. ret = CO_CANrxBufferInit(
  212. CANdevRx, /* CAN device */
  213. CANdevRxIdx, /* rx buffer index */
  214. CANidLssMaster, /* CAN identifier */
  215. 0x7FF, /* mask */
  216. 0, /* rtr */
  217. (void*)LSSslave, /* object passed to receive function */
  218. CO_LSSslave_receive); /* this function will process received message */
  219. /* configure LSS CAN Slave response message transmission */
  220. LSSslave->CANdevTx = CANdevTx;
  221. LSSslave->TXbuff = CO_CANtxBufferInit(
  222. CANdevTx, /* CAN device */
  223. CANdevTxIdx, /* index of specific buffer inside CAN module */
  224. CANidLssSlave, /* CAN identifier */
  225. 0, /* rtr */
  226. 8, /* number of data bytes */
  227. 0); /* synchronous message flag bit */
  228. if (LSSslave->TXbuff == NULL) {
  229. ret = CO_ERROR_ILLEGAL_ARGUMENT;
  230. }
  231. return ret;
  232. }
  233. #if (CO_CONFIG_LSS) & CO_CONFIG_FLAG_CALLBACK_PRE
  234. /******************************************************************************/
  235. void CO_LSSslave_initCallbackPre(
  236. CO_LSSslave_t *LSSslave,
  237. void *object,
  238. void (*pFunctSignalPre)(void *object))
  239. {
  240. if (LSSslave != NULL){
  241. LSSslave->functSignalObjectPre = object;
  242. LSSslave->pFunctSignalPre = pFunctSignalPre;
  243. }
  244. }
  245. #endif
  246. /******************************************************************************/
  247. void CO_LSSslave_initCheckBitRateCallback(
  248. CO_LSSslave_t *LSSslave,
  249. void *object,
  250. bool_t (*pFunctLSScheckBitRate)(void *object, uint16_t bitRate))
  251. {
  252. if (LSSslave != NULL){
  253. LSSslave->functLSScheckBitRateObject = object;
  254. LSSslave->pFunctLSScheckBitRate = pFunctLSScheckBitRate;
  255. }
  256. }
  257. /******************************************************************************/
  258. void CO_LSSslave_initActivateBitRateCallback(
  259. CO_LSSslave_t *LSSslave,
  260. void *object,
  261. void (*pFunctLSSactivateBitRate)(void *object, uint16_t delay))
  262. {
  263. if (LSSslave != NULL){
  264. LSSslave->functLSSactivateBitRateObject = object;
  265. LSSslave->pFunctLSSactivateBitRate = pFunctLSSactivateBitRate;
  266. }
  267. }
  268. /******************************************************************************/
  269. void CO_LSSslave_initCfgStoreCallback(
  270. CO_LSSslave_t *LSSslave,
  271. void *object,
  272. bool_t (*pFunctLSScfgStore)(void *object, uint8_t id, uint16_t bitRate))
  273. {
  274. if (LSSslave != NULL){
  275. LSSslave->functLSScfgStoreObject = object;
  276. LSSslave->pFunctLSScfgStore = pFunctLSScfgStore;
  277. }
  278. }
  279. /******************************************************************************/
  280. bool_t CO_LSSslave_process(CO_LSSslave_t *LSSslave) {
  281. bool_t resetCommunication = false;
  282. if (CO_FLAG_READ(LSSslave->sendResponse)) {
  283. uint8_t nid;
  284. uint8_t errorCode;
  285. uint8_t errorCodeManuf;
  286. uint8_t tableSelector;
  287. uint8_t tableIndex;
  288. bool_t CANsend = false;
  289. uint32_t valSw;
  290. memset(&LSSslave->TXbuff->data[0], 0, sizeof(LSSslave->TXbuff->data));
  291. switch (LSSslave->service) {
  292. case CO_LSS_SWITCH_STATE_GLOBAL: {
  293. /* Node-Id was unconfigured before, now it is configured,
  294. * enter the NMT Reset communication autonomously. */
  295. resetCommunication = true;
  296. break;
  297. }
  298. case CO_LSS_SWITCH_STATE_SEL_SERIAL: {
  299. LSSslave->TXbuff->data[0] = CO_LSS_SWITCH_STATE_SEL;
  300. CANsend = true;
  301. break;
  302. }
  303. case CO_LSS_CFG_NODE_ID: {
  304. nid = LSSslave->CANdata[1];
  305. errorCode = CO_LSS_CFG_NODE_ID_OK;
  306. if (CO_LSS_NODE_ID_VALID(nid)) {
  307. *LSSslave->pendingNodeID = nid;
  308. }
  309. else {
  310. errorCode = CO_LSS_CFG_NODE_ID_OUT_OF_RANGE;
  311. }
  312. /* send confirmation */
  313. LSSslave->TXbuff->data[0] = LSSslave->service;
  314. LSSslave->TXbuff->data[1] = errorCode;
  315. /* we do not use spec-error, always 0 */
  316. CANsend = true;
  317. break;
  318. }
  319. case CO_LSS_CFG_BIT_TIMING: {
  320. if (LSSslave->pFunctLSScheckBitRate == NULL) {
  321. /* setting bit timing is not supported. Drop request */
  322. break;
  323. }
  324. tableSelector = LSSslave->CANdata[1];
  325. tableIndex = LSSslave->CANdata[2];
  326. errorCode = CO_LSS_CFG_BIT_TIMING_OK;
  327. errorCodeManuf = CO_LSS_CFG_BIT_TIMING_OK;
  328. if (tableSelector == 0 && CO_LSS_BIT_TIMING_VALID(tableIndex)) {
  329. uint16_t bit = CO_LSS_bitTimingTableLookup[tableIndex];
  330. bool_t bit_rate_supported = LSSslave->pFunctLSScheckBitRate(
  331. LSSslave->functLSScheckBitRateObject, bit);
  332. if (bit_rate_supported) {
  333. *LSSslave->pendingBitRate = bit;
  334. }
  335. else {
  336. errorCode = CO_LSS_CFG_BIT_TIMING_MANUFACTURER;
  337. errorCodeManuf = CO_LSS_CFG_BIT_TIMING_OUT_OF_RANGE;
  338. }
  339. }
  340. else {
  341. /* we currently only support CiA301 bit timing table */
  342. errorCode = CO_LSS_CFG_BIT_TIMING_OUT_OF_RANGE;
  343. }
  344. /* send confirmation */
  345. LSSslave->TXbuff->data[0] = LSSslave->service;
  346. LSSslave->TXbuff->data[1] = errorCode;
  347. LSSslave->TXbuff->data[2] = errorCodeManuf;
  348. CANsend = true;
  349. break;
  350. }
  351. case CO_LSS_CFG_ACTIVATE_BIT_TIMING: {
  352. if (LSSslave->pFunctLSScheckBitRate == NULL) {
  353. /* setting bit timing is not supported. Drop request */
  354. break;
  355. }
  356. /* notify application */
  357. if (LSSslave->pFunctLSSactivateBitRate != NULL) {
  358. uint16_t delay = ((uint16_t) LSSslave->CANdata[2]) << 8;
  359. delay |= LSSslave->CANdata[1];
  360. LSSslave->pFunctLSSactivateBitRate(
  361. LSSslave->functLSSactivateBitRateObject, delay);
  362. }
  363. break;
  364. }
  365. case CO_LSS_CFG_STORE: {
  366. errorCode = CO_LSS_CFG_STORE_OK;
  367. if (LSSslave->pFunctLSScfgStore == NULL) {
  368. /* storing is not supported. Reply error */
  369. errorCode = CO_LSS_CFG_STORE_NOT_SUPPORTED;
  370. }
  371. else {
  372. bool_t result;
  373. /* Store "pending" to "persistent" */
  374. result =
  375. LSSslave->pFunctLSScfgStore(LSSslave->functLSScfgStoreObject,
  376. *LSSslave->pendingNodeID,
  377. *LSSslave->pendingBitRate);
  378. if (!result) {
  379. errorCode = CO_LSS_CFG_STORE_FAILED;
  380. }
  381. }
  382. /* send confirmation */
  383. LSSslave->TXbuff->data[0] = LSSslave->service;
  384. LSSslave->TXbuff->data[1] = errorCode;
  385. /* we do not use spec-error, always 0 */
  386. CANsend = true;
  387. break;
  388. }
  389. case CO_LSS_INQUIRE_VENDOR: {
  390. LSSslave->TXbuff->data[0] = LSSslave->service;
  391. valSw = CO_SWAP_32(LSSslave->lssAddress.identity.vendorID);
  392. memcpy(&LSSslave->TXbuff->data[1], &valSw, sizeof(valSw));
  393. CANsend = true;
  394. break;
  395. }
  396. case CO_LSS_INQUIRE_PRODUCT: {
  397. LSSslave->TXbuff->data[0] = LSSslave->service;
  398. valSw = CO_SWAP_32(LSSslave->lssAddress.identity.productCode);
  399. memcpy(&LSSslave->TXbuff->data[1], &valSw, sizeof(valSw));
  400. CANsend = true;
  401. break;
  402. }
  403. case CO_LSS_INQUIRE_REV: {
  404. LSSslave->TXbuff->data[0] = LSSslave->service;
  405. valSw = CO_SWAP_32(LSSslave->lssAddress.identity.revisionNumber);
  406. memcpy(&LSSslave->TXbuff->data[1], &valSw, sizeof(valSw));
  407. CANsend = true;
  408. break;
  409. }
  410. case CO_LSS_INQUIRE_SERIAL: {
  411. LSSslave->TXbuff->data[0] = LSSslave->service;
  412. valSw = CO_SWAP_32(LSSslave->lssAddress.identity.serialNumber);
  413. memcpy(&LSSslave->TXbuff->data[1], &valSw, sizeof(valSw));
  414. CANsend = true;
  415. break;
  416. }
  417. case CO_LSS_INQUIRE_NODE_ID: {
  418. LSSslave->TXbuff->data[0] = LSSslave->service;
  419. LSSslave->TXbuff->data[1] = LSSslave->activeNodeID;
  420. CANsend = true;
  421. break;
  422. }
  423. case CO_LSS_IDENT_FASTSCAN: {
  424. LSSslave->TXbuff->data[0] = CO_LSS_IDENT_SLAVE;
  425. CANsend = true;
  426. break;
  427. }
  428. default: {
  429. break;
  430. }
  431. }
  432. if (CANsend) {
  433. CO_CANsend(LSSslave->CANdevTx, LSSslave->TXbuff);
  434. }
  435. CO_FLAG_CLEAR(LSSslave->sendResponse);
  436. }
  437. return resetCommunication;
  438. }
  439. #endif /* (CO_CONFIG_LSS) & CO_CONFIG_LSS_SLAVE */