tlc16c550.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*
  2. * Copyright (C) 2001-2003 by Cyber Integration, LLC. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY CYBER INTEGRATION, LLC AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CYBER
  21. * INTEGRATION, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. *
  31. */
  32. /*!
  33. * \file arch/avr/dev/tlc16c550.c
  34. * \brief AVR driver for external TLC16C550.
  35. *
  36. * \verbatim
  37. * $Id: tlc16c550.c 5472 2013-12-06 00:16:28Z olereinhardt $
  38. * \endverbatim
  39. */
  40. // system include files
  41. #include <string.h>
  42. #include <sys/atom.h>
  43. #include <sys/heap.h>
  44. #include <sys/event.h>
  45. #include <sys/timer.h>
  46. #include <sys/device.h>
  47. #include <dev/irqreg.h>
  48. #include <dev/tlc16c550.h>
  49. #include <fcntl.h>
  50. #include <stdio.h>
  51. /*
  52. * Not nice because stdio already defined them. But in order to save memory,
  53. * we do the whole buffering and including stdio here would be more weird.
  54. */
  55. #ifndef _IOFBF
  56. #define _IOFBF 0x00
  57. #define _IOLBF 0x01
  58. #define _IONBF 0x02
  59. #endif
  60. /*!
  61. * \
  62. */
  63. /*@{*/
  64. // define the register offset
  65. // define the UART Register offsets
  66. #define ACE_RBR_OFS 0
  67. #define ACE_THR_OFS 0
  68. #define ACE_DLL_OFS 0
  69. #define ACE_DLM_OFS 1
  70. #define ACE_IER_OFS 1
  71. #define ACE_FCR_OFS 2
  72. #define ACE_IIR_OFS 2
  73. #define ACE_LCR_OFS 3
  74. #define ACE_MCR_OFS 4
  75. #define ACE_LSR_OFS 5
  76. #define ACE_MSR_OFS 6
  77. #define ACE_SRC_OFS 7
  78. // define the interrupt enable masks
  79. #define IER_RDA_MSK 0x01 // receiver data avaialbe
  80. #define IER_THE_MSK 0x02 // transmit holding empty
  81. #define IER_LST_MSK 0x04 // line status
  82. #define IER_MST_MSK 0x08 // modem status
  83. // define the fifo control mask
  84. #define FCR_ENABLE 0x01 //fifo enable
  85. #define FCR_PURGE_I 0x02 //purge all data from input fifo
  86. #define FCR_PURGE_O 0x04 //purge all data from output fifo
  87. #define FCR_LEVEL_1 0x00 //receive trigger level 1
  88. #define FCR_LEVEL_4 0x40 //receive trigger level 4
  89. #define FCR_LEVEL_8 0x80 //receive trigger level 8
  90. #define FCR_LEVEL_14 0xc0 //receive trigger level 14
  91. // define the interrupt id masks
  92. #define IIR_MST_MSK 0x00 // modem status interrupt
  93. #define IIR_TXE_MSK 0x02 // transmit buffer empty
  94. #define IIR_RDA_MSK 0x04 // receive data available
  95. #define IIR_TDA_MSK 0x0c // timeout receive data available
  96. #define IIR_LST_MSK 0x06 // line status interrupt
  97. #define IIR_NON_MSK 0x01 // no interrupt
  98. #define IIR_FIFO_MSK 0xc0 // mask to eliminate the fifo status
  99. // define the line control masks
  100. #define LCR_WS0_MSK 0x01
  101. #define LCR_WS1_MSK 0x02
  102. #define LCR_STB_MSK 0x04
  103. #define LCR_PEN_MSK 0x08
  104. #define LCR_PRE_MSK 0x10
  105. #define LCR_PRS_MSK 0x20
  106. #define LCR_BRK_MSK 0x40
  107. #define LCR_ENB_MSK 0x80
  108. // define the modem control masks
  109. #define MCR_DTR_MSK 0x01
  110. #define MCR_RTS_MSK 0x02
  111. #define MCR_GP1_MSK 0x04
  112. #define MCR_GP2_MSK 0x08
  113. #define MCR_LOP_MSK 0x10
  114. // define the line status masks
  115. #define LSR_RDR_MSK 0x01
  116. #define LSR_OVR_MSK 0x02
  117. #define LSR_PER_MSK 0x04
  118. #define LSR_FER_MSK 0x08
  119. #define LSR_BDT_MSK 0x10
  120. #define LSR_THE_MSK 0x20
  121. #define LSR_TXE_MSK 0x40
  122. #define LSR_EIF_MSK 0x80
  123. // define the modem status masks
  124. #define MSR_DCTS_MSK 0x01
  125. #define MSR_DDSR_MSK 0x02
  126. #define MSR_DRI_MSK 0x04
  127. #define MSR_DDCD_MSK 0x08
  128. #define MSR_CTS_MSK 0x10
  129. #define MSR_DSR_MSK 0x20
  130. #define MSR_RI_MSK 0x40
  131. #define MSR_DCD_MSK 0x80
  132. // define the irq structure
  133. typedef struct tagIRQDEFS {
  134. IRQ_HANDLER *pvIrq;
  135. volatile uint8_t *pnIrqMskPort;
  136. uint8_t nMask;
  137. } IRQDEFS;
  138. // define the interrupt handlers
  139. static const PROGMEM IRQDEFS atIrqDefs[] = {
  140. {&sig_INTERRUPT0, &EICRA, 0x00},
  141. {&sig_INTERRUPT1, &EICRA, 0x00},
  142. {&sig_INTERRUPT2, &EICRA, 0x00},
  143. {&sig_INTERRUPT3, &EICRA, 0x00},
  144. {&sig_INTERRUPT4, &EICRB, 0x00},
  145. {&sig_INTERRUPT5, &EICRB, 0x00},
  146. {&sig_INTERRUPT6, &EICRB, 0x00},
  147. {&sig_INTERRUPT7, &EICRB, 0x00}
  148. };
  149. // define the dcb's asigned to the interrupt to have more than one device on the same interrupt
  150. // NUT intrnal irq structure could be used instead but that would be a hack
  151. static NUTDEVICE *pIrqDev[8] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
  152. static uint8_t irqMask = 0;
  153. #ifdef ACE_HDX_LINE
  154. static unsigned int ByteOcrTime(NUTDEVICE * dev);
  155. static void AceTmr3Init(void);
  156. static void AceOutComp3AInt(void *arg);
  157. static void AceAddHdxTime(ACEDCB * dev);
  158. #endif
  159. /*
  160. * Handle AVR ACE interrupts
  161. */
  162. static void AceIrqHandler(void *arg)
  163. {
  164. NUTDEVICE *dev = (NUTDEVICE *) arg;
  165. IFSTREAM *ifs;
  166. ACEDCB *dcb;
  167. volatile uint8_t event;
  168. uint8_t maxData;
  169. do {
  170. ifs = dev->dev_icb;
  171. dcb = dev->dev_dcb;
  172. // get the interrupt source
  173. while (((event = *(uint8_t *) (dev->dev_base + ACE_IIR_OFS)) & ~IIR_FIFO_MSK) != IIR_NON_MSK) {
  174. switch (event & ~IIR_FIFO_MSK) {
  175. case IIR_RDA_MSK: // receive data available
  176. case IIR_TDA_MSK: // timeout receive data available
  177. /* maxData can be avoided but it ensures that for slow system and fast uart we will not get stuck
  178. * reading incomming data all the time.
  179. */
  180. maxData = (dcb->dcb_rfifo == 0) ? 1 : dcb->dcb_rfifo;
  181. for (; (*(uint8_t *) (dev->dev_base + ACE_LSR_OFS) & LSR_RDR_MSK) && (maxData > 0); --maxData) {
  182. // get the character and store it
  183. ifs->if_rx_buf[ifs->if_rx_idx] = *(uint8_t *) (dev->dev_base + ACE_RBR_OFS);
  184. /* if we have just received a first byte into the empty buffer */
  185. if (ifs->if_rd_idx == ifs->if_rx_idx) {
  186. NutEventPostFromIrq(&(dcb->dcb_rx_rdy));
  187. }
  188. /* Late increment fixes ICCAVR bug on volatile variables. */
  189. ifs->if_rx_idx++;
  190. }
  191. break;
  192. case IIR_TXE_MSK: // transmit buffer empty
  193. dcb->dcb_wfifo = (event & IIR_FIFO_MSK) ? ACE_FIFO_SIZE : 1;
  194. if (ifs->if_tx_idx != ifs->if_wr_idx) {
  195. for (; (ifs->if_tx_idx != ifs->if_wr_idx) && (dcb->dcb_wfifo > 0); ++ifs->if_tx_idx) {
  196. --dcb->dcb_wfifo;
  197. // send a character
  198. *(uint8_t *) (dev->dev_base + ACE_THR_OFS) = ifs->if_tx_buf[ifs->if_tx_idx];
  199. }
  200. } else {
  201. #ifdef ACE_HDX_LINE
  202. if (dcb->dcb_modeflags & ACE_MF_HALFDUPLEX) {
  203. AceAddHdxTime(dcb);
  204. }
  205. #endif
  206. ifs->if_tx_act = 0;
  207. NutEventPostFromIrq(&(dcb->dcb_tx_rdy));
  208. }
  209. break;
  210. case IIR_MST_MSK: // modem status interrupt
  211. break;
  212. case IIR_LST_MSK: // line status interrupt
  213. break;
  214. }
  215. }
  216. /* get the next device assigned to this interrupt */
  217. dev = dcb->dev_next;
  218. } while (dev != NULL);
  219. }
  220. #ifdef ACE_HDX_LINE
  221. static unsigned int ByteOcrTime(NUTDEVICE * dev)
  222. {
  223. uint8_t bv;
  224. uint8_t tb = 14; /* twice of 1 start 5 char min. 1 stop */
  225. unsigned int sv;
  226. uint32_t s, c;
  227. /* get speed */
  228. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) |= LCR_ENB_MSK;
  229. sv = *(uint8_t *) (dev->dev_base + ACE_DLL_OFS);
  230. sv |= *(uint8_t *) (dev->dev_base + ACE_DLM_OFS) << 8;
  231. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) &= (uint8_t) ~ LCR_ENB_MSK;
  232. bv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS);
  233. /* character length *2 */
  234. tb += (bv & (LCR_WS0_MSK | LCR_WS1_MSK)) << 1;
  235. /* stop bits *2 */
  236. if (bv & LCR_STB_MSK) {
  237. tb += 1 + !!(bv & (LCR_WS0_MSK | LCR_WS1_MSK));
  238. }
  239. /* parity bit *2 */
  240. tb += (!!(bv & LCR_PEN_MSK)) << 1;
  241. s = ACE_CLOCK * 8UL;
  242. s = s / (uint32_t) (sv);
  243. c = NutGetCpuClock();
  244. c = c * (uint32_t) tb;
  245. sv = ((unsigned int) (c / s) & 0x0000ffff) - 1;
  246. return sv;
  247. }
  248. #endif
  249. /*!
  250. * \brief Wait for input.
  251. *
  252. * This function checks the input buffer for any data. If
  253. * the buffer is empty, the calling \ref xrThread "thread"
  254. * will be blocked until at least one new character is
  255. * received or a timeout occurs.
  256. *
  257. * \param dev Indicates the ACE device.
  258. *
  259. * \return 0 on success, -1 on timeout.
  260. */
  261. int AceInput(NUTDEVICE * dev)
  262. {
  263. int rc = 0;
  264. IFSTREAM *ifs = dev->dev_icb;
  265. ACEDCB *dcb = dev->dev_dcb;
  266. if (ifs->if_rd_idx == ifs->if_rx_idx) {
  267. rc = NutEventWaitNext(&(dcb->dcb_rx_rdy), dcb->dcb_rtimeout);
  268. }
  269. return rc;
  270. }
  271. /*!
  272. * \brief Initiate output.
  273. *
  274. * This function checks the output buffer for any data. If
  275. * the buffer contains at least one character, the transmitter
  276. * is started, if not already running. The function returns
  277. * immediately, without waiting for the character being
  278. * completely transmitted. Any remaining characters in the
  279. * output buffer are transmitted in the background.
  280. *
  281. * \param dev Indicates the ACE device.
  282. *
  283. * \return 0 on success, -1 otherwise.
  284. */
  285. int AceOutput(NUTDEVICE * dev)
  286. {
  287. IFSTREAM *ifs = dev->dev_icb;
  288. ACEDCB *dcb = dev->dev_dcb;
  289. volatile uint8_t tmp;
  290. if ((ifs->if_tx_act == 0) && (ifs->if_tx_idx != ifs->if_wr_idx)) {
  291. ifs->if_tx_act = 1;
  292. --dcb->dcb_wfifo;
  293. tmp = ifs->if_tx_idx;
  294. ++ifs->if_tx_idx;
  295. #ifdef ACE_HDX_LINE
  296. if (dcb->dcb_modeflags & ACE_MF_HALFDUPLEX) {
  297. ACE_HDX_TRANSMIT(dev->dev_base);
  298. }
  299. #endif
  300. // send a character
  301. *(uint8_t *) (dev->dev_base + ACE_THR_OFS) = ifs->if_tx_buf[tmp];
  302. // no need to enable an interrupt here as it should be enabled all the time
  303. }
  304. return 0;
  305. }
  306. /*!
  307. * \brief Wait for output buffer empty.
  308. *
  309. * If the output buffer contains any data, the calling
  310. * thread is suspended until all data has been transmitted.
  311. *
  312. * \param dev Indicates the ACE device.
  313. *
  314. * \return 0 on success, -1 otherwise.
  315. */
  316. int AceFlush(NUTDEVICE * dev)
  317. {
  318. IFSTREAM *ifs = dev->dev_icb;
  319. ACEDCB *dcb = dev->dev_dcb;
  320. /*
  321. * Start any pending output.
  322. */
  323. if (AceOutput(dev))
  324. return -1;
  325. /*
  326. * Wait until output buffer empty.
  327. */
  328. while (ifs->if_tx_idx != ifs->if_wr_idx) {
  329. NutEventWaitNext(&dcb->dcb_tx_rdy, 100);
  330. }
  331. return 0;
  332. }
  333. /*
  334. *
  335. * \param dev Indicates the ACE device.
  336. *
  337. * \return 0 on success, -1 otherwise.
  338. */
  339. static int AceGetStatus(NUTDEVICE * dev, uint32_t * status)
  340. {
  341. IFSTREAM *ifs = dev->dev_icb;
  342. uint8_t us;
  343. *status = 0;
  344. us = *(uint8_t *) (dev->dev_base + ACE_LSR_OFS);
  345. if (us & LSR_FER_MSK)
  346. *status |= ACE_FRAMINGERROR;
  347. if (us & LSR_OVR_MSK)
  348. *status |= ACE_OVERRUNERROR;
  349. if (ifs->if_tx_idx == ifs->if_wr_idx)
  350. *status |= ACE_TXBUFFEREMPTY;
  351. if (ifs->if_rd_idx == ifs->if_rx_idx)
  352. *status |= ACE_RXBUFFEREMPTY;
  353. return 0;
  354. }
  355. /*
  356. * Carefully enable ACE functions.
  357. */
  358. static void AceEnable(uint16_t base)
  359. {
  360. /*volatile uint8_t* pnBase = *(volatile uint8_t* )base; */
  361. /*
  362. * Enable ACE interrupts.
  363. */
  364. NutEnterCritical();
  365. *(uint8_t *) (base + ACE_IER_OFS) = IER_RDA_MSK | IER_THE_MSK;
  366. NutExitCritical();
  367. }
  368. /*
  369. * Carefully disable ACE functions.
  370. */
  371. static void AceDisable(uint16_t base)
  372. {
  373. /*volatile uint8_t* pnBase = *(volatile uint8_t* )base; */
  374. /*
  375. * Disable ACE interrupts.
  376. */
  377. NutEnterCritical();
  378. *(uint8_t *) (base + ACE_IER_OFS) &= (uint8_t) ~ (IER_RDA_MSK);
  379. NutExitCritical();
  380. /*
  381. * Allow incoming or outgoing character to finish.
  382. */
  383. NutDelay(10);
  384. }
  385. /*!
  386. * \brief Perform ACE control functions.
  387. *
  388. * \param dev Identifies the device that receives the device-control
  389. * function.
  390. * \param req Requested control function. May be set to one of the
  391. * following constants:
  392. * - ACE_SETSPEED, conf points to an uint32_t value containing the baudrate.
  393. * - ACE_GETSPEED, conf points to an uint32_t value receiving the current baudrate.
  394. * - ACE_SETDATABITS, conf points to an uint32_t value containing the number of data bits, 5, 6, 7 or 8.
  395. * - ACE_GETDATABITS, conf points to an uint32_t value receiving the number of data bits, 5, 6, 7 or 8.
  396. * - ACE_SETPARITY, conf points to an uint32_t value containing the parity, 0 (no), 1 (odd) or 2 (even).
  397. * - ACE_GETPARITY, conf points to an uint32_t value receiving the parity, 0 (no), 1 (odd) or 2 (even).
  398. * - ACE_SETSTOPBITS, conf points to an uint32_t value containing the number of stop bits 1 or 2.
  399. * - ACE_GETSTOPBITS, conf points to an uint32_t value receiving the number of stop bits 1 or 2.
  400. * - ACE_SETSTATUS
  401. * - ACE_GETSTATUS
  402. * - ACE_SETREADTIMEOUT, conf points to an uint32_t value containing the read timeout.
  403. * - ACE_GETREADTIMEOUT, conf points to an uint32_t value receiving the read timeout.
  404. * - ACE_SETWRITETIMEOUT, conf points to an uint32_t value containing the write timeout.
  405. * - ACE_GETWRITETIMEOUT, conf points to an uint32_t value receiving the write timeout.
  406. * - ACE_SETLOCALECHO, conf points to an uint32_t value containing 0 (off) or 1 (on).
  407. * - ACE_GETLOCALECHO, conf points to an uint32_t value receiving 0 (off) or 1 (on).
  408. * - ACE_SETFLOWCONTROL, conf points to an uint32_t value containing combined ACE_FCR_ values.
  409. * - ACE_GETFLOWCONTROL, conf points to an uint32_t value containing receiving ACE_FCR_ values.
  410. * - ACE_SETCOOKEDMODE, conf points to an uint32_t value containing 0 (off) or 1 (on).
  411. * - ACE_GETCOOKEDMODE, conf points to an uint32_t value receiving 0 (off) or 1 (on).
  412. *
  413. * \param conf Points to a buffer that contains any data required for
  414. * the given control function or receives data from that
  415. * function.
  416. * \return 0 on success, -1 otherwise.
  417. *
  418. * \warning Timeout values are given in milliseconds and are limited to
  419. * the granularity of the system timer. To disable timeout,
  420. * set the parameter to NUT_WAIT_INFINITE.
  421. *
  422. *
  423. */
  424. int AceIOCtl(NUTDEVICE * dev, int req, void *conf)
  425. {
  426. int rc = 0;
  427. ACEDCB *dcb;
  428. IFSTREAM *ifs;
  429. uint32_t *lvp = (uint32_t *) conf;
  430. uint32_t lv = *lvp;
  431. uint8_t bv = (uint8_t) lv;
  432. uint16_t sv;
  433. uint16_t devnum;
  434. uint8_t tv;
  435. if (dev == 0) {
  436. return -1;
  437. }
  438. devnum = dev->dev_base;
  439. dcb = dev->dev_dcb;
  440. switch (req) {
  441. case ACE_SETSPEED:
  442. AceDisable(devnum);
  443. sv = (uint16_t) (ACE_CLOCK / (lv * 16UL));
  444. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) |= LCR_ENB_MSK;
  445. *(uint8_t *) (dev->dev_base + ACE_DLL_OFS) = (uint8_t) (sv & 0xFF);
  446. *(uint8_t *) (dev->dev_base + ACE_DLM_OFS) = (uint8_t) (sv >> 8);
  447. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) &= (uint8_t) ~ LCR_ENB_MSK;
  448. #ifdef ACE_HDX_LINE
  449. dcb->hdxByteTime = ByteOcrTime(dev);
  450. #endif
  451. AceEnable(devnum);
  452. break;
  453. case ACE_GETSPEED:
  454. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) |= LCR_ENB_MSK;
  455. sv = *(uint8_t *) (dev->dev_base + ACE_DLL_OFS);
  456. sv |= *(uint8_t *) (dev->dev_base + ACE_DLM_OFS) << 8;
  457. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) &= (uint8_t) ~ LCR_ENB_MSK;
  458. *lvp = ACE_CLOCK / (16UL * (uint32_t) (sv));
  459. break;
  460. case ACE_SETDATABITS:
  461. AceDisable(devnum);
  462. if ((bv >= 5) && (bv <= 8)) {
  463. bv -= 5;
  464. tv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS);
  465. tv &= (uint8_t) ~ (LCR_WS0_MSK | LCR_WS1_MSK);
  466. tv |= (bv & (LCR_WS0_MSK | LCR_WS1_MSK));
  467. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) = tv;
  468. #ifdef ACE_HDX_LINE
  469. dcb->hdxByteTime = ByteOcrTime(dev);
  470. #endif
  471. }
  472. AceEnable(devnum);
  473. break;
  474. case ACE_GETDATABITS:
  475. *lvp = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) & (LCR_WS0_MSK | LCR_WS1_MSK);
  476. break;
  477. case ACE_SETPARITY:
  478. AceDisable(devnum);
  479. if (bv <= 4) {
  480. switch (bv) {
  481. case 1: // odd parity
  482. bv = LCR_PEN_MSK;
  483. break;
  484. case 2: // event parity
  485. bv = LCR_PEN_MSK | LCR_PRE_MSK;
  486. break;
  487. case 3: // space
  488. bv = LCR_PEN_MSK;
  489. break;
  490. case 4: // mark
  491. bv = LCR_PEN_MSK | LCR_PRS_MSK;
  492. default: // no parity
  493. bv = 0;
  494. break;
  495. }
  496. tv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS);
  497. tv &= (uint8_t) ~ (LCR_PEN_MSK | LCR_PRE_MSK | LCR_PRS_MSK);
  498. tv |= bv;
  499. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) = tv;
  500. #ifdef ACE_HDX_LINE
  501. dcb->hdxByteTime = ByteOcrTime(dev);
  502. #endif
  503. }
  504. AceEnable(devnum);
  505. break;
  506. case ACE_GETPARITY:
  507. tv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) & (LCR_PEN_MSK | LCR_PRE_MSK | LCR_PRS_MSK);
  508. switch (tv) {
  509. case 0:
  510. *lvp = 0; // no parity
  511. break;
  512. case LCR_PEN_MSK:
  513. *lvp = 1; // odd parity
  514. break;
  515. case LCR_PEN_MSK | LCR_PRE_MSK:
  516. *lvp = 2; // event parity
  517. break;
  518. case LCR_PEN_MSK | LCR_PRS_MSK:
  519. *lvp = 4; // mark parity
  520. break;
  521. }
  522. break;
  523. case ACE_SETSTOPBITS:
  524. AceDisable(devnum);
  525. if (bv == 1 || bv == 2) {
  526. tv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS);
  527. tv &= (uint8_t) ~ (LCR_STB_MSK);
  528. tv |= (bv == 2) ? LCR_STB_MSK : 0;
  529. *(uint8_t *) (dev->dev_base + ACE_LCR_OFS) = tv;
  530. #ifdef ACE_HDX_LINE
  531. dcb->hdxByteTime = ByteOcrTime(dev);
  532. #endif
  533. }
  534. AceEnable(devnum);
  535. break;
  536. case ACE_GETSTOPBITS:
  537. tv = *(uint8_t *) (dev->dev_base + ACE_LCR_OFS);
  538. *lvp = (tv & LCR_STB_MSK) ? 2 : 1;
  539. break;
  540. case ACE_SETFIFO:
  541. AceDisable(devnum);
  542. dcb->dcb_wfifo = ACE_FIFO_SIZE;
  543. switch (bv) {
  544. case 1:
  545. tv = FCR_ENABLE | FCR_LEVEL_1 | FCR_PURGE_I | FCR_PURGE_O;
  546. break;
  547. case 4:
  548. tv = FCR_ENABLE | FCR_LEVEL_4 | FCR_PURGE_I | FCR_PURGE_O;
  549. break;
  550. case 8:
  551. tv = FCR_ENABLE | FCR_LEVEL_8 | FCR_PURGE_I | FCR_PURGE_O;
  552. break;
  553. case 14:
  554. tv = FCR_ENABLE | FCR_LEVEL_14 | FCR_PURGE_I | FCR_PURGE_O;
  555. break;
  556. default:
  557. bv = 0;
  558. tv = 0;
  559. dcb->dcb_wfifo = 1;
  560. break;
  561. }
  562. *(uint8_t *) (dev->dev_base + ACE_FCR_OFS) = tv;
  563. /* if enabling then must write the level after */
  564. *(uint8_t *) (dev->dev_base + ACE_FCR_OFS) = tv;
  565. dcb->dcb_rfifo = bv;
  566. /* must signal any active and waiting writer, discard pending data */
  567. ifs = dev->dev_icb;
  568. ifs->if_tx_act = 0;
  569. ifs->if_tx_idx = ifs->if_wr_idx;
  570. NutEventPostAsync(&(dcb->dcb_tx_rdy));
  571. AceEnable(devnum);
  572. break;
  573. case ACE_GETFIFO:
  574. *lvp = (uint32_t) (dcb->dcb_rfifo);
  575. break;
  576. case ACE_GETSTATUS:
  577. AceGetStatus(dev, lvp);
  578. break;
  579. case ACE_SETSTATUS:
  580. rc = -1;
  581. break;
  582. case ACE_SETREADTIMEOUT:
  583. dcb->dcb_rtimeout = lv;
  584. break;
  585. case ACE_GETREADTIMEOUT:
  586. *lvp = dcb->dcb_rtimeout;
  587. break;
  588. case ACE_SETWRITETIMEOUT:
  589. dcb->dcb_wtimeout = lv;
  590. break;
  591. case ACE_GETWRITETIMEOUT:
  592. *lvp = dcb->dcb_wtimeout;
  593. break;
  594. case ACE_SETLOCALECHO:
  595. if (bv)
  596. dcb->dcb_modeflags |= ACE_MF_LOCALECHO;
  597. else
  598. dcb->dcb_modeflags &= ~ACE_MF_LOCALECHO;
  599. break;
  600. case ACE_GETLOCALECHO:
  601. *lvp = (dcb->dcb_modeflags & ACE_MF_LOCALECHO) ? 1 : 0;
  602. break;
  603. case ACE_SETFLOWCONTROL:
  604. #ifdef ACE_HDX_LINE
  605. if (lv & ACE_MF_HALFDUPLEX) {
  606. /* next transmission will use HDX pin */
  607. dcb->dcb_modeflags |= ACE_MF_HALFDUPLEX;
  608. } else {
  609. dcb->dcb_modeflags &= ~ACE_MF_HALFDUPLEX;
  610. }
  611. dcb->hdxOcrTime = 0;
  612. /* switch HDX pin off */
  613. ACE_HDX_RECEIVE(dev->dev_base);
  614. #endif
  615. break;
  616. case ACE_GETFLOWCONTROL:
  617. #ifdef ACE_HDX_LINE
  618. *lvp = (uint32_t) (dcb->dcb_modeflags & ACE_MF_HALFDUPLEX);
  619. #endif
  620. break;
  621. case ACE_SETCOOKEDMODE:
  622. if (bv)
  623. dcb->dcb_modeflags |= ACE_MF_COOKEDMODE;
  624. else
  625. dcb->dcb_modeflags &= ~ACE_MF_COOKEDMODE;
  626. break;
  627. case ACE_GETCOOKEDMODE:
  628. *lvp = (dcb->dcb_modeflags & ACE_MF_COOKEDMODE) ? 1 : 0;
  629. break;
  630. default:
  631. rc = -1;
  632. break;
  633. }
  634. return rc;
  635. }
  636. /*!
  637. * \brief Initialize on chip ACE device.
  638. *
  639. * Prepares the device for subsequent reading or writing.
  640. * Enables ACE transmitter and receiver interrupts.
  641. *
  642. * \param dev Identifies the device to initialize.
  643. *
  644. * \return 0 on success, -1 otherwise.
  645. */
  646. int AceInit(NUTDEVICE * dev)
  647. {
  648. IFSTREAM *ifs;
  649. ACEDCB *dcb, *pFirstDcb;
  650. uint32_t baudrate = 9600;
  651. uint32_t databits = 8;
  652. uint32_t parity = 0;
  653. uint32_t stopbits = 1;
  654. IRQ_HANDLER *irq;
  655. uint8_t *pnPort;
  656. uint8_t nMask;
  657. #ifdef ACE_HDX_LINE
  658. uint32_t flowcontrol = 0;
  659. #endif
  660. /*
  661. * We only support character devices for on-chip ACEs.
  662. */
  663. if (dev->dev_type != IFTYP_STREAM) {
  664. return -1;
  665. }
  666. /*
  667. * Initialize interface control block.
  668. */
  669. ifs = dev->dev_icb;
  670. memset(ifs, 0, sizeof(IFSTREAM));
  671. ifs->if_input = AceInput;
  672. ifs->if_output = AceOutput;
  673. ifs->if_flush = AceFlush;
  674. /*
  675. * Initialize driver control block.
  676. */
  677. dcb = dev->dev_dcb;
  678. memset(dcb, 0, sizeof(ACEDCB));
  679. dcb->dcb_modeflags = ACE_MF_NOBUFFER;
  680. dcb->dcb_rfifo = 0;
  681. dcb->dcb_wfifo = 1;
  682. dcb->dev_next = NULL;
  683. #ifdef ACE_HDX_LINE
  684. dcb->hdxOcrTime = 0;
  685. #endif
  686. /*
  687. * Register interrupt handler.
  688. */
  689. if (dev->dev_base) {
  690. /* if any ACE is already assigned to this interrupt */
  691. if (pIrqDev[dev->dev_irq] != NULL) {
  692. pFirstDcb = pIrqDev[dev->dev_irq]->dev_dcb;
  693. dcb->dev_next = pFirstDcb->dev_next;
  694. pFirstDcb->dev_next = dev;
  695. } else {
  696. #ifdef ACE_HDX_LINE
  697. if (irqMask == 0) {
  698. /* Register interrupt handlers */
  699. if (NutRegisterIrqHandler(&sig_OUTPUT_COMPARE3A, AceOutComp3AInt, NULL)) {
  700. return -1;
  701. }
  702. AceTmr3Init();
  703. }
  704. #endif
  705. // get the appropriate irq handler
  706. irq = (IRQ_HANDLER *) pgm_read_word(&(atIrqDefs[dev->dev_irq].pvIrq));
  707. if (NutRegisterIrqHandler(irq, AceIrqHandler, dev)) {
  708. return -1;
  709. }
  710. // enable the interrupts
  711. pnPort = (uint8_t *) pgm_read_word(&(atIrqDefs[dev->dev_irq].pnIrqMskPort));
  712. nMask = pgm_read_byte(&(atIrqDefs[dev->dev_irq].nMask));
  713. *pnPort |= nMask;
  714. /* remember dcb of the recently initialized device */
  715. pIrqDev[dev->dev_irq] = dev;
  716. irqMask |= 0x01 << dev->dev_irq;
  717. }
  718. }
  719. /*
  720. * Set baudrate and handshake default. This will also
  721. * enable the ACE functions.
  722. */
  723. AceIOCtl(dev, ACE_SETSPEED, (void *) &baudrate);
  724. AceIOCtl(dev, ACE_SETDATABITS, (void *) &databits);
  725. AceIOCtl(dev, ACE_SETPARITY, (void *) &parity);
  726. AceIOCtl(dev, ACE_SETSTOPBITS, (void *) &stopbits);
  727. #ifdef ACE_HDX_LINE
  728. /* set HDX pin off by defalt */
  729. AceIOCtl(dev, ACE_SETFLOWCONTROL, (void *) &flowcontrol);
  730. #endif
  731. sbi(EIMSK, dev->dev_irq); /* dev->dev_irq to IRQ_INTx map should be used for a clean implementation but it looks like an overhead */
  732. AceEnable(dev->dev_base);
  733. return 0;
  734. }
  735. /*!
  736. * \brief Read from device.
  737. */
  738. int AceRead(NUTFILE * fp, void *buffer, int size)
  739. {
  740. int rc;
  741. NUTDEVICE *dev;
  742. IFSTREAM *ifs;
  743. ACEDCB *dcb;
  744. uint8_t elmode;
  745. uint8_t ch;
  746. uint8_t *cp = buffer;
  747. dev = fp->nf_dev;
  748. ifs = (IFSTREAM *) dev->dev_icb;
  749. dcb = dev->dev_dcb;
  750. if (dcb->dcb_modeflags & ACE_MF_COOKEDMODE)
  751. elmode = 1;
  752. else
  753. elmode = 0;
  754. /*
  755. * Call without data pointer discards receive buffer.
  756. */
  757. if (buffer == 0) {
  758. ifs->if_rd_idx = ifs->if_rx_idx;
  759. return 0;
  760. }
  761. /*
  762. * Get characters from receive buffer.
  763. */
  764. for (rc = 0; rc < size;) {
  765. /* if nothing has been received yet */
  766. if (ifs->if_rd_idx == ifs->if_rx_idx) {
  767. /* while incomming buffer is empty */
  768. while (ifs->if_rd_idx == ifs->if_rx_idx) {
  769. /* wait (timeout) for incomming data */
  770. if (AceInput(dev)) {
  771. /* if a timeout */
  772. return 0;
  773. }
  774. }
  775. }
  776. ch = ifs->if_rx_buf[ifs->if_rd_idx++];
  777. if (elmode && (ch == '\r' || ch == '\n')) {
  778. if ((ifs->if_last_eol == 0) || (ifs->if_last_eol == ch)) {
  779. ifs->if_last_eol = ch;
  780. *cp++ = '\n';
  781. rc++;
  782. }
  783. } else {
  784. ifs->if_last_eol = 0;
  785. *cp++ = ch;
  786. rc++;
  787. }
  788. }
  789. return rc;
  790. }
  791. /*!
  792. * \brief Write to device.
  793. */
  794. int AcePut(NUTDEVICE * dev, const void *buffer, int len, int pflg)
  795. {
  796. int rc;
  797. IFSTREAM *ifs;
  798. ACEDCB *dcb;
  799. const uint8_t *cp;
  800. uint8_t lbmode;
  801. uint8_t elmode;
  802. uint8_t ch;
  803. ifs = dev->dev_icb;
  804. dcb = dev->dev_dcb;
  805. if (dcb->dcb_modeflags & ACE_MF_LINEBUFFER)
  806. lbmode = 1;
  807. else
  808. lbmode = 0;
  809. if (dcb->dcb_modeflags & ACE_MF_COOKEDMODE)
  810. elmode = 1;
  811. else
  812. elmode = 0;
  813. /*
  814. * Call without data pointer starts transmission.
  815. */
  816. if (buffer == 0) {
  817. rc = AceFlush(dev);
  818. return rc;
  819. }
  820. /*
  821. * Put characters in transmit buffer.
  822. */
  823. cp = buffer;
  824. for (rc = 0; rc < len;) {
  825. if ((uint8_t) (ifs->if_wr_idx + 1) == ifs->if_tx_idx) {
  826. if (AceFlush(dev)) {
  827. return -1;
  828. }
  829. }
  830. ch = pflg ? PRG_RDB(cp) : *cp;
  831. if (elmode == 1 && ch == '\n') {
  832. elmode = 2;
  833. if (lbmode == 1)
  834. lbmode = 2;
  835. ch = '\r';
  836. } else {
  837. if (elmode == 2)
  838. elmode = 1;
  839. cp++;
  840. rc++;
  841. }
  842. ifs->if_tx_buf[ifs->if_wr_idx++] = ch;
  843. }
  844. if (lbmode > 1 || (dcb->dcb_modeflags & ACE_MF_NOBUFFER) != 0) {
  845. if (AceFlush(dev))
  846. rc = -1;
  847. }
  848. return rc;
  849. }
  850. int AceWrite(NUTFILE * fp, const void *buffer, int len)
  851. {
  852. return AcePut(fp->nf_dev, buffer, len, 0);
  853. }
  854. int AceWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  855. {
  856. return AcePut(fp->nf_dev, (const char *) buffer, len, 1);
  857. }
  858. /*!
  859. * \brief Open a device or file.
  860. */
  861. NUTFILE *AceOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  862. {
  863. NUTFILE *fp = NutHeapAlloc(sizeof(NUTFILE));
  864. ACEDCB *dcb;
  865. if (fp == 0)
  866. return NUTFILE_EOF;
  867. dcb = dev->dev_dcb;
  868. if (mode & _O_BINARY)
  869. dcb->dcb_modeflags &= ~ACE_MF_COOKEDMODE;
  870. else
  871. dcb->dcb_modeflags |= ACE_MF_COOKEDMODE;
  872. fp->nf_dev = dev;
  873. fp->nf_fcb = NULL;
  874. return fp;
  875. }
  876. /*!
  877. * \brief Close a device or file.
  878. */
  879. int AceClose(NUTFILE * fp)
  880. {
  881. NutHeapFree(fp);
  882. return 0;
  883. }
  884. /*!
  885. * \brief Request file size.
  886. */
  887. long AceSize(NUTFILE * fp)
  888. {
  889. NUTDEVICE *dev;
  890. IFSTREAM *ifs;
  891. dev = fp->nf_dev;
  892. ifs = (IFSTREAM *) dev->dev_icb;
  893. return ((uint8_t) (ifs->if_rx_idx - ifs->if_rd_idx));
  894. }
  895. #ifdef ACE_HDX_LINE
  896. static void AceTmr3Init(void)
  897. {
  898. /* TMR3 also runs in normal mode */
  899. TCCR3A &= ~(_BV(COM3A1) | _BV(COM3A0) | _BV(WGM31) | _BV(WGM30)); /* normal mode */
  900. TCCR3B &= ~(_BV(WGM33) | _BV(WGM32) | _BV(CS32) | _BV(CS31) | _BV(CS30));
  901. TCCR3B |= _BV(CS31) | _BV(CS30); /* f/64 (155200Hz - 1.75Hz) */
  902. /* TMR3 output compare A match interrupt disable */
  903. ETIMSK &= ~_BV(OCIE3A);
  904. }
  905. static void AceOutComp3AInt(void *arg)
  906. {
  907. NUTDEVICE *dev;
  908. ACEDCB *dcb;
  909. int i;
  910. unsigned int nextOcr = 0xffff, ocr;
  911. unsigned int timerOcrDiff;
  912. /* TMR3 stop counting */
  913. TCCR3B &= ~(_BV(CS31) | _BV(CS30));
  914. timerOcrDiff = (unsigned int /* modulo max */ )((unsigned int /* modulo max */ )TCNT3 - (unsigned int /* modulo max */ )OCR3A);
  915. /* Due to interrupt nesting and as TL interrupts are higher priority - disable them.
  916. * This routine cannot be interrupted by a cal to the AceAddHdxTime()
  917. */
  918. EIMSK &= ~irqMask;
  919. for (i = 0; i < 8; ++i) {
  920. for (dev = pIrqDev[i]; dev != NULL; dev = dcb->dev_next) {
  921. dcb = dev->dev_dcb;
  922. /* only if enabled */
  923. if (dcb->hdxOcrTime != 0) {
  924. if ((unsigned int /* modulo max */ )(dcb->hdxOcrTime - (unsigned int /* modulo max */ )OCR3A) <= timerOcrDiff) {
  925. dcb->hdxOcrTime = 0;
  926. ACE_HDX_RECEIVE(dev->dev_base);
  927. } else {
  928. ocr = (unsigned int /* modulo max */ )(dcb->hdxOcrTime - (unsigned int /* modulo max */ )TCNT3);
  929. if (ocr < nextOcr) {
  930. nextOcr = ocr;
  931. }
  932. }
  933. }
  934. }
  935. }
  936. if (nextOcr == 0xffff) {
  937. /* TMR3 output compare A match interrupt disable */
  938. ETIMSK &= ~_BV(OCIE3A);
  939. } else {
  940. OCR3A = nextOcr;
  941. /* start timer */
  942. TCCR3B |= _BV(CS31) | _BV(CS30); /* f/64 (155200Hz - 1.75Hz) */
  943. }
  944. /* Re-enable TL interrupt. */
  945. EIMSK |= irqMask;
  946. }
  947. static void AceAddHdxTime(ACEDCB * dcb)
  948. {
  949. /* TMR3 stop counting */
  950. TCCR3B &= ~(_BV(CS31) | _BV(CS30));
  951. /* if disabled */
  952. if ((ETIMSK & _BV(OCIE3A)) == 0) {
  953. /* initialize */
  954. OCR3A = 0;
  955. TCNT3 = 1;
  956. }
  957. /* set offset from current counter value */
  958. dcb->hdxOcrTime = (unsigned int /* modulo max */ )((unsigned int /* modulo max */ )TCNT3 + dcb->hdxByteTime);
  959. if (dcb->hdxOcrTime == 0) {
  960. dcb->hdxOcrTime = 1; /* 0 means disabled, one bit delay is not a problem as there is interrupt latency anyway */
  961. }
  962. if (dcb->hdxByteTime < (unsigned int /* modulo max */ )((unsigned int /* modulo max */ )OCR3A - (unsigned int /* modulo max */ )TCNT3)) {
  963. OCR3A = dcb->hdxOcrTime;
  964. }
  965. /* TMR3 output compare A match interrupt enable */
  966. ETIMSK |= _BV(OCIE3A);
  967. /* start timer */
  968. TCCR3B |= _BV(CS31) | _BV(CS30); /* f/64 (155200Hz - 1.75Hz) */
  969. }
  970. #endif
  971. /*@}*/