hal_nca9555.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include "hal_nca9555.h"
  2. #define READ_SDA1 GPIO_ReadInputPins(GPIO_PORT_C,GPIO_PIN_10)
  3. #define IIC_SDA1(n) (n?GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_10):GPIO_ResetPins(GPIO_PORT_C, GPIO_PIN_10))
  4. #define IIC_SCL1(n) (n?GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_11):GPIO_ResetPins(GPIO_PORT_C, GPIO_PIN_11))
  5. static void delay_us(uint16_t time)
  6. {
  7. uint16_t i;
  8. for(i=time;i>0;i--)
  9. {
  10. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  11. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  12. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  13. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  14. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  15. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  16. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  17. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  18. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  19. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  20. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  21. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  22. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  23. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  24. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  25. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  26. __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();
  27. __nop();__nop();__nop();
  28. }
  29. }
  30. static void iic1_init(void)
  31. {
  32. stc_gpio_init_t pstcGpioInit1;
  33. GPIO_StructInit(&pstcGpioInit1);
  34. pstcGpioInit1.u16PinDir = PIN_DIR_OUT;
  35. pstcGpioInit1.u16PinState = PIN_STAT_RST;
  36. GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit1);
  37. GPIO_Init(GPIO_PORT_C, GPIO_PIN_11,&pstcGpioInit1);
  38. GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_10);
  39. GPIO_SetPins(GPIO_PORT_C, GPIO_PIN_11);
  40. }
  41. static void SDA_IN1(void)
  42. {
  43. stc_gpio_init_t pstcGpioInit3;//iic的引脚
  44. pstcGpioInit3.u16PinDir = PIN_DIR_IN;
  45. GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit3);
  46. }
  47. static void SDA_OUT1(void)
  48. {
  49. stc_gpio_init_t pstcGpioInit3;
  50. pstcGpioInit3.u16PinDir = PIN_DIR_OUT;
  51. pstcGpioInit3.u16PinOutputType=PIN_OUT_TYPE_NMOS;
  52. GPIO_Init(GPIO_PORT_C, GPIO_PIN_10,&pstcGpioInit3);
  53. }
  54. static void iic_start(void)
  55. {
  56. SDA_OUT1();
  57. IIC_SDA1(1);
  58. IIC_SCL1(1);
  59. delay_us(5);
  60. IIC_SDA1(0);
  61. delay_us(5);
  62. IIC_SCL1(0);
  63. }
  64. static void iic_stop(void)
  65. {
  66. SDA_OUT1();
  67. IIC_SCL1(0);
  68. IIC_SDA1(0);
  69. delay_us(5);
  70. IIC_SCL1(1);
  71. IIC_SDA1(1);
  72. delay_us(5);
  73. }
  74. static void iic_ack(void)
  75. {
  76. SDA_OUT1();
  77. IIC_SCL1(0);
  78. IIC_SDA1(0);
  79. delay_us(5);
  80. IIC_SCL1(1);
  81. delay_us(5);
  82. IIC_SCL1(0);
  83. }
  84. static void iic_nack(void)
  85. {
  86. SDA_OUT1();
  87. IIC_SCL1(0);
  88. IIC_SDA1(1);
  89. delay_us(5);
  90. IIC_SCL1(1);
  91. delay_us(5);
  92. IIC_SCL1(0);
  93. }
  94. static uint8_t iic_wait_ack(void)
  95. {
  96. uint16_t t =200;
  97. SDA_OUT1();
  98. IIC_SDA1(1);delay_us(1);
  99. IIC_SCL1(1);delay_us(1);
  100. SDA_IN1();
  101. while(READ_SDA1)
  102. {
  103. t--;
  104. if(t==0)
  105. {
  106. iic_stop();
  107. return 1;
  108. }
  109. delay_us(1);
  110. }
  111. IIC_SCL1(0);
  112. return 0;
  113. }
  114. static void iic_send_byte(uint8_t byte)
  115. {
  116. uint8_t bit_cnt;
  117. SDA_OUT1();
  118. IIC_SCL1(0);
  119. for(bit_cnt=0;bit_cnt<8;bit_cnt++)
  120. {
  121. if(byte&0x80)
  122. IIC_SDA1(1);
  123. else
  124. IIC_SDA1(0);
  125. byte<<=1;
  126. delay_us(5);
  127. IIC_SCL1(1);
  128. delay_us(5);
  129. IIC_SCL1(0);
  130. }
  131. }
  132. static uint8_t iic_recive_byte(unsigned char ack)
  133. {
  134. uint8_t retc = 0;
  135. uint8_t bit_cnt;
  136. SDA_IN1();
  137. for(bit_cnt=0;bit_cnt<8;bit_cnt++)
  138. {
  139. IIC_SCL1(0);
  140. delay_us(5);
  141. IIC_SCL1(1);
  142. retc=retc<<1;
  143. if(READ_SDA1) retc++;
  144. delay_us(5);
  145. }
  146. return(retc);
  147. }
  148. void nca9555_init(void)
  149. {
  150. iic1_init();
  151. }
  152. uint8_t nca9555_write_byte(uint8_t addr, uint8_t command, uint8_t write_register_data)
  153. {
  154. uint8_t ret = 1;
  155. iic_start();
  156. iic_send_byte(addr);
  157. ret = iic_wait_ack();
  158. iic_send_byte(command);
  159. ret = iic_wait_ack();
  160. iic_send_byte(write_register_data);
  161. ret = iic_wait_ack();
  162. iic_stop();
  163. return SUCCESS;
  164. }
  165. /*
  166. * nca9555读取寄存器值
  167. *
  168. * addr 读取地址
  169. * read_register_data 要读取的寄存器
  170. * read_data 读取数据存放地址
  171. *
  172. *
  173. * 返回值:读取成功返回SUCCESS 失败返回ERROR
  174. *
  175. * */
  176. uint8_t nca9555_read_byte(uint8_t slave_num, uint8_t addr, uint8_t read_register_data, uint8_t *read_data)
  177. {
  178. uint8_t ret = 0;
  179. iic_start();
  180. iic_send_byte(slave_num);
  181. ret = iic_wait_ack();
  182. iic_send_byte(read_register_data);
  183. ret = iic_wait_ack();
  184. iic_start(); /* 开始接收数据 */
  185. iic_send_byte(addr);
  186. ret = iic_wait_ack();
  187. *read_data = iic_recive_byte(0);
  188. iic_stop();
  189. return SUCCESS;
  190. }
  191. /*
  192. * 设置指定GPIO的模式
  193. *
  194. * slave_num 需要操作的从机设备
  195. * gpio_port gpio端口 端口0/1
  196. * gpio_num 哪一个GPIO
  197. *
  198. * 返回值:void
  199. * */
  200. void nca9555_set_output_mode(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
  201. {
  202. uint8_t register_original_data = 0;
  203. if(gpio_port > 1 || gpio_num > 0x80) return;
  204. if(gpio_port == 0)
  205. {
  206. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER0, &register_original_data);
  207. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER0, register_original_data & (~gpio_num));
  208. }
  209. else if(gpio_port == 1)
  210. {
  211. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER1, &register_original_data);
  212. nca9555_write_byte( slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER1, register_original_data & (~gpio_num));
  213. }
  214. }
  215. /*
  216. * 设置GPIO输出状态
  217. *
  218. * slave_num 需要操作的从机设备
  219. * gpio_port gpio端口 端口0/1
  220. * gpio_num 哪一个GPIO
  221. * status 输出状态
  222. *
  223. * 返回值:void
  224. **/
  225. void nca9555_set_gpio_output_status(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num, uint8_t status)
  226. {
  227. uint8_t register_original_data = 0;
  228. if(gpio_port > 1 || gpio_num > 0x80) return;
  229. if(gpio_port == 0)
  230. {
  231. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, OUTPUT_PORT_REGISTER0, &register_original_data);
  232. if(status == 1)
  233. {
  234. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER0, register_original_data | gpio_num);
  235. }
  236. else
  237. {
  238. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER0, register_original_data & (~gpio_num));
  239. }
  240. }
  241. else if(gpio_port == 1)
  242. {
  243. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, OUTPUT_PORT_REGISTER1, &register_original_data);
  244. if(status == 1)
  245. {
  246. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER1, register_original_data | gpio_num);
  247. }
  248. else
  249. {
  250. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, OUTPUT_PORT_REGISTER1, register_original_data & (~gpio_num));
  251. }
  252. }
  253. }
  254. /*
  255. * 设置GPIO为输入模式
  256. *
  257. * slave_num 需要操作的从机设备
  258. * gpio_port gpio端口 端口0/1
  259. * gpio_num 哪一个GPIO
  260. *
  261. * 返回值:void
  262. **/
  263. void nca9555_set_input_mode(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
  264. {
  265. uint8_t register_original_data = 0;
  266. if(gpio_port > 1 || gpio_num > 0x80) return;
  267. if(gpio_port == 0)
  268. {
  269. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER0, &register_original_data);
  270. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER0, register_original_data & gpio_num);
  271. }
  272. else if(gpio_port == 1)
  273. {
  274. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, CONFIG_PORT_REGISTER1, &register_original_data);
  275. nca9555_write_byte(slave_num | HOST_WRITE_COMMAND, CONFIG_PORT_REGISTER1, register_original_data & gpio_num);
  276. }
  277. }
  278. /*
  279. * 获取GPIO状态
  280. *
  281. * slave_num 需要操作的从机设备
  282. * gpio_port gpio端口 端口0/1
  283. * gpio_num 哪一个GPIO
  284. *
  285. * 返回值:GPIO状态
  286. **/
  287. uint8_t nca9555_get_gpio_status(uint8_t slave_num, uint8_t gpio_port, uint8_t gpio_num)
  288. {
  289. uint8_t register_original_data = 0;
  290. uint8_t gpio_status = 0;
  291. if(gpio_port > 1 || gpio_num > 0x80)
  292. {
  293. return 2;
  294. }
  295. if(gpio_port == 0)
  296. {
  297. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, INPUT_PORT_REGISTER0, &register_original_data);
  298. }
  299. else if(gpio_port == 1)
  300. {
  301. nca9555_read_byte(slave_num, slave_num | HOST_READ_COMMAND, INPUT_PORT_REGISTER1, &register_original_data);
  302. }
  303. switch(gpio_num)
  304. {
  305. case 0x01: gpio_status = register_original_data & gpio_num;break;
  306. case 0x02: gpio_status = (register_original_data & gpio_num) >> 1;break;
  307. case 0x04: gpio_status = (register_original_data & gpio_num) >> 2;break;
  308. case 0x08: gpio_status = (register_original_data & gpio_num) >> 3;break;
  309. case 0x10: gpio_status = (register_original_data & gpio_num) >> 4;break;
  310. case 0x20: gpio_status = (register_original_data & gpio_num) >> 5;break;
  311. case 0x40: gpio_status = (register_original_data & gpio_num) >> 6;break;
  312. case 0x80: gpio_status = (register_original_data & gpio_num) >> 7;break;
  313. default: break;
  314. }
  315. return gpio_status;
  316. }
  317. #include "FreeRTOS.h"
  318. #include "FreeRTOSConfig.h"
  319. #include "task.h"
  320. #include "hal_sc7a20.h"
  321. int32_t hc32_flash_write(uint32_t write_addr,uint8_t *buffer,uint32_t number);
  322. void nca9555_task(void *argv)
  323. {
  324. nca9555_set_output_mode(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03);
  325. uint8_t tmp[256] = "asdasd00";
  326. while(1)
  327. {
  328. //hc32_flash_write(0X22000, tmp, 232);
  329. //nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,HIGH);
  330. vTaskDelay(500);
  331. //nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,LOW);
  332. vTaskDelay(18000);
  333. //hc32_flash_write(0X22000, tmp, 256);
  334. }
  335. }