Mercurial > public > hwos_code
comparison src/i2c.asm @ 650:bc214815deb2
3.19/10.75 release
author | heinrichsweikamp |
---|---|
date | Sun, 28 Aug 2022 13:13:38 +0200 |
parents | ef2ed7e3a895 |
children | 75e90cd0c2c3 |
comparison
equal
deleted
inserted
replaced
649:ef2ed7e3a895 | 650:bc214815deb2 |
---|---|
958 bra WaitMSSP_loop ; NO - loop | 958 bra WaitMSSP_loop ; NO - loop |
959 bcf PIR1,SSP1IF ; YES - clear TX completion flag | 959 bcf PIR1,SSP1IF ; YES - clear TX completion flag |
960 return ; - done | 960 return ; - done |
961 | 961 |
962 | 962 |
963 ;----------------------------------------------------------------------------- | |
964 ; Helper Function - Master NOT acknowledge and Stop | |
965 ; | |
966 I2C_MasterNotAckStop: | |
967 bsf SSP1CON2,ACKDT ; set ACKDT flag | |
968 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
969 rcall WaitMSSP ; wait for TX to complete | |
970 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
971 | |
972 bsf SSP1CON2,PEN ; stop condition | |
973 bra WaitMSSP ; wait for TX to complete | |
974 | |
963 ;----------------------------------------------------------------------------- | 975 ;----------------------------------------------------------------------------- |
964 ; Helper Function - check for Acknowledge by Receiver | 976 ; Helper Function - check for Acknowledge by Receiver |
965 ; | 977 ; |
966 I2C_Check_ACK: | 978 I2C_Check_ACK: |
967 btfss SSP1CON2,ACKSTAT ; ACK received from slave? | 979 btfss SSP1CON2,ACKSTAT ; ACK received from slave? |
1111 rcall WaitMSSP ; wait for TX to complete | 1123 rcall WaitMSSP ; wait for TX to complete |
1112 movff SSP1BUF,xA+1 ; copy received byte to xA+1 | 1124 movff SSP1BUF,xA+1 ; copy received byte to xA+1 |
1113 bsf SSP1CON2,RCEN ; enable receive mode | 1125 bsf SSP1CON2,RCEN ; enable receive mode |
1114 rcall WaitMSSP ; wait for TX to complete | 1126 rcall WaitMSSP ; wait for TX to complete |
1115 movff SSP1BUF,xA+0 ; copy received byte to xA+0 | 1127 movff SSP1BUF,xA+0 ; copy received byte to xA+0 |
1116 bsf SSP1CON2,ACKDT ; set ACKDT flag | 1128 |
1117 bsf SSP1CON2,ACKEN ; master NOT acknowledge | 1129 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1118 rcall WaitMSSP ; wait for TX to complete | |
1119 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1120 | |
1121 bsf SSP1CON2,PEN ; stop condition | |
1122 rcall WaitMSSP ; wait for TX to complete | |
1123 | 1130 |
1124 ; convert voltage from raw value to Volt | 1131 ; convert voltage from raw value to Volt |
1125 MOVLI .6000,xB ; load conversion multiplicand into xB | 1132 MOVLI .6000,xB ; load conversion multiplicand into xB |
1126 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand | 1133 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand |
1127 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % | 1134 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % |
1145 rcall WaitMSSP ; wait for TX to complete | 1152 rcall WaitMSSP ; wait for TX to complete |
1146 movff SSP1BUF,xA+1 ; store raw temperature, high byte | 1153 movff SSP1BUF,xA+1 ; store raw temperature, high byte |
1147 bsf SSP1CON2,RCEN ; enable receive mode | 1154 bsf SSP1CON2,RCEN ; enable receive mode |
1148 rcall WaitMSSP ; wait for TX to complete | 1155 rcall WaitMSSP ; wait for TX to complete |
1149 movff SSP1BUF,xA+0 ; store raw temperature, low byte | 1156 movff SSP1BUF,xA+0 ; store raw temperature, low byte |
1150 bsf SSP1CON2,ACKDT ; set ACKDT flag | 1157 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1151 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1152 rcall WaitMSSP ; wait for TX to complete | |
1153 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1154 | |
1155 bsf SSP1CON2,PEN ; stop condition | |
1156 rcall WaitMSSP ; wait for TX to complete | |
1157 | 1158 |
1158 ; convert temperature from raw value to Kelvin | 1159 ; convert temperature from raw value to Kelvin |
1159 MOVLI .6000,xB ; load conversion multiplicand into xB | 1160 MOVLI .6000,xB ; load conversion multiplicand into xB |
1160 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand | 1161 call mult16x16 ; xC = xA * xB -> multiply raw value in xA with conversion multiplicand |
1161 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % | 1162 ; divide by 65536 instead of 65535, introducing an error of 65536/65535 = 0.002 % |
1215 rcall WaitMSSP ; wait for TX to complete | 1216 rcall WaitMSSP ; wait for TX to complete |
1216 | 1217 |
1217 bsf SSP1CON2,RCEN ; enable receive mode | 1218 bsf SSP1CON2,RCEN ; enable receive mode |
1218 rcall WaitMSSP ; wait for TX to complete | 1219 rcall WaitMSSP ; wait for TX to complete |
1219 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0 | 1220 movff SSP1BUF,sub_a+0 ; copy received byte to sub_a+0 |
1220 bsf SSP1CON2,ACKDT ; set ACKDT flag | 1221 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1221 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1222 rcall WaitMSSP ; wait for TX to complete | |
1223 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1224 | |
1225 bsf SSP1CON2,PEN ; stop condition | |
1226 rcall WaitMSSP ; wait for TX to complete | |
1227 | 1222 |
1228 btfsc gauge_status_byte,0 ; UVLO event ? | 1223 btfsc gauge_status_byte,0 ; UVLO event ? |
1229 rcall lt2942_init_again ; YES - do an re-initialization | 1224 rcall lt2942_init_again ; YES - do an re-initialization |
1230 MOVII sub_a,battery_accumulated_charge ; save raw value | 1225 MOVII sub_a,battery_accumulated_charge ; save raw value |
1231 | 1226 |
1397 | 1392 |
1398 ; last byte in read from RX circuity always with a NACK! | 1393 ; last byte in read from RX circuity always with a NACK! |
1399 bsf SSP1CON2,RCEN ; enable receive mode | 1394 bsf SSP1CON2,RCEN ; enable receive mode |
1400 rcall WaitMSSP ; wait for TX to complete | 1395 rcall WaitMSSP ; wait for TX to complete |
1401 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor | 1396 movff SSP1BUF,rx_firmware_cur_minor ; store as firmware version, minor |
1402 bsf SSP1CON2,ACKDT ; set ACKDT flag | 1397 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1403 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1404 rcall WaitMSSP ; wait for TX to complete | |
1405 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1406 bsf SSP1CON2,PEN ; stop condition | |
1407 rcall WaitMSSP ; wait for TX to complete | |
1408 | 1398 |
1409 ; wait for TR module becoming ready | 1399 ; wait for TR module becoming ready |
1410 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common | 1400 movff rx_firmware_cur_minor,i2c_temp1 ; copy minor firmware version to bank common |
1411 movlw .147 ; code for not ready, minor | 1401 movlw .147 ; code for not ready, minor |
1412 cpfseq i2c_temp1 ; equal? | 1402 cpfseq i2c_temp1 ; equal? |
1468 bra I2C_get_tankdata_loop_read ; NO - loop | 1458 bra I2C_get_tankdata_loop_read ; NO - loop |
1469 ; read last byte without ACK | 1459 ; read last byte without ACK |
1470 bsf SSP1CON2,RCEN ; enable receive mode | 1460 bsf SSP1CON2,RCEN ; enable receive mode |
1471 rcall WaitMSSP ; wait for TX to complete | 1461 rcall WaitMSSP ; wait for TX to complete |
1472 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer | 1462 movff SSP1BUF,POSTINC2 ; copy received byte to the rx buffer |
1473 bsf SSP1CON2,ACKDT ; set ACKDT flag | 1463 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1474 bsf SSP1CON2,ACKEN ; master NOT acknowledge | |
1475 rcall WaitMSSP ; wait for TX to complete | |
1476 bcf SSP1CON2,ACKDT ; reset ACKDT flag | |
1477 bsf SSP1CON2,PEN ; stop condition | |
1478 rcall WaitMSSP ; wait for TX to complete | |
1479 return ; done | 1464 return ; done |
1480 | 1465 |
1481 | 1466 |
1482 | 1467 |
1483 ;----------------------------------------------------------------------------- | 1468 ;----------------------------------------------------------------------------- |
1539 bsf SSP1CON2, RCEN ; YES - enable receive mode | 1524 bsf SSP1CON2, RCEN ; YES - enable receive mode |
1540 rcall WaitMSSP ; - wait for TX to complete | 1525 rcall WaitMSSP ; - wait for TX to complete |
1541 movf SSP1BUF,W ; - get 64th byte | 1526 movf SSP1BUF,W ; - get 64th byte |
1542 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal? | 1527 cpfseq POSTINC2 ; - compare read-back byte with sent byte, equal? |
1543 bsf i2c_error_flag ; NO - not equal, set error flag | 1528 bsf i2c_error_flag ; NO - not equal, set error flag |
1544 bsf SSP1CON2,ACKDT ; - set ACKDT flag | 1529 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1545 bsf SSP1CON2,ACKEN ; - master NOT acknowledge | |
1546 rcall WaitMSSP ; - wait for TX to complete | |
1547 bcf SSP1CON2,ACKDT ; - reset ACKDT flag | |
1548 ; stop | |
1549 bsf SSP1CON2,PEN ; stop condition | |
1550 rcall WaitMSSP ; wait for TX to complete | |
1551 WAITMS .1 | 1530 WAITMS .1 |
1552 ; address commit | 1531 ; address commit |
1553 bsf SSP1CON2,SEN ; start condition | 1532 bsf SSP1CON2,SEN ; start condition |
1554 rcall WaitMSSP ; wait for TX to complete | 1533 rcall WaitMSSP ; wait for TX to complete |
1555 movlw 0x50 ; address byte + write bit | 1534 movlw 0x50 ; address byte + write bit |
1676 | 1655 |
1677 return | 1656 return |
1678 | 1657 |
1679 global I2C_get_press_val_MS5837 | 1658 global I2C_get_press_val_MS5837 |
1680 I2C_get_press_val_MS5837: | 1659 I2C_get_press_val_MS5837: |
1681 bsf i2c_busy_pressure ; reading new pressure | |
1682 | |
1683 bsf SSP1CON2,SEN ; start condition | 1660 bsf SSP1CON2,SEN ; start condition |
1684 rcall WaitMSSP ; wait for TX to complete | 1661 rcall WaitMSSP ; wait for TX to complete |
1685 movlw 0xEC ; address byte + write bit | 1662 movlw 0xEC ; address byte + write bit |
1686 movff WREG,i2c_error_vault+0 ; Store address | 1663 movff WREG,i2c_error_vault+0 ; Store address |
1687 rcall I2C_TX ; send byte | 1664 rcall I2C_TX ; send byte |
1694 movff WREG,i2c_error_vault+0 ; Store address | 1671 movff WREG,i2c_error_vault+0 ; Store address |
1695 movwf SSP1BUF ; control byte | 1672 movwf SSP1BUF ; control byte |
1696 rcall WaitMSSP ; wait for TX to complete | 1673 rcall WaitMSSP ; wait for TX to complete |
1697 rcall I2C_Check_ACK ; check for acknowledge by receiver | 1674 rcall I2C_Check_ACK ; check for acknowledge by receiver |
1698 | 1675 |
1676 bsf i2c_busy_pressure ; reading new pressure | |
1699 bsf SSP1CON2,RCEN ; enable receive mode | 1677 bsf SSP1CON2,RCEN ; enable receive mode |
1700 rcall WaitMSSP ; wait for reception and return | 1678 rcall WaitMSSP ; wait for reception and return |
1701 movff SSP1BUF,D1_buffer+2 ; Upper byte | 1679 movff SSP1BUF,D1_buffer+2 ; Upper byte |
1702 bsf SSP1CON2,ACKEN ; master acknowledge | 1680 bsf SSP1CON2,ACKEN ; master acknowledge |
1703 rcall WaitMSSP ; wait for TX to complete | 1681 rcall WaitMSSP ; wait for TX to complete |
1707 bsf SSP1CON2,ACKEN ; master acknowledge | 1685 bsf SSP1CON2,ACKEN ; master acknowledge |
1708 rcall WaitMSSP ; wait for TX to complete | 1686 rcall WaitMSSP ; wait for TX to complete |
1709 bsf SSP1CON2,RCEN ; enable receive mode | 1687 bsf SSP1CON2,RCEN ; enable receive mode |
1710 rcall WaitMSSP ; wait for reception | 1688 rcall WaitMSSP ; wait for reception |
1711 movff SSP1BUF,D1_buffer+0 ; Low byte | 1689 movff SSP1BUF,D1_buffer+0 ; Low byte |
1712 bsf SSP1CON2,ACKEN ; master acknowledge | 1690 |
1713 rcall WaitMSSP ; wait for TX to complete | 1691 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1714 bsf SSP1CON2,PEN ; stop condition | 1692 bcf i2c_busy_pressure ; reading new pressure done. |
1715 rcall WaitMSSP ; wait for TX to complete | 1693 |
1716 | |
1717 ; Start temperature measurement | 1694 ; Start temperature measurement |
1718 bsf SSP1CON2,SEN ; start condition | 1695 bsf SSP1CON2,SEN ; start condition |
1719 rcall WaitMSSP ; wait for TX to complete | 1696 rcall WaitMSSP ; wait for TX to complete |
1720 movlw 0xEC ; address byte + write bit | 1697 movlw 0xEC ; address byte + write bit |
1721 rcall I2C_TX ; send byte | 1698 rcall I2C_TX ; send byte |
1722 movlw 0x58 ; OSR=4096, type=D2 | 1699 movlw 0x58 ; OSR=4096, type=D2 |
1723 rcall I2C_TX ; send byte | 1700 rcall I2C_TX ; send byte |
1724 bsf SSP1CON2,PEN ; stop condition | 1701 bsf SSP1CON2,PEN ; stop condition |
1725 rcall WaitMSSP ; wait for TX to complete | 1702 rcall WaitMSSP ; wait for TX to complete |
1726 bcf ms5837_state ; =0: result of temperature will be in the ADC | 1703 bcf ms5837_state ; =0: result of temperature will be in the ADC |
1727 bcf i2c_busy_pressure ; reading new pressure | |
1728 return | 1704 return |
1729 | 1705 |
1730 global I2C_get_temp_val_MS5837 | 1706 global I2C_get_temp_val_MS5837 |
1731 I2C_get_temp_val_MS5837: | 1707 I2C_get_temp_val_MS5837: |
1732 bsf i2c_busy_temperature ; reading new temperature | |
1733 | |
1734 bsf SSP1CON2,SEN ; start condition | 1708 bsf SSP1CON2,SEN ; start condition |
1735 rcall WaitMSSP ; wait for TX to complete | 1709 rcall WaitMSSP ; wait for TX to complete |
1736 movlw 0xEC ; address byte + write bit | 1710 movlw 0xEC ; address byte + write bit |
1737 movff WREG,i2c_error_vault+0 ; Store address | 1711 movff WREG,i2c_error_vault+0 ; Store address |
1738 rcall I2C_TX ; send byte | 1712 rcall I2C_TX ; send byte |
1745 movff WREG,i2c_error_vault+0 ; Store address | 1719 movff WREG,i2c_error_vault+0 ; Store address |
1746 movwf SSP1BUF ; control byte | 1720 movwf SSP1BUF ; control byte |
1747 rcall WaitMSSP ; wait for TX to complete | 1721 rcall WaitMSSP ; wait for TX to complete |
1748 rcall I2C_Check_ACK ; check for acknowledge by receiver | 1722 rcall I2C_Check_ACK ; check for acknowledge by receiver |
1749 | 1723 |
1724 bsf i2c_busy_temperature ; reading new temperature | |
1750 bsf SSP1CON2,RCEN ; enable receive mode | 1725 bsf SSP1CON2,RCEN ; enable receive mode |
1751 rcall WaitMSSP ; wait for reception and return | 1726 rcall WaitMSSP ; wait for reception and return |
1752 movff SSP1BUF,D2_buffer+2 ; Upper byte | 1727 movff SSP1BUF,D2_buffer+2 ; Upper byte |
1753 bsf SSP1CON2,ACKEN ; master acknowledge | 1728 bsf SSP1CON2,ACKEN ; master acknowledge |
1754 rcall WaitMSSP ; wait for TX to complete | 1729 rcall WaitMSSP ; wait for TX to complete |
1758 bsf SSP1CON2,ACKEN ; master acknowledge | 1733 bsf SSP1CON2,ACKEN ; master acknowledge |
1759 rcall WaitMSSP ; wait for TX to complete | 1734 rcall WaitMSSP ; wait for TX to complete |
1760 bsf SSP1CON2,RCEN ; enable receive mode | 1735 bsf SSP1CON2,RCEN ; enable receive mode |
1761 rcall WaitMSSP ; wait for reception | 1736 rcall WaitMSSP ; wait for reception |
1762 movff SSP1BUF,D2_buffer+0 ; Low byte | 1737 movff SSP1BUF,D2_buffer+0 ; Low byte |
1763 bsf SSP1CON2,ACKEN ; master acknowledge | 1738 |
1764 rcall WaitMSSP ; wait for TX to complete | 1739 rcall I2C_MasterNotAckStop ; Master NOT acknowledge and Stop |
1765 bsf SSP1CON2,PEN ; stop condition | 1740 bcf i2c_busy_temperature ; reading new temperature done. |
1766 rcall WaitMSSP ; wait for TX to complete | 1741 |
1767 | |
1768 ; Start pressure measurement | 1742 ; Start pressure measurement |
1769 bsf SSP1CON2,SEN ; start condition | 1743 bsf SSP1CON2,SEN ; start condition |
1770 rcall WaitMSSP ; wait for TX to complete | 1744 rcall WaitMSSP ; wait for TX to complete |
1771 movlw 0xEC ; address byte + write bit | 1745 movlw 0xEC ; address byte + write bit |
1772 rcall I2C_TX ; send byte | 1746 rcall I2C_TX ; send byte |
1773 movlw 0x48 ; OSR=4096, type=D1 | 1747 movlw 0x48 ; OSR=4096, type=D1 |
1774 rcall I2C_TX ; send byte | 1748 rcall I2C_TX ; send byte |
1775 bsf SSP1CON2,PEN ; stop condition | 1749 bsf SSP1CON2,PEN ; stop condition |
1776 rcall WaitMSSP ; wait for TX to complete | 1750 rcall WaitMSSP ; wait for TX to complete |
1777 bsf ms5837_state ; =0: result of pressure will be in the ADC | 1751 bsf ms5837_state ; =0: result of pressure will be in the ADC |
1778 bcf i2c_busy_temperature ; reading new temperature | |
1779 return | 1752 return |
1780 | 1753 |
1781 | 1754 |
1782 ;----------------------------------------------------------------------------- | 1755 ;----------------------------------------------------------------------------- |
1783 ; I2C Bus error checker | 1756 ; I2C Bus error checker |