/lab4/mandelbrot/BSP/drivers/src/altera_up_avalon_rs232.c

https://github.com/jpwright/ece5760 · C · 145 lines · 85 code · 17 blank · 43 comment · 4 complexity · 23c5088e6df5596aa60c26e7ce040463 MD5 · raw file

  1. /******************************************************************************
  2. * *
  3. * License Agreement *
  4. * *
  5. * Copyright (c) 2007 Altera Corporation, San Jose, California, USA. *
  6. * All rights reserved. *
  7. * *
  8. * Permission is hereby granted, free of charge, to any person obtaining a *
  9. * copy of this software and associated documentation files (the "Software"), *
  10. * to deal in the Software without restriction, including without limitation *
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  12. * and/or sell copies of the Software, and to permit persons to whom the *
  13. * Software is furnished to do so, subject to the following conditions: *
  14. * *
  15. * The above copyright notice and this permission notice shall be included in *
  16. * all copies or substantial portions of the Software. *
  17. * *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  24. * DEALINGS IN THE SOFTWARE. *
  25. * *
  26. * This agreement shall be governed in all respects by the laws of the State *
  27. * of California and by the laws of the United States of America. *
  28. * *
  29. * Altera does not recommend, suggest or require that this reference design *
  30. * file be used in conjunction or combination with any other product. *
  31. * *
  32. ******************************************************************************/
  33. #include <errno.h>
  34. #include <priv/alt_file.h>
  35. #include "altera_up_avalon_rs232.h"
  36. #include "altera_up_avalon_rs232_regs.h"
  37. void alt_up_rs232_enable_read_interrupt(alt_up_rs232_dev *rs232)
  38. {
  39. alt_u32 ctrl_reg;
  40. ctrl_reg = IORD_ALT_UP_RS232_CONTROL(rs232->base);
  41. // set RE to 1 while maintaining other bits the same
  42. ctrl_reg |= ALT_UP_RS232_CONTROL_RE_MSK;
  43. IOWR_ALT_UP_RS232_CONTROL(rs232->base, ctrl_reg);
  44. }
  45. void alt_up_rs232_disable_read_interrupt(alt_up_rs232_dev *rs232)
  46. {
  47. alt_u32 ctrl_reg;
  48. ctrl_reg = IORD_ALT_UP_RS232_CONTROL(rs232->base);
  49. // set RE to 0 while maintaining other bits the same
  50. ctrl_reg &= ~ALT_UP_RS232_CONTROL_RE_MSK;
  51. IOWR_ALT_UP_RS232_CONTROL(rs232->base, ctrl_reg);
  52. }
  53. unsigned alt_up_rs232_get_used_space_in_read_FIFO(alt_up_rs232_dev *rs232)
  54. {
  55. alt_u16 ravail = 0;
  56. // we can only read the 16 bits for RAVAIL --- a read of DATA will discard the data
  57. // ravail = IORD_16DIRECT(IOADDR_ALT_UP_RS232_DATA(rs232->base), 2);
  58. ravail = IORD_ALT_UP_RS232_RAVAIL(rs232->base);
  59. // return ravail;
  60. return (ravail & ALT_UP_RS232_RAVAIL_MSK) >> ALT_UP_RS232_RAVAIL_OFST;
  61. }
  62. unsigned alt_up_rs232_get_available_space_in_write_FIFO(alt_up_rs232_dev *rs232)
  63. {
  64. alt_u32 ctrl_reg;
  65. ctrl_reg = IORD_ALT_UP_RS232_CONTROL(rs232->base);
  66. return (ctrl_reg & ALT_UP_RS232_CONTROL_WSPACE_MSK) >> ALT_UP_RS232_CONTROL_WSPACE_OFST;
  67. }
  68. int alt_up_rs232_check_parity(alt_u32 data_reg)
  69. {
  70. unsigned parity_error = (data_reg & ALT_UP_RS232_DATA_PE_MSK) >> ALT_UP_RS232_DATA_PE_OFST;
  71. return (parity_error ? -1 : 0);
  72. }
  73. int alt_up_rs232_write_data(alt_up_rs232_dev *rs232, alt_u8 data)
  74. {
  75. alt_u32 data_reg;
  76. data_reg = IORD_ALT_UP_RS232_DATA(rs232->base);
  77. // we can write directly without thinking about other bit fields for this
  78. // case ONLY, because only DATA field of the data register is writable
  79. IOWR_ALT_UP_RS232_DATA(rs232->base, (data>>ALT_UP_RS232_DATA_DATA_OFST) & ALT_UP_RS232_DATA_DATA_MSK);
  80. return 0;
  81. }
  82. int alt_up_rs232_read_data(alt_up_rs232_dev *rs232, alt_u8 *data, alt_u8 *parity_error)
  83. {
  84. alt_u32 data_reg;
  85. data_reg = IORD_ALT_UP_RS232_DATA(rs232->base);
  86. *data = (data_reg & ALT_UP_RS232_DATA_DATA_MSK) >> ALT_UP_RS232_DATA_DATA_OFST;
  87. *parity_error = alt_up_rs232_check_parity(data_reg);
  88. return (((data_reg & ALT_UP_RS232_DATA_RVALID_MSK) >> ALT_UP_RS232_DATA_RVALID_OFST) - 1);
  89. }
  90. int alt_up_rs232_read_fd (alt_fd* fd, char* ptr, int len)
  91. {
  92. alt_up_rs232_dev *rs232 = (alt_up_rs232_dev*)fd->dev;
  93. int count = 0;
  94. alt_u8 parity_error;
  95. while(len--)
  96. {
  97. if (alt_up_rs232_read_data(rs232, ptr++, &parity_error)==0)
  98. count++;
  99. else
  100. break;
  101. }
  102. return count;
  103. }
  104. int alt_up_rs232_write_fd (alt_fd* fd, const char* ptr, int len)
  105. {
  106. alt_up_rs232_dev *rs232 = (alt_up_rs232_dev*)fd->dev;
  107. int count = 0;
  108. while(len--)
  109. {
  110. if (alt_up_rs232_write_data(rs232, *ptr)==0)
  111. {
  112. count++;
  113. ptr++;
  114. }
  115. else
  116. break;
  117. }
  118. return count;
  119. }
  120. alt_up_rs232_dev* alt_up_rs232_open_dev(const char* name)
  121. {
  122. // find the device from the device list
  123. // (see altera_hal/HAL/inc/priv/alt_file.h
  124. // and altera_hal/HAL/src/alt_find_dev.c
  125. // for details)
  126. alt_up_rs232_dev *dev = (alt_up_rs232_dev*)alt_find_dev(name, &alt_dev_list);
  127. return dev;
  128. }